fix(storage): FATFS WL function formatting wrong partition

Closes https://github.com/espressif/esp-idf/issues/12542

Co-authored-by: Tony Stuart <anthonyfstuart@gmail.com>
pull/13022/head
Adam Múdry 2023-11-16 21:21:33 +01:00
rodzic 3bcbbf13fe
commit ff8b0cf14b
4 zmienionych plików z 36 dodań i 8 usunięć

Wyświetl plik

@ -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();

Wyświetl plik

@ -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,

1 # Name Type SubType Offset Size Flags
2 factory app factory 0x10000 1M 768k
3 storage data fat 528k
4 storage2 data fat 528k

Wyświetl plik

@ -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 <stdlib.h>
#include <string.h>
#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;

Wyświetl plik

@ -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);