Skip to contentSkip to conversation

Re-submit of the erase command addition plus removal of MMC_IOC_MULTI_CMD ifndef for erase. Author=Kimito Sakata <[email protected]>

PATCH In review [email protected] 4 participants 9 messages Apr 1, 2021 – Apr 22, 2021 MMC, SD and SDIO

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 Re-submit of the erase command addition plus removal of MMC_IOC_MULTI_CMD ifndef for erase. Author=Kimito Sakata <[email protected]>

From: Kimito Sakata <[email protected]>

---
 mmc.c      |   8 ++++
 mmc.h      |  13 +++++-
 mmc_cmds.c | 129 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 mmc_cmds.h |   1 +
 4 files changed, 150 insertions(+), 1 deletion(-)
mmc.c +8 -0
index f3d724b..eb2638b 100644
@@ -229,6 +229,14 @@ static struct Command commands[] = {
 		"Run Field Firmware Update with <image name> on <device>.\n",
 	  NULL
 	},
+	{ do_erase, -4,
+	"erase", "<type> " "<start address> " "<end address> " "<device>\n"
+		"Send Erase CMD38 with specific argument to the <device>\n\n"
+		"NOTE!: This will delete all user data in the specified region of the device\n"
+		"<type> must be: legacy | discard | secure-erase | "
+		"secure-trim1 | secure-trim2 | trim \n",
+	NULL
+	},
 	{ 0, 0, 0, 0 }
 };
 
mmc.h +12 -1
index 5754a9d..e9766d7 100644
@@ -35,7 +35,15 @@
 #define MMC_SET_WRITE_PROT	28    /* ac   [31:0] data addr   R1b */
 #define MMC_CLEAR_WRITE_PROT	29    /* ac   [31:0] data addr   R1b */
 #define MMC_SEND_WRITE_PROT_TYPE 31   /* ac   [31:0] data addr   R1  */
-
+#define MMC_ERASE_GROUP_START	35    /* ac   [31:0] data addr   R1  */
+#define MMC_ERASE_GROUP_END	36    /* ac   [31:0] data addr   R1  */
+#define MMC_ERASE		38    /* ac   [31] Secure request
+					      [30:16] set to 0
+					      [15] Force Garbage Collect request
+					      [14:2] set to 0
+					      [1] Discard Enable
+					      [0] Identify Write Blocks for
+					      Erase (or TRIM Enable)  R1b */
 /*
  * EXT_CSD fields
  */
@@ -62,6 +70,7 @@
 #define EXT_CSD_CACHE_SIZE_2		251
 #define EXT_CSD_CACHE_SIZE_1		250
 #define EXT_CSD_CACHE_SIZE_0		249
+#define EXT_CSD_SEC_FEATURE_SUPPORT	231
 #define EXT_CSD_BOOT_INFO		228	/* R/W */
 #define EXT_CSD_HC_ERASE_GRP_SIZE	224
 #define EXT_CSD_HC_WP_GRP_SIZE		221
@@ -190,6 +199,8 @@
 #define EXT_CSD_REV_V4_2		2
 #define EXT_CSD_REV_V4_1		1
 #define EXT_CSD_REV_V4_0		0
+#define EXT_CSD_SEC_GB_CL_EN		(1<<4)
+#define EXT_CSD_SEC_ER_EN		(1<<0)
 
 
 /* From kernel linux/mmc/core.h */
mmc_cmds.c +129 -0

135 lines not on this page · raw message

mmc_cmds.h +1 -0
index 9d3246c..8331ab2 100644
@@ -45,3 +45,4 @@ int do_ffu(int nargs, char **argv);
 int do_read_scr(int argc, char **argv);
 int do_read_cid(int argc, char **argv);
 int do_read_csd(int argc, char **argv);
+int do_erase(int nargs, char **argv);
Signature · 1 line
2.24.1 (Apple Git-126)
#1 lore raw

+ Avri

Quoted 4 lines · On Fri, 2 Apr 2021 at 01:02, <[email protected]> wrote:

From: Kimito Sakata <[email protected]>

This looks okay to me, but I have looped in Avri who might have some comments.

Kind regards
Uffe

Quoted 216 lines
---
 mmc.c      |   8 ++++
 mmc.h      |  13 +++++-
 mmc_cmds.c | 129 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 mmc_cmds.h |   1 +
 4 files changed, 150 insertions(+), 1 deletion(-)
mmc.c +8 -0
index f3d724b..eb2638b 100644
@@ -229,6 +229,14 @@ static struct Command commands[] = {
                "Run Field Firmware Update with <image name> on <device>.\n",
          NULL
        },
+       { do_erase, -4,
+       "erase", "<type> " "<start address> " "<end address> " "<device>\n"
+               "Send Erase CMD38 with specific argument to the <device>\n\n"
+               "NOTE!: This will delete all user data in the specified region of the device\n"
+               "<type> must be: legacy | discard | secure-erase | "
+               "secure-trim1 | secure-trim2 | trim \n",
+       NULL
+       },
        { 0, 0, 0, 0 }
 };
 
mmc.h +12 -1
index 5754a9d..e9766d7 100644
@@ -35,7 +35,15 @@
 #define MMC_SET_WRITE_PROT     28    /* ac   [31:0] data addr   R1b */
 #define MMC_CLEAR_WRITE_PROT   29    /* ac   [31:0] data addr   R1b */
 #define MMC_SEND_WRITE_PROT_TYPE 31   /* ac   [31:0] data addr   R1  */
-
+#define MMC_ERASE_GROUP_START  35    /* ac   [31:0] data addr   R1  */
+#define MMC_ERASE_GROUP_END    36    /* ac   [31:0] data addr   R1  */
+#define MMC_ERASE              38    /* ac   [31] Secure request
+                                             [30:16] set to 0
+                                             [15] Force Garbage Collect request
+                                             [14:2] set to 0
+                                             [1] Discard Enable
+                                             [0] Identify Write Blocks for
+                                             Erase (or TRIM Enable)  R1b */
 /*
  * EXT_CSD fields
  */
@@ -62,6 +70,7 @@
 #define EXT_CSD_CACHE_SIZE_2           251
 #define EXT_CSD_CACHE_SIZE_1           250
 #define EXT_CSD_CACHE_SIZE_0           249
+#define EXT_CSD_SEC_FEATURE_SUPPORT    231
 #define EXT_CSD_BOOT_INFO              228     /* R/W */
 #define EXT_CSD_HC_ERASE_GRP_SIZE      224
 #define EXT_CSD_HC_WP_GRP_SIZE         221
@@ -190,6 +199,8 @@
 #define EXT_CSD_REV_V4_2               2
 #define EXT_CSD_REV_V4_1               1
 #define EXT_CSD_REV_V4_0               0
+#define EXT_CSD_SEC_GB_CL_EN           (1<<4)
+#define EXT_CSD_SEC_ER_EN              (1<<0)
 
 
 /* From kernel linux/mmc/core.h */
mmc_cmds.c +129 -0
index 6c24cea..3e36ff2 100644
@@ -2514,6 +2514,135 @@ int do_cache_dis(int nargs, char **argv)
        return do_cache_ctrl(0, nargs, argv);
 }
 
