Skip to content

Commit ebeb776

Browse files
authored
Merge pull request #3972 from geky/fat-big-blocks
FAT: Add support for block sizes of 512-4096 bytes
2 parents 027843a + c0aa841 commit ebeb776

File tree

4 files changed

+62
-6
lines changed

4 files changed

+62
-6
lines changed

features/filesystem/fat/ChaN/ff.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2249,6 +2249,13 @@ FRESULT find_volume ( /* FR_OK(0): successful, !=0: any error occurred */
22492249
#if _MAX_SS != _MIN_SS /* Get sector size (multiple sector size cfg only) */
22502250
if (disk_ioctl(fs->drv, GET_SECTOR_SIZE, &SS(fs)) != RES_OK
22512251
|| SS(fs) < _MIN_SS || SS(fs) > _MAX_SS) return FR_DISK_ERR;
2252+
#endif
2253+
#if _FS_HEAPBUF
2254+
if (!fs->win) {
2255+
fs->win = (BYTE*)ff_memalloc(SS(fs)); /* Allocate buffer to back window if necessary */
2256+
if (!fs->win)
2257+
return FR_NOT_ENOUGH_CORE;
2258+
}
22522259
#endif
22532260
/* Find an FAT partition on the drive. Supports only generic partitioning, FDISK and SFD. */
22542261
bsect = 0;
@@ -2423,12 +2430,18 @@ FRESULT f_mount (
24232430
if (!ff_del_syncobj(cfs->sobj)) return FR_INT_ERR;
24242431
#endif
24252432
cfs->fs_type = 0; /* Clear old fs object */
2433+
#if _FS_HEAPBUF
2434+
ff_memfree(cfs->win); /* Clean up window buffer */
2435+
#endif
24262436
}
24272437

24282438
if (fs) {
24292439
fs->fs_type = 0; /* Clear new fs object */
24302440
#if _FS_REENTRANT /* Create sync object for the new volume */
24312441
if (!ff_cre_syncobj((BYTE)vol, &fs->sobj)) return FR_INT_ERR;
2442+
#endif
2443+
#if _FS_HEAPBUF
2444+
fs->win = 0; /* NULL buffer to prevent use of uninitialized buffer */
24322445
#endif
24332446
}
24342447
FatFs[vol] = fs; /* Register new fs object */
@@ -2570,6 +2583,11 @@ FRESULT f_open (
25702583
#endif
25712584
fp->fs = dj.fs; /* Validate file object */
25722585
fp->id = fp->fs->id;
2586+
#if !_FS_TINY && _FS_HEAPBUF
2587+
fp->buf = (BYTE*)ff_memalloc(SS(dj.fs)); /* Allocate buffer if necessary */
2588+
if (!fp->buf)
2589+
return FR_NOT_ENOUGH_CORE;
2590+
#endif
25732591
}
25742592
}
25752593

