From 39ae954b04ef2fc4a8c14410379b663deb391fde Mon Sep 17 00:00:00 2001 From: Shiji Yang Date: Fri, 25 Jul 2025 08:20:44 +0800 Subject: [PATCH 1/2] env: mtd: add the missing put_mtd_device() The mtd device is got in setup_mtd_device(), we must put the mtd device before exiting the function to update the mtd use count. This patch fixes the following env error: > Removing MTD device #2 (u-boot-env) with use count 1 > Error when deleting partition "u-boot-env" (-16) Fixes: 03fb08d4aef8 ("env: Introduce support for MTD") Signed-off-by: Shiji Yang --- env/mtd.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/env/mtd.c b/env/mtd.c index d7ec30e183a..ad263ed4b29 100644 --- a/env/mtd.c +++ b/env/mtd.c @@ -131,6 +131,8 @@ static int env_mtd_save(void) puts("done\n"); done: + put_mtd_device(mtd_env); + if (saved_buf) free(saved_buf); @@ -188,6 +190,8 @@ static int env_mtd_load(void) gd->env_valid = ENV_VALID; out: + put_mtd_device(mtd_env); + free(buf); return ret; @@ -280,6 +284,8 @@ static int env_mtd_erase(void) ret = 0; done: + put_mtd_device(mtd_env); + if (saved_buf) free(saved_buf); From 7e842bd33154946052068195df3446635236d3d9 Mon Sep 17 00:00:00 2001 From: Shiji Yang Date: Fri, 25 Jul 2025 08:20:45 +0800 Subject: [PATCH 2/2] env: mtd: initialize saved_buf pointer When sect_size is greater than the CONFIG_ENV_SIZE, this wild pointer will cause CPU halt or system crash. Fixes: 03fb08d4aef8 ("env: Introduce support for MTD") Signed-off-by: Shiji Yang --- env/mtd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/env/mtd.c b/env/mtd.c index ad263ed4b29..b26ee80985a 100644 --- a/env/mtd.c +++ b/env/mtd.c @@ -201,7 +201,7 @@ static int env_mtd_erase(void) { struct mtd_info *mtd_env; u32 sect_size, sect_num; - char *saved_buf, *tmp; + char *saved_buf = NULL, *tmp; struct erase_info ei; size_t ret_len; int remaining;