+static int erase(int dev_fd, __u32 argin, __u32 start, __u32 end)
+{
+       int ret = 0;
+       struct mmc_ioc_multi_cmd *multi_cmd;
+
+       multi_cmd = calloc(1, sizeof(struct mmc_ioc_multi_cmd) +
+                          3 * sizeof(struct mmc_ioc_cmd));
+       if (!multi_cmd) {
+               perror("Failed to allocate memory");
+               return -ENOMEM;
+       }
+
+       multi_cmd->num_of_cmds = 3;
+       /* Set erase start address */
+       multi_cmd->cmds[0].opcode = MMC_ERASE_GROUP_START;
+       multi_cmd->cmds[0].arg = start;
+       multi_cmd->cmds[0].flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
+       multi_cmd->cmds[0].write_flag = 1;
+
+       /* Set erase end address */
+       multi_cmd->cmds[1].opcode = MMC_ERASE_GROUP_END;
+       multi_cmd->cmds[1].arg = end;
+       multi_cmd->cmds[1].flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
+       multi_cmd->cmds[1].write_flag = 1;
+
+       /* Send Erase Command */
+       multi_cmd->cmds[2].opcode = MMC_ERASE;
+       multi_cmd->cmds[2].arg = argin;
+       multi_cmd->cmds[2].cmd_timeout_ms = 300*255*255;
+       multi_cmd->cmds[2].flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
+       multi_cmd->cmds[2].write_flag = 1;
+
+       /* send erase cmd with multi-cmd */
+       ret = ioctl(dev_fd, MMC_IOC_MULTI_CMD, multi_cmd);
+       if (ret)
+               perror("Erase multi-cmd ioctl");
+
+       free(multi_cmd);
+       return ret;
+}
+
+int do_erase(int nargs, char **argv)
+{
+       int dev_fd, ret;
+       char *print_str;
+       char **eptr = NULL;
+       __u8 ext_csd[512], checkup_mask = 0;
+       __u32 arg, start, end;
+
+       if (nargs != 5) {
+               fprintf(stderr, "Usage: erase <type> <start addr> <end addr> </path/to/mmcblkX>\n");
+               exit(1);
+       }
+
+       if (strstr(argv[2], "0x") || strstr(argv[2], "0X"))
+               start = strtol(argv[2], eptr, 16);
+       else
+               start = strtol(argv[2], eptr, 10);
+
+       if (strstr(argv[3], "0x") || strstr(argv[3], "0X"))
+               end = strtol(argv[3], eptr, 16);
+       else
+               end = strtol(argv[3], eptr, 10);
+
+       if (end < start) {
+               fprintf(stderr, "erase start [0x%08x] > erase end [0x%08x]\n",
+                       start, end);
+               exit(1);
+       }
+
+       if (strcmp(argv[1], "legacy") == 0) {
+               arg = 0x00000000;
+               print_str = "Legacy Erase";
+       } else if (strcmp(argv[1], "discard") == 0) {
+               arg = 0x00000003;
+               print_str = "Discard";
+       } else if (strcmp(argv[1], "secure-erase") == 0) {
+               print_str = "Secure Erase";
+               checkup_mask = EXT_CSD_SEC_ER_EN;
+               arg = 0x80000000;
+       } else if (strcmp(argv[1], "secure-trim1") == 0) {
+               print_str = "Secure Trim Step 1";
+               checkup_mask = EXT_CSD_SEC_ER_EN | EXT_CSD_SEC_GB_CL_EN;
+               arg = 0x80000001;
+       } else if (strcmp(argv[1], "secure-trim2") == 0) {
+               print_str = "Secure Trim Step 2";
+               checkup_mask = EXT_CSD_SEC_ER_EN | EXT_CSD_SEC_GB_CL_EN;
+               arg = 0x80008000;
+       } else if (strcmp(argv[1], "trim") == 0) {
+               print_str = "Trim";
+               checkup_mask = EXT_CSD_SEC_GB_CL_EN;
+               arg = 0x00000001;
+       } else {
+               fprintf(stderr, "Unknown erase type: %s\n", argv[1]);
+               exit(1);
+       }
+
+       dev_fd = open(argv[4], O_RDWR);
+       if (dev_fd < 0) {
+               perror(argv[4]);
+               exit(1);
+       }
+
+       if (checkup_mask) {
+               ret = read_extcsd(dev_fd, ext_csd);
+               if (ret) {
+                       fprintf(stderr, "Could not read EXT_CSD from %s\n",
+                               argv[4]);
+                       goto out;
+               }
+               if ((checkup_mask & ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) !=
+                                                               checkup_mask) {
+                       fprintf(stderr, "%s is not supported in %s\n",
+                               print_str, argv[4]);
+                       ret = -ENOTSUP;
+                       goto out;
+               }
+
+       }
+       printf("Executing %s from 0x%08x to 0x%08x\n", print_str, start, end);
+
+       ret = erase(dev_fd, arg, start, end);
+out:
+       printf(" %s %s!\n\n", print_str, ret ? "Failed" : "Succeed");
+       close(dev_fd);
+       return ret;
+}
+
+
 int do_ffu(int nargs, char **argv)
 {
 #ifndef MMC_IOC_MULTI_CMD
mmc_cmds.h +1 -1
index 9d3246c..8331ab2 100644
@@ -45,3 +45,4 @@ int do_ffu(int nargs, char **argv);
 int do_read_scr(int argc, char **argv);
 int do_read_cid(int argc, char **argv);
 int do_read_csd(int argc, char **argv);
+int do_erase(int nargs, char **argv);
--

2.24.1 (Apple Git-126)

#2 lore raw

Re: [External] : Re: [PATCH v3] Re-submit of the erase command addition plus removal of MMC_IOC_MULTI_CMD ifndef for erase. Author=Kimito Sakata <[email protected]>

Avri

Please let me know if you have some concerns.

Kimito

Quoted 227 lines · Ulf Hansson

+ Avri

Quoted 3 lines · On Fri, 2 Apr 2021 at 01:02, <[email protected]> wrote:

From: Kimito Sakata <[email protected]>

This looks okay to me, but I have looped in Avri who might have some comments.

Kind regards
Uffe

Quoted 216 lines
---
  mmc.c      |   8 ++++
  mmc.h      |  13 +++++-
  mmc_cmds.c | 129 +++++++++++++++++++++++++++++++++++++++++++++++++++++
  mmc_cmds.h |   1 +
  4 files changed, 150 insertions(+), 1 deletion(-)
mmc.c +8 -0
index f3d724b..eb2638b 100644
@@ -229,6 +229,14 @@ static struct Command commands[] = {
                 "Run Field Firmware Update with <image name> on <device>.\n",
           NULL
         },
+       { do_erase, -4,
+       "erase", "<type> " "<start address> " "<end address> " "<device>\n"
+               "Send Erase CMD38 with specific argument to the <device>\n\n"
+               "NOTE!: This will delete all user data in the specified region of the device\n"
+               "<type> must be: legacy | discard | secure-erase | "
+               "secure-trim1 | secure-trim2 | trim \n",
+       NULL
+       },
         { 0, 0, 0, 0 }
  };
 
mmc.h +12 -1
index 5754a9d..e9766d7 100644
@@ -35,7 +35,15 @@
  #define MMC_SET_WRITE_PROT     28    /* ac   [31:0] data addr   R1b */
  #define MMC_CLEAR_WRITE_PROT   29    /* ac   [31:0] data addr   R1b */
  #define MMC_SEND_WRITE_PROT_TYPE 31   /* ac   [31:0] data addr   R1  */
-
+#define MMC_ERASE_GROUP_START  35    /* ac   [31:0] data addr   R1  */
+#define MMC_ERASE_GROUP_END    36    /* ac   [31:0] data addr   R1  */
+#define MMC_ERASE              38    /* ac   [31] Secure request
+                                             [30:16] set to 0
+                                             [15] Force Garbage Collect request
+                                             [14:2] set to 0
+                                             [1] Discard Enable
+                                             [0] Identify Write Blocks for
+                                             Erase (or TRIM Enable)  R1b */
  /*
   * EXT_CSD fields
   */
@@ -62,6 +70,7 @@
  #define EXT_CSD_CACHE_SIZE_2           251
  #define EXT_CSD_CACHE_SIZE_1           250
  #define EXT_CSD_CACHE_SIZE_0           249
+#define EXT_CSD_SEC_FEATURE_SUPPORT    231
  #define EXT_CSD_BOOT_INFO              228     /* R/W */
  #define EXT_CSD_HC_ERASE_GRP_SIZE      224
  #define EXT_CSD_HC_WP_GRP_SIZE         221
@@ -190,6 +199,8 @@
  #define EXT_CSD_REV_V4_2               2
  #define EXT_CSD_REV_V4_1               1
  #define EXT_CSD_REV_V4_0               0
+#define EXT_CSD_SEC_GB_CL_EN           (1<<4)
+#define EXT_CSD_SEC_ER_EN              (1<<0)
 
 
  /* From kernel linux/mmc/core.h */
mmc_cmds.c +129 -0
index 6c24cea..3e36ff2 100644
@@ -2514,6 +2514,135 @@ int do_cache_dis(int nargs, char **argv)
         return do_cache_ctrl(0, nargs, argv);
  }
 
+static int erase(int dev_fd, __u32 argin, __u32 start, __u32 end)
+{
+       int ret = 0;
+       struct mmc_ioc_multi_cmd *multi_cmd;
+
+       multi_cmd = calloc(1, sizeof(struct mmc_ioc_multi_cmd) +
+                          3 * sizeof(struct mmc_ioc_cmd));
+       if (!multi_cmd) {
+               perror("Failed to allocate memory");
+               return -ENOMEM;
+       }
+
+       multi_cmd->num_of_cmds = 3;
+       /* Set erase start address */
+       multi_cmd->cmds[0].opcode = MMC_ERASE_GROUP_START;
+       multi_cmd->cmds[0].arg = start;
+       multi_cmd->cmds[0].flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
+       multi_cmd->cmds[0].write_flag = 1;
+
+       /* Set erase end address */
+       multi_cmd->cmds[1].opcode = MMC_ERASE_GROUP_END;
+       multi_cmd->cmds[1].arg = end;
+       multi_cmd->cmds[1].flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
+       multi_cmd->cmds[1].write_flag = 1;
+
+       /* Send Erase Command */
+       multi_cmd->cmds[2].opcode = MMC_ERASE;
+       multi_cmd->cmds[2].arg = argin;
+       multi_cmd->cmds[2].cmd_timeout_ms = 300*255*255;
+       multi_cmd->cmds[2].flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
+       multi_cmd->cmds[2].write_flag = 1;
+
+       /* send erase cmd with multi-cmd */
+       ret = ioctl(dev_fd, MMC_IOC_MULTI_CMD, multi_cmd);
+       if (ret)
+               perror("Erase multi-cmd ioctl");
+
+       free(multi_cmd);
+       return ret;
+}
+
+int do_erase(int nargs, char **argv)
+{
+       int dev_fd, ret;
+       char *print_str;
+       char **eptr = NULL;
+       __u8 ext_csd[512], checkup_mask = 0;
+       __u32 arg, start, end;
+
+       if (nargs != 5) {
+               fprintf(stderr, "Usage: erase <type> <start addr> <end addr> </path/to/mmcblkX>\n");
+               exit(1);
+       }
+
+       if (strstr(argv[2], "0x") || strstr(argv[2], "0X"))
+               start = strtol(argv[2], eptr, 16);
+       else
+               start = strtol(argv[2], eptr, 10);
+
+       if (strstr(argv[3], "0x") || strstr(argv[3], "0X"))
+               end = strtol(argv[3], eptr, 16);
+       else
+               end = strtol(argv[3], eptr, 10);
+
+       if (end < start) {
+               fprintf(stderr, "erase start [0x%08x] > erase end [0x%08x]\n",
+                       start, end);
+               exit(1);
+       }
+
+       if (strcmp(argv[1], "legacy") == 0) {
+               arg = 0x00000000;
+               print_str = "Legacy Erase";
+       } else if (strcmp(argv[1], "discard") == 0) {
+               arg = 0x00000003;
+               print_str = "Discard";
+       } else if (strcmp(argv[1], "secure-erase") == 0) {
+               print_str = "Secure Erase";
+               checkup_mask = EXT_CSD_SEC_ER_EN;
+               arg = 0x80000000;
+       } else if (strcmp(argv[1], "secure-trim1") == 0) {
+               print_str = "Secure Trim Step 1";
+               checkup_mask = EXT_CSD_SEC_ER_EN | EXT_CSD_SEC_GB_CL_EN;
+               arg = 0x80000001;
+       } else if (strcmp(argv[1], "secure-trim2") == 0) {
+               print_str = "Secure Trim Step 2";
+               checkup_mask = EXT_CSD_SEC_ER_EN | EXT_CSD_SEC_GB_CL_EN;
+               arg = 0x80008000;
+       } else if (strcmp(argv[1], "trim") == 0) {
+               print_str = "Trim";
+               checkup_mask = EXT_CSD_SEC_GB_CL_EN;
+               arg = 0x00000001;
+       } else {
+               fprintf(stderr, "Unknown erase type: %s\n", argv[1]);
+               exit(1);
+       }
+
+       dev_fd = open(argv[4], O_RDWR);
+       if (dev_fd < 0) {
+               perror(argv[4]);
+               exit(1);
+       }
+
+       if (checkup_mask) {
+               ret = read_extcsd(dev_fd, ext_csd);
+               if (ret) {
+                       fprintf(stderr, "Could not read EXT_CSD from %s\n",
+                               argv[4]);
+                       goto out;
+               }
+               if ((checkup_mask & ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) !=
+                                                               checkup_mask) {
+                       fprintf(stderr, "%s is not supported in %s\n",
+                               print_str, argv[4]);
+                       ret = -ENOTSUP;
+                       goto out;
+               }
+
+       }
+       printf("Executing %s from 0x%08x to 0x%08x\n", print_str, start, end);
+
+       ret = erase(dev_fd, arg, start, end);
+out:
+       printf(" %s %s!\n\n", print_str, ret ? "Failed" : "Succeed");
+       close(dev_fd);
+       return ret;
+}
+
+
  int do_ffu(int nargs, char **argv)
  {
  #ifndef MMC_IOC_MULTI_CMD
mmc_cmds.h +1 -1
index 9d3246c..8331ab2 100644
@@ -45,3 +45,4 @@ int do_ffu(int nargs, char **argv);
  int do_read_scr(int argc, char **argv);
  int do_read_cid(int argc, char **argv);
  int do_read_csd(int argc, char **argv);
+int do_erase(int nargs, char **argv);
--

2.24.1 (Apple Git-126)

#3 lore raw

Re: [External] : Re: [PATCH v3] Re-submit of the erase command addition plus removal of MMC_IOC_MULTI_CMD ifndef for erase. Author=Kimito Sakata <[email protected]>

Avri

I'm sure you are very busy, but I would appreciate if you have any concerns on these changes that I submitted.

Thanks
Kimito

Quoted 252 lines · On 4/15/2021 2:11 PM, [email protected] wrote:

Avri

Please let me know if you have some concerns.

Kimito

Quoted 244 lines · Ulf Hansson

+ Avri

Quoted 3 lines · On Fri, 2 Apr 2021 at 01:02, <[email protected]> wrote:

From: Kimito Sakata <[email protected]>

This looks okay to me, but I have looped in Avri who might have some comments.

Kind regards
Uffe

Quoted 233 lines

---
  mmc.c      |   8 ++++
  mmc.h      |  13 +++++-
  mmc_cmds.c | 129
+++++++++++++++++++++++++++++++++++++++++++++++++++++
  mmc_cmds.h |   1 +

  4 files changed, 150 insertions(+), 1 deletion(-)
mmc.c +0 -0
index f3d724b..eb2638b 100644
@@ -229,6 +229,14 @@ static struct Command commands[] = {
                  "Run Field Firmware Update with <image name> on <device>.\n",

           NULL
         },
+       { do_erase, -4,
+       "erase", "<type> " "<start address> " "<end address> " "<device>\n"
+               "Send Erase CMD38 with specific argument to the <device>\n\n"
+               "NOTE!: This will delete all user data in the specified region of the device\n"
+               "<type> must be: legacy | discard | secure-erase | " +               "secure-trim1 | secure-trim2 | trim \n", +       NULL
+       },
         { 0, 0, 0, 0 }
  };

