/*----------------------------------------------------------------------------/1/ FatFs - Generic FAT Filesystem module R0.13c /2/-----------------------------------------------------------------------------/3/4/ Copyright (C) 2018, ChaN, all right reserved.5/6/ FatFs module is an open source software. Redistribution and use of FatFs in7/ source and binary forms, with or without modification, are permitted provided8/ that the following condition is met:910/ 1. Redistributions of source code must retain the above copyright notice,11/ this condition and the following disclaimer.12/13/ This software is provided by the copyright holder and contributors "AS IS"14/ and any warranties related to this software are DISCLAIMED.15/ The copyright owner or contributors be NOT LIABLE for any damages caused16/ by use of this software.17/18/----------------------------------------------------------------------------*/192021#ifndef FF_DEFINED22#define FF_DEFINED 86604 /* Revision ID */2324#ifdef __cplusplus25extern "C" {26#endif2728#include <utils/types.h> /* Basic integer types */29#include <fatfs_cfg.h> /* FatFs configuration options */3031#if FF_DEFINED != FFCONF_DEF32#error Wrong configuration file (ffconf.h).33#endif34353637/* Definitions of volume management */3839#if FF_MULTI_PARTITION /* Multiple partition configuration */40typedef struct {41BYTE pd; /* Physical drive number */42BYTE pt; /* Partition: 0:Auto detect, 1-4:Forced partition) */43} PARTITION;44extern PARTITION VolToPart[]; /* Volume - Partition resolution table */45#endif4647#if FF_STR_VOLUME_ID48#ifndef FF_VOLUME_STRS49extern const char* VolumeStr[FF_VOLUMES]; /* User defied volume ID */50#endif51#endif52535455/* Type of path name strings on FatFs API */5657#ifndef _INC_TCHAR58#define _INC_TCHAR5960#if FF_USE_LFN && FF_LFN_UNICODE == 1 /* Unicode in UTF-16 encoding */61typedef WCHAR TCHAR;62#define _T(x) L ## x63#define _TEXT(x) L ## x64#elif FF_USE_LFN && FF_LFN_UNICODE == 2 /* Unicode in UTF-8 encoding */65typedef char TCHAR;66#define _T(x) u8 ## x67#define _TEXT(x) u8 ## x68#elif FF_USE_LFN && FF_LFN_UNICODE == 3 /* Unicode in UTF-32 encoding */69typedef DWORD TCHAR;70#define _T(x) U ## x71#define _TEXT(x) U ## x72#elif FF_USE_LFN && (FF_LFN_UNICODE < 0 || FF_LFN_UNICODE > 3)73#error Wrong FF_LFN_UNICODE setting74#else /* ANSI/OEM code in SBCS/DBCS */75typedef char TCHAR;76#define _T(x) x77#define _TEXT(x) x78#endif7980#endif81828384/* Type of file size variables */8586#if FF_FS_EXFAT87typedef QWORD FSIZE_t;88#else89typedef DWORD FSIZE_t;90#endif91929394/* Filesystem object structure (FATFS) */9596typedef struct {97BYTE fs_type; /* Filesystem type (0:not mounted) */98BYTE part_type; /* Partition type (0:MBR, 1:GPT) */99BYTE pdrv; /* Associated physical drive */100BYTE n_fats; /* Number of FATs (1 or 2) */101BYTE wflag; /* win[] flag (b0:dirty) */102BYTE fsi_flag; /* FSINFO flags (b7:disabled, b0:dirty) */103WORD id; /* Volume mount ID */104WORD n_rootdir; /* Number of root directory entries (FAT12/16) */105WORD csize; /* Cluster size [sectors] */106#if FF_MAX_SS != FF_MIN_SS107WORD ssize; /* Sector size (512, 1024, 2048 or 4096) */108#endif109#if FF_USE_LFN110WCHAR* lfnbuf; /* LFN working buffer */111#endif112#if FF_FS_EXFAT113BYTE* dirbuf; /* Directory entry block scratchpad buffer for exFAT */114#endif115#if FF_FS_REENTRANT116FF_SYNC_t sobj; /* Identifier of sync object */117#endif118#if !FF_FS_READONLY119DWORD last_clst; /* Last allocated cluster */120DWORD free_clst; /* Number of free clusters */121#endif122#if FF_FS_RPATH123DWORD cdir; /* Current directory start cluster (0:root) */124#if FF_FS_EXFAT125DWORD cdc_scl; /* Containing directory start cluster (invalid when cdir is 0) */126DWORD cdc_size; /* b31-b8:Size of containing directory, b7-b0: Chain status */127DWORD cdc_ofs; /* Offset in the containing directory (invalid when cdir is 0) */128#endif129#endif130DWORD n_fatent; /* Number of FAT entries (number of clusters + 2) */131DWORD fsize; /* Size of an FAT [sectors] */132DWORD volbase; /* Volume base sector */133DWORD fatbase; /* FAT base sector */134DWORD dirbase; /* Root directory base sector/cluster */135DWORD database; /* Data base sector */136#if FF_FS_EXFAT137DWORD bitbase; /* Allocation bitmap base sector */138#endif139DWORD winsect; /* Current sector appearing in the win[] */140BYTE win[FF_MAX_SS] __attribute__((aligned(8))); /* Disk access window for Directory, FAT (and file data at tiny cfg). DMA aligned. */141} FATFS;142143144145/* Object ID and allocation information (FFOBJID) */146147typedef struct {148FATFS* fs; /* Pointer to the hosting volume of this object */149WORD id; /* Hosting volume mount ID */150BYTE attr; /* Object attribute */151BYTE stat; /* Object chain status (b1-0: =0:not contiguous, =2:contiguous, =3:fragmented in this session, b2:sub-directory stretched) */152DWORD sclust; /* Object data start cluster (0:no cluster or root directory) */153FSIZE_t objsize; /* Object size (valid when sclust != 0) */154#if FF_FS_EXFAT155DWORD n_cont; /* Size of first fragment - 1 (valid when stat == 3) */156DWORD n_frag; /* Size of last fragment needs to be written to FAT (valid when not zero) */157DWORD c_scl; /* Containing directory start cluster (valid when sclust != 0) */158DWORD c_size; /* b31-b8:Size of containing directory, b7-b0: Chain status (valid when c_scl != 0) */159DWORD c_ofs; /* Offset in the containing directory (valid when file object and sclust != 0) */160#endif161#if FF_FS_LOCK162UINT lockid; /* File lock ID origin from 1 (index of file semaphore table Files[]) */163#endif164} FFOBJID;165166167168/* File object structure (FIL) */169170typedef struct {171FFOBJID obj; /* Object identifier (must be the 1st member to detect invalid object pointer) */172BYTE flag; /* File status flags */173BYTE err; /* Abort flag (error code) */174FSIZE_t fptr; /* File read/write pointer (Zeroed on file open) */175DWORD clust; /* Current cluster of fpter (invalid when fptr is 0) */176DWORD sect; /* Sector number appearing in buf[] (0:invalid) */177#if !FF_FS_READONLY178DWORD dir_sect; /* Sector number containing the directory entry (not used at exFAT) */179BYTE* dir_ptr; /* Pointer to the directory entry in the win[] (not used at exFAT) */180#endif181#if FF_USE_FASTSEEK182DWORD* cltbl; /* Pointer to the cluster link map table (nulled on open, set by application) */183#endif184#if !FF_FS_TINY185BYTE buf[FF_MAX_SS] __attribute__((aligned(8))); /* File private data read/write window. DMA aligned. */186#endif187} FIL;188189190191/* Directory object structure (DIR) */192193typedef struct {194FFOBJID obj; /* Object identifier */195DWORD dptr; /* Current read/write offset */196DWORD clust; /* Current cluster */197DWORD sect; /* Current sector (0:Read operation has terminated) */198BYTE* dir; /* Pointer to the directory item in the win[] */199BYTE fn[12]; /* SFN (in/out) {body[8],ext[3],status[1]} */200#if FF_USE_LFN201DWORD blk_ofs; /* Offset of current entry block being processed (0xFFFFFFFF:Invalid) */202#endif203#if FF_USE_FIND204const TCHAR* pat; /* Pointer to the name matching pattern */205#endif206} DIR;207208209210/* File information structure (FILINFO) */211212typedef struct {213FSIZE_t fsize; /* File size */214WORD fdate; /* Modified date */215WORD ftime; /* Modified time */216BYTE fattrib; /* File attribute */217#if FF_USE_LFN218TCHAR altname[FF_SFN_BUF + 1];/* Altenative file name */219TCHAR fname[FF_LFN_BUF + 1]; /* Primary file name */220#else221TCHAR fname[12 + 1]; /* File name */222#endif223} FILINFO;224225226227/* File function return code (FRESULT) */228229typedef enum {230FR_OK = 0, /* (0) Succeeded */231FR_DISK_ERR, /* (1) A hard error occurred in the low level disk I/O layer */232FR_INT_ERR, /* (2) Assertion failed */233FR_NOT_READY, /* (3) The physical drive cannot work */234FR_NO_FILE, /* (4) Could not find the file */235FR_NO_PATH, /* (5) Could not find the path */236FR_INVALID_NAME, /* (6) The path name format is invalid */237FR_DENIED, /* (7) Access denied due to prohibited access or directory full */238FR_EXIST, /* (8) Access denied due to prohibited access */239FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */240FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */241FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */242FR_NOT_ENABLED, /* (12) The volume has no work area */243FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume */244FR_MKFS_ABORTED, /* (14) The f_mkfs() aborted due to any problem */245FR_TIMEOUT, /* (15) Could not get a grant to access the volume within defined period */246FR_LOCKED, /* (16) The operation is rejected according to the file sharing policy */247FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */248FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > FF_FS_LOCK */249#if FF_FASTFS250FR_INVALID_PARAMETER, /* (19) Given parameter is invalid */251FR_CLTBL_NO_INIT /* (20) The cluster table for fast seek/read/write was not created */252#else253FR_INVALID_PARAMETER /* (19) Given parameter is invalid */254#endif255} FRESULT;256257258259/*--------------------------------------------------------------*/260/* FatFs module application interface */261262FRESULT f_open (FIL* fp, const TCHAR* path, BYTE mode); /* Open or create a file */263FRESULT f_close (FIL* fp); /* Close an open file object */264FRESULT f_read (FIL* fp, void* buff, UINT btr, UINT* br); /* Read data from the file */265FRESULT f_write (FIL* fp, const void* buff, UINT btw, UINT* bw); /* Write data to the file */266FRESULT f_read_fast (FIL* fp, const void* buff, UINT btr); /* Fast read data from the file */267FRESULT f_write_fast (FIL* fp, const void* buff, UINT btw); /* Fast write data to the file */268FRESULT f_lseek (FIL* fp, FSIZE_t ofs); /* Move file pointer of the file object */269FRESULT f_truncate (FIL* fp); /* Truncate the file */270FRESULT f_sync (FIL* fp); /* Flush cached data of the writing file */271FRESULT f_opendir (DIR* dp, const TCHAR* path); /* Open a directory */272FRESULT f_closedir (DIR* dp); /* Close an open directory */273FRESULT f_readdir (DIR* dp, FILINFO* fno); /* Read a directory item */274FRESULT f_findfirst (DIR* dp, FILINFO* fno, const TCHAR* path, const TCHAR* pattern); /* Find first file */275FRESULT f_findnext (DIR* dp, FILINFO* fno); /* Find next file */276FRESULT f_mkdir (const TCHAR* path); /* Create a sub directory */277FRESULT f_unlink (const TCHAR* path); /* Delete an existing file or directory */278FRESULT f_rename (const TCHAR* path_old, const TCHAR* path_new); /* Rename/Move a file or directory */279FRESULT f_stat (const TCHAR* path, FILINFO* fno); /* Get file status */280FRESULT f_chmod (const TCHAR* path, BYTE attr, BYTE mask); /* Change attribute of a file/dir */281FRESULT f_utime (const TCHAR* path, const FILINFO* fno); /* Change timestamp of a file/dir */282FRESULT f_chdir (const TCHAR* path); /* Change current directory */283FRESULT f_chdrive (const TCHAR* path); /* Change current drive */284FRESULT f_getcwd (TCHAR* buff, UINT len); /* Get current directory */285FRESULT f_getfree (const TCHAR* path, DWORD* nclst, FATFS** fatfs); /* Get number of free clusters on the drive */286FRESULT f_getlabel (const TCHAR* path, TCHAR* label, DWORD* vsn); /* Get volume label */287FRESULT f_setlabel (const TCHAR* label); /* Set volume label */288FRESULT f_forward (FIL* fp, UINT(*func)(const BYTE*,UINT), UINT btf, UINT* bf); /* Forward data to the stream */289DWORD *f_expand_cltbl (FIL* fp, UINT tblsz, FSIZE_t ofs); /* Expand file and populate cluster table */290FRESULT f_expand (FIL* fp, FSIZE_t fsz, BYTE opt); /* Allocate a contiguous block to the file */291FRESULT f_mount (FATFS* fs, const TCHAR* path, BYTE opt); /* Mount/Unmount a logical drive */292FRESULT f_mkfs (const TCHAR* path, BYTE opt, DWORD au, void* work, UINT len); /* Create a FAT volume */293FRESULT f_fdisk (BYTE pdrv, const DWORD* szt, void* work); /* Divide a physical drive into some partitions */294FRESULT f_setcp (WORD cp); /* Set current code page */295int f_putc (TCHAR c, FIL* fp); /* Put a character to the file */296int f_puts (const TCHAR* str, FIL* cp); /* Put a string to the file */297int f_printf (FIL* fp, const TCHAR* str, ...); /* Put a formatted string to the file */298TCHAR* f_gets (TCHAR* buff, int len, FIL* fp); /* Get a string from the file */299300#define f_eof(fp) ((int)((fp)->fptr == (fp)->obj.objsize))301#define f_error(fp) ((fp)->err)302#define f_tell(fp) ((fp)->fptr)303#define f_size(fp) ((fp)->obj.objsize)304#define f_rewind(fp) f_lseek((fp), 0)305#define f_rewinddir(dp) f_readdir((dp), 0)306#define f_rmdir(path) f_unlink(path)307#define f_unmount(path) f_mount(0, path, 0)308309#ifndef EOF310#define EOF (-1)311#endif312313314315316/*--------------------------------------------------------------*/317/* Additional user defined functions */318319/* RTC function */320#if !FF_FS_READONLY && !FF_FS_NORTC321DWORD get_fattime (void);322#endif323324/* LFN support functions */325#if FF_USE_LFN >= 1 /* Code conversion (defined in unicode.c) */326WCHAR ff_oem2uni (WCHAR oem, WORD cp); /* OEM code to Unicode conversion */327WCHAR ff_uni2oem (DWORD uni, WORD cp); /* Unicode to OEM code conversion */328DWORD ff_wtoupper (DWORD uni); /* Unicode upper-case conversion */329#endif330#if FF_USE_LFN == 3 /* Dynamic memory allocation */331void* ff_memalloc (UINT msize); /* Allocate memory block */332void ff_memfree (void* mblock); /* Free memory block */333#endif334335/* Sync functions */336#if FF_FS_REENTRANT337int ff_cre_syncobj (BYTE vol, FF_SYNC_t* sobj); /* Create a sync object */338int ff_req_grant (FF_SYNC_t sobj); /* Lock sync object */339void ff_rel_grant (FF_SYNC_t sobj); /* Unlock sync object */340int ff_del_syncobj (FF_SYNC_t sobj); /* Delete a sync object */341#endif342343344345346/*--------------------------------------------------------------*/347/* Flags and offset address */348349350/* File access mode and open method flags (3rd argument of f_open) */351#define FA_READ 0x01352#define FA_WRITE 0x02353#define FA_OPEN_EXISTING 0x00354#define FA_CREATE_NEW 0x04355#define FA_CREATE_ALWAYS 0x08356#define FA_OPEN_ALWAYS 0x10357#define FA_OPEN_APPEND 0x30358359/* Fast seek controls (2nd argument of f_lseek) */360#define CREATE_LINKMAP ((FSIZE_t)0 - 1)361362/* Format options (2nd argument of f_mkfs) */363#define FM_FAT 0x01364#define FM_FAT32 0x02365#define FM_EXFAT 0x04366#define FM_ANY 0x07367#define FM_SFD 0x08368#define FM_PRF2 0x10369370/* Filesystem type (FATFS.fs_type) */371#define FS_FAT12 1372#define FS_FAT16 2373#define FS_FAT32 3374#define FS_EXFAT 4375376/* File attribute bits for directory entry (FILINFO.fattrib) */377#define AM_RDO 0x01 /* Read only */378#define AM_HID 0x02 /* Hidden */379#define AM_SYS 0x04 /* System */380#define AM_VOL 0x08 /* Volume */381#define AM_DIR 0x10 /* Directory */382#define AM_ARC 0x20 /* Archive */383#define AM_DEV 0x40 /* Device */384#define AM_RVD 0x80 /* Reserved */385386387#ifdef __cplusplus388}389#endif390391#endif /* FF_DEFINED */392393394