Skip to content

Commit 3f347ed

Browse files
authored
Merge pull request #4843 from geky/fat-min-block
fatfs: Add lower bound to block sizes
2 parents b0dd984 + 626256e commit 3f347ed

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

features/filesystem/fat/FATFileSystem.cpp

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,26 @@ void ff_memfree(void *p)
160160
}
161161

162162
// Implementation of diskio functions (see ChaN/diskio.h)
163+
static WORD disk_get_sector_size(BYTE pdrv)
164+
{
165+
WORD ssize = _ffs[pdrv]->get_erase_size();
166+
if (ssize < 512) {
167+
ssize = 512;
168+
}
169+
170+
MBED_ASSERT(ssize >= _MIN_SS && ssize <= _MAX_SS);
171+
MBED_ASSERT(_ffs[pdrv]->get_read_size() <= _ffs[pdrv]->get_erase_size());
172+
MBED_ASSERT(_ffs[pdrv]->get_program_size() <= _ffs[pdrv]->get_erase_size());
173+
return ssize;
174+
}
175+
176+
static DWORD disk_get_sector_count(BYTE pdrv)
177+
{
178+
DWORD scount = _ffs[pdrv]->size() / disk_get_sector_size(pdrv);
179+
MBED_ASSERT(scount >= 64);
180+
return scount;
181+
}
182+
163183
DSTATUS disk_status(BYTE pdrv)
164184
{
165185
debug_if(FFS_DBG, "disk_status on pdrv [%d]\n", pdrv);
@@ -175,15 +195,15 @@ DSTATUS disk_initialize(BYTE pdrv)
175195
DRESULT disk_read(BYTE pdrv, BYTE *buff, DWORD sector, UINT count)
176196
{
177197
debug_if(FFS_DBG, "disk_read(sector %d, count %d) on pdrv [%d]\n", sector, count, pdrv);
178-
bd_size_t ssize = _ffs[pdrv]->get_erase_size();
198+
DWORD ssize = disk_get_sector_size(pdrv);
179199
int err = _ffs[pdrv]->read(buff, sector*ssize, count*ssize);
180200
return err ? RES_PARERR : RES_OK;
181201
}
182202

183203
DRESULT disk_write(BYTE pdrv, const BYTE *buff, DWORD sector, UINT count)
184204
{
185205
debug_if(FFS_DBG, "disk_write(sector %d, count %d) on pdrv [%d]\n", sector, count, pdrv);
186-
bd_size_t ssize = _ffs[pdrv]->get_erase_size();
206+
DWORD ssize = disk_get_sector_size(pdrv);
187207
int err = _ffs[pdrv]->erase(sector*ssize, count*ssize);
188208
if (err) {
189209
return RES_PARERR;
@@ -211,16 +231,14 @@ DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void *buff)
211231
if (_ffs[pdrv] == NULL) {
212232
return RES_NOTRDY;
213233
} else {
214-
DWORD count = _ffs[pdrv]->size() / _ffs[pdrv]->get_erase_size();
215-
*((DWORD*)buff) = count;
234+
*((DWORD*)buff) = disk_get_sector_count(pdrv);
216235
return RES_OK;
217236
}
218237
case GET_SECTOR_SIZE:
219238
if (_ffs[pdrv] == NULL) {
220239
return RES_NOTRDY;
221240
} else {
222-
WORD size = _ffs[pdrv]->get_erase_size();
223-
*((WORD*)buff) = size;
241+
*((WORD*)buff) = disk_get_sector_size(pdrv);
224242
return RES_OK;
225243
}
226244
case GET_BLOCK_SIZE:
@@ -295,7 +313,7 @@ int FATFileSystem::unmount()
295313

296314
/* See http://elm-chan.org/fsw/ff/en/mkfs.html for details of f_mkfs() and
297315
* associated arguments. */
298-
int FATFileSystem::format(BlockDevice *bd, int allocation_unit) {
316+
int FATFileSystem::format(BlockDevice *bd, bd_size_t cluster_size) {
299317
FATFileSystem fs;
300318
int err = fs.mount(bd, false);
301319
if (err) {
@@ -304,7 +322,7 @@ int FATFileSystem::format(BlockDevice *bd, int allocation_unit) {
304322

305323
// Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster)
306324
fs.lock();
307-
FRESULT res = f_mkfs(fs._fsid, 1, allocation_unit);
325+
FRESULT res = f_mkfs(fs._fsid, 1, cluster_size);
308326
fs.unlock();
309327
if (res != FR_OK) {
310328
return fat_error_remap(res);

features/filesystem/fat/FATFileSystem.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,18 @@ class FATFileSystem : public FileSystem {
4949
* The block device to format should be mounted when this function is called.
5050
*
5151
* @param bd
52-
* This is the block device that will be formated.
52+
* This is the block device that will be formatted.
5353
*
54-
* @param allocation_unit
55-
* This is the number of bytes per cluster size. The valid value is N
56-
* times the sector size. N is a power of 2 from 1 to 128 for FAT
57-
* volume and upto 16MiB for exFAT volume. If zero is given,
58-
* the default allocation unit size is selected by the underlying
59-
* filesystem, which depends on the volume size.
54+
* @param cluster_size
55+
* This is the number of bytes per cluster. A larger cluster size will decrease
56+
* the overhead of the FAT table, but also increase the minimum file size. The
57+
* cluster_size must be a multiple of the underlying device's allocation unit
58+
* and is currently limited to a max of 32,768 bytes. If zero, a cluster size
59+
* will be determined from the device's allocation unit. Defaults to zero.
6060
*
6161
* @return 0 on success, negative error code on failure
6262
*/
63-
static int format(BlockDevice *bd, int allocation_unit = 0);
63+
static int format(BlockDevice *bd, bd_size_t cluster_size = 0);
6464

6565
/** Mounts a filesystem to a block device
6666
*

0 commit comments

Comments
 (0)