mmc.h +5 -1
index 5754a9d..e9766d7 100644
@@ -35,7 +35,15 @@
   #define MMC_SET_WRITE_PROT     28    /* ac   [31:0] data addr   R1b */
   #define MMC_CLEAR_WRITE_PROT   29    /* ac   [31:0] data addr   R1b */
   #define MMC_SEND_WRITE_PROT_TYPE 31   /* ac   [31:0] data addr   R1  */
-
+#define MMC_ERASE_GROUP_START  35    /* ac   [31:0] data addr   R1  */
+#define MMC_ERASE_GROUP_END    36    /* ac   [31:0] data addr   R1  */
+#define MMC_ERASE              38    /* ac   [31] Secure request
+                                             [30:16] set to 0
+                                             [15] Force Garbage 

Collect request
+                                             [14:2] set to 0 +                                             [1] Discard Enable +                                             [0] Identify Write Blocks for
+                                             Erase (or TRIM Enable)  R1b */
  /*
   * EXT_CSD fields
   */

mmc.h +3 -0
@@ -62,6 +70,7 @@
   #define EXT_CSD_CACHE_SIZE_2           251
   #define EXT_CSD_CACHE_SIZE_1           250
   #define EXT_CSD_CACHE_SIZE_0           249
+#define EXT_CSD_SEC_FEATURE_SUPPORT    231
   #define EXT_CSD_BOOT_INFO              228     /* R/W */
   #define EXT_CSD_HC_ERASE_GRP_SIZE      224
   #define EXT_CSD_HC_WP_GRP_SIZE         221
@@ -190,6 +199,8 @@
   #define EXT_CSD_REV_V4_2               2
   #define EXT_CSD_REV_V4_1               1
   #define EXT_CSD_REV_V4_0               0
+#define EXT_CSD_SEC_GB_CL_EN           (1<<4)
+#define EXT_CSD_SEC_ER_EN              (1<<0)
 
 
   /* From kernel linux/mmc/core.h */
mmc_cmds.c +51 -0
index 6c24cea..3e36ff2 100644
@@ -2514,6 +2514,135 @@ int do_cache_dis(int nargs, char **argv)
          return do_cache_ctrl(0, nargs, argv);
   }
 
+static int erase(int dev_fd, __u32 argin, __u32 start, __u32 end)
+{
+       int ret = 0;
+       struct mmc_ioc_multi_cmd *multi_cmd;
+
+       multi_cmd = calloc(1, sizeof(struct mmc_ioc_multi_cmd) +
+                          3 * sizeof(struct mmc_ioc_cmd));
+       if (!multi_cmd) {
+               perror("Failed to allocate memory");
+               return -ENOMEM;
+       }
+
+       multi_cmd->num_of_cmds = 3;
+       /* Set erase start address */
+       multi_cmd->cmds[0].opcode = MMC_ERASE_GROUP_START;
+       multi_cmd->cmds[0].arg = start;
+       multi_cmd->cmds[0].flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | 
 MMC_CMD_AC;
+       multi_cmd->cmds[0].write_flag = 1;
+
+       /* Set erase end address */
+       multi_cmd->cmds[1].opcode = MMC_ERASE_GROUP_END;
+       multi_cmd->cmds[1].arg = end;
+       multi_cmd->cmds[1].flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | 
 MMC_CMD_AC;
+       multi_cmd->cmds[1].write_flag = 1;
+
+       /* Send Erase Command */
+       multi_cmd->cmds[2].opcode = MMC_ERASE;
+       multi_cmd->cmds[2].arg = argin;
+       multi_cmd->cmds[2].cmd_timeout_ms = 300*255*255;
+       multi_cmd->cmds[2].flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | 
 MMC_CMD_AC;
+       multi_cmd->cmds[2].write_flag = 1;
+
+       /* send erase cmd with multi-cmd */
+       ret = ioctl(dev_fd, MMC_IOC_MULTI_CMD, multi_cmd);
+       if (ret)
+               perror("Erase multi-cmd ioctl");
+
+       free(multi_cmd);
+       return ret;
+}
+
+int do_erase(int nargs, char **argv)
+{
+       int dev_fd, ret;
+       char *print_str;
+       char **eptr = NULL;
+       __u8 ext_csd[512], checkup_mask = 0;
+       __u32 arg, start, end;
+
+       if (nargs != 5) {
+               fprintf(stderr, "Usage: erase <type> <start addr> 

<end addr> </path/to/mmcblkX>\n");
+               exit(1);
+       }

mmc_cmds.c +13 -0
+
+       if (strstr(argv[2], "0x") || strstr(argv[2], "0X"))
+               start = strtol(argv[2], eptr, 16);
+       else
+               start = strtol(argv[2], eptr, 10);
+
+       if (strstr(argv[3], "0x") || strstr(argv[3], "0X"))
+               end = strtol(argv[3], eptr, 16);
+       else
+               end = strtol(argv[3], eptr, 10);
+
+       if (end < start) {
+               fprintf(stderr, "erase start [0x%08x] > erase end 

[0x%08x]\n",
+                       start, end);
+               exit(1);
+       }
+
+       if (strcmp(argv[1], "legacy") == 0) {
+               arg = 0x00000000;
+               print_str = "Legacy Erase";
+       } else if (strcmp(argv[1], "discard") == 0) {
+               arg = 0x00000003;
+               print_str = "Discard";
+       } else if (strcmp(argv[1], "secure-erase") == 0) { +               print_str = "Secure Erase";
+               checkup_mask = EXT_CSD_SEC_ER_EN;
+               arg = 0x80000000;
+       } else if (strcmp(argv[1], "secure-trim1") == 0) { +               print_str = "Secure Trim Step 1";
+               checkup_mask = EXT_CSD_SEC_ER_EN | EXT_CSD_SEC_GB_CL_EN;
+               arg = 0x80000001;
+       } else if (strcmp(argv[1], "secure-trim2") == 0) { +               print_str = "Secure Trim Step 2";
+               checkup_mask = EXT_CSD_SEC_ER_EN | EXT_CSD_SEC_GB_CL_EN;
+               arg = 0x80008000;
+       } else if (strcmp(argv[1], "trim") == 0) {
+               print_str = "Trim";
+               checkup_mask = EXT_CSD_SEC_GB_CL_EN;
+               arg = 0x00000001;
+       } else {
+               fprintf(stderr, "Unknown erase type: %s\n", argv[1]); +               exit(1);
+       }

mmc_cmds.c +11 -0
+
+       dev_fd = open(argv[4], O_RDWR);
+       if (dev_fd < 0) {
+               perror(argv[4]);
+               exit(1);
+       }
+
+       if (checkup_mask) {
+               ret = read_extcsd(dev_fd, ext_csd);
+               if (ret) {
+                       fprintf(stderr, "Could not read EXT_CSD from 

%s\n",
+                               argv[4]);
+                       goto out;
+               }
+               if ((checkup_mask & ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) !=
+ checkup_mask) {
+                       fprintf(stderr, "%s is not supported in %s\n", +                               print_str, argv[4]);
+                       ret = -ENOTSUP;
+                       goto out;
+               }
+
+       }
+       printf("Executing %s from 0x%08x to 0x%08x\n", print_str, start, end);

mmc_cmds.c +9 -0
+
+       ret = erase(dev_fd, arg, start, end);
+out:
+       printf(" %s %s!\n\n", print_str, ret ? "Failed" : "Succeed");
+       close(dev_fd);
+       return ret;
+}
+
+

  int do_ffu(int nargs, char **argv)
  {
  #ifndef MMC_IOC_MULTI_CMD

mmc_cmds.h +1 -0
index 9d3246c..8331ab2 100644
@@ -45,3 +45,4 @@ int do_ffu(int nargs, char **argv);
   int do_read_scr(int argc, char **argv);
   int do_read_cid(int argc, char **argv);
   int do_read_csd(int argc, char **argv);
+int do_erase(int nargs, char **argv);
Signature · 1 line
2.24.1 (Apple Git-126)
#4 lore raw

Re: [External] : Re: [PATCH v3] Re-submit of the erase command addition plus removal of MMC_IOC_MULTI_CMD ifndef for erase. Author=Kimito Sakata <[email protected]>

Ulf

Looks like Avri either has no comments or cannot respond. Could we please go ahead with the changes?

Thanks
Kimito

Quoted 264 lines · On 4/20/2021 11:22 AM, [email protected] wrote:

Avri

I'm sure you are very busy, but I would appreciate if you have any concerns on these changes that I submitted.

Thanks
Kimito

Quoted 254 lines · On 4/15/2021 2:11 PM, [email protected] wrote:

Avri

Please let me know if you have some concerns.

Kimito

Quoted 246 lines · Ulf Hansson

+ Avri

Quoted 3 lines · On Fri, 2 Apr 2021 at 01:02, <[email protected]> wrote:

From: Kimito Sakata <[email protected]>

This looks okay to me, but I have looped in Avri who might have some comments.

Kind regards
Uffe

Quoted 235 lines

---
  mmc.c      |   8 ++++
  mmc.h      |  13 +++++-
  mmc_cmds.c | 129
+++++++++++++++++++++++++++++++++++++++++++++++++++++
  mmc_cmds.h |   1 +

  4 files changed, 150 insertions(+), 1 deletion(-)
mmc.c +0 -0
index f3d724b..eb2638b 100644
@@ -229,6 +229,14 @@ static struct Command commands[] = {
                  "Run Field Firmware Update with <image name> on <device>.\n",

           NULL
         },
+       { do_erase, -4,
+       "erase", "<type> " "<start address> " "<end address> " "<device>\n"
+               "Send Erase CMD38 with specific argument to the <device>\n\n"
+               "NOTE!: This will delete all user data in the specified region of the device\n"
+               "<type> must be: legacy | discard | secure-erase | " +               "secure-trim1 | secure-trim2 | trim \n", +       NULL
+       },
         { 0, 0, 0, 0 }
  };

mmc.h +1 -1
index 5754a9d..e9766d7 100644
@@ -35,7 +35,15 @@
   #define MMC_SET_WRITE_PROT     28    /* ac   [31:0] data addr   R1b */
   #define MMC_CLEAR_WRITE_PROT   29    /* ac   [31:0] data addr   R1b */
   #define MMC_SEND_WRITE_PROT_TYPE 31   /* ac   [31:0] data addr   R1  */
-
+#define MMC_ERASE_GROUP_START  35    /* ac   [31:0] data addr   

R1  */
+#define MMC_ERASE_GROUP_END    36    /* ac   [31:0] data addr R1  */
+#define MMC_ERASE              38    /* ac   [31] Secure request +                                             [30:16] set to 0 +                                             [15] Force Garbage Collect request
+                                             [14:2] set to 0 +                                             [1] Discard Enable +                                             [0] Identify Write Blocks for
+                                             Erase (or TRIM Enable)  R1b */
  /*
   * EXT_CSD fields
   */

mmc.h +3 -0
@@ -62,6 +70,7 @@
   #define EXT_CSD_CACHE_SIZE_2           251
   #define EXT_CSD_CACHE_SIZE_1           250
   #define EXT_CSD_CACHE_SIZE_0           249
+#define EXT_CSD_SEC_FEATURE_SUPPORT    231
   #define EXT_CSD_BOOT_INFO              228     /* R/W */
   #define EXT_CSD_HC_ERASE_GRP_SIZE      224
   #define EXT_CSD_HC_WP_GRP_SIZE         221
@@ -190,6 +199,8 @@
   #define EXT_CSD_REV_V4_2               2
   #define EXT_CSD_REV_V4_1               1
   #define EXT_CSD_REV_V4_0               0
+#define EXT_CSD_SEC_GB_CL_EN           (1<<4)
+#define EXT_CSD_SEC_ER_EN              (1<<0)
 
 
   /* From kernel linux/mmc/core.h */
mmc_cmds.c +51 -0
index 6c24cea..3e36ff2 100644
@@ -2514,6 +2514,135 @@ int do_cache_dis(int nargs, char **argv)
          return do_cache_ctrl(0, nargs, argv);
   }
 
