Skip to content

Commit a6e7046

Browse files
committed
Merge branch 'feat/fatfs_r_0_16' into 'master'
feat(storage/fatfs): Update to FatFS R0.16 Closes IDF-14371 See merge request espressif/esp-idf!42810
2 parents c7b4804 + 36045c6 commit a6e7046

File tree

8 files changed

+1346
-1167
lines changed

8 files changed

+1346
-1167
lines changed
Lines changed: 42 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -16,47 +16,39 @@
1616

1717
TEST_CASE("Create volume, open file, write and read back data", "[fatfs]")
1818
{
19-
FRESULT fr_result;
2019
BYTE pdrv;
2120
FATFS fs;
2221
FIL file;
2322
UINT bw;
2423

25-
esp_err_t esp_result;
26-
2724
const esp_partition_t *partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, "storage");
2825

2926
// Mount wear-levelled partition
3027
wl_handle_t wl_handle;
31-
esp_result = wl_mount(partition, &wl_handle);
32-
REQUIRE(esp_result == ESP_OK);
28+
REQUIRE(wl_mount(partition, &wl_handle) == ESP_OK);
3329

3430
// Get a physical drive
35-
esp_result = ff_diskio_get_drive(&pdrv);
36-
REQUIRE(esp_result == ESP_OK);
31+
REQUIRE(ff_diskio_get_drive(&pdrv) == ESP_OK);
3732

3833
// Register physical drive as wear-levelled partition
39-
esp_result = ff_diskio_register_wl_partition(pdrv, wl_handle);
34+
REQUIRE(ff_diskio_register_wl_partition(pdrv, wl_handle) == ESP_OK);
4035

4136
// Create FAT volume on the entire disk
4237
LBA_t part_list[] = {100, 0, 0, 0};
4338
BYTE work_area[FF_MAX_SS];
4439

45-
fr_result = f_fdisk(pdrv, part_list, work_area);
46-
REQUIRE(fr_result == FR_OK);
40+
REQUIRE(f_fdisk(pdrv, part_list, work_area) == FR_OK);
4741

4842
// For host tests, include FM_SFD flag when formatting partitions smaller than 128KB.
4943
// if n_root field of MKFS_PARM is set to 128 => 1 root directory sec and if set to 0(default 512) => 4 root directory sectors.
5044
const MKFS_PARM opt = {(BYTE)(FM_ANY | FM_SFD), 0, 0, 128, 0};
51-
fr_result = f_mkfs("", &opt, work_area, sizeof(work_area)); // Use default volume
45+
REQUIRE(f_mkfs("", &opt, work_area, sizeof(work_area)) == FR_OK); // Use default volume
5246

5347
// Mount the volume
54-
fr_result = f_mount(&fs, "", 0);
55-
REQUIRE(fr_result == FR_OK);
48+
REQUIRE(f_mount(&fs, "", 0) == FR_OK);
5649

5750
// Open, write and read data
58-
fr_result = f_open(&file, "test.txt", FA_OPEN_ALWAYS | FA_READ | FA_WRITE);
59-
REQUIRE(fr_result == FR_OK);
51+
REQUIRE(f_open(&file, "test.txt", FA_OPEN_ALWAYS | FA_READ | FA_WRITE) == FR_OK);
6052

6153
// Generate data
6254
uint32_t data_size = 1000;
@@ -70,72 +62,59 @@ TEST_CASE("Create volume, open file, write and read back data", "[fatfs]")
7062
}
7163

7264
// Write generated data
73-
fr_result = f_write(&file, data, data_size, &bw);
74-
REQUIRE(fr_result == FR_OK);
65+
REQUIRE(f_write(&file, data, data_size, &bw) == FR_OK);
7566
REQUIRE(bw == data_size);
7667

7768
// Move to beginning of file
78-
fr_result = f_lseek(&file, 0);
79-
REQUIRE(fr_result == FR_OK);
69+
REQUIRE(f_lseek(&file, 0) == FR_OK);
8070

8171
// Read written data
82-
fr_result = f_read(&file, read, data_size, &bw);
83-
REQUIRE(fr_result == FR_OK);
72+
REQUIRE(f_read(&file, read, data_size, &bw) == FR_OK);
8473
REQUIRE(bw == data_size);
8574

