ff.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*--------------------------------------------------------------------------/
  2. / FatFs - FAT file system module include file R0.06 (C)ChaN, 2008
  3. /---------------------------------------------------------------------------/
  4. / FatFs module is an experimenal project to implement FAT file system to
  5. / cheap microcontrollers. This is a free software and is opened for education,
  6. / research and development under license policy of following trems.
  7. /
  8. / Copyright (C) 2008, ChaN, all right reserved.
  9. /
  10. / * The FatFs module is a free software and there is no warranty.
  11. / * You can use, modify and/or redistribute it for personal, non-profit or
  12. / * commercial use without any restriction under your responsibility.
  13. / * Redistributions of source code must retain the above copyright notice.
  14. /
  15. /---------------------------------------------------------------------------*/
  16. #ifndef _FATFS
  17. #define _MCU_ENDIAN 1
  18. /* The _MCU_ENDIAN defines which access method is used to the FAT structure.
  19. / 1: Enable word access.
  20. / 2: Disable word access and use byte-by-byte access instead.
  21. / When the architectural byte order of the MCU is big-endian and/or address
  22. / miss-aligned access results incorrect behavior, the _MCU_ENDIAN must be set
  23. / to 2. If it is not the case, it can be set to 1 for good code efficiency. */
  24. #define _FS_READONLY 0
  25. /* Setting _FS_READONLY to 1 defines read only configuration. This removes
  26. / writing functions, f_write, f_sync, f_unlink, f_mkdir, f_chmod, f_rename,
  27. / f_truncate and useless f_getfree. */
  28. #define _FS_MINIMIZE 0
  29. /* The _FS_MINIMIZE option defines minimization level to remove some functions.
  30. / 0: Full function.
  31. / 1: f_stat, f_getfree, f_unlink, f_mkdir, f_chmod, f_truncate and f_rename are removed.
  32. / 2: f_opendir and f_readdir are removed in addition to level 1.
  33. / 3: f_lseek is removed in addition to level 2. */
  34. #define _USE_STRFUNC 0
  35. /* To enable string functions, set _USE_STRFUNC to 1 or 2. */
  36. #define _DRIVES 1
  37. /*MAX_DRIVES*/
  38. /* Number of physical drives to be used. This affects the size of internal
  39. * table. (when using _USE_DRIVE_PREFIX */
  40. #define _USE_MKFS 0
  41. /* When _USE_MKFS is set to 1 and _FS_READONLY is set to 0, f_mkfs function is
  42. / enabled. */
  43. #define _MULTI_PARTITION 0
  44. /* When _MULTI_PARTITION is set to 0, each logical drive is bound to the same
  45. / physical drive number and can mount only 1st primary partition.
  46. /
  47. / When it is set to 1, the low _PARTITION_MASK bits of each partition represent
  48. / the partition and the high (8-_PARTITION_MASK) bits represent the physical
  49. / drive */
  50. #define _PARTITION_MASK 4
  51. #define _USE_FSINFO 1
  52. /* To enable FSInfo support on FAT32 volume, set _USE_FSINFO to 1. */
  53. #define _USE_SJIS 0
  54. /* When _USE_SJIS is set to 1, Shift-JIS code transparency is enabled, otherwise
  55. / only US-ASCII(7bit) code can be accepted as file/directory name. */
  56. #define _USE_NTFLAG 0
  57. /* When _USE_NTFLAG is set to 1, upper/lower case of the file name is preserved.
  58. / Note that the files are always accessed in case insensitive. */
  59. #define _USE_CHDIR 0
  60. #define _USE_CURR_DIR 1
  61. #define _USE_LFN 1
  62. /* Maximum number of characters to return for a LFN */
  63. /* The buffer used for FILINFO.lfn must be at least */
  64. /* _MAX_LFN_LENGTH+1 characters long! */
  65. /* Note that if _USE_LFN_DBCS is set, this value */
  66. /* represents the characters needed, not bytes */
  67. #define _MAX_LFN_LENGTH 255
  68. /* When _USE_LFN_DBCS is set to 1, FILINFO.lfn will contain a DBCS string, not
  69. / a simple ASCII string */
  70. #define _USE_LFN_DBCS 0
  71. /* When set to 1, All FIL objects will use the buffer. This reduces memory
  72. / requirements as open files will only require space for a FIL object, but
  73. / operate slower. When set, ff.c will behave like tff.c, but will allow
  74. / multiple filesystems. */
  75. #define _USE_FS_BUF 1
  76. /* When set to 1, All objects will use a static buffer. This reduces memory
  77. / requirements to the absolute minimum ~512 bytes for the buffer, but will
  78. / operate slower. This option can only be set if _USE_FS_BUF is set. */
  79. #define _USE_1_BUF 1
  80. /* If set to 1, FatFs will manage the FATFS structures after mounting. If
  81. / set to 0, the caller must send the correct drive FATFS structure for each
  82. / call. Normally, this should be set to 1, but if the caller wants to use
  83. / low level l_* functions or skip sending the drive number in the path string,
  84. / this must be turned off. */
  85. #define _USE_DRIVE_PREFIX 1
  86. /* If set to 1, FatFS will delay mounting the drive until first use. Normally,
  87. / this should be turned on. However, it cannot be used with
  88. / _USE_DRIVE_PREFIX = 0 */
  89. #define _USE_DEFERRED_MOUNT 0
  90. /* New features in 0.05a, not required yet */
  91. #define _USE_TRUNCATE 0
  92. #define _USE_UTIME 0
  93. #include "integer.h"
  94. #if _USE_LFN_DBCS != 0
  95. #define S_LFN_OFFSET 26
  96. #define S_LFN_INCREMENT 2
  97. #else
  98. #define S_LFN_OFFSET 13
  99. #define S_LFN_INCREMENT 1
  100. #endif
  101. /* Definitions corresponds to multiple sector size (not tested) */
  102. #define S_MAX_SIZ 512U /* Do not change */
  103. #if S_MAX_SIZ > 512U
  104. #define SS(fs) ((fs)->s_size)
  105. #else
  106. #define SS(fs) 512U
  107. #endif
  108. #if _USE_1_BUF == 1 && _USE_FS_BUF == 0
  109. #error You can only use 1_BUF with _USE_FS_BUF at present
  110. #define _USE_1_BUF 0
  111. #endif
  112. typedef struct _BUF {
  113. DWORD sect;
  114. BYTE dirty; /* dirty flag (1:must be written back) */
  115. //BYTE pad1;
  116. #if _USE_1_BUF != 0
  117. struct _FATFS *fs;
  118. #endif
  119. BYTE data[S_MAX_SIZ]; /* Disk access window for Directory/FAT */
  120. } BUF;
  121. /* File system object structure */
  122. typedef struct _FATFS {
  123. //WORD id; /* File system mount ID */
  124. WORD n_rootdir; /* Number of root directory entries */
  125. DWORD sects_fat; /* Sectors per fat */
  126. DWORD max_clust; /* Maximum cluster# + 1 */
  127. DWORD fatbase; /* FAT start sector */
  128. DWORD dirbase; /* Root directory start sector (cluster# for FAT32) */
  129. DWORD database; /* Data start sector */
  130. #if _USE_CHDIR != 0 || _USE_CURR_DIR != 0
  131. DWORD curr_dir;
  132. #endif
  133. #if !_FS_READONLY
  134. DWORD last_clust; /* Last allocated cluster */
  135. DWORD free_clust; /* Number of free clusters */
  136. #if _USE_FSINFO
  137. DWORD fsi_sector; /* fsinfo sector */
  138. BYTE fsi_flag; /* fsinfo dirty flag (1:must be written back) */
  139. //BYTE pad2;
  140. #endif
  141. #endif
  142. BYTE fs_type; /* FAT sub type */
  143. BYTE csize; /* Number of sectors per cluster */
  144. #if S_MAX_SIZ > 512U
  145. WORD s_size; /* Sector size */
  146. #endif
  147. BYTE n_fats; /* Number of FAT copies */
  148. BYTE drive; /* Physical drive number */
  149. #if _USE_1_BUF == 0
  150. BUF buf;
  151. #endif
  152. } FATFS;
  153. /* Directory object structure */
  154. typedef struct _DIR {
  155. //WORD id; /* Owner file system mount ID */
  156. WORD index; /* Current index */
  157. FATFS* fs; /* Pointer to the owner file system object */
  158. DWORD sclust; /* Start cluster */
  159. DWORD clust; /* Current cluster */
  160. DWORD sect; /* Current sector */
  161. } DIR;
  162. /* File object structure */
  163. typedef struct _FIL {
  164. //WORD id; /* Owner file system mount ID */
  165. BYTE flag; /* File status flags */
  166. BYTE csect; /* Sector address in the cluster */
  167. FATFS* fs; /* Pointer to the owner file system object */
  168. DWORD fptr; /* File R/W pointer */
  169. DWORD fsize; /* File size */
  170. DWORD org_clust; /* File start cluster */
  171. DWORD curr_clust; /* Current cluster */
  172. DWORD curr_sect; /* Current sector */
  173. #if _FS_READONLY == 0
  174. DWORD dir_sect; /* Sector containing the directory entry */
  175. BYTE* dir_ptr; /* Ponter to the directory entry in the window */
  176. #endif
  177. #if _USE_LESS_BUF == 0 && _USE_1_BUF == 0
  178. BUF buf; /* File R/W buffer */
  179. #endif
  180. } FIL;
  181. /* File status structure */
  182. typedef struct _FILINFO {
  183. DWORD fsize; /* Size */
  184. WORD fdate; /* Date */
  185. WORD ftime; /* Time */
  186. BYTE fattrib; /* Attribute */
  187. DWORD clust; /* Start cluster */
  188. UCHAR fname[8+1+3+1]; /* Name (8.3 format) */
  189. #if _USE_LFN != 0
  190. UCHAR* lfn;
  191. #endif
  192. } FILINFO;
  193. /* Definitions corresponds to multi partition */
  194. #if _MULTI_PARTITION != 0 /* Multiple partition cfg */
  195. #define LD2PD(drv) (drv >> (8-_PARTITION_MASK)) /* Get physical drive# */
  196. #define LD2PT(drv) (drv & ((1<<_PARTITION_MASK)-1)) /* Get partition# */
  197. #define _LOGICAL_DRIVES (_DRIVES * (1<<_PARTITION_MASK))
  198. #else /* Single partition cfg */
  199. #define LD2PD(drv) (drv) /* Physical drive# is equal to logical drive# */
  200. #define LD2PT(drv) 0 /* Always mounts the 1st partition */
  201. #define _LOGICAL_DRIVES _DRIVES
  202. #endif
  203. /* File function return code (FRESULT) */
  204. typedef enum {
  205. FR_OK = 0, /* 0 */
  206. FR_NOT_READY, /* 1 */
  207. FR_NO_FILE, /* 2 */
  208. FR_NO_PATH, /* 3 */
  209. FR_INVALID_NAME, /* 4 */
  210. FR_INVALID_DRIVE, /* 5 */
  211. FR_DENIED, /* 6 */
  212. FR_EXIST, /* 7 */
  213. FR_RW_ERROR, /* 8 */
  214. FR_WRITE_PROTECTED, /* 9 */
  215. FR_NOT_ENABLED, /* 10 */
  216. FR_NO_FILESYSTEM, /* 11 */
  217. FR_INVALID_OBJECT, /* 12 */
  218. FR_MKFS_ABORTED, /* 13 */
  219. FR_IS_DIRECTORY, /* 14 */
  220. FR_IS_READONLY, /* 15 */
  221. FR_DIR_NOT_EMPTY, /* 16 */
  222. FR_NOT_DIRECTORY /* 17 */
  223. } FRESULT;
  224. /*-----------------------------------------------------*/
  225. /* FatFs module application interface */
  226. #if _USE_DRIVE_PREFIX == 0
  227. FRESULT f_mount (BYTE, FATFS*); /* Mount/Unmount a logical drive */
  228. FRESULT f_open (FATFS*, FIL*, const UCHAR*, BYTE); /* Open or create a file */
  229. FRESULT f_read (FIL*, void*, UINT, UINT*); /* Read data from a file */
  230. FRESULT f_write (FIL*, const void*, UINT, UINT*); /* Write data to a file */
  231. FRESULT f_lseek (FIL*, DWORD); /* Move file pointer of a file object */
  232. FRESULT f_close (FIL*); /* Close an open file object */
  233. FRESULT f_opendir (FATFS*, DIR*, const UCHAR*); /* Open an existing directory */
  234. FRESULT f_readdir (DIR*, FILINFO*); /* Read a directory item */
  235. FRESULT f_stat (FATFS*, const UCHAR*, FILINFO*); /* Get file status */
  236. FRESULT f_getfree (FATFS*, const UCHAR*, DWORD*); /* Get number of free clusters on the drive */
  237. FRESULT f_sync (FIL*); /* Flush cached data of a writing file */
  238. FRESULT f_unlink (FATFS*, const UCHAR*); /* Delete an existing file or directory */
  239. FRESULT f_mkdir (FATFS*, const UCHAR*); /* Create a new directory */
  240. FRESULT f_chmod (FATFS*, const UCHAR*, BYTE, BYTE); /* Change file/dir attriburte */
  241. FRESULT f_rename (FATFS*, const UCHAR*, const UCHAR*); /* Rename/Move a file or directory */
  242. FRESULT f_mkfs (BYTE, BYTE, WORD); /* Create a file system on the drive */
  243. FRESULT f_chdir (FATFS*, const UCHAR*); /* Change current directory */
  244. #else
  245. FRESULT f_mount (BYTE, FATFS*); /* Mount/Unmount a logical drive */
  246. FRESULT f_open (FIL*, const UCHAR*, BYTE); /* Open or create a file */
  247. FRESULT f_read (FIL*, void*, UINT, UINT*); /* Read data from a file */
  248. FRESULT f_write (FIL*, const void*, UINT, UINT*); /* Write data to a file */
  249. FRESULT f_lseek (FIL*, DWORD); /* Move file pointer of a file object */
  250. FRESULT f_close (FIL*); /* Close an open file object */
  251. FRESULT f_opendir (DIR*, const UCHAR*); /* Open an existing directory */
  252. FRESULT f_readdir (DIR*, FILINFO*); /* Read a directory item */
  253. FRESULT f_stat (const UCHAR*, FILINFO*); /* Get file status */
  254. FRESULT f_getfree (const UCHAR*, DWORD*, FATFS**); /* Get number of free clusters on the drive */
  255. FRESULT f_sync (FIL*); /* Flush cached data of a writing file */
  256. FRESULT f_unlink (const UCHAR*); /* Delete an existing file or directory */
  257. FRESULT f_mkdir (const UCHAR*); /* Create a new directory */
  258. FRESULT f_chmod (const UCHAR*, BYTE, BYTE); /* Change file/dir attriburte */
  259. FRESULT f_rename (const UCHAR*, const UCHAR*); /* Rename/Move a file or directory */
  260. FRESULT f_mkfs (BYTE, BYTE, WORD); /* Create a file system on the drive */
  261. FRESULT f_chdir (const UCHAR*); /* Change current directory */
  262. #endif
  263. /* Low Level functions */
  264. FRESULT l_opendir(FATFS* fs, DWORD cluster, DIR *dirobj); /* Open an existing directory by its start cluster */
  265. FRESULT l_opencluster(FATFS *fs, FIL *fp, DWORD clust); /* Open a cluster by number as a read-only file */
  266. FRESULT l_openfilebycluster(FATFS *fs, FIL *fp, const UCHAR *path, DWORD clust, DWORD fsize); /* Open a file by its start cluster using supplied file size */
  267. FRESULT l_getfree (FATFS*, const UCHAR*, DWORD*, DWORD); /* Get number of free clusters on the drive, limited */
  268. #if _USE_STRFUNC
  269. #define feof(fp) ((fp)->fptr == (fp)->fsize)
  270. #define EOF -1
  271. int fputc (int, FIL*); /* Put a character to the file */
  272. int fputs (const char*, FIL*); /* Put a string to the file */
  273. int fprintf (FIL*, const char*, ...); /* Put a formatted string to the file */
  274. char* fgets (char*, int, FIL*); /* Get a string from the file */
  275. #endif
  276. /* User defined function to give a current time to fatfs module */
  277. #if CONFIG_RTC_VARIANT > 0
  278. DWORD get_fattime (void); /* 31-25: Year(0-127 org.1980), 24-21: Month(1-12), 20-16: Day(1-31) */
  279. /* 15-11: Hour(0-23), 10-5: Minute(0-59), 4-0: Second(0-29 *2) */
  280. #else
  281. /* Fixed time: 1982-08-31 0:00:00, same month as the introduction of the C64 */
  282. # define get_fattime() 0x51f0000
  283. #endif
  284. /* File access control and file status flags (FIL.flag) */
  285. #define FA_READ 0x01
  286. #define FA_OPEN_EXISTING 0x00
  287. #if _FS_READONLY == 0
  288. #define FA_WRITE 0x02
  289. #define FA_CREATE_NEW 0x04
  290. #define FA_CREATE_ALWAYS 0x08
  291. #define FA_OPEN_ALWAYS 0x10
  292. #define FA__WRITTEN 0x20
  293. #define FA__DIRTY 0x40
  294. #endif
  295. #define FA__ERROR 0x80
  296. /* FAT sub type (FATFS.fs_type) */
  297. #define FS_FAT12 1
  298. #define FS_FAT16 2
  299. #define FS_FAT32 3
  300. /* File attribute bits for directory entry */
  301. #define AM_RDO 0x01 /* Read only */
  302. #define AM_HID 0x02 /* Hidden */
  303. #define AM_SYS 0x04 /* System */
  304. #define AM_VOL 0x08 /* Volume label */
  305. #define AM_LFN 0x0F /* LFN entry */
  306. #define AM_DIR 0x10 /* Directory */
  307. #define AM_ARC 0x20 /* Archive */
  308. /* Offset of FAT structure members */
  309. #define BS_jmpBoot 0
  310. #define BS_OEMName 3
  311. #define BPB_BytsPerSec 11
  312. #define BPB_SecPerClus 13
  313. #define BPB_RsvdSecCnt 14
  314. #define BPB_NumFATs 16
  315. #define BPB_RootEntCnt 17
  316. #define BPB_TotSec16 19
  317. #define BPB_Media 21
  318. #define BPB_FATSz16 22
  319. #define BPB_SecPerTrk 24
  320. #define BPB_NumHeads 26
  321. #define BPB_HiddSec 28
  322. #define BPB_TotSec32 32
  323. #define BS_55AA 510
  324. #define BS_DrvNum 36
  325. #define BS_BootSig 38
  326. #define BS_VolID 39
  327. #define BS_VolLab 43
  328. #define BS_FilSysType 54
  329. #define BPB_FATSz32 36
  330. #define BPB_ExtFlags 40
  331. #define BPB_FSVer 42
  332. #define BPB_RootClus 44
  333. #define BPB_FSInfo 48
  334. #define BPB_BkBootSec 50
  335. #define BS_DrvNum32 64
  336. #define BS_BootSig32 66
  337. #define BS_VolID32 67
  338. #define BS_VolLab32 71
  339. #define BS_FilSysType32 82
  340. #define FSI_LeadSig 0
  341. #define FSI_StrucSig 484
  342. #define FSI_Free_Count 488
  343. #define FSI_Nxt_Free 492
  344. #define MBR_Table 446
  345. #define DIR_Name 0
  346. #define DIR_Attr 11
  347. #define DIR_NTres 12
  348. #define DIR_Chksum 13
  349. #define DIR_CrtTime 14
  350. #define DIR_CrtDate 16
  351. #define DIR_FstClusHI 20
  352. #define DIR_WrtTime 22
  353. #define DIR_WrtDate 24
  354. #define DIR_FstClusLO 26
  355. #define DIR_FileSize 28
  356. /* Multi-byte word access macros */
  357. #if _MCU_ENDIAN == 1 /* Use word access */
  358. #define LD_WORD(ptr) (WORD)(*(WORD*)(BYTE*)(ptr))
  359. #define LD_DWORD(ptr) (DWORD)(*(DWORD*)(BYTE*)(ptr))
  360. #define ST_WORD(ptr,val) *(WORD*)(BYTE*)(ptr)=(WORD)(val)
  361. #define ST_DWORD(ptr,val) *(DWORD*)(BYTE*)(ptr)=(DWORD)(val)
  362. #elif _MCU_ENDIAN == 2 /* Use byte-by-byte access */
  363. #define LD_WORD(ptr) (WORD)(((WORD)*(volatile BYTE*)((ptr)+1)<<8)|(WORD)*(volatile BYTE*)(ptr))
  364. #define LD_DWORD(ptr) (DWORD)(((DWORD)*(volatile BYTE*)((ptr)+3)<<24)|((DWORD)*(volatile BYTE*)((ptr)+2)<<16)|((WORD)*(volatile BYTE*)((ptr)+1)<<8)|*(volatile BYTE*)(ptr))
  365. #define ST_WORD(ptr,val) *(volatile BYTE*)(ptr)=(BYTE)(val); *(volatile BYTE*)((ptr)+1)=(BYTE)((WORD)(val)>>8)
  366. #define ST_DWORD(ptr,val) *(volatile BYTE*)(ptr)=(BYTE)(val); *(volatile BYTE*)((ptr)+1)=(BYTE)((WORD)(val)>>8); *(volatile BYTE*)((ptr)+2)=(BYTE)((DWORD)(val)>>16); *(volatile BYTE*)((ptr)+3)=(BYTE)((DWORD)(val)>>24)
  367. #else
  368. #error Do not forget to set _MCU_ENDIAN properly!
  369. #endif
  370. #define _FATFS
  371. #endif /* _FATFS */