@@ -2892,6 +2910,9 @@ FRESULT f_close (
28922910
fp->fs = 0; /* Invalidate file object */
28932911
#if _FS_REENTRANT
28942912
unlock_fs(fs, FR_OK); /* Unlock volume */
2913+
#endif
2914+
#if !_FS_TINY && _FS_HEAPBUF
2915+
ff_memfree(fp->buf); /* Deallocate buffer */
28952916
#endif
28962917
}
28972918
}
@@ -4112,6 +4133,13 @@ FRESULT f_mkfs (
41124133
#if _MAX_SS != _MIN_SS /* Get disk sector size */
41134134
if (disk_ioctl(pdrv, GET_SECTOR_SIZE, &SS(fs)) != RES_OK || SS(fs) > _MAX_SS || SS(fs) < _MIN_SS)
41144135
return FR_DISK_ERR;
4136+
#endif
4137+
#if _FS_HEAPBUF
4138+
if (!fs->win) {
4139+
fs->win = (BYTE*)ff_memalloc(SS(fs)); /* Allocate buffer to back window if necessary */
4140+
if (!fs->win)
4141+
return FR_NOT_ENOUGH_CORE;
4142+
}
41154143
#endif
41164144
if (_MULTI_PARTITION && part) {
41174145
/* Get partition information from partition table in the MBR */

features/filesystem/fat/ChaN/ff.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,11 @@ typedef struct {
104104
DWORD dirbase; /* Root directory start sector (FAT32:Cluster#) */
105105
DWORD database; /* Data start sector */
106106
DWORD winsect; /* Current sector appearing in the win[] */
107+
#if _FS_HEAPBUF
108+
BYTE *win; /* Disk access window for Directory, FAT (and file data at tiny cfg) */
109+
#else
107110
BYTE win[_MAX_SS]; /* Disk access window for Directory, FAT (and file data at tiny cfg) */
111+
#endif
108112
} FATFS;
109113

110114

@@ -132,8 +136,12 @@ typedef struct {
132136
UINT lockid; /* File lock ID origin from 1 (index of file semaphore table Files[]) */
133137
#endif
134138
#if !_FS_TINY
139+
#if _FS_HEAPBUF
140+
BYTE *buf; /* File private data read/write window */
141+
#else
135142
BYTE buf[_MAX_SS]; /* File private data read/write window */
136143
#endif
144+
#endif
137145
} FIL;
138146

139147

@@ -264,14 +272,16 @@ TCHAR* f_gets (TCHAR* buff, int len, FIL* fp); /* Get a string from the fil
264272
DWORD get_fattime (void);
265273
#endif
266274

275+
/* Memory functions */
276+
#if _USE_LFN == 3 || _FS_HEAPBUF
277+
void* ff_memalloc (UINT msize); /* Allocate memory block */
278+
void ff_memfree (void* mblock); /* Free memory block */
279+
#endif
280+
267281
/* Unicode support functions */
268282
#if _USE_LFN /* Unicode - OEM code conversion */
269283
WCHAR ff_convert (WCHAR chr, UINT dir); /* OEM-Unicode bidirectional conversion */
270284
WCHAR ff_wtoupper (WCHAR chr); /* Unicode upper-case conversion */
271-
#if _USE_LFN == 3 /* Memory functions */
272-
void* ff_memalloc (UINT msize); /* Allocate memory block */
273-
void ff_memfree (void* mblock); /* Free memory block */
274-
#endif
275285
#endif
276286

277287
/* Sync functions */

features/filesystem/fat/ChaN/ffconf.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@
162162

163163

164164
#define _MIN_SS 512
165-
#define _MAX_SS 512
165+
#define _MAX_SS 4096
166166
/* These options configure the range of sector size to be supported. (512, 1024,
167167
/ 2048 or 4096) Always set both 512 for most systems, all type of memory cards and
168168
/ harddisk. But a larger value may be required for on-board flash memory and some
@@ -194,14 +194,22 @@
194194
/ System Configurations
195195
/---------------------------------------------------------------------------*/
196196

197-
#define _FS_TINY 0
197+
#define _FS_TINY 1
198198
/* This option switches tiny buffer configuration. (0:Normal or 1:Tiny)
199199
/ At the tiny configuration, size of the file object (FIL) is reduced _MAX_SS
200200
/ bytes. Instead of private sector buffer eliminated from the file object,
201201
/ common sector buffer in the file system object (FATFS) is used for the file
202202
/ data transfer. */
203203

204204

205+
#define _FS_HEAPBUF 1
206+
/* This option enables the use of the heap for allocating buffers. Otherwise
207+
/ _MAX_SS sized buffers are allocated statically in relevant structures (in
208+
/ FATFS if _FS_TINY, otherwise in FATFS and FIL)
209+
/ This option allows the filesystem to dynamically allocate the buffers based
210+
/ on underlying sector size. */
211+
212+
205213
#define _FS_NORTC 0
206214
#define _NORTC_MON 1
207215
#define _NORTC_MDAY 1

features/filesystem/fat/FATFileSystem.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,16 @@ DWORD get_fattime(void)
146146
| (DWORD)(ptm->tm_sec/2 );
147147
}
148148

149+
void *ff_memalloc(UINT size)
150+
{
151+
return malloc(size);
152+
}
153+
154+
void ff_memfree(void *p)
155+
{
156+
free(p);
157+
}
158+
149159
// Implementation of diskio functions (see ChaN/diskio.h)
150160
DSTATUS disk_status(BYTE pdrv)
151161
{

0 commit comments

Comments
 (0)