Arduino FatFS
Loading...
Searching...
No Matches
ff.h
1/*----------------------------------------------------------------------------/
2/ FatFs - Generic FAT Filesystem module R0.14 /
3/-----------------------------------------------------------------------------/
4/
5/ Copyright (C) 2019, ChaN, all right reserved.
6/
7/ FatFs module is an open source software. Redistribution and use of FatFs in
8/ source and binary forms, with or without modification, are permitted provided
9/ that the following condition is met:
10
11/ 1. Redistributions of source code must retain the above copyright notice,
12/ this condition and the following disclaimer.
13/
14/ This software is provided by the copyright holder and contributors "AS IS"
15/ and any warranties related to this software are DISCLAIMED.
16/ The copyright owner or contributors be NOT LIABLE for any damages caused
17/ by use of this software.
18/
19/----------------------------------------------------------------------------*/
20
21#pragma once
22#include <cstdlib>
23#include "ffconf.h" // FatFs configuration options
24#include "ffdef.h" // common structures and defines
25#include "driver/IO.h"
26
27namespace fatfs {
28
34class FatFs {
35 public:
36 FatFs() = default;
38 FatFs(IO& io) { setDriver(io); }
39 void setDriver(IO& io) { p_io = &io; }
40 IO* getDriver() {return p_io;}
44 FRESULT f_open(FIL* fp, const TCHAR* path,
45 BYTE mode);
46 FRESULT f_close(FIL* fp);
47 FRESULT f_read(FIL* fp, void* buff, UINT btr,
48 UINT* br);
49 FRESULT f_write(FIL* fp, const void* buff, UINT btw,
50 UINT* bw);
51 FRESULT f_lseek(FIL* fp,
52 FSIZE_t ofs);
53 FRESULT f_truncate(FIL* fp);
54 FRESULT f_sync(FIL* fp);
55 FRESULT f_opendir(DIR* dp, const TCHAR* path);
56 FRESULT f_closedir(DIR* dp);
57 FRESULT f_readdir(DIR* dp, FILINFO* fno);
58 FRESULT f_findfirst(DIR* dp, FILINFO* fno, const TCHAR* path,
59 const TCHAR* pattern);
60 FRESULT f_findnext(DIR* dp, FILINFO* fno);
61 FRESULT f_mkdir(const TCHAR* path);
62 FRESULT f_unlink(
63 const TCHAR* path);
64 FRESULT f_rename(const TCHAR* path_old,
65 const TCHAR* path_new);
66 FRESULT f_stat(const TCHAR* path, FILINFO* fno);
67 FRESULT f_chmod(const TCHAR* path, BYTE attr,
68 BYTE mask);
69 FRESULT f_utime(const TCHAR* path,
70 const FILINFO* fno);
71 FRESULT f_chdir(const TCHAR* path);
72 FRESULT f_chdrive(const TCHAR* path);
73 FRESULT f_getcwd(TCHAR* buff, UINT len);
74 FRESULT f_getfree(
75 const TCHAR* path, DWORD* nclst,
76 FATFS** fatfs);
77 FRESULT f_getlabel(const TCHAR* path, TCHAR* label,
78 DWORD* vsn);
79 FRESULT f_setlabel(const TCHAR* label);
80 FRESULT f_forward(FIL* fp, UINT (*func)(const BYTE*, UINT), UINT btf,
81 UINT* bf);
82 FRESULT f_expand(FIL* fp, FSIZE_t fsz,
83 BYTE opt);
84 FRESULT f_mount(FATFS* fs, const TCHAR* path,
85 BYTE opt);
86 FRESULT f_mkfs(const TCHAR* path, const MKFS_PARM* opt, void* work,
87 UINT len);
88 FRESULT f_fdisk(
89 BYTE pdrv, const LBA_t ptbl[],
90 void* work);
91 FRESULT f_setcp(WORD cp);
92 int f_putc(TCHAR c, FIL* fp);
93 int f_puts(const TCHAR* str, FIL* cp);
94 int f_printf(FIL* fp, const TCHAR* str,
95 ...);
96 TCHAR* f_gets(TCHAR* buff, int len, FIL* fp);
98 inline bool f_eof(FIL* fp) {
99 return ((int)((fp)->fptr == (fp)->obj.objsize));
100 }
101 inline BYTE f_error(FIL* fp) { return ((fp)->err); }
102 inline FSIZE_t f_tell(FIL* fp) { return ((fp)->fptr); }
103 inline FSIZE_t f_size(FIL* fp) { return ((fp)->obj.objsize); }
104 inline FRESULT f_rewind(FIL* fp) { return f_lseek((fp), 0); }
105 inline FRESULT f_rewinddir(DIR* dp) { return f_readdir((dp), 0); }
106 inline FRESULT f_rmdir(const TCHAR* path) { return f_unlink(path); }
107 inline FRESULT f_unmount(const TCHAR* path) { return f_mount(0, path, 0); }
108
109/*--------------------------------------------------------------*/
110/* Additional user defined functions */
111
112/* RTC function */
113#if !FF_FS_READONLY && !FF_FS_NORTC
114 DWORD get_fattime(void);
115#endif
116
118#if FF_FS_REENTRANT
119 int ff_cre_syncobj(BYTE vol, FF_SYNC_t* sobj);
120 int ff_req_grant(FF_SYNC_t sobj);
121 void ff_rel_grant(FF_SYNC_t sobj);
122 int ff_del_syncobj(FF_SYNC_t sobj);
123#endif
124
125 protected:
126 IO* p_io = nullptr;
127
128 /*--------------------------------------------------------------------------
129
130 Module Private Work Area
131
132 ---------------------------------------------------------------------------*/
133 /* Remark: Variables defined here without initial value shall be guaranteed
134 / zero/null at start-up. If not, the linker option or start-up routine is
135 / not compliance with C standard. */
136
137 /*--------------------------------*/
138 /* File/Volume controls */
139 /*--------------------------------*/
140
141#if FF_VOLUMES < 1 || FF_VOLUMES > 10
142#error Wrong FF_VOLUMES setting
143#endif
144 FATFS* FatFsDir[FF_VOLUMES];
146 WORD Fsid;
148#if FF_FS_RPATH != 0
149 BYTE CurrVol;
150#endif
151
152#if FF_FS_LOCK != 0
153 FILESEM Files[FF_FS_LOCK];
154#endif
155
156#if FF_STR_VOLUME_ID
157#ifdef FF_VOLUME_STRS
158 const char* const VolumeStr[FF_VOLUMES] = {
159 FF_VOLUME_STRS};
160#endif
161#endif
162
163#if FF_LBA64
164#if FF_MIN_GPT > 0x100000000
165#error Wrong FF_MIN_GPT setting
166#endif
167 const BYTE GUID_MS_Basic[16] = {0xA2, 0xA0, 0xD0, 0xEB, 0xE5, 0xB9,
168 0x33, 0x44, 0x87, 0xC0, 0x68, 0xB6,
169 0xB7, 0x26, 0x99, 0xC7};
170#endif
171
172 /*--------------------------------*/
173 /* LFN/Directory working buffer */
174 /*--------------------------------*/
175
176#if FF_USE_LFN == 0
177#if FF_FS_EXFAT
178#error LFN must be enabled when enable exFAT
179#endif
180#define DEF_NAMBUF
181#define INIT_NAMBUF(fs)
182#define FREE_NAMBUF()
183#define LEAVE_MKFS(res) return res
184
185#else
186#if FF_MAX_LFN < 12 || FF_MAX_LFN > 255
187#error Wrong setting of FF_MAX_LFN
188#endif
189#if FF_LFN_BUF < FF_SFN_BUF || FF_SFN_BUF < 12
190#error Wrong setting of FF_LFN_BUF or FF_SFN_BUF
191#endif
192#if FF_LFN_UNICODE < 0 || FF_LFN_UNICODE > 3
193#error Wrong setting of FF_LFN_UNICODE
194#endif
195 const BYTE LfnOfs[13] = {
196 1, 3, 5, 7, 9, 14, 16,
197 18, 20, 22, 24, 28, 30};
199#define MAXDIRB(nc) \
200 ((nc + 44U) / 15 * \
201 SZDIRE)
204#if FF_USE_LFN == 1
205#if FF_FS_EXFAT
206 BYTE
207 DirBuf[MAXDIRB(FF_MAX_LFN)];
208#endif
209 WCHAR LfnBuf[FF_MAX_LFN + 1];
210#define DEF_NAMBUF
211#define INIT_NAMBUF(fs)
212#define FREE_NAMBUF()
213#define LEAVE_MKFS(res) return res
214
215#elif FF_USE_LFN == 2
217#if FF_FS_EXFAT
218#define DEF_NAMBUF \
219 WCHAR lbuf[FF_MAX_LFN + 1]; \
220 BYTE dbuf[MAXDIRB(FF_MAX_LFN)];
222#define INIT_NAMBUF(fs) \
223 { \
224 (fs)->lfnbuf = lbuf; \
225 (fs)->dirbuf = dbuf; \
226 }
227#define FREE_NAMBUF()
228#else // !FF_FS_EXFAT
229#define DEF_NAMBUF WCHAR lbuf[FF_MAX_LFN + 1];
230#define INIT_NAMBUF(fs) \
231 { (fs)->lfnbuf = lbuf; }
232#define FREE_NAMBUF()
233#endif // FF_FS_EXFAT
234
235#define LEAVE_MKFS(res) return res
236
237#elif FF_USE_LFN == 3
239#if FF_FS_EXFAT
240#define DEF_NAMBUF \
241 WCHAR* lfn;
243#define INIT_NAMBUF(fs) \
244 { \
245 lfn = ff_memalloc((FF_MAX_LFN + 1) * 2 + MAXDIRB(FF_MAX_LFN)); \
246 if (!lfn) LEAVE_FF(fs, FR_NOT_ENOUGH_CORE); \
247 (fs)->lfnbuf = lfn; \
248 (fs)->dirbuf = (BYTE*)(lfn + FF_MAX_LFN + 1); \
249 }
250#define FREE_NAMBUF() ff_memfree(lfn)
251#else
252#define DEF_NAMBUF WCHAR* lfn;
253#define INIT_NAMBUF(fs) \
254 { \
255 lfn = ff_memalloc((FF_MAX_LFN + 1) * 2); \
256 if (!lfn) LEAVE_FF(fs, FR_NOT_ENOUGH_CORE); \
257 (fs)->lfnbuf = lfn; \
258 }
259#define FREE_NAMBUF() ff_memfree(lfn)
260#endif
261#define LEAVE_MKFS(res) \
262 { \
263 if (!work) ff_memfree(buf); \
264 return res; \
265 }
266#define MAX_MALLOC 0x8000
268#else
269#error Wrong setting of FF_USE_LFN
270#endif
271#endif
273 void flush(putbuff* pb);
274 void putc_bfd(putbuff* pb, TCHAR c);
275 int putc_flush(putbuff* pb);
276 FRESULT validate(FFOBJID* obj, FATFS** rfs);
277 FRESULT sync_window(FATFS* fs);
278 FRESULT move_window(FATFS* fs, LBA_t sect);
279 FRESULT sync_fs(FATFS* fs);
280 DWORD get_fat(FFOBJID* obj, DWORD clst);
281 FRESULT put_fat(FATFS* fs, DWORD clst, DWORD val);
282 DWORD find_bitmap(FATFS* fs, DWORD clst, DWORD ncl);
283 FRESULT remove_chain(FFOBJID* obj, DWORD clst, DWORD pclst);
284 DWORD create_chain(FFOBJID* obj, DWORD clst);
285 FRESULT dir_clear(FATFS* fs, DWORD clst);
286 FRESULT dir_sdi(DIR* dp, DWORD ofs);
287 FRESULT dir_next(DIR* dp, int stretch);
288 FRESULT dir_alloc(DIR* dp, UINT nent);
289 FRESULT dir_read(DIR* dp, int vol);
290 FRESULT dir_find(DIR* dp);
291 FRESULT dir_register(DIR* dp);
292 FRESULT dir_remove(DIR* dp);
293 FRESULT follow_path(DIR* dp, const TCHAR* path);
294 UINT check_fs(FATFS* fs, LBA_t sect);
295 UINT find_volume(FATFS* fs, UINT part);
296 FRESULT mount_volume(const TCHAR** path, FATFS** rfs, BYTE mode);
297 int cmp_lfn(const WCHAR* lfnbuf, BYTE* dir);
298 int pick_lfn(WCHAR* lfnbuf, BYTE* dir);
299 void put_lfn(const WCHAR* lfn, BYTE* dir, BYTE ord, BYTE sum);
300 FRESULT create_partition(BYTE drv, const LBA_t plst[], UINT sys, BYTE* buf);
301 FRESULT change_bitmap(FATFS* fs, DWORD clst, DWORD ncl, int bv);
302 FRESULT fill_first_frag(FFOBJID* obj);
303 FRESULT fill_last_frag(FFOBJID* obj, DWORD lcl, DWORD term);
304 FRESULT load_xdir(DIR* dp);
305 FRESULT load_obj_xdir(DIR* dp, const FFOBJID* obj);
306 FRESULT store_xdir(DIR*);
307 FRESULT chk_lock(DIR* dp, int acc);
308 int enq_lock(void);
309 UINT inc_lock(DIR* dp, int acc);
310 FRESULT dec_lock(UINT i);
311 void clear_lock(FATFS* fs);
312
313#if FF_USE_LFN == 3
315 /*------------------------------------------------------------------------*/
316 /* Allocate a memory block */
317 /*------------------------------------------------------------------------*/
318
319 void* ff_memalloc(
321 UINT msize
322 ) {
323 return malloc(msize);
324 }
325
326 /*------------------------------------------------------------------------*/
327 /* Free a memory block */
328 /*------------------------------------------------------------------------*/
329
330 void ff_memfree(void* mblock
332 ) {
333 free(mblock);
334 }
335
336#endif
337};
338
339/* LFN support functions */
340#if FF_USE_LFN >= 1
341WCHAR ff_oem2uni(WCHAR oem, WORD cp);
342WCHAR ff_uni2oem(DWORD uni, WORD cp);
343DWORD ff_wtoupper(DWORD uni);
344#endif
345
346} // namespace fatfs
347
348// Header-only library: include implementations
349#ifndef FATFS_HEADER_ONLY_IMPL
350#define FATFS_HEADER_ONLY_IMPL
351
352// Include FatFs core implementation
353#include "ff-inc.h"
354
355// Include system functions (memory allocation, locking)
356#include "ffsystem-inc.h"
357
358// Include Unicode conversion tables (if LFN support enabled)
359#if FF_USE_LFN >= 1
360#include "ffunicode-inc.h"
361#endif
362
363#endif // FATFS_HEADER_ONLY_IMPL
API for FatFS See http://elm-chan.org/fsw/ff/00index_e.html.
Definition ff.h:34
FRESULT f_readdir(DIR *dp, FILINFO *fno)
Definition ff-inc.h:4543
FRESULT f_close(FIL *fp)
Definition ff-inc.h:4071
FRESULT f_getcwd(TCHAR *buff, UINT len)
FRESULT f_expand(FIL *fp, FSIZE_t fsz, BYTE opt)
FRESULT f_utime(const TCHAR *path, const FILINFO *fno)
Definition ff-inc.h:5141
FRESULT f_mount(FATFS *fs, const TCHAR *path, BYTE opt)
Definition ff-inc.h:3527
BYTE DirBuf[MAXDIRB(FF_MAX_LFN)]
Definition ff.h:208
FRESULT f_findnext(DIR *dp, FILINFO *fno)
FRESULT f_stat(const TCHAR *path, FILINFO *fno)
Definition ff-inc.h:4631
TCHAR * f_gets(TCHAR *buff, int len, FIL *fp)
Definition ff-inc.h:6250
WORD Fsid
Definition ff.h:145
IO * p_io
Definition ff.h:125
int f_puts(const TCHAR *str, FIL *cp)
Definition ff-inc.h:6556
FRESULT f_forward(FIL *fp, UINT(*func)(const BYTE *, UINT), UINT btf, UINT *bf)
FRESULT f_opendir(DIR *dp, const TCHAR *path)
Definition ff-inc.h:4449
FRESULT f_chdrive(const TCHAR *path)
FRESULT f_getlabel(const TCHAR *path, TCHAR *label, DWORD *vsn)
int f_printf(FIL *fp, const TCHAR *str,...)
Definition ff-inc.h:6576
FRESULT f_chmod(const TCHAR *path, BYTE attr, BYTE mask)
Definition ff-inc.h:5094
FRESULT f_unlink(const TCHAR *path)
Definition ff-inc.h:4806
FRESULT f_fdisk(BYTE pdrv, const LBA_t ptbl[], void *work)
FatFs(IO &io)
Constructor which is providing the io driver.
Definition ff.h:38
FRESULT f_setlabel(const TCHAR *label)
FRESULT f_rename(const TCHAR *path_old, const TCHAR *path_new)
Definition ff-inc.h:4984
FRESULT f_getfree(const TCHAR *path, DWORD *nclst, FATFS **fatfs)
Definition ff-inc.h:4666
WCHAR LfnBuf[FF_MAX_LFN+1]
Definition ff.h:210
FRESULT f_sync(FIL *fp)
Definition ff-inc.h:3990
FRESULT f_mkfs(const TCHAR *path, const MKFS_PARM *opt, void *work, UINT len)
Definition ff-inc.h:5714
FATFS * FatFsDir[FF_VOLUMES]
Definition ff.h:143
FRESULT f_lseek(FIL *fp, FSIZE_t ofs)
Definition ff-inc.h:4286
FRESULT f_write(FIL *fp, const void *buff, UINT btw, UINT *bw)
Definition ff-inc.h:3868
FRESULT f_read(FIL *fp, void *buff, UINT btr, UINT *br)
Definition ff-inc.h:3767
FRESULT f_findfirst(DIR *dp, FILINFO *fno, const TCHAR *path, const TCHAR *pattern)
FRESULT f_chdir(const TCHAR *path)
FRESULT f_mkdir(const TCHAR *path)
Definition ff-inc.h:4900
FRESULT f_closedir(DIR *dp)
Definition ff-inc.h:4515
const BYTE LfnOfs[13]
Definition ff.h:194
int f_putc(TCHAR c, FIL *fp)
Definition ff-inc.h:6536
FRESULT f_setcp(WORD cp)
void clear_lock(FATFS *fs)
FRESULT f_open(FIL *fp, const TCHAR *path, BYTE mode)
Definition ff-inc.h:3575
IO * getDriver()
Definition ff.h:40
FRESULT f_truncate(FIL *fp)
Definition ff-inc.h:4756
FatFS interface definition.
Definition IO.h:74
Definition ffdef.h:200
Definition ffdef.h:98
Definition ffdef.h:149
Definition ffdef.h:218
Definition ffdef.h:175
Definition ffdef.h:233
Definition ffdef.h:270