8675
REQUIRE(memcmp(data, read, data_size) == 0);
8776

8877
// Close file
89-
fr_result = f_close(&file);
90-
REQUIRE(fr_result == FR_OK);
78+
REQUIRE(f_close(&file) == FR_OK);
9179

9280
// Unmount default volume
93-
fr_result = f_mount(0, "", 0);
94-
REQUIRE(fr_result == FR_OK);
81+
REQUIRE(f_mount(0, "", 0) == FR_OK);
9582

9683
// Clear
9784
free(read);
9885
free(data);
9986
ff_diskio_unregister(pdrv);
10087
ff_diskio_clear_pdrv_wl(wl_handle);
101-
esp_result = wl_unmount(wl_handle);
102-
REQUIRE(esp_result == ESP_OK);
88+
REQUIRE(wl_unmount(wl_handle) == ESP_OK);
10389
}
10490

10591
static void prepare_fatfs(const char* partition_label, const esp_partition_t** partition, wl_handle_t* wl_handle, BYTE* pdrv)
10692
{
107-
FRESULT fr_result;
108-
esp_err_t esp_result;
109-
11093
*partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, partition_label);
11194
REQUIRE(partition != NULL);
11295
printf("partition address=0x%x\n", (*partition)->address);
11396
printf("partition size=0x%x\n", (*partition)->size);
11497

11598
// Mount wear-levelled partition
116-
esp_result = wl_mount(*partition, wl_handle);
117-
REQUIRE(esp_result == ESP_OK);
99+
REQUIRE(wl_mount(*partition, wl_handle) == ESP_OK);
118100

119101
// Get a physical drive
120102
BYTE _pdrv;
121-
esp_result = ff_diskio_get_drive(&_pdrv);
122-
REQUIRE(esp_result == ESP_OK);
103+
REQUIRE(ff_diskio_get_drive(&_pdrv) == ESP_OK);
123104
printf("using pdrv=%i\n", _pdrv);
124105
char drv[3] = {(char)('0' + _pdrv), ':', 0};
125106
*pdrv = _pdrv;
126107

127108
// Register physical drive as wear-levelled partition
128-
esp_result = ff_diskio_register_wl_partition(_pdrv, *wl_handle);
109+
REQUIRE(ff_diskio_register_wl_partition(_pdrv, *wl_handle) == ESP_OK);
129110

130111
// Create FAT volume on the entire disk
131112
LBA_t part_list[] = {100, 0, 0, 0};
132113
BYTE work_area[FF_MAX_SS];
133114

134-
fr_result = f_fdisk(_pdrv, part_list, work_area);
135-
REQUIRE(fr_result == FR_OK);
115+
REQUIRE(f_fdisk(_pdrv, part_list, work_area) == FR_OK);
136116
const MKFS_PARM opt = {(BYTE)(FM_ANY | FM_SFD), 0, 0, 128, 0};
137-
fr_result = f_mkfs(drv, &opt, work_area, sizeof(work_area)); // Use default volume
138-
REQUIRE(fr_result == FR_OK);
117+
REQUIRE(f_mkfs(drv, &opt, work_area, sizeof(work_area)) == FR_OK); // Use default volume
139118
}
140119

