[U-Boot] [PATCH v4 2/4] env: mmc: add erase-function

Frank Wunderlich frank-w at public-files.de
Sun Apr 28 08:51:26 UTC 2019


this adds erase environment for mmc storage

Signed-off-by: Frank Wunderlich <frank-w at public-files.de>

squashed fixes:
 - fix bogus indent
 - add CONFIG_CMD_ERASEENV

Suggested-by: Simon Goldschmidt <simon.k.r.goldschmidt at gmail.com>
---
 env/mmc.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/env/mmc.c b/env/mmc.c
index c3cf35d01b..9ae9b1a66a 100644
--- a/env/mmc.c
+++ b/env/mmc.c
@@ -242,6 +242,34 @@ fini:
 	fini_mmc_for_env(mmc);
 	return ret;
 }
+
+#if defined(CONFIG_CMD_ERASEENV)
+static int env_mmc_erase(void)
+{
+	int dev = mmc_get_env_dev();
+	struct mmc *mmc = find_mmc_device(dev);
+	int n, blk, cnt;
+
+	if (!mmc)
+		return CMD_RET_FAILURE;
+
+	blk = CONFIG_ENV_OFFSET / mmc->read_bl_len;
+	cnt = CONFIG_ENV_SIZE / mmc->read_bl_len;
+
+	printf("\nMMC erase env: dev # %d, block # %d (0x%x), count %d (0x%x)\n",
+	       dev, blk, blk * mmc->read_bl_len,
+	       cnt, cnt * mmc->read_bl_len);
+
+	if (mmc_getwp(mmc) == 1) {
+		printf("Error: card is write protected!\n");
+		return CMD_RET_FAILURE;
+	}
+	n = blk_derase(mmc_get_blk_desc(mmc), blk, cnt);
+	printf("%d blocks erased: %s\n", n, (n == cnt) ? "OK" : "ERROR");
+
+	return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
+}
+#endif /* CONFIG_CMD_ERASEENV */
 #endif /* CONFIG_CMD_SAVEENV && !CONFIG_SPL_BUILD */

 static inline int read_env(struct mmc *mmc, unsigned long size,
@@ -351,5 +379,8 @@ U_BOOT_ENV_LOCATION(mmc) = {
 	.load		= env_mmc_load,
 #ifndef CONFIG_SPL_BUILD
 	.save		= env_save_ptr(env_mmc_save),
+#if defined(CONFIG_CMD_ERASEENV)
+	.erase		= env_mmc_erase,
+#endif
 #endif
 };
--
2.17.1



More information about the U-Boot mailing list