+static int erase(int dev_fd, __u32 argin, __u32 start, __u32 end)
+{
+       int ret = 0;
+       struct mmc_ioc_multi_cmd *multi_cmd;
+
+       multi_cmd = calloc(1, sizeof(struct mmc_ioc_multi_cmd) +
+                          3 * sizeof(struct mmc_ioc_cmd));
+       if (!multi_cmd) {
+               perror("Failed to allocate memory");
+               return -ENOMEM;
+       }
+
+       multi_cmd->num_of_cmds = 3;
+       /* Set erase start address */
+       multi_cmd->cmds[0].opcode = MMC_ERASE_GROUP_START;
+       multi_cmd->cmds[0].arg = start;
+       multi_cmd->cmds[0].flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | 
 MMC_CMD_AC;
+       multi_cmd->cmds[0].write_flag = 1;
+
+       /* Set erase end address */
+       multi_cmd->cmds[1].opcode = MMC_ERASE_GROUP_END;
+       multi_cmd->cmds[1].arg = end;
+       multi_cmd->cmds[1].flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | 
 MMC_CMD_AC;
+       multi_cmd->cmds[1].write_flag = 1;
+
+       /* Send Erase Command */
+       multi_cmd->cmds[2].opcode = MMC_ERASE;
+       multi_cmd->cmds[2].arg = argin;
+       multi_cmd->cmds[2].cmd_timeout_ms = 300*255*255;
+       multi_cmd->cmds[2].flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | 
 MMC_CMD_AC;
+       multi_cmd->cmds[2].write_flag = 1;
+
+       /* send erase cmd with multi-cmd */
+       ret = ioctl(dev_fd, MMC_IOC_MULTI_CMD, multi_cmd);
+       if (ret)
+               perror("Erase multi-cmd ioctl");
+
+       free(multi_cmd);
+       return ret;
+}
+
+int do_erase(int nargs, char **argv)
+{
+       int dev_fd, ret;
+       char *print_str;
+       char **eptr = NULL;
+       __u8 ext_csd[512], checkup_mask = 0;
+       __u32 arg, start, end;
+
+       if (nargs != 5) {
+               fprintf(stderr, "Usage: erase <type> <start addr> 

<end addr> </path/to/mmcblkX>\n");
+               exit(1);
+       }

mmc_cmds.c +13 -0
+
+       if (strstr(argv[2], "0x") || strstr(argv[2], "0X"))
+               start = strtol(argv[2], eptr, 16);
+       else
+               start = strtol(argv[2], eptr, 10);
+
+       if (strstr(argv[3], "0x") || strstr(argv[3], "0X"))
+               end = strtol(argv[3], eptr, 16);
+       else
+               end = strtol(argv[3], eptr, 10);
+
+       if (end < start) {
+               fprintf(stderr, "erase start [0x%08x] > erase end 

[0x%08x]\n",
+                       start, end);
+               exit(1);
+       }
+
+       if (strcmp(argv[1], "legacy") == 0) {
+               arg = 0x00000000;
+               print_str = "Legacy Erase";
+       } else if (strcmp(argv[1], "discard") == 0) {
+               arg = 0x00000003;
+               print_str = "Discard";
+       } else if (strcmp(argv[1], "secure-erase") == 0) { +               print_str = "Secure Erase";
+               checkup_mask = EXT_CSD_SEC_ER_EN;
+               arg = 0x80000000;
+       } else if (strcmp(argv[1], "secure-trim1") == 0) { +               print_str = "Secure Trim Step 1";
+               checkup_mask = EXT_CSD_SEC_ER_EN | EXT_CSD_SEC_GB_CL_EN;
+               arg = 0x80000001;
+       } else if (strcmp(argv[1], "secure-trim2") == 0) { +               print_str = "Secure Trim Step 2";
+               checkup_mask = EXT_CSD_SEC_ER_EN | EXT_CSD_SEC_GB_CL_EN;
+               arg = 0x80008000;
+       } else if (strcmp(argv[1], "trim") == 0) {
+               print_str = "Trim";
+               checkup_mask = EXT_CSD_SEC_GB_CL_EN;
+               arg = 0x00000001;
+       } else {
+               fprintf(stderr, "Unknown erase type: %s\n", argv[1]); +               exit(1);
+       }

mmc_cmds.c +11 -0
+
+       dev_fd = open(argv[4], O_RDWR);
+       if (dev_fd < 0) {
+               perror(argv[4]);
+               exit(1);
+       }
+
+       if (checkup_mask) {
+               ret = read_extcsd(dev_fd, ext_csd);
+               if (ret) {
+                       fprintf(stderr, "Could not read EXT_CSD 

from %s\n",
+                               argv[4]);
+                       goto out;
+               }
+               if ((checkup_mask & ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) !=
+ checkup_mask) {
+                       fprintf(stderr, "%s is not supported in %s\n", +                               print_str, argv[4]);
+                       ret = -ENOTSUP;
+                       goto out;
+               }
+
+       }
+       printf("Executing %s from 0x%08x to 0x%08x\n", print_str, start, end);

mmc_cmds.c +9 -0
+
+       ret = erase(dev_fd, arg, start, end);
+out:
+       printf(" %s %s!\n\n", print_str, ret ? "Failed" : "Succeed");
+       close(dev_fd);
+       return ret;
+}
+
+

  int do_ffu(int nargs, char **argv)
  {
  #ifndef MMC_IOC_MULTI_CMD

mmc_cmds.h +1 -0
index 9d3246c..8331ab2 100644
@@ -45,3 +45,4 @@ int do_ffu(int nargs, char **argv);
   int do_read_scr(int argc, char **argv);
   int do_read_cid(int argc, char **argv);
   int do_read_csd(int argc, char **argv);
+int do_erase(int nargs, char **argv);
Signature · 1 line
2.24.1 (Apple Git-126)
#8 lore raw

Re: [External] : Re: [PATCH v3] Re-submit of the erase command addition plus removal of MMC_IOC_MULTI_CMD ifndef for erase. Author=Kimito Sakata <[email protected]>

In reply to [email protected]

Not sure if this was received so re-sending since I got a send error.

Ulf

Looks like Avri either has no comments or cannot respond. Could we please go ahead with the changes?

Thanks
Kimito

Quoted 261 lines · On 4/20/2021 11:22 AM, [email protected] wrote:

Avri

I'm sure you are very busy, but I would appreciate if you have any concerns on these changes that I submitted.

Thanks
Kimito

Quoted 251 lines · On 4/15/2021 2:11 PM, [email protected] wrote:

Avri

Please let me know if you have some concerns.

Kimito

Quoted 243 lines · Ulf Hansson

+ Avri

Quoted 3 lines · On Fri, 2 Apr 2021 at 01:02, <[email protected]> wrote:

From: Kimito Sakata <[email protected]>

This looks okay to me, but I have looped in Avri who might have some comments.

Kind regards
Uffe

Quoted 232 lines

---
  mmc.c      |   8 ++++
  mmc.h      |  13 +++++-
  mmc_cmds.c | 129
+++++++++++++++++++++++++++++++++++++++++++++++++++++
  mmc_cmds.h |   1 +

  4 files changed, 150 insertions(+), 1 deletion(-)
mmc.c +0 -0
index f3d724b..eb2638b 100644
@@ -229,6 +229,14 @@ static struct Command commands[] = {
                  "Run Field Firmware Update with <image name> on <device>.\n",

           NULL
         },
+       { do_erase, -4,
+       "erase", "<type> " "<start address> " "<end address> " "<device>\n"
+               "Send Erase CMD38 with specific argument to the <device>\n\n"
+               "NOTE!: This will delete all user data in the specified region of the device\n"
+               "<type> must be: legacy | discard | secure-erase | " +               "secure-trim1 | secure-trim2 | trim \n", +       NULL
+       },
         { 0, 0, 0, 0 }
  };

mmc.h +5 -1
index 5754a9d..e9766d7 100644
@@ -35,7 +35,15 @@
   #define MMC_SET_WRITE_PROT     28    /* ac   [31:0] data addr   R1b */
   #define MMC_CLEAR_WRITE_PROT   29    /* ac   [31:0] data addr   R1b */
   #define MMC_SEND_WRITE_PROT_TYPE 31   /* ac   [31:0] data addr   R1  */
-
+#define MMC_ERASE_GROUP_START  35    /* ac   [31:0] data addr   R1  */
+#define MMC_ERASE_GROUP_END    36    /* ac   [31:0] data addr   R1  */
+#define MMC_ERASE              38    /* ac   [31] Secure request
+                                             [30:16] set to 0
+                                             [15] Force Garbage 

Collect request
+                                             [14:2] set to 0 +                                             [1] Discard Enable +                                             [0] Identify Write Blocks for
+                                             Erase (or TRIM Enable)  R1b */
  /*
   * EXT_CSD fields
   */

mmc.h +3 -0
@@ -62,6 +70,7 @@
   #define EXT_CSD_CACHE_SIZE_2           251
   #define EXT_CSD_CACHE_SIZE_1           250
   #define EXT_CSD_CACHE_SIZE_0           249
+#define EXT_CSD_SEC_FEATURE_SUPPORT    231
   #define EXT_CSD_BOOT_INFO              228     /* R/W */
   #define EXT_CSD_HC_ERASE_GRP_SIZE      224
   #define EXT_CSD_HC_WP_GRP_SIZE         221
@@ -190,6 +199,8 @@
   #define EXT_CSD_REV_V4_2               2
   #define EXT_CSD_REV_V4_1               1
   #define EXT_CSD_REV_V4_0               0
+#define EXT_CSD_SEC_GB_CL_EN           (1<<4)
+#define EXT_CSD_SEC_ER_EN              (1<<0)
 
 
   /* From kernel linux/mmc/core.h */
mmc_cmds.c +51 -0
index 6c24cea..3e36ff2 100644
@@ -2514,6 +2514,135 @@ int do_cache_dis(int nargs, char **argv)
          return do_cache_ctrl(0, nargs, argv);
   }
 
