From 1b24b3663e024e6e8f55c833d9bca73b332a9722 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 26 Aug 2019 17:00:24 +0200 Subject: [PATCH] fatfs: handle FR_INT_ERR as "filesystem corrupted" FatFS library can sometimes return FR_INT_ERR if the filesystem is corrupted. Propagate the error from VFS functions instead of asserting, so that the application can handle the error. Also handle the error during initialization of FatFS and format the filesystem if it occurs. --- components/fatfs/vfs/vfs_fat.c | 4 +--- components/fatfs/vfs/vfs_fat_sdmmc.c | 3 ++- components/fatfs/vfs/vfs_fat_spiflash.c | 3 ++- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/components/fatfs/vfs/vfs_fat.c b/components/fatfs/vfs/vfs_fat.c index e4be41516..08bb49eec 100644 --- a/components/fatfs/vfs/vfs_fat.c +++ b/components/fatfs/vfs/vfs_fat.c @@ -255,9 +255,7 @@ static int fresult_to_errno(FRESULT fr) { switch(fr) { case FR_DISK_ERR: return EIO; - case FR_INT_ERR: - assert(0 && "fatfs internal error"); - return EIO; + case FR_INT_ERR: return EIO; case FR_NOT_READY: return ENODEV; case FR_NO_FILE: return ENOENT; case FR_NO_PATH: return ENOENT; diff --git a/components/fatfs/vfs/vfs_fat_sdmmc.c b/components/fatfs/vfs/vfs_fat_sdmmc.c index 83cc0aee5..80d2d9f83 100644 --- a/components/fatfs/vfs/vfs_fat_sdmmc.c +++ b/components/fatfs/vfs/vfs_fat_sdmmc.c @@ -111,7 +111,8 @@ esp_err_t esp_vfs_fat_sdmmc_mount(const char* base_path, if (res != FR_OK) { err = ESP_FAIL; ESP_LOGW(TAG, "failed to mount card (%d)", res); - if (!(res == FR_NO_FILESYSTEM && mount_config->format_if_mount_failed)) { + if (!((res == FR_NO_FILESYSTEM || res == FR_INT_ERR) + && mount_config->format_if_mount_failed)) { goto fail; } ESP_LOGW(TAG, "partitioning card"); diff --git a/components/fatfs/vfs/vfs_fat_spiflash.c b/components/fatfs/vfs/vfs_fat_spiflash.c index afd46453b..3110d2e2d 100644 --- a/components/fatfs/vfs/vfs_fat_spiflash.c +++ b/components/fatfs/vfs/vfs_fat_spiflash.c @@ -76,7 +76,8 @@ esp_err_t esp_vfs_fat_spiflash_mount(const char* base_path, FRESULT fresult = f_mount(fs, drv, 1); if (fresult != FR_OK) { ESP_LOGW(TAG, "f_mount failed (%d)", fresult); - if (!(fresult == FR_NO_FILESYSTEM && mount_config->format_if_mount_failed)) { + if (!((fresult == FR_NO_FILESYSTEM || fresult == FR_INT_ERR) + && mount_config->format_if_mount_failed)) { result = ESP_FAIL; goto fail; }