[U-Boot] [PATCH] mmc: add eraseenv command

Frank Wunderlich frank-w at public-files.de
Sat Apr 6 18:12:35 UTC 2019


"mmc eraseenv" allows to erase the section on mmc where env is stored

Signed-off-by: Frank Wunderlich <frank-w at public-files.de>
---
 cmd/mmc.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/cmd/mmc.c b/cmd/mmc.c
index 8bc3648193..b8e2c353b4 100644
--- a/cmd/mmc.c
+++ b/cmd/mmc.c
@@ -441,6 +441,37 @@ static int do_mmc_erase(cmd_tbl_t *cmdtp, int flag,

 	return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
 }
+
+static int do_mmc_erase_env(cmd_tbl_t *cmdtp, int flag,
+			    int argc, char * const argv[])
+{
+	struct mmc *mmc;
+	u32 blk, cnt, n;
+
+	if (argc != 1)
+		return CMD_RET_USAGE;
+
+	mmc = init_mmc_device(curr_device, false);
+
+	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%8x), count %d (0x%8x)",
+	       curr_device, 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

 static int do_mmc_rescan(cmd_tbl_t *cmdtp, int flag,
@@ -878,6 +909,7 @@ static cmd_tbl_t cmd_mmc[] = {
 #if CONFIG_IS_ENABLED(MMC_WRITE)
 	U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""),
 	U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""),
+	U_BOOT_CMD_MKENT(eraseenv, 1, 0, do_mmc_erase_env, "", ""),
 #endif
 #if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
 	U_BOOT_CMD_MKENT(swrite, 3, 0, do_mmc_sparse_write, "", ""),
@@ -940,6 +972,7 @@ U_BOOT_CMD(
 	"mmc swrite addr blk#\n"
 #endif
 	"mmc erase blk# cnt\n"
+	"mmc eraseenv - erase environment\n"
 	"mmc rescan\n"
 	"mmc part - lists available partition on current mmc device\n"
 	"mmc dev [dev] [part] - show or set current mmc device [partition]\n"
--
2.17.1



More information about the U-Boot mailing list