+static int erase(int dev_fd, __u32 argin, __u32 start, __u32 end)
+{
+       int ret = 0;
+       struct mmc_ioc_multi_cmd *multi_cmd;
+
+       multi_cmd = calloc(1, sizeof(struct mmc_ioc_multi_cmd) +
+                          3 * sizeof(struct mmc_ioc_cmd));
+       if (!multi_cmd) {
+               perror("Failed to allocate memory");
+               return -ENOMEM;
+       }
+
+       multi_cmd->num_of_cmds = 3;
+       /* Set erase start address */
+       multi_cmd->cmds[0].opcode = MMC_ERASE_GROUP_START;
+       multi_cmd->cmds[0].arg = start;
+       multi_cmd->cmds[0].flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | 
 MMC_CMD_AC;
+       multi_cmd->cmds[0].write_flag = 1;
+
+       /* Set erase end address */
+       multi_cmd->cmds[1].opcode = MMC_ERASE_GROUP_END;
+       multi_cmd->cmds[1].arg = end;
+       multi_cmd->cmds[1].flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | 
 MMC_CMD_AC;
+       multi_cmd->cmds[1].write_flag = 1;
+
+       /* Send Erase Command */
+       multi_cmd->cmds[2].opcode = MMC_ERASE;
+       multi_cmd->cmds[2].arg = argin;
+       multi_cmd->cmds[2].cmd_timeout_ms = 300*255*255;
+       multi_cmd->cmds[2].flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | 
 MMC_CMD_AC;
+       multi_cmd->cmds[2].write_flag = 1;
+
+       /* send erase cmd with multi-cmd */
+       ret = ioctl(dev_fd, MMC_IOC_MULTI_CMD, multi_cmd);
+       if (ret)
+               perror("Erase multi-cmd ioctl");
+
+       free(multi_cmd);
+       return ret;
+}
+
+int do_erase(int nargs, char **argv)
+{
+       int dev_fd, ret;
+       char *print_str;
+       char **eptr = NULL;
+       __u8 ext_csd[512], checkup_mask = 0;
+       __u32 arg, start, end;
+
+       if (nargs != 5) {
+               fprintf(stderr, "Usage: erase <type> <start addr> 

<end addr> </path/to/mmcblkX>\n");
+               exit(1);
+       }

mmc_cmds.c +13 -0
+
+       if (strstr(argv[2], "0x") || strstr(argv[2], "0X"))
+               start = strtol(argv[2], eptr, 16);
+       else
+               start = strtol(argv[2], eptr, 10);
+
+       if (strstr(argv[3], "0x") || strstr(argv[3], "0X"))
+               end = strtol(argv[3], eptr, 16);
+       else
+               end = strtol(argv[3], eptr, 10);
+
+       if (end < start) {
+               fprintf(stderr, "erase start [0x%08x] > erase end 

[0x%08x]\n",
+                       start, end);
+               exit(1);
+       }
+
+       if (strcmp(argv[1], "legacy") == 0) {
+               arg = 0x00000000;
+               print_str = "Legacy Erase";
+       } else if (strcmp(argv[1], "discard") == 0) {
+               arg = 0x00000003;
+               print_str = "Discard";
+       } else if (strcmp(argv[1], "secure-erase") == 0) { +               print_str = "Secure Erase";
+               checkup_mask = EXT_CSD_SEC_ER_EN;
+               arg = 0x80000000;
+       } else if (strcmp(argv[1], "secure-trim1") == 0) { +               print_str = "Secure Trim Step 1";
+               checkup_mask = EXT_CSD_SEC_ER_EN | EXT_CSD_SEC_GB_CL_EN;
+               arg = 0x80000001;
+       } else if (strcmp(argv[1], "secure-trim2") == 0) { +               print_str = "Secure Trim Step 2";
+               checkup_mask = EXT_CSD_SEC_ER_EN | EXT_CSD_SEC_GB_CL_EN;
+               arg = 0x80008000;
+       } else if (strcmp(argv[1], "trim") == 0) {
+               print_str = "Trim";
+               checkup_mask = EXT_CSD_SEC_GB_CL_EN;
+               arg = 0x00000001;
+       } else {
+               fprintf(stderr, "Unknown erase type: %s\n", argv[1]); +               exit(1);
+       }

mmc_cmds.c +11 -0
+
+       dev_fd = open(argv[4], O_RDWR);
+       if (dev_fd < 0) {
+               perror(argv[4]);
+               exit(1);
+       }
+
+       if (checkup_mask) {
+               ret = read_extcsd(dev_fd, ext_csd);
+               if (ret) {
+                       fprintf(stderr, "Could not read EXT_CSD 

from %s\n",
+                               argv[4]);
+                       goto out;
+               }
+               if ((checkup_mask & ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) !=
+ checkup_mask) {
+                       fprintf(stderr, "%s is not supported in %s\n", +                               print_str, argv[4]);
+                       ret = -ENOTSUP;
+                       goto out;
+               }
+
+       }
+       printf("Executing %s from 0x%08x to 0x%08x\n", print_str, start, end);

mmc_cmds.c +9 -0
+
+       ret = erase(dev_fd, arg, start, end);
+out:
+       printf(" %s %s!\n\n", print_str, ret ? "Failed" : "Succeed");
+       close(dev_fd);
+       return ret;
+}
+
+

  int do_ffu(int nargs, char **argv)
  {
  #ifndef MMC_IOC_MULTI_CMD

mmc_cmds.h +1 -1
index 9d3246c..8331ab2 100644
@@ -45,3 +45,4 @@ int do_ffu(int nargs, char **argv);
   int do_read_scr(int argc, char **argv);
   int do_read_cid(int argc, char **argv);
   int do_read_csd(int argc, char **argv);
+int do_erase(int nargs, char **argv);
-- 2.24.1 (Apple Git-126)
#5 lore raw

Re: [External] : Re: [PATCH v3] Re-submit of the erase command addition plus removal of MMC_IOC_MULTI_CMD ifndef for erase. Author=Kimito Sakata <[email protected]>

In reply to [email protected]
Quoted 5 lines · On Wed, 21 Apr 2021 at 20:39, <[email protected]> wrote:

Ulf

Looks like Avri either has no comments or cannot respond. Could we please go ahead with the changes?

I think we should give him a couple more days, it's not been more than just a week since I looped him in to review the patch.

Another option, that might be easier for him, is if you repost the patch and make sure Avri is on the to-list, this time.

Kind regards
Uffe

Quoted 276 lines

Thanks
Kimito

Quoted 270 lines · On 4/20/2021 11:22 AM, [email protected] wrote:

Avri

I'm sure you are very busy, but I would appreciate if you have any concerns on these changes that I submitted.

Thanks
Kimito

Quoted 259 lines · On 4/15/2021 2:11 PM, [email protected] wrote:

Avri

Please let me know if you have some concerns.

Kimito

Quoted 251 lines · Ulf Hansson

+ Avri

Quoted 3 lines · On Fri, 2 Apr 2021 at 01:02, <[email protected]> wrote:

From: Kimito Sakata <[email protected]>

This looks okay to me, but I have looped in Avri who might have some comments.

Kind regards
Uffe

Quoted 239 lines
---
  mmc.c      |   8 ++++
  mmc.h      |  13 +++++-
  mmc_cmds.c | 129
+++++++++++++++++++++++++++++++++++++++++++++++++++++
  mmc_cmds.h |   1 +
  4 files changed, 150 insertions(+), 1 deletion(-)
mmc.c +2 -0
index f3d724b..eb2638b 100644
@@ -229,6 +229,14 @@ static struct Command commands[] = {
                 "Run Field Firmware Update with <image name> on
 <device>.\n",
           NULL
         },
+       { do_erase, -4,
+       "erase", "<type> " "<start address> " "<end address> "

"<device>\n"
+ "Send Erase CMD38 with specific argument to the
<device>\n\n"
+ "NOTE!: This will delete all user data in the
specified region of the device\n"

mmc.c +4 -0
+               "<type> must be: legacy | discard | secure-erase | "
+               "secure-trim1 | secure-trim2 | trim \n",
+       NULL
+       },
         { 0, 0, 0, 0 }
  };
mmc.h +0 -0
index 5754a9d..e9766d7 100644
@@ -35,7 +35,15 @@
  #define MMC_SET_WRITE_PROT     28    /* ac   [31:0] data addr

R1b */
#define MMC_CLEAR_WRITE_PROT 29 /* ac [31:0] data addr
R1b */
#define MMC_SEND_WRITE_PROT_TYPE 31 /* ac [31:0] data addr
R1 */

mmc.h +1 -1
-
+#define MMC_ERASE_GROUP_START  35    /* ac   [31:0] data addr

R1 */
+#define MMC_ERASE_GROUP_END 36 /* ac [31:0] data addr
R1 */

mmc.h +3 -0
+#define MMC_ERASE              38    /* ac   [31] Secure request
+                                             [30:16] set to 0
+                                             [15] Force Garbage

Collect request

mmc.h +3 -0
+                                             [14:2] set to 0
+                                             [1] Discard Enable
+                                             [0] Identify Write

Blocks for
+ Erase (or TRIM
Enable) R1b */
/*
* EXT_CSD fields
*/

mmc.h +3 -0
@@ -62,6 +70,7 @@
  #define EXT_CSD_CACHE_SIZE_2           251
  #define EXT_CSD_CACHE_SIZE_1           250
  #define EXT_CSD_CACHE_SIZE_0           249
+#define EXT_CSD_SEC_FEATURE_SUPPORT    231
  #define EXT_CSD_BOOT_INFO              228     /* R/W */
  #define EXT_CSD_HC_ERASE_GRP_SIZE      224
  #define EXT_CSD_HC_WP_GRP_SIZE         221
@@ -190,6 +199,8 @@
  #define EXT_CSD_REV_V4_2               2
  #define EXT_CSD_REV_V4_1               1
  #define EXT_CSD_REV_V4_0               0
+#define EXT_CSD_SEC_GB_CL_EN           (1<<4)
+#define EXT_CSD_SEC_ER_EN              (1<<0)
 
 
  /* From kernel linux/mmc/core.h */
mmc_cmds.c +51 -0
index 6c24cea..3e36ff2 100644
@@ -2514,6 +2514,135 @@ int do_cache_dis(int nargs, char **argv)
         return do_cache_ctrl(0, nargs, argv);
  }
 
