Skip to content

FAT: Add support for block sizes of 512-4096 bytes #3972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions features/filesystem/fat/ChaN/ff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2249,6 +2249,13 @@ FRESULT find_volume ( /* FR_OK(0): successful, !=0: any error occurred */
#if _MAX_SS != _MIN_SS /* Get sector size (multiple sector size cfg only) */
if (disk_ioctl(fs->drv, GET_SECTOR_SIZE, &SS(fs)) != RES_OK
|| SS(fs) < _MIN_SS || SS(fs) > _MAX_SS) return FR_DISK_ERR;
#endif
#if _FS_HEAPBUF
if (!fs->win) {
fs->win = (BYTE*)ff_memalloc(SS(fs)); /* Allocate buffer to back window if necessary */
if (!fs->win)
return FR_NOT_ENOUGH_CORE;
}
#endif
/* Find an FAT partition on the drive. Supports only generic partitioning, FDISK and SFD. */
bsect = 0;
Expand Down Expand Up @@ -2423,12 +2430,18 @@ FRESULT f_mount (
if (!ff_del_syncobj(cfs->sobj)) return FR_INT_ERR;
#endif
cfs->fs_type = 0; /* Clear old fs object */
#if _FS_HEAPBUF
ff_memfree(cfs->win); /* Clean up window buffer */
#endif
}

if (fs) {
fs->fs_type = 0; /* Clear new fs object */
#if _FS_REENTRANT /* Create sync object for the new volume */
if (!ff_cre_syncobj((BYTE)vol, &fs->sobj)) return FR_INT_ERR;
#endif
#if _FS_HEAPBUF
fs->win = 0; /* NULL buffer to prevent use of uninitialized buffer */
#endif
}
FatFs[vol] = fs; /* Register new fs object */
Expand Down Expand Up @@ -2570,6 +2583,11 @@ FRESULT f_open (
#endif
fp->fs = dj.fs; /* Validate file object */
fp->id = fp->fs->id;
#if !_FS_TINY && _FS_HEAPBUF
fp->buf = (BYTE*)ff_memalloc(SS(dj.fs)); /* Allocate buffer if necessary */
if (!fp->buf)
return FR_NOT_ENOUGH_CORE;
#endif
}
}

Expand Down Expand Up @@ -2892,6 +2910,9 @@ FRESULT f_close (
fp->fs = 0; /* Invalidate file object */
#if _FS_REENTRANT
unlock_fs(fs, FR_OK); /* Unlock volume */
#endif
#if !_FS_TINY && _FS_HEAPBUF
ff_memfree(fp->buf); /* Deallocate buffer */
#endif
}
}
Expand Down Expand Up @@ -4112,6 +4133,13 @@ FRESULT f_mkfs (
#if _MAX_SS != _MIN_SS /* Get disk sector size */
if (disk_ioctl(pdrv, GET_SECTOR_SIZE, &SS(fs)) != RES_OK || SS(fs) > _MAX_SS || SS(fs) < _MIN_SS)
return FR_DISK_ERR;
#endif
#if _FS_HEAPBUF
if (!fs->win) {
fs->win = (BYTE*)ff_memalloc(SS(fs)); /* Allocate buffer to back window if necessary */
if (!fs->win)
return FR_NOT_ENOUGH_CORE;
}
#endif
if (_MULTI_PARTITION && part) {
/* Get partition information from partition table in the MBR */
Expand Down
18 changes: 14 additions & 4 deletions features/filesystem/fat/ChaN/ff.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ typedef struct {
DWORD dirbase; /* Root directory start sector (FAT32:Cluster#) */
DWORD database; /* Data start sector */
DWORD winsect; /* Current sector appearing in the win[] */
#if _FS_HEAPBUF
BYTE *win; /* Disk access window for Directory, FAT (and file data at tiny cfg) */
#else
BYTE win[_MAX_SS]; /* Disk access window for Directory, FAT (and file data at tiny cfg) */
#endif
} FATFS;


Expand Down Expand Up @@ -132,8 +136,12 @@ typedef struct {
UINT lockid; /* File lock ID origin from 1 (index of file semaphore table Files[]) */
#endif
#if !_FS_TINY
#if _FS_HEAPBUF
BYTE *buf; /* File private data read/write window */
#else
BYTE buf[_MAX_SS]; /* File private data read/write window */
#endif
#endif
} FIL;


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

/* Memory functions */
#if _USE_LFN == 3 || _FS_HEAPBUF
void* ff_memalloc (UINT msize); /* Allocate memory block */
void ff_memfree (void* mblock); /* Free memory block */
#endif

/* Unicode support functions */
#if _USE_LFN /* Unicode - OEM code conversion */
WCHAR ff_convert (WCHAR chr, UINT dir); /* OEM-Unicode bidirectional conversion */
WCHAR ff_wtoupper (WCHAR chr); /* Unicode upper-case conversion */
#if _USE_LFN == 3 /* Memory functions */
void* ff_memalloc (UINT msize); /* Allocate memory block */
void ff_memfree (void* mblock); /* Free memory block */
#endif
#endif

/* Sync functions */
Expand Down
12 changes: 10 additions & 2 deletions features/filesystem/fat/ChaN/ffconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@


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

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


#define _FS_HEAPBUF 1
/* This option enables the use of the heap for allocating buffers. Otherwise
/ _MAX_SS sized buffers are allocated statically in relevant structures (in
/ FATFS if _FS_TINY, otherwise in FATFS and FIL)
/ This option allows the filesystem to dynamically allocate the buffers based
/ on underlying sector size. */


#define _FS_NORTC 0
#define _NORTC_MON 1
#define _NORTC_MDAY 1
Expand Down
10 changes: 10 additions & 0 deletions features/filesystem/fat/FATFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ DWORD get_fattime(void)
| (DWORD)(ptm->tm_sec/2 );
}

void *ff_memalloc(UINT size)
{
return malloc(size);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be new or is the OOM condition caught by the caller?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah, you're right this should probably be new

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I changed this to propogate the error up, it's used for allocating large file buffers, so it's not unreasonable to be propogated as an error code (complies to posix and matches existing use of ff_memalloc in the chanfs layer).

}

void ff_memfree(void *p)
{
free(p);
}

// Implementation of diskio functions (see ChaN/diskio.h)
DSTATUS disk_status(BYTE pdrv)
{
Expand Down