]> cloudbase.mooo.com Git - z180-stamp.git/blame - fatfs/source/ff.c
Merge branch 'chan-fatfs' into fatfs-integration
[z180-stamp.git] / fatfs / source / ff.c
CommitLineData
53668523 1/*----------------------------------------------------------------------------/\r
289f6a14 2/ FatFs - Generic FAT Filesystem Module R0.13b /\r
53668523 3/-----------------------------------------------------------------------------/\r
53668523 4/\r
289f6a14 5/ Copyright (C) 2018, ChaN, all right reserved.\r
53668523 6/\r
70702af1
L
7/ FatFs module is an open source software. Redistribution and use of FatFs in\r
8/ source and binary forms, with or without modification, are permitted provided\r
9/ that the following condition is met:\r
289f6a14 10/\r
70702af1
L
11/ 1. Redistributions of source code must retain the above copyright notice,\r
12/ this condition and the following disclaimer.\r
13/\r
14/ This software is provided by the copyright holder and contributors "AS IS"\r
15/ and any warranties related to this software are DISCLAIMED.\r
16/ The copyright owner or contributors be NOT LIABLE for any damages caused\r
17/ by use of this software.\r
289f6a14 18/\r
70702af1 19/----------------------------------------------------------------------------*/\r
53668523
L
20\r
21\r
70702af1
L
22#include "ff.h" /* Declarations of FatFs API */\r
23#include "diskio.h" /* Declarations of device I/O functions */\r
53668523
L
24\r
25\r
26/*--------------------------------------------------------------------------\r
27\r
28 Module Private Definitions\r
29\r
30---------------------------------------------------------------------------*/\r
31\r
289f6a14 32#if FF_DEFINED != 63463 /* Revision ID */\r
53668523
L
33#error Wrong include file (ff.h).\r
34#endif\r
35\r
36\r
53668523 37/* Character code support macros */\r
289f6a14
L
38#define IsUpper(c) ((c) >= 'A' && (c) <= 'Z')\r
39#define IsLower(c) ((c) >= 'a' && (c) <= 'z')\r
40#define IsDigit(c) ((c) >= '0' && (c) <= '9')\r
41#define IsSurrogate(c) ((c) >= 0xD800 && (c) <= 0xDFFF)\r
42#define IsSurrogateH(c) ((c) >= 0xD800 && (c) <= 0xDBFF)\r
43#define IsSurrogateL(c) ((c) >= 0xDC00 && (c) <= 0xDFFF)\r
53668523 44\r
53668523 45\r
289f6a14
L
46/* Additional file attribute bits for internal use */\r
47#define AM_VOL 0x08 /* Volume label */\r
70702af1
L
48#define AM_LFN 0x0F /* LFN entry */\r
49#define AM_MASK 0x3F /* Mask of defined bits */\r
50\r
51\r
289f6a14 52/* Additional file access control and file status flags for internal use */\r
70702af1
L
53#define FA_SEEKEND 0x20 /* Seek to end of the file on file open */\r
54#define FA_MODIFIED 0x40 /* File has been modified */\r
55#define FA_DIRTY 0x80 /* FIL.buf[] needs to be written-back */\r
56\r
57\r
289f6a14
L
58/* Name status flags in fn[11] */\r
59#define NSFLAG 11 /* Index of the name status byte */\r
53668523
L
60#define NS_LOSS 0x01 /* Out of 8.3 format */\r
61#define NS_LFN 0x02 /* Force to create LFN entry */\r
62#define NS_LAST 0x04 /* Last segment */\r
63#define NS_BODY 0x08 /* Lower case flag (body) */\r
64#define NS_EXT 0x10 /* Lower case flag (ext) */\r
65#define NS_DOT 0x20 /* Dot entry */\r
70702af1
L
66#define NS_NOLFN 0x40 /* Do not find LFN */\r
67#define NS_NONAME 0x80 /* Not followed */\r
53668523
L
68\r
69\r
289f6a14
L
70/* Limits and boundaries */\r
71#define MAX_DIR 0x200000 /* Max size of FAT directory */\r
72#define MAX_DIR_EX 0x10000000 /* Max size of exFAT directory */\r
73#define MAX_FAT12 0xFF5 /* Max FAT12 clusters (differs from specs, but right for real DOS/Windows behavior) */\r
74#define MAX_FAT16 0xFFF5 /* Max FAT16 clusters (differs from specs, but right for real DOS/Windows behavior) */\r
75#define MAX_FAT32 0x0FFFFFF5 /* Max FAT32 clusters (not specified, practical limit) */\r
76#define MAX_EXFAT 0x7FFFFFFD /* Max exFAT clusters (differs from specs, implementation limit) */\r
53668523
L
77\r
78\r
289f6a14
L
79/* FatFs refers the FAT structure as simple byte array instead of structure member\r
80/ because the C structure is not binary compatible between different platforms */\r
53668523 81\r
70702af1
L
82#define BS_JmpBoot 0 /* x86 jump instruction (3-byte) */\r
83#define BS_OEMName 3 /* OEM name (8-byte) */\r
84#define BPB_BytsPerSec 11 /* Sector size [byte] (WORD) */\r
85#define BPB_SecPerClus 13 /* Cluster size [sector] (BYTE) */\r
86#define BPB_RsvdSecCnt 14 /* Size of reserved area [sector] (WORD) */\r
87#define BPB_NumFATs 16 /* Number of FATs (BYTE) */\r
289f6a14 88#define BPB_RootEntCnt 17 /* Size of root directory area for FAT [entry] (WORD) */\r
70702af1
L
89#define BPB_TotSec16 19 /* Volume size (16-bit) [sector] (WORD) */\r
90#define BPB_Media 21 /* Media descriptor byte (BYTE) */\r
91#define BPB_FATSz16 22 /* FAT size (16-bit) [sector] (WORD) */\r
289f6a14 92#define BPB_SecPerTrk 24 /* Number of sectors per track for int13h [sector] (WORD) */\r
70702af1
L
93#define BPB_NumHeads 26 /* Number of heads for int13h (WORD) */\r
94#define BPB_HiddSec 28 /* Volume offset from top of the drive (DWORD) */\r
95#define BPB_TotSec32 32 /* Volume size (32-bit) [sector] (DWORD) */\r
96#define BS_DrvNum 36 /* Physical drive number for int13h (BYTE) */\r
289f6a14 97#define BS_NTres 37 /* WindowsNT error flag (BYTE) */\r
70702af1
L
98#define BS_BootSig 38 /* Extended boot signature (BYTE) */\r
99#define BS_VolID 39 /* Volume serial number (DWORD) */\r
100#define BS_VolLab 43 /* Volume label string (8-byte) */\r
289f6a14 101#define BS_FilSysType 54 /* Filesystem type string (8-byte) */\r
70702af1
L
102#define BS_BootCode 62 /* Boot code (448-byte) */\r
103#define BS_55AA 510 /* Signature word (WORD) */\r
104\r
105#define BPB_FATSz32 36 /* FAT32: FAT size [sector] (DWORD) */\r
106#define BPB_ExtFlags32 40 /* FAT32: Extended flags (WORD) */\r
289f6a14 107#define BPB_FSVer32 42 /* FAT32: Filesystem version (WORD) */\r
70702af1
L
108#define BPB_RootClus32 44 /* FAT32: Root directory cluster (DWORD) */\r
109#define BPB_FSInfo32 48 /* FAT32: Offset of FSINFO sector (WORD) */\r
110#define BPB_BkBootSec32 50 /* FAT32: Offset of backup boot sector (WORD) */\r
111#define BS_DrvNum32 64 /* FAT32: Physical drive number for int13h (BYTE) */\r
112#define BS_NTres32 65 /* FAT32: Error flag (BYTE) */\r
113#define BS_BootSig32 66 /* FAT32: Extended boot signature (BYTE) */\r
114#define BS_VolID32 67 /* FAT32: Volume serial number (DWORD) */\r
115#define BS_VolLab32 71 /* FAT32: Volume label string (8-byte) */\r
289f6a14 116#define BS_FilSysType32 82 /* FAT32: Filesystem type string (8-byte) */\r
70702af1
L
117#define BS_BootCode32 90 /* FAT32: Boot code (420-byte) */\r
118\r
119#define BPB_ZeroedEx 11 /* exFAT: MBZ field (53-byte) */\r
120#define BPB_VolOfsEx 64 /* exFAT: Volume offset from top of the drive [sector] (QWORD) */\r
121#define BPB_TotSecEx 72 /* exFAT: Volume size [sector] (QWORD) */\r
122#define BPB_FatOfsEx 80 /* exFAT: FAT offset from top of the volume [sector] (DWORD) */\r
123#define BPB_FatSzEx 84 /* exFAT: FAT size [sector] (DWORD) */\r
124#define BPB_DataOfsEx 88 /* exFAT: Data offset from top of the volume [sector] (DWORD) */\r
125#define BPB_NumClusEx 92 /* exFAT: Number of clusters (DWORD) */\r
289f6a14 126#define BPB_RootClusEx 96 /* exFAT: Root directory start cluster (DWORD) */\r
70702af1 127#define BPB_VolIDEx 100 /* exFAT: Volume serial number (DWORD) */\r
289f6a14
L
128#define BPB_FSVerEx 104 /* exFAT: Filesystem version (WORD) */\r
129#define BPB_VolFlagEx 106 /* exFAT: Volume flags (WORD) */\r
130#define BPB_BytsPerSecEx 108 /* exFAT: Log2 of sector size in unit of byte (BYTE) */\r
131#define BPB_SecPerClusEx 109 /* exFAT: Log2 of cluster size in unit of sector (BYTE) */\r
70702af1
L
132#define BPB_NumFATsEx 110 /* exFAT: Number of FATs (BYTE) */\r
133#define BPB_DrvNumEx 111 /* exFAT: Physical drive number for int13h (BYTE) */\r
134#define BPB_PercInUseEx 112 /* exFAT: Percent in use (BYTE) */\r
289f6a14 135#define BPB_RsvdEx 113 /* exFAT: Reserved (7-byte) */\r
70702af1
L
136#define BS_BootCodeEx 120 /* exFAT: Boot code (390-byte) */\r
137\r
289f6a14
L
138#define DIR_Name 0 /* Short file name (11-byte) */\r
139#define DIR_Attr 11 /* Attribute (BYTE) */\r
140#define DIR_NTres 12 /* Lower case flag (BYTE) */\r
141#define DIR_CrtTime10 13 /* Created time sub-second (BYTE) */\r
142#define DIR_CrtTime 14 /* Created time (DWORD) */\r
143#define DIR_LstAccDate 18 /* Last accessed date (WORD) */\r
144#define DIR_FstClusHI 20 /* Higher 16-bit of first cluster (WORD) */\r
145#define DIR_ModTime 22 /* Modified time (DWORD) */\r
146#define DIR_FstClusLO 26 /* Lower 16-bit of first cluster (WORD) */\r
147#define DIR_FileSize 28 /* File size (DWORD) */\r
148#define LDIR_Ord 0 /* LFN: LFN order and LLE flag (BYTE) */\r
149#define LDIR_Attr 11 /* LFN: LFN attribute (BYTE) */\r
150#define LDIR_Type 12 /* LFN: Entry type (BYTE) */\r
151#define LDIR_Chksum 13 /* LFN: Checksum of the SFN (BYTE) */\r
152#define LDIR_FstClusLO 26 /* LFN: MBZ field (WORD) */\r
153#define XDIR_Type 0 /* exFAT: Type of exFAT directory entry (BYTE) */\r
154#define XDIR_NumLabel 1 /* exFAT: Number of volume label characters (BYTE) */\r
155#define XDIR_Label 2 /* exFAT: Volume label (11-WORD) */\r
156#define XDIR_CaseSum 4 /* exFAT: Sum of case conversion table (DWORD) */\r
157#define XDIR_NumSec 1 /* exFAT: Number of secondary entries (BYTE) */\r
158#define XDIR_SetSum 2 /* exFAT: Sum of the set of directory entries (WORD) */\r
159#define XDIR_Attr 4 /* exFAT: File attribute (WORD) */\r
160#define XDIR_CrtTime 8 /* exFAT: Created time (DWORD) */\r
161#define XDIR_ModTime 12 /* exFAT: Modified time (DWORD) */\r
162#define XDIR_AccTime 16 /* exFAT: Last accessed time (DWORD) */\r
163#define XDIR_CrtTime10 20 /* exFAT: Created time subsecond (BYTE) */\r
164#define XDIR_ModTime10 21 /* exFAT: Modified time subsecond (BYTE) */\r
165#define XDIR_CrtTZ 22 /* exFAT: Created timezone (BYTE) */\r
166#define XDIR_ModTZ 23 /* exFAT: Modified timezone (BYTE) */\r
167#define XDIR_AccTZ 24 /* exFAT: Last accessed timezone (BYTE) */\r
168#define XDIR_GenFlags 33 /* exFAT: General secondary flags (BYTE) */\r
169#define XDIR_NumName 35 /* exFAT: Number of file name characters (BYTE) */\r
170#define XDIR_NameHash 36 /* exFAT: Hash of file name (WORD) */\r
171#define XDIR_ValidFileSize 40 /* exFAT: Valid file size (QWORD) */\r
172#define XDIR_FstClus 52 /* exFAT: First cluster of the file data (DWORD) */\r
173#define XDIR_FileSize 56 /* exFAT: File/Directory size (QWORD) */\r
174\r
175#define SZDIRE 32 /* Size of a directory entry */\r
176#define DDEM 0xE5 /* Deleted directory entry mark set to DIR_Name[0] */\r
177#define RDDEM 0x05 /* Replacement of the character collides with DDEM */\r
178#define LLEF 0x40 /* Last long entry flag in LDIR_Ord */\r
179\r
180#define FSI_LeadSig 0 /* FAT32 FSI: Leading signature (DWORD) */\r
181#define FSI_StrucSig 484 /* FAT32 FSI: Structure signature (DWORD) */\r
182#define FSI_Free_Count 488 /* FAT32 FSI: Number of free clusters (DWORD) */\r
183#define FSI_Nxt_Free 492 /* FAT32 FSI: Last allocated cluster (DWORD) */\r
70702af1
L
184\r
185#define MBR_Table 446 /* MBR: Offset of partition table in the MBR */\r
289f6a14 186#define SZ_PTE 16 /* MBR: Size of a partition table entry */\r
70702af1
L
187#define PTE_Boot 0 /* MBR PTE: Boot indicator */\r
188#define PTE_StHead 1 /* MBR PTE: Start head */\r
189#define PTE_StSec 2 /* MBR PTE: Start sector */\r
190#define PTE_StCyl 3 /* MBR PTE: Start cylinder */\r
191#define PTE_System 4 /* MBR PTE: System ID */\r
192#define PTE_EdHead 5 /* MBR PTE: End head */\r
193#define PTE_EdSec 6 /* MBR PTE: End sector */\r
194#define PTE_EdCyl 7 /* MBR PTE: End cylinder */\r
195#define PTE_StLba 8 /* MBR PTE: Start in LBA */\r
196#define PTE_SizLba 12 /* MBR PTE: Size in LBA */\r
197\r
289f6a14
L
198\r
199/* Post process on fatal error in the file operations */\r
200#define ABORT(fs, res) { fp->err = (BYTE)(res); LEAVE_FF(fs, res); }\r
201\r
202\r
203/* Re-entrancy related */\r
204#if FF_FS_REENTRANT\r
205#if FF_USE_LFN == 1\r
206#error Static LFN work area cannot be used at thread-safe configuration\r
207#endif\r
208#define LEAVE_FF(fs, res) { unlock_fs(fs, res); return res; }\r
209#else\r
210#define LEAVE_FF(fs, res) return res\r
211#endif\r
212\r
213\r
214/* Definitions of volume - physical location conversion */\r
215#if FF_MULTI_PARTITION\r
216#define LD2PD(vol) VolToPart[vol].pd /* Get physical drive number */\r
217#define LD2PT(vol) VolToPart[vol].pt /* Get partition index */\r
218#else\r
219#define LD2PD(vol) (BYTE)(vol) /* Each logical drive is bound to the same physical drive number */\r
220#define LD2PT(vol) 0 /* Find first valid partition or in SFD */\r
221#endif\r
222\r
223\r
224/* Definitions of sector size */\r
225#if (FF_MAX_SS < FF_MIN_SS) || (FF_MAX_SS != 512 && FF_MAX_SS != 1024 && FF_MAX_SS != 2048 && FF_MAX_SS != 4096) || (FF_MIN_SS != 512 && FF_MIN_SS != 1024 && FF_MIN_SS != 2048 && FF_MIN_SS != 4096)\r
226#error Wrong sector size configuration\r
227#endif\r
228#if FF_MAX_SS == FF_MIN_SS\r
229#define SS(fs) ((UINT)FF_MAX_SS) /* Fixed sector size */\r
230#else\r
231#define SS(fs) ((fs)->ssize) /* Variable sector size */\r
232#endif\r
70702af1
L
233\r
234\r
289f6a14
L
235/* Timestamp */\r
236#if FF_FS_NORTC == 1\r
237#if FF_NORTC_YEAR < 1980 || FF_NORTC_YEAR > 2107 || FF_NORTC_MON < 1 || FF_NORTC_MON > 12 || FF_NORTC_MDAY < 1 || FF_NORTC_MDAY > 31\r
238#error Invalid FF_FS_NORTC settings\r
239#endif\r
240#define GET_FATTIME() ((DWORD)(FF_NORTC_YEAR - 1980) << 25 | (DWORD)FF_NORTC_MON << 21 | (DWORD)FF_NORTC_MDAY << 16)\r
241#else\r
242#define GET_FATTIME() get_fattime()\r
243#endif\r
244\r
245\r
246/* File lock controls */\r
247#if FF_FS_LOCK != 0\r
248#if FF_FS_READONLY\r
249#error FF_FS_LOCK must be 0 at read-only configuration\r
250#endif\r
251typedef struct {\r
252 FATFS *fs; /* Object ID 1, volume (NULL:blank entry) */\r
253 DWORD clu; /* Object ID 2, containing directory (0:root) */\r
254 DWORD ofs; /* Object ID 3, offset in the directory */\r
255 WORD ctr; /* Object open counter, 0:none, 0x01..0xFF:read mode open count, 0x100:write mode */\r
256} FILESEM;\r
257#endif\r
258\r
259\r
260/* SBCS up-case tables (\x80-\xFF) */\r
261#define TBL_CT437 {0x80,0x9A,0x45,0x41,0x8E,0x41,0x8F,0x80,0x45,0x45,0x45,0x49,0x49,0x49,0x8E,0x8F, \\r
262 0x90,0x92,0x92,0x4F,0x99,0x4F,0x55,0x55,0x59,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \\r
263 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \\r
264 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
265 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \\r
266 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
267 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \\r
268 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}\r
269#define TBL_CT720 {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \\r
270 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \\r
271 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \\r
272 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
273 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \\r
274 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
275 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \\r
276 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}\r
277#define TBL_CT737 {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \\r
278 0x90,0x92,0x92,0x93,0x94,0x95,0x96,0x97,0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87, \\r
279 0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0xAA,0x92,0x93,0x94,0x95,0x96, \\r
280 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
281 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \\r
282 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
283 0x97,0xEA,0xEB,0xEC,0xE4,0xED,0xEE,0xEF,0xF5,0xF0,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \\r
284 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}\r
285#define TBL_CT771 {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \\r
286 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \\r
287 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \\r
288 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
289 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \\r
290 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDC,0xDE,0xDE, \\r
291 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \\r
292 0xF0,0xF0,0xF2,0xF2,0xF4,0xF4,0xF6,0xF6,0xF8,0xF8,0xFA,0xFA,0xFC,0xFC,0xFE,0xFF}\r
293#define TBL_CT775 {0x80,0x9A,0x91,0xA0,0x8E,0x95,0x8F,0x80,0xAD,0xED,0x8A,0x8A,0xA1,0x8D,0x8E,0x8F, \\r
294 0x90,0x92,0x92,0xE2,0x99,0x95,0x96,0x97,0x97,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9F, \\r
295 0xA0,0xA1,0xE0,0xA3,0xA3,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \\r
296 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
297 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \\r
298 0xB5,0xB6,0xB7,0xB8,0xBD,0xBE,0xC6,0xC7,0xA5,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
299 0xE0,0xE1,0xE2,0xE3,0xE5,0xE5,0xE6,0xE3,0xE8,0xE8,0xEA,0xEA,0xEE,0xED,0xEE,0xEF, \\r
300 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}\r
301#define TBL_CT850 {0x43,0x55,0x45,0x41,0x41,0x41,0x41,0x43,0x45,0x45,0x45,0x49,0x49,0x49,0x41,0x41, \\r
302 0x45,0x92,0x92,0x4F,0x4F,0x4F,0x55,0x55,0x59,0x4F,0x55,0x4F,0x9C,0x4F,0x9E,0x9F, \\r
303 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \\r
304 0xB0,0xB1,0xB2,0xB3,0xB4,0x41,0x41,0x41,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
305 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0x41,0x41,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \\r
306 0xD1,0xD1,0x45,0x45,0x45,0x49,0x49,0x49,0x49,0xD9,0xDA,0xDB,0xDC,0xDD,0x49,0xDF, \\r
307 0x4F,0xE1,0x4F,0x4F,0x4F,0x4F,0xE6,0xE8,0xE8,0x55,0x55,0x55,0x59,0x59,0xEE,0xEF, \\r
308 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}\r
309#define TBL_CT852 {0x80,0x9A,0x90,0xB6,0x8E,0xDE,0x8F,0x80,0x9D,0xD3,0x8A,0x8A,0xD7,0x8D,0x8E,0x8F, \\r
310 0x90,0x91,0x91,0xE2,0x99,0x95,0x95,0x97,0x97,0x99,0x9A,0x9B,0x9B,0x9D,0x9E,0xAC, \\r
311 0xB5,0xD6,0xE0,0xE9,0xA4,0xA4,0xA6,0xA6,0xA8,0xA8,0xAA,0x8D,0xAC,0xB8,0xAE,0xAF, \\r
312 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBD,0xBF, \\r
313 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC6,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \\r
314 0xD1,0xD1,0xD2,0xD3,0xD2,0xD5,0xD6,0xD7,0xB7,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
315 0xE0,0xE1,0xE2,0xE3,0xE3,0xD5,0xE6,0xE6,0xE8,0xE9,0xE8,0xEB,0xED,0xED,0xDD,0xEF, \\r
316 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xEB,0xFC,0xFC,0xFE,0xFF}\r
317#define TBL_CT855 {0x81,0x81,0x83,0x83,0x85,0x85,0x87,0x87,0x89,0x89,0x8B,0x8B,0x8D,0x8D,0x8F,0x8F, \\r
318 0x91,0x91,0x93,0x93,0x95,0x95,0x97,0x97,0x99,0x99,0x9B,0x9B,0x9D,0x9D,0x9F,0x9F, \\r
319 0xA1,0xA1,0xA3,0xA3,0xA5,0xA5,0xA7,0xA7,0xA9,0xA9,0xAB,0xAB,0xAD,0xAD,0xAE,0xAF, \\r
320 0xB0,0xB1,0xB2,0xB3,0xB4,0xB6,0xB6,0xB8,0xB8,0xB9,0xBA,0xBB,0xBC,0xBE,0xBE,0xBF, \\r
321 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC7,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \\r
322 0xD1,0xD1,0xD3,0xD3,0xD5,0xD5,0xD7,0xD7,0xDD,0xD9,0xDA,0xDB,0xDC,0xDD,0xE0,0xDF, \\r
323 0xE0,0xE2,0xE2,0xE4,0xE4,0xE6,0xE6,0xE8,0xE8,0xEA,0xEA,0xEC,0xEC,0xEE,0xEE,0xEF, \\r
324 0xF0,0xF2,0xF2,0xF4,0xF4,0xF6,0xF6,0xF8,0xF8,0xFA,0xFA,0xFC,0xFC,0xFD,0xFE,0xFF}\r
325#define TBL_CT857 {0x80,0x9A,0x90,0xB6,0x8E,0xB7,0x8F,0x80,0xD2,0xD3,0xD4,0xD8,0xD7,0x49,0x8E,0x8F, \\r
326 0x90,0x92,0x92,0xE2,0x99,0xE3,0xEA,0xEB,0x98,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9E, \\r
327 0xB5,0xD6,0xE0,0xE9,0xA5,0xA5,0xA6,0xA6,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \\r
328 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
329 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC7,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \\r
330 0xD0,0xD1,0xD2,0xD3,0xD4,0x49,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
331 0xE0,0xE1,0xE2,0xE3,0xE5,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xDE,0xED,0xEE,0xEF, \\r
332 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}\r
333#define TBL_CT860 {0x80,0x9A,0x90,0x8F,0x8E,0x91,0x86,0x80,0x89,0x89,0x92,0x8B,0x8C,0x98,0x8E,0x8F, \\r
334 0x90,0x91,0x92,0x8C,0x99,0xA9,0x96,0x9D,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \\r
335 0x86,0x8B,0x9F,0x96,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \\r
336 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
337 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \\r
338 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
339 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \\r
340 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}\r
341#define TBL_CT861 {0x80,0x9A,0x90,0x41,0x8E,0x41,0x8F,0x80,0x45,0x45,0x45,0x8B,0x8B,0x8D,0x8E,0x8F, \\r
342 0x90,0x92,0x92,0x4F,0x99,0x8D,0x55,0x97,0x97,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9F, \\r
343 0xA4,0xA5,0xA6,0xA7,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \\r
344 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
345 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \\r
346 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
347 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \\r
348 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}\r
349#define TBL_CT862 {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \\r
350 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \\r
351 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \\r
352 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
353 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \\r
354 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
355 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \\r
356 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}\r
357#define TBL_CT863 {0x43,0x55,0x45,0x41,0x41,0x41,0x86,0x43,0x45,0x45,0x45,0x49,0x49,0x8D,0x41,0x8F, \\r
358 0x45,0x45,0x45,0x4F,0x45,0x49,0x55,0x55,0x98,0x4F,0x55,0x9B,0x9C,0x55,0x55,0x9F, \\r
359 0xA0,0xA1,0x4F,0x55,0xA4,0xA5,0xA6,0xA7,0x49,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \\r
360 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
361 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \\r
362 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
363 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \\r
364 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}\r
365#define TBL_CT864 {0x80,0x9A,0x45,0x41,0x8E,0x41,0x8F,0x80,0x45,0x45,0x45,0x49,0x49,0x49,0x8E,0x8F, \\r
366 0x90,0x92,0x92,0x4F,0x99,0x4F,0x55,0x55,0x59,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \\r
367 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \\r
368 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
369 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \\r
370 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
371 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \\r
372 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}\r
373#define TBL_CT865 {0x80,0x9A,0x90,0x41,0x8E,0x41,0x8F,0x80,0x45,0x45,0x45,0x49,0x49,0x49,0x8E,0x8F, \\r
374 0x90,0x92,0x92,0x4F,0x99,0x4F,0x55,0x55,0x59,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \\r
375 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \\r
376 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
377 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \\r
378 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
379 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \\r
380 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}\r
381#define TBL_CT866 {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \\r
382 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \\r
383 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \\r
384 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
385 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \\r
386 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
387 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \\r
388 0xF0,0xF0,0xF2,0xF2,0xF4,0xF4,0xF6,0xF6,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}\r
389#define TBL_CT869 {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \\r
390 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x86,0x9C,0x8D,0x8F,0x90, \\r
391 0x91,0x90,0x92,0x95,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \\r
392 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
393 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \\r
394 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xA4,0xA5,0xA6,0xD9,0xDA,0xDB,0xDC,0xA7,0xA8,0xDF, \\r
395 0xA9,0xAA,0xAC,0xAD,0xB5,0xB6,0xB7,0xB8,0xBD,0xBE,0xC6,0xC7,0xCF,0xCF,0xD0,0xEF, \\r
396 0xF0,0xF1,0xD1,0xD2,0xD3,0xF5,0xD4,0xF7,0xF8,0xF9,0xD5,0x96,0x95,0x98,0xFE,0xFF}\r
397\r
398\r
399/* DBCS code range |----- 1st byte -----| |----------- 2nd byte -----------| */\r
400#define TBL_DC932 {0x81, 0x9F, 0xE0, 0xFC, 0x40, 0x7E, 0x80, 0xFC, 0x00, 0x00}\r
401#define TBL_DC936 {0x81, 0xFE, 0x00, 0x00, 0x40, 0x7E, 0x80, 0xFE, 0x00, 0x00}\r
402#define TBL_DC949 {0x81, 0xFE, 0x00, 0x00, 0x41, 0x5A, 0x61, 0x7A, 0x81, 0xFE}\r
403#define TBL_DC950 {0x81, 0xFE, 0x00, 0x00, 0x40, 0x7E, 0xA1, 0xFE, 0x00, 0x00}\r
404\r
405\r
406/* Macros for table definitions */\r
407#define MERGE_2STR(a, b) a ## b\r
408#define MKCVTBL(hd, cp) MERGE_2STR(hd, cp)\r
409\r
70702af1
L
410\r
411\r
412\r
413/*--------------------------------------------------------------------------\r
414\r
415 Module Private Work Area\r
416\r
417---------------------------------------------------------------------------*/\r
289f6a14
L
418/* Remark: Variables defined here without initial value shall be guaranteed\r
419/ zero/null at start-up. If not, the linker option or start-up routine is\r
70702af1 420/ not compliance with C standard. */\r
53668523 421\r
289f6a14
L
422/*--------------------------------*/\r
423/* File/Volume controls */\r
424/*--------------------------------*/\r
425\r
426#if FF_VOLUMES < 1 || FF_VOLUMES > 10\r
427#error Wrong FF_VOLUMES setting\r
53668523 428#endif\r
289f6a14
L
429static FATFS* FatFs[FF_VOLUMES]; /* Pointer to the filesystem objects (logical drives) */\r
430static WORD Fsid; /* Filesystem mount ID */\r
53668523 431\r
289f6a14
L
432#if FF_FS_RPATH != 0\r
433static BYTE CurrVol; /* Current drive */\r
53668523
L
434#endif\r
435\r
289f6a14
L
436#if FF_FS_LOCK != 0\r
437static FILESEM Files[FF_FS_LOCK]; /* Open object lock semaphores */\r
53668523
L
438#endif\r
439\r
289f6a14
L
440#if FF_STR_VOLUME_ID\r
441#ifdef FF_VOLUME_STRS\r
442static const char* const VolumeStr[FF_VOLUMES] = {FF_VOLUME_STRS}; /* Pre-defined volume ID */\r
443#endif\r
444#endif\r
445\r
446\r
447/*--------------------------------*/\r
448/* LFN/Directory working buffer */\r
449/*--------------------------------*/\r
450\r
451#if FF_USE_LFN == 0 /* Non-LFN configuration */\r
452#if FF_FS_EXFAT\r
453#error LFN must be enabled when enable exFAT\r
454#endif\r
455#define DEF_NAMBUF\r
70702af1 456#define INIT_NAMBUF(fs)\r
289f6a14
L
457#define FREE_NAMBUF()\r
458#define LEAVE_MKFS(res) return res\r
459\r
460#else /* LFN configurations */\r
461#if FF_MAX_LFN < 12 || FF_MAX_LFN > 255\r
462#error Wrong setting of FF_MAX_LFN\r
463#endif\r
464#if FF_LFN_BUF < FF_SFN_BUF || FF_SFN_BUF < 12\r
465#error Wrong setting of FF_LFN_BUF or FF_SFN_BUF\r
7b78a5a2 466#endif\r
289f6a14
L
467#if FF_LFN_UNICODE < 0 || FF_LFN_UNICODE > 3\r
468#error Wrong setting of FF_LFN_UNICODE\r
469#endif\r
470static const BYTE LfnOfs[] = {1,3,5,7,9,14,16,18,20,22,24,28,30}; /* FAT: Offset of LFN characters in the directory entry */\r
471#define MAXDIRB(nc) ((nc + 44U) / 15 * SZDIRE) /* exFAT: Size of directory entry block scratchpad buffer needed for the name length */\r
70702af1 472\r
289f6a14
L
473#if FF_USE_LFN == 1 /* LFN enabled with static working buffer */\r
474#if FF_FS_EXFAT\r
475static BYTE DirBuf[MAXDIRB(FF_MAX_LFN)]; /* Directory entry block scratchpad buffer */\r
70702af1 476#endif\r
289f6a14
L
477static WCHAR LfnBuf[FF_MAX_LFN + 1]; /* LFN working buffer */\r
478#define DEF_NAMBUF\r
70702af1 479#define INIT_NAMBUF(fs)\r
289f6a14
L
480#define FREE_NAMBUF()\r
481#define LEAVE_MKFS(res) return res\r
70702af1 482\r
289f6a14
L
483#elif FF_USE_LFN == 2 /* LFN enabled with dynamic working buffer on the stack */\r
484#if FF_FS_EXFAT\r
485#define DEF_NAMBUF WCHAR lbuf[FF_MAX_LFN+1]; BYTE dbuf[MAXDIRB(FF_MAX_LFN)]; /* LFN working buffer and directory entry block scratchpad buffer */\r
70702af1 486#define INIT_NAMBUF(fs) { (fs)->lfnbuf = lbuf; (fs)->dirbuf = dbuf; }\r
289f6a14 487#define FREE_NAMBUF()\r
70702af1 488#else\r
289f6a14 489#define DEF_NAMBUF WCHAR lbuf[FF_MAX_LFN+1]; /* LFN working buffer */\r
70702af1 490#define INIT_NAMBUF(fs) { (fs)->lfnbuf = lbuf; }\r
289f6a14 491#define FREE_NAMBUF()\r
70702af1 492#endif\r
289f6a14 493#define LEAVE_MKFS(res) return res\r
70702af1 494\r
289f6a14
L
495#elif FF_USE_LFN == 3 /* LFN enabled with dynamic working buffer on the heap */\r
496#if FF_FS_EXFAT\r
497#define DEF_NAMBUF WCHAR *lfn; /* Pointer to LFN working buffer and directory entry block scratchpad buffer */\r
498#define INIT_NAMBUF(fs) { lfn = ff_memalloc((FF_MAX_LFN+1)*2 + MAXDIRB(FF_MAX_LFN)); if (!lfn) LEAVE_FF(fs, FR_NOT_ENOUGH_CORE); (fs)->lfnbuf = lfn; (fs)->dirbuf = (BYTE*)(lfn+FF_MAX_LFN+1); }\r
499#define FREE_NAMBUF() ff_memfree(lfn)\r
70702af1 500#else\r
289f6a14
L
501#define DEF_NAMBUF WCHAR *lfn; /* Pointer to LFN working buffer */\r
502#define INIT_NAMBUF(fs) { lfn = ff_memalloc((FF_MAX_LFN+1)*2); if (!lfn) LEAVE_FF(fs, FR_NOT_ENOUGH_CORE); (fs)->lfnbuf = lfn; }\r
503#define FREE_NAMBUF() ff_memfree(lfn)\r
70702af1 504#endif\r
289f6a14
L
505#define LEAVE_MKFS(res) { if (!work) ff_memfree(buf); return res; }\r
506#define MAX_MALLOC 0x8000 /* Must be >=FF_MAX_SS */\r
70702af1 507\r
53668523 508#else\r
289f6a14 509#error Wrong setting of FF_USE_LFN\r
53668523 510\r
289f6a14
L
511#endif /* FF_USE_LFN == 1 */\r
512#endif /* FF_USE_LFN == 0 */\r
513\r
514\r
515\r
516/*--------------------------------*/\r
517/* Code conversion tables */\r
518/*--------------------------------*/\r
519\r
520#if FF_CODE_PAGE == 0 /* Run-time code page configuration */\r
521#define CODEPAGE CodePage\r
522static WORD CodePage; /* Current code page */\r
523static const BYTE *ExCvt, *DbcTbl; /* Pointer to current SBCS up-case table and DBCS code range table below */\r
524static const BYTE Ct437[] = TBL_CT437;\r
525static const BYTE Ct720[] = TBL_CT720;\r
526static const BYTE Ct737[] = TBL_CT737;\r
527static const BYTE Ct771[] = TBL_CT771;\r
528static const BYTE Ct775[] = TBL_CT775;\r
529static const BYTE Ct850[] = TBL_CT850;\r
530static const BYTE Ct852[] = TBL_CT852;\r
531static const BYTE Ct855[] = TBL_CT855;\r
532static const BYTE Ct857[] = TBL_CT857;\r
533static const BYTE Ct860[] = TBL_CT860;\r
534static const BYTE Ct861[] = TBL_CT861;\r
535static const BYTE Ct862[] = TBL_CT862;\r
536static const BYTE Ct863[] = TBL_CT863;\r
537static const BYTE Ct864[] = TBL_CT864;\r
538static const BYTE Ct865[] = TBL_CT865;\r
539static const BYTE Ct866[] = TBL_CT866;\r
540static const BYTE Ct869[] = TBL_CT869;\r
541static const BYTE Dc932[] = TBL_DC932;\r
542static const BYTE Dc936[] = TBL_DC936;\r
543static const BYTE Dc949[] = TBL_DC949;\r
544static const BYTE Dc950[] = TBL_DC950;\r
545\r
546#elif FF_CODE_PAGE < 900 /* Static code page configuration (SBCS) */\r
547#define CODEPAGE FF_CODE_PAGE\r
548static const BYTE ExCvt[] = MKCVTBL(TBL_CT, FF_CODE_PAGE);\r
53668523 549\r
289f6a14
L
550#else /* Static code page configuration (DBCS) */\r
551#define CODEPAGE FF_CODE_PAGE\r
552static const BYTE DbcTbl[] = MKCVTBL(TBL_DC, FF_CODE_PAGE);\r
53668523 553\r
289f6a14 554#endif\r
53668523
L
555\r
556\r
557\r
558\r
559/*--------------------------------------------------------------------------\r
560\r
561 Module Private Functions\r
562\r
563---------------------------------------------------------------------------*/\r
564\r
565\r
70702af1
L
566/*-----------------------------------------------------------------------*/\r
567/* Load/Store multi-byte word in the FAT structure */\r
568/*-----------------------------------------------------------------------*/\r
569\r
289f6a14 570static WORD ld_word (const BYTE* ptr) /* Load a 2-byte little-endian word */\r
70702af1
L
571{\r
572 WORD rv;\r
573\r
574 rv = ptr[1];\r
575 rv = rv << 8 | ptr[0];\r
576 return rv;\r
577}\r
578\r
289f6a14 579static DWORD ld_dword (const BYTE* ptr) /* Load a 4-byte little-endian word */\r
70702af1
L
580{\r
581 DWORD rv;\r
582\r
583 rv = ptr[3];\r
584 rv = rv << 8 | ptr[2];\r
585 rv = rv << 8 | ptr[1];\r
586 rv = rv << 8 | ptr[0];\r
587 return rv;\r
588}\r
589\r
289f6a14
L
590#if FF_FS_EXFAT\r
591static QWORD ld_qword (const BYTE* ptr) /* Load an 8-byte little-endian word */\r
70702af1
L
592{\r
593 QWORD rv;\r
594\r
595 rv = ptr[7];\r
596 rv = rv << 8 | ptr[6];\r
597 rv = rv << 8 | ptr[5];\r
598 rv = rv << 8 | ptr[4];\r
599 rv = rv << 8 | ptr[3];\r
600 rv = rv << 8 | ptr[2];\r
601 rv = rv << 8 | ptr[1];\r
602 rv = rv << 8 | ptr[0];\r
603 return rv;\r
604}\r
605#endif\r
606\r
289f6a14
L
607#if !FF_FS_READONLY\r
608static void st_word (BYTE* ptr, WORD val) /* Store a 2-byte word in little-endian */\r
70702af1
L
609{\r
610 *ptr++ = (BYTE)val; val >>= 8;\r
611 *ptr++ = (BYTE)val;\r
612}\r
613\r
289f6a14 614static void st_dword (BYTE* ptr, DWORD val) /* Store a 4-byte word in little-endian */\r
70702af1
L
615{\r
616 *ptr++ = (BYTE)val; val >>= 8;\r
617 *ptr++ = (BYTE)val; val >>= 8;\r
618 *ptr++ = (BYTE)val; val >>= 8;\r
619 *ptr++ = (BYTE)val;\r
620}\r
621\r
289f6a14
L
622#if FF_FS_EXFAT\r
623static void st_qword (BYTE* ptr, QWORD val) /* Store an 8-byte word in little-endian */\r
70702af1
L
624{\r
625 *ptr++ = (BYTE)val; val >>= 8;\r
626 *ptr++ = (BYTE)val; val >>= 8;\r
627 *ptr++ = (BYTE)val; val >>= 8;\r
628 *ptr++ = (BYTE)val; val >>= 8;\r
629 *ptr++ = (BYTE)val; val >>= 8;\r
630 *ptr++ = (BYTE)val; val >>= 8;\r
631 *ptr++ = (BYTE)val; val >>= 8;\r
632 *ptr++ = (BYTE)val;\r
633}\r
634#endif\r
289f6a14 635#endif /* !FF_FS_READONLY */\r
70702af1
L
636\r
637\r
638\r
53668523
L
639/*-----------------------------------------------------------------------*/\r
640/* String functions */\r
641/*-----------------------------------------------------------------------*/\r
642\r
643/* Copy memory to memory */\r
289f6a14
L
644static void mem_cpy (void* dst, const void* src, UINT cnt)\r
645{\r
53668523
L
646 BYTE *d = (BYTE*)dst;\r
647 const BYTE *s = (const BYTE*)src;\r
648\r
289f6a14
L
649 if (cnt != 0) {\r
650 do {\r
651 *d++ = *s++;\r
652 } while (--cnt);\r
53668523 653 }\r
53668523
L
654}\r
655\r
289f6a14 656\r
70702af1 657/* Fill memory block */\r
289f6a14
L
658static void mem_set (void* dst, int val, UINT cnt)\r
659{\r
53668523
L
660 BYTE *d = (BYTE*)dst;\r
661\r
289f6a14
L
662 do {\r
663 *d++ = (BYTE)val;\r
664 } while (--cnt);\r
53668523
L
665}\r
666\r
289f6a14 667\r
70702af1 668/* Compare memory block */\r
289f6a14
L
669static int mem_cmp (const void* dst, const void* src, UINT cnt) /* ZR:same, NZ:different */\r
670{\r
53668523
L
671 const BYTE *d = (const BYTE *)dst, *s = (const BYTE *)src;\r
672 int r = 0;\r
673\r
70702af1
L
674 do {\r
675 r = *d++ - *s++;\r
676 } while (--cnt && r == 0);\r
677\r
53668523
L
678 return r;\r
679}\r
680\r
289f6a14 681\r
53668523 682/* Check if chr is contained in the string */\r
289f6a14
L
683static int chk_chr (const char* str, int chr) /* NZ:contained, ZR:not contained */\r
684{\r
53668523
L
685 while (*str && *str != chr) str++;\r
686 return *str;\r
687}\r
688\r
689\r
289f6a14
L
690/* Test if the character is DBC 1st byte */\r
691static int dbc_1st (BYTE c)\r
692{\r
693#if FF_CODE_PAGE == 0 /* Variable code page */\r
694 if (DbcTbl && c >= DbcTbl[0]) {\r
695 if (c <= DbcTbl[1]) return 1; /* 1st byte range 1 */\r
696 if (c >= DbcTbl[2] && c <= DbcTbl[3]) return 1; /* 1st byte range 2 */\r
697 }\r
698#elif FF_CODE_PAGE >= 900 /* DBCS fixed code page */\r
699 if (c >= DbcTbl[0]) {\r
700 if (c <= DbcTbl[1]) return 1;\r
701 if (c >= DbcTbl[2] && c <= DbcTbl[3]) return 1;\r
702 }\r
703#else /* SBCS fixed code page */\r
704 if (c != 0) return 0; /* Always false */\r
705#endif\r
706 return 0;\r
707}\r
53668523
L
708\r
709\r
289f6a14
L
710/* Test if the character is DBC 2nd byte */\r
711static int dbc_2nd (BYTE c)\r
712{\r
713#if FF_CODE_PAGE == 0 /* Variable code page */\r
714 if (DbcTbl && c >= DbcTbl[4]) {\r
715 if (c <= DbcTbl[5]) return 1; /* 2nd byte range 1 */\r
716 if (c >= DbcTbl[6] && c <= DbcTbl[7]) return 1; /* 2nd byte range 2 */\r
717 if (c >= DbcTbl[8] && c <= DbcTbl[9]) return 1; /* 2nd byte range 3 */\r
718 }\r
719#elif FF_CODE_PAGE >= 900 /* DBCS fixed code page */\r
720 if (c >= DbcTbl[4]) {\r
721 if (c <= DbcTbl[5]) return 1;\r
722 if (c >= DbcTbl[6] && c <= DbcTbl[7]) return 1;\r
723 if (c >= DbcTbl[8] && c <= DbcTbl[9]) return 1;\r
724 }\r
725#else /* SBCS fixed code page */\r
726 if (c != 0) return 0; /* Always false */\r
727#endif\r
728 return 0;\r
729}\r
730\r
731\r
732#if FF_USE_LFN\r
733\r
734/* Get a character from TCHAR string in defined API encodeing */\r
735static DWORD tchar2uni ( /* Returns character in UTF-16 encoding (>=0x10000 on double encoding unit, 0xFFFFFFFF on decode error) */\r
736 const TCHAR** str /* Pointer to pointer to TCHAR string in configured encoding */\r
737)\r
738{\r
739 DWORD uc;\r
740 const TCHAR *p = *str;\r
741\r
742#if FF_LFN_UNICODE == 1 /* UTF-16 input */\r
743 WCHAR wc;\r
744\r
745 uc = *p++; /* Get a unit */\r
746 if (IsSurrogate(uc)) { /* Surrogate? */\r
747 wc = *p++; /* Get low surrogate */\r
748 if (!IsSurrogateH(uc) || !IsSurrogateL(wc)) return 0xFFFFFFFF; /* Wrong surrogate? */\r
749 uc = uc << 16 | wc;\r
750 }\r
751\r
752#elif FF_LFN_UNICODE == 2 /* UTF-8 input */\r
753 BYTE b;\r
754 int nf;\r
755\r
756 uc = (BYTE)*p++; /* Get a unit */\r
757 if (uc & 0x80) { /* Multiple byte code? */\r
758 if ((uc & 0xE0) == 0xC0) { /* 2-byte sequence? */\r
759 uc &= 0x1F; nf = 1;\r
760 } else {\r
761 if ((uc & 0xF0) == 0xE0) { /* 3-byte sequence? */\r
762 uc &= 0x0F; nf = 2;\r
763 } else {\r
764 if ((uc & 0xF8) == 0xF0) { /* 4-byte sequence? */\r
765 uc &= 0x07; nf = 3;\r
766 } else { /* Wrong sequence */\r
767 return 0xFFFFFFFF;\r
768 }\r
769 }\r
770 }\r
771 do { /* Get trailing bytes */\r
772 b = (BYTE)*p++;\r
773 if ((b & 0xC0) != 0x80) return 0xFFFFFFFF; /* Wrong sequence? */\r
774 uc = uc << 6 | (b & 0x3F);\r
775 } while (--nf != 0);\r
776 if (uc < 0x80 || IsSurrogate(uc) || uc >= 0x110000) return 0xFFFFFFFF; /* Wrong code? */\r
777 if (uc >= 0x010000) uc = 0xD800DC00 | ((uc - 0x10000) << 6 & 0x3FF0000) | (uc & 0x3FF); /* Make a surrogate pair if needed */\r
778 }\r
779\r
780#elif FF_LFN_UNICODE == 3 /* UTF-32 input */\r
781 uc = (TCHAR)*p++; /* Get a unit */\r
782 if (uc >= 0x110000) return 0xFFFFFFFF; /* Wrong code? */\r
783 if (uc >= 0x010000) uc = 0xD800DC00 | ((uc - 0x10000) << 6 & 0x3FF0000) | (uc & 0x3FF); /* Make a surrogate pair if needed */\r
784\r
785#else /* ANSI/OEM input */\r
786 BYTE b;\r
787 WCHAR wc;\r
788\r
789 wc = (BYTE)*p++; /* Get a byte */\r
790 if (dbc_1st((BYTE)wc)) { /* Is it a DBC 1st byte? */\r
791 b = (BYTE)*p++; /* Get 2nd byte */\r
792 if (!dbc_2nd(b)) return 0xFFFFFFFF; /* Invalid code? */\r
793 wc = (wc << 8) + b; /* Make a DBC */\r
794 }\r
795 if (wc != 0) {\r
796 wc = ff_oem2uni(wc, CODEPAGE); /* ANSI/OEM ==> Unicode */\r
797 if (wc == 0) return 0xFFFFFFFF; /* Invalid code? */\r
798 }\r
799 uc = wc;\r
800\r
801#endif\r
802 *str = p; /* Next read pointer */\r
803 return uc;\r
804}\r
805\r
806\r
807/* Output a TCHAR string in defined API encoding */\r
808static BYTE put_utf ( /* Returns number of encoding units written (0:buffer overflow or wrong encoding) */\r
809 DWORD chr, /* UTF-16 encoded character (Double encoding unit char if >=0x10000) */\r
810 TCHAR* buf, /* Output buffer */\r
811 UINT szb /* Size of the buffer */\r
812)\r
813{\r
814#if FF_LFN_UNICODE == 1 /* UTF-16 output */\r
815 WCHAR hs, wc;\r
816\r
817 hs = (WCHAR)(chr >> 16);\r
818 wc = (WCHAR)chr;\r
819 if (hs == 0) { /* Single encoding unit? */\r
820 if (szb < 1 || IsSurrogate(wc)) return 0; /* Buffer overflow or wrong code? */\r
821 *buf = wc;\r
822 return 1;\r
823 }\r
824 if (szb < 2 || !IsSurrogateH(hs) || !IsSurrogateL(wc)) return 0; /* Buffer overflow or wrong surrogate? */\r
825 *buf++ = hs;\r
826 *buf++ = wc;\r
827 return 2;\r
828\r
829#elif FF_LFN_UNICODE == 2 /* UTF-8 output */\r
830 DWORD hc;\r
831\r
832 if (chr < 0x80) { /* Single byte code? */\r
833 if (szb < 1) return 0; /* Buffer overflow? */\r
834 *buf = (TCHAR)chr;\r
835 return 1;\r
836 }\r
837 if (chr < 0x800) { /* 2-byte sequence? */\r
838 if (szb < 2) return 0; /* Buffer overflow? */\r
839 *buf++ = (TCHAR)(0xC0 | (chr >> 6 & 0x1F));\r
840 *buf++ = (TCHAR)(0x80 | (chr >> 0 & 0x3F));\r
841 return 2;\r
842 }\r
843 if (chr < 0x10000) { /* 3-byte sequence? */\r
844 if (szb < 3 || IsSurrogate(chr)) return 0; /* Buffer overflow or wrong code? */\r
845 *buf++ = (TCHAR)(0xE0 | (chr >> 12 & 0x0F));\r
846 *buf++ = (TCHAR)(0x80 | (chr >> 6 & 0x3F));\r
847 *buf++ = (TCHAR)(0x80 | (chr >> 0 & 0x3F));\r
848 return 3;\r
849 }\r
850 /* 4-byte sequence */\r
851 if (szb < 4) return 0; /* Buffer overflow? */\r
852 hc = ((chr & 0xFFFF0000) - 0xD8000000) >> 6; /* Get high 10 bits */\r
853 chr = (chr & 0xFFFF) - 0xDC00; /* Get low 10 bits */\r
854 if (hc >= 0x100000 || chr >= 0x400) return 0; /* Wrong surrogate? */\r
855 chr = (hc | chr) + 0x10000;\r
856 *buf++ = (TCHAR)(0xF0 | (chr >> 18 & 0x07));\r
857 *buf++ = (TCHAR)(0x80 | (chr >> 12 & 0x3F));\r
858 *buf++ = (TCHAR)(0x80 | (chr >> 6 & 0x3F));\r
859 *buf++ = (TCHAR)(0x80 | (chr >> 0 & 0x3F));\r
860 return 4;\r
861\r
862#elif FF_LFN_UNICODE == 3 /* UTF-32 output */\r
863 DWORD hc;\r
864\r
865 if (szb < 1) return 0; /* Buffer overflow? */\r
866 if (chr >= 0x10000) { /* Out of BMP? */\r
867 hc = ((chr & 0xFFFF0000) - 0xD8000000) >> 6; /* Get high 10 bits */\r
868 chr = (chr & 0xFFFF) - 0xDC00; /* Get low 10 bits */\r
869 if (hc >= 0x100000 || chr >= 0x400) return 0; /* Wrong surrogate? */\r
870 chr = (hc | chr) + 0x10000;\r
871 }\r
872 *buf++ = (TCHAR)chr;\r
873 return 1;\r
874\r
875#else /* ANSI/OEM output */\r
876 WCHAR wc;\r
877\r
878 wc = ff_uni2oem(chr, CODEPAGE);\r
879 if (wc >= 0x100) { /* Is this a DBC? */\r
880 if (szb < 2) return 0;\r
881 *buf++ = (char)(wc >> 8); /* Store DBC 1st byte */\r
882 *buf++ = (TCHAR)wc; /* Store DBC 2nd byte */\r
883 return 2;\r
884 }\r
885 if (wc == 0 || szb < 1) return 0; /* Invalid char or buffer overflow? */\r
886 *buf++ = (TCHAR)wc; /* Store the character */\r
887 return 1;\r
888#endif\r
889}\r
890#endif /* FF_USE_LFN */\r
891\r
892\r
893#if FF_FS_REENTRANT\r
53668523
L
894/*-----------------------------------------------------------------------*/\r
895/* Request/Release grant to access the volume */\r
896/*-----------------------------------------------------------------------*/\r
289f6a14
L
897static int lock_fs ( /* 1:Ok, 0:timeout */\r
898 FATFS* fs /* Filesystem object */\r
53668523
L
899)\r
900{\r
901 return ff_req_grant(fs->sobj);\r
902}\r
903\r
904\r
289f6a14
L
905static void unlock_fs (\r
906 FATFS* fs, /* Filesystem object */\r
53668523
L
907 FRESULT res /* Result code to be returned */\r
908)\r
909{\r
70702af1 910 if (fs && res != FR_NOT_ENABLED && res != FR_INVALID_DRIVE && res != FR_TIMEOUT) {\r
53668523
L
911 ff_rel_grant(fs->sobj);\r
912 }\r
913}\r
53668523 914\r
70702af1 915#endif\r
53668523
L
916\r
917\r
918\r
289f6a14 919#if FF_FS_LOCK != 0\r
53668523
L
920/*-----------------------------------------------------------------------*/\r
921/* File lock control functions */\r
922/*-----------------------------------------------------------------------*/\r
53668523 923\r
289f6a14 924static FRESULT chk_lock ( /* Check if the file can be accessed */\r
53668523 925 DIR* dp, /* Directory object pointing the file to be checked */\r
289f6a14 926 int acc /* Desired access type (0:Read mode open, 1:Write mode open, 2:Delete or rename) */\r
53668523
L
927)\r
928{\r
929 UINT i, be;\r
930\r
289f6a14
L
931 /* Search open object table for the object */\r
932 be = 0;\r
933 for (i = 0; i < FF_FS_LOCK; i++) {\r
53668523 934 if (Files[i].fs) { /* Existing entry */\r
289f6a14 935 if (Files[i].fs == dp->obj.fs && /* Check if the object matches with an open object */\r
70702af1
L
936 Files[i].clu == dp->obj.sclust &&\r
937 Files[i].ofs == dp->dptr) break;\r
53668523
L
938 } else { /* Blank entry */\r
939 be = 1;\r
940 }\r
941 }\r
289f6a14
L
942 if (i == FF_FS_LOCK) { /* The object has not been opened */\r
943 return (!be && acc != 2) ? FR_TOO_MANY_OPEN_FILES : FR_OK; /* Is there a blank entry for new object? */\r
70702af1 944 }\r
53668523 945\r
289f6a14
L
946 /* The object was opened. Reject any open against writing file and all write mode open */\r
947 return (acc != 0 || Files[i].ctr == 0x100) ? FR_LOCKED : FR_OK;\r
53668523
L
948}\r
949\r
950\r
289f6a14 951static int enq_lock (void) /* Check if an entry is available for a new object */\r
53668523
L
952{\r
953 UINT i;\r
954\r
289f6a14
L
955 for (i = 0; i < FF_FS_LOCK && Files[i].fs; i++) ;\r
956 return (i == FF_FS_LOCK) ? 0 : 1;\r
53668523
L
957}\r
958\r
959\r
289f6a14 960static UINT inc_lock ( /* Increment object open counter and returns its index (0:Internal error) */\r
53668523
L
961 DIR* dp, /* Directory object pointing the file to register or increment */\r
962 int acc /* Desired access (0:Read, 1:Write, 2:Delete/Rename) */\r
963)\r
964{\r
965 UINT i;\r
966\r
967\r
289f6a14 968 for (i = 0; i < FF_FS_LOCK; i++) { /* Find the object */\r
70702af1
L
969 if (Files[i].fs == dp->obj.fs &&\r
970 Files[i].clu == dp->obj.sclust &&\r
971 Files[i].ofs == dp->dptr) break;\r
53668523
L
972 }\r
973\r
289f6a14
L
974 if (i == FF_FS_LOCK) { /* Not opened. Register it as new. */\r
975 for (i = 0; i < FF_FS_LOCK && Files[i].fs; i++) ;\r
976 if (i == FF_FS_LOCK) return 0; /* No free entry to register (int err) */\r
70702af1
L
977 Files[i].fs = dp->obj.fs;\r
978 Files[i].clu = dp->obj.sclust;\r
979 Files[i].ofs = dp->dptr;\r
53668523
L
980 Files[i].ctr = 0;\r
981 }\r
982\r
289f6a14 983 if (acc >= 1 && Files[i].ctr) return 0; /* Access violation (int err) */\r
53668523
L
984\r
985 Files[i].ctr = acc ? 0x100 : Files[i].ctr + 1; /* Set semaphore value */\r
986\r
289f6a14 987 return i + 1; /* Index number origin from 1 */\r
53668523
L
988}\r
989\r
990\r
289f6a14 991static FRESULT dec_lock ( /* Decrement object open counter */\r
53668523
L
992 UINT i /* Semaphore index (1..) */\r
993)\r
994{\r
995 WORD n;\r
996 FRESULT res;\r
997\r
998\r
289f6a14 999 if (--i < FF_FS_LOCK) { /* Index number origin from 0 */\r
53668523
L
1000 n = Files[i].ctr;\r
1001 if (n == 0x100) n = 0; /* If write mode open, delete the entry */\r
70702af1 1002 if (n > 0) n--; /* Decrement read mode open count */\r
53668523 1003 Files[i].ctr = n;\r
70702af1 1004 if (n == 0) Files[i].fs = 0; /* Delete the entry if open count gets zero */\r
53668523
L
1005 res = FR_OK;\r
1006 } else {\r
1007 res = FR_INT_ERR; /* Invalid index nunber */\r
1008 }\r
1009 return res;\r
1010}\r
1011\r
1012\r
289f6a14 1013static void clear_lock ( /* Clear lock entries of the volume */\r
53668523
L
1014 FATFS *fs\r
1015)\r
1016{\r
1017 UINT i;\r
1018\r
289f6a14 1019 for (i = 0; i < FF_FS_LOCK; i++) {\r
53668523
L
1020 if (Files[i].fs == fs) Files[i].fs = 0;\r
1021 }\r
1022}\r
53668523 1023\r
289f6a14 1024#endif /* FF_FS_LOCK != 0 */\r
53668523
L
1025\r
1026\r
1027\r
1028/*-----------------------------------------------------------------------*/\r
289f6a14 1029/* Move/Flush disk access window in the filesystem object */\r
53668523 1030/*-----------------------------------------------------------------------*/\r
289f6a14
L
1031#if !FF_FS_READONLY\r
1032static FRESULT sync_window ( /* Returns FR_OK or FR_DISK_ERR */\r
1033 FATFS* fs /* Filesystem object */\r
53668523
L
1034)\r
1035{\r
7b78a5a2 1036 FRESULT res = FR_OK;\r
53668523
L
1037\r
1038\r
289f6a14
L
1039 if (fs->wflag) { /* Is the disk access window dirty */\r
1040 if (disk_write(fs->pdrv, fs->win, fs->winsect, 1) == RES_OK) { /* Write back the window */\r
1041 fs->wflag = 0; /* Clear window dirty flag */\r
1042 if (fs->winsect - fs->fatbase < fs->fsize) { /* Is it in the 1st FAT? */\r
1043 if (fs->n_fats == 2) disk_write(fs->pdrv, fs->win, fs->winsect + fs->fsize, 1); /* Reflect it to 2nd FAT if needed */\r
53668523 1044 }\r
289f6a14
L
1045 } else {\r
1046 res = FR_DISK_ERR;\r
53668523
L
1047 }\r
1048 }\r
7b78a5a2 1049 return res;\r
53668523
L
1050}\r
1051#endif\r
1052\r
1053\r
289f6a14
L
1054static FRESULT move_window ( /* Returns FR_OK or FR_DISK_ERR */\r
1055 FATFS* fs, /* Filesystem object */\r
70702af1 1056 DWORD sector /* Sector number to make appearance in the fs->win[] */\r
53668523
L
1057)\r
1058{\r
7b78a5a2
L
1059 FRESULT res = FR_OK;\r
1060\r
1061\r
1062 if (sector != fs->winsect) { /* Window offset changed? */\r
289f6a14 1063#if !FF_FS_READONLY\r
7b78a5a2 1064 res = sync_window(fs); /* Write-back changes */\r
53668523 1065#endif\r
7b78a5a2 1066 if (res == FR_OK) { /* Fill sector window with new data */\r
289f6a14
L
1067 if (disk_read(fs->pdrv, fs->win, sector, 1) != RES_OK) {\r
1068 sector = 0xFFFFFFFF; /* Invalidate window if read data is not valid */\r
7b78a5a2
L
1069 res = FR_DISK_ERR;\r
1070 }\r
1071 fs->winsect = sector;\r
1072 }\r
53668523 1073 }\r
7b78a5a2 1074 return res;\r
53668523
L
1075}\r
1076\r
1077\r
1078\r
1079\r
289f6a14 1080#if !FF_FS_READONLY\r
53668523 1081/*-----------------------------------------------------------------------*/\r
289f6a14 1082/* Synchronize filesystem and data on the storage */\r
53668523 1083/*-----------------------------------------------------------------------*/\r
70702af1 1084\r
289f6a14
L
1085static FRESULT sync_fs ( /* Returns FR_OK or FR_DISK_ERR */\r
1086 FATFS* fs /* Filesystem object */\r
53668523
L
1087)\r
1088{\r
1089 FRESULT res;\r
1090\r
1091\r
1092 res = sync_window(fs);\r
1093 if (res == FR_OK) {\r
289f6a14 1094 if (fs->fs_type == FS_FAT32 && fs->fsi_flag == 1) { /* FAT32: Update FSInfo sector if needed */\r
70702af1 1095 /* Create FSInfo structure */\r
53668523 1096 mem_set(fs->win, 0, SS(fs));\r
70702af1
L
1097 st_word(fs->win + BS_55AA, 0xAA55);\r
1098 st_dword(fs->win + FSI_LeadSig, 0x41615252);\r
1099 st_dword(fs->win + FSI_StrucSig, 0x61417272);\r
1100 st_dword(fs->win + FSI_Free_Count, fs->free_clst);\r
1101 st_dword(fs->win + FSI_Nxt_Free, fs->last_clst);\r
1102 /* Write it into the FSInfo sector */\r
53668523 1103 fs->winsect = fs->volbase + 1;\r
289f6a14 1104 disk_write(fs->pdrv, fs->win, fs->winsect, 1);\r
53668523
L
1105 fs->fsi_flag = 0;\r
1106 }\r
289f6a14
L
1107 /* Make sure that no pending write process in the lower layer */\r
1108 if (disk_ioctl(fs->pdrv, CTRL_SYNC, 0) != RES_OK) res = FR_DISK_ERR;\r
53668523
L
1109 }\r
1110\r
1111 return res;\r
1112}\r
53668523 1113\r
70702af1 1114#endif\r
53668523
L
1115\r
1116\r
1117\r
1118/*-----------------------------------------------------------------------*/\r
289f6a14 1119/* Get physical sector number from cluster number */\r
53668523 1120/*-----------------------------------------------------------------------*/\r
53668523 1121\r
289f6a14
L
1122static DWORD clst2sect ( /* !=0:Sector number, 0:Failed (invalid cluster#) */\r
1123 FATFS* fs, /* Filesystem object */\r
53668523
L
1124 DWORD clst /* Cluster# to be converted */\r
1125)\r
1126{\r
289f6a14
L
1127 clst -= 2; /* Cluster number is origin from 2 */\r
1128 if (clst >= fs->n_fatent - 2) return 0; /* Is it invalid cluster number? */\r
1129 return fs->database + fs->csize * clst; /* Start sector number of the cluster */\r
53668523
L
1130}\r
1131\r
1132\r
1133\r
1134\r
1135/*-----------------------------------------------------------------------*/\r
1136/* FAT access - Read value of a FAT entry */\r
1137/*-----------------------------------------------------------------------*/\r
1138\r
289f6a14
L
1139static DWORD get_fat ( /* 0xFFFFFFFF:Disk error, 1:Internal error, 2..0x7FFFFFFF:Cluster status */\r
1140 FFOBJID* obj, /* Corresponding object */\r
1141 DWORD clst /* Cluster number to get the value */\r
53668523
L
1142)\r
1143{\r
1144 UINT wc, bc;\r
7b78a5a2 1145 DWORD val;\r
70702af1 1146 FATFS *fs = obj->fs;\r
53668523
L
1147\r
1148\r
70702af1 1149 if (clst < 2 || clst >= fs->n_fatent) { /* Check if in valid range */\r
7b78a5a2 1150 val = 1; /* Internal error */\r
53668523 1151\r
7b78a5a2
L
1152 } else {\r
1153 val = 0xFFFFFFFF; /* Default value falls on disk error */\r
53668523 1154\r
7b78a5a2
L
1155 switch (fs->fs_type) {\r
1156 case FS_FAT12 :\r
1157 bc = (UINT)clst; bc += bc / 2;\r
1158 if (move_window(fs, fs->fatbase + (bc / SS(fs))) != FR_OK) break;\r
289f6a14 1159 wc = fs->win[bc++ % SS(fs)]; /* Get 1st byte of the entry */\r
7b78a5a2 1160 if (move_window(fs, fs->fatbase + (bc / SS(fs))) != FR_OK) break;\r
289f6a14
L
1161 wc |= fs->win[bc % SS(fs)] << 8; /* Merge 2nd byte of the entry */\r
1162 val = (clst & 1) ? (wc >> 4) : (wc & 0xFFF); /* Adjust bit position */\r
7b78a5a2
L
1163 break;\r
1164\r
1165 case FS_FAT16 :\r
1166 if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 2))) != FR_OK) break;\r
289f6a14 1167 val = ld_word(fs->win + clst * 2 % SS(fs)); /* Simple WORD array */\r
7b78a5a2 1168 break;\r
53668523 1169\r
7b78a5a2
L
1170 case FS_FAT32 :\r
1171 if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 4))) != FR_OK) break;\r
289f6a14 1172 val = ld_dword(fs->win + clst * 4 % SS(fs)) & 0x0FFFFFFF; /* Simple DWORD array but mask out upper 4 bits */\r
7b78a5a2 1173 break;\r
289f6a14 1174#if FF_FS_EXFAT\r
70702af1 1175 case FS_EXFAT :\r
289f6a14 1176 if ((obj->objsize != 0 && obj->sclust != 0) || obj->stat == 0) { /* Object except root dir must have valid data length */\r
70702af1
L
1177 DWORD cofs = clst - obj->sclust; /* Offset from start cluster */\r
1178 DWORD clen = (DWORD)((obj->objsize - 1) / SS(fs)) / fs->csize; /* Number of clusters - 1 */\r
1179\r
289f6a14
L
1180 if (obj->stat == 2 && cofs <= clen) { /* Is it a contiguous chain? */\r
1181 val = (cofs == clen) ? 0x7FFFFFFF : clst + 1; /* No data on the FAT, generate the value */\r
1182 break;\r
70702af1 1183 }\r
289f6a14 1184 if (obj->stat == 3 && cofs < obj->n_cont) { /* Is it in the 1st fragment? */\r
70702af1
L
1185 val = clst + 1; /* Generate the value */\r
1186 break;\r
1187 }\r
1188 if (obj->stat != 2) { /* Get value from FAT if FAT chain is valid */\r
289f6a14
L
1189 if (obj->n_frag != 0) { /* Is it on the growing edge? */\r
1190 val = 0x7FFFFFFF; /* Generate EOC */\r
1191 } else {\r
1192 if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 4))) != FR_OK) break;\r
1193 val = ld_dword(fs->win + clst * 4 % SS(fs)) & 0x7FFFFFFF;\r
1194 }\r
70702af1
L
1195 break;\r
1196 }\r
1197 }\r
289f6a14 1198 /* go to default */\r
70702af1 1199#endif\r
7b78a5a2
L
1200 default:\r
1201 val = 1; /* Internal error */\r
1202 }\r
53668523
L
1203 }\r
1204\r
7b78a5a2 1205 return val;\r
53668523
L
1206}\r
1207\r
1208\r
1209\r
1210\r
289f6a14 1211#if !FF_FS_READONLY\r
53668523
L
1212/*-----------------------------------------------------------------------*/\r
1213/* FAT access - Change value of a FAT entry */\r
1214/*-----------------------------------------------------------------------*/\r
53668523 1215\r
289f6a14
L
1216static FRESULT put_fat ( /* FR_OK(0):succeeded, !=0:error */\r
1217 FATFS* fs, /* Corresponding filesystem object */\r
70702af1
L
1218 DWORD clst, /* FAT index number (cluster number) to be changed */\r
1219 DWORD val /* New value to be set to the entry */\r
53668523
L
1220)\r
1221{\r
1222 UINT bc;\r
1223 BYTE *p;\r
70702af1 1224 FRESULT res = FR_INT_ERR;\r
53668523
L
1225\r
1226\r
70702af1 1227 if (clst >= 2 && clst < fs->n_fatent) { /* Check if in valid range */\r
53668523 1228 switch (fs->fs_type) {\r
289f6a14
L
1229 case FS_FAT12 :\r
1230 bc = (UINT)clst; bc += bc / 2; /* bc: byte offset of the entry */\r
53668523
L
1231 res = move_window(fs, fs->fatbase + (bc / SS(fs)));\r
1232 if (res != FR_OK) break;\r
70702af1 1233 p = fs->win + bc++ % SS(fs);\r
289f6a14 1234 *p = (clst & 1) ? ((*p & 0x0F) | ((BYTE)val << 4)) : (BYTE)val; /* Put 1st byte */\r
53668523
L
1235 fs->wflag = 1;\r
1236 res = move_window(fs, fs->fatbase + (bc / SS(fs)));\r
1237 if (res != FR_OK) break;\r
70702af1 1238 p = fs->win + bc % SS(fs);\r
289f6a14 1239 *p = (clst & 1) ? (BYTE)(val >> 4) : ((*p & 0xF0) | ((BYTE)(val >> 8) & 0x0F)); /* Put 2nd byte */\r
7b78a5a2 1240 fs->wflag = 1;\r
53668523
L
1241 break;\r
1242\r
289f6a14 1243 case FS_FAT16 :\r
53668523
L
1244 res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 2)));\r
1245 if (res != FR_OK) break;\r
289f6a14 1246 st_word(fs->win + clst * 2 % SS(fs), (WORD)val); /* Simple WORD array */\r
7b78a5a2 1247 fs->wflag = 1;\r
53668523
L
1248 break;\r
1249\r
289f6a14
L
1250 case FS_FAT32 :\r
1251#if FF_FS_EXFAT\r
70702af1
L
1252 case FS_EXFAT :\r
1253#endif\r
53668523
L
1254 res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 4)));\r
1255 if (res != FR_OK) break;\r
289f6a14 1256 if (!FF_FS_EXFAT || fs->fs_type != FS_EXFAT) {\r
70702af1
L
1257 val = (val & 0x0FFFFFFF) | (ld_dword(fs->win + clst * 4 % SS(fs)) & 0xF0000000);\r
1258 }\r
1259 st_dword(fs->win + clst * 4 % SS(fs), val);\r
7b78a5a2 1260 fs->wflag = 1;\r
53668523 1261 break;\r
53668523 1262 }\r
53668523 1263 }\r
53668523
L
1264 return res;\r
1265}\r
70702af1 1266\r
289f6a14 1267#endif /* !FF_FS_READONLY */\r
53668523
L
1268\r
1269\r
1270\r
1271\r
289f6a14 1272#if FF_FS_EXFAT && !FF_FS_READONLY\r
53668523 1273/*-----------------------------------------------------------------------*/\r
70702af1 1274/* exFAT: Accessing FAT and Allocation Bitmap */\r
53668523 1275/*-----------------------------------------------------------------------*/\r
70702af1 1276\r
289f6a14
L
1277/*--------------------------------------*/\r
1278/* Find a contiguous free cluster block */\r
1279/*--------------------------------------*/\r
70702af1 1280\r
289f6a14
L
1281static DWORD find_bitmap ( /* 0:Not found, 2..:Cluster block found, 0xFFFFFFFF:Disk error */\r
1282 FATFS* fs, /* Filesystem object */\r
70702af1
L
1283 DWORD clst, /* Cluster number to scan from */\r
1284 DWORD ncl /* Number of contiguous clusters to find (1..) */\r
1285)\r
1286{\r
1287 BYTE bm, bv;\r
1288 UINT i;\r
1289 DWORD val, scl, ctr;\r
1290\r
1291\r
1292 clst -= 2; /* The first bit in the bitmap corresponds to cluster #2 */\r
1293 if (clst >= fs->n_fatent - 2) clst = 0;\r
1294 scl = val = clst; ctr = 0;\r
1295 for (;;) {\r
1296 if (move_window(fs, fs->database + val / 8 / SS(fs)) != FR_OK) return 0xFFFFFFFF; /* (assuming bitmap is located top of the cluster heap) */\r
1297 i = val / 8 % SS(fs); bm = 1 << (val % 8);\r
1298 do {\r
1299 do {\r
1300 bv = fs->win[i] & bm; bm <<= 1; /* Get bit value */\r
1301 if (++val >= fs->n_fatent - 2) { /* Next cluster (with wrap-around) */\r
289f6a14 1302 val = 0; bm = 0; i = SS(fs);\r
70702af1 1303 }\r
289f6a14
L
1304 if (bv == 0) { /* Is it a free cluster? */\r
1305 if (++ctr == ncl) return scl + 2; /* Check if run length is sufficient for required */\r
70702af1 1306 } else {\r
289f6a14 1307 scl = val; ctr = 0; /* Encountered a cluster in-use, restart to scan */\r
70702af1
L
1308 }\r
1309 if (val == clst) return 0; /* All cluster scanned? */\r
289f6a14 1310 } while (bm != 0);\r
70702af1
L
1311 bm = 1;\r
1312 } while (++i < SS(fs));\r
1313 }\r
1314}\r
1315\r
1316\r
289f6a14
L
1317/*----------------------------------------*/\r
1318/* Set/Clear a block of allocation bitmap */\r
1319/*----------------------------------------*/\r
70702af1 1320\r
289f6a14
L
1321static FRESULT change_bitmap (\r
1322 FATFS* fs, /* Filesystem object */\r
70702af1
L
1323 DWORD clst, /* Cluster number to change from */\r
1324 DWORD ncl, /* Number of clusters to be changed */\r
1325 int bv /* bit value to be set (0 or 1) */\r
1326)\r
1327{\r
1328 BYTE bm;\r
1329 UINT i;\r
1330 DWORD sect;\r
1331\r
1332\r
1333 clst -= 2; /* The first bit corresponds to cluster #2 */\r
1334 sect = fs->database + clst / 8 / SS(fs); /* Sector address (assuming bitmap is located top of the cluster heap) */\r
1335 i = clst / 8 % SS(fs); /* Byte offset in the sector */\r
1336 bm = 1 << (clst % 8); /* Bit mask in the byte */\r
1337 for (;;) {\r
1338 if (move_window(fs, sect++) != FR_OK) return FR_DISK_ERR;\r
1339 do {\r
1340 do {\r
1341 if (bv == (int)((fs->win[i] & bm) != 0)) return FR_INT_ERR; /* Is the bit expected value? */\r
1342 fs->win[i] ^= bm; /* Flip the bit */\r
1343 fs->wflag = 1;\r
1344 if (--ncl == 0) return FR_OK; /* All bits processed? */\r
1345 } while (bm <<= 1); /* Next bit */\r
1346 bm = 1;\r
1347 } while (++i < SS(fs)); /* Next byte */\r
1348 i = 0;\r
1349 }\r
1350}\r
1351\r
1352\r
1353/*---------------------------------------------*/\r
289f6a14 1354/* Fill the first fragment of the FAT chain */\r
70702af1
L
1355/*---------------------------------------------*/\r
1356\r
289f6a14
L
1357static FRESULT fill_first_frag (\r
1358 FFOBJID* obj /* Pointer to the corresponding object */\r
53668523
L
1359)\r
1360{\r
1361 FRESULT res;\r
70702af1
L
1362 DWORD cl, n;\r
1363\r
289f6a14
L
1364\r
1365 if (obj->stat == 3) { /* Has the object been changed 'fragmented' in this session? */\r
70702af1
L
1366 for (cl = obj->sclust, n = obj->n_cont; n; cl++, n--) { /* Create cluster chain on the FAT */\r
1367 res = put_fat(obj->fs, cl, cl + 1);\r
1368 if (res != FR_OK) return res;\r
1369 }\r
1370 obj->stat = 0; /* Change status 'FAT chain is valid' */\r
1371 }\r
1372 return FR_OK;\r
1373}\r
1374\r
70702af1 1375\r
289f6a14
L
1376/*---------------------------------------------*/\r
1377/* Fill the last fragment of the FAT chain */\r
1378/*---------------------------------------------*/\r
1379\r
1380static FRESULT fill_last_frag (\r
1381 FFOBJID* obj, /* Pointer to the corresponding object */\r
1382 DWORD lcl, /* Last cluster of the fragment */\r
1383 DWORD term /* Value to set the last FAT entry */\r
1384)\r
1385{\r
1386 FRESULT res;\r
1387\r
1388\r
1389 while (obj->n_frag > 0) { /* Create the chain of last fragment */\r
1390 res = put_fat(obj->fs, lcl - obj->n_frag + 1, (obj->n_frag > 1) ? lcl - obj->n_frag + 2 : term);\r
1391 if (res != FR_OK) return res;\r
1392 obj->n_frag--;\r
1393 }\r
1394 return FR_OK;\r
1395}\r
70702af1 1396\r
289f6a14 1397#endif /* FF_FS_EXFAT && !FF_FS_READONLY */\r
70702af1 1398\r
289f6a14
L
1399\r
1400\r
1401#if !FF_FS_READONLY\r
70702af1
L
1402/*-----------------------------------------------------------------------*/\r
1403/* FAT handling - Remove a cluster chain */\r
1404/*-----------------------------------------------------------------------*/\r
289f6a14
L
1405\r
1406static FRESULT remove_chain ( /* FR_OK(0):succeeded, !=0:error */\r
1407 FFOBJID* obj, /* Corresponding object */\r
70702af1 1408 DWORD clst, /* Cluster to remove a chain from */\r
289f6a14 1409 DWORD pclst /* Previous cluster of clst (0:entire chain) */\r
70702af1
L
1410)\r
1411{\r
1412 FRESULT res = FR_OK;\r
53668523 1413 DWORD nxt;\r
70702af1 1414 FATFS *fs = obj->fs;\r
289f6a14 1415#if FF_FS_EXFAT || FF_USE_TRIM\r
70702af1
L
1416 DWORD scl = clst, ecl = clst;\r
1417#endif\r
289f6a14 1418#if FF_USE_TRIM\r
70702af1 1419 DWORD rt[2];\r
53668523
L
1420#endif\r
1421\r
70702af1 1422 if (clst < 2 || clst >= fs->n_fatent) return FR_INT_ERR; /* Check if in valid range */\r
53668523 1423\r
70702af1 1424 /* Mark the previous cluster 'EOC' on the FAT if it exists */\r
289f6a14 1425 if (pclst != 0 && (!FF_FS_EXFAT || fs->fs_type != FS_EXFAT || obj->stat != 2)) {\r
70702af1
L
1426 res = put_fat(fs, pclst, 0xFFFFFFFF);\r
1427 if (res != FR_OK) return res;\r
1428 }\r
1429\r
1430 /* Remove the chain */\r
1431 do {\r
1432 nxt = get_fat(obj, clst); /* Get cluster status */\r
1433 if (nxt == 0) break; /* Empty cluster? */\r
1434 if (nxt == 1) return FR_INT_ERR; /* Internal error? */\r
1435 if (nxt == 0xFFFFFFFF) return FR_DISK_ERR; /* Disk error? */\r
289f6a14 1436 if (!FF_FS_EXFAT || fs->fs_type != FS_EXFAT) {\r
70702af1
L
1437 res = put_fat(fs, clst, 0); /* Mark the cluster 'free' on the FAT */\r
1438 if (res != FR_OK) return res;\r
1439 }\r
1440 if (fs->free_clst < fs->n_fatent - 2) { /* Update FSINFO */\r
1441 fs->free_clst++;\r
1442 fs->fsi_flag |= 1;\r
1443 }\r
289f6a14 1444#if FF_FS_EXFAT || FF_USE_TRIM\r
70702af1
L
1445 if (ecl + 1 == nxt) { /* Is next cluster contiguous? */\r
1446 ecl = nxt;\r
289f6a14
L
1447 } else { /* End of contiguous cluster block */\r
1448#if FF_FS_EXFAT\r
70702af1
L
1449 if (fs->fs_type == FS_EXFAT) {\r
1450 res = change_bitmap(fs, scl, ecl - scl + 1, 0); /* Mark the cluster block 'free' on the bitmap */\r
1451 if (res != FR_OK) return res;\r
53668523 1452 }\r
70702af1 1453#endif\r
289f6a14
L
1454#if FF_USE_TRIM\r
1455 rt[0] = clst2sect(fs, scl); /* Start of data area freed */\r
1456 rt[1] = clst2sect(fs, ecl) + fs->csize - 1; /* End of data area freed */\r
1457 disk_ioctl(fs->pdrv, CTRL_TRIM, rt); /* Inform device the data in the block is no longer needed */\r
53668523 1458#endif\r
70702af1 1459 scl = ecl = nxt;\r
53668523 1460 }\r
70702af1
L
1461#endif\r
1462 clst = nxt; /* Next cluster */\r
1463 } while (clst < fs->n_fatent); /* Repeat while not the last link */\r
53668523 1464\r
289f6a14
L
1465#if FF_FS_EXFAT\r
1466 /* Some post processes for chain status */\r
70702af1 1467 if (fs->fs_type == FS_EXFAT) {\r
289f6a14
L
1468 if (pclst == 0) { /* Has the entire chain been removed? */\r
1469 obj->stat = 0; /* Change the chain status 'initial' */\r
70702af1 1470 } else {\r
289f6a14
L
1471 if (obj->stat == 0) { /* Is it a fragmented chain from the beginning of this session? */\r
1472 clst = obj->sclust; /* Follow the chain to check if it gets contiguous */\r
1473 while (clst != pclst) {\r
1474 nxt = get_fat(obj, clst);\r
1475 if (nxt < 2) return FR_INT_ERR;\r
1476 if (nxt == 0xFFFFFFFF) return FR_DISK_ERR;\r
1477 if (nxt != clst + 1) break; /* Not contiguous? */\r
1478 clst++;\r
1479 }\r
1480 if (clst == pclst) { /* Has the chain got contiguous again? */\r
1481 obj->stat = 2; /* Change the chain status 'contiguous' */\r
1482 }\r
1483 } else {\r
1484 if (obj->stat == 3 && pclst >= obj->sclust && pclst <= obj->sclust + obj->n_cont) { /* Was the chain fragmented in this session and got contiguous again? */\r
1485 obj->stat = 2; /* Change the chain status 'contiguous' */\r
1486 }\r
70702af1
L
1487 }\r
1488 }\r
1489 }\r
53668523 1490#endif\r
70702af1
L
1491 return FR_OK;\r
1492}\r
53668523
L
1493\r
1494\r
1495\r
1496\r
1497/*-----------------------------------------------------------------------*/\r
70702af1 1498/* FAT handling - Stretch a chain or Create a new chain */\r
53668523 1499/*-----------------------------------------------------------------------*/\r
289f6a14
L
1500\r
1501static DWORD create_chain ( /* 0:No free cluster, 1:Internal error, 0xFFFFFFFF:Disk error, >=2:New cluster# */\r
1502 FFOBJID* obj, /* Corresponding object */\r
70702af1 1503 DWORD clst /* Cluster# to stretch, 0:Create a new chain */\r
53668523
L
1504)\r
1505{\r
1506 DWORD cs, ncl, scl;\r
1507 FRESULT res;\r
70702af1 1508 FATFS *fs = obj->fs;\r
53668523
L
1509\r
1510\r
70702af1 1511 if (clst == 0) { /* Create a new chain */\r
289f6a14 1512 scl = fs->last_clst; /* Suggested cluster to start to find */\r
70702af1 1513 if (scl == 0 || scl >= fs->n_fatent) scl = 1;\r
53668523 1514 }\r
289f6a14 1515 else { /* Stretch a chain */\r
70702af1 1516 cs = get_fat(obj, clst); /* Check the cluster status */\r
289f6a14
L
1517 if (cs < 2) return 1; /* Test for insanity */\r
1518 if (cs == 0xFFFFFFFF) return cs; /* Test for disk error */\r
53668523 1519 if (cs < fs->n_fatent) return cs; /* It is already followed by next cluster */\r
289f6a14 1520 scl = clst; /* Cluster to start to find */\r
53668523 1521 }\r
289f6a14 1522 if (fs->free_clst == 0) return 0; /* No free cluster */\r
53668523 1523\r
289f6a14 1524#if FF_FS_EXFAT\r
70702af1
L
1525 if (fs->fs_type == FS_EXFAT) { /* On the exFAT volume */\r
1526 ncl = find_bitmap(fs, scl, 1); /* Find a free cluster */\r
1527 if (ncl == 0 || ncl == 0xFFFFFFFF) return ncl; /* No free cluster or hard error? */\r
1528 res = change_bitmap(fs, ncl, 1, 1); /* Mark the cluster 'in use' */\r
1529 if (res == FR_INT_ERR) return 1;\r
1530 if (res == FR_DISK_ERR) return 0xFFFFFFFF;\r
1531 if (clst == 0) { /* Is it a new chain? */\r
289f6a14
L
1532 obj->stat = 2; /* Set status 'contiguous' */\r
1533 } else { /* It is a stretched chain */\r
70702af1
L
1534 if (obj->stat == 2 && ncl != scl + 1) { /* Is the chain got fragmented? */\r
1535 obj->n_cont = scl - obj->sclust; /* Set size of the contiguous part */\r
1536 obj->stat = 3; /* Change status 'just fragmented' */\r
1537 }\r
1538 }\r
289f6a14
L
1539 if (obj->stat != 2) { /* Is the file non-contiguous? */\r
1540 if (ncl == clst + 1) { /* Is the cluster next to previous one? */\r
1541 obj->n_frag = obj->n_frag ? obj->n_frag + 1 : 2; /* Increment size of last framgent */\r
1542 } else { /* New fragment */\r
1543 if (obj->n_frag == 0) obj->n_frag = 1;\r
1544 res = fill_last_frag(obj, clst, ncl); /* Fill last fragment on the FAT and link it to new one */\r
1545 if (res == FR_OK) obj->n_frag = 1;\r
1546 }\r
1547 }\r
70702af1
L
1548 } else\r
1549#endif\r
289f6a14
L
1550 { /* On the FAT/FAT32 volume */\r
1551 ncl = 0;\r
1552 if (scl == clst) { /* Stretching an existing chain? */\r
1553 ncl = scl + 1; /* Test if next cluster is free */\r
1554 if (ncl >= fs->n_fatent) ncl = 2;\r
1555 cs = get_fat(obj, ncl); /* Get next cluster status */\r
1556 if (cs == 1 || cs == 0xFFFFFFFF) return cs; /* Test for error */\r
1557 if (cs != 0) { /* Not free? */\r
1558 cs = fs->last_clst; /* Start at suggested cluster if it is valid */\r
1559 if (cs >= 2 && cs < fs->n_fatent) scl = cs;\r
1560 ncl = 0;\r
70702af1 1561 }\r
53668523 1562 }\r
289f6a14
L
1563 if (ncl == 0) { /* The new cluster cannot be contiguous and find another fragment */\r
1564 ncl = scl; /* Start cluster */\r
1565 for (;;) {\r
1566 ncl++; /* Next cluster */\r
1567 if (ncl >= fs->n_fatent) { /* Check wrap-around */\r
1568 ncl = 2;\r
1569 if (ncl > scl) return 0; /* No free cluster found? */\r
1570 }\r
1571 cs = get_fat(obj, ncl); /* Get the cluster status */\r
1572 if (cs == 0) break; /* Found a free cluster? */\r
1573 if (cs == 1 || cs == 0xFFFFFFFF) return cs; /* Test for error */\r
1574 if (ncl == scl) return 0; /* No free cluster found? */\r
1575 }\r
1576 }\r
1577 res = put_fat(fs, ncl, 0xFFFFFFFF); /* Mark the new cluster 'EOC' */\r
1578 if (res == FR_OK && clst != 0) {\r
1579 res = put_fat(fs, clst, ncl); /* Link it from the previous one if needed */\r
53668523 1580 }\r
70702af1
L
1581 }\r
1582\r
1583 if (res == FR_OK) { /* Update FSINFO if function succeeded. */\r
1584 fs->last_clst = ncl;\r
289f6a14 1585 if (fs->free_clst <= fs->n_fatent - 2) fs->free_clst--;\r
70702af1 1586 fs->fsi_flag |= 1;\r
53668523 1587 } else {\r
289f6a14 1588 ncl = (res == FR_DISK_ERR) ? 0xFFFFFFFF : 1; /* Failed. Generate error status */\r
53668523
L
1589 }\r
1590\r
70702af1 1591 return ncl; /* Return new cluster number or error status */\r
53668523 1592}\r
70702af1 1593\r
289f6a14 1594#endif /* !FF_FS_READONLY */\r
53668523
L
1595\r
1596\r
1597\r
1598\r
289f6a14 1599#if FF_USE_FASTSEEK\r
53668523
L
1600/*-----------------------------------------------------------------------*/\r
1601/* FAT handling - Convert offset into cluster with link map table */\r
1602/*-----------------------------------------------------------------------*/\r
1603\r
289f6a14 1604static DWORD clmt_clust ( /* <2:Error, >=2:Cluster number */\r
53668523 1605 FIL* fp, /* Pointer to the file object */\r
70702af1 1606 FSIZE_t ofs /* File offset to be converted to cluster# */\r
53668523
L
1607)\r
1608{\r
1609 DWORD cl, ncl, *tbl;\r
70702af1 1610 FATFS *fs = fp->obj.fs;\r
53668523
L
1611\r
1612\r
1613 tbl = fp->cltbl + 1; /* Top of CLMT */\r
70702af1 1614 cl = (DWORD)(ofs / SS(fs) / fs->csize); /* Cluster order from top of the file */\r
53668523
L
1615 for (;;) {\r
1616 ncl = *tbl++; /* Number of cluters in the fragment */\r
70702af1 1617 if (ncl == 0) return 0; /* End of table? (error) */\r
53668523
L
1618 if (cl < ncl) break; /* In this fragment? */\r
1619 cl -= ncl; tbl++; /* Next fragment */\r
1620 }\r
1621 return cl + *tbl; /* Return the cluster number */\r
1622}\r
70702af1 1623\r
289f6a14
L
1624#endif /* FF_USE_FASTSEEK */\r
1625\r
1626\r
1627\r
1628\r
1629/*-----------------------------------------------------------------------*/\r
1630/* Directory handling - Fill a cluster with zeros */\r
1631/*-----------------------------------------------------------------------*/\r
1632\r
1633#if !FF_FS_READONLY\r
1634static FRESULT dir_clear ( /* Returns FR_OK or FR_DISK_ERR */\r
1635 FATFS *fs, /* Filesystem object */\r
1636 DWORD clst /* Directory table to clear */\r
1637)\r
1638{\r
1639 DWORD sect;\r
1640 UINT n, szb;\r
1641 BYTE *ibuf;\r
1642\r
1643\r
1644 if (sync_window(fs) != FR_OK) return FR_DISK_ERR; /* Flush disk access window */\r
1645 sect = clst2sect(fs, clst); /* Top of the cluster */\r
1646 fs->winsect = sect; /* Set window to top of the cluster */\r
1647 mem_set(fs->win, 0, SS(fs)); /* Clear window buffer */\r
1648#if FF_USE_LFN == 3 /* Quick table clear by using multi-secter write */\r
1649 /* Allocate a temporary buffer */\r
1650 for (szb = ((DWORD)fs->csize * SS(fs) >= MAX_MALLOC) ? MAX_MALLOC : fs->csize * SS(fs), ibuf = 0; szb > SS(fs) && (ibuf = ff_memalloc(szb)) == 0; szb /= 2) ;\r
1651 if (szb > SS(fs)) { /* Buffer allocated? */\r
1652 mem_set(ibuf, 0, szb);\r
1653 szb /= SS(fs); /* Bytes -> Sectors */\r
1654 for (n = 0; n < fs->csize && disk_write(fs->pdrv, ibuf, sect + n, szb) == RES_OK; n += szb) ; /* Fill the cluster with 0 */\r
1655 ff_memfree(ibuf);\r
1656 } else\r
1657#endif\r
1658 {\r
1659 ibuf = fs->win; szb = 1; /* Use window buffer (many single-sector writes may take a time) */\r
1660 for (n = 0; n < fs->csize && disk_write(fs->pdrv, ibuf, sect + n, szb) == RES_OK; n += szb) ; /* Fill the cluster with 0 */\r
1661 }\r
1662 return (n == fs->csize) ? FR_OK : FR_DISK_ERR;\r
1663}\r
1664#endif /* !FF_FS_READONLY */\r
53668523
L
1665\r
1666\r
1667\r
1668\r
1669/*-----------------------------------------------------------------------*/\r
1670/* Directory handling - Set directory index */\r
1671/*-----------------------------------------------------------------------*/\r
1672\r
289f6a14 1673static FRESULT dir_sdi ( /* FR_OK(0):succeeded, !=0:error */\r
53668523 1674 DIR* dp, /* Pointer to directory object */\r
70702af1 1675 DWORD ofs /* Offset of directory table */\r
53668523
L
1676)\r
1677{\r
70702af1
L
1678 DWORD csz, clst;\r
1679 FATFS *fs = dp->obj.fs;\r
53668523
L
1680\r
1681\r
289f6a14 1682 if (ofs >= (DWORD)((FF_FS_EXFAT && fs->fs_type == FS_EXFAT) ? MAX_DIR_EX : MAX_DIR) || ofs % SZDIRE) { /* Check range of offset and alignment */\r
53668523 1683 return FR_INT_ERR;\r
70702af1
L
1684 }\r
1685 dp->dptr = ofs; /* Set current offset */\r
1686 clst = dp->obj.sclust; /* Table start cluster (0:root) */\r
1687 if (clst == 0 && fs->fs_type >= FS_FAT32) { /* Replace cluster# 0 with root cluster# */\r
1688 clst = fs->dirbase;\r
289f6a14 1689 if (FF_FS_EXFAT) dp->obj.stat = 0; /* exFAT: Root dir has an FAT chain */\r
70702af1 1690 }\r
53668523 1691\r
289f6a14
L
1692 if (clst == 0) { /* Static table (root-directory on the FAT volume) */\r
1693 if (ofs / SZDIRE >= fs->n_rootdir) return FR_INT_ERR; /* Is index out of range? */\r
70702af1
L
1694 dp->sect = fs->dirbase;\r
1695\r
289f6a14 1696 } else { /* Dynamic table (sub-directory or root-directory on the FAT32/exFAT volume) */\r
70702af1
L
1697 csz = (DWORD)fs->csize * SS(fs); /* Bytes per cluster */\r
1698 while (ofs >= csz) { /* Follow cluster chain */\r
1699 clst = get_fat(&dp->obj, clst); /* Get next cluster */\r
53668523 1700 if (clst == 0xFFFFFFFF) return FR_DISK_ERR; /* Disk error */\r
70702af1
L
1701 if (clst < 2 || clst >= fs->n_fatent) return FR_INT_ERR; /* Reached to end of table or internal error */\r
1702 ofs -= csz;\r
53668523 1703 }\r
289f6a14 1704 dp->sect = clst2sect(fs, clst);\r
53668523 1705 }\r
70702af1 1706 dp->clust = clst; /* Current cluster# */\r
289f6a14 1707 if (dp->sect == 0) return FR_INT_ERR;\r
70702af1
L
1708 dp->sect += ofs / SS(fs); /* Sector# of the directory entry */\r
1709 dp->dir = fs->win + (ofs % SS(fs)); /* Pointer to the entry in the win[] */\r
53668523
L
1710\r
1711 return FR_OK;\r
1712}\r
1713\r
1714\r
1715\r
1716\r
1717/*-----------------------------------------------------------------------*/\r
1718/* Directory handling - Move directory table index next */\r
1719/*-----------------------------------------------------------------------*/\r
1720\r
289f6a14
L
1721static FRESULT dir_next ( /* FR_OK(0):succeeded, FR_NO_FILE:End of table, FR_DENIED:Could not stretch */\r
1722 DIR* dp, /* Pointer to the directory object */\r
1723 int stretch /* 0: Do not stretch table, 1: Stretch table if needed */\r
53668523
L
1724)\r
1725{\r
70702af1
L
1726 DWORD ofs, clst;\r
1727 FATFS *fs = dp->obj.fs;\r
289f6a14 1728\r
53668523 1729\r
70702af1 1730 ofs = dp->dptr + SZDIRE; /* Next entry */\r
dbd0d34e
L
1731 if (ofs >= (DWORD)((FF_FS_EXFAT && fs->fs_type == FS_EXFAT) ? MAX_DIR_EX : MAX_DIR)) dp->sect = 0; /* Disable it if the offset reached the max value */\r
1732 if (dp->sect == 0) return FR_NO_FILE; /* Report EOT if it has been disabled */\r
53668523 1733\r
70702af1
L
1734 if (ofs % SS(fs) == 0) { /* Sector changed? */\r
1735 dp->sect++; /* Next sector */\r
53668523 1736\r
289f6a14 1737 if (dp->clust == 0) { /* Static table */\r
70702af1
L
1738 if (ofs / SZDIRE >= fs->n_rootdir) { /* Report EOT if it reached end of static table */\r
1739 dp->sect = 0; return FR_NO_FILE;\r
1740 }\r
53668523
L
1741 }\r
1742 else { /* Dynamic table */\r
289f6a14
L
1743 if ((ofs / SS(fs) & (fs->csize - 1)) == 0) { /* Cluster changed? */\r
1744 clst = get_fat(&dp->obj, dp->clust); /* Get next cluster */\r
1745 if (clst <= 1) return FR_INT_ERR; /* Internal error */\r
1746 if (clst == 0xFFFFFFFF) return FR_DISK_ERR; /* Disk error */\r
1747 if (clst >= fs->n_fatent) { /* It reached end of dynamic table */\r
1748#if !FF_FS_READONLY\r
70702af1
L
1749 if (!stretch) { /* If no stretch, report EOT */\r
1750 dp->sect = 0; return FR_NO_FILE;\r
1751 }\r
1752 clst = create_chain(&dp->obj, dp->clust); /* Allocate a cluster */\r
53668523 1753 if (clst == 0) return FR_DENIED; /* No free cluster */\r
70702af1
L
1754 if (clst == 1) return FR_INT_ERR; /* Internal error */\r
1755 if (clst == 0xFFFFFFFF) return FR_DISK_ERR; /* Disk error */\r
289f6a14
L
1756 if (dir_clear(fs, clst) != FR_OK) return FR_DISK_ERR; /* Clean up the stretched table */\r
1757 if (FF_FS_EXFAT) dp->obj.stat |= 4; /* exFAT: The directory has been stretched */\r
53668523 1758#else\r
289f6a14 1759 if (!stretch) dp->sect = 0; /* (this line is to suppress compiler warning) */\r
70702af1 1760 dp->sect = 0; return FR_NO_FILE; /* Report EOT */\r
53668523
L
1761#endif\r
1762 }\r
70702af1 1763 dp->clust = clst; /* Initialize data for new cluster */\r
289f6a14 1764 dp->sect = clst2sect(fs, clst);\r
53668523
L
1765 }\r
1766 }\r
1767 }\r
70702af1
L
1768 dp->dptr = ofs; /* Current entry */\r
1769 dp->dir = fs->win + ofs % SS(fs); /* Pointer to the entry in the win[] */\r
53668523
L
1770\r
1771 return FR_OK;\r
1772}\r
1773\r
1774\r
1775\r
1776\r
289f6a14 1777#if !FF_FS_READONLY\r
53668523 1778/*-----------------------------------------------------------------------*/\r
70702af1 1779/* Directory handling - Reserve a block of directory entries */\r
53668523
L
1780/*-----------------------------------------------------------------------*/\r
1781\r
289f6a14
L
1782static FRESULT dir_alloc ( /* FR_OK(0):succeeded, !=0:error */\r
1783 DIR* dp, /* Pointer to the directory object */\r
1784 UINT nent /* Number of contiguous entries to allocate */\r
53668523
L
1785)\r
1786{\r
1787 FRESULT res;\r
1788 UINT n;\r
70702af1 1789 FATFS *fs = dp->obj.fs;\r
53668523
L
1790\r
1791\r
1792 res = dir_sdi(dp, 0);\r
1793 if (res == FR_OK) {\r
1794 n = 0;\r
1795 do {\r
70702af1 1796 res = move_window(fs, dp->sect);\r
53668523 1797 if (res != FR_OK) break;\r
289f6a14 1798#if FF_FS_EXFAT\r
70702af1
L
1799 if ((fs->fs_type == FS_EXFAT) ? (int)((dp->dir[XDIR_Type] & 0x80) == 0) : (int)(dp->dir[DIR_Name] == DDEM || dp->dir[DIR_Name] == 0)) {\r
1800#else\r
1801 if (dp->dir[DIR_Name] == DDEM || dp->dir[DIR_Name] == 0) {\r
1802#endif\r
7b78a5a2 1803 if (++n == nent) break; /* A block of contiguous free entries is found */\r
53668523
L
1804 } else {\r
1805 n = 0; /* Not a blank entry. Restart to search */\r
1806 }\r
70702af1
L
1807 res = dir_next(dp, 1);\r
1808 } while (res == FR_OK); /* Next entry with table stretch enabled */\r
53668523 1809 }\r
70702af1 1810\r
53668523
L
1811 if (res == FR_NO_FILE) res = FR_DENIED; /* No directory entry to allocate */\r
1812 return res;\r
1813}\r
70702af1 1814\r
289f6a14 1815#endif /* !FF_FS_READONLY */\r
53668523
L
1816\r
1817\r
1818\r
1819\r
1820/*-----------------------------------------------------------------------*/\r
70702af1 1821/* FAT: Directory handling - Load/Store start cluster number */\r
53668523
L
1822/*-----------------------------------------------------------------------*/\r
1823\r
289f6a14
L
1824static DWORD ld_clust ( /* Returns the top cluster value of the SFN entry */\r
1825 FATFS* fs, /* Pointer to the fs object */\r
1826 const BYTE* dir /* Pointer to the key entry */\r
53668523
L
1827)\r
1828{\r
1829 DWORD cl;\r
1830\r
70702af1
L
1831 cl = ld_word(dir + DIR_FstClusLO);\r
1832 if (fs->fs_type == FS_FAT32) {\r
1833 cl |= (DWORD)ld_word(dir + DIR_FstClusHI) << 16;\r
1834 }\r
53668523
L
1835\r
1836 return cl;\r
1837}\r
1838\r
1839\r
289f6a14
L
1840#if !FF_FS_READONLY\r
1841static void st_clust (\r
70702af1
L
1842 FATFS* fs, /* Pointer to the fs object */\r
1843 BYTE* dir, /* Pointer to the key entry */\r
53668523
L
1844 DWORD cl /* Value to be set */\r
1845)\r
1846{\r
70702af1
L
1847 st_word(dir + DIR_FstClusLO, (WORD)cl);\r
1848 if (fs->fs_type == FS_FAT32) {\r
1849 st_word(dir + DIR_FstClusHI, (WORD)(cl >> 16));\r
1850 }\r
53668523
L
1851}\r
1852#endif\r
1853\r
1854\r
1855\r
289f6a14 1856#if FF_USE_LFN\r
70702af1
L
1857/*--------------------------------------------------------*/\r
1858/* FAT-LFN: Compare a part of file name with an LFN entry */\r
1859/*--------------------------------------------------------*/\r
289f6a14
L
1860\r
1861static int cmp_lfn ( /* 1:matched, 0:not matched */\r
70702af1
L
1862 const WCHAR* lfnbuf, /* Pointer to the LFN working buffer to be compared */\r
1863 BYTE* dir /* Pointer to the directory entry containing the part of LFN */\r
53668523
L
1864)\r
1865{\r
1866 UINT i, s;\r
1867 WCHAR wc, uc;\r
1868\r
1869\r
70702af1
L
1870 if (ld_word(dir + LDIR_FstClusLO) != 0) return 0; /* Check LDIR_FstClusLO */\r
1871\r
1872 i = ((dir[LDIR_Ord] & 0x3F) - 1) * 13; /* Offset in the LFN buffer */\r
1873\r
1874 for (wc = 1, s = 0; s < 13; s++) { /* Process all characters in the entry */\r
1875 uc = ld_word(dir + LfnOfs[s]); /* Pick an LFN character */\r
289f6a14
L
1876 if (wc != 0) {\r
1877 if (i >= FF_MAX_LFN || ff_wtoupper(uc) != ff_wtoupper(lfnbuf[i++])) { /* Compare it */\r
70702af1
L
1878 return 0; /* Not matched */\r
1879 }\r
1880 wc = uc;\r
53668523 1881 } else {\r
70702af1 1882 if (uc != 0xFFFF) return 0; /* Check filler */\r
53668523 1883 }\r
70702af1 1884 }\r
53668523 1885\r
70702af1 1886 if ((dir[LDIR_Ord] & LLEF) && wc && lfnbuf[i]) return 0; /* Last segment matched but different length */\r
53668523 1887\r
70702af1 1888 return 1; /* The part of LFN matched */\r
53668523
L
1889}\r
1890\r
1891\r
289f6a14 1892#if FF_FS_MINIMIZE <= 1 || FF_FS_RPATH >= 2 || FF_USE_LABEL || FF_FS_EXFAT\r
70702af1
L
1893/*-----------------------------------------------------*/\r
1894/* FAT-LFN: Pick a part of file name from an LFN entry */\r
1895/*-----------------------------------------------------*/\r
289f6a14
L
1896\r
1897static int pick_lfn ( /* 1:succeeded, 0:buffer overflow or invalid LFN entry */\r
70702af1
L
1898 WCHAR* lfnbuf, /* Pointer to the LFN working buffer */\r
1899 BYTE* dir /* Pointer to the LFN entry */\r
53668523
L
1900)\r
1901{\r
1902 UINT i, s;\r
1903 WCHAR wc, uc;\r
1904\r
1905\r
289f6a14 1906 if (ld_word(dir + LDIR_FstClusLO) != 0) return 0; /* Check LDIR_FstClusLO is 0 */\r
70702af1 1907\r
289f6a14 1908 i = ((dir[LDIR_Ord] & ~LLEF) - 1) * 13; /* Offset in the LFN buffer */\r
53668523 1909\r
70702af1
L
1910 for (wc = 1, s = 0; s < 13; s++) { /* Process all characters in the entry */\r
1911 uc = ld_word(dir + LfnOfs[s]); /* Pick an LFN character */\r
289f6a14
L
1912 if (wc != 0) {\r
1913 if (i >= FF_MAX_LFN) return 0; /* Buffer overflow? */\r
53668523
L
1914 lfnbuf[i++] = wc = uc; /* Store it */\r
1915 } else {\r
1916 if (uc != 0xFFFF) return 0; /* Check filler */\r
1917 }\r
70702af1 1918 }\r
53668523 1919\r
70702af1 1920 if (dir[LDIR_Ord] & LLEF) { /* Put terminator if it is the last LFN part */\r
289f6a14 1921 if (i >= FF_MAX_LFN) return 0; /* Buffer overflow? */\r
53668523
L
1922 lfnbuf[i] = 0;\r
1923 }\r
1924\r
70702af1 1925 return 1; /* The part of LFN is valid */\r
53668523 1926}\r
70702af1 1927#endif\r
53668523
L
1928\r
1929\r
289f6a14 1930#if !FF_FS_READONLY\r
70702af1
L
1931/*-----------------------------------------*/\r
1932/* FAT-LFN: Create an entry of LFN entries */\r
1933/*-----------------------------------------*/\r
289f6a14
L
1934\r
1935static void put_lfn (\r
70702af1
L
1936 const WCHAR* lfn, /* Pointer to the LFN */\r
1937 BYTE* dir, /* Pointer to the LFN entry to be created */\r
1938 BYTE ord, /* LFN order (1-20) */\r
1939 BYTE sum /* Checksum of the corresponding SFN */\r
53668523
L
1940)\r
1941{\r
1942 UINT i, s;\r
1943 WCHAR wc;\r
1944\r
1945\r
70702af1 1946 dir[LDIR_Chksum] = sum; /* Set checksum */\r
53668523
L
1947 dir[LDIR_Attr] = AM_LFN; /* Set attribute. LFN entry */\r
1948 dir[LDIR_Type] = 0;\r
70702af1 1949 st_word(dir + LDIR_FstClusLO, 0);\r
53668523 1950\r
70702af1 1951 i = (ord - 1) * 13; /* Get offset in the LFN working buffer */\r
53668523
L
1952 s = wc = 0;\r
1953 do {\r
70702af1
L
1954 if (wc != 0xFFFF) wc = lfn[i++]; /* Get an effective character */\r
1955 st_word(dir + LfnOfs[s], wc); /* Put it */\r
1956 if (wc == 0) wc = 0xFFFF; /* Padding characters for left locations */\r
53668523 1957 } while (++s < 13);\r
70702af1 1958 if (wc == 0xFFFF || !lfn[i]) ord |= LLEF; /* Last LFN part is the start of LFN sequence */\r
53668523
L
1959 dir[LDIR_Ord] = ord; /* Set the LFN order */\r
1960}\r
1961\r
289f6a14
L
1962#endif /* !FF_FS_READONLY */\r
1963#endif /* FF_USE_LFN */\r
53668523
L
1964\r
1965\r
1966\r
289f6a14 1967#if FF_USE_LFN && !FF_FS_READONLY\r
53668523 1968/*-----------------------------------------------------------------------*/\r
70702af1 1969/* FAT-LFN: Create a Numbered SFN */\r
53668523 1970/*-----------------------------------------------------------------------*/\r
70702af1 1971\r
289f6a14 1972static void gen_numname (\r
53668523
L
1973 BYTE* dst, /* Pointer to the buffer to store numbered SFN */\r
1974 const BYTE* src, /* Pointer to SFN */\r
1975 const WCHAR* lfn, /* Pointer to LFN */\r
1976 UINT seq /* Sequence number */\r
1977)\r
1978{\r
1979 BYTE ns[8], c;\r
1980 UINT i, j;\r
70702af1
L
1981 WCHAR wc;\r
1982 DWORD sr;\r
53668523
L
1983\r
1984\r
1985 mem_cpy(dst, src, 11);\r
1986\r
70702af1
L
1987 if (seq > 5) { /* In case of many collisions, generate a hash number instead of sequential number */\r
1988 sr = seq;\r
289f6a14 1989 while (*lfn) { /* Create a CRC as hash value */\r
53668523
L
1990 wc = *lfn++;\r
1991 for (i = 0; i < 16; i++) {\r
1992 sr = (sr << 1) + (wc & 1);\r
1993 wc >>= 1;\r
1994 if (sr & 0x10000) sr ^= 0x11021;\r
1995 }\r
1996 }\r
1997 seq = (UINT)sr;\r
1998 }\r
1999\r
2000 /* itoa (hexdecimal) */\r
2001 i = 7;\r
2002 do {\r
70702af1 2003 c = (BYTE)((seq % 16) + '0');\r
53668523
L
2004 if (c > '9') c += 7;\r
2005 ns[i--] = c;\r
2006 seq /= 16;\r
2007 } while (seq);\r
2008 ns[i] = '~';\r
2009\r
289f6a14 2010 /* Append the number to the SFN body */\r
53668523 2011 for (j = 0; j < i && dst[j] != ' '; j++) {\r
289f6a14 2012 if (dbc_1st(dst[j])) {\r
53668523
L
2013 if (j == i - 1) break;\r
2014 j++;\r
2015 }\r
2016 }\r
2017 do {\r
2018 dst[j++] = (i < 8) ? ns[i++] : ' ';\r
2019 } while (j < 8);\r
2020}\r
289f6a14 2021#endif /* FF_USE_LFN && !FF_FS_READONLY */\r
53668523
L
2022\r
2023\r
2024\r
289f6a14 2025#if FF_USE_LFN\r
53668523 2026/*-----------------------------------------------------------------------*/\r
70702af1 2027/* FAT-LFN: Calculate checksum of an SFN entry */\r
53668523 2028/*-----------------------------------------------------------------------*/\r
70702af1 2029\r
289f6a14 2030static BYTE sum_sfn (\r
53668523
L
2031 const BYTE* dir /* Pointer to the SFN entry */\r
2032)\r
2033{\r
2034 BYTE sum = 0;\r
2035 UINT n = 11;\r
2036\r
289f6a14
L
2037 do {\r
2038 sum = (sum >> 1) + (sum << 7) + *dir++;\r
2039 } while (--n);\r
53668523
L
2040 return sum;\r
2041}\r
53668523 2042\r
289f6a14 2043#endif /* FF_USE_LFN */\r
53668523
L
2044\r
2045\r
2046\r
289f6a14 2047#if FF_FS_EXFAT\r
53668523 2048/*-----------------------------------------------------------------------*/\r
70702af1 2049/* exFAT: Checksum */\r
53668523
L
2050/*-----------------------------------------------------------------------*/\r
2051\r
289f6a14 2052static WORD xdir_sum ( /* Get checksum of the directoly entry block */\r
70702af1 2053 const BYTE* dir /* Directory entry block to be calculated */\r
53668523
L
2054)\r
2055{\r
70702af1
L
2056 UINT i, szblk;\r
2057 WORD sum;\r
2058\r
2059\r
289f6a14 2060 szblk = (dir[XDIR_NumSec] + 1) * SZDIRE; /* Number of bytes of the entry block */\r
70702af1 2061 for (i = sum = 0; i < szblk; i++) {\r
289f6a14 2062 if (i == XDIR_SetSum) { /* Skip 2-byte sum field */\r
70702af1
L
2063 i++;\r
2064 } else {\r
2065 sum = ((sum & 1) ? 0x8000 : 0) + (sum >> 1) + dir[i];\r
2066 }\r
2067 }\r
2068 return sum;\r
2069}\r
2070\r
2071\r
2072\r
289f6a14 2073static WORD xname_sum ( /* Get check sum (to be used as hash) of the file name */\r
70702af1
L
2074 const WCHAR* name /* File name to be calculated */\r
2075)\r
2076{\r
2077 WCHAR chr;\r
2078 WORD sum = 0;\r
2079\r
2080\r
2081 while ((chr = *name++) != 0) {\r
289f6a14 2082 chr = (WCHAR)ff_wtoupper(chr); /* File name needs to be up-case converted */\r
70702af1
L
2083 sum = ((sum & 1) ? 0x8000 : 0) + (sum >> 1) + (chr & 0xFF);\r
2084 sum = ((sum & 1) ? 0x8000 : 0) + (sum >> 1) + (chr >> 8);\r
2085 }\r
2086 return sum;\r
2087}\r
2088\r
2089\r
289f6a14
L
2090#if !FF_FS_READONLY && FF_USE_MKFS\r
2091static DWORD xsum32 ( /* Returns 32-bit checksum */\r
2092 BYTE dat, /* Byte to be calculated (byte-by-byte processing) */\r
2093 DWORD sum /* Previous sum value */\r
70702af1
L
2094)\r
2095{\r
2096 sum = ((sum & 1) ? 0x80000000 : 0) + (sum >> 1) + dat;\r
2097 return sum;\r
2098}\r
53668523
L
2099#endif\r
2100\r
70702af1 2101\r
289f6a14 2102#if FF_FS_MINIMIZE <= 1 || FF_FS_RPATH >= 2\r
70702af1
L
2103/*------------------------------------------------------*/\r
2104/* exFAT: Get object information from a directory block */\r
2105/*------------------------------------------------------*/\r
2106\r
289f6a14 2107static void get_xfileinfo (\r
70702af1
L
2108 BYTE* dirb, /* Pointer to the direcotry entry block 85+C0+C1s */\r
2109 FILINFO* fno /* Buffer to store the extracted file information */\r
2110)\r
2111{\r
289f6a14
L
2112 WCHAR wc, hs;\r
2113 UINT di, si, nc;\r
2114\r
2115 /* Get file name from the entry block */\r
2116 si = SZDIRE * 2; /* 1st C1 entry */\r
2117 nc = 0; hs = 0; di = 0;\r
2118 while (nc < dirb[XDIR_NumName]) {\r
2119 if (si >= MAXDIRB(FF_MAX_LFN)) { di = 0; break; } /* Truncated directory block? */\r
2120 if ((si % SZDIRE) == 0) si += 2; /* Skip entry type field */\r
2121 wc = ld_word(dirb + si); si += 2; nc++; /* Get a character */\r
2122 if (hs == 0 && IsSurrogate(wc)) { /* Is it a surrogate? */\r
2123 hs = wc; continue; /* Get low surrogate */\r
70702af1 2124 }\r
289f6a14
L
2125 wc = put_utf((DWORD)hs << 16 | wc, &fno->fname[di], FF_LFN_BUF - di); /* Store it in API encoding */\r
2126 if (wc == 0) { di = 0; break; } /* Buffer overflow or wrong encoding? */\r
2127 di += wc;\r
2128 hs = 0;\r
70702af1 2129 }\r
289f6a14
L
2130 if (hs != 0) di = 0; /* Broken surrogate pair? */\r
2131 if (di == 0) fno->fname[di++] = '?'; /* Inaccessible object name? */\r
2132 fno->fname[di] = 0; /* Terminate the name */\r
2133 fno->altname[0] = 0; /* exFAT does not support SFN */\r
70702af1 2134\r
289f6a14 2135 fno->fattrib = dirb[XDIR_Attr]; /* Attribute */\r
70702af1
L
2136 fno->fsize = (fno->fattrib & AM_DIR) ? 0 : ld_qword(dirb + XDIR_FileSize); /* Size */\r
2137 fno->ftime = ld_word(dirb + XDIR_ModTime + 0); /* Time */\r
2138 fno->fdate = ld_word(dirb + XDIR_ModTime + 2); /* Date */\r
2139}\r
2140\r
289f6a14 2141#endif /* FF_FS_MINIMIZE <= 1 || FF_FS_RPATH >= 2 */\r
70702af1
L
2142\r
2143\r
2144/*-----------------------------------*/\r
2145/* exFAT: Get a directry entry block */\r
2146/*-----------------------------------*/\r
2147\r
289f6a14
L
2148static FRESULT load_xdir ( /* FR_INT_ERR: invalid entry block */\r
2149 DIR* dp /* Reading direcotry object pointing top of the entry block to load */\r
70702af1
L
2150)\r
2151{\r
2152 FRESULT res;\r
289f6a14 2153 UINT i, sz_ent;\r
70702af1
L
2154 BYTE* dirb = dp->obj.fs->dirbuf; /* Pointer to the on-memory direcotry entry block 85+C0+C1s */\r
2155\r
2156\r
2157 /* Load 85 entry */\r
2158 res = move_window(dp->obj.fs, dp->sect);\r
2159 if (res != FR_OK) return res;\r
289f6a14
L
2160 if (dp->dir[XDIR_Type] != 0x85) return FR_INT_ERR; /* Invalid order */\r
2161 mem_cpy(dirb + 0 * SZDIRE, dp->dir, SZDIRE);\r
2162 sz_ent = (dirb[XDIR_NumSec] + 1) * SZDIRE;\r
2163 if (sz_ent < 3 * SZDIRE || sz_ent > 19 * SZDIRE) return FR_INT_ERR;\r
70702af1
L
2164\r
2165 /* Load C0 entry */\r
2166 res = dir_next(dp, 0);\r
289f6a14 2167 if (res == FR_NO_FILE) res = FR_INT_ERR; /* It cannot be */\r
70702af1
L
2168 if (res != FR_OK) return res;\r
2169 res = move_window(dp->obj.fs, dp->sect);\r
53668523 2170 if (res != FR_OK) return res;\r
289f6a14
L
2171 if (dp->dir[XDIR_Type] != 0xC0) return FR_INT_ERR; /* Invalid order */\r
2172 mem_cpy(dirb + 1 * SZDIRE, dp->dir, SZDIRE);\r
2173 if (MAXDIRB(dirb[XDIR_NumName]) > sz_ent) return FR_INT_ERR;\r
70702af1
L
2174\r
2175 /* Load C1 entries */\r
289f6a14 2176 i = 2 * SZDIRE; /* C1 offset to load */\r
70702af1
L
2177 do {\r
2178 res = dir_next(dp, 0);\r
289f6a14 2179 if (res == FR_NO_FILE) res = FR_INT_ERR; /* It cannot be */\r
70702af1
L
2180 if (res != FR_OK) return res;\r
2181 res = move_window(dp->obj.fs, dp->sect);\r
2182 if (res != FR_OK) return res;\r
289f6a14
L
2183 if (dp->dir[XDIR_Type] != 0xC1) return FR_INT_ERR; /* Invalid order */\r
2184 if (i < MAXDIRB(FF_MAX_LFN)) mem_cpy(dirb + i, dp->dir, SZDIRE);\r
2185 } while ((i += SZDIRE) < sz_ent);\r
70702af1 2186\r
289f6a14
L
2187 /* Sanity check (do it for only accessible object) */\r
2188 if (i <= MAXDIRB(FF_MAX_LFN)) {\r
2189 if (xdir_sum(dirb) != ld_word(dirb + XDIR_SetSum)) return FR_INT_ERR;\r
2190 }\r
70702af1
L
2191 return FR_OK;\r
2192}\r
2193\r
53668523 2194\r
289f6a14
L
2195/*------------------------------------------------------------------*/\r
2196/* exFAT: Initialize object allocation info with loaded entry block */\r
2197/*------------------------------------------------------------------*/\r
2198\r
2199static void init_alloc_info (\r
2200 FATFS* fs, /* Filesystem object */\r
2201 FFOBJID* obj /* Object allocation information to be initialized */\r
2202)\r
2203{\r
2204 obj->sclust = ld_dword(fs->dirbuf + XDIR_FstClus); /* Start cluster */\r
2205 obj->objsize = ld_qword(fs->dirbuf + XDIR_FileSize); /* Size */\r
2206 obj->stat = fs->dirbuf[XDIR_GenFlags] & 2; /* Allocation status */\r
2207 obj->n_frag = 0; /* No last fragment info */\r
2208}\r
2209\r
2210\r
2211\r
2212#if !FF_FS_READONLY || FF_FS_RPATH != 0\r
70702af1
L
2213/*------------------------------------------------*/\r
2214/* exFAT: Load the object's directory entry block */\r
2215/*------------------------------------------------*/\r
289f6a14
L
2216\r
2217static FRESULT load_obj_xdir (\r
70702af1 2218 DIR* dp, /* Blank directory object to be used to access containing direcotry */\r
289f6a14 2219 const FFOBJID* obj /* Object with its containing directory information */\r
70702af1
L
2220)\r
2221{\r
2222 FRESULT res;\r
2223\r
70702af1
L
2224 /* Open object containing directory */\r
2225 dp->obj.fs = obj->fs;\r
2226 dp->obj.sclust = obj->c_scl;\r
2227 dp->obj.stat = (BYTE)obj->c_size;\r
2228 dp->obj.objsize = obj->c_size & 0xFFFFFF00;\r
289f6a14 2229 dp->obj.n_frag = 0;\r
70702af1
L
2230 dp->blk_ofs = obj->c_ofs;\r
2231\r
289f6a14 2232 res = dir_sdi(dp, dp->blk_ofs); /* Goto object's entry block */\r
70702af1
L
2233 if (res == FR_OK) {\r
2234 res = load_xdir(dp); /* Load the object's entry block */\r
2235 }\r
2236 return res;\r
2237}\r
53668523 2238#endif\r
70702af1
L
2239\r
2240\r
289f6a14
L
2241#if !FF_FS_READONLY\r
2242/*----------------------------------------*/\r
2243/* exFAT: Store the directory entry block */\r
2244/*----------------------------------------*/\r
2245\r
2246static FRESULT store_xdir (\r
70702af1
L
2247 DIR* dp /* Pointer to the direcotry object */\r
2248)\r
2249{\r
2250 FRESULT res;\r
2251 UINT nent;\r
2252 BYTE* dirb = dp->obj.fs->dirbuf; /* Pointer to the direcotry entry block 85+C0+C1s */\r
2253\r
2254 /* Create set sum */\r
2255 st_word(dirb + XDIR_SetSum, xdir_sum(dirb));\r
2256 nent = dirb[XDIR_NumSec] + 1;\r
2257\r
289f6a14 2258 /* Store the direcotry entry block to the directory */\r
70702af1
L
2259 res = dir_sdi(dp, dp->blk_ofs);\r
2260 while (res == FR_OK) {\r
2261 res = move_window(dp->obj.fs, dp->sect);\r
2262 if (res != FR_OK) break;\r
2263 mem_cpy(dp->dir, dirb, SZDIRE);\r
2264 dp->obj.fs->wflag = 1;\r
2265 if (--nent == 0) break;\r
2266 dirb += SZDIRE;\r
2267 res = dir_next(dp, 0);\r
2268 }\r
2269 return (res == FR_OK || res == FR_DISK_ERR) ? res : FR_INT_ERR;\r
2270}\r
2271\r
2272\r
2273\r
2274/*-------------------------------------------*/\r
2275/* exFAT: Create a new directory enrty block */\r
2276/*-------------------------------------------*/\r
2277\r
289f6a14 2278static void create_xdir (\r
70702af1 2279 BYTE* dirb, /* Pointer to the direcotry entry block buffer */\r
289f6a14 2280 const WCHAR* lfn /* Pointer to the object name */\r
70702af1
L
2281)\r
2282{\r
2283 UINT i;\r
289f6a14
L
2284 BYTE nc1, nlen;\r
2285 WCHAR wc;\r
70702af1
L
2286\r
2287\r
289f6a14
L
2288 /* Create 85,C0 entry */\r
2289 mem_set(dirb, 0, 2 * SZDIRE);\r
2290 dirb[0 * SZDIRE + XDIR_Type] = 0x85; /* 85 entry */\r
2291 dirb[1 * SZDIRE + XDIR_Type] = 0xC0; /* C0 entry */\r
70702af1 2292\r
289f6a14
L
2293 /* Create C1 entries */\r
2294 i = SZDIRE * 2; /* Top of C1 entries */\r
2295 nlen = nc1 = 0; wc = 1;\r
53668523 2296 do {\r
70702af1
L
2297 dirb[i++] = 0xC1; dirb[i++] = 0; /* Entry type C1 */\r
2298 do { /* Fill name field */\r
289f6a14
L
2299 if (wc != 0 && (wc = lfn[nlen]) != 0) nlen++; /* Get a character if exist */\r
2300 st_word(dirb + i, wc); /* Store it */\r
2301 i += 2;\r
2302 } while (i % SZDIRE != 0);\r
2303 nc1++;\r
2304 } while (lfn[nlen]); /* Fill next entry if any char follows */\r
2305\r
2306 dirb[XDIR_NumName] = nlen; /* Set name length */\r
2307 dirb[XDIR_NumSec] = 1 + nc1; /* Set secondary count (C0 + C1s) */\r
2308 st_word(dirb + XDIR_NameHash, xname_sum(lfn)); /* Set name hash */\r
70702af1
L
2309}\r
2310\r
289f6a14
L
2311#endif /* !FF_FS_READONLY */\r
2312#endif /* FF_FS_EXFAT */\r
70702af1
L
2313\r
2314\r
2315\r
289f6a14 2316#if FF_FS_MINIMIZE <= 1 || FF_FS_RPATH >= 2 || FF_USE_LABEL || FF_FS_EXFAT\r
70702af1
L
2317/*-----------------------------------------------------------------------*/\r
2318/* Read an object from the directory */\r
2319/*-----------------------------------------------------------------------*/\r
2320\r
289f6a14
L
2321#define dir_read_file(dp) dir_read(dp, 0)\r
2322#define dir_read_label(dp) dir_read(dp, 1)\r
2323\r
2324static FRESULT dir_read (\r
70702af1
L
2325 DIR* dp, /* Pointer to the directory object */\r
2326 int vol /* Filtered by 0:file/directory or 1:volume label */\r
2327)\r
2328{\r
2329 FRESULT res = FR_NO_FILE;\r
2330 FATFS *fs = dp->obj.fs;\r
2331 BYTE a, c;\r
289f6a14 2332#if FF_USE_LFN\r
70702af1
L
2333 BYTE ord = 0xFF, sum = 0xFF;\r
2334#endif\r
2335\r
2336 while (dp->sect) {\r
2337 res = move_window(fs, dp->sect);\r
53668523 2338 if (res != FR_OK) break;\r
70702af1 2339 c = dp->dir[DIR_Name]; /* Test for the entry type */\r
289f6a14
L
2340 if (c == 0) {\r
2341 res = FR_NO_FILE; break; /* Reached to end of the directory */\r
2342 }\r
2343#if FF_FS_EXFAT\r
70702af1 2344 if (fs->fs_type == FS_EXFAT) { /* On the exFAT volume */\r
289f6a14 2345 if (FF_USE_LABEL && vol) {\r
70702af1
L
2346 if (c == 0x83) break; /* Volume label entry? */\r
2347 } else {\r
2348 if (c == 0x85) { /* Start of the file entry block? */\r
2349 dp->blk_ofs = dp->dptr; /* Get location of the block */\r
2350 res = load_xdir(dp); /* Load the entry block */\r
2351 if (res == FR_OK) {\r
2352 dp->obj.attr = fs->dirbuf[XDIR_Attr] & AM_MASK; /* Get attribute */\r
53668523 2353 }\r
70702af1
L
2354 break;\r
2355 }\r
2356 }\r
2357 } else\r
2358#endif\r
289f6a14 2359 { /* On the FAT/FAT32 volume */\r
70702af1 2360 dp->obj.attr = a = dp->dir[DIR_Attr] & AM_MASK; /* Get attribute */\r
289f6a14 2361#if FF_USE_LFN /* LFN configuration */\r
70702af1
L
2362 if (c == DDEM || c == '.' || (int)((a & ~AM_ARC) == AM_VOL) != vol) { /* An entry without valid data */\r
2363 ord = 0xFF;\r
2364 } else {\r
2365 if (a == AM_LFN) { /* An LFN entry is found */\r
2366 if (c & LLEF) { /* Is it start of an LFN sequence? */\r
2367 sum = dp->dir[LDIR_Chksum];\r
2368 c &= (BYTE)~LLEF; ord = c;\r
2369 dp->blk_ofs = dp->dptr;\r
2370 }\r
2371 /* Check LFN validity and capture it */\r
2372 ord = (c == ord && sum == dp->dir[LDIR_Chksum] && pick_lfn(fs->lfnbuf, dp->dir)) ? ord - 1 : 0xFF;\r
2373 } else { /* An SFN entry is found */\r
289f6a14 2374 if (ord != 0 || sum != sum_sfn(dp->dir)) { /* Is there a valid LFN? */\r
70702af1
L
2375 dp->blk_ofs = 0xFFFFFFFF; /* It has no LFN. */\r
2376 }\r
2377 break;\r
53668523 2378 }\r
53668523 2379 }\r
53668523 2380#else /* Non LFN configuration */\r
70702af1
L
2381 if (c != DDEM && c != '.' && a != AM_LFN && (int)((a & ~AM_ARC) == AM_VOL) == vol) { /* Is it a valid entry? */\r
2382 break;\r
2383 }\r
53668523 2384#endif\r
70702af1 2385 }\r
53668523 2386 res = dir_next(dp, 0); /* Next entry */\r
70702af1
L
2387 if (res != FR_OK) break;\r
2388 }\r
53668523 2389\r
70702af1 2390 if (res != FR_OK) dp->sect = 0; /* Terminate the read operation on error or EOT */\r
53668523
L
2391 return res;\r
2392}\r
2393\r
289f6a14 2394#endif /* FF_FS_MINIMIZE <= 1 || FF_USE_LABEL || FF_FS_RPATH >= 2 */\r
53668523
L
2395\r
2396\r
2397\r
2398/*-----------------------------------------------------------------------*/\r
70702af1 2399/* Directory handling - Find an object in the directory */\r
53668523 2400/*-----------------------------------------------------------------------*/\r
70702af1 2401\r
289f6a14
L
2402static FRESULT dir_find ( /* FR_OK(0):succeeded, !=0:error */\r
2403 DIR* dp /* Pointer to the directory object with the file name */\r
53668523
L
2404)\r
2405{\r
2406 FRESULT res;\r
70702af1
L
2407 FATFS *fs = dp->obj.fs;\r
2408 BYTE c;\r
289f6a14 2409#if FF_USE_LFN\r
70702af1 2410 BYTE a, ord, sum;\r
53668523
L
2411#endif\r
2412\r
70702af1
L
2413 res = dir_sdi(dp, 0); /* Rewind directory object */\r
2414 if (res != FR_OK) return res;\r
289f6a14 2415#if FF_FS_EXFAT\r
70702af1
L
2416 if (fs->fs_type == FS_EXFAT) { /* On the exFAT volume */\r
2417 BYTE nc;\r
2418 UINT di, ni;\r
2419 WORD hash = xname_sum(fs->lfnbuf); /* Hash value of the name to find */\r
2420\r
289f6a14
L
2421 while ((res = dir_read_file(dp)) == FR_OK) { /* Read an item */\r
2422#if FF_MAX_LFN < 255\r
2423 if (fs->dirbuf[XDIR_NumName] > FF_MAX_LFN) continue; /* Skip comparison if inaccessible object name */\r
2424#endif\r
2425 if (ld_word(fs->dirbuf + XDIR_NameHash) != hash) continue; /* Skip comparison if hash mismatched */\r
70702af1
L
2426 for (nc = fs->dirbuf[XDIR_NumName], di = SZDIRE * 2, ni = 0; nc; nc--, di += 2, ni++) { /* Compare the name */\r
2427 if ((di % SZDIRE) == 0) di += 2;\r
2428 if (ff_wtoupper(ld_word(fs->dirbuf + di)) != ff_wtoupper(fs->lfnbuf[ni])) break;\r
2429 }\r
2430 if (nc == 0 && !fs->lfnbuf[ni]) break; /* Name matched? */\r
2431 }\r
2432 return res;\r
2433 }\r
2434#endif\r
289f6a14
L
2435 /* On the FAT/FAT32 volume */\r
2436#if FF_USE_LFN\r
70702af1
L
2437 ord = sum = 0xFF; dp->blk_ofs = 0xFFFFFFFF; /* Reset LFN sequence */\r
2438#endif\r
2439 do {\r
2440 res = move_window(fs, dp->sect);\r
53668523 2441 if (res != FR_OK) break;\r
70702af1 2442 c = dp->dir[DIR_Name];\r
53668523 2443 if (c == 0) { res = FR_NO_FILE; break; } /* Reached to end of table */\r
289f6a14 2444#if FF_USE_LFN /* LFN configuration */\r
70702af1
L
2445 dp->obj.attr = a = dp->dir[DIR_Attr] & AM_MASK;\r
2446 if (c == DDEM || ((a & AM_VOL) && a != AM_LFN)) { /* An entry without valid data */\r
2447 ord = 0xFF; dp->blk_ofs = 0xFFFFFFFF; /* Reset LFN sequence */\r
53668523
L
2448 } else {\r
2449 if (a == AM_LFN) { /* An LFN entry is found */\r
70702af1
L
2450 if (!(dp->fn[NSFLAG] & NS_NOLFN)) {\r
2451 if (c & LLEF) { /* Is it start of LFN sequence? */\r
2452 sum = dp->dir[LDIR_Chksum];\r
2453 c &= (BYTE)~LLEF; ord = c; /* LFN start order */\r
2454 dp->blk_ofs = dp->dptr; /* Start offset of LFN */\r
2455 }\r
2456 /* Check validity of the LFN entry and compare it with given name */\r
2457 ord = (c == ord && sum == dp->dir[LDIR_Chksum] && cmp_lfn(fs->lfnbuf, dp->dir)) ? ord - 1 : 0xFF;\r
53668523 2458 }\r
53668523 2459 } else { /* An SFN entry is found */\r
289f6a14 2460 if (ord == 0 && sum == sum_sfn(dp->dir)) break; /* LFN matched? */\r
70702af1
L
2461 if (!(dp->fn[NSFLAG] & NS_LOSS) && !mem_cmp(dp->dir, dp->fn, 11)) break; /* SFN matched? */\r
2462 ord = 0xFF; dp->blk_ofs = 0xFFFFFFFF; /* Reset LFN sequence */\r
53668523
L
2463 }\r
2464 }\r
2465#else /* Non LFN configuration */\r
70702af1
L
2466 dp->obj.attr = dp->dir[DIR_Attr] & AM_MASK;\r
2467 if (!(dp->dir[DIR_Attr] & AM_VOL) && !mem_cmp(dp->dir, dp->fn, 11)) break; /* Is it a valid entry? */\r
53668523 2468#endif\r
70702af1
L
2469 res = dir_next(dp, 0); /* Next entry */\r
2470 } while (res == FR_OK);\r
53668523
L
2471\r
2472 return res;\r
2473}\r
53668523
L
2474\r
2475\r
2476\r
2477\r
289f6a14 2478#if !FF_FS_READONLY\r
53668523
L
2479/*-----------------------------------------------------------------------*/\r
2480/* Register an object to the directory */\r
2481/*-----------------------------------------------------------------------*/\r
70702af1 2482\r
289f6a14
L
2483static FRESULT dir_register ( /* FR_OK:succeeded, FR_DENIED:no free entry or too many SFN collision, FR_DISK_ERR:disk error */\r
2484 DIR* dp /* Target directory with object name to be created */\r
53668523
L
2485)\r
2486{\r
2487 FRESULT res;\r
70702af1 2488 FATFS *fs = dp->obj.fs;\r
289f6a14 2489#if FF_USE_LFN /* LFN configuration */\r
70702af1
L
2490 UINT n, nlen, nent;\r
2491 BYTE sn[12], sum;\r
2492\r
2493\r
2494 if (dp->fn[NSFLAG] & (NS_DOT | NS_NONAME)) return FR_INVALID_NAME; /* Check name validity */\r
2495 for (nlen = 0; fs->lfnbuf[nlen]; nlen++) ; /* Get lfn length */\r
2496\r
289f6a14 2497#if FF_FS_EXFAT\r
70702af1 2498 if (fs->fs_type == FS_EXFAT) { /* On the exFAT volume */\r
70702af1
L
2499 nent = (nlen + 14) / 15 + 2; /* Number of entries to allocate (85+C0+C1s) */\r
2500 res = dir_alloc(dp, nent); /* Allocate entries */\r
2501 if (res != FR_OK) return res;\r
289f6a14 2502 dp->blk_ofs = dp->dptr - SZDIRE * (nent - 1); /* Set the allocated entry block offset */\r
70702af1 2503\r
289f6a14
L
2504 if (dp->obj.stat & 4) { /* Has the directory been stretched? */\r
2505 dp->obj.stat &= ~4;\r
2506 res = fill_first_frag(&dp->obj); /* Fill the first fragment on the FAT if needed */\r
70702af1 2507 if (res != FR_OK) return res;\r
289f6a14 2508 res = fill_last_frag(&dp->obj, dp->clust, 0xFFFFFFFF); /* Fill the last fragment on the FAT if needed */\r
70702af1 2509 if (res != FR_OK) return res;\r
289f6a14
L
2510 if (dp->obj.sclust != 0) { /* Is it a sub directory? */\r
2511 DIR dj;\r
2512\r
2513 res = load_obj_xdir(&dj, &dp->obj); /* Load the object status */\r
2514 if (res != FR_OK) return res;\r
2515 dp->obj.objsize += (DWORD)fs->csize * SS(fs); /* Increase the directory size by cluster size */\r
2516 st_qword(fs->dirbuf + XDIR_FileSize, dp->obj.objsize); /* Update the allocation status */\r
2517 st_qword(fs->dirbuf + XDIR_ValidFileSize, dp->obj.objsize);\r
2518 fs->dirbuf[XDIR_GenFlags] = dp->obj.stat | 1;\r
2519 res = store_xdir(&dj); /* Store the object status */\r
2520 if (res != FR_OK) return res;\r
2521 }\r
70702af1 2522 }\r
53668523 2523\r
70702af1
L
2524 create_xdir(fs->dirbuf, fs->lfnbuf); /* Create on-memory directory block to be written later */\r
2525 return FR_OK;\r
2526 }\r
2527#endif\r
289f6a14 2528 /* On the FAT/FAT32 volume */\r
70702af1 2529 mem_cpy(sn, dp->fn, 12);\r
7b78a5a2 2530 if (sn[NSFLAG] & NS_LOSS) { /* When LFN is out of 8.3 format, generate a numbered name */\r
70702af1 2531 dp->fn[NSFLAG] = NS_NOLFN; /* Find only SFN */\r
53668523 2532 for (n = 1; n < 100; n++) {\r
70702af1 2533 gen_numname(dp->fn, sn, fs->lfnbuf, n); /* Generate a numbered name */\r
53668523
L
2534 res = dir_find(dp); /* Check if the name collides with existing SFN */\r
2535 if (res != FR_OK) break;\r
2536 }\r
2537 if (n == 100) return FR_DENIED; /* Abort if too many collisions */\r
2538 if (res != FR_NO_FILE) return res; /* Abort if the result is other than 'not collided' */\r
70702af1 2539 dp->fn[NSFLAG] = sn[NSFLAG];\r
53668523
L
2540 }\r
2541\r
70702af1
L
2542 /* Create an SFN with/without LFNs. */\r
2543 nent = (sn[NSFLAG] & NS_LFN) ? (nlen + 12) / 13 + 1 : 1; /* Number of entries to allocate */\r
53668523 2544 res = dir_alloc(dp, nent); /* Allocate entries */\r
53668523 2545 if (res == FR_OK && --nent) { /* Set LFN entry if needed */\r
70702af1 2546 res = dir_sdi(dp, dp->dptr - nent * SZDIRE);\r
53668523 2547 if (res == FR_OK) {\r
70702af1 2548 sum = sum_sfn(dp->fn); /* Checksum value of the SFN tied to the LFN */\r
53668523 2549 do { /* Store LFN entries in bottom first */\r
70702af1 2550 res = move_window(fs, dp->sect);\r
53668523 2551 if (res != FR_OK) break;\r
70702af1
L
2552 put_lfn(fs->lfnbuf, dp->dir, (BYTE)nent, sum);\r
2553 fs->wflag = 1;\r
53668523
L
2554 res = dir_next(dp, 0); /* Next entry */\r
2555 } while (res == FR_OK && --nent);\r
2556 }\r
2557 }\r
70702af1 2558\r
53668523
L
2559#else /* Non LFN configuration */\r
2560 res = dir_alloc(dp, 1); /* Allocate an entry for SFN */\r
70702af1 2561\r
53668523
L
2562#endif\r
2563\r
70702af1
L
2564 /* Set SFN entry */\r
2565 if (res == FR_OK) {\r
2566 res = move_window(fs, dp->sect);\r
53668523 2567 if (res == FR_OK) {\r
70702af1
L
2568 mem_set(dp->dir, 0, SZDIRE); /* Clean the entry */\r
2569 mem_cpy(dp->dir + DIR_Name, dp->fn, 11); /* Put SFN */\r
289f6a14 2570#if FF_USE_LFN\r
7b78a5a2 2571 dp->dir[DIR_NTres] = dp->fn[NSFLAG] & (NS_BODY | NS_EXT); /* Put NT flag */\r
53668523 2572#endif\r
70702af1 2573 fs->wflag = 1;\r
53668523
L
2574 }\r
2575 }\r
2576\r
2577 return res;\r
2578}\r
53668523 2579\r
289f6a14 2580#endif /* !FF_FS_READONLY */\r
53668523
L
2581\r
2582\r
2583\r
289f6a14 2584#if !FF_FS_READONLY && FF_FS_MINIMIZE == 0\r
53668523
L
2585/*-----------------------------------------------------------------------*/\r
2586/* Remove an object from the directory */\r
2587/*-----------------------------------------------------------------------*/\r
70702af1 2588\r
289f6a14
L
2589static FRESULT dir_remove ( /* FR_OK:Succeeded, FR_DISK_ERR:A disk error */\r
2590 DIR* dp /* Directory object pointing the entry to be removed */\r
53668523
L
2591)\r
2592{\r
2593 FRESULT res;\r
70702af1 2594 FATFS *fs = dp->obj.fs;\r
289f6a14 2595#if FF_USE_LFN /* LFN configuration */\r
70702af1 2596 DWORD last = dp->dptr;\r
53668523 2597\r
70702af1 2598 res = (dp->blk_ofs == 0xFFFFFFFF) ? FR_OK : dir_sdi(dp, dp->blk_ofs); /* Goto top of the entry block if LFN is exist */\r
53668523
L
2599 if (res == FR_OK) {\r
2600 do {\r
70702af1 2601 res = move_window(fs, dp->sect);\r
53668523 2602 if (res != FR_OK) break;\r
289f6a14
L
2603 if (FF_FS_EXFAT && fs->fs_type == FS_EXFAT) { /* On the exFAT volume */\r
2604 dp->dir[XDIR_Type] &= 0x7F; /* Clear the entry InUse flag. */\r
2605 } else { /* On the FAT/FAT32 volume */\r
2606 dp->dir[DIR_Name] = DDEM; /* Mark the entry 'deleted'. */\r
70702af1
L
2607 }\r
2608 fs->wflag = 1;\r
2609 if (dp->dptr >= last) break; /* If reached last entry then all entries of the object has been deleted. */\r
2610 res = dir_next(dp, 0); /* Next entry */\r
53668523
L
2611 } while (res == FR_OK);\r
2612 if (res == FR_NO_FILE) res = FR_INT_ERR;\r
2613 }\r
53668523 2614#else /* Non LFN configuration */\r
70702af1
L
2615\r
2616 res = move_window(fs, dp->sect);\r
53668523 2617 if (res == FR_OK) {\r
289f6a14 2618 dp->dir[DIR_Name] = DDEM; /* Mark the entry 'deleted'.*/\r
70702af1 2619 fs->wflag = 1;\r
53668523
L
2620 }\r
2621#endif\r
2622\r
2623 return res;\r
2624}\r
53668523 2625\r
289f6a14 2626#endif /* !FF_FS_READONLY && FF_FS_MINIMIZE == 0 */\r
53668523
L
2627\r
2628\r
2629\r
289f6a14 2630#if FF_FS_MINIMIZE <= 1 || FF_FS_RPATH >= 2\r
53668523
L
2631/*-----------------------------------------------------------------------*/\r
2632/* Get file information from directory entry */\r
2633/*-----------------------------------------------------------------------*/\r
70702af1 2634\r
289f6a14 2635static void get_fileinfo (\r
53668523 2636 DIR* dp, /* Pointer to the directory object */\r
289f6a14 2637 FILINFO* fno /* Pointer to the file information to be filled */\r
53668523
L
2638)\r
2639{\r
289f6a14
L
2640 UINT si, di;\r
2641#if FF_USE_LFN\r
2642 WCHAR wc, hs;\r
70702af1 2643 FATFS *fs = dp->obj.fs;\r
289f6a14
L
2644#else\r
2645 TCHAR c;\r
70702af1 2646#endif\r
53668523
L
2647\r
2648\r
289f6a14
L
2649 fno->fname[0] = 0; /* Invaidate file info */\r
2650 if (dp->sect == 0) return; /* Exit if read pointer has reached end of directory */\r
53668523 2651\r
289f6a14
L
2652#if FF_USE_LFN /* LFN configuration */\r
2653#if FF_FS_EXFAT\r
70702af1 2654 if (fs->fs_type == FS_EXFAT) { /* On the exFAT volume */\r
289f6a14 2655 get_xfileinfo(fs->dirbuf, fno);\r
70702af1
L
2656 return;\r
2657 } else\r
53668523 2658#endif\r
289f6a14 2659 { /* On the FAT/FAT32 volume */\r
70702af1 2660 if (dp->blk_ofs != 0xFFFFFFFF) { /* Get LFN if available */\r
289f6a14
L
2661 si = di = hs = 0;\r
2662 while (fs->lfnbuf[si] != 0) {\r
2663 wc = fs->lfnbuf[si++]; /* Get an LFN character (UTF-16) */\r
2664 if (hs == 0 && IsSurrogate(wc)) { /* Is it a surrogate? */\r
2665 hs = wc; continue; /* Get low surrogate */\r
70702af1 2666 }\r
289f6a14
L
2667 wc = put_utf((DWORD)hs << 16 | wc, &fno->fname[di], FF_LFN_BUF - di); /* Store it in UTF-16 or UTF-8 encoding */\r
2668 if (wc == 0) { di = 0; break; } /* Invalid char or buffer overflow? */\r
2669 di += wc;\r
2670 hs = 0;\r
70702af1 2671 }\r
289f6a14
L
2672 if (hs != 0) di = 0; /* Broken surrogate pair? */\r
2673 fno->fname[di] = 0; /* Terminate the LFN (null string means LFN is invalid) */\r
53668523 2674 }\r
53668523 2675 }\r
53668523 2676\r
289f6a14
L
2677 si = di = 0;\r
2678 while (si < 11) { /* Get SFN from SFN entry */\r
2679 wc = dp->dir[si++]; /* Get a char */\r
2680 if (wc == ' ') continue; /* Skip padding spaces */\r
2681 if (wc == RDDEM) wc = DDEM; /* Restore replaced DDEM character */\r
2682 if (si == 9 && di < FF_SFN_BUF) fno->altname[di++] = '.'; /* Insert a . if extension is exist */\r
2683#if FF_LFN_UNICODE >= 1 /* Unicode output */\r
2684 if (dbc_1st((BYTE)wc) && si != 8 && si != 11 && dbc_2nd(dp->dir[si])) { /* Make a DBC if needed */\r
2685 wc = wc << 8 | dp->dir[si++];\r
70702af1 2686 }\r
289f6a14
L
2687 wc = ff_oem2uni(wc, CODEPAGE); /* ANSI/OEM -> Unicode */\r
2688 if (wc == 0) { di = 0; break; } /* Wrong char in the current code page? */\r
2689 wc = put_utf(wc, &fno->altname[di], FF_SFN_BUF - di); /* Store it in Unicode */\r
2690 if (wc == 0) { di = 0; break; } /* Buffer overflow? */\r
2691 di += wc;\r
2692#else /* ANSI/OEM output */\r
2693 fno->altname[di++] = (TCHAR)wc; /* Store it without any conversion */\r
53668523 2694#endif\r
289f6a14
L
2695 }\r
2696 fno->altname[di] = 0; /* Terminate the SFN (null string means SFN is invalid) */\r
2697\r
2698 if (fno->fname[0] == 0) { /* If LFN is invalid, altname[] needs to be copied to fname[] */\r
2699 if (di == 0) { /* If LFN and SFN both are invalid, this object is inaccesible */\r
2700 fno->fname[di++] = '?';\r
2701 } else {\r
2702 for (si = di = 0; fno->altname[si]; si++, di++) { /* Copy altname[] to fname[] with case information */\r
2703 wc = (WCHAR)fno->altname[si];\r
2704 if (IsUpper(wc) && (dp->dir[DIR_NTres] & ((si >= 9) ? NS_EXT : NS_BODY))) wc += 0x20;\r
2705 fno->fname[di] = (TCHAR)wc;\r
53668523
L
2706 }\r
2707 }\r
289f6a14
L
2708 fno->fname[di] = 0; /* Terminate the LFN */\r
2709 if (!dp->dir[DIR_NTres]) fno->altname[0] = 0; /* Altname is not needed if neither LFN nor case info is exist. */\r
53668523 2710 }\r
70702af1
L
2711\r
2712#else /* Non-LFN configuration */\r
289f6a14
L
2713 si = di = 0;\r
2714 while (si < 11) { /* Copy name body and extension */\r
2715 c = (TCHAR)dp->dir[si++];\r
2716 if (c == ' ') continue; /* Skip padding spaces */\r
2717 if (c == RDDEM) c = DDEM; /* Restore replaced DDEM character */\r
2718 if (si == 9) fno->fname[di++] = '.';/* Insert a . if extension is exist */\r
2719 fno->fname[di++] = c;\r
70702af1 2720 }\r
289f6a14 2721 fno->fname[di] = 0;\r
53668523 2722#endif\r
70702af1 2723\r
289f6a14
L
2724 fno->fattrib = dp->dir[DIR_Attr]; /* Attribute */\r
2725 fno->fsize = ld_dword(dp->dir + DIR_FileSize); /* Size */\r
2726 fno->ftime = ld_word(dp->dir + DIR_ModTime + 0); /* Time */\r
2727 fno->fdate = ld_word(dp->dir + DIR_ModTime + 2); /* Date */\r
53668523 2728}\r
53668523 2729\r
289f6a14 2730#endif /* FF_FS_MINIMIZE <= 1 || FF_FS_RPATH >= 2 */\r
53668523
L
2731\r
2732\r
2733\r
289f6a14 2734#if FF_USE_FIND && FF_FS_MINIMIZE <= 1\r
53668523 2735/*-----------------------------------------------------------------------*/\r
70702af1 2736/* Pattern matching */\r
53668523
L
2737/*-----------------------------------------------------------------------*/\r
2738\r
289f6a14
L
2739static DWORD get_achar ( /* Get a character and advances ptr */\r
2740 const TCHAR** ptr /* Pointer to pointer to the ANSI/OEM or Unicode string */\r
70702af1
L
2741)\r
2742{\r
289f6a14 2743 DWORD chr;\r
70702af1 2744\r
289f6a14
L
2745\r
2746#if FF_USE_LFN && FF_LFN_UNICODE >= 1 /* Unicode input */\r
2747 chr = tchar2uni(ptr);\r
2748 if (chr == 0xFFFFFFFF) chr = 0; /* Wrong UTF encoding is recognized as end of the string */\r
2749 chr = ff_wtoupper(chr);\r
2750\r
2751#else /* ANSI/OEM input */\r
2752 chr = (BYTE)*(*ptr)++; /* Get a byte */\r
2753 if (IsLower(chr)) chr -= 0x20; /* To upper ASCII char */\r
2754#if FF_CODE_PAGE == 0\r
2755 if (ExCvt && chr >= 0x80) chr = ExCvt[chr - 0x80]; /* To upper SBCS extended char */\r
2756#elif FF_CODE_PAGE < 900\r
70702af1 2757 if (chr >= 0x80) chr = ExCvt[chr - 0x80]; /* To upper SBCS extended char */\r
289f6a14
L
2758#endif\r
2759#if FF_CODE_PAGE == 0 || FF_CODE_PAGE >= 900\r
2760 if (dbc_1st((BYTE)chr)) { /* Get DBC 2nd byte if needed */\r
2761 chr = dbc_2nd((BYTE)**ptr) ? chr << 8 | (BYTE)*(*ptr)++ : 0;\r
70702af1
L
2762 }\r
2763#endif\r
289f6a14 2764\r
70702af1 2765#endif\r
289f6a14 2766 return chr;\r
70702af1
L
2767}\r
2768\r
2769\r
289f6a14 2770static int pattern_matching ( /* 0:not matched, 1:matched */\r
70702af1
L
2771 const TCHAR* pat, /* Matching pattern */\r
2772 const TCHAR* nam, /* String to be tested */\r
2773 int skip, /* Number of pre-skip chars (number of ?s) */\r
2774 int inf /* Infinite search (* specified) */\r
2775)\r
2776{\r
2777 const TCHAR *pp, *np;\r
289f6a14 2778 DWORD pc, nc;\r
70702af1
L
2779 int nm, nx;\r
2780\r
2781\r
2782 while (skip--) { /* Pre-skip name chars */\r
2783 if (!get_achar(&nam)) return 0; /* Branch mismatched if less name chars */\r
2784 }\r
289f6a14 2785 if (*pat == 0 && inf) return 1; /* (short circuit) */\r
70702af1
L
2786\r
2787 do {\r
2788 pp = pat; np = nam; /* Top of pattern and name to match */\r
2789 for (;;) {\r
2790 if (*pp == '?' || *pp == '*') { /* Wildcard? */\r
2791 nm = nx = 0;\r
289f6a14 2792 do { /* Analyze the wildcard block */\r
70702af1
L
2793 if (*pp++ == '?') nm++; else nx = 1;\r
2794 } while (*pp == '?' || *pp == '*');\r
2795 if (pattern_matching(pp, np, nm, nx)) return 1; /* Test new branch (recurs upto number of wildcard blocks in the pattern) */\r
2796 nc = *np; break; /* Branch mismatched */\r
2797 }\r
2798 pc = get_achar(&pp); /* Get a pattern char */\r
2799 nc = get_achar(&np); /* Get a name char */\r
2800 if (pc != nc) break; /* Branch mismatched? */\r
2801 if (pc == 0) return 1; /* Branch matched? (matched at end of both strings) */\r
2802 }\r
2803 get_achar(&nam); /* nam++ */\r
2804 } while (inf && nc); /* Retry until end of name if infinite search is specified */\r
2805\r
2806 return 0;\r
2807}\r
2808\r
289f6a14 2809#endif /* FF_USE_FIND && FF_FS_MINIMIZE <= 1 */\r
70702af1
L
2810\r
2811\r
2812\r
2813/*-----------------------------------------------------------------------*/\r
2814/* Pick a top segment and create the object name in directory form */\r
2815/*-----------------------------------------------------------------------*/\r
2816\r
289f6a14
L
2817static FRESULT create_name ( /* FR_OK: successful, FR_INVALID_NAME: could not create */\r
2818 DIR* dp, /* Pointer to the directory object */\r
2819 const TCHAR** path /* Pointer to pointer to the segment in the path string */\r
53668523
L
2820)\r
2821{\r
289f6a14 2822#if FF_USE_LFN /* LFN configuration */\r
53668523 2823 BYTE b, cf;\r
289f6a14
L
2824 WCHAR wc, *lfn;\r
2825 DWORD uc;\r
53668523
L
2826 UINT i, ni, si, di;\r
2827 const TCHAR *p;\r
2828\r
289f6a14
L
2829\r
2830 /* Create LFN into LFN working buffer */\r
2831 p = *path; lfn = dp->obj.fs->lfnbuf; di = 0;\r
2832 for (;;) {\r
2833 uc = tchar2uni(&p); /* Get a character */\r
2834 if (uc == 0xFFFFFFFF) return FR_INVALID_NAME; /* Invalid code or UTF decode error */\r
2835 if (uc >= 0x10000) lfn[di++] = (WCHAR)(uc >> 16); /* Store high surrogate if needed */\r
2836 wc = (WCHAR)uc;\r
2837 if (wc < ' ' || wc == '/' || wc == '\\') break; /* Break if end of the path or a separator is found */\r
2838 if (wc < 0x80 && chk_chr("\"*:<>\?|\x7F", wc)) return FR_INVALID_NAME; /* Reject illegal characters for LFN */\r
2839 if (di >= FF_MAX_LFN) return FR_INVALID_NAME; /* Reject too long name */\r
2840 lfn[di++] = wc; /* Store the Unicode character */\r
53668523 2841 }\r
289f6a14
L
2842 while (*p == '/' || *p == '\\') p++; /* Skip duplicated separators if exist */\r
2843 *path = p; /* Return pointer to the next segment */\r
2844 cf = (wc < ' ') ? NS_LAST : 0; /* Set last segment flag if end of the path */\r
2845\r
2846#if FF_FS_RPATH != 0\r
70702af1
L
2847 if ((di == 1 && lfn[di - 1] == '.') ||\r
2848 (di == 2 && lfn[di - 1] == '.' && lfn[di - 2] == '.')) { /* Is this segment a dot name? */\r
53668523 2849 lfn[di] = 0;\r
289f6a14 2850 for (i = 0; i < 11; i++) { /* Create dot name for SFN entry */\r
53668523 2851 dp->fn[i] = (i < di) ? '.' : ' ';\r
289f6a14 2852 }\r
53668523
L
2853 dp->fn[i] = cf | NS_DOT; /* This is a dot entry */\r
2854 return FR_OK;\r
2855 }\r
2856#endif\r
70702af1 2857 while (di) { /* Snip off trailing spaces and dots if exist */\r
289f6a14
L
2858 wc = lfn[di - 1];\r
2859 if (wc != ' ' && wc != '.') break;\r
53668523
L
2860 di--;\r
2861 }\r
289f6a14
L
2862 lfn[di] = 0; /* LFN is created into the working buffer */\r
2863 if (di == 0) return FR_INVALID_NAME; /* Reject null name */\r
53668523
L
2864\r
2865 /* Create SFN in directory form */\r
289f6a14
L
2866 for (si = 0; lfn[si] == ' '; si++) ; /* Remove leading spaces */\r
2867 if (si > 0 || lfn[si] == '.') cf |= NS_LOSS | NS_LFN; /* Is there any leading space or dot? */\r
2868 while (di > 0 && lfn[di - 1] != '.') di--; /* Find last dot (di<=si: no extension) */\r
53668523 2869\r
289f6a14 2870 mem_set(dp->fn, ' ', 11);\r
70702af1 2871 i = b = 0; ni = 8;\r
53668523 2872 for (;;) {\r
289f6a14
L
2873 wc = lfn[si++]; /* Get an LFN character */\r
2874 if (wc == 0) break; /* Break on end of the LFN */\r
2875 if (wc == ' ' || (wc == '.' && si != di)) { /* Remove embedded spaces and dots */\r
2876 cf |= NS_LOSS | NS_LFN;\r
2877 continue;\r
53668523
L
2878 }\r
2879\r
289f6a14
L
2880 if (i >= ni || si == di) { /* End of field? */\r
2881 if (ni == 11) { /* Name extension overflow? */\r
2882 cf |= NS_LOSS | NS_LFN;\r
2883 break;\r
53668523 2884 }\r
289f6a14
L
2885 if (si != di) cf |= NS_LOSS | NS_LFN; /* Name body overflow? */\r
2886 if (si > di) break; /* No name extension? */\r
2887 si = di; i = 8; ni = 11; b <<= 2; /* Enter name extension */\r
2888 continue;\r
53668523
L
2889 }\r
2890\r
289f6a14
L
2891 if (wc >= 0x80) { /* Is this a non-ASCII character? */\r
2892 cf |= NS_LFN; /* LFN entry needs to be created */\r
2893#if FF_CODE_PAGE == 0\r
2894 if (ExCvt) { /* At SBCS */\r
2895 wc = ff_uni2oem(wc, CODEPAGE); /* Unicode ==> ANSI/OEM code */\r
2896 if (wc & 0x80) wc = ExCvt[wc & 0x7F]; /* Convert extended character to upper (SBCS) */\r
2897 } else { /* At DBCS */\r
2898 wc = ff_uni2oem(ff_wtoupper(wc), CODEPAGE); /* Unicode ==> Upper convert ==> ANSI/OEM code */\r
2899 }\r
2900#elif FF_CODE_PAGE < 900 /* SBCS cfg */\r
2901 wc = ff_uni2oem(wc, CODEPAGE); /* Unicode ==> ANSI/OEM code */\r
2902 if (wc & 0x80) wc = ExCvt[wc & 0x7F]; /* Convert extended character to upper (SBCS) */\r
2903#else /* DBCS cfg */\r
2904 wc = ff_uni2oem(ff_wtoupper(wc), CODEPAGE); /* Unicode ==> Upper convert ==> ANSI/OEM code */\r
53668523 2905#endif\r
53668523
L
2906 }\r
2907\r
289f6a14
L
2908 if (wc >= 0x100) { /* Is this a DBC? */\r
2909 if (i >= ni - 1) { /* Field overflow? */\r
2910 cf |= NS_LOSS | NS_LFN;\r
2911 i = ni; continue; /* Next field */\r
53668523 2912 }\r
289f6a14 2913 dp->fn[i++] = (BYTE)(wc >> 8); /* Put 1st byte */\r
70702af1 2914 } else { /* SBC */\r
289f6a14
L
2915 if (wc == 0 || chk_chr("+,;=[]", wc)) { /* Replace illegal characters for SFN if needed */\r
2916 wc = '_'; cf |= NS_LOSS | NS_LFN;/* Lossy conversion */\r
53668523 2917 } else {\r
289f6a14 2918 if (IsUpper(wc)) { /* ASCII upper case? */\r
53668523 2919 b |= 2;\r
289f6a14
L
2920 }\r
2921 if (IsLower(wc)) { /* ASCII lower case? */\r
2922 b |= 1; wc -= 0x20;\r
53668523
L
2923 }\r
2924 }\r
2925 }\r
289f6a14 2926 dp->fn[i++] = (BYTE)wc;\r
53668523
L
2927 }\r
2928\r
70702af1 2929 if (dp->fn[0] == DDEM) dp->fn[0] = RDDEM; /* If the first character collides with DDEM, replace it with RDDEM */\r
53668523 2930\r
289f6a14
L
2931 if (ni == 8) b <<= 2; /* Shift capital flags if no extension */\r
2932 if ((b & 0x0C) == 0x0C || (b & 0x03) == 0x03) cf |= NS_LFN; /* LFN entry needs to be created if composite capitals */\r
2933 if (!(cf & NS_LFN)) { /* When LFN is in 8.3 format without extended character, NT flags are created */\r
2934 if (b & 0x01) cf |= NS_EXT; /* NT flag (Extension has small capital letters only) */\r
2935 if (b & 0x04) cf |= NS_BODY; /* NT flag (Body has small capital letters only) */\r
53668523
L
2936 }\r
2937\r
289f6a14 2938 dp->fn[NSFLAG] = cf; /* SFN is created into dp->fn[] */\r
53668523
L
2939\r
2940 return FR_OK;\r
2941\r
2942\r
289f6a14 2943#else /* FF_USE_LFN : Non-LFN configuration */\r
70702af1 2944 BYTE c, d, *sfn;\r
53668523
L
2945 UINT ni, si, i;\r
2946 const char *p;\r
2947\r
2948 /* Create file name in directory form */\r
70702af1 2949 p = *path; sfn = dp->fn;\r
53668523 2950 mem_set(sfn, ' ', 11);\r
70702af1 2951 si = i = 0; ni = 8;\r
289f6a14 2952#if FF_FS_RPATH != 0\r
53668523
L
2953 if (p[si] == '.') { /* Is this a dot entry? */\r
2954 for (;;) {\r
2955 c = (BYTE)p[si++];\r
2956 if (c != '.' || si >= 3) break;\r
2957 sfn[i++] = c;\r
2958 }\r
2959 if (c != '/' && c != '\\' && c > ' ') return FR_INVALID_NAME;\r
70702af1
L
2960 *path = p + si; /* Return pointer to the next segment */\r
2961 sfn[NSFLAG] = (c <= ' ') ? NS_LAST | NS_DOT : NS_DOT; /* Set last segment flag if end of the path */\r
53668523
L
2962 return FR_OK;\r
2963 }\r
2964#endif\r
2965 for (;;) {\r
289f6a14 2966 c = (BYTE)p[si++]; /* Get a byte */\r
70702af1
L
2967 if (c <= ' ') break; /* Break if end of the path name */\r
2968 if (c == '/' || c == '\\') { /* Break if a separator is found */\r
2969 while (p[si] == '/' || p[si] == '\\') si++; /* Skip duplicated separator if exist */\r
2970 break;\r
2971 }\r
289f6a14
L
2972 if (c == '.' || i >= ni) { /* End of body or field overflow? */\r
2973 if (ni == 11 || c != '.') return FR_INVALID_NAME; /* Field overflow or invalid dot? */\r
2974 i = 8; ni = 11; /* Enter file extension field */\r
70702af1 2975 continue;\r
53668523 2976 }\r
289f6a14
L
2977#if FF_CODE_PAGE == 0\r
2978 if (ExCvt && c >= 0x80) { /* Is SBC extended character? */\r
2979 c = ExCvt[c & 0x7F]; /* To upper SBC extended character */\r
2980 }\r
2981#elif FF_CODE_PAGE < 900\r
2982 if (c >= 0x80) { /* Is SBC extended character? */\r
2983 c = ExCvt[c & 0x7F]; /* To upper SBC extended character */\r
53668523 2984 }\r
289f6a14
L
2985#endif\r
2986 if (dbc_1st(c)) { /* Check if it is a DBC 1st byte */\r
53668523 2987 d = (BYTE)p[si++]; /* Get 2nd byte */\r
289f6a14 2988 if (!dbc_2nd(d) || i >= ni - 1) return FR_INVALID_NAME; /* Reject invalid DBC */\r
53668523
L
2989 sfn[i++] = c;\r
2990 sfn[i++] = d;\r
70702af1
L
2991 } else { /* SBC */\r
2992 if (chk_chr("\"*+,:;<=>\?[]|\x7F", c)) return FR_INVALID_NAME; /* Reject illegal chrs for SFN */\r
2993 if (IsLower(c)) c -= 0x20; /* To upper */\r
53668523
L
2994 sfn[i++] = c;\r
2995 }\r
2996 }\r
70702af1
L
2997 *path = p + si; /* Return pointer to the next segment */\r
2998 if (i == 0) return FR_INVALID_NAME; /* Reject nul string */\r
53668523 2999\r
70702af1
L
3000 if (sfn[0] == DDEM) sfn[0] = RDDEM; /* If the first character collides with DDEM, replace it with RDDEM */\r
3001 sfn[NSFLAG] = (c <= ' ') ? NS_LAST : 0; /* Set last segment flag if end of the path */\r
53668523
L
3002\r
3003 return FR_OK;\r
289f6a14 3004#endif /* FF_USE_LFN */\r
53668523
L
3005}\r
3006\r
3007\r
3008\r
3009\r
3010/*-----------------------------------------------------------------------*/\r
3011/* Follow a file path */\r
3012/*-----------------------------------------------------------------------*/\r
3013\r
289f6a14
L
3014static FRESULT follow_path ( /* FR_OK(0): successful, !=0: error code */\r
3015 DIR* dp, /* Directory object to return last directory and found object */\r
3016 const TCHAR* path /* Full-path string to find a file or directory */\r
53668523
L
3017)\r
3018{\r
3019 FRESULT res;\r
70702af1 3020 BYTE ns;\r
289f6a14 3021 FATFS *fs = dp->obj.fs;\r
53668523
L
3022\r
3023\r
289f6a14 3024#if FF_FS_RPATH != 0\r
70702af1 3025 if (*path != '/' && *path != '\\') { /* Without heading separator */\r
289f6a14 3026 dp->obj.sclust = fs->cdir; /* Start from current directory */\r
70702af1
L
3027 } else\r
3028#endif\r
3029 { /* With heading separator */\r
3030 while (*path == '/' || *path == '\\') path++; /* Strip heading separator */\r
289f6a14 3031 dp->obj.sclust = 0; /* Start from root directory */\r
70702af1 3032 }\r
289f6a14
L
3033#if FF_FS_EXFAT\r
3034 dp->obj.n_frag = 0; /* Invalidate last fragment counter of the object */\r
3035#if FF_FS_RPATH != 0\r
3036 if (fs->fs_type == FS_EXFAT && dp->obj.sclust) { /* exFAT: Retrieve the sub-directory's status */\r
70702af1
L
3037 DIR dj;\r
3038\r
289f6a14
L
3039 dp->obj.c_scl = fs->cdc_scl;\r
3040 dp->obj.c_size = fs->cdc_size;\r
3041 dp->obj.c_ofs = fs->cdc_ofs;\r
3042 res = load_obj_xdir(&dj, &dp->obj);\r
70702af1 3043 if (res != FR_OK) return res;\r
289f6a14
L
3044 dp->obj.objsize = ld_dword(fs->dirbuf + XDIR_FileSize);\r
3045 dp->obj.stat = fs->dirbuf[XDIR_GenFlags] & 2;\r
53668523 3046 }\r
289f6a14 3047#endif\r
53668523
L
3048#endif\r
3049\r
3050 if ((UINT)*path < ' ') { /* Null path name is the origin directory itself */\r
70702af1 3051 dp->fn[NSFLAG] = NS_NONAME;\r
53668523 3052 res = dir_sdi(dp, 0);\r
70702af1 3053\r
53668523
L
3054 } else { /* Follow path */\r
3055 for (;;) {\r
3056 res = create_name(dp, &path); /* Get a segment name of the path */\r
3057 if (res != FR_OK) break;\r
70702af1 3058 res = dir_find(dp); /* Find an object with the segment name */\r
7b78a5a2 3059 ns = dp->fn[NSFLAG];\r
53668523
L
3060 if (res != FR_OK) { /* Failed to find the object */\r
3061 if (res == FR_NO_FILE) { /* Object is not found */\r
289f6a14 3062 if (FF_FS_RPATH && (ns & NS_DOT)) { /* If dot entry is not exist, stay there */\r
53668523 3063 if (!(ns & NS_LAST)) continue; /* Continue to follow if not last segment */\r
70702af1
L
3064 dp->fn[NSFLAG] = NS_NONAME;\r
3065 res = FR_OK;\r
53668523
L
3066 } else { /* Could not find the object */\r
3067 if (!(ns & NS_LAST)) res = FR_NO_PATH; /* Adjust error code if not last segment */\r
3068 }\r
3069 }\r
3070 break;\r
3071 }\r
3072 if (ns & NS_LAST) break; /* Last segment matched. Function completed. */\r
70702af1 3073 /* Get into the sub-directory */\r
289f6a14 3074 if (!(dp->obj.attr & AM_DIR)) { /* It is not a sub-directory and cannot follow */\r
53668523
L
3075 res = FR_NO_PATH; break;\r
3076 }\r
289f6a14
L
3077#if FF_FS_EXFAT\r
3078 if (fs->fs_type == FS_EXFAT) { /* Save containing directory information for next dir */\r
3079 dp->obj.c_scl = dp->obj.sclust;\r
3080 dp->obj.c_size = ((DWORD)dp->obj.objsize & 0xFFFFFF00) | dp->obj.stat;\r
3081 dp->obj.c_ofs = dp->blk_ofs;\r
3082 init_alloc_info(fs, &dp->obj); /* Open next directory */\r
70702af1
L
3083 } else\r
3084#endif\r
3085 {\r
289f6a14 3086 dp->obj.sclust = ld_clust(fs, fs->win + dp->dptr % SS(fs)); /* Open next directory */\r
70702af1 3087 }\r
53668523
L
3088 }\r
3089 }\r
3090\r
3091 return res;\r
3092}\r
3093\r
3094\r
3095\r
3096\r
3097/*-----------------------------------------------------------------------*/\r
3098/* Get logical drive number from path name */\r
3099/*-----------------------------------------------------------------------*/\r
3100\r
289f6a14
L
3101static int get_ldnumber ( /* Returns logical drive number (-1:invalid drive number or null pointer) */\r
3102 const TCHAR** path /* Pointer to pointer to the path name */\r
53668523
L
3103)\r
3104{\r
3105 const TCHAR *tp, *tt;\r
289f6a14
L
3106 TCHAR tc;\r
3107 int i, vol = -1;\r
3108#if FF_STR_VOLUME_ID /* Find string volume ID */\r
70702af1
L
3109 const char *sp;\r
3110 char c;\r
70702af1 3111#endif\r
53668523 3112\r
289f6a14
L
3113 tt = tp = *path;\r
3114 if (!tp) return vol; /* Invalid path name? */\r
3115 do tc = *tt++; while ((UINT)tc >= (FF_USE_LFN ? ' ' : '!') && tc != ':'); /* Find a colon in the path */\r
53668523 3116\r
289f6a14
L
3117 if (tc == ':') { /* DOS/Windows style volume ID? */\r
3118 i = FF_VOLUMES;\r
3119 if (IsDigit(*tp) && tp + 2 == tt) { /* Is there a numeric volume ID + colon? */\r
3120 i = (int)*tp - '0'; /* Get the LD number */\r
3121 }\r
3122#if FF_STR_VOLUME_ID == 1 /* Arbitrary string is enabled */\r
3123 else {\r
3124 i = 0;\r
3125 do {\r
3126 sp = VolumeStr[i]; tp = *path; /* This string volume ID and path name */\r
3127 do { /* Compare the volume ID with path name */\r
3128 c = *sp++; tc = *tp++;\r
3129 if (IsLower(c)) c -= 0x20;\r
3130 if (IsLower(tc)) tc -= 0x20;\r
3131 } while (c && (TCHAR)c == tc);\r
3132 } while ((c || tp != tt) && ++i < FF_VOLUMES); /* Repeat for each id until pattern match */\r
3133 }\r
70702af1 3134#endif\r
289f6a14
L
3135 if (i < FF_VOLUMES) { /* If a volume ID is found, get the drive number and strip it */\r
3136 vol = i; /* Drive number */\r
3137 *path = tt; /* Snip the drive prefix off */\r
3138 }\r
3139 return vol;\r
3140 }\r
3141#if FF_STR_VOLUME_ID == 2 /* Unix style volume ID is enabled */\r
3142 if (*tp == '/') {\r
3143 i = 0;\r
3144 do {\r
3145 sp = VolumeStr[i]; tp = *path; /* This string volume ID and path name */\r
3146 do { /* Compare the volume ID with path name */\r
3147 c = *sp++; tc = *(++tp);\r
3148 if (IsLower(c)) c -= 0x20;\r
3149 if (IsLower(tc)) tc -= 0x20;\r
3150 } while (c && (TCHAR)c == tc);\r
3151 } while ((c || (tc != '/' && (UINT)tc >= (FF_USE_LFN ? ' ' : '!'))) && ++i < FF_VOLUMES); /* Repeat for each ID until pattern match */\r
3152 if (i < FF_VOLUMES) { /* If a volume ID is found, get the drive number and strip it */\r
3153 vol = i; /* Drive number */\r
3154 *path = tp; /* Snip the drive prefix off */\r
53668523
L
3155 return vol;\r
3156 }\r
289f6a14
L
3157 }\r
3158#endif\r
3159 /* No drive prefix is found */\r
3160#if FF_FS_RPATH != 0\r
3161 vol = CurrVol; /* Default drive is current drive */\r
53668523 3162#else\r
289f6a14 3163 vol = 0; /* Default drive is 0 */\r
53668523 3164#endif\r
289f6a14 3165 return vol; /* Return the default drive */\r
53668523
L
3166}\r
3167\r
3168\r
3169\r
3170\r
3171/*-----------------------------------------------------------------------*/\r
289f6a14 3172/* Load a sector and check if it is an FAT VBR */\r
53668523
L
3173/*-----------------------------------------------------------------------*/\r
3174\r
289f6a14
L
3175static BYTE check_fs ( /* 0:FAT, 1:exFAT, 2:Valid BS but not FAT, 3:Not a BS, 4:Disk error */\r
3176 FATFS* fs, /* Filesystem object */\r
3177 DWORD sect /* Sector# (lba) to load and check if it is an FAT-VBR or not */\r
53668523
L
3178)\r
3179{\r
70702af1
L
3180 fs->wflag = 0; fs->winsect = 0xFFFFFFFF; /* Invaidate window */\r
3181 if (move_window(fs, sect) != FR_OK) return 4; /* Load boot record */\r
53668523 3182\r
289f6a14 3183 if (ld_word(fs->win + BS_55AA) != 0xAA55) return 3; /* Check boot record signature (always here regardless of the sector size) */\r
53668523 3184\r
289f6a14
L
3185#if FF_FS_EXFAT\r
3186 if (!mem_cmp(fs->win + BS_JmpBoot, "\xEB\x76\x90" "EXFAT ", 11)) return 1; /* Check if exFAT VBR */\r
70702af1 3187#endif\r
289f6a14
L
3188 if (fs->win[BS_JmpBoot] == 0xE9 || fs->win[BS_JmpBoot] == 0xEB || fs->win[BS_JmpBoot] == 0xE8) { /* Valid JumpBoot code? */\r
3189 if (!mem_cmp(fs->win + BS_FilSysType, "FAT", 3)) return 0; /* Is it an FAT VBR? */\r
3190 if (!mem_cmp(fs->win + BS_FilSysType32, "FAT32", 5)) return 0; /* Is it an FAT32 VBR? */\r
3191 }\r
3192 return 2; /* Valid BS but not FAT */\r
53668523
L
3193}\r
3194\r
3195\r
3196\r
3197\r
3198/*-----------------------------------------------------------------------*/\r
289f6a14 3199/* Determine logical drive number and mount the volume if needed */\r
53668523
L
3200/*-----------------------------------------------------------------------*/\r
3201\r
289f6a14
L
3202static FRESULT find_volume ( /* FR_OK(0): successful, !=0: an error occurred */\r
3203 const TCHAR** path, /* Pointer to pointer to the path name (drive number) */\r
3204 FATFS** rfs, /* Pointer to pointer to the found filesystem object */\r
3205 BYTE mode /* !=0: Check write protection for write access */\r
53668523
L
3206)\r
3207{\r
70702af1 3208 BYTE fmt, *pt;\r
53668523
L
3209 int vol;\r
3210 DSTATUS stat;\r
70702af1 3211 DWORD bsect, fasize, tsect, sysect, nclst, szbfat, br[4];\r
53668523
L
3212 WORD nrsv;\r
3213 FATFS *fs;\r
70702af1 3214 UINT i;\r
53668523
L
3215\r
3216\r
70702af1 3217 /* Get logical drive number */\r
53668523
L
3218 *rfs = 0;\r
3219 vol = get_ldnumber(path);\r
3220 if (vol < 0) return FR_INVALID_DRIVE;\r
3221\r
289f6a14
L
3222 /* Check if the filesystem object is valid or not */\r
3223 fs = FatFs[vol]; /* Get pointer to the filesystem object */\r
3224 if (!fs) return FR_NOT_ENABLED; /* Is the filesystem object available? */\r
3225#if FF_FS_REENTRANT\r
3226 if (!lock_fs(fs)) return FR_TIMEOUT; /* Lock the volume */\r
3227#endif\r
3228 *rfs = fs; /* Return pointer to the filesystem object */\r
53668523 3229\r
70702af1 3230 mode &= (BYTE)~FA_READ; /* Desired access mode, write access or not */\r
289f6a14
L
3231 if (fs->fs_type != 0) { /* If the volume has been mounted */\r
3232 stat = disk_status(fs->pdrv);\r
53668523 3233 if (!(stat & STA_NOINIT)) { /* and the physical drive is kept initialized */\r
289f6a14 3234 if (!FF_FS_READONLY && mode && (stat & STA_PROTECT)) { /* Check write protection if needed */\r
53668523 3235 return FR_WRITE_PROTECTED;\r
70702af1 3236 }\r
289f6a14 3237 return FR_OK; /* The filesystem object is valid */\r
53668523
L
3238 }\r
3239 }\r
3240\r
289f6a14
L
3241 /* The filesystem object is not valid. */\r
3242 /* Following code attempts to mount the volume. (analyze BPB and initialize the filesystem object) */\r
53668523 3243\r
289f6a14
L
3244 fs->fs_type = 0; /* Clear the filesystem object */\r
3245 fs->pdrv = LD2PD(vol); /* Bind the logical drive and a physical drive */\r
3246 stat = disk_initialize(fs->pdrv); /* Initialize the physical drive */\r
70702af1 3247 if (stat & STA_NOINIT) { /* Check if the initialization succeeded */\r
53668523 3248 return FR_NOT_READY; /* Failed to initialize due to no medium or hard error */\r
70702af1 3249 }\r
289f6a14 3250 if (!FF_FS_READONLY && mode && (stat & STA_PROTECT)) { /* Check disk write protection if needed */\r
53668523 3251 return FR_WRITE_PROTECTED;\r
70702af1 3252 }\r
289f6a14
L
3253#if FF_MAX_SS != FF_MIN_SS /* Get sector size (multiple sector size cfg only) */\r
3254 if (disk_ioctl(fs->pdrv, GET_SECTOR_SIZE, &SS(fs)) != RES_OK) return FR_DISK_ERR;\r
3255 if (SS(fs) > FF_MAX_SS || SS(fs) < FF_MIN_SS || (SS(fs) & (SS(fs) - 1))) return FR_DISK_ERR;\r
53668523 3256#endif\r
289f6a14
L
3257\r
3258 /* Find an FAT partition on the drive. Supports only generic partitioning rules, FDISK and SFD. */\r
53668523 3259 bsect = 0;\r
70702af1
L
3260 fmt = check_fs(fs, bsect); /* Load sector 0 and check if it is an FAT-VBR as SFD */\r
3261 if (fmt == 2 || (fmt < 2 && LD2PT(vol) != 0)) { /* Not an FAT-VBR or forced partition number */\r
289f6a14 3262 for (i = 0; i < 4; i++) { /* Get partition offset */\r
70702af1
L
3263 pt = fs->win + (MBR_Table + i * SZ_PTE);\r
3264 br[i] = pt[PTE_System] ? ld_dword(pt + PTE_StLba) : 0;\r
53668523 3265 }\r
289f6a14
L
3266 i = LD2PT(vol); /* Partition number: 0:auto, 1-4:forced */\r
3267 if (i != 0) i--;\r
3268 do { /* Find an FAT volume */\r
53668523 3269 bsect = br[i];\r
70702af1 3270 fmt = bsect ? check_fs(fs, bsect) : 3; /* Check the partition */\r
289f6a14 3271 } while (LD2PT(vol) == 0 && fmt >= 2 && ++i < 4);\r
53668523 3272 }\r
70702af1
L
3273 if (fmt == 4) return FR_DISK_ERR; /* An error occured in the disk I/O layer */\r
3274 if (fmt >= 2) return FR_NO_FILESYSTEM; /* No FAT volume is found */\r
53668523 3275\r
289f6a14 3276 /* An FAT volume is found (bsect). Following code initializes the filesystem object */\r
53668523 3277\r
289f6a14 3278#if FF_FS_EXFAT\r
70702af1
L
3279 if (fmt == 1) {\r
3280 QWORD maxlba;\r
3281\r
3282 for (i = BPB_ZeroedEx; i < BPB_ZeroedEx + 53 && fs->win[i] == 0; i++) ; /* Check zero filler */\r
3283 if (i < BPB_ZeroedEx + 53) return FR_NO_FILESYSTEM;\r
3284\r
289f6a14 3285 if (ld_word(fs->win + BPB_FSVerEx) != 0x100) return FR_NO_FILESYSTEM; /* Check exFAT version (must be version 1.0) */\r
70702af1 3286\r
289f6a14 3287 if (1 << fs->win[BPB_BytsPerSecEx] != SS(fs)) { /* (BPB_BytsPerSecEx must be equal to the physical sector size) */\r
70702af1 3288 return FR_NO_FILESYSTEM;\r
289f6a14 3289 }\r
70702af1
L
3290\r
3291 maxlba = ld_qword(fs->win + BPB_TotSecEx) + bsect; /* Last LBA + 1 of the volume */\r
3292 if (maxlba >= 0x100000000) return FR_NO_FILESYSTEM; /* (It cannot be handled in 32-bit LBA) */\r
3293\r
3294 fs->fsize = ld_dword(fs->win + BPB_FatSzEx); /* Number of sectors per FAT */\r
3295\r
3296 fs->n_fats = fs->win[BPB_NumFATsEx]; /* Number of FATs */\r
3297 if (fs->n_fats != 1) return FR_NO_FILESYSTEM; /* (Supports only 1 FAT) */\r
3298\r
3299 fs->csize = 1 << fs->win[BPB_SecPerClusEx]; /* Cluster size */\r
3300 if (fs->csize == 0) return FR_NO_FILESYSTEM; /* (Must be 1..32768) */\r
3301\r
3302 nclst = ld_dword(fs->win + BPB_NumClusEx); /* Number of clusters */\r
3303 if (nclst > MAX_EXFAT) return FR_NO_FILESYSTEM; /* (Too many clusters) */\r
3304 fs->n_fatent = nclst + 2;\r
53668523 3305\r
70702af1
L
3306 /* Boundaries and Limits */\r
3307 fs->volbase = bsect;\r
3308 fs->database = bsect + ld_dword(fs->win + BPB_DataOfsEx);\r
3309 fs->fatbase = bsect + ld_dword(fs->win + BPB_FatOfsEx);\r
3310 if (maxlba < (QWORD)fs->database + nclst * fs->csize) return FR_NO_FILESYSTEM; /* (Volume size must not be smaller than the size requiered) */\r
3311 fs->dirbase = ld_dword(fs->win + BPB_RootClusEx);\r
3312\r
3313 /* Check if bitmap location is in assumption (at the first cluster) */\r
289f6a14 3314 if (move_window(fs, clst2sect(fs, fs->dirbase)) != FR_OK) return FR_DISK_ERR;\r
70702af1
L
3315 for (i = 0; i < SS(fs); i += SZDIRE) {\r
3316 if (fs->win[i] == 0x81 && ld_dword(fs->win + i + 20) == 2) break; /* 81 entry with cluster #2? */\r
3317 }\r
3318 if (i == SS(fs)) return FR_NO_FILESYSTEM;\r
289f6a14 3319#if !FF_FS_READONLY\r
70702af1
L
3320 fs->last_clst = fs->free_clst = 0xFFFFFFFF; /* Initialize cluster allocation information */\r
3321#endif\r
3322 fmt = FS_EXFAT; /* FAT sub-type */\r
3323 } else\r
289f6a14 3324#endif /* FF_FS_EXFAT */\r
70702af1
L
3325 {\r
3326 if (ld_word(fs->win + BPB_BytsPerSec) != SS(fs)) return FR_NO_FILESYSTEM; /* (BPB_BytsPerSec must be equal to the physical sector size) */\r
3327\r
289f6a14 3328 fasize = ld_word(fs->win + BPB_FATSz16); /* Number of sectors per FAT */\r
70702af1
L
3329 if (fasize == 0) fasize = ld_dword(fs->win + BPB_FATSz32);\r
3330 fs->fsize = fasize;\r
3331\r
289f6a14 3332 fs->n_fats = fs->win[BPB_NumFATs]; /* Number of FATs */\r
70702af1 3333 if (fs->n_fats != 1 && fs->n_fats != 2) return FR_NO_FILESYSTEM; /* (Must be 1 or 2) */\r
289f6a14 3334 fasize *= fs->n_fats; /* Number of sectors for FAT area */\r
70702af1 3335\r
289f6a14 3336 fs->csize = fs->win[BPB_SecPerClus]; /* Cluster size */\r
70702af1
L
3337 if (fs->csize == 0 || (fs->csize & (fs->csize - 1))) return FR_NO_FILESYSTEM; /* (Must be power of 2) */\r
3338\r
3339 fs->n_rootdir = ld_word(fs->win + BPB_RootEntCnt); /* Number of root directory entries */\r
3340 if (fs->n_rootdir % (SS(fs) / SZDIRE)) return FR_NO_FILESYSTEM; /* (Must be sector aligned) */\r
3341\r
289f6a14 3342 tsect = ld_word(fs->win + BPB_TotSec16); /* Number of sectors on the volume */\r
70702af1
L
3343 if (tsect == 0) tsect = ld_dword(fs->win + BPB_TotSec32);\r
3344\r
289f6a14
L
3345 nrsv = ld_word(fs->win + BPB_RsvdSecCnt); /* Number of reserved sectors */\r
3346 if (nrsv == 0) return FR_NO_FILESYSTEM; /* (Must not be 0) */\r
70702af1
L
3347\r
3348 /* Determine the FAT sub type */\r
3349 sysect = nrsv + fasize + fs->n_rootdir / (SS(fs) / SZDIRE); /* RSV + FAT + DIR */\r
289f6a14
L
3350 if (tsect < sysect) return FR_NO_FILESYSTEM; /* (Invalid volume size) */\r
3351 nclst = (tsect - sysect) / fs->csize; /* Number of clusters */\r
3352 if (nclst == 0) return FR_NO_FILESYSTEM; /* (Invalid volume size) */\r
3353 fmt = 0;\r
3354 if (nclst <= MAX_FAT32) fmt = FS_FAT32;\r
70702af1
L
3355 if (nclst <= MAX_FAT16) fmt = FS_FAT16;\r
3356 if (nclst <= MAX_FAT12) fmt = FS_FAT12;\r
289f6a14 3357 if (fmt == 0) return FR_NO_FILESYSTEM;\r
70702af1
L
3358\r
3359 /* Boundaries and Limits */\r
289f6a14
L
3360 fs->n_fatent = nclst + 2; /* Number of FAT entries */\r
3361 fs->volbase = bsect; /* Volume start sector */\r
3362 fs->fatbase = bsect + nrsv; /* FAT start sector */\r
3363 fs->database = bsect + sysect; /* Data start sector */\r
70702af1
L
3364 if (fmt == FS_FAT32) {\r
3365 if (ld_word(fs->win + BPB_FSVer32) != 0) return FR_NO_FILESYSTEM; /* (Must be FAT32 revision 0.0) */\r
289f6a14 3366 if (fs->n_rootdir != 0) return FR_NO_FILESYSTEM; /* (BPB_RootEntCnt must be 0) */\r
70702af1 3367 fs->dirbase = ld_dword(fs->win + BPB_RootClus32); /* Root directory start cluster */\r
289f6a14 3368 szbfat = fs->n_fatent * 4; /* (Needed FAT size) */\r
70702af1 3369 } else {\r
289f6a14
L
3370 if (fs->n_rootdir == 0) return FR_NO_FILESYSTEM; /* (BPB_RootEntCnt must not be 0) */\r
3371 fs->dirbase = fs->fatbase + fasize; /* Root directory start sector */\r
3372 szbfat = (fmt == FS_FAT16) ? /* (Needed FAT size) */\r
70702af1
L
3373 fs->n_fatent * 2 : fs->n_fatent * 3 / 2 + (fs->n_fatent & 1);\r
3374 }\r
3375 if (fs->fsize < (szbfat + (SS(fs) - 1)) / SS(fs)) return FR_NO_FILESYSTEM; /* (BPB_FATSz must not be less than the size needed) */\r
53668523 3376\r
289f6a14
L
3377#if !FF_FS_READONLY\r
3378 /* Get FSInfo if available */\r
70702af1
L
3379 fs->last_clst = fs->free_clst = 0xFFFFFFFF; /* Initialize cluster allocation information */\r
3380 fs->fsi_flag = 0x80;\r
289f6a14
L
3381#if (FF_FS_NOFSINFO & 3) != 3\r
3382 if (fmt == FS_FAT32 /* Allow to update FSInfo only if BPB_FSInfo32 == 1 */\r
70702af1
L
3383 && ld_word(fs->win + BPB_FSInfo32) == 1\r
3384 && move_window(fs, bsect + 1) == FR_OK)\r
53668523 3385 {\r
70702af1 3386 fs->fsi_flag = 0;\r
289f6a14 3387 if (ld_word(fs->win + BS_55AA) == 0xAA55 /* Load FSInfo data if available */\r
70702af1
L
3388 && ld_dword(fs->win + FSI_LeadSig) == 0x41615252\r
3389 && ld_dword(fs->win + FSI_StrucSig) == 0x61417272)\r
3390 {\r
289f6a14 3391#if (FF_FS_NOFSINFO & 1) == 0\r
70702af1 3392 fs->free_clst = ld_dword(fs->win + FSI_Free_Count);\r
53668523 3393#endif\r
289f6a14 3394#if (FF_FS_NOFSINFO & 2) == 0\r
70702af1 3395 fs->last_clst = ld_dword(fs->win + FSI_Nxt_Free);\r
53668523 3396#endif\r
70702af1 3397 }\r
53668523 3398 }\r
289f6a14
L
3399#endif /* (FF_FS_NOFSINFO & 3) != 3 */\r
3400#endif /* !FF_FS_READONLY */\r
53668523 3401 }\r
70702af1 3402\r
289f6a14
L
3403 fs->fs_type = fmt; /* FAT sub-type */\r
3404 fs->id = ++Fsid; /* Volume mount ID */\r
3405#if FF_USE_LFN == 1\r
70702af1 3406 fs->lfnbuf = LfnBuf; /* Static LFN working buffer */\r
289f6a14
L
3407#if FF_FS_EXFAT\r
3408 fs->dirbuf = DirBuf; /* Static directory block scratchpad buuffer */\r
70702af1 3409#endif\r
53668523 3410#endif\r
289f6a14
L
3411#if FF_FS_RPATH != 0\r
3412 fs->cdir = 0; /* Initialize current directory */\r
70702af1 3413#endif\r
289f6a14 3414#if FF_FS_LOCK != 0 /* Clear file lock semaphores */\r
53668523
L
3415 clear_lock(fs);\r
3416#endif\r
53668523
L
3417 return FR_OK;\r
3418}\r
3419\r
3420\r
3421\r
3422\r
3423/*-----------------------------------------------------------------------*/\r
3424/* Check if the file/directory object is valid or not */\r
3425/*-----------------------------------------------------------------------*/\r
3426\r
289f6a14
L
3427static FRESULT validate ( /* Returns FR_OK or FR_INVALID_OBJECT */\r
3428 FFOBJID* obj, /* Pointer to the FFOBJID, the 1st member in the FIL/DIR object, to check validity */\r
3429 FATFS** rfs /* Pointer to pointer to the owner filesystem object to return */\r
53668523
L
3430)\r
3431{\r
289f6a14 3432 FRESULT res = FR_INVALID_OBJECT;\r
53668523 3433\r
53668523 3434\r
289f6a14
L
3435 if (obj && obj->fs && obj->fs->fs_type && obj->id == obj->fs->id) { /* Test if the object is valid */\r
3436#if FF_FS_REENTRANT\r
3437 if (lock_fs(obj->fs)) { /* Obtain the filesystem object */\r
3438 if (!(disk_status(obj->fs->pdrv) & STA_NOINIT)) { /* Test if the phsical drive is kept initialized */\r
3439 res = FR_OK;\r
3440 } else {\r
3441 unlock_fs(obj->fs, FR_OK);\r
3442 }\r
3443 } else {\r
3444 res = FR_TIMEOUT;\r
3445 }\r
3446#else\r
3447 if (!(disk_status(obj->fs->pdrv) & STA_NOINIT)) { /* Test if the phsical drive is kept initialized */\r
3448 res = FR_OK;\r
3449 }\r
3450#endif\r
70702af1 3451 }\r
289f6a14 3452 *rfs = (res == FR_OK) ? obj->fs : 0; /* Corresponding filesystem object */\r
70702af1 3453 return res;\r
53668523
L
3454}\r
3455\r
3456\r
3457\r
3458\r
70702af1 3459/*---------------------------------------------------------------------------\r
53668523 3460\r
70702af1 3461 Public Functions (FatFs API)\r
53668523 3462\r
70702af1 3463----------------------------------------------------------------------------*/\r
53668523
L
3464\r
3465\r
3466\r
3467/*-----------------------------------------------------------------------*/\r
3468/* Mount/Unmount a Logical Drive */\r
3469/*-----------------------------------------------------------------------*/\r
3470\r
3471FRESULT f_mount (\r
289f6a14 3472 FATFS* fs, /* Pointer to the filesystem object (NULL:unmount)*/\r
53668523 3473 const TCHAR* path, /* Logical drive number to be mounted/unmounted */\r
70702af1 3474 BYTE opt /* Mode option 0:Do not mount (delayed mount), 1:Mount immediately */\r
53668523
L
3475)\r
3476{\r
3477 FATFS *cfs;\r
3478 int vol;\r
3479 FRESULT res;\r
3480 const TCHAR *rp = path;\r
3481\r
3482\r
70702af1 3483 /* Get logical drive number */\r
53668523
L
3484 vol = get_ldnumber(&rp);\r
3485 if (vol < 0) return FR_INVALID_DRIVE;\r
3486 cfs = FatFs[vol]; /* Pointer to fs object */\r
3487\r
3488 if (cfs) {\r
289f6a14 3489#if FF_FS_LOCK != 0\r
53668523
L
3490 clear_lock(cfs);\r
3491#endif\r
289f6a14 3492#if FF_FS_REENTRANT /* Discard sync object of the current volume */\r
53668523
L
3493 if (!ff_del_syncobj(cfs->sobj)) return FR_INT_ERR;\r
3494#endif\r
3495 cfs->fs_type = 0; /* Clear old fs object */\r
3496 }\r
3497\r
3498 if (fs) {\r
3499 fs->fs_type = 0; /* Clear new fs object */\r
289f6a14 3500#if FF_FS_REENTRANT /* Create sync object for the new volume */\r
53668523
L
3501 if (!ff_cre_syncobj((BYTE)vol, &fs->sobj)) return FR_INT_ERR;\r
3502#endif\r
3503 }\r
3504 FatFs[vol] = fs; /* Register new fs object */\r
3505\r
289f6a14 3506 if (opt == 0) return FR_OK; /* Do not mount now, it will be mounted later */\r
53668523 3507\r
70702af1 3508 res = find_volume(&path, &fs, 0); /* Force mounted the volume */\r
53668523
L
3509 LEAVE_FF(fs, res);\r
3510}\r
3511\r
3512\r
3513\r
3514\r
3515/*-----------------------------------------------------------------------*/\r
3516/* Open or Create a File */\r
3517/*-----------------------------------------------------------------------*/\r
3518\r
3519FRESULT f_open (\r
3520 FIL* fp, /* Pointer to the blank file object */\r
3521 const TCHAR* path, /* Pointer to the file name */\r
3522 BYTE mode /* Access mode and file open mode flags */\r
3523)\r
3524{\r
3525 FRESULT res;\r
3526 DIR dj;\r
70702af1 3527 FATFS *fs;\r
289f6a14 3528#if !FF_FS_READONLY\r
70702af1
L
3529 DWORD dw, cl, bcs, clst, sc;\r
3530 FSIZE_t ofs;\r
3531#endif\r
3532 DEF_NAMBUF\r
53668523
L
3533\r
3534\r
3535 if (!fp) return FR_INVALID_OBJECT;\r
53668523 3536\r
289f6a14
L
3537 /* Get logical drive number */\r
3538 mode &= FF_FS_READONLY ? FA_READ : FA_READ | FA_WRITE | FA_CREATE_ALWAYS | FA_CREATE_NEW | FA_OPEN_ALWAYS | FA_OPEN_APPEND;\r
70702af1 3539 res = find_volume(&path, &fs, mode);\r
53668523 3540 if (res == FR_OK) {\r
70702af1
L
3541 dj.obj.fs = fs;\r
3542 INIT_NAMBUF(fs);\r
53668523 3543 res = follow_path(&dj, path); /* Follow the file path */\r
289f6a14 3544#if !FF_FS_READONLY /* Read/Write configuration */\r
53668523 3545 if (res == FR_OK) {\r
70702af1 3546 if (dj.fn[NSFLAG] & NS_NONAME) { /* Origin directory itself? */\r
53668523 3547 res = FR_INVALID_NAME;\r
70702af1 3548 }\r
289f6a14 3549#if FF_FS_LOCK != 0\r
70702af1 3550 else {\r
289f6a14 3551 res = chk_lock(&dj, (mode & ~FA_READ) ? 1 : 0); /* Check if the file can be used */\r
70702af1 3552 }\r
53668523
L
3553#endif\r
3554 }\r
3555 /* Create or Open a file */\r
3556 if (mode & (FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW)) {\r
53668523 3557 if (res != FR_OK) { /* No file, create new */\r
289f6a14
L
3558 if (res == FR_NO_FILE) { /* There is no file to open, create a new entry */\r
3559#if FF_FS_LOCK != 0\r
53668523
L
3560 res = enq_lock() ? dir_register(&dj) : FR_TOO_MANY_OPEN_FILES;\r
3561#else\r
3562 res = dir_register(&dj);\r
3563#endif\r
289f6a14 3564 }\r
53668523 3565 mode |= FA_CREATE_ALWAYS; /* File is created */\r
53668523 3566 }\r
289f6a14 3567 else { /* Any object with the same name is already existing */\r
70702af1 3568 if (dj.obj.attr & (AM_RDO | AM_DIR)) { /* Cannot overwrite it (R/O or DIR) */\r
53668523
L
3569 res = FR_DENIED;\r
3570 } else {\r
70702af1 3571 if (mode & FA_CREATE_NEW) res = FR_EXIST; /* Cannot create as new file */\r
53668523
L
3572 }\r
3573 }\r
289f6a14
L
3574 if (res == FR_OK && (mode & FA_CREATE_ALWAYS)) { /* Truncate the file if overwrite mode */\r
3575#if FF_FS_EXFAT\r
70702af1
L
3576 if (fs->fs_type == FS_EXFAT) {\r
3577 /* Get current allocation info */\r
3578 fp->obj.fs = fs;\r
289f6a14
L
3579 init_alloc_info(fs, &fp->obj);\r
3580 /* Set directory entry block initial state */\r
3581 mem_set(fs->dirbuf + 2, 0, 30); /* Clear 85 entry except for NumSec */\r
3582 mem_set(fs->dirbuf + 38, 0, 26); /* Clear C0 entry except for NumName and NameHash */\r
3583 fs->dirbuf[XDIR_Attr] = AM_ARC;\r
3584 st_dword(fs->dirbuf + XDIR_CrtTime, GET_FATTIME());\r
70702af1
L
3585 fs->dirbuf[XDIR_GenFlags] = 1;\r
3586 res = store_xdir(&dj);\r
289f6a14 3587 if (res == FR_OK && fp->obj.sclust != 0) { /* Remove the cluster chain if exist */\r
70702af1
L
3588 res = remove_chain(&fp->obj, fp->obj.sclust, 0);\r
3589 fs->last_clst = fp->obj.sclust - 1; /* Reuse the cluster hole */\r
3590 }\r
3591 } else\r
3592#endif\r
3593 {\r
289f6a14
L
3594 /* Set directory entry initial state */\r
3595 cl = ld_clust(fs, dj.dir); /* Get current cluster chain */\r
3596 st_dword(dj.dir + DIR_CrtTime, GET_FATTIME()); /* Set created time */\r
70702af1 3597 dj.dir[DIR_Attr] = AM_ARC; /* Reset attribute */\r
70702af1
L
3598 st_clust(fs, dj.dir, 0); /* Reset file allocation info */\r
3599 st_dword(dj.dir + DIR_FileSize, 0);\r
3600 fs->wflag = 1;\r
289f6a14 3601 if (cl != 0) { /* Remove the cluster chain if exist */\r
70702af1
L
3602 dw = fs->winsect;\r
3603 res = remove_chain(&dj.obj, cl, 0);\r
3604 if (res == FR_OK) {\r
3605 res = move_window(fs, dw);\r
3606 fs->last_clst = cl - 1; /* Reuse the cluster hole */\r
3607 }\r
53668523
L
3608 }\r
3609 }\r
3610 }\r
3611 }\r
3612 else { /* Open an existing file */\r
289f6a14
L
3613 if (res == FR_OK) { /* Is the object exsiting? */\r
3614 if (dj.obj.attr & AM_DIR) { /* File open against a directory */\r
53668523
L
3615 res = FR_NO_FILE;\r
3616 } else {\r
289f6a14 3617 if ((mode & FA_WRITE) && (dj.obj.attr & AM_RDO)) { /* Write mode open against R/O file */\r
53668523 3618 res = FR_DENIED;\r
70702af1 3619 }\r
53668523
L
3620 }\r
3621 }\r
3622 }\r
3623 if (res == FR_OK) {\r
289f6a14 3624 if (mode & FA_CREATE_ALWAYS) mode |= FA_MODIFIED; /* Set file change flag if created or overwritten */\r
70702af1
L
3625 fp->dir_sect = fs->winsect; /* Pointer to the directory entry */\r
3626 fp->dir_ptr = dj.dir;\r
289f6a14
L
3627#if FF_FS_LOCK != 0\r
3628 fp->obj.lockid = inc_lock(&dj, (mode & ~FA_READ) ? 1 : 0); /* Lock the file for this session */\r
3629 if (fp->obj.lockid == 0) res = FR_INT_ERR;\r
53668523
L
3630#endif\r
3631 }\r
70702af1
L
3632#else /* R/O configuration */\r
3633 if (res == FR_OK) {\r
289f6a14 3634 if (dj.fn[NSFLAG] & NS_NONAME) { /* Is it origin directory itself? */\r
53668523
L
3635 res = FR_INVALID_NAME;\r
3636 } else {\r
289f6a14 3637 if (dj.obj.attr & AM_DIR) { /* Is it a directory? */\r
53668523 3638 res = FR_NO_FILE;\r
70702af1 3639 }\r
53668523
L
3640 }\r
3641 }\r
3642#endif\r
70702af1
L
3643\r
3644 if (res == FR_OK) {\r
289f6a14 3645#if FF_FS_EXFAT\r
70702af1 3646 if (fs->fs_type == FS_EXFAT) {\r
289f6a14 3647 fp->obj.c_scl = dj.obj.sclust; /* Get containing directory info */\r
70702af1
L
3648 fp->obj.c_size = ((DWORD)dj.obj.objsize & 0xFFFFFF00) | dj.obj.stat;\r
3649 fp->obj.c_ofs = dj.blk_ofs;\r
289f6a14 3650 init_alloc_info(fs, &fp->obj);\r
70702af1
L
3651 } else\r
3652#endif\r
3653 {\r
289f6a14 3654 fp->obj.sclust = ld_clust(fs, dj.dir); /* Get object allocation info */\r
70702af1
L
3655 fp->obj.objsize = ld_dword(dj.dir + DIR_FileSize);\r
3656 }\r
289f6a14 3657#if FF_USE_FASTSEEK\r
70702af1
L
3658 fp->cltbl = 0; /* Disable fast seek mode */\r
3659#endif\r
3660 fp->obj.fs = fs; /* Validate the file object */\r
3661 fp->obj.id = fs->id;\r
3662 fp->flag = mode; /* Set file access mode */\r
3663 fp->err = 0; /* Clear error flag */\r
3664 fp->sect = 0; /* Invalidate current data sector */\r
3665 fp->fptr = 0; /* Set file pointer top of the file */\r
289f6a14
L
3666#if !FF_FS_READONLY\r
3667#if !FF_FS_TINY\r
3668 mem_set(fp->buf, 0, FF_MAX_SS); /* Clear sector buffer */\r
70702af1
L
3669#endif\r
3670 if ((mode & FA_SEEKEND) && fp->obj.objsize > 0) { /* Seek to end of file if FA_OPEN_APPEND is specified */\r
3671 fp->fptr = fp->obj.objsize; /* Offset to seek */\r
3672 bcs = (DWORD)fs->csize * SS(fs); /* Cluster size in byte */\r
3673 clst = fp->obj.sclust; /* Follow the cluster chain */\r
3674 for (ofs = fp->obj.objsize; res == FR_OK && ofs > bcs; ofs -= bcs) {\r
3675 clst = get_fat(&fp->obj, clst);\r
3676 if (clst <= 1) res = FR_INT_ERR;\r
3677 if (clst == 0xFFFFFFFF) res = FR_DISK_ERR;\r
3678 }\r
3679 fp->clust = clst;\r
3680 if (res == FR_OK && ofs % SS(fs)) { /* Fill sector buffer if not on the sector boundary */\r
289f6a14 3681 if ((sc = clst2sect(fs, clst)) == 0) {\r
70702af1
L
3682 res = FR_INT_ERR;\r
3683 } else {\r
3684 fp->sect = sc + (DWORD)(ofs / SS(fs));\r
289f6a14
L
3685#if !FF_FS_TINY\r
3686 if (disk_read(fs->pdrv, fp->buf, fp->sect, 1) != RES_OK) res = FR_DISK_ERR;\r
70702af1
L
3687#endif\r
3688 }\r
3689 }\r
3690 }\r
53668523 3691#endif\r
53668523 3692 }\r
70702af1
L
3693\r
3694 FREE_NAMBUF();\r
53668523
L
3695 }\r
3696\r
70702af1
L
3697 if (res != FR_OK) fp->obj.fs = 0; /* Invalidate file object on error */\r
3698\r
3699 LEAVE_FF(fs, res);\r
53668523
L
3700}\r
3701\r
3702\r
3703\r
3704\r
3705/*-----------------------------------------------------------------------*/\r
3706/* Read File */\r
3707/*-----------------------------------------------------------------------*/\r
3708\r
3709FRESULT f_read (\r
70702af1
L
3710 FIL* fp, /* Pointer to the file object */\r
3711 void* buff, /* Pointer to data buffer */\r
3712 UINT btr, /* Number of bytes to read */\r
3713 UINT* br /* Pointer to number of bytes read */\r
53668523
L
3714)\r
3715{\r
3716 FRESULT res;\r
70702af1
L
3717 FATFS *fs;\r
3718 DWORD clst, sect;\r
3719 FSIZE_t remain;\r
3720 UINT rcnt, cc, csect;\r
3721 BYTE *rbuff = (BYTE*)buff;\r
53668523
L
3722\r
3723\r
3724 *br = 0; /* Clear read byte counter */\r
70702af1
L
3725 res = validate(&fp->obj, &fs); /* Check validity of the file object */\r
3726 if (res != FR_OK || (res = (FRESULT)fp->err) != FR_OK) LEAVE_FF(fs, res); /* Check validity */\r
3727 if (!(fp->flag & FA_READ)) LEAVE_FF(fs, FR_DENIED); /* Check access mode */\r
3728 remain = fp->obj.objsize - fp->fptr;\r
53668523
L
3729 if (btr > remain) btr = (UINT)remain; /* Truncate btr by remaining bytes */\r
3730\r
289f6a14
L
3731 for ( ; btr; /* Repeat until btr bytes read */\r
3732 btr -= rcnt, *br += rcnt, rbuff += rcnt, fp->fptr += rcnt) {\r
70702af1
L
3733 if (fp->fptr % SS(fs) == 0) { /* On the sector boundary? */\r
3734 csect = (UINT)(fp->fptr / SS(fs) & (fs->csize - 1)); /* Sector offset in the cluster */\r
3735 if (csect == 0) { /* On the cluster boundary? */\r
53668523 3736 if (fp->fptr == 0) { /* On the top of the file? */\r
70702af1 3737 clst = fp->obj.sclust; /* Follow cluster chain from the origin */\r
53668523 3738 } else { /* Middle or end of the file */\r
289f6a14 3739#if FF_USE_FASTSEEK\r
70702af1 3740 if (fp->cltbl) {\r
53668523 3741 clst = clmt_clust(fp, fp->fptr); /* Get cluster# from the CLMT */\r
70702af1 3742 } else\r
53668523 3743#endif\r
70702af1
L
3744 {\r
3745 clst = get_fat(&fp->obj, fp->clust); /* Follow cluster chain on the FAT */\r
3746 }\r
53668523 3747 }\r
70702af1
L
3748 if (clst < 2) ABORT(fs, FR_INT_ERR);\r
3749 if (clst == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR);\r
53668523
L
3750 fp->clust = clst; /* Update current cluster */\r
3751 }\r
289f6a14
L
3752 sect = clst2sect(fs, fp->clust); /* Get current sector */\r
3753 if (sect == 0) ABORT(fs, FR_INT_ERR);\r
53668523 3754 sect += csect;\r
70702af1 3755 cc = btr / SS(fs); /* When remaining bytes >= sector size, */\r
289f6a14 3756 if (cc > 0) { /* Read maximum contiguous sectors directly */\r
70702af1
L
3757 if (csect + cc > fs->csize) { /* Clip at cluster boundary */\r
3758 cc = fs->csize - csect;\r
3759 }\r
289f6a14
L
3760 if (disk_read(fs->pdrv, rbuff, sect, cc) != RES_OK) ABORT(fs, FR_DISK_ERR);\r
3761#if !FF_FS_READONLY && FF_FS_MINIMIZE <= 2 /* Replace one of the read sectors with cached data if it contains a dirty sector */\r
3762#if FF_FS_TINY\r
70702af1
L
3763 if (fs->wflag && fs->winsect - sect < cc) {\r
3764 mem_cpy(rbuff + ((fs->winsect - sect) * SS(fs)), fs->win, SS(fs));\r
3765 }\r
53668523 3766#else\r
70702af1
L
3767 if ((fp->flag & FA_DIRTY) && fp->sect - sect < cc) {\r
3768 mem_cpy(rbuff + ((fp->sect - sect) * SS(fs)), fp->buf, SS(fs));\r
3769 }\r
53668523
L
3770#endif\r
3771#endif\r
70702af1 3772 rcnt = SS(fs) * cc; /* Number of bytes transferred */\r
53668523
L
3773 continue;\r
3774 }\r
289f6a14 3775#if !FF_FS_TINY\r
70702af1 3776 if (fp->sect != sect) { /* Load data sector if not in cache */\r
289f6a14 3777#if !FF_FS_READONLY\r
70702af1 3778 if (fp->flag & FA_DIRTY) { /* Write-back dirty sector cache */\r
289f6a14 3779 if (disk_write(fs->pdrv, fp->buf, fp->sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR);\r
70702af1 3780 fp->flag &= (BYTE)~FA_DIRTY;\r
53668523
L
3781 }\r
3782#endif\r
289f6a14 3783 if (disk_read(fs->pdrv, fp->buf, sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR); /* Fill sector cache */\r
53668523
L
3784 }\r
3785#endif\r
70702af1 3786 fp->sect = sect;\r
53668523 3787 }\r
70702af1
L
3788 rcnt = SS(fs) - (UINT)fp->fptr % SS(fs); /* Number of bytes left in the sector */\r
3789 if (rcnt > btr) rcnt = btr; /* Clip it by btr if needed */\r
289f6a14 3790#if FF_FS_TINY\r
70702af1
L
3791 if (move_window(fs, fp->sect) != FR_OK) ABORT(fs, FR_DISK_ERR); /* Move sector window */\r
3792 mem_cpy(rbuff, fs->win + fp->fptr % SS(fs), rcnt); /* Extract partial sector */\r
53668523 3793#else\r
70702af1 3794 mem_cpy(rbuff, fp->buf + fp->fptr % SS(fs), rcnt); /* Extract partial sector */\r
53668523
L
3795#endif\r
3796 }\r
3797\r
70702af1 3798 LEAVE_FF(fs, FR_OK);\r
53668523
L
3799}\r
3800\r
3801\r
3802\r
3803\r
289f6a14 3804#if !FF_FS_READONLY\r
53668523
L
3805/*-----------------------------------------------------------------------*/\r
3806/* Write File */\r
3807/*-----------------------------------------------------------------------*/\r
3808\r
3809FRESULT f_write (\r
3810 FIL* fp, /* Pointer to the file object */\r
70702af1 3811 const void* buff, /* Pointer to the data to be written */\r
53668523
L
3812 UINT btw, /* Number of bytes to write */\r
3813 UINT* bw /* Pointer to number of bytes written */\r
3814)\r
3815{\r
3816 FRESULT res;\r
70702af1 3817 FATFS *fs;\r
53668523 3818 DWORD clst, sect;\r
70702af1 3819 UINT wcnt, cc, csect;\r
53668523 3820 const BYTE *wbuff = (const BYTE*)buff;\r
53668523
L
3821\r
3822\r
3823 *bw = 0; /* Clear write byte counter */\r
70702af1
L
3824 res = validate(&fp->obj, &fs); /* Check validity of the file object */\r
3825 if (res != FR_OK || (res = (FRESULT)fp->err) != FR_OK) LEAVE_FF(fs, res); /* Check validity */\r
3826 if (!(fp->flag & FA_WRITE)) LEAVE_FF(fs, FR_DENIED); /* Check access mode */\r
53668523 3827\r
289f6a14
L
3828 /* Check fptr wrap-around (file size cannot reach 4 GiB at FAT volume) */\r
3829 if ((!FF_FS_EXFAT || fs->fs_type != FS_EXFAT) && (DWORD)(fp->fptr + btw) < (DWORD)fp->fptr) {\r
70702af1
L
3830 btw = (UINT)(0xFFFFFFFF - (DWORD)fp->fptr);\r
3831 }\r
53668523
L
3832\r
3833 for ( ; btw; /* Repeat until all data written */\r
289f6a14 3834 btw -= wcnt, *bw += wcnt, wbuff += wcnt, fp->fptr += wcnt, fp->obj.objsize = (fp->fptr > fp->obj.objsize) ? fp->fptr : fp->obj.objsize) {\r
70702af1
L
3835 if (fp->fptr % SS(fs) == 0) { /* On the sector boundary? */\r
3836 csect = (UINT)(fp->fptr / SS(fs)) & (fs->csize - 1); /* Sector offset in the cluster */\r
3837 if (csect == 0) { /* On the cluster boundary? */\r
53668523 3838 if (fp->fptr == 0) { /* On the top of the file? */\r
70702af1
L
3839 clst = fp->obj.sclust; /* Follow from the origin */\r
3840 if (clst == 0) { /* If no cluster is allocated, */\r
3841 clst = create_chain(&fp->obj, 0); /* create a new cluster chain */\r
3842 }\r
3843 } else { /* On the middle or end of the file */\r
289f6a14 3844#if FF_USE_FASTSEEK\r
70702af1 3845 if (fp->cltbl) {\r
53668523 3846 clst = clmt_clust(fp, fp->fptr); /* Get cluster# from the CLMT */\r
70702af1 3847 } else\r
53668523 3848#endif\r
70702af1
L
3849 {\r
3850 clst = create_chain(&fp->obj, fp->clust); /* Follow or stretch cluster chain on the FAT */\r
3851 }\r
53668523
L
3852 }\r
3853 if (clst == 0) break; /* Could not allocate a new cluster (disk full) */\r
70702af1
L
3854 if (clst == 1) ABORT(fs, FR_INT_ERR);\r
3855 if (clst == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR);\r
53668523 3856 fp->clust = clst; /* Update current cluster */\r
70702af1 3857 if (fp->obj.sclust == 0) fp->obj.sclust = clst; /* Set start cluster if the first write */\r
53668523 3858 }\r
289f6a14 3859#if FF_FS_TINY\r
70702af1 3860 if (fs->winsect == fp->sect && sync_window(fs) != FR_OK) ABORT(fs, FR_DISK_ERR); /* Write-back sector cache */\r
53668523 3861#else\r
70702af1 3862 if (fp->flag & FA_DIRTY) { /* Write-back sector cache */\r
289f6a14 3863 if (disk_write(fs->pdrv, fp->buf, fp->sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR);\r
70702af1 3864 fp->flag &= (BYTE)~FA_DIRTY;\r
53668523
L
3865 }\r
3866#endif\r
289f6a14
L
3867 sect = clst2sect(fs, fp->clust); /* Get current sector */\r
3868 if (sect == 0) ABORT(fs, FR_INT_ERR);\r
53668523 3869 sect += csect;\r
70702af1 3870 cc = btw / SS(fs); /* When remaining bytes >= sector size, */\r
289f6a14 3871 if (cc > 0) { /* Write maximum contiguous sectors directly */\r
70702af1
L
3872 if (csect + cc > fs->csize) { /* Clip at cluster boundary */\r
3873 cc = fs->csize - csect;\r
3874 }\r
289f6a14
L
3875 if (disk_write(fs->pdrv, wbuff, sect, cc) != RES_OK) ABORT(fs, FR_DISK_ERR);\r
3876#if FF_FS_MINIMIZE <= 2\r
3877#if FF_FS_TINY\r
70702af1
L
3878 if (fs->winsect - sect < cc) { /* Refill sector cache if it gets invalidated by the direct write */\r
3879 mem_cpy(fs->win, wbuff + ((fs->winsect - sect) * SS(fs)), SS(fs));\r
3880 fs->wflag = 0;\r
53668523
L
3881 }\r
3882#else\r
70702af1
L
3883 if (fp->sect - sect < cc) { /* Refill sector cache if it gets invalidated by the direct write */\r
3884 mem_cpy(fp->buf, wbuff + ((fp->sect - sect) * SS(fs)), SS(fs));\r
3885 fp->flag &= (BYTE)~FA_DIRTY;\r
53668523
L
3886 }\r
3887#endif\r
3888#endif\r
70702af1 3889 wcnt = SS(fs) * cc; /* Number of bytes transferred */\r
53668523
L
3890 continue;\r
3891 }\r
289f6a14 3892#if FF_FS_TINY\r
70702af1
L
3893 if (fp->fptr >= fp->obj.objsize) { /* Avoid silly cache filling on the growing edge */\r
3894 if (sync_window(fs) != FR_OK) ABORT(fs, FR_DISK_ERR);\r
3895 fs->winsect = sect;\r
53668523
L
3896 }\r
3897#else\r
70702af1
L
3898 if (fp->sect != sect && /* Fill sector cache with file data */\r
3899 fp->fptr < fp->obj.objsize &&\r
289f6a14 3900 disk_read(fs->pdrv, fp->buf, sect, 1) != RES_OK) {\r
70702af1 3901 ABORT(fs, FR_DISK_ERR);\r
53668523
L
3902 }\r
3903#endif\r
70702af1 3904 fp->sect = sect;\r
53668523 3905 }\r
70702af1
L
3906 wcnt = SS(fs) - (UINT)fp->fptr % SS(fs); /* Number of bytes left in the sector */\r
3907 if (wcnt > btw) wcnt = btw; /* Clip it by btw if needed */\r
289f6a14 3908#if FF_FS_TINY\r
70702af1
L
3909 if (move_window(fs, fp->sect) != FR_OK) ABORT(fs, FR_DISK_ERR); /* Move sector window */\r
3910 mem_cpy(fs->win + fp->fptr % SS(fs), wbuff, wcnt); /* Fit data to the sector */\r
3911 fs->wflag = 1;\r
53668523 3912#else\r
70702af1
L
3913 mem_cpy(fp->buf + fp->fptr % SS(fs), wbuff, wcnt); /* Fit data to the sector */\r
3914 fp->flag |= FA_DIRTY;\r
53668523
L
3915#endif\r
3916 }\r
3917\r
70702af1 3918 fp->flag |= FA_MODIFIED; /* Set file change flag */\r
53668523 3919\r
70702af1 3920 LEAVE_FF(fs, FR_OK);\r
53668523
L
3921}\r
3922\r
3923\r
3924\r
3925\r
3926/*-----------------------------------------------------------------------*/\r
3927/* Synchronize the File */\r
3928/*-----------------------------------------------------------------------*/\r
3929\r
3930FRESULT f_sync (\r
3931 FIL* fp /* Pointer to the file object */\r
3932)\r
3933{\r
3934 FRESULT res;\r
70702af1 3935 FATFS *fs;\r
53668523
L
3936 DWORD tm;\r
3937 BYTE *dir;\r
3938\r
3939\r
70702af1 3940 res = validate(&fp->obj, &fs); /* Check validity of the file object */\r
53668523 3941 if (res == FR_OK) {\r
70702af1 3942 if (fp->flag & FA_MODIFIED) { /* Is there any change to the file? */\r
289f6a14 3943#if !FF_FS_TINY\r
70702af1 3944 if (fp->flag & FA_DIRTY) { /* Write-back cached data if needed */\r
289f6a14 3945 if (disk_write(fs->pdrv, fp->buf, fp->sect, 1) != RES_OK) LEAVE_FF(fs, FR_DISK_ERR);\r
70702af1 3946 fp->flag &= (BYTE)~FA_DIRTY;\r
53668523
L
3947 }\r
3948#endif\r
3949 /* Update the directory entry */\r
70702af1 3950 tm = GET_FATTIME(); /* Modified time */\r
289f6a14 3951#if FF_FS_EXFAT\r
70702af1 3952 if (fs->fs_type == FS_EXFAT) {\r
289f6a14
L
3953 res = fill_first_frag(&fp->obj); /* Fill first fragment on the FAT if needed */\r
3954 if (res == FR_OK) {\r
3955 res = fill_last_frag(&fp->obj, fp->clust, 0xFFFFFFFF); /* Fill last fragment on the FAT if needed */\r
3956 }\r
70702af1
L
3957 if (res == FR_OK) {\r
3958 DIR dj;\r
289f6a14 3959 DEF_NAMBUF\r
70702af1
L
3960\r
3961 INIT_NAMBUF(fs);\r
289f6a14 3962 res = load_obj_xdir(&dj, &fp->obj); /* Load directory entry block */\r
70702af1 3963 if (res == FR_OK) {\r
289f6a14
L
3964 fs->dirbuf[XDIR_Attr] |= AM_ARC; /* Set archive attribute to indicate that the file has been changed */\r
3965 fs->dirbuf[XDIR_GenFlags] = fp->obj.stat | 1; /* Update file allocation information */\r
70702af1
L
3966 st_dword(fs->dirbuf + XDIR_FstClus, fp->obj.sclust);\r
3967 st_qword(fs->dirbuf + XDIR_FileSize, fp->obj.objsize);\r
3968 st_qword(fs->dirbuf + XDIR_ValidFileSize, fp->obj.objsize);\r
3969 st_dword(fs->dirbuf + XDIR_ModTime, tm); /* Update modified time */\r
3970 fs->dirbuf[XDIR_ModTime10] = 0;\r
3971 st_dword(fs->dirbuf + XDIR_AccTime, 0);\r
3972 res = store_xdir(&dj); /* Restore it to the directory */\r
3973 if (res == FR_OK) {\r
3974 res = sync_fs(fs);\r
3975 fp->flag &= (BYTE)~FA_MODIFIED;\r
3976 }\r
3977 }\r
3978 FREE_NAMBUF();\r
3979 }\r
3980 } else\r
3981#endif\r
3982 {\r
3983 res = move_window(fs, fp->dir_sect);\r
3984 if (res == FR_OK) {\r
3985 dir = fp->dir_ptr;\r
289f6a14
L
3986 dir[DIR_Attr] |= AM_ARC; /* Set archive attribute to indicate that the file has been changed */\r
3987 st_clust(fp->obj.fs, dir, fp->obj.sclust); /* Update file allocation information */\r
70702af1
L
3988 st_dword(dir + DIR_FileSize, (DWORD)fp->obj.objsize); /* Update file size */\r
3989 st_dword(dir + DIR_ModTime, tm); /* Update modified time */\r
3990 st_word(dir + DIR_LstAccDate, 0);\r
3991 fs->wflag = 1;\r
3992 res = sync_fs(fs); /* Restore it to the directory */\r
3993 fp->flag &= (BYTE)~FA_MODIFIED;\r
3994 }\r
53668523
L
3995 }\r
3996 }\r
3997 }\r
3998\r
70702af1 3999 LEAVE_FF(fs, res);\r
53668523
L
4000}\r
4001\r
289f6a14 4002#endif /* !FF_FS_READONLY */\r
53668523
L
4003\r
4004\r
4005\r
4006\r
4007/*-----------------------------------------------------------------------*/\r
4008/* Close File */\r
4009/*-----------------------------------------------------------------------*/\r
4010\r
4011FRESULT f_close (\r
70702af1 4012 FIL* fp /* Pointer to the file object to be closed */\r
53668523
L
4013)\r
4014{\r
4015 FRESULT res;\r
70702af1 4016 FATFS *fs;\r
53668523 4017\r
289f6a14 4018#if !FF_FS_READONLY\r
53668523
L
4019 res = f_sync(fp); /* Flush cached data */\r
4020 if (res == FR_OK)\r
4021#endif\r
4022 {\r
70702af1 4023 res = validate(&fp->obj, &fs); /* Lock volume */\r
53668523 4024 if (res == FR_OK) {\r
289f6a14
L
4025#if FF_FS_LOCK != 0\r
4026 res = dec_lock(fp->obj.lockid); /* Decrement file open counter */\r
4027 if (res == FR_OK) fp->obj.fs = 0; /* Invalidate file object */\r
4028#else\r
4029 fp->obj.fs = 0; /* Invalidate file object */\r
53668523 4030#endif\r
289f6a14 4031#if FF_FS_REENTRANT\r
53668523
L
4032 unlock_fs(fs, FR_OK); /* Unlock volume */\r
4033#endif\r
4034 }\r
4035 }\r
4036 return res;\r
4037}\r
4038\r
4039\r
4040\r
4041\r
289f6a14 4042#if FF_FS_RPATH >= 1\r
53668523
L
4043/*-----------------------------------------------------------------------*/\r
4044/* Change Current Directory or Current Drive, Get Current Directory */\r
4045/*-----------------------------------------------------------------------*/\r
4046\r
53668523 4047FRESULT f_chdrive (\r
289f6a14 4048 const TCHAR* path /* Drive number to set */\r
53668523
L
4049)\r
4050{\r
4051 int vol;\r
4052\r
4053\r
70702af1 4054 /* Get logical drive number */\r
53668523
L
4055 vol = get_ldnumber(&path);\r
4056 if (vol < 0) return FR_INVALID_DRIVE;\r
70702af1 4057 CurrVol = (BYTE)vol; /* Set it as current volume */\r
53668523
L
4058\r
4059 return FR_OK;\r
4060}\r
289f6a14 4061\r
53668523
L
4062\r
4063\r
4064FRESULT f_chdir (\r
4065 const TCHAR* path /* Pointer to the directory path */\r
4066)\r
4067{\r
289f6a14
L
4068#if FF_STR_VOLUME_ID == 2\r
4069 UINT i;\r
4070#endif\r
53668523
L
4071 FRESULT res;\r
4072 DIR dj;\r
70702af1
L
4073 FATFS *fs;\r
4074 DEF_NAMBUF\r
53668523 4075\r
289f6a14 4076\r
70702af1
L
4077 /* Get logical drive */\r
4078 res = find_volume(&path, &fs, 0);\r
53668523 4079 if (res == FR_OK) {\r
70702af1
L
4080 dj.obj.fs = fs;\r
4081 INIT_NAMBUF(fs);\r
53668523 4082 res = follow_path(&dj, path); /* Follow the path */\r
53668523 4083 if (res == FR_OK) { /* Follow completed */\r
289f6a14
L
4084 if (dj.fn[NSFLAG] & NS_NONAME) { /* Is it the start directory itself? */\r
4085 fs->cdir = dj.obj.sclust;\r
4086#if FF_FS_EXFAT\r
70702af1
L
4087 if (fs->fs_type == FS_EXFAT) {\r
4088 fs->cdc_scl = dj.obj.c_scl;\r
4089 fs->cdc_size = dj.obj.c_size;\r
4090 fs->cdc_ofs = dj.obj.c_ofs;\r
4091 }\r
4092#endif\r
53668523 4093 } else {\r
70702af1 4094 if (dj.obj.attr & AM_DIR) { /* It is a sub-directory */\r
289f6a14 4095#if FF_FS_EXFAT\r
70702af1
L
4096 if (fs->fs_type == FS_EXFAT) {\r
4097 fs->cdir = ld_dword(fs->dirbuf + XDIR_FstClus); /* Sub-directory cluster */\r
4098 fs->cdc_scl = dj.obj.sclust; /* Save containing directory information */\r
4099 fs->cdc_size = ((DWORD)dj.obj.objsize & 0xFFFFFF00) | dj.obj.stat;\r
4100 fs->cdc_ofs = dj.blk_ofs;\r
4101 } else\r
4102#endif\r
4103 {\r
4104 fs->cdir = ld_clust(fs, dj.dir); /* Sub-directory cluster */\r
4105 }\r
4106 } else {\r
53668523 4107 res = FR_NO_PATH; /* Reached but a file */\r
70702af1 4108 }\r
53668523
L
4109 }\r
4110 }\r
70702af1 4111 FREE_NAMBUF();\r
53668523 4112 if (res == FR_NO_FILE) res = FR_NO_PATH;\r
289f6a14
L
4113#if FF_STR_VOLUME_ID == 2 /* Also current drive is changed at Unix style volume ID */\r
4114 if (res == FR_OK) {\r
4115 for (i = FF_VOLUMES - 1; i && fs != FatFs[i]; i--) ; /* Set current drive */\r
4116 CurrVol = (BYTE)i;\r
4117 }\r
4118#endif\r
53668523
L
4119 }\r
4120\r
70702af1 4121 LEAVE_FF(fs, res);\r
53668523
L
4122}\r
4123\r
4124\r
289f6a14 4125#if FF_FS_RPATH >= 2\r
53668523
L
4126FRESULT f_getcwd (\r
4127 TCHAR* buff, /* Pointer to the directory path */\r
289f6a14 4128 UINT len /* Size of buff in unit of TCHAR */\r
53668523
L
4129)\r
4130{\r
4131 FRESULT res;\r
4132 DIR dj;\r
70702af1 4133 FATFS *fs;\r
53668523
L
4134 UINT i, n;\r
4135 DWORD ccl;\r
289f6a14
L
4136 TCHAR *tp = buff;\r
4137#if FF_VOLUMES >= 2\r
4138 UINT vl;\r
4139#endif\r
4140#if FF_STR_VOLUME_ID\r
4141 const char *vp;\r
4142#endif\r
53668523 4143 FILINFO fno;\r
70702af1 4144 DEF_NAMBUF\r
53668523
L
4145\r
4146\r
70702af1
L
4147 /* Get logical drive */\r
4148 res = find_volume((const TCHAR**)&buff, &fs, 0); /* Get current volume */\r
53668523 4149 if (res == FR_OK) {\r
70702af1
L
4150 dj.obj.fs = fs;\r
4151 INIT_NAMBUF(fs);\r
289f6a14
L
4152\r
4153 /* Follow parent directories and create the path */\r
53668523 4154 i = len; /* Bottom of buffer (directory stack base) */\r
289f6a14 4155 if (!FF_FS_EXFAT || fs->fs_type != FS_EXFAT) { /* (Cannot do getcwd on exFAT and returns root path) */\r
70702af1
L
4156 dj.obj.sclust = fs->cdir; /* Start to follow upper directory from current directory */\r
4157 while ((ccl = dj.obj.sclust) != 0) { /* Repeat while current directory is a sub-directory */\r
4158 res = dir_sdi(&dj, 1 * SZDIRE); /* Get parent directory */\r
53668523 4159 if (res != FR_OK) break;\r
70702af1
L
4160 res = move_window(fs, dj.sect);\r
4161 if (res != FR_OK) break;\r
4162 dj.obj.sclust = ld_clust(fs, dj.dir); /* Goto parent directory */\r
4163 res = dir_sdi(&dj, 0);\r
4164 if (res != FR_OK) break;\r
4165 do { /* Find the entry links to the child directory */\r
289f6a14 4166 res = dir_read_file(&dj);\r
70702af1
L
4167 if (res != FR_OK) break;\r
4168 if (ccl == ld_clust(fs, dj.dir)) break; /* Found the entry */\r
4169 res = dir_next(&dj, 0);\r
4170 } while (res == FR_OK);\r
4171 if (res == FR_NO_FILE) res = FR_INT_ERR;/* It cannot be 'not found'. */\r
4172 if (res != FR_OK) break;\r
4173 get_fileinfo(&dj, &fno); /* Get the directory name and push it to the buffer */\r
289f6a14
L
4174 for (n = 0; fno.fname[n]; n++) ; /* Name length */\r
4175 if (i < n + 1) { /* Insufficient space to store the path name? */\r
70702af1
L
4176 res = FR_NOT_ENOUGH_CORE; break;\r
4177 }\r
289f6a14 4178 while (n) buff[--i] = fno.fname[--n]; /* Stack the name */\r
70702af1 4179 buff[--i] = '/';\r
53668523 4180 }\r
53668523 4181 }\r
53668523 4182 if (res == FR_OK) {\r
289f6a14
L
4183 if (i == len) buff[--i] = '/'; /* Is it the root-directory? */\r
4184#if FF_VOLUMES >= 2 /* Put drive prefix */\r
4185 vl = 0;\r
4186#if FF_STR_VOLUME_ID >= 1 /* String volume ID */\r
4187 for (n = 0, vp = (const char*)VolumeStr[CurrVol]; vp[n]; n++) ;\r
4188 if (i >= n + 2) {\r
4189 if (FF_STR_VOLUME_ID == 2) *tp++ = (TCHAR)'/';\r
4190 for (vl = 0; vl < n; *tp++ = (TCHAR)vp[vl], vl++) ;\r
4191 if (FF_STR_VOLUME_ID == 1) *tp++ = (TCHAR)':';\r
4192 vl++;\r
4193 }\r
4194#else /* Numeric volume ID */\r
4195 if (i >= 3) {\r
4196 *tp++ = (TCHAR)'0' + CurrVol;\r
4197 *tp++ = (TCHAR)':';\r
4198 vl = 2;\r
4199 }\r
4200#endif\r
4201 if (vl == 0) res = FR_NOT_ENOUGH_CORE;\r
4202#endif\r
4203 /* Add current directory path */\r
4204 if (res == FR_OK) {\r
4205 do *tp++ = buff[i++]; while (i < len); /* Copy stacked path string */\r
53668523
L
4206 }\r
4207 }\r
70702af1 4208 FREE_NAMBUF();\r
53668523
L
4209 }\r
4210\r
289f6a14 4211 *tp = 0;\r
70702af1 4212 LEAVE_FF(fs, res);\r
53668523 4213}\r
70702af1 4214\r
289f6a14
L
4215#endif /* FF_FS_RPATH >= 2 */\r
4216#endif /* FF_FS_RPATH >= 1 */\r
53668523
L
4217\r
4218\r
4219\r
289f6a14 4220#if FF_FS_MINIMIZE <= 2\r
53668523 4221/*-----------------------------------------------------------------------*/\r
289f6a14 4222/* Seek File Read/Write Pointer */\r
53668523
L
4223/*-----------------------------------------------------------------------*/\r
4224\r
4225FRESULT f_lseek (\r
4226 FIL* fp, /* Pointer to the file object */\r
70702af1 4227 FSIZE_t ofs /* File pointer from top of file */\r
53668523
L
4228)\r
4229{\r
4230 FRESULT res;\r
70702af1
L
4231 FATFS *fs;\r
4232 DWORD clst, bcs, nsect;\r
4233 FSIZE_t ifptr;\r
289f6a14 4234#if FF_USE_FASTSEEK\r
70702af1
L
4235 DWORD cl, pcl, ncl, tcl, dsc, tlen, ulen, *tbl;\r
4236#endif\r
53668523 4237\r
70702af1 4238 res = validate(&fp->obj, &fs); /* Check validity of the file object */\r
289f6a14
L
4239 if (res == FR_OK) res = (FRESULT)fp->err;\r
4240#if FF_FS_EXFAT && !FF_FS_READONLY\r
4241 if (res == FR_OK && fs->fs_type == FS_EXFAT) {\r
4242 res = fill_last_frag(&fp->obj, fp->clust, 0xFFFFFFFF); /* Fill last fragment on the FAT if needed */\r
4243 }\r
4244#endif\r
4245 if (res != FR_OK) LEAVE_FF(fs, res);\r
4246\r
4247#if FF_USE_FASTSEEK\r
53668523 4248 if (fp->cltbl) { /* Fast seek */\r
53668523
L
4249 if (ofs == CREATE_LINKMAP) { /* Create CLMT */\r
4250 tbl = fp->cltbl;\r
4251 tlen = *tbl++; ulen = 2; /* Given table size and required table size */\r
70702af1 4252 cl = fp->obj.sclust; /* Origin of the chain */\r
289f6a14 4253 if (cl != 0) {\r
53668523
L
4254 do {\r
4255 /* Get a fragment */\r
4256 tcl = cl; ncl = 0; ulen += 2; /* Top, length and used items */\r
4257 do {\r
4258 pcl = cl; ncl++;\r
70702af1
L
4259 cl = get_fat(&fp->obj, cl);\r
4260 if (cl <= 1) ABORT(fs, FR_INT_ERR);\r
4261 if (cl == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR);\r
53668523
L
4262 } while (cl == pcl + 1);\r
4263 if (ulen <= tlen) { /* Store the length and top of the fragment */\r
4264 *tbl++ = ncl; *tbl++ = tcl;\r
4265 }\r
70702af1 4266 } while (cl < fs->n_fatent); /* Repeat until end of chain */\r
53668523
L
4267 }\r
4268 *fp->cltbl = ulen; /* Number of items used */\r
70702af1 4269 if (ulen <= tlen) {\r
53668523 4270 *tbl = 0; /* Terminate table */\r
70702af1 4271 } else {\r
53668523 4272 res = FR_NOT_ENOUGH_CORE; /* Given table size is smaller than required */\r
70702af1 4273 }\r
53668523 4274 } else { /* Fast seek */\r
70702af1 4275 if (ofs > fp->obj.objsize) ofs = fp->obj.objsize; /* Clip offset at the file size */\r
53668523 4276 fp->fptr = ofs; /* Set file pointer */\r
289f6a14 4277 if (ofs > 0) {\r
53668523 4278 fp->clust = clmt_clust(fp, ofs - 1);\r
289f6a14
L
4279 dsc = clst2sect(fs, fp->clust);\r
4280 if (dsc == 0) ABORT(fs, FR_INT_ERR);\r
70702af1
L
4281 dsc += (DWORD)((ofs - 1) / SS(fs)) & (fs->csize - 1);\r
4282 if (fp->fptr % SS(fs) && dsc != fp->sect) { /* Refill sector cache if needed */\r
289f6a14
L
4283#if !FF_FS_TINY\r
4284#if !FF_FS_READONLY\r
70702af1 4285 if (fp->flag & FA_DIRTY) { /* Write-back dirty sector cache */\r
289f6a14 4286 if (disk_write(fs->pdrv, fp->buf, fp->sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR);\r
70702af1 4287 fp->flag &= (BYTE)~FA_DIRTY;\r
53668523
L
4288 }\r
4289#endif\r
289f6a14 4290 if (disk_read(fs->pdrv, fp->buf, dsc, 1) != RES_OK) ABORT(fs, FR_DISK_ERR); /* Load current sector */\r
53668523 4291#endif\r
70702af1 4292 fp->sect = dsc;\r
53668523
L
4293 }\r
4294 }\r
4295 }\r
4296 } else\r
4297#endif\r
4298\r
4299 /* Normal Seek */\r
4300 {\r
289f6a14
L
4301#if FF_FS_EXFAT\r
4302 if (fs->fs_type != FS_EXFAT && ofs >= 0x100000000) ofs = 0xFFFFFFFF; /* Clip at 4 GiB - 1 if at FATxx */\r
53668523 4303#endif\r
289f6a14 4304 if (ofs > fp->obj.objsize && (FF_FS_READONLY || !(fp->flag & FA_WRITE))) { /* In read-only mode, clip offset with the file size */\r
70702af1
L
4305 ofs = fp->obj.objsize;\r
4306 }\r
53668523
L
4307 ifptr = fp->fptr;\r
4308 fp->fptr = nsect = 0;\r
289f6a14 4309 if (ofs > 0) {\r
70702af1 4310 bcs = (DWORD)fs->csize * SS(fs); /* Cluster size (byte) */\r
53668523
L
4311 if (ifptr > 0 &&\r
4312 (ofs - 1) / bcs >= (ifptr - 1) / bcs) { /* When seek to same or following cluster, */\r
70702af1 4313 fp->fptr = (ifptr - 1) & ~(FSIZE_t)(bcs - 1); /* start from the current cluster */\r
53668523
L
4314 ofs -= fp->fptr;\r
4315 clst = fp->clust;\r
4316 } else { /* When seek to back cluster, */\r
70702af1 4317 clst = fp->obj.sclust; /* start from the first cluster */\r
289f6a14 4318#if !FF_FS_READONLY\r
53668523 4319 if (clst == 0) { /* If no cluster chain, create a new chain */\r
70702af1
L
4320 clst = create_chain(&fp->obj, 0);\r
4321 if (clst == 1) ABORT(fs, FR_INT_ERR);\r
4322 if (clst == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR);\r
4323 fp->obj.sclust = clst;\r
53668523
L
4324 }\r
4325#endif\r
4326 fp->clust = clst;\r
4327 }\r
4328 if (clst != 0) {\r
4329 while (ofs > bcs) { /* Cluster following loop */\r
70702af1 4330 ofs -= bcs; fp->fptr += bcs;\r
289f6a14 4331#if !FF_FS_READONLY\r
53668523 4332 if (fp->flag & FA_WRITE) { /* Check if in write mode or not */\r
289f6a14 4333 if (FF_FS_EXFAT && fp->fptr > fp->obj.objsize) { /* No FAT chain object needs correct objsize to generate FAT value */\r
70702af1
L
4334 fp->obj.objsize = fp->fptr;\r
4335 fp->flag |= FA_MODIFIED;\r
4336 }\r
4337 clst = create_chain(&fp->obj, clst); /* Follow chain with forceed stretch */\r
4338 if (clst == 0) { /* Clip file size in case of disk full */\r
4339 ofs = 0; break;\r
53668523
L
4340 }\r
4341 } else\r
4342#endif\r
70702af1
L
4343 {\r
4344 clst = get_fat(&fp->obj, clst); /* Follow cluster chain if not in write mode */\r
4345 }\r
4346 if (clst == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR);\r
4347 if (clst <= 1 || clst >= fs->n_fatent) ABORT(fs, FR_INT_ERR);\r
53668523 4348 fp->clust = clst;\r
53668523
L
4349 }\r
4350 fp->fptr += ofs;\r
70702af1 4351 if (ofs % SS(fs)) {\r
289f6a14
L
4352 nsect = clst2sect(fs, clst); /* Current sector */\r
4353 if (nsect == 0) ABORT(fs, FR_INT_ERR);\r
70702af1 4354 nsect += (DWORD)(ofs / SS(fs));\r
53668523
L
4355 }\r
4356 }\r
4357 }\r
289f6a14 4358 if (!FF_FS_READONLY && fp->fptr > fp->obj.objsize) { /* Set file change flag if the file size is extended */\r
70702af1
L
4359 fp->obj.objsize = fp->fptr;\r
4360 fp->flag |= FA_MODIFIED;\r
4361 }\r
4362 if (fp->fptr % SS(fs) && nsect != fp->sect) { /* Fill sector cache if needed */\r
289f6a14
L
4363#if !FF_FS_TINY\r
4364#if !FF_FS_READONLY\r
70702af1 4365 if (fp->flag & FA_DIRTY) { /* Write-back dirty sector cache */\r
289f6a14 4366 if (disk_write(fs->pdrv, fp->buf, fp->sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR);\r
70702af1 4367 fp->flag &= (BYTE)~FA_DIRTY;\r
53668523
L
4368 }\r
4369#endif\r
289f6a14 4370 if (disk_read(fs->pdrv, fp->buf, nsect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR); /* Fill sector cache */\r
53668523 4371#endif\r
70702af1 4372 fp->sect = nsect;\r
53668523 4373 }\r
53668523
L
4374 }\r
4375\r
70702af1 4376 LEAVE_FF(fs, res);\r
53668523
L
4377}\r
4378\r
4379\r
4380\r
289f6a14 4381#if FF_FS_MINIMIZE <= 1\r
53668523
L
4382/*-----------------------------------------------------------------------*/\r
4383/* Create a Directory Object */\r
4384/*-----------------------------------------------------------------------*/\r
4385\r
4386FRESULT f_opendir (\r
4387 DIR* dp, /* Pointer to directory object to create */\r
4388 const TCHAR* path /* Pointer to the directory path */\r
4389)\r
4390{\r
4391 FRESULT res;\r
70702af1 4392 FATFS *fs;\r
70702af1 4393 DEF_NAMBUF\r
53668523
L
4394\r
4395\r
4396 if (!dp) return FR_INVALID_OBJECT;\r
4397\r
70702af1 4398 /* Get logical drive */\r
70702af1 4399 res = find_volume(&path, &fs, 0);\r
53668523 4400 if (res == FR_OK) {\r
289f6a14 4401 dp->obj.fs = fs;\r
70702af1 4402 INIT_NAMBUF(fs);\r
53668523 4403 res = follow_path(dp, path); /* Follow the path to the directory */\r
53668523 4404 if (res == FR_OK) { /* Follow completed */\r
70702af1 4405 if (!(dp->fn[NSFLAG] & NS_NONAME)) { /* It is not the origin directory itself */\r
289f6a14
L
4406 if (dp->obj.attr & AM_DIR) { /* This object is a sub-directory */\r
4407#if FF_FS_EXFAT\r
70702af1 4408 if (fs->fs_type == FS_EXFAT) {\r
289f6a14
L
4409 dp->obj.c_scl = dp->obj.sclust; /* Get containing directory inforamation */\r
4410 dp->obj.c_size = ((DWORD)dp->obj.objsize & 0xFFFFFF00) | dp->obj.stat;\r
4411 dp->obj.c_ofs = dp->blk_ofs;\r
4412 init_alloc_info(fs, &dp->obj); /* Get object allocation info */\r
70702af1
L
4413 } else\r
4414#endif\r
4415 {\r
289f6a14 4416 dp->obj.sclust = ld_clust(fs, dp->dir); /* Get object allocation info */\r
70702af1
L
4417 }\r
4418 } else { /* This object is a file */\r
53668523 4419 res = FR_NO_PATH;\r
70702af1 4420 }\r
53668523
L
4421 }\r
4422 if (res == FR_OK) {\r
289f6a14 4423 dp->obj.id = fs->id;\r
53668523 4424 res = dir_sdi(dp, 0); /* Rewind directory */\r
289f6a14 4425#if FF_FS_LOCK != 0\r
53668523 4426 if (res == FR_OK) {\r
289f6a14
L
4427 if (dp->obj.sclust != 0) {\r
4428 dp->obj.lockid = inc_lock(dp, 0); /* Lock the sub directory */\r
4429 if (!dp->obj.lockid) res = FR_TOO_MANY_OPEN_FILES;\r
53668523 4430 } else {\r
289f6a14 4431 dp->obj.lockid = 0; /* Root directory need not to be locked */\r
53668523
L
4432 }\r
4433 }\r
4434#endif\r
4435 }\r
4436 }\r
70702af1 4437 FREE_NAMBUF();\r
53668523
L
4438 if (res == FR_NO_FILE) res = FR_NO_PATH;\r
4439 }\r
289f6a14 4440 if (res != FR_OK) dp->obj.fs = 0; /* Invalidate the directory object if function faild */\r
53668523
L
4441\r
4442 LEAVE_FF(fs, res);\r
4443}\r
4444\r
4445\r
4446\r
4447\r
4448/*-----------------------------------------------------------------------*/\r
4449/* Close Directory */\r
4450/*-----------------------------------------------------------------------*/\r
4451\r
4452FRESULT f_closedir (\r
4453 DIR *dp /* Pointer to the directory object to be closed */\r
4454)\r
4455{\r
4456 FRESULT res;\r
70702af1 4457 FATFS *fs;\r
53668523
L
4458\r
4459\r
289f6a14 4460 res = validate(&dp->obj, &fs); /* Check validity of the file object */\r
53668523 4461 if (res == FR_OK) {\r
289f6a14
L
4462#if FF_FS_LOCK != 0\r
4463 if (dp->obj.lockid) res = dec_lock(dp->obj.lockid); /* Decrement sub-directory open counter */\r
4464 if (res == FR_OK) dp->obj.fs = 0; /* Invalidate directory object */\r
4465#else\r
4466 dp->obj.fs = 0; /* Invalidate directory object */\r
53668523 4467#endif\r
289f6a14 4468#if FF_FS_REENTRANT\r
53668523
L
4469 unlock_fs(fs, FR_OK); /* Unlock volume */\r
4470#endif\r
4471 }\r
4472 return res;\r
4473}\r
4474\r
4475\r
4476\r
4477\r
4478/*-----------------------------------------------------------------------*/\r
4479/* Read Directory Entries in Sequence */\r
4480/*-----------------------------------------------------------------------*/\r
4481\r
4482FRESULT f_readdir (\r
4483 DIR* dp, /* Pointer to the open directory object */\r
4484 FILINFO* fno /* Pointer to file information to return */\r
4485)\r
4486{\r
4487 FRESULT res;\r
70702af1
L
4488 FATFS *fs;\r
4489 DEF_NAMBUF\r
53668523
L
4490\r
4491\r
70702af1 4492 res = validate(&dp->obj, &fs); /* Check validity of the directory object */\r
53668523
L
4493 if (res == FR_OK) {\r
4494 if (!fno) {\r
4495 res = dir_sdi(dp, 0); /* Rewind the directory object */\r
4496 } else {\r
70702af1 4497 INIT_NAMBUF(fs);\r
289f6a14 4498 res = dir_read_file(dp); /* Read an item */\r
70702af1 4499 if (res == FR_NO_FILE) res = FR_OK; /* Ignore end of directory */\r
53668523
L
4500 if (res == FR_OK) { /* A valid entry is found */\r
4501 get_fileinfo(dp, fno); /* Get the object information */\r
4502 res = dir_next(dp, 0); /* Increment index for next */\r
70702af1 4503 if (res == FR_NO_FILE) res = FR_OK; /* Ignore end of directory now */\r
53668523 4504 }\r
70702af1 4505 FREE_NAMBUF();\r
53668523
L
4506 }\r
4507 }\r
70702af1
L
4508 LEAVE_FF(fs, res);\r
4509}\r
4510\r
4511\r
4512\r
289f6a14 4513#if FF_USE_FIND\r
70702af1
L
4514/*-----------------------------------------------------------------------*/\r
4515/* Find Next File */\r
4516/*-----------------------------------------------------------------------*/\r
4517\r
4518FRESULT f_findnext (\r
4519 DIR* dp, /* Pointer to the open directory object */\r
4520 FILINFO* fno /* Pointer to the file information structure */\r
4521)\r
4522{\r
4523 FRESULT res;\r
4524\r
4525\r
4526 for (;;) {\r
4527 res = f_readdir(dp, fno); /* Get a directory item */\r
4528 if (res != FR_OK || !fno || !fno->fname[0]) break; /* Terminate if any error or end of directory */\r
4529 if (pattern_matching(dp->pat, fno->fname, 0, 0)) break; /* Test for the file name */\r
289f6a14 4530#if FF_USE_LFN && FF_USE_FIND == 2\r
70702af1
L
4531 if (pattern_matching(dp->pat, fno->altname, 0, 0)) break; /* Test for alternative name if exist */\r
4532#endif\r
4533 }\r
4534 return res;\r
4535}\r
4536\r
4537\r
4538\r
4539/*-----------------------------------------------------------------------*/\r
4540/* Find First File */\r
4541/*-----------------------------------------------------------------------*/\r
53668523 4542\r
70702af1
L
4543FRESULT f_findfirst (\r
4544 DIR* dp, /* Pointer to the blank directory object */\r
4545 FILINFO* fno, /* Pointer to the file information structure */\r
4546 const TCHAR* path, /* Pointer to the directory to open */\r
4547 const TCHAR* pattern /* Pointer to the matching pattern */\r
4548)\r
4549{\r
4550 FRESULT res;\r
4551\r
4552\r
4553 dp->pat = pattern; /* Save pointer to pattern string */\r
4554 res = f_opendir(dp, path); /* Open the target directory */\r
4555 if (res == FR_OK) {\r
4556 res = f_findnext(dp, fno); /* Find the first item */\r
4557 }\r
4558 return res;\r
53668523
L
4559}\r
4560\r
289f6a14 4561#endif /* FF_USE_FIND */\r
70702af1 4562\r
53668523
L
4563\r
4564\r
289f6a14 4565#if FF_FS_MINIMIZE == 0\r
53668523
L
4566/*-----------------------------------------------------------------------*/\r
4567/* Get File Status */\r
4568/*-----------------------------------------------------------------------*/\r
4569\r
4570FRESULT f_stat (\r
4571 const TCHAR* path, /* Pointer to the file path */\r
4572 FILINFO* fno /* Pointer to file information to return */\r
4573)\r
4574{\r
4575 FRESULT res;\r
4576 DIR dj;\r
70702af1 4577 DEF_NAMBUF\r
53668523
L
4578\r
4579\r
70702af1
L
4580 /* Get logical drive */\r
4581 res = find_volume(&path, &dj.obj.fs, 0);\r
53668523 4582 if (res == FR_OK) {\r
70702af1 4583 INIT_NAMBUF(dj.obj.fs);\r
53668523
L
4584 res = follow_path(&dj, path); /* Follow the file path */\r
4585 if (res == FR_OK) { /* Follow completed */\r
70702af1 4586 if (dj.fn[NSFLAG] & NS_NONAME) { /* It is origin directory */\r
53668523 4587 res = FR_INVALID_NAME;\r
70702af1
L
4588 } else { /* Found an object */\r
4589 if (fno) get_fileinfo(&dj, fno);\r
53668523
L
4590 }\r
4591 }\r
70702af1 4592 FREE_NAMBUF();\r
53668523
L
4593 }\r
4594\r
70702af1 4595 LEAVE_FF(dj.obj.fs, res);\r
53668523
L
4596}\r
4597\r
4598\r
4599\r
289f6a14 4600#if !FF_FS_READONLY\r
53668523
L
4601/*-----------------------------------------------------------------------*/\r
4602/* Get Number of Free Clusters */\r
4603/*-----------------------------------------------------------------------*/\r
4604\r
4605FRESULT f_getfree (\r
289f6a14 4606 const TCHAR* path, /* Logical drive number */\r
53668523 4607 DWORD* nclst, /* Pointer to a variable to return number of free clusters */\r
289f6a14 4608 FATFS** fatfs /* Pointer to return pointer to corresponding filesystem object */\r
53668523
L
4609)\r
4610{\r
4611 FRESULT res;\r
4612 FATFS *fs;\r
70702af1 4613 DWORD nfree, clst, sect, stat;\r
53668523 4614 UINT i;\r
289f6a14 4615 FFOBJID obj;\r
53668523
L
4616\r
4617\r
70702af1
L
4618 /* Get logical drive */\r
4619 res = find_volume(&path, &fs, 0);\r
53668523 4620 if (res == FR_OK) {\r
70702af1 4621 *fatfs = fs; /* Return ptr to the fs object */\r
289f6a14 4622 /* If free_clst is valid, return it without full FAT scan */\r
70702af1
L
4623 if (fs->free_clst <= fs->n_fatent - 2) {\r
4624 *nclst = fs->free_clst;\r
53668523 4625 } else {\r
289f6a14 4626 /* Scan FAT to obtain number of free clusters */\r
70702af1 4627 nfree = 0;\r
289f6a14 4628 if (fs->fs_type == FS_FAT12) { /* FAT12: Scan bit field FAT entries */\r
70702af1 4629 clst = 2; obj.fs = fs;\r
53668523 4630 do {\r
70702af1 4631 stat = get_fat(&obj, clst);\r
53668523
L
4632 if (stat == 0xFFFFFFFF) { res = FR_DISK_ERR; break; }\r
4633 if (stat == 1) { res = FR_INT_ERR; break; }\r
70702af1 4634 if (stat == 0) nfree++;\r
53668523
L
4635 } while (++clst < fs->n_fatent);\r
4636 } else {\r
289f6a14
L
4637#if FF_FS_EXFAT\r
4638 if (fs->fs_type == FS_EXFAT) { /* exFAT: Scan allocation bitmap */\r
70702af1
L
4639 BYTE bm;\r
4640 UINT b;\r
4641\r
289f6a14
L
4642 clst = fs->n_fatent - 2; /* Number of clusters */\r
4643 sect = fs->database; /* Assuming bitmap starts at cluster 2 */\r
4644 i = 0; /* Offset in the sector */\r
4645 do { /* Counts numbuer of bits with zero in the bitmap */\r
4646 if (i == 0) {\r
4647 res = move_window(fs, sect++);\r
4648 if (res != FR_OK) break;\r
4649 }\r
70702af1
L
4650 for (b = 8, bm = fs->win[i]; b && clst; b--, clst--) {\r
4651 if (!(bm & 1)) nfree++;\r
4652 bm >>= 1;\r
4653 }\r
4654 i = (i + 1) % SS(fs);\r
4655 } while (clst);\r
4656 } else\r
4657#endif\r
289f6a14
L
4658 { /* FAT16/32: Scan WORD/DWORD FAT entries */\r
4659 clst = fs->n_fatent; /* Number of entries */\r
4660 sect = fs->fatbase; /* Top of the FAT */\r
4661 i = 0; /* Offset in the sector */\r
4662 do { /* Counts numbuer of entries with zero in the FAT */\r
70702af1
L
4663 if (i == 0) {\r
4664 res = move_window(fs, sect++);\r
4665 if (res != FR_OK) break;\r
70702af1
L
4666 }\r
4667 if (fs->fs_type == FS_FAT16) {\r
289f6a14
L
4668 if (ld_word(fs->win + i) == 0) nfree++;\r
4669 i += 2;\r
70702af1 4670 } else {\r
289f6a14
L
4671 if ((ld_dword(fs->win + i) & 0x0FFFFFFF) == 0) nfree++;\r
4672 i += 4;\r
70702af1 4673 }\r
289f6a14 4674 i %= SS(fs);\r
70702af1
L
4675 } while (--clst);\r
4676 }\r
53668523 4677 }\r
70702af1
L
4678 *nclst = nfree; /* Return the free clusters */\r
4679 fs->free_clst = nfree; /* Now free_clst is valid */\r
289f6a14 4680 fs->fsi_flag |= 1; /* FAT32: FSInfo is to be updated */\r
53668523
L
4681 }\r
4682 }\r
70702af1 4683\r
53668523
L
4684 LEAVE_FF(fs, res);\r
4685}\r
4686\r
4687\r
4688\r
4689\r
4690/*-----------------------------------------------------------------------*/\r
4691/* Truncate File */\r
4692/*-----------------------------------------------------------------------*/\r
4693\r
4694FRESULT f_truncate (\r
4695 FIL* fp /* Pointer to the file object */\r
4696)\r
4697{\r
4698 FRESULT res;\r
70702af1 4699 FATFS *fs;\r
53668523
L
4700 DWORD ncl;\r
4701\r
4702\r
70702af1
L
4703 res = validate(&fp->obj, &fs); /* Check validity of the file object */\r
4704 if (res != FR_OK || (res = (FRESULT)fp->err) != FR_OK) LEAVE_FF(fs, res);\r
4705 if (!(fp->flag & FA_WRITE)) LEAVE_FF(fs, FR_DENIED); /* Check access mode */\r
4706\r
289f6a14 4707 if (fp->fptr < fp->obj.objsize) { /* Process when fptr is not on the eof */\r
70702af1
L
4708 if (fp->fptr == 0) { /* When set file size to zero, remove entire cluster chain */\r
4709 res = remove_chain(&fp->obj, fp->obj.sclust, 0);\r
4710 fp->obj.sclust = 0;\r
4711 } else { /* When truncate a part of the file, remove remaining clusters */\r
4712 ncl = get_fat(&fp->obj, fp->clust);\r
4713 res = FR_OK;\r
4714 if (ncl == 0xFFFFFFFF) res = FR_DISK_ERR;\r
4715 if (ncl == 1) res = FR_INT_ERR;\r
4716 if (res == FR_OK && ncl < fs->n_fatent) {\r
4717 res = remove_chain(&fp->obj, ncl, fp->clust);\r
53668523 4718 }\r
70702af1 4719 }\r
289f6a14 4720 fp->obj.objsize = fp->fptr; /* Set file size to current read/write point */\r
70702af1 4721 fp->flag |= FA_MODIFIED;\r
289f6a14 4722#if !FF_FS_TINY\r
70702af1 4723 if (res == FR_OK && (fp->flag & FA_DIRTY)) {\r
289f6a14 4724 if (disk_write(fs->pdrv, fp->buf, fp->sect, 1) != RES_OK) {\r
70702af1
L
4725 res = FR_DISK_ERR;\r
4726 } else {\r
4727 fp->flag &= (BYTE)~FA_DIRTY;\r
53668523 4728 }\r
53668523 4729 }\r
70702af1
L
4730#endif\r
4731 if (res != FR_OK) ABORT(fs, res);\r
53668523
L
4732 }\r
4733\r
70702af1 4734 LEAVE_FF(fs, res);\r
53668523
L
4735}\r
4736\r
4737\r
4738\r
4739\r
4740/*-----------------------------------------------------------------------*/\r
70702af1 4741/* Delete a File/Directory */\r
53668523
L
4742/*-----------------------------------------------------------------------*/\r
4743\r
4744FRESULT f_unlink (\r
4745 const TCHAR* path /* Pointer to the file or directory path */\r
4746)\r
4747{\r
4748 FRESULT res;\r
4749 DIR dj, sdj;\r
7b78a5a2 4750 DWORD dclst = 0;\r
70702af1 4751 FATFS *fs;\r
289f6a14
L
4752#if FF_FS_EXFAT\r
4753 FFOBJID obj;\r
70702af1
L
4754#endif\r
4755 DEF_NAMBUF\r
53668523
L
4756\r
4757\r
70702af1
L
4758 /* Get logical drive */\r
4759 res = find_volume(&path, &fs, FA_WRITE);\r
53668523 4760 if (res == FR_OK) {\r
289f6a14 4761 dj.obj.fs = fs;\r
70702af1 4762 INIT_NAMBUF(fs);\r
53668523 4763 res = follow_path(&dj, path); /* Follow the file path */\r
289f6a14 4764 if (FF_FS_RPATH && res == FR_OK && (dj.fn[NSFLAG] & NS_DOT)) {\r
53668523 4765 res = FR_INVALID_NAME; /* Cannot remove dot entry */\r
70702af1 4766 }\r
289f6a14 4767#if FF_FS_LOCK != 0\r
70702af1 4768 if (res == FR_OK) res = chk_lock(&dj, 2); /* Check if it is an open object */\r
53668523
L
4769#endif\r
4770 if (res == FR_OK) { /* The object is accessible */\r
70702af1 4771 if (dj.fn[NSFLAG] & NS_NONAME) {\r
7b78a5a2 4772 res = FR_INVALID_NAME; /* Cannot remove the origin directory */\r
53668523 4773 } else {\r
70702af1 4774 if (dj.obj.attr & AM_RDO) {\r
53668523 4775 res = FR_DENIED; /* Cannot remove R/O object */\r
70702af1 4776 }\r
53668523 4777 }\r
70702af1 4778 if (res == FR_OK) {\r
289f6a14 4779#if FF_FS_EXFAT\r
70702af1
L
4780 obj.fs = fs;\r
4781 if (fs->fs_type == FS_EXFAT) {\r
289f6a14
L
4782 init_alloc_info(fs, &obj);\r
4783 dclst = obj.sclust;\r
70702af1
L
4784 } else\r
4785#endif\r
4786 {\r
4787 dclst = ld_clust(fs, dj.dir);\r
4788 }\r
289f6a14
L
4789 if (dj.obj.attr & AM_DIR) { /* Is it a sub-directory? */\r
4790#if FF_FS_RPATH != 0\r
4791 if (dclst == fs->cdir) { /* Is it the current directory? */\r
70702af1
L
4792 res = FR_DENIED;\r
4793 } else\r
4794#endif\r
4795 {\r
289f6a14 4796 sdj.obj.fs = fs; /* Open the sub-directory */\r
70702af1 4797 sdj.obj.sclust = dclst;\r
289f6a14 4798#if FF_FS_EXFAT\r
70702af1
L
4799 if (fs->fs_type == FS_EXFAT) {\r
4800 sdj.obj.objsize = obj.objsize;\r
4801 sdj.obj.stat = obj.stat;\r
4802 }\r
53668523 4803#endif\r
70702af1
L
4804 res = dir_sdi(&sdj, 0);\r
4805 if (res == FR_OK) {\r
289f6a14 4806 res = dir_read_file(&sdj); /* Test if the directory is empty */\r
70702af1
L
4807 if (res == FR_OK) res = FR_DENIED; /* Not empty? */\r
4808 if (res == FR_NO_FILE) res = FR_OK; /* Empty? */\r
4809 }\r
53668523
L
4810 }\r
4811 }\r
4812 }\r
4813 if (res == FR_OK) {\r
70702af1 4814 res = dir_remove(&dj); /* Remove the directory entry */\r
289f6a14
L
4815 if (res == FR_OK && dclst != 0) { /* Remove the cluster chain if exist */\r
4816#if FF_FS_EXFAT\r
70702af1
L
4817 res = remove_chain(&obj, dclst, 0);\r
4818#else\r
4819 res = remove_chain(&dj.obj, dclst, 0);\r
4820#endif\r
4821 }\r
4822 if (res == FR_OK) res = sync_fs(fs);\r
53668523
L
4823 }\r
4824 }\r
70702af1 4825 FREE_NAMBUF();\r
53668523
L
4826 }\r
4827\r
70702af1 4828 LEAVE_FF(fs, res);\r
53668523
L
4829}\r
4830\r
4831\r
4832\r
4833\r
4834/*-----------------------------------------------------------------------*/\r
4835/* Create a Directory */\r
4836/*-----------------------------------------------------------------------*/\r
4837\r
4838FRESULT f_mkdir (\r
4839 const TCHAR* path /* Pointer to the directory path */\r
4840)\r
4841{\r
4842 FRESULT res;\r
4843 DIR dj;\r
70702af1
L
4844 FATFS *fs;\r
4845 BYTE *dir;\r
289f6a14 4846 DWORD dcl, pcl, tm;\r
70702af1 4847 DEF_NAMBUF\r
53668523
L
4848\r
4849\r
70702af1
L
4850 /* Get logical drive */\r
4851 res = find_volume(&path, &fs, FA_WRITE);\r
53668523 4852 if (res == FR_OK) {\r
289f6a14 4853 dj.obj.fs = fs;\r
70702af1 4854 INIT_NAMBUF(fs);\r
53668523
L
4855 res = follow_path(&dj, path); /* Follow the file path */\r
4856 if (res == FR_OK) res = FR_EXIST; /* Any object with same name is already existing */\r
289f6a14 4857 if (FF_FS_RPATH && res == FR_NO_FILE && (dj.fn[NSFLAG] & NS_DOT)) {\r
53668523 4858 res = FR_INVALID_NAME;\r
70702af1 4859 }\r
53668523 4860 if (res == FR_NO_FILE) { /* Can create a new directory */\r
70702af1
L
4861 dcl = create_chain(&dj.obj, 0); /* Allocate a cluster for the new directory table */\r
4862 dj.obj.objsize = (DWORD)fs->csize * SS(fs);\r
53668523
L
4863 res = FR_OK;\r
4864 if (dcl == 0) res = FR_DENIED; /* No space to allocate a new cluster */\r
4865 if (dcl == 1) res = FR_INT_ERR;\r
4866 if (dcl == 0xFFFFFFFF) res = FR_DISK_ERR;\r
70702af1
L
4867 if (res == FR_OK) res = sync_window(fs); /* Flush FAT */\r
4868 tm = GET_FATTIME();\r
53668523 4869 if (res == FR_OK) { /* Initialize the new directory table */\r
289f6a14
L
4870 res = dir_clear(fs, dcl); /* Clean up the new table */\r
4871 if (res == FR_OK && (!FF_FS_EXFAT || fs->fs_type != FS_EXFAT)) { /* Create dot entries (FAT only) */\r
4872 dir = fs->win;\r
70702af1
L
4873 mem_set(dir + DIR_Name, ' ', 11); /* Create "." entry */\r
4874 dir[DIR_Name] = '.';\r
4875 dir[DIR_Attr] = AM_DIR;\r
4876 st_dword(dir + DIR_ModTime, tm);\r
4877 st_clust(fs, dir, dcl);\r
289f6a14 4878 mem_cpy(dir + SZDIRE, dir, SZDIRE); /* Create ".." entry */\r
70702af1 4879 dir[SZDIRE + 1] = '.'; pcl = dj.obj.sclust;\r
70702af1 4880 st_clust(fs, dir + SZDIRE, pcl);\r
70702af1 4881 fs->wflag = 1;\r
53668523
L
4882 }\r
4883 }\r
70702af1 4884 if (res == FR_OK) {\r
289f6a14
L
4885 res = dir_register(&dj); /* Register the object to the directoy */\r
4886 }\r
4887 if (res == FR_OK) {\r
4888#if FF_FS_EXFAT\r
70702af1
L
4889 if (fs->fs_type == FS_EXFAT) { /* Initialize directory entry block */\r
4890 st_dword(fs->dirbuf + XDIR_ModTime, tm); /* Created time */\r
4891 st_dword(fs->dirbuf + XDIR_FstClus, dcl); /* Table start cluster */\r
4892 st_dword(fs->dirbuf + XDIR_FileSize, (DWORD)dj.obj.objsize); /* File size needs to be valid */\r
4893 st_dword(fs->dirbuf + XDIR_ValidFileSize, (DWORD)dj.obj.objsize);\r
289f6a14 4894 fs->dirbuf[XDIR_GenFlags] = 3; /* Initialize the object flag */\r
70702af1
L
4895 fs->dirbuf[XDIR_Attr] = AM_DIR; /* Attribute */\r
4896 res = store_xdir(&dj);\r
4897 } else\r
4898#endif\r
4899 {\r
4900 dir = dj.dir;\r
4901 st_dword(dir + DIR_ModTime, tm); /* Created time */\r
4902 st_clust(fs, dir, dcl); /* Table start cluster */\r
4903 dir[DIR_Attr] = AM_DIR; /* Attribute */\r
4904 fs->wflag = 1;\r
4905 }\r
289f6a14
L
4906 if (res == FR_OK) {\r
4907 res = sync_fs(fs);\r
4908 }\r
53668523 4909 } else {\r
70702af1 4910 remove_chain(&dj.obj, dcl, 0); /* Could not register, remove cluster chain */\r
53668523
L
4911 }\r
4912 }\r
70702af1 4913 FREE_NAMBUF();\r
53668523
L
4914 }\r
4915\r
70702af1 4916 LEAVE_FF(fs, res);\r
53668523
L
4917}\r
4918\r
4919\r
4920\r
4921\r
4922/*-----------------------------------------------------------------------*/\r
70702af1 4923/* Rename a File/Directory */\r
53668523
L
4924/*-----------------------------------------------------------------------*/\r
4925\r
70702af1
L
4926FRESULT f_rename (\r
4927 const TCHAR* path_old, /* Pointer to the object name to be renamed */\r
4928 const TCHAR* path_new /* Pointer to the new name */\r
53668523
L
4929)\r
4930{\r
4931 FRESULT res;\r
70702af1
L
4932 DIR djo, djn;\r
4933 FATFS *fs;\r
289f6a14 4934 BYTE buf[FF_FS_EXFAT ? SZDIRE * 2 : SZDIRE], *dir;\r
70702af1
L
4935 DWORD dw;\r
4936 DEF_NAMBUF\r
53668523
L
4937\r
4938\r
289f6a14 4939 get_ldnumber(&path_new); /* Snip the drive number of new name off */\r
70702af1 4940 res = find_volume(&path_old, &fs, FA_WRITE); /* Get logical drive of the old object */\r
53668523 4941 if (res == FR_OK) {\r
70702af1
L
4942 djo.obj.fs = fs;\r
4943 INIT_NAMBUF(fs);\r
4944 res = follow_path(&djo, path_old); /* Check old object */\r
4945 if (res == FR_OK && (djo.fn[NSFLAG] & (NS_DOT | NS_NONAME))) res = FR_INVALID_NAME; /* Check validity of name */\r
289f6a14
L
4946#if FF_FS_LOCK != 0\r
4947 if (res == FR_OK) {\r
4948 res = chk_lock(&djo, 2);\r
4949 }\r
70702af1
L
4950#endif\r
4951 if (res == FR_OK) { /* Object to be renamed is found */\r
289f6a14
L
4952#if FF_FS_EXFAT\r
4953 if (fs->fs_type == FS_EXFAT) { /* At exFAT volume */\r
70702af1
L
4954 BYTE nf, nn;\r
4955 WORD nh;\r
4956\r
4957 mem_cpy(buf, fs->dirbuf, SZDIRE * 2); /* Save 85+C0 entry of old object */\r
4958 mem_cpy(&djn, &djo, sizeof djo);\r
4959 res = follow_path(&djn, path_new); /* Make sure if new object name is not in use */\r
4960 if (res == FR_OK) { /* Is new name already in use by any other object? */\r
4961 res = (djn.obj.sclust == djo.obj.sclust && djn.dptr == djo.dptr) ? FR_NO_FILE : FR_EXIST;\r
4962 }\r
4963 if (res == FR_NO_FILE) { /* It is a valid path and no name collision */\r
4964 res = dir_register(&djn); /* Register the new entry */\r
4965 if (res == FR_OK) {\r
4966 nf = fs->dirbuf[XDIR_NumSec]; nn = fs->dirbuf[XDIR_NumName];\r
4967 nh = ld_word(fs->dirbuf + XDIR_NameHash);\r
289f6a14 4968 mem_cpy(fs->dirbuf, buf, SZDIRE * 2); /* Restore 85+C0 entry */\r
70702af1
L
4969 fs->dirbuf[XDIR_NumSec] = nf; fs->dirbuf[XDIR_NumName] = nn;\r
4970 st_word(fs->dirbuf + XDIR_NameHash, nh);\r
289f6a14
L
4971 if (!(fs->dirbuf[XDIR_Attr] & AM_DIR)) fs->dirbuf[XDIR_Attr] |= AM_ARC; /* Set archive attribute if it is a file */\r
4972/* Start of critical section where an interruption can cause a cross-link */\r
70702af1
L
4973 res = store_xdir(&djn);\r
4974 }\r
4975 }\r
4976 } else\r
4977#endif\r
289f6a14
L
4978 { /* At FAT/FAT32 volume */\r
4979 mem_cpy(buf, djo.dir, SZDIRE); /* Save directory entry of the object */\r
70702af1
L
4980 mem_cpy(&djn, &djo, sizeof (DIR)); /* Duplicate the directory object */\r
4981 res = follow_path(&djn, path_new); /* Make sure if new object name is not in use */\r
4982 if (res == FR_OK) { /* Is new name already in use by any other object? */\r
4983 res = (djn.obj.sclust == djo.obj.sclust && djn.dptr == djo.dptr) ? FR_NO_FILE : FR_EXIST;\r
4984 }\r
4985 if (res == FR_NO_FILE) { /* It is a valid path and no name collision */\r
4986 res = dir_register(&djn); /* Register the new entry */\r
4987 if (res == FR_OK) {\r
289f6a14
L
4988 dir = djn.dir; /* Copy directory entry of the object except name */\r
4989 mem_cpy(dir + 13, buf + 13, SZDIRE - 13);\r
4990 dir[DIR_Attr] = buf[DIR_Attr];\r
4991 if (!(dir[DIR_Attr] & AM_DIR)) dir[DIR_Attr] |= AM_ARC; /* Set archive attribute if it is a file */\r
70702af1
L
4992 fs->wflag = 1;\r
4993 if ((dir[DIR_Attr] & AM_DIR) && djo.obj.sclust != djn.obj.sclust) { /* Update .. entry in the sub-directory if needed */\r
289f6a14
L
4994 dw = clst2sect(fs, ld_clust(fs, dir));\r
4995 if (dw == 0) {\r
70702af1
L
4996 res = FR_INT_ERR;\r
4997 } else {\r
289f6a14 4998/* Start of critical section where an interruption can cause a cross-link */\r
70702af1
L
4999 res = move_window(fs, dw);\r
5000 dir = fs->win + SZDIRE * 1; /* Ptr to .. entry */\r
5001 if (res == FR_OK && dir[1] == '.') {\r
5002 st_clust(fs, dir, djn.obj.sclust);\r
5003 fs->wflag = 1;\r
5004 }\r
5005 }\r
5006 }\r
5007 }\r
5008 }\r
5009 }\r
5010 if (res == FR_OK) {\r
5011 res = dir_remove(&djo); /* Remove old entry */\r
5012 if (res == FR_OK) {\r
5013 res = sync_fs(fs);\r
5014 }\r
53668523 5015 }\r
289f6a14 5016/* End of the critical section */\r
53668523 5017 }\r
70702af1 5018 FREE_NAMBUF();\r
53668523
L
5019 }\r
5020\r
70702af1 5021 LEAVE_FF(fs, res);\r
53668523
L
5022}\r
5023\r
289f6a14
L
5024#endif /* !FF_FS_READONLY */\r
5025#endif /* FF_FS_MINIMIZE == 0 */\r
5026#endif /* FF_FS_MINIMIZE <= 1 */\r
5027#endif /* FF_FS_MINIMIZE <= 2 */\r
53668523
L
5028\r
5029\r
5030\r
289f6a14 5031#if FF_USE_CHMOD && !FF_FS_READONLY\r
53668523 5032/*-----------------------------------------------------------------------*/\r
70702af1 5033/* Change Attribute */\r
53668523
L
5034/*-----------------------------------------------------------------------*/\r
5035\r
70702af1
L
5036FRESULT f_chmod (\r
5037 const TCHAR* path, /* Pointer to the file path */\r
5038 BYTE attr, /* Attribute bits */\r
5039 BYTE mask /* Attribute mask to change */\r
53668523
L
5040)\r
5041{\r
5042 FRESULT res;\r
70702af1
L
5043 DIR dj;\r
5044 FATFS *fs;\r
5045 DEF_NAMBUF\r
53668523
L
5046\r
5047\r
70702af1 5048 res = find_volume(&path, &fs, FA_WRITE); /* Get logical drive */\r
53668523 5049 if (res == FR_OK) {\r
289f6a14 5050 dj.obj.fs = fs;\r
70702af1
L
5051 INIT_NAMBUF(fs);\r
5052 res = follow_path(&dj, path); /* Follow the file path */\r
5053 if (res == FR_OK && (dj.fn[NSFLAG] & (NS_DOT | NS_NONAME))) res = FR_INVALID_NAME; /* Check object validity */\r
5054 if (res == FR_OK) {\r
5055 mask &= AM_RDO|AM_HID|AM_SYS|AM_ARC; /* Valid attribute mask */\r
289f6a14 5056#if FF_FS_EXFAT\r
70702af1
L
5057 if (fs->fs_type == FS_EXFAT) {\r
5058 fs->dirbuf[XDIR_Attr] = (attr & mask) | (fs->dirbuf[XDIR_Attr] & (BYTE)~mask); /* Apply attribute change */\r
5059 res = store_xdir(&dj);\r
5060 } else\r
53668523 5061#endif\r
70702af1
L
5062 {\r
5063 dj.dir[DIR_Attr] = (attr & mask) | (dj.dir[DIR_Attr] & (BYTE)~mask); /* Apply attribute change */\r
5064 fs->wflag = 1;\r
53668523 5065 }\r
289f6a14
L
5066 if (res == FR_OK) {\r
5067 res = sync_fs(fs);\r
5068 }\r
53668523 5069 }\r
70702af1 5070 FREE_NAMBUF();\r
53668523
L
5071 }\r
5072\r
70702af1 5073 LEAVE_FF(fs, res);\r
53668523
L
5074}\r
5075\r
7b78a5a2
L
5076\r
5077\r
5078\r
5079/*-----------------------------------------------------------------------*/\r
5080/* Change Timestamp */\r
5081/*-----------------------------------------------------------------------*/\r
5082\r
5083FRESULT f_utime (\r
5084 const TCHAR* path, /* Pointer to the file/directory name */\r
289f6a14 5085 const FILINFO* fno /* Pointer to the timestamp to be set */\r
7b78a5a2
L
5086)\r
5087{\r
5088 FRESULT res;\r
5089 DIR dj;\r
70702af1
L
5090 FATFS *fs;\r
5091 DEF_NAMBUF\r
7b78a5a2
L
5092\r
5093\r
70702af1 5094 res = find_volume(&path, &fs, FA_WRITE); /* Get logical drive */\r
7b78a5a2 5095 if (res == FR_OK) {\r
289f6a14 5096 dj.obj.fs = fs;\r
70702af1 5097 INIT_NAMBUF(fs);\r
7b78a5a2 5098 res = follow_path(&dj, path); /* Follow the file path */\r
70702af1 5099 if (res == FR_OK && (dj.fn[NSFLAG] & (NS_DOT | NS_NONAME))) res = FR_INVALID_NAME; /* Check object validity */\r
7b78a5a2 5100 if (res == FR_OK) {\r
289f6a14 5101#if FF_FS_EXFAT\r
70702af1
L
5102 if (fs->fs_type == FS_EXFAT) {\r
5103 st_dword(fs->dirbuf + XDIR_ModTime, (DWORD)fno->fdate << 16 | fno->ftime);\r
5104 res = store_xdir(&dj);\r
5105 } else\r
5106#endif\r
5107 {\r
5108 st_dword(dj.dir + DIR_ModTime, (DWORD)fno->fdate << 16 | fno->ftime);\r
5109 fs->wflag = 1;\r
7b78a5a2 5110 }\r
289f6a14
L
5111 if (res == FR_OK) {\r
5112 res = sync_fs(fs);\r
5113 }\r
7b78a5a2 5114 }\r
70702af1 5115 FREE_NAMBUF();\r
7b78a5a2
L
5116 }\r
5117\r
70702af1 5118 LEAVE_FF(fs, res);\r
7b78a5a2
L
5119}\r
5120\r
289f6a14 5121#endif /* FF_USE_CHMOD && !FF_FS_READONLY */\r
53668523
L
5122\r
5123\r
5124\r
289f6a14 5125#if FF_USE_LABEL\r
53668523 5126/*-----------------------------------------------------------------------*/\r
70702af1 5127/* Get Volume Label */\r
53668523
L
5128/*-----------------------------------------------------------------------*/\r
5129\r
5130FRESULT f_getlabel (\r
289f6a14
L
5131 const TCHAR* path, /* Logical drive number */\r
5132 TCHAR* label, /* Buffer to store the volume label */\r
5133 DWORD* vsn /* Variable to store the volume serial number */\r
53668523
L
5134)\r
5135{\r
5136 FRESULT res;\r
5137 DIR dj;\r
70702af1
L
5138 FATFS *fs;\r
5139 UINT si, di;\r
289f6a14 5140 WCHAR wc;\r
53668523 5141\r
70702af1
L
5142 /* Get logical drive */\r
5143 res = find_volume(&path, &fs, 0);\r
53668523
L
5144\r
5145 /* Get volume label */\r
5146 if (res == FR_OK && label) {\r
70702af1 5147 dj.obj.fs = fs; dj.obj.sclust = 0; /* Open root directory */\r
53668523
L
5148 res = dir_sdi(&dj, 0);\r
5149 if (res == FR_OK) {\r
289f6a14 5150 res = dir_read_label(&dj); /* Find a volume label entry */\r
70702af1 5151 if (res == FR_OK) {\r
289f6a14 5152#if FF_FS_EXFAT\r
70702af1 5153 if (fs->fs_type == FS_EXFAT) {\r
289f6a14
L
5154 WCHAR hs;\r
5155\r
5156 for (si = di = hs = 0; si < dj.dir[XDIR_NumLabel]; si++) { /* Extract volume label from 83 entry */\r
5157 wc = ld_word(dj.dir + XDIR_Label + si * 2);\r
5158 if (hs == 0 && IsSurrogate(wc)) { /* Is the code a surrogate? */\r
5159 hs = wc; continue;\r
5160 }\r
5161 wc = put_utf((DWORD)hs << 16 | wc, &label[di], 4);\r
5162 if (wc == 0) { di = 0; break; }\r
5163 di += wc;\r
5164 hs = 0;\r
70702af1 5165 }\r
289f6a14 5166 if (hs != 0) di = 0; /* Broken surrogate pair? */\r
70702af1
L
5167 label[di] = 0;\r
5168 } else\r
5169#endif\r
5170 {\r
289f6a14
L
5171 si = di = 0; /* Extract volume label from AM_VOL entry */\r
5172 while (si < 11) {\r
5173 wc = dj.dir[si++];\r
5174#if FF_USE_LFN && FF_LFN_UNICODE >= 1 /* Unicode output */\r
5175 if (dbc_1st((BYTE)wc) && si < 11) wc = wc << 8 | dj.dir[si++]; /* Is it a DBC? */\r
5176 wc = ff_oem2uni(wc, CODEPAGE); /* Convert it into Unicode */\r
5177 if (wc != 0) wc = put_utf(wc, &label[di], 4); /* Put it in Unicode */\r
5178 if (wc == 0) { di = 0; break; }\r
5179 di += wc;\r
5180#else /* ANSI/OEM output */\r
5181 label[di++] = (TCHAR)wc;\r
70702af1 5182#endif\r
289f6a14 5183 }\r
70702af1
L
5184 do { /* Truncate trailing spaces */\r
5185 label[di] = 0;\r
5186 if (di == 0) break;\r
5187 } while (label[--di] == ' ');\r
5188 }\r
53668523
L
5189 }\r
5190 }\r
70702af1
L
5191 if (res == FR_NO_FILE) { /* No label entry and return nul string */\r
5192 label[0] = 0;\r
5193 res = FR_OK;\r
5194 }\r
53668523
L
5195 }\r
5196\r
5197 /* Get volume serial number */\r
5198 if (res == FR_OK && vsn) {\r
70702af1 5199 res = move_window(fs, fs->volbase);\r
53668523 5200 if (res == FR_OK) {\r
70702af1 5201 switch (fs->fs_type) {\r
289f6a14
L
5202 case FS_EXFAT:\r
5203 di = BPB_VolIDEx; break;\r
5204\r
5205 case FS_FAT32:\r
5206 di = BS_VolID32; break;\r
5207\r
5208 default:\r
5209 di = BS_VolID;\r
70702af1
L
5210 }\r
5211 *vsn = ld_dword(fs->win + di);\r
53668523
L
5212 }\r
5213 }\r
5214\r
70702af1 5215 LEAVE_FF(fs, res);\r
53668523
L
5216}\r
5217\r
5218\r
5219\r
289f6a14 5220#if !FF_FS_READONLY\r
53668523 5221/*-----------------------------------------------------------------------*/\r
70702af1 5222/* Set Volume Label */\r
53668523
L
5223/*-----------------------------------------------------------------------*/\r
5224\r
5225FRESULT f_setlabel (\r
289f6a14 5226 const TCHAR* label /* Volume label to set with heading logical drive number */\r
53668523
L
5227)\r
5228{\r
5229 FRESULT res;\r
5230 DIR dj;\r
70702af1
L
5231 FATFS *fs;\r
5232 BYTE dirvn[22];\r
289f6a14
L
5233 UINT di;\r
5234 WCHAR wc;\r
5235 static const char badchr[] = "+.,;=[]/\\\"*:<>\?|\x7F"; /* [0..] for FAT, [7..] for exFAT */\r
5236#if FF_USE_LFN\r
5237 DWORD dc;\r
5238#endif\r
53668523 5239\r
70702af1
L
5240 /* Get logical drive */\r
5241 res = find_volume(&label, &fs, FA_WRITE);\r
5242 if (res != FR_OK) LEAVE_FF(fs, res);\r
70702af1 5243\r
289f6a14 5244#if FF_FS_EXFAT\r
70702af1 5245 if (fs->fs_type == FS_EXFAT) { /* On the exFAT volume */\r
289f6a14
L
5246 mem_set(dirvn, 0, 22);\r
5247 di = 0;\r
5248 while ((UINT)*label >= ' ') { /* Create volume label */\r
5249 dc = tchar2uni(&label); /* Get a Unicode character */\r
5250 if (dc >= 0x10000) {\r
5251 if (dc == 0xFFFFFFFF || di >= 10) { /* Wrong surrogate or buffer overflow */\r
5252 dc = 0;\r
5253 } else {\r
5254 st_word(dirvn + di * 2, (WCHAR)(dc >> 16)); di++;\r
5255 }\r
70702af1 5256 }\r
289f6a14 5257 if (dc == 0 || chk_chr(badchr + 7, (int)dc) || di >= 11) { /* Check validity of the volume label */\r
70702af1
L
5258 LEAVE_FF(fs, FR_INVALID_NAME);\r
5259 }\r
289f6a14 5260 st_word(dirvn + di * 2, (WCHAR)dc); di++;\r
70702af1 5261 }\r
70702af1
L
5262 } else\r
5263#endif\r
289f6a14
L
5264 { /* On the FAT/FAT32 volume */\r
5265 mem_set(dirvn, ' ', 11);\r
5266 di = 0;\r
5267 while ((UINT)*label >= ' ') { /* Create volume label */\r
5268#if FF_USE_LFN\r
5269 dc = tchar2uni(&label);\r
5270 wc = (dc < 0x10000) ? ff_uni2oem(ff_wtoupper(dc), CODEPAGE) : 0;\r
5271#else /* ANSI/OEM input */\r
5272 wc = (BYTE)*label++;\r
5273 if (dbc_1st((BYTE)wc)) wc = dbc_2nd((BYTE)*label) ? wc << 8 | (BYTE)*label++ : 0;\r
5274 if (IsLower(wc)) wc -= 0x20; /* To upper ASCII characters */\r
5275#if FF_CODE_PAGE == 0\r
5276 if (ExCvt && wc >= 0x80) wc = ExCvt[wc - 0x80]; /* To upper extended characters (SBCS cfg) */\r
5277#elif FF_CODE_PAGE < 900\r
5278 if (wc >= 0x80) wc = ExCvt[wc - 0x80]; /* To upper extended characters (SBCS cfg) */\r
5279#endif\r
5280#endif\r
5281 if (wc == 0 || chk_chr(badchr + 0, (int)wc) || di >= (UINT)((wc >= 0x100) ? 10 : 11)) { /* Reject invalid characters for volume label */\r
5282 LEAVE_FF(fs, FR_INVALID_NAME);\r
5283 }\r
5284 if (wc >= 0x100) dirvn[di++] = (BYTE)(wc >> 8);\r
5285 dirvn[di++] = (BYTE)wc;\r
70702af1 5286 }\r
289f6a14
L
5287 if (dirvn[0] == DDEM) LEAVE_FF(fs, FR_INVALID_NAME); /* Reject illegal name (heading DDEM) */\r
5288 while (di && dirvn[di - 1] == ' ') di--; /* Snip trailing spaces */\r
53668523
L
5289 }\r
5290\r
5291 /* Set volume label */\r
289f6a14 5292 dj.obj.fs = fs; dj.obj.sclust = 0; /* Open root directory */\r
53668523
L
5293 res = dir_sdi(&dj, 0);\r
5294 if (res == FR_OK) {\r
289f6a14 5295 res = dir_read_label(&dj); /* Get volume label entry */\r
70702af1 5296 if (res == FR_OK) {\r
289f6a14
L
5297 if (FF_FS_EXFAT && fs->fs_type == FS_EXFAT) {\r
5298 dj.dir[XDIR_NumLabel] = (BYTE)di; /* Change the volume label */\r
5299 mem_cpy(dj.dir + XDIR_Label, dirvn, 22);\r
53668523 5300 } else {\r
289f6a14 5301 if (di != 0) {\r
70702af1
L
5302 mem_cpy(dj.dir, dirvn, 11); /* Change the volume label */\r
5303 } else {\r
5304 dj.dir[DIR_Name] = DDEM; /* Remove the volume label */\r
5305 }\r
53668523 5306 }\r
70702af1
L
5307 fs->wflag = 1;\r
5308 res = sync_fs(fs);\r
289f6a14 5309 } else { /* No volume label entry or an error */\r
53668523
L
5310 if (res == FR_NO_FILE) {\r
5311 res = FR_OK;\r
289f6a14 5312 if (di != 0) { /* Create a volume label entry */\r
70702af1 5313 res = dir_alloc(&dj, 1); /* Allocate an entry */\r
53668523 5314 if (res == FR_OK) {\r
289f6a14
L
5315 mem_set(dj.dir, 0, SZDIRE); /* Clean the entry */\r
5316 if (FF_FS_EXFAT && fs->fs_type == FS_EXFAT) {\r
70702af1 5317 dj.dir[XDIR_Type] = 0x83; /* Create 83 entry */\r
289f6a14
L
5318 dj.dir[XDIR_NumLabel] = (BYTE)di;\r
5319 mem_cpy(dj.dir + XDIR_Label, dirvn, 22);\r
70702af1
L
5320 } else {\r
5321 dj.dir[DIR_Attr] = AM_VOL; /* Create volume label entry */\r
5322 mem_cpy(dj.dir, dirvn, 11);\r
5323 }\r
5324 fs->wflag = 1;\r
5325 res = sync_fs(fs);\r
53668523
L
5326 }\r
5327 }\r
5328 }\r
5329 }\r
5330 }\r
5331\r
70702af1 5332 LEAVE_FF(fs, res);\r
53668523
L
5333}\r
5334\r
289f6a14
L
5335#endif /* !FF_FS_READONLY */\r
5336#endif /* FF_USE_LABEL */\r
53668523
L
5337\r
5338\r
5339\r
289f6a14 5340#if FF_USE_EXPAND && !FF_FS_READONLY\r
70702af1
L
5341/*-----------------------------------------------------------------------*/\r
5342/* Allocate a Contiguous Blocks to the File */\r
5343/*-----------------------------------------------------------------------*/\r
5344\r
5345FRESULT f_expand (\r
5346 FIL* fp, /* Pointer to the file object */\r
5347 FSIZE_t fsz, /* File size to be expanded to */\r
5348 BYTE opt /* Operation mode 0:Find and prepare or 1:Find and allocate */\r
5349)\r
5350{\r
5351 FRESULT res;\r
5352 FATFS *fs;\r
5353 DWORD n, clst, stcl, scl, ncl, tcl, lclst;\r
5354\r
5355\r
5356 res = validate(&fp->obj, &fs); /* Check validity of the file object */\r
5357 if (res != FR_OK || (res = (FRESULT)fp->err) != FR_OK) LEAVE_FF(fs, res);\r
5358 if (fsz == 0 || fp->obj.objsize != 0 || !(fp->flag & FA_WRITE)) LEAVE_FF(fs, FR_DENIED);\r
289f6a14 5359#if FF_FS_EXFAT\r
70702af1
L
5360 if (fs->fs_type != FS_EXFAT && fsz >= 0x100000000) LEAVE_FF(fs, FR_DENIED); /* Check if in size limit */\r
5361#endif\r
5362 n = (DWORD)fs->csize * SS(fs); /* Cluster size */\r
5363 tcl = (DWORD)(fsz / n) + ((fsz & (n - 1)) ? 1 : 0); /* Number of clusters required */\r
5364 stcl = fs->last_clst; lclst = 0;\r
5365 if (stcl < 2 || stcl >= fs->n_fatent) stcl = 2;\r
5366\r
289f6a14 5367#if FF_FS_EXFAT\r
70702af1
L
5368 if (fs->fs_type == FS_EXFAT) {\r
5369 scl = find_bitmap(fs, stcl, tcl); /* Find a contiguous cluster block */\r
5370 if (scl == 0) res = FR_DENIED; /* No contiguous cluster block was found */\r
5371 if (scl == 0xFFFFFFFF) res = FR_DISK_ERR;\r
289f6a14
L
5372 if (res == FR_OK) { /* A contiguous free area is found */\r
5373 if (opt) { /* Allocate it now */\r
70702af1
L
5374 res = change_bitmap(fs, scl, tcl, 1); /* Mark the cluster block 'in use' */\r
5375 lclst = scl + tcl - 1;\r
289f6a14 5376 } else { /* Set it as suggested point for next allocation */\r
70702af1
L
5377 lclst = scl - 1;\r
5378 }\r
5379 }\r
5380 } else\r
5381#endif\r
5382 {\r
5383 scl = clst = stcl; ncl = 0;\r
5384 for (;;) { /* Find a contiguous cluster block */\r
5385 n = get_fat(&fp->obj, clst);\r
5386 if (++clst >= fs->n_fatent) clst = 2;\r
5387 if (n == 1) { res = FR_INT_ERR; break; }\r
5388 if (n == 0xFFFFFFFF) { res = FR_DISK_ERR; break; }\r
5389 if (n == 0) { /* Is it a free cluster? */\r
5390 if (++ncl == tcl) break; /* Break if a contiguous cluster block is found */\r
5391 } else {\r
5392 scl = clst; ncl = 0; /* Not a free cluster */\r
5393 }\r
5394 if (clst == stcl) { res = FR_DENIED; break; } /* No contiguous cluster? */\r
5395 }\r
289f6a14
L
5396 if (res == FR_OK) { /* A contiguous free area is found */\r
5397 if (opt) { /* Allocate it now */\r
70702af1
L
5398 for (clst = scl, n = tcl; n; clst++, n--) { /* Create a cluster chain on the FAT */\r
5399 res = put_fat(fs, clst, (n == 1) ? 0xFFFFFFFF : clst + 1);\r
5400 if (res != FR_OK) break;\r
5401 lclst = clst;\r
5402 }\r
289f6a14 5403 } else { /* Set it as suggested point for next allocation */\r
70702af1
L
5404 lclst = scl - 1;\r
5405 }\r
5406 }\r
5407 }\r
5408\r
5409 if (res == FR_OK) {\r
5410 fs->last_clst = lclst; /* Set suggested start cluster to start next */\r
289f6a14 5411 if (opt) { /* Is it allocated now? */\r
70702af1
L
5412 fp->obj.sclust = scl; /* Update object allocation information */\r
5413 fp->obj.objsize = fsz;\r
289f6a14 5414 if (FF_FS_EXFAT) fp->obj.stat = 2; /* Set status 'contiguous chain' */\r
70702af1 5415 fp->flag |= FA_MODIFIED;\r
289f6a14 5416 if (fs->free_clst <= fs->n_fatent - 2) { /* Update FSINFO */\r
70702af1
L
5417 fs->free_clst -= tcl;\r
5418 fs->fsi_flag |= 1;\r
5419 }\r
5420 }\r
5421 }\r
5422\r
5423 LEAVE_FF(fs, res);\r
5424}\r
5425\r
289f6a14 5426#endif /* FF_USE_EXPAND && !FF_FS_READONLY */\r
70702af1
L
5427\r
5428\r
5429\r
289f6a14 5430#if FF_USE_FORWARD\r
53668523 5431/*-----------------------------------------------------------------------*/\r
289f6a14 5432/* Forward Data to the Stream Directly */\r
53668523 5433/*-----------------------------------------------------------------------*/\r
53668523
L
5434\r
5435FRESULT f_forward (\r
5436 FIL* fp, /* Pointer to the file object */\r
5437 UINT (*func)(const BYTE*,UINT), /* Pointer to the streaming function */\r
5438 UINT btf, /* Number of bytes to forward */\r
5439 UINT* bf /* Pointer to number of bytes forwarded */\r
5440)\r
5441{\r
5442 FRESULT res;\r
70702af1
L
5443 FATFS *fs;\r
5444 DWORD clst, sect;\r
5445 FSIZE_t remain;\r
5446 UINT rcnt, csect;\r
5447 BYTE *dbuf;\r
53668523
L
5448\r
5449\r
5450 *bf = 0; /* Clear transfer byte counter */\r
70702af1
L
5451 res = validate(&fp->obj, &fs); /* Check validity of the file object */\r
5452 if (res != FR_OK || (res = (FRESULT)fp->err) != FR_OK) LEAVE_FF(fs, res);\r
5453 if (!(fp->flag & FA_READ)) LEAVE_FF(fs, FR_DENIED); /* Check access mode */\r
53668523 5454\r
70702af1 5455 remain = fp->obj.objsize - fp->fptr;\r
53668523
L
5456 if (btf > remain) btf = (UINT)remain; /* Truncate btf by remaining bytes */\r
5457\r
70702af1 5458 for ( ; btf && (*func)(0, 0); /* Repeat until all data transferred or stream goes busy */\r
53668523 5459 fp->fptr += rcnt, *bf += rcnt, btf -= rcnt) {\r
70702af1
L
5460 csect = (UINT)(fp->fptr / SS(fs) & (fs->csize - 1)); /* Sector offset in the cluster */\r
5461 if (fp->fptr % SS(fs) == 0) { /* On the sector boundary? */\r
5462 if (csect == 0) { /* On the cluster boundary? */\r
53668523 5463 clst = (fp->fptr == 0) ? /* On the top of the file? */\r
70702af1
L
5464 fp->obj.sclust : get_fat(&fp->obj, fp->clust);\r
5465 if (clst <= 1) ABORT(fs, FR_INT_ERR);\r
5466 if (clst == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR);\r
53668523
L
5467 fp->clust = clst; /* Update current cluster */\r
5468 }\r
5469 }\r
289f6a14
L
5470 sect = clst2sect(fs, fp->clust); /* Get current data sector */\r
5471 if (sect == 0) ABORT(fs, FR_INT_ERR);\r
53668523 5472 sect += csect;\r
289f6a14 5473#if FF_FS_TINY\r
70702af1
L
5474 if (move_window(fs, sect) != FR_OK) ABORT(fs, FR_DISK_ERR); /* Move sector window to the file data */\r
5475 dbuf = fs->win;\r
5476#else\r
5477 if (fp->sect != sect) { /* Fill sector cache with file data */\r
289f6a14 5478#if !FF_FS_READONLY\r
70702af1 5479 if (fp->flag & FA_DIRTY) { /* Write-back dirty sector cache */\r
289f6a14 5480 if (disk_write(fs->pdrv, fp->buf, fp->sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR);\r
70702af1
L
5481 fp->flag &= (BYTE)~FA_DIRTY;\r
5482 }\r
5483#endif\r
289f6a14 5484 if (disk_read(fs->pdrv, fp->buf, sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR);\r
70702af1
L
5485 }\r
5486 dbuf = fp->buf;\r
5487#endif\r
5488 fp->sect = sect;\r
5489 rcnt = SS(fs) - (UINT)fp->fptr % SS(fs); /* Number of bytes left in the sector */\r
5490 if (rcnt > btf) rcnt = btf; /* Clip it by btr if needed */\r
5491 rcnt = (*func)(dbuf + ((UINT)fp->fptr % SS(fs)), rcnt); /* Forward the file data */\r
289f6a14 5492 if (rcnt == 0) ABORT(fs, FR_INT_ERR);\r
53668523
L
5493 }\r
5494\r
70702af1 5495 LEAVE_FF(fs, FR_OK);\r
53668523 5496}\r
289f6a14 5497#endif /* FF_USE_FORWARD */\r
53668523
L
5498\r
5499\r
5500\r
289f6a14 5501#if FF_USE_MKFS && !FF_FS_READONLY\r
53668523 5502/*-----------------------------------------------------------------------*/\r
289f6a14 5503/* Create an FAT/exFAT volume */\r
53668523 5504/*-----------------------------------------------------------------------*/\r
53668523
L
5505\r
5506FRESULT f_mkfs (\r
5507 const TCHAR* path, /* Logical drive number */\r
70702af1 5508 BYTE opt, /* Format option */\r
289f6a14
L
5509 DWORD au, /* Size of allocation unit (cluster) [byte] */\r
5510 void* work, /* Pointer to working buffer (null: use heap memory) */\r
5511 UINT len /* Size of working buffer [byte] */\r
53668523
L
5512)\r
5513{\r
289f6a14
L
5514 const UINT n_fats = 1; /* Number of FATs for FAT/FAT32 volume (1 or 2) */\r
5515 const UINT n_rootdir = 512; /* Number of root directory entries for FAT volume */\r
5516 static const WORD cst[] = {1, 4, 16, 64, 256, 512, 0}; /* Cluster size boundary for FAT volume (4Ks unit) */\r
70702af1
L
5517 static const WORD cst32[] = {1, 2, 4, 8, 16, 32, 0}; /* Cluster size boundary for FAT32 volume (128Ks unit) */\r
5518 BYTE fmt, sys, *buf, *pte, pdrv, part;\r
289f6a14 5519 WORD ss; /* Sector size */\r
70702af1
L
5520 DWORD szb_buf, sz_buf, sz_blk, n_clst, pau, sect, nsect, n;\r
5521 DWORD b_vol, b_fat, b_data; /* Base LBA for volume, fat, data */\r
5522 DWORD sz_vol, sz_rsv, sz_fat, sz_dir; /* Size for volume, fat, dir, data */\r
53668523 5523 UINT i;\r
70702af1 5524 int vol;\r
53668523 5525 DSTATUS stat;\r
289f6a14 5526#if FF_USE_TRIM || FF_FS_EXFAT\r
70702af1
L
5527 DWORD tbl[3];\r
5528#endif\r
53668523
L
5529\r
5530\r
5531 /* Check mounted drive and clear work area */\r
70702af1 5532 vol = get_ldnumber(&path); /* Get target logical drive */\r
53668523 5533 if (vol < 0) return FR_INVALID_DRIVE;\r
289f6a14 5534 if (FatFs[vol]) FatFs[vol]->fs_type = 0; /* Clear the volume if mounted */\r
53668523 5535 pdrv = LD2PD(vol); /* Physical drive */\r
70702af1 5536 part = LD2PT(vol); /* Partition (0:create as new, 1-4:get from partition table) */\r
53668523 5537\r
70702af1 5538 /* Check physical drive status */\r
53668523
L
5539 stat = disk_initialize(pdrv);\r
5540 if (stat & STA_NOINIT) return FR_NOT_READY;\r
5541 if (stat & STA_PROTECT) return FR_WRITE_PROTECTED;\r
70702af1 5542 if (disk_ioctl(pdrv, GET_BLOCK_SIZE, &sz_blk) != RES_OK || !sz_blk || sz_blk > 32768 || (sz_blk & (sz_blk - 1))) sz_blk = 1; /* Erase block to align data area */\r
289f6a14 5543#if FF_MAX_SS != FF_MIN_SS /* Get sector size of the medium if variable sector size cfg. */\r
70702af1 5544 if (disk_ioctl(pdrv, GET_SECTOR_SIZE, &ss) != RES_OK) return FR_DISK_ERR;\r
289f6a14 5545 if (ss > FF_MAX_SS || ss < FF_MIN_SS || (ss & (ss - 1))) return FR_DISK_ERR;\r
70702af1 5546#else\r
289f6a14 5547 ss = FF_MAX_SS;\r
53668523 5548#endif\r
70702af1
L
5549 if ((au != 0 && au < ss) || au > 0x1000000 || (au & (au - 1))) return FR_INVALID_PARAMETER; /* Check if au is valid */\r
5550 au /= ss; /* Cluster size in unit of sector */\r
5551\r
5552 /* Get working buffer */\r
289f6a14
L
5553#if FF_USE_LFN == 3\r
5554 if (!work) { /* Use heap memory for working buffer */\r
5555 for (szb_buf = MAX_MALLOC, buf = 0; szb_buf >= ss && (buf = ff_memalloc(szb_buf)) == 0; szb_buf /= 2) ;\r
5556 sz_buf = szb_buf / ss; /* Size of working buffer (sector) */\r
5557 } else\r
5558#endif\r
5559 {\r
5560 buf = (BYTE*)work; /* Working buffer */\r
5561 sz_buf = len / ss; /* Size of working buffer (sector) */\r
5562 szb_buf = sz_buf * ss; /* Size of working buffer (byte) */\r
5563 }\r
5564 if (!buf || sz_buf == 0) return FR_NOT_ENOUGH_CORE;\r
70702af1
L
5565\r
5566 /* Determine where the volume to be located (b_vol, sz_vol) */\r
289f6a14 5567 if (FF_MULTI_PARTITION && part != 0) {\r
53668523 5568 /* Get partition information from partition table in the MBR */\r
289f6a14
L
5569 if (disk_read(pdrv, buf, 0, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); /* Load MBR */\r
5570 if (ld_word(buf + BS_55AA) != 0xAA55) LEAVE_MKFS(FR_MKFS_ABORTED); /* Check if MBR is valid */\r
70702af1 5571 pte = buf + (MBR_Table + (part - 1) * SZ_PTE);\r
289f6a14 5572 if (pte[PTE_System] == 0) LEAVE_MKFS(FR_MKFS_ABORTED); /* No partition? */\r
70702af1
L
5573 b_vol = ld_dword(pte + PTE_StLba); /* Get volume start sector */\r
5574 sz_vol = ld_dword(pte + PTE_SizLba); /* Get volume size */\r
53668523 5575 } else {\r
70702af1 5576 /* Create a single-partition in this function */\r
289f6a14 5577 if (disk_ioctl(pdrv, GET_SECTOR_COUNT, &sz_vol) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);\r
70702af1 5578 b_vol = (opt & FM_SFD) ? 0 : 63; /* Volume start sector */\r
289f6a14 5579 if (sz_vol < b_vol) LEAVE_MKFS(FR_MKFS_ABORTED);\r
70702af1 5580 sz_vol -= b_vol; /* Volume size */\r
53668523 5581 }\r
289f6a14 5582 if (sz_vol < 128) LEAVE_MKFS(FR_MKFS_ABORTED); /* Check if volume size is >=128s */\r
53668523 5583\r
70702af1
L
5584 /* Pre-determine the FAT type */\r
5585 do {\r
289f6a14 5586 if (FF_FS_EXFAT && (opt & FM_EXFAT)) { /* exFAT possible? */\r
70702af1
L
5587 if ((opt & FM_ANY) == FM_EXFAT || sz_vol >= 0x4000000 || au > 128) { /* exFAT only, vol >= 64Ms or au > 128s ? */\r
5588 fmt = FS_EXFAT; break;\r
5589 }\r
5590 }\r
289f6a14 5591 if (au > 128) LEAVE_MKFS(FR_INVALID_PARAMETER); /* Too large au for FAT/FAT32 */\r
70702af1
L
5592 if (opt & FM_FAT32) { /* FAT32 possible? */\r
5593 if ((opt & FM_ANY) == FM_FAT32 || !(opt & FM_FAT)) { /* FAT32 only or no-FAT? */\r
5594 fmt = FS_FAT32; break;\r
5595 }\r
5596 }\r
289f6a14 5597 if (!(opt & FM_FAT)) LEAVE_MKFS(FR_INVALID_PARAMETER); /* no-FAT? */\r
70702af1
L
5598 fmt = FS_FAT16;\r
5599 } while (0);\r
5600\r
289f6a14 5601#if FF_FS_EXFAT\r
70702af1
L
5602 if (fmt == FS_EXFAT) { /* Create an exFAT volume */\r
5603 DWORD szb_bit, szb_case, sum, nb, cl;\r
5604 WCHAR ch, si;\r
5605 UINT j, st;\r
5606 BYTE b;\r
5607\r
289f6a14
L
5608 if (sz_vol < 0x1000) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too small volume? */\r
5609#if FF_USE_TRIM\r
5610 tbl[0] = b_vol; tbl[1] = b_vol + sz_vol - 1; /* Inform the device the volume area may be erased */\r
70702af1
L
5611 disk_ioctl(pdrv, CTRL_TRIM, tbl);\r
5612#endif\r
5613 /* Determine FAT location, data location and number of clusters */\r
289f6a14 5614 if (au == 0) { /* au auto-selection */\r
70702af1
L
5615 au = 8;\r
5616 if (sz_vol >= 0x80000) au = 64; /* >= 512Ks */\r
5617 if (sz_vol >= 0x4000000) au = 256; /* >= 64Ms */\r
5618 }\r
5619 b_fat = b_vol + 32; /* FAT start at offset 32 */\r
5620 sz_fat = ((sz_vol / au + 2) * 4 + ss - 1) / ss; /* Number of FAT sectors */\r
5621 b_data = (b_fat + sz_fat + sz_blk - 1) & ~(sz_blk - 1); /* Align data area to the erase block boundary */\r
289f6a14 5622 if (b_data >= sz_vol / 2) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too small volume? */\r
70702af1 5623 n_clst = (sz_vol - (b_data - b_vol)) / au; /* Number of clusters */\r
289f6a14
L
5624 if (n_clst <16) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too few clusters? */\r
5625 if (n_clst > MAX_EXFAT) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too many clusters? */\r
70702af1
L
5626\r
5627 szb_bit = (n_clst + 7) / 8; /* Size of allocation bitmap */\r
5628 tbl[0] = (szb_bit + au * ss - 1) / (au * ss); /* Number of allocation bitmap clusters */\r
5629\r
5630 /* Create a compressed up-case table */\r
5631 sect = b_data + au * tbl[0]; /* Table start sector */\r
5632 sum = 0; /* Table checksum to be stored in the 82 entry */\r
289f6a14 5633 st = 0; si = 0; i = 0; j = 0; szb_case = 0;\r
70702af1
L
5634 do {\r
5635 switch (st) {\r
5636 case 0:\r
289f6a14 5637 ch = (WCHAR)ff_wtoupper(si); /* Get an up-case char */\r
70702af1
L
5638 if (ch != si) {\r
5639 si++; break; /* Store the up-case char if exist */\r
5640 }\r
5641 for (j = 1; (WCHAR)(si + j) && (WCHAR)(si + j) == ff_wtoupper((WCHAR)(si + j)); j++) ; /* Get run length of no-case block */\r
5642 if (j >= 128) {\r
5643 ch = 0xFFFF; st = 2; break; /* Compress the no-case block if run is >= 128 */\r
5644 }\r
5645 st = 1; /* Do not compress short run */\r
289f6a14 5646 /* go to next case */\r
70702af1
L
5647 case 1:\r
5648 ch = si++; /* Fill the short run */\r
5649 if (--j == 0) st = 0;\r
5650 break;\r
289f6a14 5651\r
70702af1 5652 default:\r
289f6a14 5653 ch = (WCHAR)j; si += (WCHAR)j; /* Number of chars to skip */\r
70702af1
L
5654 st = 0;\r
5655 }\r
5656 sum = xsum32(buf[i + 0] = (BYTE)ch, sum); /* Put it into the write buffer */\r
5657 sum = xsum32(buf[i + 1] = (BYTE)(ch >> 8), sum);\r
5658 i += 2; szb_case += 2;\r
289f6a14 5659 if (si == 0 || i == szb_buf) { /* Write buffered data when buffer full or end of process */\r
70702af1 5660 n = (i + ss - 1) / ss;\r
289f6a14 5661 if (disk_write(pdrv, buf, sect, n) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);\r
70702af1
L
5662 sect += n; i = 0;\r
5663 }\r
5664 } while (si);\r
5665 tbl[1] = (szb_case + au * ss - 1) / (au * ss); /* Number of up-case table clusters */\r
5666 tbl[2] = 1; /* Number of root dir clusters */\r
53668523 5667\r
70702af1
L
5668 /* Initialize the allocation bitmap */\r
5669 sect = b_data; nsect = (szb_bit + ss - 1) / ss; /* Start of bitmap and number of sectors */\r
5670 nb = tbl[0] + tbl[1] + tbl[2]; /* Number of clusters in-use by system */\r
5671 do {\r
5672 mem_set(buf, 0, szb_buf);\r
5673 for (i = 0; nb >= 8 && i < szb_buf; buf[i++] = 0xFF, nb -= 8) ;\r
289f6a14 5674 for (b = 1; nb != 0 && i < szb_buf; buf[i] |= b, b <<= 1, nb--) ;\r
70702af1 5675 n = (nsect > sz_buf) ? sz_buf : nsect; /* Write the buffered data */\r
289f6a14 5676 if (disk_write(pdrv, buf, sect, n) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);\r
70702af1
L
5677 sect += n; nsect -= n;\r
5678 } while (nsect);\r
5679\r
5680 /* Initialize the FAT */\r
5681 sect = b_fat; nsect = sz_fat; /* Start of FAT and number of FAT sectors */\r
5682 j = nb = cl = 0;\r
5683 do {\r
5684 mem_set(buf, 0, szb_buf); i = 0; /* Clear work area and reset write index */\r
5685 if (cl == 0) { /* Set entry 0 and 1 */\r
5686 st_dword(buf + i, 0xFFFFFFF8); i += 4; cl++;\r
5687 st_dword(buf + i, 0xFFFFFFFF); i += 4; cl++;\r
5688 }\r
5689 do { /* Create chains of bitmap, up-case and root dir */\r
289f6a14 5690 while (nb != 0 && i < szb_buf) { /* Create a chain */\r
70702af1
L
5691 st_dword(buf + i, (nb > 1) ? cl + 1 : 0xFFFFFFFF);\r
5692 i += 4; cl++; nb--;\r
5693 }\r
289f6a14
L
5694 if (nb == 0 && j < 3) nb = tbl[j++]; /* Next chain */\r
5695 } while (nb != 0 && i < szb_buf);\r
70702af1 5696 n = (nsect > sz_buf) ? sz_buf : nsect; /* Write the buffered data */\r
289f6a14 5697 if (disk_write(pdrv, buf, sect, n) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);\r
70702af1
L
5698 sect += n; nsect -= n;\r
5699 } while (nsect);\r
5700\r
5701 /* Initialize the root directory */\r
5702 mem_set(buf, 0, szb_buf);\r
5703 buf[SZDIRE * 0 + 0] = 0x83; /* 83 entry (volume label) */\r
5704 buf[SZDIRE * 1 + 0] = 0x81; /* 81 entry (allocation bitmap) */\r
289f6a14
L
5705 st_dword(buf + SZDIRE * 1 + 20, 2); /* cluster */\r
5706 st_dword(buf + SZDIRE * 1 + 24, szb_bit); /* size */\r
70702af1 5707 buf[SZDIRE * 2 + 0] = 0x82; /* 82 entry (up-case table) */\r
289f6a14
L
5708 st_dword(buf + SZDIRE * 2 + 4, sum); /* sum */\r
5709 st_dword(buf + SZDIRE * 2 + 20, 2 + tbl[0]); /* cluster */\r
5710 st_dword(buf + SZDIRE * 2 + 24, szb_case); /* size */\r
70702af1
L
5711 sect = b_data + au * (tbl[0] + tbl[1]); nsect = au; /* Start of the root directory and number of sectors */\r
5712 do { /* Fill root directory sectors */\r
5713 n = (nsect > sz_buf) ? sz_buf : nsect;\r
289f6a14 5714 if (disk_write(pdrv, buf, sect, n) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);\r
70702af1
L
5715 mem_set(buf, 0, ss);\r
5716 sect += n; nsect -= n;\r
5717 } while (nsect);\r
5718\r
5719 /* Create two set of the exFAT VBR blocks */\r
5720 sect = b_vol;\r
5721 for (n = 0; n < 2; n++) {\r
5722 /* Main record (+0) */\r
5723 mem_set(buf, 0, ss);\r
5724 mem_cpy(buf + BS_JmpBoot, "\xEB\x76\x90" "EXFAT ", 11); /* Boot jump code (x86), OEM name */\r
5725 st_dword(buf + BPB_VolOfsEx, b_vol); /* Volume offset in the physical drive [sector] */\r
5726 st_dword(buf + BPB_TotSecEx, sz_vol); /* Volume size [sector] */\r
5727 st_dword(buf + BPB_FatOfsEx, b_fat - b_vol); /* FAT offset [sector] */\r
5728 st_dword(buf + BPB_FatSzEx, sz_fat); /* FAT size [sector] */\r
5729 st_dword(buf + BPB_DataOfsEx, b_data - b_vol); /* Data offset [sector] */\r
5730 st_dword(buf + BPB_NumClusEx, n_clst); /* Number of clusters */\r
5731 st_dword(buf + BPB_RootClusEx, 2 + tbl[0] + tbl[1]); /* Root dir cluster # */\r
5732 st_dword(buf + BPB_VolIDEx, GET_FATTIME()); /* VSN */\r
289f6a14 5733 st_word(buf + BPB_FSVerEx, 0x100); /* Filesystem version (1.00) */\r
70702af1
L
5734 for (buf[BPB_BytsPerSecEx] = 0, i = ss; i >>= 1; buf[BPB_BytsPerSecEx]++) ; /* Log2 of sector size [byte] */\r
5735 for (buf[BPB_SecPerClusEx] = 0, i = au; i >>= 1; buf[BPB_SecPerClusEx]++) ; /* Log2 of cluster size [sector] */\r
5736 buf[BPB_NumFATsEx] = 1; /* Number of FATs */\r
5737 buf[BPB_DrvNumEx] = 0x80; /* Drive number (for int13) */\r
5738 st_word(buf + BS_BootCodeEx, 0xFEEB); /* Boot code (x86) */\r
5739 st_word(buf + BS_55AA, 0xAA55); /* Signature (placed here regardless of sector size) */\r
5740 for (i = sum = 0; i < ss; i++) { /* VBR checksum */\r
5741 if (i != BPB_VolFlagEx && i != BPB_VolFlagEx + 1 && i != BPB_PercInUseEx) sum = xsum32(buf[i], sum);\r
5742 }\r
289f6a14 5743 if (disk_write(pdrv, buf, sect++, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);\r
70702af1
L
5744 /* Extended bootstrap record (+1..+8) */\r
5745 mem_set(buf, 0, ss);\r
5746 st_word(buf + ss - 2, 0xAA55); /* Signature (placed at end of sector) */\r
5747 for (j = 1; j < 9; j++) {\r
5748 for (i = 0; i < ss; sum = xsum32(buf[i++], sum)) ; /* VBR checksum */\r
289f6a14 5749 if (disk_write(pdrv, buf, sect++, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);\r
70702af1
L
5750 }\r
5751 /* OEM/Reserved record (+9..+10) */\r
5752 mem_set(buf, 0, ss);\r
5753 for ( ; j < 11; j++) {\r
5754 for (i = 0; i < ss; sum = xsum32(buf[i++], sum)) ; /* VBR checksum */\r
289f6a14 5755 if (disk_write(pdrv, buf, sect++, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);\r
70702af1
L
5756 }\r
5757 /* Sum record (+11) */\r
5758 for (i = 0; i < ss; i += 4) st_dword(buf + i, sum); /* Fill with checksum value */\r
289f6a14 5759 if (disk_write(pdrv, buf, sect++, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);\r
70702af1 5760 }\r
53668523 5761\r
70702af1 5762 } else\r
289f6a14
L
5763#endif /* FF_FS_EXFAT */\r
5764 { /* Create an FAT/FAT32 volume */\r
70702af1
L
5765 do {\r
5766 pau = au;\r
5767 /* Pre-determine number of clusters and FAT sub-type */\r
5768 if (fmt == FS_FAT32) { /* FAT32 volume */\r
289f6a14 5769 if (pau == 0) { /* au auto-selection */\r
70702af1
L
5770 n = sz_vol / 0x20000; /* Volume size in unit of 128KS */\r
5771 for (i = 0, pau = 1; cst32[i] && cst32[i] <= n; i++, pau <<= 1) ; /* Get from table */\r
5772 }\r
5773 n_clst = sz_vol / pau; /* Number of clusters */\r
5774 sz_fat = (n_clst * 4 + 8 + ss - 1) / ss; /* FAT size [sector] */\r
5775 sz_rsv = 32; /* Number of reserved sectors */\r
5776 sz_dir = 0; /* No static directory */\r
289f6a14
L
5777 if (n_clst <= MAX_FAT16 || n_clst > MAX_FAT32) LEAVE_MKFS(FR_MKFS_ABORTED);\r
5778 } else { /* FAT volume */\r
5779 if (pau == 0) { /* au auto-selection */\r
70702af1
L
5780 n = sz_vol / 0x1000; /* Volume size in unit of 4KS */\r
5781 for (i = 0, pau = 1; cst[i] && cst[i] <= n; i++, pau <<= 1) ; /* Get from table */\r
5782 }\r
5783 n_clst = sz_vol / pau;\r
5784 if (n_clst > MAX_FAT12) {\r
5785 n = n_clst * 2 + 4; /* FAT size [byte] */\r
5786 } else {\r
5787 fmt = FS_FAT12;\r
5788 n = (n_clst * 3 + 1) / 2 + 3; /* FAT size [byte] */\r
5789 }\r
5790 sz_fat = (n + ss - 1) / ss; /* FAT size [sector] */\r
5791 sz_rsv = 1; /* Number of reserved sectors */\r
5792 sz_dir = (DWORD)n_rootdir * SZDIRE / ss; /* Rootdir size [sector] */\r
5793 }\r
5794 b_fat = b_vol + sz_rsv; /* FAT base */\r
5795 b_data = b_fat + sz_fat * n_fats + sz_dir; /* Data base */\r
5796\r
5797 /* Align data base to erase block boundary (for flash memory media) */\r
5798 n = ((b_data + sz_blk - 1) & ~(sz_blk - 1)) - b_data; /* Next nearest erase block from current data base */\r
5799 if (fmt == FS_FAT32) { /* FAT32: Move FAT base */\r
5800 sz_rsv += n; b_fat += n;\r
289f6a14 5801 } else { /* FAT: Expand FAT size */\r
70702af1
L
5802 sz_fat += n / n_fats;\r
5803 }\r
5804\r
5805 /* Determine number of clusters and final check of validity of the FAT sub-type */\r
289f6a14 5806 if (sz_vol < b_data + pau * 16 - b_vol) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too small volume */\r
70702af1
L
5807 n_clst = (sz_vol - sz_rsv - sz_fat * n_fats - sz_dir) / pau;\r
5808 if (fmt == FS_FAT32) {\r
5809 if (n_clst <= MAX_FAT16) { /* Too few clusters for FAT32 */\r
289f6a14
L
5810 if (au == 0 && (au = pau / 2) != 0) continue; /* Adjust cluster size and retry */\r
5811 LEAVE_MKFS(FR_MKFS_ABORTED);\r
70702af1
L
5812 }\r
5813 }\r
5814 if (fmt == FS_FAT16) {\r
5815 if (n_clst > MAX_FAT16) { /* Too many clusters for FAT16 */\r
289f6a14 5816 if (au == 0 && (pau * 2) <= 64) {\r
70702af1
L
5817 au = pau * 2; continue; /* Adjust cluster size and retry */\r
5818 }\r
5819 if ((opt & FM_FAT32)) {\r
5820 fmt = FS_FAT32; continue; /* Switch type to FAT32 and retry */\r
5821 }\r
289f6a14
L
5822 if (au == 0 && (au = pau * 2) <= 128) continue; /* Adjust cluster size and retry */\r
5823 LEAVE_MKFS(FR_MKFS_ABORTED);\r
70702af1
L
5824 }\r
5825 if (n_clst <= MAX_FAT12) { /* Too few clusters for FAT16 */\r
289f6a14
L
5826 if (au == 0 && (au = pau * 2) <= 128) continue; /* Adjust cluster size and retry */\r
5827 LEAVE_MKFS(FR_MKFS_ABORTED);\r
70702af1
L
5828 }\r
5829 }\r
289f6a14 5830 if (fmt == FS_FAT12 && n_clst > MAX_FAT12) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too many clusters for FAT12 */\r
70702af1
L
5831\r
5832 /* Ok, it is the valid cluster configuration */\r
5833 break;\r
5834 } while (1);\r
5835\r
289f6a14 5836#if FF_USE_TRIM\r
70702af1
L
5837 tbl[0] = b_vol; tbl[1] = b_vol + sz_vol - 1; /* Inform the device the volume area can be erased */\r
5838 disk_ioctl(pdrv, CTRL_TRIM, tbl);\r
5839#endif\r
5840 /* Create FAT VBR */\r
5841 mem_set(buf, 0, ss);\r
5842 mem_cpy(buf + BS_JmpBoot, "\xEB\xFE\x90" "MSDOS5.0", 11);/* Boot jump code (x86), OEM name */\r
5843 st_word(buf + BPB_BytsPerSec, ss); /* Sector size [byte] */\r
5844 buf[BPB_SecPerClus] = (BYTE)pau; /* Cluster size [sector] */\r
5845 st_word(buf + BPB_RsvdSecCnt, (WORD)sz_rsv); /* Size of reserved area */\r
5846 buf[BPB_NumFATs] = (BYTE)n_fats; /* Number of FATs */\r
5847 st_word(buf + BPB_RootEntCnt, (WORD)((fmt == FS_FAT32) ? 0 : n_rootdir)); /* Number of root directory entries */\r
5848 if (sz_vol < 0x10000) {\r
5849 st_word(buf + BPB_TotSec16, (WORD)sz_vol); /* Volume size in 16-bit LBA */\r
53668523 5850 } else {\r
70702af1
L
5851 st_dword(buf + BPB_TotSec32, sz_vol); /* Volume size in 32-bit LBA */\r
5852 }\r
5853 buf[BPB_Media] = 0xF8; /* Media descriptor byte */\r
5854 st_word(buf + BPB_SecPerTrk, 63); /* Number of sectors per track (for int13) */\r
5855 st_word(buf + BPB_NumHeads, 255); /* Number of heads (for int13) */\r
5856 st_dword(buf + BPB_HiddSec, b_vol); /* Volume offset in the physical drive [sector] */\r
5857 if (fmt == FS_FAT32) {\r
5858 st_dword(buf + BS_VolID32, GET_FATTIME()); /* VSN */\r
5859 st_dword(buf + BPB_FATSz32, sz_fat); /* FAT size [sector] */\r
5860 st_dword(buf + BPB_RootClus32, 2); /* Root directory cluster # (2) */\r
5861 st_word(buf + BPB_FSInfo32, 1); /* Offset of FSINFO sector (VBR + 1) */\r
5862 st_word(buf + BPB_BkBootSec32, 6); /* Offset of backup VBR (VBR + 6) */\r
5863 buf[BS_DrvNum32] = 0x80; /* Drive number (for int13) */\r
5864 buf[BS_BootSig32] = 0x29; /* Extended boot signature */\r
5865 mem_cpy(buf + BS_VolLab32, "NO NAME " "FAT32 ", 19); /* Volume label, FAT signature */\r
5866 } else {\r
5867 st_dword(buf + BS_VolID, GET_FATTIME()); /* VSN */\r
5868 st_word(buf + BPB_FATSz16, (WORD)sz_fat); /* FAT size [sector] */\r
5869 buf[BS_DrvNum] = 0x80; /* Drive number (for int13) */\r
5870 buf[BS_BootSig] = 0x29; /* Extended boot signature */\r
5871 mem_cpy(buf + BS_VolLab, "NO NAME " "FAT ", 19); /* Volume label, FAT signature */\r
5872 }\r
5873 st_word(buf + BS_55AA, 0xAA55); /* Signature (offset is fixed here regardless of sector size) */\r
289f6a14 5874 if (disk_write(pdrv, buf, b_vol, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); /* Write it to the VBR sector */\r
70702af1
L
5875\r
5876 /* Create FSINFO record if needed */\r
5877 if (fmt == FS_FAT32) {\r
5878 disk_write(pdrv, buf, b_vol + 6, 1); /* Write backup VBR (VBR + 6) */\r
5879 mem_set(buf, 0, ss);\r
5880 st_dword(buf + FSI_LeadSig, 0x41615252);\r
5881 st_dword(buf + FSI_StrucSig, 0x61417272);\r
5882 st_dword(buf + FSI_Free_Count, n_clst - 1); /* Number of free clusters */\r
5883 st_dword(buf + FSI_Nxt_Free, 2); /* Last allocated cluster# */\r
5884 st_word(buf + BS_55AA, 0xAA55);\r
5885 disk_write(pdrv, buf, b_vol + 7, 1); /* Write backup FSINFO (VBR + 7) */\r
5886 disk_write(pdrv, buf, b_vol + 1, 1); /* Write original FSINFO (VBR + 1) */\r
53668523 5887 }\r
53668523 5888\r
70702af1
L
5889 /* Initialize FAT area */\r
5890 mem_set(buf, 0, (UINT)szb_buf);\r
5891 sect = b_fat; /* FAT start sector */\r
5892 for (i = 0; i < n_fats; i++) { /* Initialize FATs each */\r
5893 if (fmt == FS_FAT32) {\r
5894 st_dword(buf + 0, 0xFFFFFFF8); /* Entry 0 */\r
5895 st_dword(buf + 4, 0xFFFFFFFF); /* Entry 1 */\r
5896 st_dword(buf + 8, 0x0FFFFFFF); /* Entry 2 (root directory) */\r
5897 } else {\r
5898 st_dword(buf + 0, (fmt == FS_FAT12) ? 0xFFFFF8 : 0xFFFFFFF8); /* Entry 0 and 1 */\r
5899 }\r
5900 nsect = sz_fat; /* Number of FAT sectors */\r
5901 do { /* Fill FAT sectors */\r
5902 n = (nsect > sz_buf) ? sz_buf : nsect;\r
289f6a14 5903 if (disk_write(pdrv, buf, sect, (UINT)n) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);\r
70702af1
L
5904 mem_set(buf, 0, ss);\r
5905 sect += n; nsect -= n;\r
5906 } while (nsect);\r
53668523 5907 }\r
53668523 5908\r
70702af1
L
5909 /* Initialize root directory (fill with zero) */\r
5910 nsect = (fmt == FS_FAT32) ? pau : sz_dir; /* Number of root directory sectors */\r
5911 do {\r
5912 n = (nsect > sz_buf) ? sz_buf : nsect;\r
289f6a14 5913 if (disk_write(pdrv, buf, sect, (UINT)n) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);\r
70702af1
L
5914 sect += n; nsect -= n;\r
5915 } while (nsect);\r
53668523 5916 }\r
70702af1
L
5917\r
5918 /* Determine system ID in the partition table */\r
289f6a14 5919 if (FF_FS_EXFAT && fmt == FS_EXFAT) {\r
70702af1 5920 sys = 0x07; /* HPFS/NTFS/exFAT */\r
53668523 5921 } else {\r
70702af1
L
5922 if (fmt == FS_FAT32) {\r
5923 sys = 0x0C; /* FAT32X */\r
53668523 5924 } else {\r
70702af1 5925 if (sz_vol >= 0x10000) {\r
289f6a14 5926 sys = 0x06; /* FAT12/16 (large) */\r
70702af1 5927 } else {\r
289f6a14 5928 sys = (fmt == FS_FAT16) ? 0x04 : 0x01; /* FAT16 : FAT12 */\r
70702af1 5929 }\r
53668523
L
5930 }\r
5931 }\r
5932\r
289f6a14
L
5933 /* Update partition information */\r
5934 if (FF_MULTI_PARTITION && part != 0) { /* Created in the existing partition */\r
70702af1 5935 /* Update system ID in the partition table */\r
289f6a14
L
5936 if (disk_read(pdrv, buf, 0, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); /* Read the MBR */\r
5937 buf[MBR_Table + (part - 1) * SZ_PTE + PTE_System] = sys; /* Set system ID */\r
5938 if (disk_write(pdrv, buf, 0, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); /* Write it back to the MBR */\r
5939 } else { /* Created as a new single partition */\r
5940 if (!(opt & FM_SFD)) { /* Create partition table if in FDISK format */\r
70702af1
L
5941 mem_set(buf, 0, ss);\r
5942 st_word(buf + BS_55AA, 0xAA55); /* MBR signature */\r
5943 pte = buf + MBR_Table; /* Create partition table for single partition in the drive */\r
5944 pte[PTE_Boot] = 0; /* Boot indicator */\r
5945 pte[PTE_StHead] = 1; /* Start head */\r
5946 pte[PTE_StSec] = 1; /* Start sector */\r
5947 pte[PTE_StCyl] = 0; /* Start cylinder */\r
5948 pte[PTE_System] = sys; /* System type */\r
289f6a14 5949 n = (b_vol + sz_vol) / (63 * 255); /* (End CHS may be invalid) */\r
70702af1 5950 pte[PTE_EdHead] = 254; /* End head */\r
289f6a14 5951 pte[PTE_EdSec] = (BYTE)(((n >> 2) & 0xC0) | 63); /* End sector */\r
70702af1
L
5952 pte[PTE_EdCyl] = (BYTE)n; /* End cylinder */\r
5953 st_dword(pte + PTE_StLba, b_vol); /* Start offset in LBA */\r
5954 st_dword(pte + PTE_SizLba, sz_vol); /* Size in sectors */\r
289f6a14 5955 if (disk_write(pdrv, buf, 0, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); /* Write it to the MBR */\r
70702af1 5956 }\r
53668523 5957 }\r
53668523 5958\r
289f6a14 5959 if (disk_ioctl(pdrv, CTRL_SYNC, 0) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);\r
53668523 5960\r
289f6a14 5961 LEAVE_MKFS(FR_OK);\r
53668523
L
5962}\r
5963\r
5964\r
5965\r
289f6a14 5966#if FF_MULTI_PARTITION\r
53668523 5967/*-----------------------------------------------------------------------*/\r
289f6a14 5968/* Create Partition Table on the Physical Drive */\r
53668523
L
5969/*-----------------------------------------------------------------------*/\r
5970\r
5971FRESULT f_fdisk (\r
5972 BYTE pdrv, /* Physical drive number */\r
70702af1 5973 const DWORD* szt, /* Pointer to the size table for each partitions */\r
289f6a14 5974 void* work /* Pointer to the working buffer (null: use heap memory) */\r
53668523
L
5975)\r
5976{\r
5977 UINT i, n, sz_cyl, tot_cyl, b_cyl, e_cyl, p_cyl;\r
5978 BYTE s_hd, e_hd, *p, *buf = (BYTE*)work;\r
5979 DSTATUS stat;\r
5980 DWORD sz_disk, sz_part, s_part;\r
289f6a14 5981 FRESULT res;\r
53668523
L
5982\r
5983\r
5984 stat = disk_initialize(pdrv);\r
5985 if (stat & STA_NOINIT) return FR_NOT_READY;\r
5986 if (stat & STA_PROTECT) return FR_WRITE_PROTECTED;\r
5987 if (disk_ioctl(pdrv, GET_SECTOR_COUNT, &sz_disk)) return FR_DISK_ERR;\r
5988\r
289f6a14
L
5989 buf = (BYTE*)work;\r
5990#if FF_USE_LFN == 3\r
5991 if (!buf) buf = ff_memalloc(FF_MAX_SS); /* Use heap memory for working buffer */\r
5992#endif\r
5993 if (!buf) return FR_NOT_ENOUGH_CORE;\r
5994\r
5995 /* Determine the CHS without any consideration of the drive geometry */\r
53668523
L
5996 for (n = 16; n < 256 && sz_disk / n / 63 > 1024; n *= 2) ;\r
5997 if (n == 256) n--;\r
289f6a14 5998 e_hd = (BYTE)(n - 1);\r
53668523
L
5999 sz_cyl = 63 * n;\r
6000 tot_cyl = sz_disk / sz_cyl;\r
6001\r
6002 /* Create partition table */\r
289f6a14 6003 mem_set(buf, 0, FF_MAX_SS);\r
53668523
L
6004 p = buf + MBR_Table; b_cyl = 0;\r
6005 for (i = 0; i < 4; i++, p += SZ_PTE) {\r
289f6a14
L
6006 p_cyl = (szt[i] <= 100U) ? (DWORD)tot_cyl * szt[i] / 100 : szt[i] / sz_cyl; /* Number of cylinders */\r
6007 if (p_cyl == 0) continue;\r
53668523
L
6008 s_part = (DWORD)sz_cyl * b_cyl;\r
6009 sz_part = (DWORD)sz_cyl * p_cyl;\r
6010 if (i == 0) { /* Exclude first track of cylinder 0 */\r
6011 s_hd = 1;\r
6012 s_part += 63; sz_part -= 63;\r
6013 } else {\r
6014 s_hd = 0;\r
6015 }\r
289f6a14
L
6016 e_cyl = b_cyl + p_cyl - 1; /* End cylinder */\r
6017 if (e_cyl >= tot_cyl) LEAVE_MKFS(FR_INVALID_PARAMETER);\r
53668523
L
6018\r
6019 /* Set partition table */\r
6020 p[1] = s_hd; /* Start head */\r
289f6a14 6021 p[2] = (BYTE)(((b_cyl >> 2) & 0xC0) | 1); /* Start sector */\r
53668523 6022 p[3] = (BYTE)b_cyl; /* Start cylinder */\r
289f6a14 6023 p[4] = 0x07; /* System type (temporary setting) */\r
53668523 6024 p[5] = e_hd; /* End head */\r
289f6a14 6025 p[6] = (BYTE)(((e_cyl >> 2) & 0xC0) | 63); /* End sector */\r
53668523 6026 p[7] = (BYTE)e_cyl; /* End cylinder */\r
70702af1 6027 st_dword(p + 8, s_part); /* Start sector in LBA */\r
289f6a14 6028 st_dword(p + 12, sz_part); /* Number of sectors */\r
53668523
L
6029\r
6030 /* Next partition */\r
6031 b_cyl += p_cyl;\r
6032 }\r
289f6a14 6033 st_word(p, 0xAA55); /* MBR signature (always at offset 510) */\r
53668523
L
6034\r
6035 /* Write it to the MBR */\r
289f6a14
L
6036 res = (disk_write(pdrv, buf, 0, 1) == RES_OK && disk_ioctl(pdrv, CTRL_SYNC, 0) == RES_OK) ? FR_OK : FR_DISK_ERR;\r
6037 LEAVE_MKFS(res);\r
53668523
L
6038}\r
6039\r
289f6a14
L
6040#endif /* FF_MULTI_PARTITION */\r
6041#endif /* FF_USE_MKFS && !FF_FS_READONLY */\r
53668523
L
6042\r
6043\r
6044\r
6045\r
289f6a14
L
6046#if FF_USE_STRFUNC\r
6047#if FF_USE_LFN && FF_LFN_UNICODE && (FF_STRF_ENCODE < 0 || FF_STRF_ENCODE > 3)\r
6048#error Wrong FF_STRF_ENCODE setting\r
6049#endif\r
53668523 6050/*-----------------------------------------------------------------------*/\r
289f6a14 6051/* Get a String from the File */\r
53668523
L
6052/*-----------------------------------------------------------------------*/\r
6053\r
6054TCHAR* f_gets (\r
6055 TCHAR* buff, /* Pointer to the string buffer to read */\r
289f6a14 6056 int len, /* Size of string buffer (items) */\r
53668523
L
6057 FIL* fp /* Pointer to the file object */\r
6058)\r
6059{\r
289f6a14
L
6060 int nc = 0;\r
6061 TCHAR *p = buff;\r
6062 BYTE s[4];\r
53668523 6063 UINT rc;\r
289f6a14
L
6064 DWORD dc;\r
6065#if FF_USE_LFN && FF_LFN_UNICODE && FF_STRF_ENCODE <= 2\r
6066 WCHAR wc;\r
6067#endif\r
6068#if FF_USE_LFN && FF_LFN_UNICODE && FF_STRF_ENCODE == 3\r
6069 UINT ct;\r
6070#endif\r
53668523 6071\r
289f6a14
L
6072#if FF_USE_LFN && FF_LFN_UNICODE /* With code conversion (Unicode API) */\r
6073 /* Make a room for the character and terminator */\r
6074 if (FF_LFN_UNICODE == 1) len -= (FF_STRF_ENCODE == 0) ? 1 : 2;\r
6075 if (FF_LFN_UNICODE == 2) len -= (FF_STRF_ENCODE == 0) ? 3 : 4;\r
6076 if (FF_LFN_UNICODE == 3) len -= 1;\r
6077 while (nc < len) {\r
6078#if FF_STRF_ENCODE == 0 /* Read a character in ANSI/OEM */\r
53668523
L
6079 f_read(fp, s, 1, &rc);\r
6080 if (rc != 1) break;\r
289f6a14
L
6081 wc = s[0];\r
6082 if (dbc_1st((BYTE)wc)) {\r
6083 f_read(fp, s, 1, &rc);\r
6084 if (rc != 1 || !dbc_2nd(s[0])) continue;\r
6085 wc = wc << 8 | s[0];\r
53668523 6086 }\r
289f6a14
L
6087 dc = ff_oem2uni(wc, CODEPAGE);\r
6088 if (dc == 0) continue;\r
6089#elif FF_STRF_ENCODE == 1 || FF_STRF_ENCODE == 2 /* Read a character in UTF-16LE/BE */\r
53668523
L
6090 f_read(fp, s, 2, &rc);\r
6091 if (rc != 2) break;\r
289f6a14
L
6092 dc = (FF_STRF_ENCODE == 1) ? ld_word(s) : s[0] << 8 | s[1];\r
6093 if (IsSurrogateL(dc)) continue;\r
6094 if (IsSurrogateH(dc)) {\r
6095 f_read(fp, s, 2, &rc);\r
6096 if (rc != 2) break;\r
6097 wc = (FF_STRF_ENCODE == 1) ? ld_word(s) : s[0] << 8 | s[1];\r
6098 if (!IsSurrogateL(wc)) continue;\r
6099 dc = ((dc & 0x3FF) + 0x40) << 10 | (wc & 0x3FF);\r
6100 }\r
6101#else /* Read a character in UTF-8 */\r
53668523
L
6102 f_read(fp, s, 1, &rc);\r
6103 if (rc != 1) break;\r
289f6a14
L
6104 dc = s[0];\r
6105 if (dc >= 0x80) { /* Multi-byte character? */\r
6106 ct = 0;\r
6107 if ((dc & 0xE0) == 0xC0) { dc &= 0x1F; ct = 1; } /* 2-byte? */\r
6108 if ((dc & 0xF0) == 0xE0) { dc &= 0x0F; ct = 2; } /* 3-byte? */\r
6109 if ((dc & 0xF8) == 0xF0) { dc &= 0x07; ct = 3; } /* 4-byte? */\r
6110 if (ct == 0) continue;\r
6111 f_read(fp, s, ct, &rc); /* Get trailing bytes */\r
6112 if (rc != ct) break;\r
6113 rc = 0;\r
6114 do { /* Merge trailing bytes */\r
6115 if ((s[rc] & 0xC0) != 0x80) break;\r
6116 dc = dc << 6 | (s[rc] & 0x3F);\r
6117 } while (++rc < ct);\r
6118 if (rc != ct || dc < 0x80 || IsSurrogate(dc) || dc >= 0x110000) continue; /* Wrong encoding? */\r
6119 }\r
6120#endif\r
6121 if (FF_USE_STRFUNC == 2 && dc == '\r') continue; /* Strip \r off if needed */\r
6122#if FF_LFN_UNICODE == 1 || FF_LFN_UNICODE == 3 /* Output it in UTF-16/32 encoding */\r
6123 if (FF_LFN_UNICODE == 1 && dc >= 0x10000) { /* Out of BMP at UTF-16? */\r
6124 *p++ = (TCHAR)(0xD800 | ((dc >> 10) - 0x40)); nc++; /* Make and output high surrogate */\r
6125 dc = 0xDC00 | (dc & 0x3FF); /* Make low surrogate */\r
6126 }\r
6127 *p++ = (TCHAR)dc; nc++;\r
6128 if (dc == '\n') break; /* End of line? */\r
6129#elif FF_LFN_UNICODE == 2 /* Output it in UTF-8 encoding */\r
6130 if (dc < 0x80) { /* 1-byte */\r
6131 *p++ = (TCHAR)dc;\r
6132 nc++;\r
6133 if (dc == '\n') break; /* End of line? */\r
6134 } else {\r
6135 if (dc < 0x800) { /* 2-byte */\r
6136 *p++ = (TCHAR)(0xC0 | (dc >> 6 & 0x1F));\r
6137 *p++ = (TCHAR)(0x80 | (dc >> 0 & 0x3F));\r
6138 nc += 2;\r
6139 } else {\r
6140 if (dc < 0x10000) { /* 3-byte */\r
6141 *p++ = (TCHAR)(0xE0 | (dc >> 12 & 0x0F));\r
6142 *p++ = (TCHAR)(0x80 | (dc >> 6 & 0x3F));\r
6143 *p++ = (TCHAR)(0x80 | (dc >> 0 & 0x3F));\r
6144 nc += 3;\r
6145 } else { /* 4-byte */\r
6146 *p++ = (TCHAR)(0xF0 | (dc >> 18 & 0x07));\r
6147 *p++ = (TCHAR)(0x80 | (dc >> 12 & 0x3F));\r
6148 *p++ = (TCHAR)(0x80 | (dc >> 6 & 0x3F));\r
6149 *p++ = (TCHAR)(0x80 | (dc >> 0 & 0x3F));\r
6150 nc += 4;\r
6151 }\r
6152 }\r
53668523 6153 }\r
53668523 6154#endif\r
289f6a14
L
6155 }\r
6156\r
6157#else /* Byte-by-byte without any conversion (ANSI/OEM API) */\r
6158 len -= 1; /* Make a room for the terminator */\r
6159 while (nc < len) {\r
53668523
L
6160 f_read(fp, s, 1, &rc);\r
6161 if (rc != 1) break;\r
289f6a14
L
6162 dc = s[0];\r
6163 if (FF_USE_STRFUNC == 2 && dc == '\r') continue;\r
6164 *p++ = (TCHAR)dc; nc++;\r
6165 if (dc == '\n') break;\r
53668523 6166 }\r
289f6a14
L
6167#endif\r
6168\r
6169 *p = 0; /* Terminate the string */\r
6170 return nc ? buff : 0; /* When no data read due to EOF or error, return with error. */\r
53668523
L
6171}\r
6172\r
6173\r
6174\r
70702af1 6175\r
289f6a14 6176#if !FF_FS_READONLY\r
53668523
L
6177#include <stdarg.h>\r
6178/*-----------------------------------------------------------------------*/\r
289f6a14 6179/* Put a Character to the File */\r
53668523
L
6180/*-----------------------------------------------------------------------*/\r
6181\r
289f6a14 6182typedef struct { /* Putchar output buffer and work area */\r
70702af1 6183 FIL *fp; /* Ptr to the writing file */\r
289f6a14
L
6184 int idx, nchr; /* Write index of buf[] (-1:error), number of encoding units written */\r
6185#if FF_USE_LFN && FF_LFN_UNICODE == 1\r
6186 WCHAR hs;\r
6187#elif FF_USE_LFN && FF_LFN_UNICODE == 2\r
6188 BYTE bs[4];\r
6189 UINT wi, ct;\r
6190#endif\r
70702af1 6191 BYTE buf[64]; /* Write buffer */\r
53668523
L
6192} putbuff;\r
6193\r
6194\r
6195static\r
70702af1 6196void putc_bfd ( /* Buffered write with code conversion */\r
53668523
L
6197 putbuff* pb,\r
6198 TCHAR c\r
6199)\r
6200{\r
289f6a14
L
6201 UINT n;\r
6202 int i, nc;\r
6203#if FF_USE_LFN && FF_LFN_UNICODE\r
6204 WCHAR hs, wc;\r
6205#if FF_LFN_UNICODE == 2\r
6206 DWORD dc;\r
6207 TCHAR *tp;\r
6208#endif\r
6209#endif\r
53668523 6210\r
289f6a14 6211 if (FF_USE_STRFUNC == 2 && c == '\n') { /* LF -> CRLF conversion */\r
53668523 6212 putc_bfd(pb, '\r');\r
70702af1 6213 }\r
53668523 6214\r
289f6a14 6215 i = pb->idx; /* Write index of pb->buf[] */\r
53668523 6216 if (i < 0) return;\r
289f6a14 6217 nc = pb->nchr; /* Write unit counter */\r
53668523 6218\r
289f6a14
L
6219#if FF_USE_LFN && FF_LFN_UNICODE\r
6220#if FF_LFN_UNICODE == 1 /* UTF-16 input */\r
6221 if (IsSurrogateH(c)) {\r
6222 pb->hs = c; return;\r
6223 }\r
6224 hs = pb->hs; pb->hs = 0;\r
6225 if (hs != 0) {\r
6226 if (!IsSurrogateL(c)) hs = 0;\r
53668523 6227 } else {\r
289f6a14
L
6228 if (IsSurrogateL(c)) return;\r
6229 }\r
6230 wc = c;\r
6231#elif FF_LFN_UNICODE == 2 /* UTF-8 input */\r
6232 for (;;) {\r
6233 if (pb->ct == 0) { /* Out of multi-byte sequence? */\r
6234 pb->bs[pb->wi = 0] = (BYTE)c; /* Save 1st byte */\r
6235 if ((BYTE)c < 0x80) break; /* 1-byte? */\r
6236 if (((BYTE)c & 0xE0) == 0xC0) pb->ct = 1; /* 2-byte? */\r
6237 if (((BYTE)c & 0xF0) == 0xE0) pb->ct = 2; /* 3-byte? */\r
6238 if (((BYTE)c & 0xF1) == 0xF0) pb->ct = 3; /* 4-byte? */\r
6239 return;\r
6240 } else { /* In the multi-byte sequence */\r
6241 if (((BYTE)c & 0xC0) != 0x80) { /* Broken sequence? */\r
6242 pb->ct = 0; continue;\r
6243 }\r
6244 pb->bs[++pb->wi] = (BYTE)c; /* Save the trailing byte */\r
6245 if (--pb->ct == 0) break; /* End of multi-byte sequence? */\r
6246 return;\r
53668523 6247 }\r
53668523 6248 }\r
289f6a14
L
6249 tp = (TCHAR*)pb->bs;\r
6250 dc = tchar2uni(&tp); /* UTF-8 ==> UTF-16 */\r
6251 if (dc == 0xFFFFFFFF) return;\r
6252 wc = (WCHAR)dc;\r
6253 hs = (WCHAR)(dc >> 16);\r
6254#elif FF_LFN_UNICODE == 3 /* UTF-32 input */\r
6255 if (IsSurrogate(c) || c >= 0x110000) return;\r
6256 if (c >= 0x10000) {\r
6257 hs = (WCHAR)(0xD800 | ((c >> 10) - 0x40)); /* Make high surrogate */\r
6258 wc = 0xDC00 | (c & 0x3FF); /* Make low surrogate */\r
6259 } else {\r
6260 hs = 0;\r
6261 wc = (WCHAR)c;\r
6262 }\r
6263#endif\r
6264\r
6265#if FF_STRF_ENCODE == 1 /* Write a character in UTF-16LE */\r
6266 if (hs != 0) {\r
6267 st_word(&pb->buf[i], hs);\r
6268 i += 2;\r
6269 nc++;\r
6270 }\r
6271 st_word(&pb->buf[i], wc);\r
6272 i += 2;\r
6273#elif FF_STRF_ENCODE == 2 /* Write a character in UTF-16BE */\r
6274 if (hs != 0) {\r
6275 pb->buf[i++] = (BYTE)(hs >> 8);\r
6276 pb->buf[i++] = (BYTE)hs;\r
6277 nc++;\r
6278 }\r
6279 pb->buf[i++] = (BYTE)(wc >> 8);\r
6280 pb->buf[i++] = (BYTE)wc;\r
6281#elif FF_STRF_ENCODE == 3 /* Write it in UTF-8 */\r
6282 if (hs != 0) { /* 4-byte */\r
6283 nc += 3;\r
6284 hs = (hs & 0x3FF) + 0x40;\r
6285 pb->buf[i++] = (BYTE)(0xF0 | hs >> 8);\r
6286 pb->buf[i++] = (BYTE)(0x80 | (hs >> 2 & 0x3F));\r
6287 pb->buf[i++] = (BYTE)(0x80 | (hs & 3) << 4 | (wc >> 6 & 0x0F));\r
6288 pb->buf[i++] = (BYTE)(0x80 | (wc & 0x3F));\r
6289 } else {\r
6290 if (wc < 0x80) { /* 1-byte */\r
6291 pb->buf[i++] = (BYTE)wc;\r
6292 } else {\r
6293 if (wc < 0x800) { /* 2-byte */\r
6294 nc += 1;\r
6295 pb->buf[i++] = (BYTE)(0xC0 | wc >> 6);\r
6296 } else { /* 3-byte */\r
6297 nc += 2;\r
6298 pb->buf[i++] = (BYTE)(0xE0 | wc >> 12);\r
6299 pb->buf[i++] = (BYTE)(0x80 | (wc >> 6 & 0x3F));\r
6300 }\r
6301 pb->buf[i++] = (BYTE)(0x80 | (wc & 0x3F));\r
6302 }\r
6303 }\r
6304#else /* Write it in ANSI/OEM */\r
6305 if (hs != 0) return;\r
6306 wc = ff_uni2oem(wc, CODEPAGE); /* UTF-16 ==> ANSI/OEM */\r
6307 if (wc == 0) return;;\r
6308 if (wc >= 0x100) {\r
6309 pb->buf[i++] = (BYTE)(wc >> 8); nc++;\r
6310 }\r
6311 pb->buf[i++] = (BYTE)wc;\r
53668523 6312#endif\r
289f6a14
L
6313\r
6314#else /* ANSI/OEM input (without re-encode) */\r
53668523
L
6315 pb->buf[i++] = (BYTE)c;\r
6316#endif\r
6317\r
289f6a14
L
6318 if (i >= (int)(sizeof pb->buf) - 4) { /* Write buffered characters to the file */\r
6319 f_write(pb->fp, pb->buf, (UINT)i, &n);\r
6320 i = (n == (UINT)i) ? 0 : -1;\r
53668523
L
6321 }\r
6322 pb->idx = i;\r
289f6a14 6323 pb->nchr = nc + 1;\r
53668523
L
6324}\r
6325\r
6326\r
70702af1
L
6327static\r
6328int putc_flush ( /* Flush left characters in the buffer */\r
6329 putbuff* pb\r
6330)\r
6331{\r
6332 UINT nw;\r
6333\r
6334 if ( pb->idx >= 0 /* Flush buffered characters to the file */\r
6335 && f_write(pb->fp, pb->buf, (UINT)pb->idx, &nw) == FR_OK\r
6336 && (UINT)pb->idx == nw) return pb->nchr;\r
6337 return EOF;\r
6338}\r
6339\r
6340\r
6341static\r
6342void putc_init ( /* Initialize write buffer */\r
6343 putbuff* pb,\r
6344 FIL* fp\r
6345)\r
6346{\r
289f6a14 6347 mem_set(pb, 0, sizeof (putbuff));\r
70702af1 6348 pb->fp = fp;\r
70702af1
L
6349}\r
6350\r
6351\r
53668523
L
6352\r
6353int f_putc (\r
6354 TCHAR c, /* A character to be output */\r
6355 FIL* fp /* Pointer to the file object */\r
6356)\r
6357{\r
6358 putbuff pb;\r
53668523 6359\r
53668523 6360\r
70702af1
L
6361 putc_init(&pb, fp);\r
6362 putc_bfd(&pb, c); /* Put the character */\r
6363 return putc_flush(&pb);\r
53668523
L
6364}\r
6365\r
6366\r
6367\r
6368\r
6369/*-----------------------------------------------------------------------*/\r
289f6a14 6370/* Put a String to the File */\r
53668523
L
6371/*-----------------------------------------------------------------------*/\r
6372\r
6373int f_puts (\r
6374 const TCHAR* str, /* Pointer to the string to be output */\r
6375 FIL* fp /* Pointer to the file object */\r
6376)\r
6377{\r
6378 putbuff pb;\r
53668523 6379\r
53668523 6380\r
70702af1
L
6381 putc_init(&pb, fp);\r
6382 while (*str) putc_bfd(&pb, *str++); /* Put the string */\r
6383 return putc_flush(&pb);\r
53668523
L
6384}\r
6385\r
6386\r
6387\r
6388\r
6389/*-----------------------------------------------------------------------*/\r
289f6a14 6390/* Put a Formatted String to the File */\r
53668523
L
6391/*-----------------------------------------------------------------------*/\r
6392\r
6393int f_printf (\r
6394 FIL* fp, /* Pointer to the file object */\r
6395 const TCHAR* fmt, /* Pointer to the format string */\r
6396 ... /* Optional arguments... */\r
6397)\r
6398{\r
6399 va_list arp;\r
70702af1 6400 putbuff pb;\r
53668523 6401 BYTE f, r;\r
70702af1 6402 UINT i, j, w;\r
53668523 6403 DWORD v;\r
70702af1 6404 TCHAR c, d, str[32], *p;\r
53668523
L
6405\r
6406\r
70702af1 6407 putc_init(&pb, fp);\r
53668523
L
6408\r
6409 va_start(arp, fmt);\r
6410\r
6411 for (;;) {\r
6412 c = *fmt++;\r
6413 if (c == 0) break; /* End of string */\r
6414 if (c != '%') { /* Non escape character */\r
6415 putc_bfd(&pb, c);\r
6416 continue;\r
6417 }\r
6418 w = f = 0;\r
6419 c = *fmt++;\r
6420 if (c == '0') { /* Flag: '0' padding */\r
6421 f = 1; c = *fmt++;\r
6422 } else {\r
6423 if (c == '-') { /* Flag: left justified */\r
6424 f = 2; c = *fmt++;\r
6425 }\r
6426 }\r
289f6a14
L
6427 if (c == '*') { /* Minimum width by argument */\r
6428 w = va_arg(arp, int);\r
53668523 6429 c = *fmt++;\r
289f6a14
L
6430 } else {\r
6431 while (IsDigit(c)) { /* Minimum width */\r
6432 w = w * 10 + c - '0';\r
6433 c = *fmt++;\r
6434 }\r
53668523 6435 }\r
289f6a14 6436 if (c == 'l' || c == 'L') { /* Type prefix: Size is long int */\r
53668523
L
6437 f |= 4; c = *fmt++;\r
6438 }\r
289f6a14 6439 if (c == 0) break;\r
53668523
L
6440 d = c;\r
6441 if (IsLower(d)) d -= 0x20;\r
289f6a14 6442 switch (d) { /* Atgument type is... */\r
53668523
L
6443 case 'S' : /* String */\r
6444 p = va_arg(arp, TCHAR*);\r
6445 for (j = 0; p[j]; j++) ;\r
289f6a14
L
6446 if (!(f & 2)) { /* Right padded */\r
6447 while (j++ < w) putc_bfd(&pb, ' ') ;\r
53668523 6448 }\r
289f6a14
L
6449 while (*p) putc_bfd(&pb, *p++) ; /* String body */\r
6450 while (j++ < w) putc_bfd(&pb, ' ') ; /* Left padded */\r
53668523 6451 continue;\r
289f6a14 6452\r
53668523
L
6453 case 'C' : /* Character */\r
6454 putc_bfd(&pb, (TCHAR)va_arg(arp, int)); continue;\r
289f6a14
L
6455\r
6456 case 'B' : /* Unsigned binary */\r
53668523 6457 r = 2; break;\r
289f6a14
L
6458\r
6459 case 'O' : /* Unsigned octal */\r
53668523 6460 r = 8; break;\r
289f6a14 6461\r
53668523
L
6462 case 'D' : /* Signed decimal */\r
6463 case 'U' : /* Unsigned decimal */\r
6464 r = 10; break;\r
289f6a14
L
6465\r
6466 case 'X' : /* Unsigned hexdecimal */\r
53668523 6467 r = 16; break;\r
289f6a14 6468\r
53668523
L
6469 default: /* Unknown type (pass-through) */\r
6470 putc_bfd(&pb, c); continue;\r
6471 }\r
6472\r
6473 /* Get an argument and put it in numeral */\r
6474 v = (f & 4) ? (DWORD)va_arg(arp, long) : ((d == 'D') ? (DWORD)(long)va_arg(arp, int) : (DWORD)va_arg(arp, unsigned int));\r
6475 if (d == 'D' && (v & 0x80000000)) {\r
6476 v = 0 - v;\r
6477 f |= 8;\r
6478 }\r
6479 i = 0;\r
6480 do {\r
6481 d = (TCHAR)(v % r); v /= r;\r
6482 if (d > 9) d += (c == 'x') ? 0x27 : 0x07;\r
70702af1 6483 str[i++] = d + '0';\r
289f6a14 6484 } while (v && i < sizeof str / sizeof *str);\r
70702af1 6485 if (f & 8) str[i++] = '-';\r
53668523 6486 j = i; d = (f & 1) ? '0' : ' ';\r
289f6a14
L
6487 if (!(f & 2)) {\r
6488 while (j++ < w) putc_bfd(&pb, d); /* Right pad */\r
6489 }\r
6490 do {\r
6491 putc_bfd(&pb, str[--i]); /* Number body */\r
6492 } while (i);\r
6493 while (j++ < w) putc_bfd(&pb, d); /* Left pad */\r
53668523
L
6494 }\r
6495\r
6496 va_end(arp);\r
6497\r
70702af1 6498 return putc_flush(&pb);\r
53668523
L
6499}\r
6500\r
289f6a14
L
6501#endif /* !FF_FS_READONLY */\r
6502#endif /* FF_USE_STRFUNC */\r
6503\r
6504\r
6505\r
6506#if FF_CODE_PAGE == 0\r
6507/*-----------------------------------------------------------------------*/\r
6508/* Set Active Codepage for the Path Name */\r
6509/*-----------------------------------------------------------------------*/\r
6510\r
6511FRESULT f_setcp (\r
6512 WORD cp /* Value to be set as active code page */\r
6513)\r
6514{\r
6515 static const WORD validcp[] = { 437, 720, 737, 771, 775, 850, 852, 857, 860, 861, 862, 863, 864, 865, 866, 869, 932, 936, 949, 950, 0};\r
6516 static const BYTE* const tables[] = {Ct437, Ct720, Ct737, Ct771, Ct775, Ct850, Ct852, Ct857, Ct860, Ct861, Ct862, Ct863, Ct864, Ct865, Ct866, Ct869, Dc932, Dc936, Dc949, Dc950, 0};\r
6517 UINT i;\r
6518\r
6519\r
6520 for (i = 0; validcp[i] != 0 && validcp[i] != cp; i++) ; /* Find the code page */\r
6521 if (validcp[i] != cp) return FR_INVALID_PARAMETER; /* Not found? */\r
6522\r
6523 CodePage = cp;\r
6524 if (cp >= 900) { /* DBCS */\r
6525 ExCvt = 0;\r
6526 DbcTbl = tables[i];\r
6527 } else { /* SBCS */\r
6528 ExCvt = tables[i];\r
6529 DbcTbl = 0;\r
6530 }\r
6531 return FR_OK;\r
6532}\r
6533#endif /* FF_CODE_PAGE == 0 */\r
6534\r