+static int erase(int dev_fd, __u32 argin, __u32 start, __u32 end)
+{
+       int ret = 0;
+       struct mmc_ioc_multi_cmd *multi_cmd;
+
+       multi_cmd = calloc(1, sizeof(struct mmc_ioc_multi_cmd) +
+                          3 * sizeof(struct mmc_ioc_cmd));
+       if (!multi_cmd) {
+               perror("Failed to allocate memory");
+               return -ENOMEM;
+       }
+
+       multi_cmd->num_of_cmds = 3;
+       /* Set erase start address */
+       multi_cmd->cmds[0].opcode = MMC_ERASE_GROUP_START;
+       multi_cmd->cmds[0].arg = start;
+       multi_cmd->cmds[0].flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
 MMC_CMD_AC;
+       multi_cmd->cmds[0].write_flag = 1;
+
+       /* Set erase end address */
+       multi_cmd->cmds[1].opcode = MMC_ERASE_GROUP_END;
+       multi_cmd->cmds[1].arg = end;
+       multi_cmd->cmds[1].flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
 MMC_CMD_AC;
+       multi_cmd->cmds[1].write_flag = 1;
+
+       /* Send Erase Command */
+       multi_cmd->cmds[2].opcode = MMC_ERASE;
+       multi_cmd->cmds[2].arg = argin;
+       multi_cmd->cmds[2].cmd_timeout_ms = 300*255*255;
+       multi_cmd->cmds[2].flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
 MMC_CMD_AC;
+       multi_cmd->cmds[2].write_flag = 1;
+
+       /* send erase cmd with multi-cmd */
+       ret = ioctl(dev_fd, MMC_IOC_MULTI_CMD, multi_cmd);
+       if (ret)
+               perror("Erase multi-cmd ioctl");
+
+       free(multi_cmd);
+       return ret;
+}
+
+int do_erase(int nargs, char **argv)
+{
+       int dev_fd, ret;
+       char *print_str;
+       char **eptr = NULL;
+       __u8 ext_csd[512], checkup_mask = 0;
+       __u32 arg, start, end;
+
+       if (nargs != 5) {
+               fprintf(stderr, "Usage: erase <type> <start addr>

<end addr> </path/to/mmcblkX>\n");

mmc_cmds.c +15 -0
+               exit(1);
+       }
+
+       if (strstr(argv[2], "0x") || strstr(argv[2], "0X"))
+               start = strtol(argv[2], eptr, 16);
+       else
+               start = strtol(argv[2], eptr, 10);
+
+       if (strstr(argv[3], "0x") || strstr(argv[3], "0X"))
+               end = strtol(argv[3], eptr, 16);
+       else
+               end = strtol(argv[3], eptr, 10);
+
+       if (end < start) {
+               fprintf(stderr, "erase start [0x%08x] > erase end

[0x%08x]\n",

mmc_cmds.c +17 -0
+                       start, end);
+               exit(1);
+       }
+
+       if (strcmp(argv[1], "legacy") == 0) {
+               arg = 0x00000000;
+               print_str = "Legacy Erase";
+       } else if (strcmp(argv[1], "discard") == 0) {
+               arg = 0x00000003;
+               print_str = "Discard";
+       } else if (strcmp(argv[1], "secure-erase") == 0) {
+               print_str = "Secure Erase";
+               checkup_mask = EXT_CSD_SEC_ER_EN;
+               arg = 0x80000000;
+       } else if (strcmp(argv[1], "secure-trim1") == 0) {
+               print_str = "Secure Trim Step 1";
+               checkup_mask = EXT_CSD_SEC_ER_EN |

EXT_CSD_SEC_GB_CL_EN;

mmc_cmds.c +4 -0
+               arg = 0x80000001;
+       } else if (strcmp(argv[1], "secure-trim2") == 0) {
+               print_str = "Secure Trim Step 2";
+               checkup_mask = EXT_CSD_SEC_ER_EN |

EXT_CSD_SEC_GB_CL_EN;

mmc_cmds.c +20 -0
+               arg = 0x80008000;
+       } else if (strcmp(argv[1], "trim") == 0) {
+               print_str = "Trim";
+               checkup_mask = EXT_CSD_SEC_GB_CL_EN;
+               arg = 0x00000001;
+       } else {
+               fprintf(stderr, "Unknown erase type: %s\n", argv[1]);
+               exit(1);
+       }
+
+       dev_fd = open(argv[4], O_RDWR);
+       if (dev_fd < 0) {
+               perror(argv[4]);
+               exit(1);
+       }
+
+       if (checkup_mask) {
+               ret = read_extcsd(dev_fd, ext_csd);
+               if (ret) {
+                       fprintf(stderr, "Could not read EXT_CSD

from %s\n",

mmc_cmds.c +4 -0
+                               argv[4]);
+                       goto out;
+               }
+               if ((checkup_mask &

ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) !=
+ checkup_mask) {

mmc_cmds.c +8 -0
+                       fprintf(stderr, "%s is not supported in %s\n",
+                               print_str, argv[4]);
+                       ret = -ENOTSUP;
+                       goto out;
+               }
+
+       }
+       printf("Executing %s from 0x%08x to 0x%08x\n", print_str,

start, end);

mmc_cmds.c +9 -0
+
+       ret = erase(dev_fd, arg, start, end);
+out:
+       printf(" %s %s!\n\n", print_str, ret ? "Failed" : "Succeed");
+       close(dev_fd);
+       return ret;
+}
+
+
  int do_ffu(int nargs, char **argv)
  {
  #ifndef MMC_IOC_MULTI_CMD
mmc_cmds.h +1 -1
index 9d3246c..8331ab2 100644
@@ -45,3 +45,4 @@ int do_ffu(int nargs, char **argv);
  int do_read_scr(int argc, char **argv);
  int do_read_cid(int argc, char **argv);
  int do_read_csd(int argc, char **argv);
+int do_erase(int nargs, char **argv);
--

2.24.1 (Apple Git-126)

#6 lore raw

RE: [External] : Re: [PATCH v3] Re-submit of the erase command addition plus removal of MMC_IOC_MULTI_CMD ifndef for erase. Author=Kimito Sakata <[email protected]>

Quoted 8 lines · On Wed, 21 Apr 2021 at 20:39, <[email protected]> wrote:
Quoted 5 lines

Ulf

Looks like Avri either has no comments or cannot respond. Could we please go ahead with the changes?

Quoted 8 lines

I think we should give him a couple more days, it's not been more than just a week since I looped him in to review the patch.

Another option, that might be easier for him, is if you repost the patch and make sure Avri is on the to-list, this time.

Kind regards
Uffe

Hi,
Sorry for not catching this, my mail is looking for "mmc-utils" prefix.... When resending, can you please make sure that your patch is properly formatted:
Subject: [PATCH RESEND v3] mmc-utils: <your subject>

<informative commit log>

Thanks,
Avri

Quoted 279 lines
Quoted 29 lines

Thanks
Kimito

Quoted 23 lines · On 4/20/2021 11:22 AM, [email protected] wrote:

Avri

I'm sure you are very busy, but I would appreciate if you have any concerns on these changes that I submitted.

Thanks
Kimito

Quoted 13 lines · On 4/15/2021 2:11 PM, [email protected] wrote:

Avri

Please let me know if you have some concerns.

Kimito

Quoted 6 lines · Ulf Hansson

+ Avri

Quoted 3 lines · On Fri, 2 Apr 2021 at 01:02, <[email protected]> wrote:

From: Kimito Sakata <[email protected]>

MBP.hsd1.co.comcast.net>

Quoted 115 lines
Quoted 115 lines
Quoted 115 lines
Quoted 115 lines

This looks okay to me, but I have looped in Avri who might have some comments.

Kind regards
Uffe

Quoted 109 lines
---
  mmc.c      |   8 ++++
  mmc.h      |  13 +++++-
  mmc_cmds.c | 129
+++++++++++++++++++++++++++++++++++++++++++++++++++++
  mmc_cmds.h |   1 +
  4 files changed, 150 insertions(+), 1 deletion(-)
mmc.c +2 -0
index f3d724b..eb2638b 100644
@@ -229,6 +229,14 @@ static struct Command commands[] = {
                 "Run Field Firmware Update with <image name> on
 <device>.\n",
           NULL
         },
+       { do_erase, -4,
+       "erase", "<type> " "<start address> " "<end address> "

"<device>\n"
+ "Send Erase CMD38 with specific argument to the
<device>\n\n"
+ "NOTE!: This will delete all user data in the
specified region of the device\n"

mmc.c +4 -0
+               "<type> must be: legacy | discard | secure-erase | "
+               "secure-trim1 | secure-trim2 | trim \n",
+       NULL
+       },
         { 0, 0, 0, 0 }
  };
mmc.h +0 -0
index 5754a9d..e9766d7 100644
@@ -35,7 +35,15 @@
  #define MMC_SET_WRITE_PROT     28    /* ac   [31:0] data addr

R1b */
#define MMC_CLEAR_WRITE_PROT 29 /* ac [31:0] data addr
R1b */
#define MMC_SEND_WRITE_PROT_TYPE 31 /* ac [31:0] data addr
R1 */

mmc.h +1 -1
-
+#define MMC_ERASE_GROUP_START  35    /* ac   [31:0] data addr

R1 */
+#define MMC_ERASE_GROUP_END 36 /* ac [31:0] data addr
R1 */

mmc.h +3 -0
+#define MMC_ERASE              38    /* ac   [31] Secure request
+                                             [30:16] set to 0
+                                             [15] Force Garbage

Collect request

mmc.h +3 -0
+                                             [14:2] set to 0
+                                             [1] Discard Enable
+                                             [0] Identify Write

Blocks for
+ Erase (or TRIM
Enable) R1b */
/*
* EXT_CSD fields
*/

mmc.h +3 -0
@@ -62,6 +70,7 @@
  #define EXT_CSD_CACHE_SIZE_2           251
  #define EXT_CSD_CACHE_SIZE_1           250
  #define EXT_CSD_CACHE_SIZE_0           249
+#define EXT_CSD_SEC_FEATURE_SUPPORT    231
  #define EXT_CSD_BOOT_INFO              228     /* R/W */
  #define EXT_CSD_HC_ERASE_GRP_SIZE      224
  #define EXT_CSD_HC_WP_GRP_SIZE         221
@@ -190,6 +199,8 @@
  #define EXT_CSD_REV_V4_2               2
  #define EXT_CSD_REV_V4_1               1
  #define EXT_CSD_REV_V4_0               0
+#define EXT_CSD_SEC_GB_CL_EN           (1<<4)
+#define EXT_CSD_SEC_ER_EN              (1<<0)
 
 
  /* From kernel linux/mmc/core.h */
mmc_cmds.c +23 -0
index 6c24cea..3e36ff2 100644
@@ -2514,6 +2514,135 @@ int do_cache_dis(int nargs, char **argv)
         return do_cache_ctrl(0, nargs, argv);
  }
 
