Skip to contentSkip to conversation

mmc: Field Firmware Update

RFC In review Holger Schurig 7 participants 21 messages Nov 13, 2015 – Apr 14, 2016 2 messages not in the archive MMC, SD and SDIO

lore
Share & tools
lore
#0 lore raw

cover mmc: Field Firmware Update

There have been some attempts to add FFU (field firmware update). The last AFAIK in Nov 2014, http://www.spinics.net/lists/linux-mmc/msg29324.html

But it seems that the committers weren't persistent enought.

I took the liberty to take Avi's patch and make it hopefully maintainer-review friendly.

The first 5 patches just move functions out of mmc_test.c into core.c. Those functions will later be used by both mmc_test.c and mmc_ffu.c. Contrary to Avi's patch I didn't add static helper functions to mmc_test.c, e.g. there's no mmc_test_prepare_mrq() that calls mmc_prepare_mrq(). It's simpler to call mmc_prepare_mrq() directly. It's just one more dereference from *mmc_card to *mmc_test_card anyway.

The patch [6/6] is http://www.spinics.net/lists/linux-mmc/msg29326.html, but with less checkpatch warnings. And it doesn't use mmc_send_ext_csd() anymore, which has been deleted since November.

I'm sending this patch as RFC now. It compiles (for me). But I get the firmware update file from Kingston only next Tuesday. That means that so far I haven't been testing it. It won't do anything without the proper user-space command in mmc-utils anyway :-)

Comments welcome (I intent to get this patch into the kernel)

The patch is against Linux GIT (v4.3-11748-g46d862b).

Holger

 drivers/mmc/card/Kconfig    |  11 +
 drivers/mmc/card/Makefile   |   1 +
 drivers/mmc/card/block.c    |   5 +
 drivers/mmc/card/mmc_ffu.c  | 489 ++++++++++++++++++++++++++++++++++++++++++++
 drivers/mmc/card/mmc_test.c | 235 +++++----------------
 drivers/mmc/core/core.c     | 134 ++++++++++++
 drivers/mmc/core/mmc_ops.c  |   4 +-
 include/linux/mmc/card.h    |   1 +
 include/linux/mmc/core.h    |  41 ++++
 include/linux/mmc/mmc.h     |   6 +
 10 files changed, 739 insertions(+), 188 deletions(-)
 create mode 100644 drivers/mmc/card/mmc_ffu.c
Signature · 1 line
2.1.4
#1 lore raw

PATCH 1/6 mmc: move mmc_prepare_mrq() to core.c

Currently this function is used inside the mmc test driver. But it is also usable in the (upcoming) firmware update patch. So move this function out of mmc_test.c into core.c.

---
 drivers/mmc/card/mmc_test.c | 48 ++++-----------------------------------------
 drivers/mmc/core/core.c     | 41 ++++++++++++++++++++++++++++++++++++++
 include/linux/mmc/core.h    |  4 ++++
 3 files changed, 49 insertions(+), 44 deletions(-)
drivers/mmc/card/mmc_test.c +4 -44
index 7fc9174..69b6c45 100644
@@ -184,46 +184,6 @@ static int mmc_test_set_blksize(struct mmc_test_card *test, unsigned size)
 	return mmc_set_blocklen(test->card, size);
 }
 
