From ff8b0cf14b6525b1c70256647845e7f26f07c28b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20M=C3=BAdry?= Date: Thu, 16 Nov 2023 21:21:33 +0100 Subject: [PATCH] fix(storage): FATFS WL function formatting wrong partition Closes https://github.com/espressif/esp-idf/issues/12542 Co-authored-by: Tony Stuart --- .../flash_wl/main/test_fatfs_flash_wl.c | 24 +++++++++++++++++++ .../fatfs/test_apps/flash_wl/partitions.csv | 3 ++- components/fatfs/vfs/vfs_fat_sdmmc.c | 8 ++++--- components/fatfs/vfs/vfs_fat_spiflash.c | 9 +++---- 4 files changed, 36 insertions(+), 8 deletions(-) diff --git a/components/fatfs/test_apps/flash_wl/main/test_fatfs_flash_wl.c b/components/fatfs/test_apps/flash_wl/main/test_fatfs_flash_wl.c index a56b789507..bb5b3ceb96 100644 --- a/components/fatfs/test_apps/flash_wl/main/test_fatfs_flash_wl.c +++ b/components/fatfs/test_apps/flash_wl/main/test_fatfs_flash_wl.c @@ -60,6 +60,30 @@ TEST_CASE("(WL) can format when the FAT is mounted already", "[fatfs][wear_level test_teardown(); } +TEST_CASE("(WL) can format specified FAT when more are mounted", "[fatfs][wear_levelling][timeout=180]") +{ + esp_vfs_fat_sdmmc_mount_config_t mount_config = { + .format_if_mount_failed = true, + .max_files = 5, + }; + wl_handle_t s_test_wl_handle1; + wl_handle_t s_test_wl_handle2; + TEST_ESP_OK(esp_vfs_fat_spiflash_mount_rw_wl("/spiflash1", "storage", &mount_config, &s_test_wl_handle1)); + TEST_ESP_OK(esp_vfs_fat_spiflash_mount_rw_wl("/spiflash2", "storage2", &mount_config, &s_test_wl_handle2)); + + test_fatfs_create_file_with_text("/spiflash1/hello.txt", fatfs_test_hello_str); + test_fatfs_create_file_with_text("/spiflash2/hello.txt", fatfs_test_hello_str); + + TEST_ESP_OK(esp_vfs_fat_spiflash_format_rw_wl("/spiflash2", "storage2")); + + FILE* f = fopen("/spiflash2/hello.txt", "r"); + TEST_ASSERT_NULL(f); // File is erased on the formatted FAT + test_fatfs_pread_file("/spiflash1/hello.txt"); // File is still readable on the other FAT + + TEST_ESP_OK(esp_vfs_fat_spiflash_unmount_rw_wl("/spiflash1", s_test_wl_handle1)); + TEST_ESP_OK(esp_vfs_fat_spiflash_unmount_rw_wl("/spiflash2", s_test_wl_handle2)); +} + TEST_CASE("(WL) can create and write file", "[fatfs][wear_levelling]") { test_setup(); diff --git a/components/fatfs/test_apps/flash_wl/partitions.csv b/components/fatfs/test_apps/flash_wl/partitions.csv index a929971141..d1dcbae61d 100644 --- a/components/fatfs/test_apps/flash_wl/partitions.csv +++ b/components/fatfs/test_apps/flash_wl/partitions.csv @@ -1,3 +1,4 @@ # Name, Type, SubType, Offset, Size, Flags -factory, app, factory, 0x10000, 1M, +factory, app, factory, 0x10000, 768k, storage, data, fat, , 528k, +storage2, data, fat, , 528k, diff --git a/components/fatfs/vfs/vfs_fat_sdmmc.c b/components/fatfs/vfs/vfs_fat_sdmmc.c index f3625a2f0e..cbcdd32601 100644 --- a/components/fatfs/vfs/vfs_fat_sdmmc.c +++ b/components/fatfs/vfs/vfs_fat_sdmmc.c @@ -1,11 +1,12 @@ /* - * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ #include #include +#include "esp_check.h" #include "esp_log.h" #include "esp_vfs.h" #include "esp_vfs_fat.h" @@ -469,7 +470,8 @@ esp_err_t esp_vfs_fat_sdcard_format(const char *base_path, sdmmc_card_t *card) //unmount char drv[3] = {(char)('0' + pdrv), ':', 0}; - f_mount(0, drv, 0); + FRESULT res = f_mount(0, drv, 0); + ESP_RETURN_ON_FALSE(res != FR_INVALID_DRIVE, ESP_FAIL, TAG, "f_mount unmount failed (%d) - the logical drive number is invalid", res); //format uint32_t id = FF_VOLUMES; @@ -480,7 +482,7 @@ esp_err_t esp_vfs_fat_sdcard_format(const char *base_path, sdmmc_card_t *card) s_ctx[id]->mount_config.allocation_unit_size); ESP_LOGI(TAG, "Formatting card, allocation unit size=%d", alloc_unit_size); const MKFS_PARM opt = {(BYTE)FM_ANY, 0, 0, 0, alloc_unit_size}; - FRESULT res = f_mkfs(drv, &opt, workbuf, workbuf_size); + res = f_mkfs(drv, &opt, workbuf, workbuf_size); free(workbuf); if (res != FR_OK) { ret = ESP_FAIL; diff --git a/components/fatfs/vfs/vfs_fat_spiflash.c b/components/fatfs/vfs/vfs_fat_spiflash.c index d227de821b..b4f1bfd8cd 100644 --- a/components/fatfs/vfs/vfs_fat_spiflash.c +++ b/components/fatfs/vfs/vfs_fat_spiflash.c @@ -203,7 +203,6 @@ esp_err_t esp_vfs_fat_spiflash_format_rw_wl(const char* base_path, const char* p wl_handle_t temp_handle = WL_INVALID_HANDLE; uint32_t id = FF_VOLUMES; - char drv[3] = {0, ':', 0}; bool found = s_get_context_id_by_label(partition_label, &id); if (!found) { @@ -219,8 +218,10 @@ esp_err_t esp_vfs_fat_spiflash_format_rw_wl(const char* base_path, const char* p } //unmount - drv[1] = (char)('0' + s_ctx[id]->pdrv); - f_mount(0, drv, 0); + char drv[3] = {(char)('0' + s_ctx[id]->pdrv), ':', 0}; + FRESULT fresult = f_mount(0, drv, 0); + ESP_RETURN_ON_FALSE(fresult != FR_INVALID_DRIVE, ESP_FAIL, TAG, "f_mount unmount failed (%d) - the logical drive number is invalid", fresult); + ESP_GOTO_ON_FALSE(fresult == FR_OK, ESP_FAIL, recycle, TAG, "f_mount unmount failed (%d), go to recycle", fresult); const size_t workbuf_size = 4096; void *workbuf = ff_memalloc(workbuf_size); @@ -231,7 +232,7 @@ esp_err_t esp_vfs_fat_spiflash_format_rw_wl(const char* base_path, const char* p size_t alloc_unit_size = esp_vfs_fat_get_allocation_unit_size(CONFIG_WL_SECTOR_SIZE, s_ctx[id]->mount_config.allocation_unit_size); ESP_LOGI(TAG, "Formatting FATFS partition, allocation unit size=%d", alloc_unit_size); const MKFS_PARM opt = {(BYTE)(FM_ANY | FM_SFD), 0, 0, 0, alloc_unit_size}; - FRESULT fresult = f_mkfs(drv, &opt, workbuf, workbuf_size); + fresult = f_mkfs(drv, &opt, workbuf, workbuf_size); free(workbuf); workbuf = NULL; ESP_GOTO_ON_FALSE(fresult == FR_OK, ESP_FAIL, mount_back, TAG, "f_mkfs failed (%d)", fresult);