+static int erase(int dev_fd, __u32 argin, __u32 start, __u32 end)
+{
+       int ret = 0;
+       struct mmc_ioc_multi_cmd *multi_cmd;
+
+       multi_cmd = calloc(1, sizeof(struct mmc_ioc_multi_cmd) +
+                          3 * sizeof(struct mmc_ioc_cmd));
+       if (!multi_cmd) {
+               perror("Failed to allocate memory");
+               return -ENOMEM;
+       }
+
+       multi_cmd->num_of_cmds = 3;
+       /* Set erase start address */
+       multi_cmd->cmds[0].opcode = MMC_ERASE_GROUP_START;
+       multi_cmd->cmds[0].arg = start;
+       multi_cmd->cmds[0].flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
 MMC_CMD_AC;
+       multi_cmd->cmds[0].write_flag = 1;
+
+       /* Set erase end address */
+       multi_cmd->cmds[1].opcode = MMC_ERASE_GROUP_END;
+       multi_cmd->cmds[1].arg = end;
+       multi_cmd->cmds[1].flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

mmc_cmds.c int do_cache_dis(int nargs, char **argv)

Quoted 8 lines
mmc_cmds.c +7 -0
 MMC_CMD_AC;
+       multi_cmd->cmds[1].write_flag = 1;
+
+       /* Send Erase Command */
+       multi_cmd->cmds[2].opcode = MMC_ERASE;
+       multi_cmd->cmds[2].arg = argin;
+       multi_cmd->cmds[2].cmd_timeout_ms = 300*255*255;
+       multi_cmd->cmds[2].flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B

|

Quoted 125 lines
Quoted 124 lines
Quoted 123 lines
Quoted 122 lines
Quoted 122 lines
mmc_cmds.c +21 -0
 MMC_CMD_AC;
+       multi_cmd->cmds[2].write_flag = 1;
+
+       /* send erase cmd with multi-cmd */
+       ret = ioctl(dev_fd, MMC_IOC_MULTI_CMD, multi_cmd);
+       if (ret)
+               perror("Erase multi-cmd ioctl");
+
+       free(multi_cmd);
+       return ret;
+}
+
+int do_erase(int nargs, char **argv)
+{
+       int dev_fd, ret;
+       char *print_str;
+       char **eptr = NULL;
+       __u8 ext_csd[512], checkup_mask = 0;
+       __u32 arg, start, end;
+
+       if (nargs != 5) {
+               fprintf(stderr, "Usage: erase <type> <start addr>

<end addr> </path/to/mmcblkX>\n");

mmc_cmds.c +15 -0
+               exit(1);
+       }
+
+       if (strstr(argv[2], "0x") || strstr(argv[2], "0X"))
+               start = strtol(argv[2], eptr, 16);
+       else
+               start = strtol(argv[2], eptr, 10);
+
+       if (strstr(argv[3], "0x") || strstr(argv[3], "0X"))
+               end = strtol(argv[3], eptr, 16);
+       else
+               end = strtol(argv[3], eptr, 10);
+
+       if (end < start) {
+               fprintf(stderr, "erase start [0x%08x] > erase end

[0x%08x]\n",

mmc_cmds.c +17 -0
+                       start, end);
+               exit(1);
+       }
+
+       if (strcmp(argv[1], "legacy") == 0) {
+               arg = 0x00000000;
+               print_str = "Legacy Erase";
+       } else if (strcmp(argv[1], "discard") == 0) {
+               arg = 0x00000003;
+               print_str = "Discard";
+       } else if (strcmp(argv[1], "secure-erase") == 0) {
+               print_str = "Secure Erase";
+               checkup_mask = EXT_CSD_SEC_ER_EN;
+               arg = 0x80000000;
+       } else if (strcmp(argv[1], "secure-trim1") == 0) {
+               print_str = "Secure Trim Step 1";
+               checkup_mask = EXT_CSD_SEC_ER_EN |

EXT_CSD_SEC_GB_CL_EN;

mmc_cmds.c +4 -0
+               arg = 0x80000001;
+       } else if (strcmp(argv[1], "secure-trim2") == 0) {
+               print_str = "Secure Trim Step 2";
+               checkup_mask = EXT_CSD_SEC_ER_EN |

EXT_CSD_SEC_GB_CL_EN;

mmc_cmds.c +20 -0
+               arg = 0x80008000;
+       } else if (strcmp(argv[1], "trim") == 0) {
+               print_str = "Trim";
+               checkup_mask = EXT_CSD_SEC_GB_CL_EN;
+               arg = 0x00000001;
+       } else {
+               fprintf(stderr, "Unknown erase type: %s\n", argv[1]);
+               exit(1);
+       }
+
+       dev_fd = open(argv[4], O_RDWR);
+       if (dev_fd < 0) {
+               perror(argv[4]);
+               exit(1);
+       }
+
+       if (checkup_mask) {
+               ret = read_extcsd(dev_fd, ext_csd);
+               if (ret) {
+                       fprintf(stderr, "Could not read EXT_CSD

from %s\n",

mmc_cmds.c +4 -0
+                               argv[4]);
+                       goto out;
+               }
+               if ((checkup_mask &

ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) !=
+ checkup_mask) {

mmc_cmds.c +8 -0
+                       fprintf(stderr, "%s is not supported in %s\n",
+                               print_str, argv[4]);
+                       ret = -ENOTSUP;
+                       goto out;
+               }
+
+       }
+       printf("Executing %s from 0x%08x to 0x%08x\n", print_str,

start, end);

mmc_cmds.c +9 -0
+
+       ret = erase(dev_fd, arg, start, end);
+out:
+       printf(" %s %s!\n\n", print_str, ret ? "Failed" : "Succeed");
+       close(dev_fd);
+       return ret;
+}
+
+
  int do_ffu(int nargs, char **argv)
  {
  #ifndef MMC_IOC_MULTI_CMD
mmc_cmds.h +1 -1
index 9d3246c..8331ab2 100644
@@ -45,3 +45,4 @@ int do_ffu(int nargs, char **argv);
  int do_read_scr(int argc, char **argv);
  int do_read_cid(int argc, char **argv);
  int do_read_csd(int argc, char **argv);
+int do_erase(int nargs, char **argv);
--

2.24.1 (Apple Git-126)

#7 lore raw

Re: [External] : Re: [PATCH v3] Re-submit of the erase command addition plus removal of MMC_IOC_MULTI_CMD ifndef for erase. Author=Kimito Sakata <[email protected]>

Quoted 11 lines · Avri Altman
Quoted 11 lines · On Wed, 21 Apr 2021 at 20:39, <[email protected]> wrote:
Quoted 4 lines

Ulf

Looks like Avri either has no comments or cannot respond. Could we please go ahead with the changes?

I think we should give him a couple more days, it's not been more than just a week since I looped him in to review the patch.

Another option, that might be easier for him, is if you repost the patch and make sure Avri is on the to-list, this time.

Quoted 8 lines
Quoted 2 lines

Kind regards
Uffe

Hi,
Sorry for not catching this, my mail is looking for "mmc-utils" prefix.... When resending, can you please make sure that your patch is properly formatted:
Subject: [PATCH RESEND v3] mmc-utils: <your subject>

<informative commit log>

Avri
Thanks for responding.
So you want me to resubmit with the addition of "mmc-utils" in the subject line correct? Kimito

Quoted 278 lines

Thanks,
Avri

Quoted 274 lines
Quoted 27 lines

Thanks
Kimito

Quoted 23 lines · On 4/20/2021 11:22 AM, [email protected] wrote:

Avri

I'm sure you are very busy, but I would appreciate if you have any concerns on these changes that I submitted.

Thanks
Kimito

Quoted 13 lines · On 4/15/2021 2:11 PM, [email protected] wrote:

Avri

Please let me know if you have some concerns.

Kimito

Quoted 6 lines · Ulf Hansson

+ Avri

Quoted 3 lines · On Fri, 2 Apr 2021 at 01:02, <[email protected]> wrote:

From: Kimito Sakata <[email protected]>

MBP.hsd1.co.comcast.net>

Quoted 115 lines
Quoted 115 lines
Quoted 115 lines
Quoted 115 lines

This looks okay to me, but I have looped in Avri who might have some comments.

Kind regards
Uffe

Quoted 109 lines
---
   mmc.c      |   8 ++++
   mmc.h      |  13 +++++-
   mmc_cmds.c | 129
+++++++++++++++++++++++++++++++++++++++++++++++++++++
   mmc_cmds.h |   1 +
   4 files changed, 150 insertions(+), 1 deletion(-)
mmc.c +2 -0
index f3d724b..eb2638b 100644
@@ -229,6 +229,14 @@ static struct Command commands[] = {
                  "Run Field Firmware Update with <image name> on
 <device>.\n",
            NULL
          },
+       { do_erase, -4,
+       "erase", "<type> " "<start address> " "<end address> "

"<device>\n"
+ "Send Erase CMD38 with specific argument to the
<device>\n\n"
+ "NOTE!: This will delete all user data in the
specified region of the device\n"

mmc.c +4 -0
+               "<type> must be: legacy | discard | secure-erase | "
+               "secure-trim1 | secure-trim2 | trim \n",
+       NULL
+       },
          { 0, 0, 0, 0 }
   };
mmc.h +0 -0
index 5754a9d..e9766d7 100644
@@ -35,7 +35,15 @@
   #define MMC_SET_WRITE_PROT     28    /* ac   [31:0] data addr

R1b */
#define MMC_CLEAR_WRITE_PROT 29 /* ac [31:0] data addr
R1b */
#define MMC_SEND_WRITE_PROT_TYPE 31 /* ac [31:0] data addr
R1 */

mmc.h +1 -1
-
+#define MMC_ERASE_GROUP_START  35    /* ac   [31:0] data addr

R1 */
+#define MMC_ERASE_GROUP_END 36 /* ac [31:0] data addr
R1 */

mmc.h +3 -0
+#define MMC_ERASE              38    /* ac   [31] Secure request
+                                             [30:16] set to 0
+                                             [15] Force Garbage

Collect request

mmc.h +3 -0
+                                             [14:2] set to 0
+                                             [1] Discard Enable
+                                             [0] Identify Write

Blocks for
+ Erase (or TRIM
Enable) R1b */
/*
* EXT_CSD fields
*/

mmc.h +3 -0
@@ -62,6 +70,7 @@
   #define EXT_CSD_CACHE_SIZE_2           251
   #define EXT_CSD_CACHE_SIZE_1           250
   #define EXT_CSD_CACHE_SIZE_0           249
+#define EXT_CSD_SEC_FEATURE_SUPPORT    231
   #define EXT_CSD_BOOT_INFO              228     /* R/W */
   #define EXT_CSD_HC_ERASE_GRP_SIZE      224
   #define EXT_CSD_HC_WP_GRP_SIZE         221
@@ -190,6 +199,8 @@
   #define EXT_CSD_REV_V4_2               2
   #define EXT_CSD_REV_V4_1               1
   #define EXT_CSD_REV_V4_0               0
+#define EXT_CSD_SEC_GB_CL_EN           (1<<4)
+#define EXT_CSD_SEC_ER_EN              (1<<0)
 
 
   /* From kernel linux/mmc/core.h */
mmc_cmds.c +23 -0
index 6c24cea..3e36ff2 100644
@@ -2514,6 +2514,135 @@ int do_cache_dis(int nargs, char **argv)
          return do_cache_ctrl(0, nargs, argv);
   }
 
