Skip to content

Commit df8a162

Browse files
committed
feat(storage/fatfs): increase log legibility for fatfs mount
1 parent 648dc32 commit df8a162

File tree

1 file changed

+54
-34
lines changed

1 file changed

+54
-34
lines changed

components/fatfs/vfs/vfs_fat_spiflash.c

Lines changed: 54 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -81,46 +81,66 @@ vfs_fat_spiflash_ctx_t* get_vfs_fat_spiflash_ctx(wl_handle_t wlhandle)
8181
static esp_err_t s_f_mount_rw(FATFS *fs, const char *drv, const esp_vfs_fat_mount_config_t *mount_config, vfs_fat_x_ctx_flags_t *out_flags, size_t sec_num)
8282
{
8383
FRESULT fresult = f_mount(fs, drv, 1);
84-
if (fresult != FR_OK) {
85-
ESP_LOGW(TAG, "f_mount failed (%d)", fresult);
86-
87-
bool need_mount_again = (fresult == FR_NO_FILESYSTEM || fresult == FR_INT_ERR) && mount_config->format_if_mount_failed;
88-
if (!need_mount_again) {
89-
return ESP_FAIL;
84+
if (fresult == FR_OK) {
85+
if (out_flags) {
86+
*out_flags &= ~FORMATTED_DURING_LAST_MOUNT; // reset flag
9087
}
88+
return ESP_OK;
89+
}
9190

92-
const size_t workbuf_size = 4096;
93-
void *workbuf = ff_memalloc(workbuf_size);
94-
if (workbuf == NULL) {
95-
return ESP_ERR_NO_MEM;
96-
}
91+
const char *msg = "Unknown";
92+
const char *note = "";
93+
bool recoverable = false;
94+
95+
switch (fresult) {
96+
case FR_NO_FILESYSTEM:
97+
msg = "No filesystem detected";
98+
note = "(This may indicate corrupt FS, or attempt to mount read-only fatfsgen image for write)";
99+
recoverable = true;
100+
break;
101+
case FR_INT_ERR:
102+
msg = "Assertion failed";
103+
recoverable = true;
104+
break;
105+
default:
106+
break;
107+
}
97108

98-
size_t alloc_unit_size = esp_vfs_fat_get_allocation_unit_size(CONFIG_WL_SECTOR_SIZE, mount_config->allocation_unit_size);
99-
ESP_LOGI(TAG, "Formatting FATFS partition, allocation unit size=%d", alloc_unit_size);
100-
UINT root_dir_entries;
101-
if (CONFIG_WL_SECTOR_SIZE == 512) {
102-
root_dir_entries = 16;
103-
} else {
104-
root_dir_entries = 128;
105-
}
106-
const MKFS_PARM opt = {(BYTE)(FM_ANY | FM_SFD), (mount_config->use_one_fat ? 1 : 2), 0, (sec_num <= MIN_REQ_SEC ? root_dir_entries : 0), alloc_unit_size};
107-
fresult = f_mkfs(drv, &opt, workbuf, workbuf_size);
108-
free(workbuf);
109-
workbuf = NULL;
110-
ESP_RETURN_ON_FALSE(fresult == FR_OK, ESP_FAIL, TAG, "f_mkfs failed (%d)", fresult);
109+
if (!recoverable || !mount_config->format_if_mount_failed) {
110+
ESP_LOGE(TAG, "f_mount failed with error: \"%s\" [%d]. %s", msg, fresult, note);
111+
return ESP_FAIL;
112+
}
111113

112-
if (out_flags) {
113-
*out_flags |= FORMATTED_DURING_LAST_MOUNT; // set flag
114-
}
114+
ESP_LOGW(TAG, "FatFS mount (f_mount) failed with error: \"%s\" [%d]. Retrying after format...", msg, fresult);
115+
116+
const size_t workbuf_size = 4096;
117+
void *workbuf = ff_memalloc(workbuf_size);
118+
if (workbuf == NULL) {
119+
return ESP_ERR_NO_MEM;
120+
}
115121

116-
ESP_LOGI(TAG, "Mounting again");
117-
fresult = f_mount(fs, drv, 1);
118-
ESP_RETURN_ON_FALSE(fresult == FR_OK, ESP_FAIL, TAG, "f_mount failed after formatting (%d)", fresult);
122+
size_t alloc_unit_size = esp_vfs_fat_get_allocation_unit_size(CONFIG_WL_SECTOR_SIZE, mount_config->allocation_unit_size);
123+
ESP_LOGI(TAG, "Formatting FATFS partition, allocation unit size=%d", alloc_unit_size);
124+
UINT root_dir_entries;
125+
if (CONFIG_WL_SECTOR_SIZE == 512) {
126+
root_dir_entries = 16;
119127
} else {
120-
if (out_flags) {
121-
*out_flags &= ~FORMATTED_DURING_LAST_MOUNT; // reset flag
122-
}
128+
root_dir_entries = 128;
129+
}
130+
const MKFS_PARM opt = {(BYTE)(FM_ANY | FM_SFD), (mount_config->use_one_fat ? 1 : 2), 0, (sec_num <= MIN_REQ_SEC ? root_dir_entries : 0), alloc_unit_size};
131+
fresult = f_mkfs(drv, &opt, workbuf, workbuf_size);
132+
free(workbuf);
133+
workbuf = NULL;
134+
ESP_RETURN_ON_FALSE(fresult == FR_OK, ESP_FAIL, TAG, "f_mkfs failed (%d)", fresult);
135+
136+
if (out_flags) {
137+
*out_flags |= FORMATTED_DURING_LAST_MOUNT; // set flag
123138
}
139+
140+
ESP_LOGI(TAG, "Mounting again");
141+
fresult = f_mount(fs, drv, 1);
142+
ESP_RETURN_ON_FALSE(fresult == FR_OK, ESP_FAIL, TAG, "f_mount failed after formatting (%d)", fresult);
143+
124144
return ESP_OK;
125145
}
126146

0 commit comments

Comments
 (0)