-/*
- * Fill in the mmc_request structure given a set of transfer parameters.
- */
-static void mmc_test_prepare_mrq(struct mmc_test_card *test,
-	struct mmc_request *mrq, struct scatterlist *sg, unsigned sg_len,
-	unsigned dev_addr, unsigned blocks, unsigned blksz, int write)
-{
-	BUG_ON(!mrq || !mrq->cmd || !mrq->data || !mrq->stop);
-
-	if (blocks > 1) {
-		mrq->cmd->opcode = write ?
-			MMC_WRITE_MULTIPLE_BLOCK : MMC_READ_MULTIPLE_BLOCK;
-	} else {
-		mrq->cmd->opcode = write ?
-			MMC_WRITE_BLOCK : MMC_READ_SINGLE_BLOCK;
-	}
-
-	mrq->cmd->arg = dev_addr;
-	if (!mmc_card_blockaddr(test->card))
-		mrq->cmd->arg <<= 9;
-
-	mrq->cmd->flags = MMC_RSP_R1 | MMC_CMD_ADTC;
-
-	if (blocks == 1)
-		mrq->stop = NULL;
-	else {
-		mrq->stop->opcode = MMC_STOP_TRANSMISSION;
-		mrq->stop->arg = 0;
-		mrq->stop->flags = MMC_RSP_R1B | MMC_CMD_AC;
-	}
-
-	mrq->data->blksz = blksz;
-	mrq->data->blocks = blocks;
-	mrq->data->flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
-	mrq->data->sg = sg;
-	mrq->data->sg_len = sg_len;
-
-	mmc_set_data_timeout(mrq->data, test->card);
-}
-
 static int mmc_test_busy(struct mmc_command *cmd)
 {
 	return !(cmd->resp[0] & R1_READY_FOR_DATA) ||
@@ -281,7 +241,7 @@ static int mmc_test_buffer_transfer(struct mmc_test_card *test,
 
 	sg_init_one(&sg, buffer, blksz);
 
-	mmc_test_prepare_mrq(test, &mrq, &sg, 1, addr, 1, blksz, write);
+	mmc_prepare_mrq(test->card, &mrq, &sg, 1, addr, 1, blksz, write);
 
 	mmc_wait_for_req(test->card->host, &mrq);
 
@@ -805,7 +765,7 @@ static int mmc_test_nonblock_transfer(struct mmc_test_card *test,
 	other_areq->err_check = mmc_test_check_result_async;
 
 	for (i = 0; i < count; i++) {
-		mmc_test_prepare_mrq(test, cur_areq->mrq, sg, sg_len, dev_addr,
+		mmc_prepare_mrq(test->card, cur_areq->mrq, sg, sg_len, dev_addr,
 				     blocks, blksz, write);
 		done_areq = mmc_start_req(test->card->host, cur_areq, &ret);
 
@@ -847,7 +807,7 @@ static int mmc_test_simple_transfer(struct mmc_test_card *test,
 	mrq.data = &data;
 	mrq.stop = &stop;
 
-	mmc_test_prepare_mrq(test, &mrq, sg, sg_len, dev_addr,
+	mmc_prepare_mrq(test->card, &mrq, sg, sg_len, dev_addr,
 		blocks, blksz, write);
 
 	mmc_wait_for_req(test->card->host, &mrq);
@@ -876,7 +836,7 @@ static int mmc_test_broken_transfer(struct mmc_test_card *test,
 
 	sg_init_one(&sg, test->buffer, blocks * blksz);
 
-	mmc_test_prepare_mrq(test, &mrq, &sg, 1, 0, blocks, blksz, write);
+	mmc_prepare_mrq(test->card, &mrq, &sg, 1, 0, blocks, blksz, write);
 	mmc_test_prepare_broken_mrq(test, &mrq, write);
 
 	mmc_wait_for_req(test->card->host, &mrq);
drivers/mmc/core/core.c +41 -0

47 lines not on this page · raw message

include/linux/mmc/core.h +4 -0
index 37967b6..f8db960 100644
@@ -196,6 +196,10 @@ extern int mmc_flush_cache(struct mmc_card *);
 
 extern int mmc_detect_card_removed(struct mmc_host *host);
 
+extern void mmc_prepare_mrq(struct mmc_card *card,
+	struct mmc_request *mrq, struct scatterlist *sg, unsigned sg_len,
+	unsigned dev_addr, unsigned blocks, unsigned blksz, int write);
+
 /**
  *	mmc_claim_host - exclusively claim a host
  *	@host: mmc host to claim
Signature · 1 line
2.1.4
#2 lore raw

PATCH 2/6 mmc: move mmc_wait_busy() to core

In reply to Holger Schurig

Currently this function is used inside the mmc test driver. But it is also usable in the (upcoming) firmware update patch. So move this function out of mmc_test.c into core.c.

---
 drivers/mmc/card/mmc_test.c | 46 ++++-----------------------------------------
 drivers/mmc/core/core.c     | 38 +++++++++++++++++++++++++++++++++++++
 include/linux/mmc/core.h    |  1 +
 3 files changed, 43 insertions(+), 42 deletions(-)
drivers/mmc/card/mmc_test.c +4 -42
index 69b6c45..d1c2d22 100644
@@ -184,44 +184,6 @@ static int mmc_test_set_blksize(struct mmc_test_card *test, unsigned size)
 	return mmc_set_blocklen(test->card, size);
 }
 
-static int mmc_test_busy(struct mmc_command *cmd)
-{
-	return !(cmd->resp[0] & R1_READY_FOR_DATA) ||
-		(R1_CURRENT_STATE(cmd->resp[0]) == R1_STATE_PRG);
-}
-
-/*
- * Wait for the card to finish the busy state
- */
-static int mmc_test_wait_busy(struct mmc_test_card *test)
-{
-	int ret, busy;
-	struct mmc_command cmd = {0};
-
-	busy = 0;
-	do {
-		memset(&cmd, 0, sizeof(struct mmc_command));
-
-		cmd.opcode = MMC_SEND_STATUS;
-		cmd.arg = test->card->rca << 16;
-		cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
-
-		ret = mmc_wait_for_cmd(test->card->host, &cmd, 0);
-		if (ret)
-			break;
-
-		if (!busy && mmc_test_busy(&cmd)) {
-			busy = 1;
-			if (test->card->host->caps & MMC_CAP_WAIT_WHILE_BUSY)
-				pr_info("%s: Warning: Host did not "
-					"wait for busy state to end.\n",
-					mmc_hostname(test->card->host));
-		}
-	} while (mmc_test_busy(&cmd));
-
-	return ret;
-}
-
 /*
  * Transfer a single sector of kernel addressable data
  */
@@ -250,7 +212,7 @@ static int mmc_test_buffer_transfer(struct mmc_test_card *test,
 	if (data.error)
 		return data.error;
 
-	return mmc_test_wait_busy(test);
+	return mmc_wait_busy(test->card);
 }
 
 static void mmc_test_free_mem(struct mmc_test_mem *mem)
@@ -675,7 +637,7 @@ static int mmc_test_check_result_async(struct mmc_card *card,
 	struct mmc_test_async_req *test_async =
 		container_of(areq, struct mmc_test_async_req, areq);
 
-	mmc_test_wait_busy(test_async->test);
+	mmc_wait_busy(test_async->test->card);
 
 	return mmc_test_check_result(test_async->test, areq->mrq);
 }
@@ -812,7 +774,7 @@ static int mmc_test_simple_transfer(struct mmc_test_card *test,
 
 	mmc_wait_for_req(test->card->host, &mrq);
 
-	mmc_test_wait_busy(test);
+	mmc_wait_busy(test->card);
 
 	return mmc_test_check_result(test, &mrq);
 }
@@ -841,7 +803,7 @@ static int mmc_test_broken_transfer(struct mmc_test_card *test,
 
 	mmc_wait_for_req(test->card->host, &mrq);
 
-	mmc_test_wait_busy(test);
+	mmc_wait_busy(test->card);
 
 	return mmc_test_check_broken_result(test, &mrq);
 }
drivers/mmc/core/core.c +38 -0
index 2b5e398..df5a61c 100644
@@ -2856,6 +2856,44 @@ void mmc_prepare_mrq(struct mmc_card *card,
 }
 EXPORT_SYMBOL(mmc_prepare_mrq);
 
+static int mmc_test_busy(struct mmc_command *cmd)
+{
+	return !(cmd->resp[0] & R1_READY_FOR_DATA) ||
+		(R1_CURRENT_STATE(cmd->resp[0]) == R1_STATE_PRG);
+}
+
+/*
+ * Wait for the card to finish the busy state
+ */
+int mmc_wait_busy(struct mmc_card *card)
+{
+	int ret, busy;
+	struct mmc_command cmd = {0};
+
+	busy = 0;
+	do {
+		memset(&cmd, 0, sizeof(struct mmc_command));
+
+		cmd.opcode = MMC_SEND_STATUS;
+		cmd.arg = card->rca << 16;
+		cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
+
+		ret = mmc_wait_for_cmd(card->host, &cmd, 0);
+		if (ret)
+			break;
+
+		if (!busy && mmc_test_busy(&cmd)) {
+			busy = 1;
+			if (card->host->caps & MMC_CAP_WAIT_WHILE_BUSY)
+				pr_info("%s: Warning: Host did not wait for busy state to end.\n",
+					mmc_hostname(card->host));
+		}
+	} while (mmc_test_busy(&cmd));
+
+	return ret;
+}
+EXPORT_SYMBOL(mmc_wait_busy);
+
 /**
  * mmc_init_context_info() - init synchronization context
  * @host: mmc host
include/linux/mmc/core.h +1 -0
index f8db960..50e37e1 100644
@@ -199,6 +199,7 @@ extern int mmc_detect_card_removed(struct mmc_host *host);
 extern void mmc_prepare_mrq(struct mmc_card *card,
 	struct mmc_request *mrq, struct scatterlist *sg, unsigned sg_len,
 	unsigned dev_addr, unsigned blocks, unsigned blksz, int write);
+extern int mmc_wait_busy(struct mmc_card *card);
 
 /**
  *	mmc_claim_host - exclusively claim a host
Signature · 1 line
2.1.4
#3 lore raw

PATCH 3/6 mmc: move mmc_check_result() to core.c

In reply to Holger Schurig

Currently this function is used inside the mmc test driver. But it is also usable in the (upcoming) firmware update patch. So move this function out of mmc_test.c into core.c.

This also adds global MMC_RESULT_ variables that show if some condition stems from the host controller or the card/chip. This is helpful to the source in printk.

---
 drivers/mmc/card/mmc_test.c | 109 +++++++++++++++-----------------------------
 drivers/mmc/core/core.c     |  28 ++++++++++++
 include/linux/mmc/core.h    |   8 ++++
 3 files changed, 74 insertions(+), 71 deletions(-)
drivers/mmc/card/mmc_test.c +38 -71
index d1c2d22..1d9d997 100644
@@ -24,11 +24,6 @@
 #include <linux/seq_file.h>
 #include <linux/module.h>
 
-#define RESULT_OK		0
-#define RESULT_FAIL		1
-#define RESULT_UNSUP_HOST	2
-#define RESULT_UNSUP_CARD	3
-
 #define BUFFER_ORDER		2
 #define BUFFER_SIZE		(PAGE_SIZE << BUFFER_ORDER)
 
@@ -603,34 +598,6 @@ static void mmc_test_prepare_broken_mrq(struct mmc_test_card *test,
 	}
 }
 
-/*
- * Checks that a normal transfer didn't have any errors
- */
-static int mmc_test_check_result(struct mmc_test_card *test,
-				 struct mmc_request *mrq)
-{
-	int ret;
-
-	BUG_ON(!mrq || !mrq->cmd || !mrq->data);
-
-	ret = 0;
-
-	if (!ret && mrq->cmd->error)
-		ret = mrq->cmd->error;
-	if (!ret && mrq->data->error)
-		ret = mrq->data->error;
-	if (!ret && mrq->stop && mrq->stop->error)
-		ret = mrq->stop->error;
-	if (!ret && mrq->data->bytes_xfered !=
-		mrq->data->blocks * mrq->data->blksz)
-		ret = RESULT_FAIL;
-
-	if (ret == -EINVAL)
-		ret = RESULT_UNSUP_HOST;
-
-	return ret;
-}
-
 static int mmc_test_check_result_async(struct mmc_card *card,
 				       struct mmc_async_req *areq)
 {
@@ -639,7 +606,7 @@ static int mmc_test_check_result_async(struct mmc_card *card,
 
 	mmc_wait_busy(test_async->test->card);
 
-	return mmc_test_check_result(test_async->test, areq->mrq);
+	return mmc_check_result(areq->mrq);
 }
 
 /*
@@ -657,21 +624,21 @@ static int mmc_test_check_broken_result(struct mmc_test_card *test,
 	if (!ret && mrq->cmd->error)
 		ret = mrq->cmd->error;
 	if (!ret && mrq->data->error == 0)
-		ret = RESULT_FAIL;
+		ret = MMC_RESULT_FAIL;
 	if (!ret && mrq->data->error != -ETIMEDOUT)
 		ret = mrq->data->error;
 	if (!ret && mrq->stop && mrq->stop->error)
 		ret = mrq->stop->error;
 	if (mrq->data->blocks > 1) {
 		if (!ret && mrq->data->bytes_xfered > mrq->data->blksz)
-			ret = RESULT_FAIL;
+			ret = MMC_RESULT_FAIL;
 	} else {
 		if (!ret && mrq->data->bytes_xfered > 0)
-			ret = RESULT_FAIL;
+			ret = MMC_RESULT_FAIL;
 	}
 
 	if (ret == -EINVAL)
-		ret = RESULT_UNSUP_HOST;
+		ret = MMC_RESULT_UNSUP_HOST;
 
 	return ret;
 }
@@ -776,7 +743,7 @@ static int mmc_test_simple_transfer(struct mmc_test_card *test,
 
 	mmc_wait_busy(test->card);
 
-	return mmc_test_check_result(test, &mrq);
+	return mmc_check_result(&mrq);
 }
 
 /*
@@ -865,12 +832,12 @@ static int mmc_test_transfer(struct mmc_test_card *test,
 
 		for (i = 0;i < blocks * blksz;i++) {
 			if (test->buffer[i] != (u8)i)
-				return RESULT_FAIL;
+				return MMC_RESULT_FAIL;
 		}
 
 		for (;i < sectors * 512;i++) {
 			if (test->buffer[i] != 0xDF)
-				return RESULT_FAIL;
+				return MMC_RESULT_FAIL;
 		}
 	} else {
 		local_irq_save(flags);
@@ -878,7 +845,7 @@ static int mmc_test_transfer(struct mmc_test_card *test,
 		local_irq_restore(flags);
 		for (i = 0;i < blocks * blksz;i++) {
 			if (test->scratch[i] != (u8)i)
-				return RESULT_FAIL;
+				return MMC_RESULT_FAIL;
 		}
 	}
 
@@ -949,7 +916,7 @@ static int mmc_test_multi_write(struct mmc_test_card *test)
 	struct scatterlist sg;
 
 	if (test->card->host->max_blk_count == 1)
-		return RESULT_UNSUP_HOST;
+		return MMC_RESULT_UNSUP_HOST;
 
 	size = PAGE_SIZE * 2;
 	size = min(size, test->card->host->max_req_size);
@@ -957,7 +924,7 @@ static int mmc_test_multi_write(struct mmc_test_card *test)
 	size = min(size, test->card->host->max_blk_count * 512);
 
 	if (size < 1024)
-		return RESULT_UNSUP_HOST;
+		return MMC_RESULT_UNSUP_HOST;
 
 	sg_init_one(&sg, test->buffer, size);
 
@@ -970,7 +937,7 @@ static int mmc_test_multi_read(struct mmc_test_card *test)
 	struct scatterlist sg;
 
 	if (test->card->host->max_blk_count == 1)
-		return RESULT_UNSUP_HOST;
+		return MMC_RESULT_UNSUP_HOST;
 
 	size = PAGE_SIZE * 2;
 	size = min(size, test->card->host->max_req_size);
@@ -978,7 +945,7 @@ static int mmc_test_multi_read(struct mmc_test_card *test)
 	size = min(size, test->card->host->max_blk_count * 512);
 
 	if (size < 1024)
-		return RESULT_UNSUP_HOST;
+		return MMC_RESULT_UNSUP_HOST;
 
 	sg_init_one(&sg, test->buffer, size);
 
@@ -991,7 +958,7 @@ static int mmc_test_pow2_write(struct mmc_test_card *test)
 	struct scatterlist sg;
 
 	if (!test->card->csd.write_partial)
-		return RESULT_UNSUP_CARD;
+		return MMC_RESULT_UNSUP_CARD;
 
 	for (i = 1; i < 512;i <<= 1) {
 		sg_init_one(&sg, test->buffer, i);
@@ -1009,7 +976,7 @@ static int mmc_test_pow2_read(struct mmc_test_card *test)
 	struct scatterlist sg;
 
 	if (!test->card->csd.read_partial)
-		return RESULT_UNSUP_CARD;
+		return MMC_RESULT_UNSUP_CARD;
 
 	for (i = 1; i < 512;i <<= 1) {
 		sg_init_one(&sg, test->buffer, i);
@@ -1027,7 +994,7 @@ static int mmc_test_weird_write(struct mmc_test_card *test)
 	struct scatterlist sg;
 
 	if (!test->card->csd.write_partial)
-		return RESULT_UNSUP_CARD;
+		return MMC_RESULT_UNSUP_CARD;
 
 	for (i = 3; i < 512;i += 7) {
 		sg_init_one(&sg, test->buffer, i);
@@ -1045,7 +1012,7 @@ static int mmc_test_weird_read(struct mmc_test_card *test)
 	struct scatterlist sg;
 
 	if (!test->card->csd.read_partial)
-		return RESULT_UNSUP_CARD;
+		return MMC_RESULT_UNSUP_CARD;
 
 	for (i = 3; i < 512;i += 7) {
 		sg_init_one(&sg, test->buffer, i);
@@ -1094,7 +1061,7 @@ static int mmc_test_align_multi_write(struct mmc_test_card *test)
 	struct scatterlist sg;
 
 	if (test->card->host->max_blk_count == 1)
-		return RESULT_UNSUP_HOST;
+		return MMC_RESULT_UNSUP_HOST;
 
 	size = PAGE_SIZE * 2;
 	size = min(size, test->card->host->max_req_size);
@@ -1102,7 +1069,7 @@ static int mmc_test_align_multi_write(struct mmc_test_card *test)
 	size = min(size, test->card->host->max_blk_count * 512);
 
 	if (size < 1024)
-		return RESULT_UNSUP_HOST;
+		return MMC_RESULT_UNSUP_HOST;
 
 	for (i = 1; i < TEST_ALIGN_END; i++) {
 		sg_init_one(&sg, test->buffer + i, size);
@@ -1121,7 +1088,7 @@ static int mmc_test_align_multi_read(struct mmc_test_card *test)
 	struct scatterlist sg;
 
 	if (test->card->host->max_blk_count == 1)
-		return RESULT_UNSUP_HOST;
+		return MMC_RESULT_UNSUP_HOST;
 
 	size = PAGE_SIZE * 2;
 	size = min(size, test->card->host->max_req_size);
@@ -1129,7 +1096,7 @@ static int mmc_test_align_multi_read(struct mmc_test_card *test)
 	size = min(size, test->card->host->max_blk_count * 512);
 
 	if (size < 1024)
-		return RESULT_UNSUP_HOST;
+		return MMC_RESULT_UNSUP_HOST;
 
 	for (i = 1; i < TEST_ALIGN_END; i++) {
 		sg_init_one(&sg, test->buffer + i, size);
@@ -1168,7 +1135,7 @@ static int mmc_test_multi_xfersize_write(struct mmc_test_card *test)
 	int ret;
 
 	if (test->card->host->max_blk_count == 1)
-		return RESULT_UNSUP_HOST;
+		return MMC_RESULT_UNSUP_HOST;
 
 	ret = mmc_test_set_blksize(test, 512);
 	if (ret)
@@ -1182,7 +1149,7 @@ static int mmc_test_multi_xfersize_read(struct mmc_test_card *test)
 	int ret;
 
 	if (test->card->host->max_blk_count == 1)
-		return RESULT_UNSUP_HOST;
+		return MMC_RESULT_UNSUP_HOST;
 
 	ret = mmc_test_set_blksize(test, 512);
 	if (ret)
@@ -1219,7 +1186,7 @@ static int mmc_test_multi_write_high(struct mmc_test_card *test)
 	struct scatterlist sg;
 
 	if (test->card->host->max_blk_count == 1)
-		return RESULT_UNSUP_HOST;
+		return MMC_RESULT_UNSUP_HOST;
 
 	size = PAGE_SIZE * 2;
 	size = min(size, test->card->host->max_req_size);
@@ -1227,7 +1194,7 @@ static int mmc_test_multi_write_high(struct mmc_test_card *test)
 	size = min(size, test->card->host->max_blk_count * 512);
 
 	if (size < 1024)
-		return RESULT_UNSUP_HOST;
+		return MMC_RESULT_UNSUP_HOST;
 
 	sg_init_table(&sg, 1);
 	sg_set_page(&sg, test->highmem, size, 0);
@@ -1241,7 +1208,7 @@ static int mmc_test_multi_read_high(struct mmc_test_card *test)
 	struct scatterlist sg;
 
 	if (test->card->host->max_blk_count == 1)
-		return RESULT_UNSUP_HOST;
+		return MMC_RESULT_UNSUP_HOST;
 
 	size = PAGE_SIZE * 2;
 	size = min(size, test->card->host->max_req_size);
@@ -1249,7 +1216,7 @@ static int mmc_test_multi_read_high(struct mmc_test_card *test)
 	size = min(size, test->card->host->max_blk_count * 512);
 
 	if (size < 1024)
-		return RESULT_UNSUP_HOST;
+		return MMC_RESULT_UNSUP_HOST;
 
 	sg_init_table(&sg, 1);
 	sg_set_page(&sg, test->highmem, size, 0);
@@ -1615,10 +1582,10 @@ static int mmc_test_profile_trim_perf(struct mmc_test_card *test)
 	int ret;
 
 	if (!mmc_can_trim(test->card))
-		return RESULT_UNSUP_CARD;
+		return MMC_RESULT_UNSUP_CARD;
 
 	if (!mmc_can_erase(test->card))
-		return RESULT_UNSUP_HOST;
+		return MMC_RESULT_UNSUP_HOST;
 
 	for (sz = 512; sz < t->max_sz; sz <<= 1) {
 		dev_addr = t->dev_addr + (sz >> 9);
@@ -1732,10 +1699,10 @@ static int mmc_test_profile_seq_trim_perf(struct mmc_test_card *test)
 	int ret;
 
 	if (!mmc_can_trim(test->card))
-		return RESULT_UNSUP_CARD;
+		return MMC_RESULT_UNSUP_CARD;
 
 	if (!mmc_can_erase(test->card))
-		return RESULT_UNSUP_HOST;
+		return MMC_RESULT_UNSUP_HOST;
 
 	for (sz = 512; sz <= t->max_sz; sz <<= 1) {
 		ret = mmc_test_area_erase(test);
@@ -2193,11 +2160,11 @@ static int mmc_test_reset(struct mmc_test_card *test)
 
 	err = mmc_hw_reset(host);
 	if (!err)
-		return RESULT_OK;
+		return MMC_RESULT_OK;
 	else if (err == -EOPNOTSUPP)
-		return RESULT_UNSUP_HOST;
+		return MMC_RESULT_UNSUP_HOST;
 
-	return RESULT_FAIL;
+	return MMC_RESULT_FAIL;
 }
 
 static const struct mmc_test_case mmc_test_cases[] = {
@@ -2584,20 +2551,20 @@ static void mmc_test_run(struct mmc_test_card *test, int testcase)
 
 		ret = mmc_test_cases[i].run(test);
 		switch (ret) {
-		case RESULT_OK:
+		case MMC_RESULT_OK:
 			pr_info("%s: Result: OK\n",
 				mmc_hostname(test->card->host));
 			break;
-		case RESULT_FAIL:
+		case MMC_RESULT_FAIL:
 			pr_info("%s: Result: FAILED\n",
 				mmc_hostname(test->card->host));
 			break;
-		case RESULT_UNSUP_HOST:
+		case MMC_RESULT_UNSUP_HOST:
 			pr_info("%s: Result: UNSUPPORTED "
 				"(by host)\n",
 				mmc_hostname(test->card->host));
 			break;
-		case RESULT_UNSUP_CARD:
+		case MMC_RESULT_UNSUP_CARD:
 			pr_info("%s: Result: UNSUPPORTED "
 				"(by card)\n",
 				mmc_hostname(test->card->host));
drivers/mmc/core/core.c +28 -0
index df5a61c..3254bce 100644
@@ -2894,6 +2894,34 @@ int mmc_wait_busy(struct mmc_card *card)
 }
 EXPORT_SYMBOL(mmc_wait_busy);
 
+/*
+ * Checks that a normal transfer didn't have any errors
+ */
+int mmc_check_result(struct mmc_request *mrq)
+{
+	int ret;
+
+	BUG_ON(!mrq || !mrq->cmd || !mrq->data);
+
+	ret = 0;
+
+	if (!ret && mrq->cmd->error)
+		ret = mrq->cmd->error;
+	if (!ret && mrq->data->error)
+		ret = mrq->data->error;
+	if (!ret && mrq->stop && mrq->stop->error)
+		ret = mrq->stop->error;
+	if (!ret && mrq->data->bytes_xfered !=
+		mrq->data->blocks * mrq->data->blksz)
+		ret = MMC_RESULT_FAIL;
+
+	if (ret == -EINVAL)
+		ret = MMC_RESULT_UNSUP_HOST;
+
+	return ret;
+}
+EXPORT_SYMBOL(mmc_check_result);
+
 /**
  * mmc_init_context_info() - init synchronization context
  * @host: mmc host
include/linux/mmc/core.h +8 -0
index 50e37e1..a5ad2d8 100644
@@ -81,6 +81,13 @@ struct mmc_command {
 	unsigned int		retries;	/* max number of retries */
 	int			error;		/* command error */
 
+/* for mmc_check_result() */
+#define MMC_RESULT_OK		0
+#define MMC_RESULT_FAIL		1
+#define MMC_RESULT_UNSUP_HOST	2
+#define MMC_RESULT_UNSUP_CARD	3
+
+
 /*
  * Standard errno values are used for errors, but some have specific
  * meaning in the MMC layer:
@@ -200,6 +207,7 @@ extern void mmc_prepare_mrq(struct mmc_card *card,
 	struct mmc_request *mrq, struct scatterlist *sg, unsigned sg_len,
 	unsigned dev_addr, unsigned blocks, unsigned blksz, int write);
 extern int mmc_wait_busy(struct mmc_card *card);
+extern int mmc_check_result(struct mmc_request *mrq);
 
 /**
  *	mmc_claim_host - exclusively claim a host
Signature · 1 line
2.1.4
#4 lore raw

PATCH 4/6 mmc: move mmc_simple_transfer() to core.c

In reply to Holger Schurig

Currently this function is used inside the mmc test driver. But it is also usable in the (upcoming) firmware update patch. So move this function out of mmc_test.c into core.c.

---
 drivers/mmc/card/mmc_test.c | 38 ++++++--------------------------------
 drivers/mmc/core/core.c     | 27 +++++++++++++++++++++++++++
 include/linux/mmc/core.h    |  3 +++
 3 files changed, 36 insertions(+), 32 deletions(-)
drivers/mmc/card/mmc_test.c +6 -32
index 1d9d997..94298fa 100644
@@ -721,32 +721,6 @@ err:
 }
 
 /*
- * Tests a basic transfer with certain parameters
- */
-static int mmc_test_simple_transfer(struct mmc_test_card *test,
-	struct scatterlist *sg, unsigned sg_len, unsigned dev_addr,
-	unsigned blocks, unsigned blksz, int write)
-{
-	struct mmc_request mrq = {0};
-	struct mmc_command cmd = {0};
-	struct mmc_command stop = {0};
-	struct mmc_data data = {0};
-
-	mrq.cmd = &cmd;
-	mrq.data = &data;
-	mrq.stop = &stop;
-
-	mmc_prepare_mrq(test->card, &mrq, sg, sg_len, dev_addr,
-		blocks, blksz, write);
-
-	mmc_wait_for_req(test->card->host, &mrq);
-
-	mmc_wait_busy(test->card);
-
-	return mmc_check_result(&mrq);
-}
-
-/*
  * Tests a transfer where the card will fail completely or partly
  */
 static int mmc_test_broken_transfer(struct mmc_test_card *test,
@@ -801,7 +775,7 @@ static int mmc_test_transfer(struct mmc_test_card *test,
 	if (ret)
 		return ret;
 
-	ret = mmc_test_simple_transfer(test, sg, sg_len, dev_addr,
+	ret = mmc_simple_transfer(test->card, sg, sg_len, dev_addr,
 		blocks, blksz, write);
 	if (ret)
 		return ret;
@@ -875,7 +849,7 @@ static int mmc_test_basic_write(struct mmc_test_card *test)
 
 	sg_init_one(&sg, test->buffer, 512);
 
-	return mmc_test_simple_transfer(test, &sg, 1, 0, 1, 512, 1);
+	return mmc_simple_transfer(test->card, &sg, 1, 0, 1, 512, 1);
 }
 
 static int mmc_test_basic_read(struct mmc_test_card *test)
@@ -889,7 +863,7 @@ static int mmc_test_basic_read(struct mmc_test_card *test)
 
 	sg_init_one(&sg, test->buffer, 512);
 
-	return mmc_test_simple_transfer(test, &sg, 1, 0, 1, 512, 0);
+	return mmc_simple_transfer(test->card, &sg, 1, 0, 1, 512, 0);
 }
 
 static int mmc_test_verify_write(struct mmc_test_card *test)
@@ -898,7 +872,7 @@ static int mmc_test_verify_write(struct mmc_test_card *test)
 
 	sg_init_one(&sg, test->buffer, 512);
 
-	return mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
+	return mmc_simple_transfer(test->card, &sg, 1, 0, 1, 512, 1);
 }
 
 static int mmc_test_verify_read(struct mmc_test_card *test)
@@ -907,7 +881,7 @@ static int mmc_test_verify_read(struct mmc_test_card *test)
 
 	sg_init_one(&sg, test->buffer, 512);
 
-	return mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
+	return mmc_simple_transfer(test->card, &sg, 1, 0, 1, 512, 0);
 }
 
 static int mmc_test_multi_write(struct mmc_test_card *test)
@@ -1268,7 +1242,7 @@ static int mmc_test_area_transfer(struct mmc_test_card *test,
 {
 	struct mmc_test_area *t = &test->area;
 
-	return mmc_test_simple_transfer(test, t->sg, t->sg_len, dev_addr,
+	return mmc_simple_transfer(test->card, t->sg, t->sg_len, dev_addr,
 					t->blocks, 512, write);
 }
 
drivers/mmc/core/core.c +27 -0
index 3254bce..fbc59ad 100644
@@ -2922,6 +2922,33 @@ int mmc_check_result(struct mmc_request *mrq)
 }
 EXPORT_SYMBOL(mmc_check_result);
 
+/*
+ * Run a basic transfer
+ */
+int mmc_simple_transfer(struct mmc_card *card,
+	struct scatterlist *sg, unsigned sg_len, unsigned dev_addr,
+	unsigned blocks, unsigned blksz, int write)
+{
+	struct mmc_request mrq = {0};
+	struct mmc_command cmd = {0};
+	struct mmc_command stop = {0};
+	struct mmc_data data = {0};
+
+	mrq.cmd = &cmd;
+	mrq.data = &data;
+	mrq.stop = &stop;
+
+	mmc_prepare_mrq(card, &mrq, sg, sg_len, dev_addr,
+		blocks, blksz, write);
+
+	mmc_wait_for_req(card->host, &mrq);
+
+	mmc_wait_busy(card);
+
+	return mmc_check_result(&mrq);
+}
+EXPORT_SYMBOL(mmc_simple_transfer);
+
 /**
  * mmc_init_context_info() - init synchronization context
  * @host: mmc host
include/linux/mmc/core.h +3 -0
index a5ad2d8..774846f 100644
@@ -208,6 +208,9 @@ extern void mmc_prepare_mrq(struct mmc_card *card,
 	unsigned dev_addr, unsigned blocks, unsigned blksz, int write);
 extern int mmc_wait_busy(struct mmc_card *card);
 extern int mmc_check_result(struct mmc_request *mrq);
+extern int mmc_simple_transfer(struct mmc_card *test,
+	struct scatterlist *sg, unsigned sg_len, unsigned dev_addr,
+	unsigned blocks, unsigned blksz, int write);
 
 /**
  *	mmc_claim_host - exclusively claim a host
Signature · 1 line
2.1.4
#5 lore raw

PATCH 5/6 mmc: move mmc_send_cxd_data() to core.c

In reply to Holger Schurig

This function can be used to send ext_csd data towards the chip, which is needed in the (upcoming) firmware update patch.

---
 drivers/mmc/core/mmc_ops.c | 4 ++--
 include/linux/mmc/core.h   | 3 +++
 2 files changed, 5 insertions(+), 2 deletions(-)
drivers/mmc/core/mmc_ops.c +2 -2
index 1f44426..d1d6abc 100644
@@ -287,8 +287,7 @@ mmc_send_cxd_native(struct mmc_host *host, u32 arg, u32 *cxd, int opcode)
  * NOTE: void *buf, caller for the buf is required to use DMA-capable
  * buffer or on-stack buffer (with some overhead in callee).
  */
-static int
-mmc_send_cxd_data(struct mmc_card *card, struct mmc_host *host,
+int mmc_send_cxd_data(struct mmc_card *card, struct mmc_host *host,
 		u32 opcode, void *buf, unsigned len)
 {
 	struct mmc_request mrq = {NULL};
@@ -336,6 +335,7 @@ mmc_send_cxd_data(struct mmc_card *card, struct mmc_host *host,
 
 	return 0;
 }
+EXPORT_SYMBOL(mmc_send_cxd_data);
 
 int mmc_send_csd(struct mmc_card *card, u32 *csd)
 {
include/linux/mmc/core.h +3 -0
index 774846f..b0e0f15 100644
@@ -162,6 +162,9 @@ extern void mmc_start_bkops(struct mmc_card *card, bool from_exception);
 extern int mmc_switch(struct mmc_card *, u8, u8, u8, unsigned int);
 extern int mmc_send_tuning(struct mmc_host *host, u32 opcode, int *cmd_error);
 extern int mmc_get_ext_csd(struct mmc_card *card, u8 **new_ext_csd);
+extern int mmc_send_cxd_data(struct mmc_card *card, struct mmc_host *host,
+		u32 opcode, void *buf, unsigned len);
+
 
 #define MMC_ERASE_ARG		0x00000000
 #define MMC_SECURE_ERASE_ARG	0x80000000
Signature · 1 line
2.1.4
#6 lore raw

PATCH 6/6 mmc: eMMC Field Firmware Update support

In reply to Holger Schurig

The Field Firmware Update (FFU) feature is in the eMMC 5.0 spec, see
http://www.jedec.org/standards-documents/technology-focus-areas/flash-memory-ssds-ufs-emmc/e-mmc)

This adds a new ioctl MMC_FFU_INVOKE to transfer the new Firmware data from user space (via udev firmware request) to the eMMC device and install the new firmware.

This solution allows to:
- complete FFU as an atomic operation, without being interrupted by other IO requests (theoretically the firmware update could be done completely from userspace, just not atomic)
- not limited in firmware data size because it's using multiple write operations
- support of both EXT_CSD_MODE_OPERATION_CODES modes

Almost completely taken from Avi Shchislowsk/Alex Lemberg patch "[PATCH 3/3]mmc: Support-FFU-for-eMMC-v5.0".

---
 drivers/mmc/card/Kconfig   |  11 +
 drivers/mmc/card/Makefile  |   1 +
 drivers/mmc/card/block.c   |   5 +
 drivers/mmc/card/mmc_ffu.c | 487 +++++++++++++++++++++++++++++++++++++++++++++
 include/linux/mmc/card.h   |   1 +
 include/linux/mmc/core.h   |  22 ++
 include/linux/mmc/mmc.h    |   6 +
 7 files changed, 533 insertions(+)
 create mode 100644 drivers/mmc/card/mmc_ffu.c
drivers/mmc/card/Kconfig +11 -0
index 5562308..b37937b 100644
@@ -57,6 +57,17 @@ config SDIO_UART
 	  SDIO function driver for SDIO cards that implements the UART
 	  class, as well as the GPS class which appears like a UART.
 
+config MMC_FFU
+	bool "Field Firmware Update support"
+	depends on MMC != n
+	help
+	  Some eMMC 5.0 devices allow to update their firmware "in the
+	  field". This option enables support for this.
+
+	  If this is compiled in, you can use the mmc utility to request
+	  that the kernel loads the firmware via udev and writes it
+	  to the eMMC.
+
 config MMC_TEST
 	tristate "MMC host test driver"
 	help
drivers/mmc/card/Makefile +1 -0
index c73b406..99a01e8 100644
@@ -4,6 +4,7 @@
 
 obj-$(CONFIG_MMC_BLOCK)		+= mmc_block.o
 mmc_block-objs			:= block.o queue.o
+obj-$(CONFIG_MMC_FFU)		+= mmc_ffu.o
 obj-$(CONFIG_MMC_TEST)		+= mmc_test.o
 
 obj-$(CONFIG_SDIO_UART)		+= sdio_uart.o
drivers/mmc/card/block.c +5 -0
index 23b6c8e..a49cfea 100644
@@ -486,6 +486,11 @@ static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
 	cmd.arg = idata->ic.arg;
 	cmd.flags = idata->ic.flags;
 
+	if (idata->ic.opcode == MMC_FFU_INVOKE_OP) {
+		err = mmc_ffu_invoke(card, idata->buf);
+		goto cmd_done;
+	}
+
 	if (idata->buf_bytes) {
 		data.sg = &sg;
 		data.sg_len = 1;
drivers/mmc/card/mmc_ffu.c new file +487 -0

487 lines not on this page · raw message

include/linux/mmc/card.h +1 -0
index eb0151b..5b7e236 100644
@@ -89,6 +89,7 @@ struct mmc_ext_csd {
 	unsigned int		boot_ro_lock;		/* ro lock support */
 	bool			boot_ro_lockable;
 	bool			ffu_capable;	/* Firmware upgrade support */
+	bool			ffu_mode_op;	/* FFU mode operation */
 #define MMC_FIRMWARE_LEN 8
 	u8			fwrev[MMC_FIRMWARE_LEN];  /* FW version */
 	u8			raw_exception_status;	/* 54 */
include/linux/mmc/core.h +22 -0
index b0e0f15..27cbe75 100644
@@ -230,4 +230,26 @@ struct device_node;
 extern u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max);
 extern int mmc_of_parse_voltage(struct device_node *np, u32 *mask);
 
+/*
+ * eMMC5.0 Field Firmware Update (FFU) opcodes
+*/
+#define MMC_FFU_INVOKE_OP 302
+
+#define MMC_FFU_MODE_SET 0x1
+#define MMC_FFU_MODE_NORMAL 0x0
+#define MMC_FFU_INSTALL_SET 0x2
+
+#ifdef CONFIG_MMC_FFU
+#define MMC_FFU_FEATURES 0x1
+#define FFU_FEATURES(ffu_features) (ffu_features & MMC_FFU_FEATURES)
+
+int mmc_ffu_invoke(struct mmc_card *card, const char *name);
+
+#else
+static inline int mmc_ffu_invoke(struct mmc_card *card, const char *name)
+{
+	return -ENOSYS;
+}
+#endif
+
 #endif /* LINUX_MMC_CORE_H */
include/linux/mmc/mmc.h +6 -0
index 15f2c4a..bb40ebd 100644
@@ -272,6 +272,9 @@ struct _mmc_csd {
  * EXT_CSD fields
  */
 
+#define EXT_CSD_FFU_STATUS		26	/* R */
+#define EXT_CSD_MODE_OPERATION_CODES	29	/* W */
+#define EXT_CSD_MODE_CONFIG		30	/* R/W */
 #define EXT_CSD_FLUSH_CACHE		32      /* W */
 #define EXT_CSD_CACHE_CTRL		33      /* R/W */
 #define EXT_CSD_POWER_OFF_NOTIFICATION	34	/* R/W */
@@ -330,6 +333,9 @@ struct _mmc_csd {
 #define EXT_CSD_CACHE_SIZE		249	/* RO, 4 bytes */
 #define EXT_CSD_PWR_CL_DDR_200_360	253	/* RO */
 #define EXT_CSD_FIRMWARE_VERSION	254	/* RO, 8 bytes */
+#define EXT_CSD_NUM_OF_FW_SEC_PROG	302	/* RO, 4 bytes */
+#define EXT_CSD_FFU_ARG			487	/* RO, 4 bytes */
+#define EXT_CSD_OPERATION_CODE_TIMEOUT	491	/* RO */
 #define EXT_CSD_SUPPORTED_MODE		493	/* RO */
 #define EXT_CSD_TAG_UNIT_SIZE		498	/* RO */
 #define EXT_CSD_DATA_TAG_SUPPORT	499	/* RO */
Signature · 1 line
2.1.4
#7 lore raw
In reply to Holger Schurig
Quoted 30 lines · Holger Schurig

There have been some attempts to add FFU (field firmware update). The last AFAIK in Nov 2014, http://www.spinics.net/lists/linux-mmc/msg29324.html

But it seems that the committers weren't persistent enought.

I took the liberty to take Avi's patch and make it hopefully maintainer-review friendly.

The first 5 patches just move functions out of mmc_test.c into core.c. Those functions will later be used by both mmc_test.c and mmc_ffu.c. Contrary to Avi's patch I didn't add static helper functions to mmc_test.c, e.g. there's no mmc_test_prepare_mrq() that calls mmc_prepare_mrq(). It's simpler to call mmc_prepare_mrq() directly. It's just one more dereference from *mmc_card to *mmc_test_card anyway.

The patch [6/6] is http://www.spinics.net/lists/linux-mmc/msg29326.html, but with less checkpatch warnings. And it doesn't use mmc_send_ext_csd() anymore, which has been deleted since November.

I'm sending this patch as RFC now. It compiles (for me). But I get the firmware update file from Kingston only next Tuesday. That means that so far I haven't been testing it. It won't do anything without the proper user-space command in mmc-utils anyway :-)

Comments welcome (I intent to get this patch into the kernel)

The patch is against Linux GIT (v4.3-11748-g46d862b).

Holger

Quoted 12 lines
 drivers/mmc/card/Kconfig    |  11 +
 drivers/mmc/card/Makefile   |   1 +
 drivers/mmc/card/block.c    |   5 +
 drivers/mmc/card/mmc_ffu.c  | 489 ++++++++++++++++++++++++++++++++++++++++++++
 drivers/mmc/card/mmc_test.c | 235 +++++----------------
 drivers/mmc/core/core.c     | 134 ++++++++++++
 drivers/mmc/core/mmc_ops.c  |   4 +-
 include/linux/mmc/card.h    |   1 +
 include/linux/mmc/core.h    |  41 ++++
 include/linux/mmc/mmc.h     |   6 +
 10 files changed, 739 insertions(+), 188 deletions(-)
 create mode 100644 drivers/mmc/card/mmc_ffu.c

The newly added ioctl MMC_IOC_MULTI_CMD allows user space to send multiple commands
atomically, so mmu-utils may be a better place for this functionality.

Al

#8 lore raw
In reply to Holger Schurig

Hi Holger,

Thank you for resubmitting the FFU patches again.
We did couple of fixes since our last submission (in [RFC 6/6]), so we will post them soon. Also, we would like to leave the original "signed-off-by" names in your FFU patch commits. Could you please add the original:

Thanks,
Avi & Alex

Quoted 54 lines

-----Original Message-----
From: Holger Schurig [mailto:[email protected]]
Sent: Friday, November 13, 2015 4:56 PM
To: Ulf Hansson; [email protected]; [email protected]; Avi Shchislowski; Alex Lemberg
Cc: Holger Schurig
Subject: [RFC 0/6] mmc: Field Firmware Update

There have been some attempts to add FFU (field firmware update). The last AFAIK in Nov 2014, http://www.spinics.net/lists/linux-mmc/msg29324.html

But it seems that the committers weren't persistent enought.

I took the liberty to take Avi's patch and make it hopefully maintainer-review friendly.

The first 5 patches just move functions out of mmc_test.c into core.c. Those functions will later be used by both mmc_test.c and mmc_ffu.c. Contrary to Avi's patch I didn't add static helper functions to mmc_test.c, e.g. there's no mmc_test_prepare_mrq() that calls mmc_prepare_mrq(). It's simpler to call mmc_prepare_mrq() directly. It's just one more dereference from *mmc_card to *mmc_test_card anyway.

The patch [6/6] is http://www.spinics.net/lists/linux-mmc/msg29326.html, but with less checkpatch warnings. And it doesn't use mmc_send_ext_csd() anymore, which has been deleted since November.

I'm sending this patch as RFC now. It compiles (for me). But I get the firmware update file from Kingston only next Tuesday. That means that so far I haven't been testing it. It won't do anything without the proper user-space command in mmc-utils anyway :-)

Comments welcome (I intent to get this patch into the kernel)

The patch is against Linux GIT (v4.3-11748-g46d862b).

Holger

 drivers/mmc/card/Kconfig    |  11 +
 drivers/mmc/card/Makefile   |   1 +
 drivers/mmc/card/block.c    |   5 +
 drivers/mmc/card/mmc_ffu.c  | 489
++++++++++++++++++++++++++++++++++++++++++++
 drivers/mmc/card/mmc_test.c | 235 +++++----------------
 drivers/mmc/core/core.c     | 134 ++++++++++++
 drivers/mmc/core/mmc_ops.c  |   4 +-
 include/linux/mmc/card.h    |   1 +
 include/linux/mmc/core.h    |  41 ++++
 include/linux/mmc/mmc.h     |   6 +
 10 files changed, 739 insertions(+), 188 deletions(-)  create mode 100644

drivers/mmc/card/mmc_ffu.c

--
2.1.4

A message that is not in the archive (sent off-list or not yet received): CAOpc7mHbM8EYYNRKR+pKnzc0wf1DR1ojX-iFuDWDYyquC3EVZg@mail.gmail.com

#10 lore raw

+Chris, Baolin

Quoted 8 lines · Holger Schurig

We had to modify the last patch a bit and was able to update a Kingston device from Firmware 0xba to 0xbd. But when doing this in user-space only is now the way to go, I should probably not submit it anymore?

BTW, for this Kingston "EMMC16G-WL110-EBAU" the argument in the ext_csd is bogus. You need to use the "ffu_arg" hack from the mmc-utils patch and use argument 0x0000ffff. Sigh. They can't even stick to the standard.

Quoted 5 lines
Quoted 1 line

Could you please add the original:

Sorry, yes I could. But I won't, because if this can now be done reliably from user-space, there is no need to do this in kernel. So I guess the chances that a kernel solution get's applies is next to zero.

Hi Holger,

Sorry for the delay.

My advise right now is to try this out via the mmc ioctl in userspace, yes. Although, if you encounter any issues with that approach that it might not be reliable, I am open to look into the in-kernel solution again.

Regarding mmc-utils as where I recommend you to implement this, I have been thinking of moving this tool into the tools directory in the kernel.

Baolin Wang, at Linaro is currently looking into this to see if it's feasible. Ideas/comments are usual greatly appreciated.

Kind regards
Uffe

#11 lore raw
Quoted 2 lines

Sorry for the delay.

No problem, I was busy with many other projects as well.

Quoted 4 lines

My advise right now is to try this out via the mmc ioctl in userspace, yes. Although, if you encounter any issues with that approach that it might not be reliable, I am open to look into the in-kernel solution again.

I managed to update my Kingston eMMCs with a slighly modified patch to what I submitted. I however didn't bother to submit this, as I saw no chance of getting it applied.

Also I once asked in the mailing list if there is some user-space example of how to use the multi-block feature that is supposed to enable this, but I haven't gotten an answer.

Quoted 3 lines

Regarding mmc-utils as where I recommend you to implement this, I have been thinking of moving this tool into the tools directory in the kernel.

Sounds good to me.

Remotely related:

Do you know that some google people made their own version of mmc-utils for ChromeOS? And the don't seem to give much effort in unification of them? As if the world wouldn't know how hard it can be to re-unite things again, Google should know from their custom kernels they use on Android ... Sigh.

So you can

* git clone git://git.kernel.org/pub/scm/linux/kernel/git/cjb/mmc-utils.git
* git clone https://chromium.googlesource.com/chromiumos/third_party/mmc-utils

currently.

#12 lore raw

+ Gwendal, Grant, Olof, Seshagiri, Jon

Quoted 35 lines · Holger Schurig
Quoted 2 lines

Sorry for the delay.

No problem, I was busy with many other projects as well.

Quoted 4 lines

My advise right now is to try this out via the mmc ioctl in userspace, yes. Although, if you encounter any issues with that approach that it might not be reliable, I am open to look into the in-kernel solution again.

I managed to update my Kingston eMMCs with a slighly modified patch to what I submitted. I however didn't bother to submit this, as I saw no chance of getting it applied.

Also I once asked in the mailing list if there is some user-space example of how to use the multi-block feature that is supposed to enable this, but I haven't gotten an answer.

Quoted 3 lines

Regarding mmc-utils as where I recommend you to implement this, I have been thinking of moving this tool into the tools directory in the kernel.

Sounds good to me.

Remotely related:

Do you know that some google people made their own version of mmc-utils for ChromeOS? And the don't seem to give much effort in unification of them? As if the world wouldn't know how hard it can be to re-unite things again, Google should know from their custom kernels they use on Android ... Sigh.

Quoted 4 lines

So you can

* git clone git://git.kernel.org/pub/scm/linux/kernel/git/cjb/mmc-utils.git
* git clone https://chromium.googlesource.com/chromiumos/third_party/mmc-utils

Thanks for sharing the information. Some Chromium folkz has certainly been helpful in upstreaming activities, but you are right that there is more to be done for mmc utils.

Perhaps by moving the code into the kernel tools dir and finding someone who actively wants to maintain it, will enable improved upstream activities!?

Kind regards
Uffe

#13 lore raw

+ Olof (forgot to add him last time)

Quoted 53 lines · Ulf Hansson

+ Gwendal, Grant, Olof, Seshagiri, Jon

Quoted 35 lines · Holger Schurig
Quoted 2 lines

Sorry for the delay.

No problem, I was busy with many other projects as well.

Quoted 4 lines

My advise right now is to try this out via the mmc ioctl in userspace, yes. Although, if you encounter any issues with that approach that it might not be reliable, I am open to look into the in-kernel solution again.

I managed to update my Kingston eMMCs with a slighly modified patch to what I submitted. I however didn't bother to submit this, as I saw no chance of getting it applied.

Also I once asked in the mailing list if there is some user-space example of how to use the multi-block feature that is supposed to enable this, but I haven't gotten an answer.

Quoted 3 lines

Regarding mmc-utils as where I recommend you to implement this, I have been thinking of moving this tool into the tools directory in the kernel.

Sounds good to me.

Remotely related:

Do you know that some google people made their own version of mmc-utils for ChromeOS? And the don't seem to give much effort in unification of them? As if the world wouldn't know how hard it can be to re-unite things again, Google should know from their custom kernels they use on Android ... Sigh.

Quoted 4 lines

So you can

* git clone git://git.kernel.org/pub/scm/linux/kernel/git/cjb/mmc-utils.git
* git clone https://chromium.googlesource.com/chromiumos/third_party/mmc-utils

Thanks for sharing the information. Some Chromium folkz has certainly been helpful in upstreaming activities, but you are right that there is more to be done for mmc utils.

Perhaps by moving the code into the kernel tools dir and finding someone who actively wants to maintain it, will enable improved upstream activities!?

Kind regards
Uffe

#14 lore raw

[sigh, in cleartext this time.]

2015-12-22 0:57 GMT-08:00 Ulf Hansson <[email protected]>:

Quoted 36 lines

+ Olof (forgot to add him last time)

Quoted 33 lines · Ulf Hansson

+ Gwendal, Grant, Olof, Seshagiri, Jon

Quoted 30 lines · Holger Schurig
Quoted 2 lines

Sorry for the delay.

No problem, I was busy with many other projects as well.

Quoted 4 lines

My advise right now is to try this out via the mmc ioctl in userspace, yes. Although, if you encounter any issues with that approach that it might not be reliable, I am open to look into the in-kernel solution again.

I managed to update my Kingston eMMCs with a slighly modified patch to what I submitted. I however didn't bother to submit this, as I saw no chance of getting it applied.

Also I once asked in the mailing list if there is some user-space example of how to use the multi-block feature that is supposed to enable this, but I haven't gotten an answer.

Quoted 3 lines

Regarding mmc-utils as where I recommend you to implement this, I have been thinking of moving this tool into the tools directory in the kernel.

Sounds good to me.

Remotely related:

Do you know that some google people made their own version of mmc-utils for ChromeOS?

"Make their own version" makes it sound like we've rewritten it. We haven't. We've applied some patches locally over the last two years based on work we've done for our products. It's not a deep fork.

We'd be happy to upstream them if there's interest from other users and maintainers.

Quoted 3 lines
Quoted 3 lines

Perhaps by moving the code into the kernel tools dir and finding someone who actively wants to maintain it, will enable improved upstream activities!?

Hmm. If there's a well-defined userspace interface there's little reason to bundle it directly, and I think that's the case here.

Chris has been busy with other stuff the last couple of years, is there a lot of mmc-utils code that's out there that hasn't been picked up?

-Olof

A message that is not in the archive (sent off-list or not yet received): CAG=SpYGf7czPwLrJVrq49n_Lhph33fmyKq-Nkm6=c0fB1Gn9_w@mail.gmail.com

#16 lore raw
Quoted 4 lines · Chris Ball

Hi,

Quoted 2 lines

Chris has been busy with other stuff the last couple of years, is there a lot of mmc-utils code that's out there that hasn't been picked up?

I don't really know.

Although, I wanted to make sure that the tool still was still being maintained as otherwise people sooner or later will not care to send patches.

Quoted 5 lines

I've been applying mmc-utils patches as I get them -- last push was five weeks ago. So I don't have a backlog, but if people are sending mmc-utils patches to lists rather than to me directly, I'm unlikely to see them there.

I haven't bothered much at looking at mmc utils patches, although I have noticed some being posted without any response or being picked up.
I browsed the history of these patches and noticed that it's exactly as you say. These haven't been sent directly you or they were sent to an incorrect email address.

I apologize if I was giving the wrong impression that you didn't maintain mmc utils very well, you certainly are!

Perhaps we should add some information about it in the mmc kernel doc, to make it more visible.

Kind regards
Uffe

#17 lore raw
In reply to Holger Schurig

Hi Ulf,

We succeeded to run FFU via new mmc multi-command ioctl without any code modification, but only by using Single Sector commands (CMD24).

Quoted 1 line

From running the FFU and from code review, we see two minor issues in this way of running FFU:

1. There is no support for Multiple Block write commands (CMD25) in existing IOCTL implementation - seems like there is no polling for the card status on data transfer completion.
(The kernel FFU implementation supports FFU using Multiple Block Write commands).
2. As you probably remember, there are two ways to install the new FW in the end of FFU process - In case MODE_OPERATION_CODES field is not supported by the device, the host sets to NORMAL state and initiates a CMD0/HW_Reset/Power cycle to install the new firmware.
This sequence cannot be done via multi-command ioctl, and requires manual reset of the device/platform. (The kernel FFU implementation supports both FW install methods).

For running FFU via new mmc multi-command ioctl, we have modified mmc-utils and add new functionality for FFU. Please let us know if you want us to submit the patch for mmc-utils FFU functionality via multi-command ioctl.

Best regards,
Alex

Quoted 51 lines

-----Original Message-----
From: Holger Schurig [mailto:[email protected]]
Sent: Tuesday, December 22, 2015 10:15 AM
To: Ulf Hansson
Cc: Avi Shchislowski; [email protected]; linux- [email protected]; Alex Lemberg; Chris Ball; Baolin Wang
Subject: Re: [RFC 0/6] mmc: Field Firmware Update

Quoted 1 line

Sorry for the delay.

No problem, I was busy with many other projects as well.

Quoted 4 lines

My advise right now is to try this out via the mmc ioctl in userspace, yes. Although, if you encounter any issues with that approach that it might not be reliable, I am open to look into the in-kernel solution again.

I managed to update my Kingston eMMCs with a slighly modified patch to what I submitted. I however didn't bother to submit this, as I saw no chance of getting it applied.

Also I once asked in the mailing list if there is some user-space example of how to use the multi-block feature that is supposed to enable this, but I haven't gotten an answer.

Quoted 3 lines

Regarding mmc-utils as where I recommend you to implement this, I have been thinking of moving this tool into the tools directory in the kernel.

Sounds good to me.

Remotely related:

Do you know that some google people made their own version of mmc-utils for ChromeOS? And the don't seem to give much effort in unification of them? As if the world wouldn't know how hard it can be to re-unite things again, Google should know from their custom kernels they use on Android ... Sigh.

So you can

* git clone git://git.kernel.org/pub/scm/linux/kernel/git/cjb/mmc-utils.git
* git clone
https://chromium.googlesource.com/chromiumos/third_party/mmc-utils

currently.

#18 lore raw
Quoted 7 lines · Alex Lemberg

Hi Ulf,

We succeeded to run FFU via new mmc multi-command ioctl without any code modification, but only by using Single Sector commands (CMD24).

From running the FFU and from code review, we see two minor issues in this way of running FFU:
1. There is no support for Multiple Block write commands (CMD25) in existing IOCTL implementation -

That's right. But I guess we cope without the multiple block support!?

Although, I wonder how hard it would be to add it...

Quoted 1 line

seems like there is no polling for the card status on data transfer completion.

We should fix that!

In the rpmb case, we check the status so we can probably trigger that code to run for CMD24/25 as well.

Quoted 3 lines

(The kernel FFU implementation supports FFU using Multiple Block Write commands).
2. As you probably remember, there are two ways to install the new FW in the end of FFU process - In case MODE_OPERATION_CODES field is not supported by the device, the host sets to NORMAL state

Before starting the update, you can find out which mode that is supported and take relevant actions, right?

Quoted 1 line

and initiates a CMD0/HW_Reset/Power cycle to install the new firmware.

Yes, but that's fragile - as discussed earlier.

What we really need to do is to also remove the "card" device from the system, as otherwise we may have invalid data in its member variables and who knows what issues that can cause to upper levels.

Quoted 1 line

This sequence cannot be done via multi-command ioctl, and requires manual reset of the device/platform.

Yepp, it seems so at least for now. Perhaps we can think of a way to improve this?

Quoted 4 lines

(The kernel FFU implementation supports both FW install methods).

For running FFU via new mmc multi-command ioctl, we have modified mmc-utils and add new functionality for FFU. Please let us know if you want us to submit the patch for mmc-utils FFU functionality via multi-command ioctl.

Yes please. Don't forget to send this to Chris as well!

[...]

Kind regards
Uffe

#19 lore raw
Quoted 40 lines · Ulf Hansson
Quoted 7 lines · Alex Lemberg

Hi Ulf,

We succeeded to run FFU via new mmc multi-command ioctl without any code modification, but only by using Single Sector commands (CMD24).

From running the FFU and from code review, we see two minor issues in this way of running FFU:
1. There is no support for Multiple Block write commands (CMD25) in existing IOCTL implementation -

That's right. But I guess we cope without the multiple block support!?

Although, I wonder how hard it would be to add it...

Quoted 1 line

seems like there is no polling for the card status on data transfer completion.

We should fix that!

In the rpmb case, we check the status so we can probably trigger that code to run for CMD24/25 as well.

Quoted 3 lines

(The kernel FFU implementation supports FFU using Multiple Block Write commands).
2. As you probably remember, there are two ways to install the new FW in the end of FFU process - In case MODE_OPERATION_CODES field is not supported by the device, the host sets to NORMAL state

Before starting the update, you can find out which mode that is supported and take relevant actions, right?

Quoted 1 line

and initiates a CMD0/HW_Reset/Power cycle to install the new firmware.

Yes, but that's fragile - as discussed earlier.

What we really need to do is to also remove the "card" device from the system, as otherwise we may have invalid data in its member variables and who knows what issues that can cause to upper levels.

Quoted 1 line

This sequence cannot be done via multi-command ioctl, and requires manual reset of the device/platform.

Yepp, it seems so at least for now. Perhaps we can think of a way to improve this?

Quoted 6 lines
Quoted 4 lines

(The kernel FFU implementation supports both FW install methods).

For running FFU via new mmc multi-command ioctl, we have modified mmc-utils and add new functionality for FFU. Please let us know if you want us to submit the patch for mmc-utils FFU functionality via multi-command ioctl.

Yes please. Don't forget to send this to Chris as well!

I am arriving after the battle, but I have finally rebased the eMMC FFU kernel ffu code to 4.x. It is based on what Avi and Alex have written.
As stated earlier, the advantage over using MMC_MUTLI_CMD is we can force a reset and rescan of the card without asking the user to reboot their machine.
Also, by only sending a firmware name over the ioctl, we can use Kees' work for firmware validation (https://lwn.net/Articles/605432/).
To prevent downloading firmware from unknown source, we would reject some commands (like SWITCH with FFU_MODE) in the kernel MMC_IOC/MULTI_CMD ioctl handler.

Gwendal.

Quoted 9 lines

[...]

Kind regards
Uffe
--
To unsubscribe from this list: send the line "unsubscribe linux-mmc" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#20 lore raw
Quoted 46 lines · Gwendal Grignou
Quoted 45 lines · Ulf Hansson
Quoted 7 lines · Alex Lemberg

Hi Ulf,

We succeeded to run FFU via new mmc multi-command ioctl without any code modification, but only by using Single Sector commands (CMD24).

From running the FFU and from code review, we see two minor issues in this way of running FFU:
1. There is no support for Multiple Block write commands (CMD25) in existing IOCTL implementation -

That's right. But I guess we cope without the multiple block support!?

Although, I wonder how hard it would be to add it...

Quoted 1 line

seems like there is no polling for the card status on data transfer completion.

We should fix that!

In the rpmb case, we check the status so we can probably trigger that code to run for CMD24/25 as well.

Quoted 3 lines

(The kernel FFU implementation supports FFU using Multiple Block Write commands).
2. As you probably remember, there are two ways to install the new FW in the end of FFU process - In case MODE_OPERATION_CODES field is not supported by the device, the host sets to NORMAL state

Before starting the update, you can find out which mode that is supported and take relevant actions, right?

Quoted 1 line

and initiates a CMD0/HW_Reset/Power cycle to install the new firmware.

Yes, but that's fragile - as discussed earlier.

What we really need to do is to also remove the "card" device from the system, as otherwise we may have invalid data in its member variables and who knows what issues that can cause to upper levels.

Quoted 1 line

This sequence cannot be done via multi-command ioctl, and requires manual reset of the device/platform.

Yepp, it seems so at least for now. Perhaps we can think of a way to improve this?

Quoted 4 lines

(The kernel FFU implementation supports both FW install methods).

For running FFU via new mmc multi-command ioctl, we have modified mmc-utils and add new functionality for FFU. Please let us know if you want us to submit the patch for mmc-utils FFU functionality via multi-command ioctl.

Quoted 7 lines
Quoted 1 line

Yes please. Don't forget to send this to Chris as well!

I am arriving after the battle, but I have finally rebased the eMMC FFU kernel ffu code to 4.x. It is based on what Avi and Alex have written.
As stated earlier, the advantage over using MMC_MUTLI_CMD is we can force a reset and rescan of the card without asking the user to reboot their machine.

No matter what, I think the problem is how you would *safely* deal with the reset. Especially in the case when the eMMC already has an mounted file system on it.

Just doing something that *might* work, isn't good enough to me.

Quoted 2 lines

Also, by only sending a firmware name over the ioctl, we can use Kees' work for firmware validation (https://lwn.net/Articles/605432/).

The request_firmware() interface would indeed be good to use. Although unless we can figure out a way on how to safely deal with reset, we will have to live without request_firmware().

Quoted 3 lines

To prevent downloading firmware from unknown source, we would reject some commands (like SWITCH with FFU_MODE) in the kernel MMC_IOC/MULTI_CMD ioctl handler.

I don't follow, can you elaborate on this please.

Kind regards
Uffe

#21 lore raw
Quoted 12 lines · Ulf Hansson
Quoted 9 lines · Gwendal Grignou
Quoted 1 line · Ulf Hansson

On 28 December 2015 at 15:12, Alex Lemberg <[email protected]> wrote:

I am arriving after the battle, but I have finally rebased the eMMC FFU kernel ffu code to 4.x. It is based on what Avi and Alex have written.
As stated earlier, the advantage over using MMC_MUTLI_CMD is we can force a reset and rescan of the card without asking the user to reboot their machine.

Quoted 3 lines

No matter what, I think the problem is how you would *safely* deal with the reset. Especially in the case when the eMMC already has an mounted file system on it.

Assuming the firmware is not wiping data or resizing the available space, the data in the flash is readable after the upgrade. For the host point of view, a firmware update and a reset is equivalent to a reset, that could happen during error recovery. The only change are in the cid/csd//extcsd registers the firmware may have updated.
The stack has to assume these registers are not constant and can change after reset.

When looking into the mmc stack, AFAICT, the code that needs to get device specifics always rely on fields that are re-generated by mmc_card_inif() (card->ext_csd, output of mmc_decode_csd()/cid(() and so on).

Quoted 11 lines

Just doing something that *might* work, isn't good enough to me.

Quoted 2 lines

Also, by only sending a firmware name over the ioctl, we can use Kees' work for firmware validation (https://lwn.net/Articles/605432/).

The request_firmware() interface would indeed be good to use. Although unless we can figure out a way on how to safely deal with reset, we will have to live without request_firmware().

Quoted 5 lines
Quoted 3 lines

To prevent downloading firmware from unknown source, we would reject some commands (like SWITCH with FFU_MODE) in the kernel MMC_IOC/MULTI_CMD ioctl handler.

I don't follow, can you elaborate on this please.

Today, an attacker with root access could break the chain of trust by writing a firmware in the eMMC that corrupts data on the fly and return infected code to the host after verification.
One way is to use firmware signed by the manufacturer, a stronger approach is to enforce that the firmware is part of the root partition.
To prevent a bad firmware from being downloaded, we have to make sure downloading firmware using raw single or multi commands ioctls does not work.

Gwendal.

Quoted 4 lines

Kind regards
Uffe

#22 lore raw
Quoted 13 lines · Gwendal Grignou
Quoted 12 lines · Ulf Hansson
Quoted 9 lines · Gwendal Grignou
Quoted 1 line · Ulf Hansson

On 28 December 2015 at 15:12, Alex Lemberg <[email protected]> wrote:

I am arriving after the battle, but I have finally rebased the eMMC FFU kernel ffu code to 4.x. It is based on what Avi and Alex have written.
As stated earlier, the advantage over using MMC_MUTLI_CMD is we can force a reset and rescan of the card without asking the user to reboot their machine.

Quoted 6 lines
Quoted 3 lines

No matter what, I think the problem is how you would *safely* deal with the reset. Especially in the case when the eMMC already has an mounted file system on it.

Assuming the firmware is not wiping data or resizing the available space, the data in the flash is readable after the upgrade.

This is exactly my point. You can no* assume anything about the card after a firmware upgrade.

Quoted 4 lines

For the host point of view, a firmware update and a reset is equivalent to a reset, that could happen during error recovery. The only change are in the cid/csd//extcsd registers the firmware may have updated.

Is that defined by the spec and are all eMMC vendors conforming to your above statement?

Quoted 24 lines

The stack has to assume these registers are not constant and can change after reset.

When looking into the mmc stack, AFAICT, the code that needs to get device specifics always rely on fields that are re-generated by mmc_card_inif() (card->ext_csd, output of mmc_decode_csd()/cid(() and so on).

Quoted 11 lines

Just doing something that *might* work, isn't good enough to me.

Quoted 2 lines

Also, by only sending a firmware name over the ioctl, we can use Kees' work for firmware validation (https://lwn.net/Articles/605432/).

The request_firmware() interface would indeed be good to use. Although unless we can figure out a way on how to safely deal with reset, we will have to live without request_firmware().

Quoted 5 lines
Quoted 3 lines

To prevent downloading firmware from unknown source, we would reject some commands (like SWITCH with FFU_MODE) in the kernel MMC_IOC/MULTI_CMD ioctl handler.

I don't follow, can you elaborate on this please.

Quoted 9 lines

Today, an attacker with root access could break the chain of trust by writing a firmware in the eMMC that corrupts data on the fly and return infected code to the host after verification.
One way is to use firmware signed by the manufacturer, a stronger approach is to enforce that the firmware is part of the root partition.
To prevent a bad firmware from being downloaded, we have to make sure downloading firmware using raw single or multi commands ioctls does not work.

I clearly see the benefit of using request_firmware() and I open to adopt an in-kernel FFU solution that uses it, as long as a safe reset can be managed.

However, whether it's more safe to hackers has nothing to do with it. If a hacker becomes root on a device they can do all kind of magic things, for example replacing a firmware in rootfs or sending ioctl commands to a device node that has root permissions. To achieve security, verification of a signatures are needed and currently the request_firmware() API doesn't support this and nor does the eMMC device itself (at least to my knowledge).

Regarding the safe reset, the only way I see how to deal with this, is to force a reboot and prevent serving new read/write request after a firmware upgrade. Although, perhaps you can think of something more clever.

Kind regards
Uffe