+static int erase(int dev_fd, __u32 argin, __u32 start, __u32 end)
+{
+       int ret = 0;
+       struct mmc_ioc_multi_cmd *multi_cmd;
+
+       multi_cmd = calloc(1, sizeof(struct mmc_ioc_multi_cmd) +
+                          3 * sizeof(struct mmc_ioc_cmd));
+       if (!multi_cmd) {
+               perror("Failed to allocate memory");
+               return -ENOMEM;
+       }
+
+       multi_cmd->num_of_cmds = 3;
+       /* Set erase start address */
+       multi_cmd->cmds[0].opcode = MMC_ERASE_GROUP_START;
+       multi_cmd->cmds[0].arg = start;
+       multi_cmd->cmds[0].flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
 MMC_CMD_AC;
+       multi_cmd->cmds[0].write_flag = 1;
+
+       /* Set erase end address */
+       multi_cmd->cmds[1].opcode = MMC_ERASE_GROUP_END;
+       multi_cmd->cmds[1].arg = end;
+       multi_cmd->cmds[1].flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

mmc_cmds.c int do_cache_dis(int nargs, char **argv) · line 2548

Quoted 8 lines
mmc_cmds.c +7 -0
 MMC_CMD_AC;
+       multi_cmd->cmds[1].write_flag = 1;
+
+       /* Send Erase Command */
+       multi_cmd->cmds[2].opcode = MMC_ERASE;
+       multi_cmd->cmds[2].arg = argin;
+       multi_cmd->cmds[2].cmd_timeout_ms = 300*255*255;
+       multi_cmd->cmds[2].flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B

|

Quoted 122 lines
Quoted 122 lines
Quoted 122 lines
Quoted 122 lines
Quoted 122 lines
mmc_cmds.c +21 -0
 MMC_CMD_AC;
+       multi_cmd->cmds[2].write_flag = 1;
+
+       /* send erase cmd with multi-cmd */
+       ret = ioctl(dev_fd, MMC_IOC_MULTI_CMD, multi_cmd);
+       if (ret)
+               perror("Erase multi-cmd ioctl");
+
+       free(multi_cmd);
+       return ret;
+}
+
+int do_erase(int nargs, char **argv)
+{
+       int dev_fd, ret;
+       char *print_str;
+       char **eptr = NULL;
+       __u8 ext_csd[512], checkup_mask = 0;
+       __u32 arg, start, end;
+
+       if (nargs != 5) {
+               fprintf(stderr, "Usage: erase <type> <start addr>

<end addr> </path/to/mmcblkX>\n");

mmc_cmds.c +15 -0
+               exit(1);
+       }
+
+       if (strstr(argv[2], "0x") || strstr(argv[2], "0X"))
+               start = strtol(argv[2], eptr, 16);
+       else
+               start = strtol(argv[2], eptr, 10);
+
+       if (strstr(argv[3], "0x") || strstr(argv[3], "0X"))
+               end = strtol(argv[3], eptr, 16);
+       else
+               end = strtol(argv[3], eptr, 10);
+
+       if (end < start) {
+               fprintf(stderr, "erase start [0x%08x] > erase end

[0x%08x]\n",

mmc_cmds.c +17 -0
+                       start, end);
+               exit(1);
+       }
+
+       if (strcmp(argv[1], "legacy") == 0) {
+               arg = 0x00000000;
+               print_str = "Legacy Erase";
+       } else if (strcmp(argv[1], "discard") == 0) {
+               arg = 0x00000003;
+               print_str = "Discard";
+       } else if (strcmp(argv[1], "secure-erase") == 0) {
+               print_str = "Secure Erase";
+               checkup_mask = EXT_CSD_SEC_ER_EN;
+               arg = 0x80000000;
+       } else if (strcmp(argv[1], "secure-trim1") == 0) {
+               print_str = "Secure Trim Step 1";
+               checkup_mask = EXT_CSD_SEC_ER_EN |

EXT_CSD_SEC_GB_CL_EN;

mmc_cmds.c +4 -0
+               arg = 0x80000001;
+       } else if (strcmp(argv[1], "secure-trim2") == 0) {
+               print_str = "Secure Trim Step 2";
+               checkup_mask = EXT_CSD_SEC_ER_EN |

EXT_CSD_SEC_GB_CL_EN;

mmc_cmds.c +20 -0
+               arg = 0x80008000;
+       } else if (strcmp(argv[1], "trim") == 0) {
+               print_str = "Trim";
+               checkup_mask = EXT_CSD_SEC_GB_CL_EN;
+               arg = 0x00000001;
+       } else {
+               fprintf(stderr, "Unknown erase type: %s\n", argv[1]);
+               exit(1);
+       }
+
+       dev_fd = open(argv[4], O_RDWR);
+       if (dev_fd < 0) {
+               perror(argv[4]);
+               exit(1);
+       }
+
+       if (checkup_mask) {
+               ret = read_extcsd(dev_fd, ext_csd);
+               if (ret) {
+                       fprintf(stderr, "Could not read EXT_CSD

from %s\n",

mmc_cmds.c +4 -0
+                               argv[4]);
+                       goto out;
+               }
+               if ((checkup_mask &

ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) !=
+ checkup_mask) {

mmc_cmds.c +8 -0
+                       fprintf(stderr, "%s is not supported in %s\n",
+                               print_str, argv[4]);
+                       ret = -ENOTSUP;
+                       goto out;
+               }
+
+       }
+       printf("Executing %s from 0x%08x to 0x%08x\n", print_str,

start, end);

mmc_cmds.c +9 -0
+
+       ret = erase(dev_fd, arg, start, end);
+out:
+       printf(" %s %s!\n\n", print_str, ret ? "Failed" : "Succeed");
+       close(dev_fd);
+       return ret;
+}
+
+
   int do_ffu(int nargs, char **argv)
   {
   #ifndef MMC_IOC_MULTI_CMD
mmc_cmds.h +1 -1
index 9d3246c..8331ab2 100644
@@ -45,3 +45,4 @@ int do_ffu(int nargs, char **argv);
   int do_read_scr(int argc, char **argv);
   int do_read_cid(int argc, char **argv);
   int do_read_csd(int argc, char **argv);
+int do_erase(int nargs, char **argv);
--

2.24.1 (Apple Git-126)