141120
/*
@@ -146,9 +125,6 @@ static void prepare_fatfs(const char* partition_label, const esp_partition_t** p
146125
*/
147126
TEST_CASE("Test mounting 2 volumes, writing data and formatting the 2nd one, reading data", "[fatfs]")
148127
{
149-
FRESULT fr_result;
150-
esp_err_t esp_result;
151-
152128
const char* partition_label0 = "storage";
153129
const esp_partition_t *partition0 = NULL;
154130
BYTE pdrv0 = UINT8_MAX;
@@ -170,27 +146,22 @@ TEST_CASE("Test mounting 2 volumes, writing data and formatting the 2nd one, rea
170146
REQUIRE(wl_handle0 != WL_INVALID_HANDLE);
171147
REQUIRE(pdrv0 == 0);
172148
char drv0[3] = {(char)('0' + pdrv0), ':', 0};
173-
fr_result = f_mount(&fs0, drv0, 0);
174-
REQUIRE(fr_result == FR_OK);
149+
REQUIRE(f_mount(&fs0, drv0, 0) == FR_OK);
175150

176151
// Open file and write data
177152
FIL file0;
178153
UINT bw0;
179-
fr_result = f_open(&file0, "0:/test0.txt", FA_OPEN_ALWAYS | FA_WRITE);
180-
REQUIRE(fr_result == FR_OK);
154+
REQUIRE(f_open(&file0, "0:/test0.txt", FA_OPEN_ALWAYS | FA_WRITE) == FR_OK);
181155
// Write data
182156
const char *data0 = "123456789";
183157
char read0[10] = {0};
184-
fr_result = f_write(&file0, data0, data_size, &bw0);
185-
REQUIRE(fr_result == FR_OK);
158+
REQUIRE(f_write(&file0, data0, data_size, &bw0) == FR_OK);
186159
REQUIRE(bw0 == data_size);
187160
// Close file
188-
fr_result = f_close(&file0);
189-
REQUIRE(fr_result == FR_OK);
161+
REQUIRE(f_close(&file0) == FR_OK);
190162

191163
// Unmount volume 0
192-
fr_result = f_mount(0, drv0, 0);
193-
REQUIRE(fr_result == FR_OK);
164+
REQUIRE(f_mount(0, drv0, 0) == FR_OK);
194165

195166

196167
// Mount the volume 1
@@ -199,89 +170,72 @@ TEST_CASE("Test mounting 2 volumes, writing data and formatting the 2nd one, rea
199170
REQUIRE(wl_handle1 != WL_INVALID_HANDLE);
200171
REQUIRE(pdrv1 == 1);
201172
char drv1[3] = {(char)('0' + pdrv1), ':', 0};
202-
fr_result = f_mount(&fs1, drv1, 0);
203-
REQUIRE(fr_result == FR_OK);
173+
REQUIRE(f_mount(&fs1, drv1, 0) == FR_OK);
204174

205175
// Open file and write data
206176
FIL file1;
207177
UINT bw1;
208-
fr_result = f_open(&file1, "1:/test1.txt", FA_OPEN_ALWAYS | FA_WRITE);
209-
REQUIRE(fr_result == FR_OK);
178+
REQUIRE(f_open(&file1, "1:/test1.txt", FA_OPEN_ALWAYS | FA_WRITE) == FR_OK);
210179
// Write data
211180
const char* data1 = "987654321";
212181
char read1[10] = {0};
213-
fr_result = f_write(&file1, data1, data_size, &bw1);
214-
REQUIRE(fr_result == FR_OK);
182+
REQUIRE(f_write(&file1, data1, data_size, &bw1) == FR_OK);
215183
REQUIRE(bw1 == data_size);
216184
// Close file
217-
fr_result = f_close(&file1);
218-
REQUIRE(fr_result == FR_OK);
185+
REQUIRE(f_close(&file1) == FR_OK);
219186

220187
// Unmount volume 1
221-
fr_result = f_mount(0, drv1, 0);
222-
REQUIRE(fr_result == FR_OK);
188+
REQUIRE(f_mount(0, drv1, 0) == FR_OK);
223189

224190
// Format the volume 1
225191
const size_t workbuf_size = 4096;
226192
void *workbuf = ff_memalloc(workbuf_size);
227193
REQUIRE(workbuf != NULL);
228194
const MKFS_PARM opt = {(BYTE)(FM_ANY | FM_SFD), 0, 0, 128, CONFIG_WL_SECTOR_SIZE};
229-
fr_result = f_mkfs(drv1, &opt, workbuf, workbuf_size);
195+
FRESULT fr_result = f_mkfs(drv1, &opt, workbuf, workbuf_size);
230196
free(workbuf);
231197
workbuf = NULL;
232198
REQUIRE(fr_result == FR_OK);
233199
printf("partition1 formatted\n");
234200

235201

236202
// Remount the volume 1
237-
fr_result = f_mount(&fs1, drv1, 1);
238-
REQUIRE(fr_result == FR_OK);
203+
REQUIRE(f_mount(&fs1, drv1, 1) == FR_OK);
239204
// Open file and read data from file1
240-
fr_result = f_open(&file1, "1:/test1.txt", FA_OPEN_ALWAYS | FA_READ);
241-
REQUIRE(fr_result == FR_OK);
205+
REQUIRE(f_open(&file1, "1:/test1.txt", FA_OPEN_ALWAYS | FA_READ) == FR_OK);
242206
// Read written data from file1
243-
fr_result = f_read(&file1, read1, data_size, &bw1);
244-
REQUIRE(fr_result == FR_OK);
207+
REQUIRE(f_read(&file1, read1, data_size, &bw1) == FR_OK);
245208
REQUIRE(bw1 != data_size);
246209
// Compare data
247210
printf("data1=%s, read1=%s\n", data1, read1);
248211
REQUIRE(strncmp(data1, read1, data_size-1) != 0); // 987654321 should be ersead due to formatting
249212
// Close file from file1
250-
fr_result = f_close(&file1);
251-
REQUIRE(fr_result == FR_OK);
213+
REQUIRE(f_close(&file1) == FR_OK);
252214

253215

254216
// Remount the volume 0
255-
fr_result = f_mount(&fs0, drv0, 1);
256-
REQUIRE(fr_result == FR_OK);
217+
REQUIRE(f_mount(&fs0, drv0, 1) == FR_OK);
257218
// Open file and read data from file0
258-
fr_result = f_open(&file0, "0:/test0.txt", FA_OPEN_ALWAYS | FA_READ);
259-
REQUIRE(fr_result == FR_OK);
219+
REQUIRE(f_open(&file0, "0:/test0.txt", FA_OPEN_ALWAYS | FA_READ) == FR_OK);
260220
// Read written data from file0
261-
fr_result = f_read(&file0, read0, data_size, &bw0);
262-
REQUIRE(fr_result == FR_OK);
221+
REQUIRE(f_read(&file0, read0, data_size, &bw0) == FR_OK);
263222
REQUIRE(bw0 == data_size);
264223
// Compare data
265224
printf("data0=%s, read0=%s\n", data0, read0);
266225
REQUIRE(strncmp(data0, read0, data_size-1) == 0); // should match since the partition was not formatted
267226
// Close file from file0
268-
fr_result = f_close(&file0);
269-
REQUIRE(fr_result == FR_OK);
227+
REQUIRE(f_close(&file0) == FR_OK);
270228

271229

272230
// Unmount both volumes
273-
fr_result = f_mount(0, drv0, 0);
274-
REQUIRE(fr_result == FR_OK);
275-
fr_result = f_mount(0, drv1, 0);
276-
REQUIRE(fr_result == FR_OK);
231+
REQUIRE(f_mount(0, drv0, 0) == FR_OK);
232+
REQUIRE(f_mount(0, drv1, 0) == FR_OK);
277233

278234
// Clear
279235
ff_diskio_unregister(pdrv0);
280236
ff_diskio_unregister(pdrv1);
281237
ff_diskio_clear_pdrv_wl(wl_handle0);
282238
ff_diskio_clear_pdrv_wl(wl_handle1);
283-
esp_result = wl_unmount(wl_handle0);
284-
REQUIRE(esp_result == ESP_OK);
285-
esp_result = wl_unmount(wl_handle1);
286-
REQUIRE(esp_result == ESP_OK);
239+
REQUIRE(wl_unmount(wl_handle0) == ESP_OK);
240+
REQUIRE(wl_unmount(wl_handle1) == ESP_OK);
287241
}

components/fatfs/src/00history.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,3 +366,24 @@ R0.15 (November 6, 2022)
366366
Fixed f_mkfs() creates broken exFAT volume when the size of volume is >= 2^32 sectors.
367367
Fixed string functions cannot write the unicode characters not in BMP when FF_LFN_UNICODE == 2 (UTF-8).
368368
Fixed a compatibility issue in identification of GPT header.
369+
370+
371+
372+
R0.15a (November 22, 2024)
373+
Fixed a complie error when FF_FS_LOCK != 0.
374+
Fixed a potential issue when work FatFs concurrency with FF_FS_REENTRANT, FF_VOLUMES >= 2 and FF_FS_LOCK > 0.
375+
Made f_setlabel() accept a volume label in Unix style volume ID when FF_STR_VOLUME_ID == 2.
376+
Made FatFs update PercInUse field in exFAT VBR. (A preceding f_getfree() is needed for the accuracy)
377+
378+
379+
380+
R0.15b (June 21, 2025)
381+
Added support for timestamp of created time. (FF_FS_CRTIME)
382+
Fixed FatFs fails to load the FsInfo in FAT32 volumes and the f_getfree always be forced a full FAT scan which takes a long time. (appeared at R0.15a)
383+
384+
385+
386+
R0.16 (July 22, 2025)
387+
Removed a long-pending limitation that f_getcwd and double-dot .. in the path name did not work on the exFAT volume.
388+
Fixed f_readdir cannot detect end of directory and it leads the application process into infinite loop. (appeared at R0.15b)
389+
Fixed dot names with terminating separator or duplicated separator are rejected when LFN is not enabled.

components/fatfs/src/00readme.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FatFs Module Source Files R0.15
1+
FatFs Module Source Files R0.16
22

33

44
FILES

components/fatfs/src/diskio.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
/*-----------------------------------------------------------------------*/
2-
/* Low level disk I/O module SKELETON for FatFs (C)ChaN, 2019 */
2+
/* Low level disk I/O module SKELETON for FatFs (C)ChaN, 2025 */
33
/*-----------------------------------------------------------------------*/
44
/* If a working storage control module is available, it should be */
55
/* attached to the FatFs via a glue function rather than modifying it. */
66
/* This is an example of glue functions to attach various exsisting */
77
/* storage control modules to the FatFs module with a defined API. */
88
/*-----------------------------------------------------------------------*/
99

10-
#include "ff.h" /* Obtains integer types */
11-
#include "diskio.h" /* Declarations of disk functions */
10+
#include "ff.h" /* Basic definitions of FatFs */
11+
#include "diskio.h" /* Declarations FatFs MAI */
1212

13-
/* Definitions of physical drive number for each drive */
14-
#define DEV_RAM 0 /* Example: Map Ramdisk to physical drive 0 */
15-
#define DEV_MMC 1 /* Example: Map MMC/SD card to physical drive 1 */
16-
#define DEV_USB 2 /* Example: Map USB MSD to physical drive 2 */
13+
/* Example: Declarations of the platform and disk functions in the project */
14+
#include "platform.h"
15+
#include "storage.h"
16+
17+
/* Example: Mapping of physical drive number for each drive */
18+
#define DEV_FLASH 0 /* Map FTL to physical drive 0 */
19+
#define DEV_MMC 1 /* Map MMC/SD card to physical drive 1 */
20+
#define DEV_USB 2 /* Map USB MSD to physical drive 2 */
1721

1822

1923
/*-----------------------------------------------------------------------*/

components/fatfs/src/diskio.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*-----------------------------------------------------------------------/
2-
/ Low level disk interface modlue include file (C)ChaN, 2019 /
2+
/ Low level disk interface modlue include file (C)ChaN, 2025 /
33
/-----------------------------------------------------------------------*/
44

55
#ifndef _DISKIO_DEFINED
@@ -57,7 +57,7 @@ DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff);
5757
#define CTRL_EJECT 7 /* Eject media */
5858
#define CTRL_FORMAT 8 /* Create physical format on the media */
5959

60-
/* MMC/SDC specific ioctl command */
60+
/* MMC/SDC specific ioctl command (Not used by FatFs) */
6161
#define MMC_GET_TYPE 10 /* Get card type */
6262
#define MMC_GET_CSD 11 /* Get CSD */
6363
#define MMC_GET_CID 12 /* Get CID */
@@ -67,7 +67,7 @@ DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff);
6767
#define ISDIO_WRITE 56 /* Write data to SD iSDIO register */
6868
#define ISDIO_MRITE 57 /* Masked write data to SD iSDIO register */
6969

70-
/* ATA/CF specific ioctl command */
70+
/* ATA/CF specific ioctl command (Not used by FatFs) */
7171
#define ATA_GET_REV 20 /* Get F/W revision */
7272
#define ATA_GET_MODEL 21 /* Get model name */
7373
#define ATA_GET_SN 22 /* Get serial number */

0 commit comments

Comments
 (0)