diff mbox series

[v3,4/5] mmc-utils: Added FFU mode 4 that uses CMD6 and CMD24 for single-block firmware download

Message ID 20241013205807.121932-5-beanhuo@iokpp.de (mailing list archive)
State New
Headers show
Series Add multiple FFU modes based on eMMC specification 6.6.18 for flexible firmware updates | expand

Commit Message

Bean Huo Oct. 13, 2024, 8:58 p.m. UTC
From: Bean Huo <beanhuo@micron.com>

FFU mode 4 initiates firmware download with CMD6 to enter FFU mode, followed by CMD24 for
single-block write. After each block is written, CMD6 is used to exit FFU mode.

Signed-off-by: Bean Huo <beanhuo@micron.com>
---
 mmc.1      |  3 +++
 mmc.c      |  5 +++++
 mmc_cmds.c | 17 ++++++++++++++++-
 mmc_cmds.h |  1 +
 4 files changed, 25 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/mmc.1 b/mmc.1
index d1598f7..5e147d7 100644
--- a/mmc.1
+++ b/mmc.1
@@ -198,6 +198,9 @@  Same as 'ffu', but uses CMD23+CMD25 for repeated downloads and remains in FFU mo
 .BI ffu3 " \fIimage\-file\-name\fR " " \fIdevice\fR " " [\fIchunk\-bytes\fR]
 Same as 'ffu', but uses CMD25+CMD12 Open-ended Multiple-block write to download and remains in FFU mode until completion.
 .TP
+.BI ffu4 " \fIimage\-file\-name\fR " " \fIdevice\fR " " [\fIchunk\-bytes\fR]
+Same as 'ffu', but uses CMD24 Single-block write to download, exiting FFU mode after each block is written.
+.TP
 .BI erase " " \fItype\fR " " \fIstart-address\fR " " \fIend\-address\fR " " \fIdevice\fR
 Send Erase CMD38 with specific argument to the device.
 .br
diff --git a/mmc.c b/mmc.c
index a0227ca..fd78f9b 100644
--- a/mmc.c
+++ b/mmc.c
@@ -244,6 +244,11 @@  static struct Command commands[] = {
 		"Same as 'ffu', but uses CMD25+CMD12 Open-ended Multiple-block write to download and remains in FFU mode until completion.\n",
 	  NULL
 	},
+	{ do_ffu4, -2,
+	  "ffu4", "<image name> <device> [chunk-bytes]\n"
+		"Same as 'ffu', but uses CMD24 Single-block write to download, exiting FFU mode after each block written.\n",
+	  NULL
+	},
 	{ do_erase, -4,
 	"erase", "<type> " "<start address> " "<end address> " "<device>\n"
 		"Send Erase CMD38 with specific argument to the <device>\n\n"
diff --git a/mmc_cmds.c b/mmc_cmds.c
index 6c38387..6290440 100644
--- a/mmc_cmds.c
+++ b/mmc_cmds.c
@@ -2839,6 +2839,11 @@  static void set_ffu_download_cmd(struct mmc_ioc_multi_cmd *multi_cmd, __u8 *ext_
 		mmc_ioc_cmd_set_data(multi_cmd->cmds[0], buf + offset);
 		set_single_cmd(&multi_cmd->cmds[1], MMC_STOP_TRANSMISSION, 0, 0, 0);
 		multi_cmd->cmds[1].flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
+	} else if (ffu_mode == 4) {
+		fill_switch_cmd(&multi_cmd->cmds[0], EXT_CSD_MODE_CONFIG, EXT_CSD_FFU_MODE);
+		set_single_cmd(&multi_cmd->cmds[1], MMC_WRITE_BLOCK, 1, 1, arg);
+		mmc_ioc_cmd_set_data(multi_cmd->cmds[1], buf + offset);
+		fill_switch_cmd(&multi_cmd->cmds[2], EXT_CSD_MODE_CONFIG, EXT_CSD_NORMAL_MODE);
 	}
 }
 static int enter_ffu_mode(int *dev_fd)
@@ -2924,8 +2929,10 @@  static int do_ffu_download(int *dev_fd, __u8 *ext_csd, __u8 *fw_buf, off_t fw_si
 		return -EINVAL;
 	}
 
-	if (ffu_mode != 1) /* in FFU mode 1, mmc_ioc_multi_cmd contains 4 commands */
+	if (ffu_mode == 2 || ffu_mode == 3) /* in FFU mode 2, 3, mmc_ioc_multi_cmd contains 2 commands */
 		num_of_cmds = 2;
+	else if (ffu_mode == 4)
+		num_of_cmds = 3; /* in FFU mode 4, mmc_ioc_multi_cmd contains 3 commands */
 
 	/* allocate maximum required */
 	multi_cmd = calloc(1, sizeof(struct mmc_ioc_multi_cmd) + num_of_cmds * sizeof(struct mmc_ioc_cmd));
@@ -3068,6 +3075,10 @@  static int __do_ffu(int nargs, char **argv, __u8 ffu_mode)
 		goto out;
 	}
 
+	if (ffu_mode ==4)
+		/* FFU mode 4 uses CMD24, the write is limited to single-block operations*/
+		default_chunk = 512;
+
 	sect_done = do_ffu_download((int *)&dev_fd, ext_csd, fw_buf, fw_size, default_chunk, ffu_mode);
 
 	/* Check programmed sectors */
@@ -3150,6 +3161,10 @@  int do_ffu3(int nargs, char **argv) {
 	return __do_ffu(nargs, argv, 3);
 }
 
+int do_ffu4(int nargs, char **argv) {
+	return __do_ffu(nargs, argv, 4);
+}
+
 int do_general_cmd_read(int nargs, char **argv)
 {
 	int dev_fd;
diff --git a/mmc_cmds.h b/mmc_cmds.h
index e3e9192..27dc6c4 100644
--- a/mmc_cmds.h
+++ b/mmc_cmds.h
@@ -44,6 +44,7 @@  int do_cache_dis(int nargs, char **argv);
 int do_ffu(int nargs, char **argv);
 int do_ffu2(int nargs, char **argv);
 int do_ffu3(int nargs, char **argv);
+int do_ffu4(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);