26#include "module_debug.h"
34#if FF_DEFINED != 80386
35#error Wrong include file (ff.h).
40#define MAX_DIR 0x200000
41#define MAX_DIR_EX 0x10000000
42#define MAX_FAT12 0xFF5
43#define MAX_FAT16 0xFFF5
44#define MAX_FAT32 0x0FFFFFF5
45#define MAX_EXFAT 0x7FFFFFFD
49#define IsUpper(c) ((c) >= 'A' && (c) <= 'Z')
50#define IsLower(c) ((c) >= 'a' && (c) <= 'z')
51#define IsDigit(c) ((c) >= '0' && (c) <= '9')
52#define IsSeparator(c) ((c) == '/' || (c) == '\\')
53#define IsTerminator(c) ((UINT)(c) < (FF_USE_LFN ? ' ' : '!'))
54#define IsSurrogate(c) ((c) >= 0xD800 && (c) <= 0xDFFF)
55#define IsSurrogateH(c) ((c) >= 0xD800 && (c) <= 0xDBFF)
56#define IsSurrogateL(c) ((c) >= 0xDC00 && (c) <= 0xDFFF)
60#define FA_SEEKEND 0x20
61#define FA_MODIFIED 0x40
88#define ET_FILEDIR 0x85
90#define ET_FILENAME 0xC1
98#define BPB_BytsPerSec 11
99#define BPB_SecPerClus 13
100#define BPB_RsvdSecCnt 14
101#define BPB_NumFATs 16
102#define BPB_RootEntCnt 17
103#define BPB_TotSec16 19
105#define BPB_FATSz16 22
106#define BPB_SecPerTrk 24
107#define BPB_NumHeads 26
108#define BPB_HiddSec 28
109#define BPB_TotSec32 32
115#define BS_FilSysType 54
116#define BS_BootCode 62
119#define BPB_FATSz32 36
120#define BPB_ExtFlags32 40
121#define BPB_FSVer32 42
122#define BPB_RootClus32 44
123#define BPB_FSInfo32 48
124#define BPB_BkBootSec32 50
125#define BS_DrvNum32 64
127#define BS_BootSig32 66
129#define BS_VolLab32 71
130#define BS_FilSysType32 82
131#define BS_BootCode32 90
133#define BPB_ZeroedEx 11
134#define BPB_VolOfsEx 64
135#define BPB_TotSecEx 72
136#define BPB_FatOfsEx 80
137#define BPB_FatSzEx 84
138#define BPB_DataOfsEx 88
139#define BPB_NumClusEx 92
140#define BPB_RootClusEx 96
141#define BPB_VolIDEx 100
142#define BPB_FSVerEx 104
143#define BPB_VolFlagEx 106
144#define BPB_BytsPerSecEx 108
145#define BPB_SecPerClusEx 109
146#define BPB_NumFATsEx 110
147#define BPB_DrvNumEx 111
148#define BPB_PercInUseEx 112
149#define BPB_RsvdEx 113
150#define BS_BootCodeEx 120
155#define DIR_CrtTime10 13
156#define DIR_CrtTime 14
157#define DIR_LstAccDate 18
158#define DIR_FstClusHI 20
159#define DIR_ModTime 22
160#define DIR_FstClusLO 26
161#define DIR_FileSize 28
165#define LDIR_Chksum 13
166#define LDIR_FstClusLO 26
168#define XDIR_NumLabel 1
170#define XDIR_CaseSum 4
174#define XDIR_CrtTime 8
175#define XDIR_ModTime 12
176#define XDIR_AccTime 16
177#define XDIR_CrtTime10 20
178#define XDIR_ModTime10 21
182#define XDIR_GenFlags 33
183#define XDIR_NumName 35
184#define XDIR_NameHash 36
185#define XDIR_ValidFileSize 40
186#define XDIR_FstClus 52
187#define XDIR_FileSize 56
195#define FSI_StrucSig 484
196#define FSI_Free_Count 488
197#define FSI_Nxt_Free 492
198#define FSI_TrailSig 508
217#define GPTH_CurLba 24
218#define GPTH_BakLba 32
219#define GPTH_FstLba 40
220#define GPTH_LstLba 48
221#define GPTH_DskGuid 56
224#define GPTH_PteSize 84
228#define GPTE_UpGuid 16
229#define GPTE_FstLba 32
230#define GPTE_LstLba 40
236#define ABORT(fs, res) { fp->err = (BYTE)(res); LEAVE_FF(fs, res); }
242#error Static LFN work area cannot be used in thread-safe configuration
244#define LEAVE_FF(fs, res) { unlock_volume(fs, res); return res; }
246#define LEAVE_FF(fs, res) return res
251#if FF_MULTI_PARTITION
252#define LD2PD(vol) VolToPart[vol].pd
253#define LD2PT(vol) VolToPart[vol].pt
255#define LD2PD(vol) (BYTE)(vol)
261#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)
262#error Wrong sector size configuration
264#if FF_MAX_SS == FF_MIN_SS
265#define SS(fs) ((UINT)FF_MAX_SS)
267#define SS(fs) ((fs)->ssize)
273#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
274#error Invalid FF_FS_NORTC settings
276#define GET_FATTIME() ((DWORD)(FF_NORTC_YEAR - 1980) << 25 | (DWORD)FF_NORTC_MON << 21 | (DWORD)FF_NORTC_MDAY << 16)
278#define GET_FATTIME() get_fattime()
285#error FF_FS_LOCK must be 0 at read-only configuration
297#define TBL_CT437 {0x80,0x9A,0x45,0x41,0x8E,0x41,0x8F,0x80,0x45,0x45,0x45,0x49,0x49,0x49,0x8E,0x8F, \
298 0x90,0x92,0x92,0x4F,0x99,0x4F,0x55,0x55,0x59,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
299 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
300 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
301 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
302 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
303 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \
304 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
305#define TBL_CT720 {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \
306 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
307 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
308 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
309 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
310 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
311 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \
312 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
313#define TBL_CT737 {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \
314 0x90,0x92,0x92,0x93,0x94,0x95,0x96,0x97,0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87, \
315 0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0xAA,0x92,0x93,0x94,0x95,0x96, \
316 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
317 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
318 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
319 0x97,0xEA,0xEB,0xEC,0xE4,0xED,0xEE,0xEF,0xF5,0xF0,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \
320 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
321#define TBL_CT771 {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \
322 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
323 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \
324 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
325 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
326 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDC,0xDE,0xDE, \
327 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
328 0xF0,0xF0,0xF2,0xF2,0xF4,0xF4,0xF6,0xF6,0xF8,0xF8,0xFA,0xFA,0xFC,0xFC,0xFE,0xFF}
329#define TBL_CT775 {0x80,0x9A,0x91,0xA0,0x8E,0x95,0x8F,0x80,0xAD,0xED,0x8A,0x8A,0xA1,0x8D,0x8E,0x8F, \
330 0x90,0x92,0x92,0xE2,0x99,0x95,0x96,0x97,0x97,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9F, \
331 0xA0,0xA1,0xE0,0xA3,0xA3,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
332 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
333 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
334 0xB5,0xB6,0xB7,0xB8,0xBD,0xBE,0xC6,0xC7,0xA5,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
335 0xE0,0xE1,0xE2,0xE3,0xE5,0xE5,0xE6,0xE3,0xE8,0xE8,0xEA,0xEA,0xEE,0xED,0xEE,0xEF, \
336 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
337#define TBL_CT850 {0x43,0x55,0x45,0x41,0x41,0x41,0x41,0x43,0x45,0x45,0x45,0x49,0x49,0x49,0x41,0x41, \
338 0x45,0x92,0x92,0x4F,0x4F,0x4F,0x55,0x55,0x59,0x4F,0x55,0x4F,0x9C,0x4F,0x9E,0x9F, \
339 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
340 0xB0,0xB1,0xB2,0xB3,0xB4,0x41,0x41,0x41,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
341 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0x41,0x41,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
342 0xD1,0xD1,0x45,0x45,0x45,0x49,0x49,0x49,0x49,0xD9,0xDA,0xDB,0xDC,0xDD,0x49,0xDF, \
343 0x4F,0xE1,0x4F,0x4F,0x4F,0x4F,0xE6,0xE8,0xE8,0x55,0x55,0x55,0x59,0x59,0xEE,0xEF, \
344 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
345#define TBL_CT852 {0x80,0x9A,0x90,0xB6,0x8E,0xDE,0x8F,0x80,0x9D,0xD3,0x8A,0x8A,0xD7,0x8D,0x8E,0x8F, \
346 0x90,0x91,0x91,0xE2,0x99,0x95,0x95,0x97,0x97,0x99,0x9A,0x9B,0x9B,0x9D,0x9E,0xAC, \
347 0xB5,0xD6,0xE0,0xE9,0xA4,0xA4,0xA6,0xA6,0xA8,0xA8,0xAA,0x8D,0xAC,0xB8,0xAE,0xAF, \
348 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBD,0xBF, \
349 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC6,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
350 0xD1,0xD1,0xD2,0xD3,0xD2,0xD5,0xD6,0xD7,0xB7,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
351 0xE0,0xE1,0xE2,0xE3,0xE3,0xD5,0xE6,0xE6,0xE8,0xE9,0xE8,0xEB,0xED,0xED,0xDD,0xEF, \
352 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xEB,0xFC,0xFC,0xFE,0xFF}
353#define TBL_CT855 {0x81,0x81,0x83,0x83,0x85,0x85,0x87,0x87,0x89,0x89,0x8B,0x8B,0x8D,0x8D,0x8F,0x8F, \
354 0x91,0x91,0x93,0x93,0x95,0x95,0x97,0x97,0x99,0x99,0x9B,0x9B,0x9D,0x9D,0x9F,0x9F, \
355 0xA1,0xA1,0xA3,0xA3,0xA5,0xA5,0xA7,0xA7,0xA9,0xA9,0xAB,0xAB,0xAD,0xAD,0xAE,0xAF, \
356 0xB0,0xB1,0xB2,0xB3,0xB4,0xB6,0xB6,0xB8,0xB8,0xB9,0xBA,0xBB,0xBC,0xBE,0xBE,0xBF, \
357 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC7,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
358 0xD1,0xD1,0xD3,0xD3,0xD5,0xD5,0xD7,0xD7,0xDD,0xD9,0xDA,0xDB,0xDC,0xDD,0xE0,0xDF, \
359 0xE0,0xE2,0xE2,0xE4,0xE4,0xE6,0xE6,0xE8,0xE8,0xEA,0xEA,0xEC,0xEC,0xEE,0xEE,0xEF, \
360 0xF0,0xF2,0xF2,0xF4,0xF4,0xF6,0xF6,0xF8,0xF8,0xFA,0xFA,0xFC,0xFC,0xFD,0xFE,0xFF}
361#define TBL_CT857 {0x80,0x9A,0x90,0xB6,0x8E,0xB7,0x8F,0x80,0xD2,0xD3,0xD4,0xD8,0xD7,0x49,0x8E,0x8F, \
362 0x90,0x92,0x92,0xE2,0x99,0xE3,0xEA,0xEB,0x98,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9E, \
363 0xB5,0xD6,0xE0,0xE9,0xA5,0xA5,0xA6,0xA6,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
364 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
365 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC7,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
366 0xD0,0xD1,0xD2,0xD3,0xD4,0x49,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
367 0xE0,0xE1,0xE2,0xE3,0xE5,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xDE,0xED,0xEE,0xEF, \
368 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
369#define TBL_CT860 {0x80,0x9A,0x90,0x8F,0x8E,0x91,0x86,0x80,0x89,0x89,0x92,0x8B,0x8C,0x98,0x8E,0x8F, \
370 0x90,0x91,0x92,0x8C,0x99,0xA9,0x96,0x9D,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
371 0x86,0x8B,0x9F,0x96,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
372 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
373 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
374 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
375 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \
376 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
377#define TBL_CT861 {0x80,0x9A,0x90,0x41,0x8E,0x41,0x8F,0x80,0x45,0x45,0x45,0x8B,0x8B,0x8D,0x8E,0x8F, \
378 0x90,0x92,0x92,0x4F,0x99,0x8D,0x55,0x97,0x97,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9F, \
379 0xA4,0xA5,0xA6,0xA7,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
380 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
381 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
382 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
383 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \
384 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
385#define TBL_CT862 {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \
386 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
387 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
388 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
389 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
390 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
391 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \
392 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
393#define TBL_CT863 {0x43,0x55,0x45,0x41,0x41,0x41,0x86,0x43,0x45,0x45,0x45,0x49,0x49,0x8D,0x41,0x8F, \
394 0x45,0x45,0x45,0x4F,0x45,0x49,0x55,0x55,0x98,0x4F,0x55,0x9B,0x9C,0x55,0x55,0x9F, \
395 0xA0,0xA1,0x4F,0x55,0xA4,0xA5,0xA6,0xA7,0x49,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
396 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
397 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
398 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
399 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \
400 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
401#define TBL_CT864 {0x80,0x9A,0x45,0x41,0x8E,0x41,0x8F,0x80,0x45,0x45,0x45,0x49,0x49,0x49,0x8E,0x8F, \
402 0x90,0x92,0x92,0x4F,0x99,0x4F,0x55,0x55,0x59,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
403 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
404 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
405 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
406 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
407 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \
408 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
409#define TBL_CT865 {0x80,0x9A,0x90,0x41,0x8E,0x41,0x8F,0x80,0x45,0x45,0x45,0x49,0x49,0x49,0x8E,0x8F, \
410 0x90,0x92,0x92,0x4F,0x99,0x4F,0x55,0x55,0x59,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
411 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
412 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
413 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
414 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
415 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \
416 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
417#define TBL_CT866 {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \
418 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
419 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \
420 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
421 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
422 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
423 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
424 0xF0,0xF0,0xF2,0xF2,0xF4,0xF4,0xF6,0xF6,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
425#define TBL_CT869 {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \
426 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x86,0x9C,0x8D,0x8F,0x90, \
427 0x91,0x90,0x92,0x95,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
428 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
429 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
430 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xA4,0xA5,0xA6,0xD9,0xDA,0xDB,0xDC,0xA7,0xA8,0xDF, \
431 0xA9,0xAA,0xAC,0xAD,0xB5,0xB6,0xB7,0xB8,0xBD,0xBE,0xC6,0xC7,0xCF,0xCF,0xD0,0xEF, \
432 0xF0,0xF1,0xD1,0xD2,0xD3,0xF5,0xD4,0xF7,0xF8,0xF9,0xD5,0x96,0x95,0x98,0xFE,0xFF}
437#define TBL_DC932 {0x81, 0x9F, 0xE0, 0xFC, 0x40, 0x7E, 0x80, 0xFC, 0x00, 0x00}
438#define TBL_DC936 {0x81, 0xFE, 0x00, 0x00, 0x40, 0x7E, 0x80, 0xFE, 0x00, 0x00}
439#define TBL_DC949 {0x81, 0xFE, 0x00, 0x00, 0x41, 0x5A, 0x61, 0x7A, 0x81, 0xFE}
440#define TBL_DC950 {0x81, 0xFE, 0x00, 0x00, 0x40, 0x7E, 0xA1, 0xFE, 0x00, 0x00}
444#define MERGE_2STR(a, b) a ## b
445#define MKCVTBL(hd, cp) MERGE_2STR(hd, cp)
463#if FF_VOLUMES < 1 || FF_VOLUMES > 10
464#error Wrong FF_VOLUMES setting
466static FATFS *FatFs[FF_VOLUMES];
474static FILESEM Files[FF_FS_LOCK];
476static volatile BYTE SysLock;
477static volatile BYTE SysLockVolume;
483static const char *
const VolumeStr[FF_VOLUMES] = {FF_VOLUME_STRS};
488#if FF_MIN_GPT > 0x100000000
489#error Wrong FF_MIN_GPT setting
491static const BYTE GUID_MS_Basic[16] = {0xA2,0xA0,0xD0,0xEB,0xE5,0xB9,0x33,0x44,0x87,0xC0,0x68,0xB6,0xB7,0x26,0x99,0xC7};
502#error LFN must be enabled when enable exFAT
505#define INIT_NAMEBUFF(fs)
506#define FREE_NAMEBUFF()
507#define LEAVE_MKFS(res) return res
510#if FF_MAX_LFN < 12 || FF_MAX_LFN > 255
511#error Wrong setting of FF_MAX_LFN
513#if FF_LFN_BUF < FF_SFN_BUF || FF_SFN_BUF < 12
514#error Wrong setting of FF_LFN_BUF or FF_SFN_BUF
516#if FF_LFN_UNICODE < 0 || FF_LFN_UNICODE > 3
517#error Wrong setting of FF_LFN_UNICODE
519static const BYTE LfnOfs[] = {1,3,5,7,9,14,16,18,20,22,24,28,30};
520#define MAXDIRB(nc) ((nc + 44U) / 15 * SZDIRE)
524static BYTE DirBuf[MAXDIRB(FF_MAX_LFN)];
526static WCHAR LfnBuf[FF_MAX_LFN + 1];
528#define INIT_NAMEBUFF(fs)
529#define FREE_NAMEBUFF()
530#define LEAVE_MKFS(res) return res
534#define DEF_NAMEBUFF WCHAR lbuf[FF_MAX_LFN+1]; BYTE dbuf[MAXDIRB(FF_MAX_LFN)];
535#define INIT_NAMEBUFF(fs) { (fs)->lfnbuf = lbuf; (fs)->dirbuf = dbuf; }
536#define FREE_NAMEBUFF()
538#define DEF_NAMEBUFF WCHAR lbuf[FF_MAX_LFN+1];
539#define INIT_NAMEBUFF(fs) { (fs)->lfnbuf = lbuf; }
540#define FREE_NAMEBUFF()
542#define LEAVE_MKFS(res) return res
546#define DEF_NAMEBUFF WCHAR *lfn;
547#define INIT_NAMEBUFF(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); }
548#define FREE_NAMEBUFF() ff_memfree(lfn)
550#define DEF_NAMEBUFF WCHAR *lfn;
551#define INIT_NAMEBUFF(fs) { lfn = ff_memalloc((FF_MAX_LFN+1)*2); if (!lfn) LEAVE_FF(fs, FR_NOT_ENOUGH_CORE); (fs)->lfnbuf = lfn; }
552#define FREE_NAMEBUFF() ff_memfree(lfn)
554#define LEAVE_MKFS(res) { if (!work) ff_memfree(buf); return res; }
555#define MAX_MALLOC 0x8000
558#error Wrong setting of FF_USE_LFN
570#define CODEPAGE CodePage
572static const BYTE* ExCvt;
573static const BYTE* DbcTbl;
575static const BYTE Ct437[] = TBL_CT437;
576static const BYTE Ct720[] = TBL_CT720;
577static const BYTE Ct737[] = TBL_CT737;
578static const BYTE Ct771[] = TBL_CT771;
579static const BYTE Ct775[] = TBL_CT775;
580static const BYTE Ct850[] = TBL_CT850;
581static const BYTE Ct852[] = TBL_CT852;
582static const BYTE Ct855[] = TBL_CT855;
583static const BYTE Ct857[] = TBL_CT857;
584static const BYTE Ct860[] = TBL_CT860;
585static const BYTE Ct861[] = TBL_CT861;
586static const BYTE Ct862[] = TBL_CT862;
587static const BYTE Ct863[] = TBL_CT863;
588static const BYTE Ct864[] = TBL_CT864;
589static const BYTE Ct865[] = TBL_CT865;
590static const BYTE Ct866[] = TBL_CT866;
591static const BYTE Ct869[] = TBL_CT869;
592static const BYTE Dc932[] = TBL_DC932;
593static const BYTE Dc936[] = TBL_DC936;
594static const BYTE Dc949[] = TBL_DC949;
595static const BYTE Dc950[] = TBL_DC950;
597#elif FF_CODE_PAGE < 900
598#define CODEPAGE FF_CODE_PAGE
599static const BYTE ExCvt[] = MKCVTBL(TBL_CT, FF_CODE_PAGE);
602#define CODEPAGE FF_CODE_PAGE
603static const BYTE DbcTbl[] = MKCVTBL(TBL_DC, FF_CODE_PAGE);
621static WORD ld_16 (
const BYTE* ptr)
626 rv = rv << 8 | ptr[0];
630static DWORD ld_32 (
const BYTE* ptr)
635 rv = rv << 8 | ptr[2];
636 rv = rv << 8 | ptr[1];
637 rv = rv << 8 | ptr[0];
642static QWORD ld_64 (
const BYTE* ptr)
647 rv = rv << 8 | ptr[6];
648 rv = rv << 8 | ptr[5];
649 rv = rv << 8 | ptr[4];
650 rv = rv << 8 | ptr[3];
651 rv = rv << 8 | ptr[2];
652 rv = rv << 8 | ptr[1];
653 rv = rv << 8 | ptr[0];
659static void st_16 (BYTE* ptr, WORD val)
661 *ptr++ = (BYTE)val; val >>= 8;
665static void st_32 (BYTE* ptr, DWORD val)
667 *ptr++ = (BYTE)val; val >>= 8;
668 *ptr++ = (BYTE)val; val >>= 8;
669 *ptr++ = (BYTE)val; val >>= 8;
674static void st_64 (BYTE* ptr,
QWORD val)
676 *ptr++ = (BYTE)val; val >>= 8;
677 *ptr++ = (BYTE)val; val >>= 8;
678 *ptr++ = (BYTE)val; val >>= 8;
679 *ptr++ = (BYTE)val; val >>= 8;
680 *ptr++ = (BYTE)val; val >>= 8;
681 *ptr++ = (BYTE)val; val >>= 8;
682 *ptr++ = (BYTE)val; val >>= 8;
695static int dbc_1st (BYTE c)
698 if (DbcTbl && c >= DbcTbl[0]) {
699 if (c <= DbcTbl[1])
return 1;
700 if (c >= DbcTbl[2] && c <= DbcTbl[3])
return 1;
702#elif FF_CODE_PAGE >= 900
703 if (c >= DbcTbl[0]) {
704 if (c <= DbcTbl[1])
return 1;
705 if (c >= DbcTbl[2] && c <= DbcTbl[3])
return 1;
708 if (c != 0)
return 0;
715static int dbc_2nd (BYTE c)
718 if (DbcTbl && c >= DbcTbl[4]) {
719 if (c <= DbcTbl[5])
return 1;
720 if (c >= DbcTbl[6] && c <= DbcTbl[7])
return 1;
721 if (c >= DbcTbl[8] && c <= DbcTbl[9])
return 1;
723#elif FF_CODE_PAGE >= 900
724 if (c >= DbcTbl[4]) {
725 if (c <= DbcTbl[5])
return 1;
726 if (c >= DbcTbl[6] && c <= DbcTbl[7])
return 1;
727 if (c >= DbcTbl[8] && c <= DbcTbl[9])
return 1;
730 if (c != 0)
return 0;
739static DWORD tchar2uni (
744 const TCHAR *p = *str;
746#if FF_LFN_UNICODE == 1
750 if (IsSurrogate(uc)) {
752 if (!IsSurrogateH(uc) || !IsSurrogateL(wc))
return 0xFFFFFFFF;
756#elif FF_LFN_UNICODE == 2
762 if ((uc & 0xE0) == 0xC0) {
764 }
else if ((uc & 0xF0) == 0xE0) {
766 }
else if ((uc & 0xF8) == 0xF0) {
773 if ((tb & 0xC0) != 0x80)
return 0xFFFFFFFF;
774 uc = uc << 6 | (tb & 0x3F);
776 if (uc < 0x80 || IsSurrogate(uc) || uc >= 0x110000)
return 0xFFFFFFFF;
777 if (uc >= 0x010000) uc = 0xD800DC00 | ((uc - 0x10000) << 6 & 0x3FF0000) | (uc & 0x3FF);
780#elif FF_LFN_UNICODE == 3
782 if (uc >= 0x110000 || IsSurrogate(uc))
return 0xFFFFFFFF;
783 if (uc >= 0x010000) uc = 0xD800DC00 | ((uc - 0x10000) << 6 & 0x3FF0000) | (uc & 0x3FF);
790 if (dbc_1st((BYTE)wc)) {
792 if (!dbc_2nd(sb))
return 0xFFFFFFFF;
796 wc = ff_oem2uni(wc, CODEPAGE);
797 if (wc == 0)
return 0xFFFFFFFF;
814#if FF_LFN_UNICODE == 1
817 hs = (WCHAR)(chr >> 16);
820 if (szb < 1 || IsSurrogate(wc))
return 0;
824 if (szb < 2 || !IsSurrogateH(hs) || !IsSurrogateL(wc))
return 0;
829#elif FF_LFN_UNICODE == 2
833 if (szb < 1)
return 0;
838 if (szb < 2)
return 0;
839 *buf++ = (TCHAR)(0xC0 | (chr >> 6 & 0x1F));
840 *buf++ = (TCHAR)(0x80 | (chr >> 0 & 0x3F));
844 if (szb < 3 || IsSurrogate(chr))
return 0;
845 *buf++ = (TCHAR)(0xE0 | (chr >> 12 & 0x0F));
846 *buf++ = (TCHAR)(0x80 | (chr >> 6 & 0x3F));
847 *buf++ = (TCHAR)(0x80 | (chr >> 0 & 0x3F));
851 if (szb < 4)
return 0;
852 hc = ((chr & 0xFFFF0000) - 0xD8000000) >> 6;
853 chr = (chr & 0xFFFF) - 0xDC00;
854 if (hc >= 0x100000 || chr >= 0x400)
return 0;
855 chr = (hc | chr) + 0x10000;
856 *buf++ = (TCHAR)(0xF0 | (chr >> 18 & 0x07));
857 *buf++ = (TCHAR)(0x80 | (chr >> 12 & 0x3F));
858 *buf++ = (TCHAR)(0x80 | (chr >> 6 & 0x3F));
859 *buf++ = (TCHAR)(0x80 | (chr >> 0 & 0x3F));
862#elif FF_LFN_UNICODE == 3
865 if (szb < 1)
return 0;
866 if (chr >= 0x10000) {
867 hc = ((chr & 0xFFFF0000) - 0xD8000000) >> 6;
868 chr = (chr & 0xFFFF) - 0xDC00;
869 if (hc >= 0x100000 || chr >= 0x400)
return 0;
870 chr = (hc | chr) + 0x10000;
878 wc = ff_uni2oem(chr, CODEPAGE);
880 if (szb < 2)
return 0;
881 *buf++ = (char)(wc >> 8);
885 if (wc == 0 || szb < 1)
return 0;
898static int lock_volume (
907 rv = ff_mutex_take(fs->ldrv);
909 rv = ff_mutex_take(FF_VOLUMES);
911 SysLockVolume = fs->ldrv;
914 ff_mutex_give(fs->ldrv);
918 rv = syslock ? ff_mutex_take(fs->ldrv) : ff_mutex_take(fs->ldrv);
924static void unlock_volume (
929 if (fs && res != FR_NOT_ENABLED && res != FR_INVALID_DRIVE && res != FR_TIMEOUT) {
931 if (SysLock == 2 && SysLockVolume == fs->ldrv) {
933 ff_mutex_give(FF_VOLUMES);
936 ff_mutex_give(fs->ldrv);
949static FRESULT chk_share (
958 for (i = 0; i < FF_FS_LOCK; i++) {
960 if (Files[i].fs == dp->obj.fs &&
961 Files[i].clu == dp->obj.sclust &&
962 Files[i].ofs == dp->dptr)
break;
967 if (i == FF_FS_LOCK) {
968 return (!be && acc != 2) ? FR_TOO_MANY_OPEN_FILES : FR_OK;
972 return (acc != 0 || Files[i].ctr == 0x100) ? FR_LOCKED : FR_OK;
976static int enq_share (
void)
980 for (i = 0; i < FF_FS_LOCK && Files[i].fs; i++) ;
981 return (i == FF_FS_LOCK) ? 0 : 1;
985static UINT inc_share (
993 for (i = 0; i < FF_FS_LOCK; i++) {
994 if (Files[i].fs == dp->obj.fs
995 && Files[i].clu == dp->obj.sclust
996 && Files[i].ofs == dp->dptr)
break;
999 if (i == FF_FS_LOCK) {
1000 for (i = 0; i < FF_FS_LOCK && Files[i].fs; i++) ;
1001 if (i == FF_FS_LOCK)
return 0;
1002 Files[i].fs = dp->obj.fs;
1003 Files[i].clu = dp->obj.sclust;
1004 Files[i].ofs = dp->dptr;
1008 if (acc >= 1 && Files[i].ctr)
return 0;
1010 Files[i].ctr = acc ? 0x100 : Files[i].ctr + 1;
1016static FRESULT dec_share (
1024 if (--i < FF_FS_LOCK) {
1026 if (n == 0x100) n = 0;
1040static void clear_share (
1046 for (i = 0; i < FF_FS_LOCK; i++) {
1047 if (Files[i].fs == fs) Files[i].fs = 0;
1059static FRESULT sync_window (
1063 FRESULT res = FR_OK;
1067 if (disk_write(fs->pdrv, fs->win, fs->winsect, 1) == RES_OK) {
1069 if (fs->winsect - fs->fatbase < fs->fsize) {
1070 if (fs->n_fats == 2) disk_write(fs->pdrv, fs->win, fs->winsect + fs->fsize, 1);
1081static FRESULT move_window (
1086 FRESULT res = FR_OK;
1089 if (sect != fs->winsect) {
1091 res = sync_window(fs);
1094 if (disk_read(fs->pdrv, fs->win, sect, 1) != RES_OK) {
1095 sect = (LBA_t)0 - 1;
1112static FRESULT sync_fs (
1119 res = sync_window(fs);
1121 if (fs->fsi_flag == 1) {
1123 if (fs->fs_type == FS_FAT32) {
1125 memset(fs->win, 0,
sizeof fs->win);
1126 st_32(fs->win + FSI_LeadSig, 0x41615252);
1127 st_32(fs->win + FSI_StrucSig, 0x61417272);
1128 st_32(fs->win + FSI_Free_Count, fs->free_clst);
1129 st_32(fs->win + FSI_Nxt_Free, fs->last_clst);
1130 st_32(fs->win + FSI_TrailSig, 0xAA550000);
1131 disk_write(fs->pdrv, fs->win, fs->winsect = fs->volbase + 1, 1);
1134 else if (fs->fs_type == FS_EXFAT) {
1135 if (disk_read(fs->pdrv, fs->win, fs->winsect = fs->volbase, 1) == RES_OK) {
1136 BYTE perc_inuse = (fs->free_clst <= fs->n_fatent - 2) ? (BYTE)((
QWORD)(fs->n_fatent - 2 - fs->free_clst) * 100 / (fs->n_fatent - 2)) : 0xFF;
1138 if (fs->win[BPB_PercInUseEx] != perc_inuse) {
1139 fs->win[BPB_PercInUseEx] = perc_inuse;
1140 disk_write(fs->pdrv, fs->win, fs->winsect, 1);
1147 if (disk_ioctl(fs->pdrv, CTRL_SYNC, 0) != RES_OK) res = FR_DISK_ERR;
1167 if (clst >= fs->n_fatent - 2)
return 0;
1168 return fs->database + (LBA_t)fs->csize * clst;
1185 FATFS *fs = obj->fs;
1188 if (clst < 2 || clst >= fs->n_fatent) {
1194 switch (fs->fs_type) {
1196 bc = (UINT)clst; bc += bc / 2;
1197 if (move_window(fs, fs->fatbase + (bc / SS(fs))) != FR_OK)
break;
1198 wc = fs->win[bc++ % SS(fs)];
1199 if (move_window(fs, fs->fatbase + (bc / SS(fs))) != FR_OK)
break;
1200 wc |= fs->win[bc % SS(fs)] << 8;
1201 val = (clst & 1) ? (wc >> 4) : (wc & 0xFFF);
1205 if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 2))) != FR_OK)
break;
1206 val = ld_16(fs->win + clst * 2 % SS(fs));
1210 if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 4))) != FR_OK)
break;
1211 val = ld_32(fs->win + clst * 4 % SS(fs)) & 0x0FFFFFFF;
1215 if ((obj->objsize != 0 && obj->sclust != 0) || obj->stat == 0) {
1216 DWORD cofs = clst - obj->sclust;
1217 DWORD clen = (DWORD)((LBA_t)((obj->objsize - 1) / SS(fs)) / fs->csize);
1219 if (obj->stat == 2 && cofs <= clen) {
1220 val = (cofs == clen) ? 0x7FFFFFFF : clst + 1;
1223 if (obj->stat == 3 && cofs < obj->n_cont) {
1227 if (obj->stat != 2) {
1228 if (obj->n_frag != 0) {
1231 if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 4))) != FR_OK)
break;
1232 val = ld_32(fs->win + clst * 4 % SS(fs)) & 0x7FFFFFFF;
1256static FRESULT put_fat (
1264 FRESULT res = FR_INT_ERR;
1267 if (clst >= 2 && clst < fs->n_fatent) {
1268 switch (fs->fs_type) {
1270 bc = (UINT)clst; bc += bc / 2;
1271 res = move_window(fs, fs->fatbase + (bc / SS(fs)));
1272 if (res != FR_OK)
break;
1273 p = fs->win + bc++ % SS(fs);
1274 *p = (clst & 1) ? ((*p & 0x0F) | ((BYTE)val << 4)) : (BYTE)val;
1276 res = move_window(fs, fs->fatbase + (bc / SS(fs)));
1277 if (res != FR_OK)
break;
1278 p = fs->win + bc % SS(fs);
1279 *p = (clst & 1) ? (BYTE)(val >> 4) : ((*p & 0xF0) | ((BYTE)(val >> 8) & 0x0F));
1284 res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 2)));
1285 if (res != FR_OK)
break;
1286 st_16(fs->win + clst * 2 % SS(fs), (WORD)val);
1294 res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 4)));
1295 if (res != FR_OK)
break;
1296 if (!FF_FS_EXFAT || fs->fs_type != FS_EXFAT) {
1297 val = (val & 0x0FFFFFFF) | (ld_32(fs->win + clst * 4 % SS(fs)) & 0xF0000000);
1299 st_32(fs->win + clst * 4 % SS(fs), val);
1312#if FF_FS_EXFAT && !FF_FS_READONLY
1321static DWORD find_bitmap (
1329 DWORD val, scl, ctr;
1333 if (clst >= fs->n_fatent - 2) clst = 0;
1334 scl = val = clst; ctr = 0;
1336 if (move_window(fs, fs->bitbase + val / 8 / SS(fs)) != FR_OK)
return 0xFFFFFFFF;
1337 i = val / 8 % SS(fs); bm = 1 << (val % 8);
1340 bv = fs->win[i] & bm; bm <<= 1;
1341 if (++val >= fs->n_fatent - 2) {
1342 val = 0; bm = 0; i = SS(fs);
1345 if (++ctr == ncl)
return scl + 2;
1349 if (val == clst)
return 0;
1352 }
while (++i < SS(fs));
1361static FRESULT change_bitmap (
1374 sect = fs->bitbase + clst / 8 / SS(fs);
1375 i = clst / 8 % SS(fs);
1376 bm = 1 << (clst % 8);
1378 if (move_window(fs, sect++) != FR_OK)
return FR_DISK_ERR;
1381 if (bv == (
int)((fs->win[i] & bm) != 0))
return FR_INT_ERR;
1384 if (--ncl == 0)
return FR_OK;
1387 }
while (++i < SS(fs));
1397static FRESULT fill_first_frag (
1405 if (obj->stat == 3) {
1406 for (cl = obj->sclust, n = obj->n_cont; n; cl++, n--) {
1407 res = put_fat(obj->fs, cl, cl + 1);
1408 if (res != FR_OK)
return res;
1420static FRESULT fill_last_frag (
1429 while (obj->n_frag > 0) {
1430 res = put_fat(obj->fs, lcl - obj->n_frag + 1, (obj->n_frag > 1) ? lcl - obj->n_frag + 2 : term);
1431 if (res != FR_OK)
return res;
1446static FRESULT remove_chain (
1452 FRESULT res = FR_OK;
1454 FATFS *fs = obj->fs;
1455#if FF_FS_EXFAT || FF_USE_TRIM
1456 DWORD scl = clst, ecl = clst;
1462 if (clst < 2 || clst >= fs->n_fatent)
return FR_INT_ERR;
1465 if (pclst != 0 && (!FF_FS_EXFAT || fs->fs_type != FS_EXFAT || obj->stat != 2)) {
1466 res = put_fat(fs, pclst, 0xFFFFFFFF);
1467 if (res != FR_OK)
return res;
1472 nxt = get_fat(obj, clst);
1473 if (nxt == 0)
break;
1474 if (nxt == 1)
return FR_INT_ERR;
1475 if (nxt == 0xFFFFFFFF)
return FR_DISK_ERR;
1476 if (!FF_FS_EXFAT || fs->fs_type != FS_EXFAT) {
1477 res = put_fat(fs, clst, 0);
1478 if (res != FR_OK)
return res;
1480 if (fs->free_clst < fs->n_fatent - 2) {
1484#if FF_FS_EXFAT || FF_USE_TRIM
1485 if (ecl + 1 == nxt) {
1489 if (fs->fs_type == FS_EXFAT) {
1490 res = change_bitmap(fs, scl, ecl - scl + 1, 0);
1491 if (res != FR_OK)
return res;
1495 rt[0] = clst2sect(fs, scl);
1496 rt[1] = clst2sect(fs, ecl) + fs->csize - 1;
1497 disk_ioctl(fs->pdrv, CTRL_TRIM, rt);
1503 }
while (clst < fs->n_fatent);
1507 if (fs->fs_type == FS_EXFAT) {
1511 if (obj->stat == 0) {
1513 while (clst != pclst) {
1514 nxt = get_fat(obj, clst);
1515 if (nxt < 2)
return FR_INT_ERR;
1516 if (nxt == 0xFFFFFFFF)
return FR_DISK_ERR;
1517 if (nxt != clst + 1)
break;
1520 if (clst == pclst) {
1524 if (obj->stat == 3 && pclst >= obj->sclust && pclst <= obj->sclust + obj->n_cont) {
1541static DWORD create_chain (
1548 FATFS *fs = obj->fs;
1552 scl = fs->last_clst;
1553 if (scl == 0 || scl >= fs->n_fatent) scl = 1;
1556 cs = get_fat(obj, clst);
1557 if (cs < 2)
return 1;
1558 if (cs == 0xFFFFFFFF)
return cs;
1559 if (cs < fs->n_fatent)
return cs;
1562 if (fs->free_clst == 0)
return 0;
1565 if (fs->fs_type == FS_EXFAT) {
1566 ncl = find_bitmap(fs, scl, 1);
1567 if (ncl == 0 || ncl == 0xFFFFFFFF)
return ncl;
1568 res = change_bitmap(fs, ncl, 1, 1);
1569 if (res == FR_INT_ERR)
return 1;
1570 if (res == FR_DISK_ERR)
return 0xFFFFFFFF;
1574 if (obj->stat == 2 && ncl != scl + 1) {
1575 obj->n_cont = scl - obj->sclust;
1579 if (obj->stat != 2) {
1580 if (ncl == clst + 1) {
1581 obj->n_frag = obj->n_frag ? obj->n_frag + 1 : 2;
1583 if (obj->n_frag == 0) obj->n_frag = 1;
1584 res = fill_last_frag(obj, clst, ncl);
1585 if (res == FR_OK) obj->n_frag = 1;
1594 if (ncl >= fs->n_fatent) ncl = 2;
1595 cs = get_fat(obj, ncl);
1596 if (cs == 1 || cs == 0xFFFFFFFF)
return cs;
1599 if (cs >= 2 && cs < fs->n_fatent) scl = cs;
1607 if (ncl >= fs->n_fatent) {
1609 if (ncl > scl)
return 0;
1611 cs = get_fat(obj, ncl);
1613 if (cs == 1 || cs == 0xFFFFFFFF)
return cs;
1614 if (ncl == scl)
return 0;
1617 res = put_fat(fs, ncl, 0xFFFFFFFF);
1618 if (res == FR_OK && clst != 0) {
1619 res = put_fat(fs, clst, ncl);
1624 fs->last_clst = ncl;
1625 if (fs->free_clst > 0 && fs->free_clst <= fs->n_fatent - 2) {
1630 ncl = (res == FR_DISK_ERR) ? 0xFFFFFFFF : 1;
1646static DWORD clmt_clust (
1653 FATFS *fs = fp->obj.fs;
1656 tbl = fp->cltbl + 1;
1657 cl = (DWORD)(ofs / SS(fs) / fs->csize);
1660 if (ncl == 0)
return 0;
1661 if (cl < ncl)
break;
1677static FRESULT dir_clear (
1687 if (sync_window(fs) != FR_OK)
return FR_DISK_ERR;
1688 sect = clst2sect(fs, clst);
1690 memset(fs->win, 0,
sizeof fs->win);
1693 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) ;
1695 memset(ibuf, 0, szb);
1697 for (n = 0; n < fs->csize && disk_write(fs->pdrv, ibuf, sect + n, szb) == RES_OK; n += szb) ;
1702 ibuf = fs->win; szb = 1;
1703 for (n = 0; n < fs->csize && disk_write(fs->pdrv, ibuf, sect + n, szb) == RES_OK; n += szb) ;
1705 return (n == fs->csize) ? FR_OK : FR_DISK_ERR;
1716static FRESULT dir_sdi (
1722 FATFS *fs = dp->obj.fs;
1725 if (ofs >= (DWORD)((FF_FS_EXFAT && fs->fs_type == FS_EXFAT) ? MAX_DIR_EX : MAX_DIR) || ofs % SZDIRE) {
1729 clst = dp->obj.sclust;
1730 if (clst == 0 && fs->fs_type >= FS_FAT32) {
1731 clst = (DWORD)fs->dirbase;
1732 if (FF_FS_EXFAT) dp->obj.stat = 0;
1736 if (ofs / SZDIRE >= fs->n_rootdir)
return FR_INT_ERR;
1737 dp->sect = fs->dirbase;
1740 csz = (DWORD)fs->csize * SS(fs);
1741 while (ofs >= csz) {
1742 clst = get_fat(&dp->obj, clst);
1743 if (clst == 0xFFFFFFFF)
return FR_DISK_ERR;
1744 if (clst < 2 || clst >= fs->n_fatent)
return FR_INT_ERR;
1747 dp->sect = clst2sect(fs, clst);
1750 if (dp->sect == 0)
return FR_INT_ERR;
1751 dp->sect += ofs / SS(fs);
1752 dp->dir = fs->win + (ofs % SS(fs));
1764static FRESULT dir_next (
1770 FATFS *fs = dp->obj.fs;
1773 ofs = dp->dptr + SZDIRE;
1774 if (ofs >= (DWORD)((FF_FS_EXFAT && fs->fs_type == FS_EXFAT) ? MAX_DIR_EX : MAX_DIR)) dp->sect = 0;
1775 if (dp->sect == 0)
return FR_NO_FILE;
1777 if (ofs % SS(fs) == 0) {
1780 if (dp->clust == 0) {
1781 if (ofs / SZDIRE >= fs->n_rootdir) {
1782 dp->sect = 0;
return FR_NO_FILE;
1786 if ((ofs / SS(fs) & (fs->csize - 1)) == 0) {
1787 clst = get_fat(&dp->obj, dp->clust);
1788 if (clst <= 1)
return FR_INT_ERR;
1789 if (clst == 0xFFFFFFFF)
return FR_DISK_ERR;
1790 if (clst >= fs->n_fatent) {
1793 dp->sect = 0;
return FR_NO_FILE;
1795 clst = create_chain(&dp->obj, dp->clust);
1796 if (clst == 0)
return FR_DENIED;
1797 if (clst == 1)
return FR_INT_ERR;
1798 if (clst == 0xFFFFFFFF)
return FR_DISK_ERR;
1799 if (dir_clear(fs, clst) != FR_OK)
return FR_DISK_ERR;
1800 if (FF_FS_EXFAT) dp->obj.stat |= 4;
1802 if (!stretch) dp->sect = 0;
1803 dp->sect = 0;
return FR_NO_FILE;
1807 dp->sect = clst2sect(fs, clst);
1812 dp->dir = fs->win + ofs % SS(fs);
1825static FRESULT dir_alloc (
1832 FATFS *fs = dp->obj.fs;
1835 res = dir_sdi(dp, 0);
1839 res = move_window(fs, dp->sect);
1840 if (res != FR_OK)
break;
1842 if ((fs->fs_type == FS_EXFAT) ? (
int)((dp->dir[XDIR_Type] & 0x80) == 0) : (
int)(dp->dir[DIR_Name] == DDEM || dp->dir[DIR_Name] == 0)) {
1844 if (dp->dir[DIR_Name] == DDEM || dp->dir[DIR_Name] == 0) {
1846 if (++n == n_ent)
break;
1850 res = dir_next(dp, 1);
1851 }
while (res == FR_OK);
1854 if (res == FR_NO_FILE) res = FR_DENIED;
1867static DWORD ld_clust (
1874 cl = ld_16(dir + DIR_FstClusLO);
1875 if (fs->fs_type == FS_FAT32) {
1876 cl |= (DWORD)ld_16(dir + DIR_FstClusHI) << 16;
1884static void st_clust (
1890 st_16(dir + DIR_FstClusLO, (WORD)cl);
1891 if (fs->fs_type == FS_FAT32) {
1892 st_16(dir + DIR_FstClusHI, (WORD)(cl >> 16));
1905 const WCHAR* lfnbuf,
1913 if (ld_16(dir + LDIR_FstClusLO) != 0)
return 0;
1915 ni = (UINT)((dir[LDIR_Ord] & 0x3F) - 1) * 13;
1917 for (pchr = 1, di = 0; di < 13; di++) {
1918 chr = ld_16(dir + LfnOfs[di]);
1920 if (ni >= FF_MAX_LFN + 1 || ff_wtoupper(chr) != ff_wtoupper(lfnbuf[ni++])) {
1925 if (chr != 0xFFFF)
return 0;
1929 if ((dir[LDIR_Ord] & LLEF) && pchr && lfnbuf[ni])
return 0;
1935#if FF_FS_MINIMIZE <= 1 || FF_FS_RPATH >= 2 || FF_USE_LABEL || FF_FS_EXFAT
1940static int pick_lfn (
1949 if (ld_16(dir + LDIR_FstClusLO) != 0)
return 0;
1951 ni = (UINT)((dir[LDIR_Ord] & ~LLEF) - 1) * 13;
1953 for (pchr = 1, di = 0; di < 13; di++) {
1954 chr = ld_16(dir + LfnOfs[di]);
1956 if (ni >= FF_MAX_LFN + 1)
return 0;
1957 lfnbuf[ni++] = pchr = chr;
1959 if (chr != 0xFFFF)
return 0;
1963 if (dir[LDIR_Ord] & LLEF && pchr != 0) {
1964 if (ni >= FF_MAX_LFN + 1)
return 0;
1978static void put_lfn (
1989 dir[LDIR_Chksum] = sum;
1990 dir[LDIR_Attr] = AM_LFN;
1992 st_16(dir + LDIR_FstClusLO, 0);
1994 ni = (UINT)(ord - 1) * 13;
1997 if (chr != 0xFFFF) chr = lfn[ni++];
1998 st_16(dir + LfnOfs[di], chr);
1999 if (chr == 0) chr = 0xFFFF;
2000 }
while (++di < 13);
2001 if (chr == 0xFFFF || !lfn[ni]) ord |= LLEF;
2002 dir[LDIR_Ord] = ord;
2010#if FF_USE_LFN && !FF_FS_READONLY
2015static void gen_numname (
2026 memcpy(dst, src, 11);
2030 DWORD crc_sreg = seq;
2034 for (i = 0; i < 16; i++) {
2035 crc_sreg = (crc_sreg << 1) + (wc & 1);
2037 if (crc_sreg & 0x10000) crc_sreg ^= 0x11021;
2040 seq = (WORD)crc_sreg;
2046 c = (BYTE)((seq % 16) +
'0'); seq /= 16;
2047 if (c >
'9') c += 7;
2053 for (j = 0; j < i && dst[j] !=
' '; j++) {
2054 if (dbc_1st(dst[j])) {
2055 if (j == i - 1)
break;
2060 dst[j++] = (i < 8) ? ns[i++] :
' ';
2072static BYTE sum_sfn (
2080 sum = (sum >> 1) + (sum << 7) + *dir++;
2094static WORD xdir_sum (
2102 szblk = ((UINT)dir[XDIR_NumSec] + 1) * SZDIRE;
2103 for (i = sum = 0; i < szblk; i++) {
2104 if (i == XDIR_SetSum) {
2107 sum = ((sum & 1) ? 0x8000 : 0) + (sum >> 1) + dir[i];
2115static WORD xname_sum (
2123 while ((chr = *name++) != 0) {
2124 chr = (WCHAR)ff_wtoupper(chr);
2125 sum = ((sum & 1) ? 0x8000 : 0) + (sum >> 1) + (chr & 0xFF);
2126 sum = ((sum & 1) ? 0x8000 : 0) + (sum >> 1) + (chr >> 8);
2132#if !FF_FS_READONLY && FF_USE_MKFS
2133static DWORD xsum32 (
2138 sum = ((sum & 1) ? 0x80000000 : 0) + (sum >> 1) + dat;
2149static FRESULT load_xdir (
2155 BYTE *dirb = dp->obj.fs->dirbuf;
2159 res = move_window(dp->obj.fs, dp->sect);
2160 if (res != FR_OK)
return res;
2161 if (dp->dir[XDIR_Type] != ET_FILEDIR)
return FR_INT_ERR;
2162 memcpy(dirb + 0 * SZDIRE, dp->dir, SZDIRE);
2163 sz_ent = ((UINT)dirb[XDIR_NumSec] + 1) * SZDIRE;
2164 if (sz_ent < 3 * SZDIRE || sz_ent > 19 * SZDIRE)
return FR_INT_ERR;
2167 res = dir_next(dp, 0);
2168 if (res == FR_NO_FILE) res = FR_INT_ERR;
2169 if (res != FR_OK)
return res;
2170 res = move_window(dp->obj.fs, dp->sect);
2171 if (res != FR_OK)
return res;
2172 if (dp->dir[XDIR_Type] != ET_STREAM)
return FR_INT_ERR;
2173 memcpy(dirb + 1 * SZDIRE, dp->dir, SZDIRE);
2174 if (MAXDIRB(dirb[XDIR_NumName]) > sz_ent)
return FR_INT_ERR;
2179 res = dir_next(dp, 0);
2180 if (res == FR_NO_FILE) res = FR_INT_ERR;
2181 if (res != FR_OK)
return res;
2182 res = move_window(dp->obj.fs, dp->sect);
2183 if (res != FR_OK)
return res;
2184 if (dp->dir[XDIR_Type] != ET_FILENAME)
return FR_INT_ERR;
2185 if (i < MAXDIRB(FF_MAX_LFN)) memcpy(dirb + i, dp->dir, SZDIRE);
2186 }
while ((i += SZDIRE) < sz_ent);
2189 if (i <= MAXDIRB(FF_MAX_LFN)) {
2190 if (xdir_sum(dirb) != ld_16(dirb + XDIR_SetSum))
return FR_INT_ERR;
2201static void init_alloc_info (
2206 FATFS *fs = dobj->fs;
2210 dobj->c_scl = sdir->obj.sclust;
2211 dobj->c_size = ((DWORD)sdir->obj.objsize & 0xFFFFFF00) | sdir->obj.stat;
2212 dobj->c_ofs = sdir->blk_ofs;
2214 dobj->sclust = ld_32(fs->dirbuf + XDIR_FstClus);
2215 dobj->objsize = ld_64(fs->dirbuf + XDIR_FileSize);
2216 dobj->stat = fs->dirbuf[XDIR_GenFlags] & 2;
2222#if !FF_FS_READONLY || FF_FS_RPATH
2227static FRESULT load_obj_xdir (
2235 dp->obj.fs = obj->fs;
2236 dp->obj.sclust = obj->c_scl;
2237 dp->obj.stat = (BYTE)obj->c_size;
2238 dp->obj.objsize = obj->c_size & 0xFFFFFF00;
2240 dp->blk_ofs = obj->c_ofs;
2242 res = dir_sdi(dp, dp->blk_ofs);
2244 res = load_xdir(dp);
2256static FRESULT store_xdir (
2262 BYTE *dirb = dp->obj.fs->dirbuf;
2265 st_16(dirb + XDIR_SetSum, xdir_sum(dirb));
2268 nent = dirb[XDIR_NumSec] + 1;
2269 res = dir_sdi(dp, dp->blk_ofs);
2270 while (res == FR_OK) {
2272 res = move_window(dp->obj.fs, dp->sect);
2273 if (res != FR_OK)
break;
2274 memcpy(dp->dir, dirb, SZDIRE);
2275 dp->obj.fs->wflag = 1;
2277 if (--nent == 0)
break;
2279 res = dir_next(dp, 0);
2281 return (res == FR_OK || res == FR_DISK_ERR) ? res : FR_INT_ERR;
2290static void create_xdir (
2301 memset(dirb, 0, 2 * SZDIRE);
2302 dirb[0 * SZDIRE + XDIR_Type] = ET_FILEDIR;
2303 dirb[1 * SZDIRE + XDIR_Type] = ET_STREAM;
2307 nlen = n_c1 = 0; chr = 1;
2309 dirb[i++] = ET_FILENAME; dirb[i++] = 0;
2311 if (chr != 0 && (chr = lfn[nlen]) != 0) nlen++;
2312 st_16(dirb + i, chr);
2314 }
while (i % SZDIRE != 0);
2316 }
while (lfn[nlen]);
2318 dirb[XDIR_NumName] = nlen;
2319 dirb[XDIR_NumSec] = 1 + n_c1;
2320 st_16(dirb + XDIR_NameHash, xname_sum(lfn));
2328#if FF_FS_MINIMIZE <= 1 || FF_FS_RPATH >= 2 || FF_USE_LABEL || FF_FS_EXFAT
2333#define DIR_READ_FILE(dp) dir_read(dp, 0)
2334#define DIR_READ_LABEL(dp) dir_read(dp, 1)
2336static FRESULT dir_read (
2341 FRESULT res = FR_NO_FILE;
2342 FATFS *fs = dp->obj.fs;
2345 BYTE ord = 0xFF, sum = 0xFF;
2349 res = move_window(fs, dp->sect);
2350 if (res != FR_OK)
break;
2351 et = dp->dir[DIR_Name];
2353 res = FR_NO_FILE;
break;
2356 if (fs->fs_type == FS_EXFAT) {
2357 if (FF_USE_LABEL && vol) {
2358 if (et == ET_VLABEL)
break;
2360 if (et == ET_FILEDIR) {
2361 dp->blk_ofs = dp->dptr;
2362 res = load_xdir(dp);
2364 dp->obj.attr = fs->dirbuf[XDIR_Attr] & AM_MASK;
2372 dp->obj.attr = attr = dp->dir[DIR_Attr] & AM_MASK;
2374 if (et == DDEM || et ==
'.' || (
int)((attr & ~AM_ARC) == AM_VOL) != vol) {
2377 if (attr == AM_LFN) {
2379 sum = dp->dir[LDIR_Chksum];
2380 et &= (BYTE)~LLEF; ord = et;
2381 dp->blk_ofs = dp->dptr;
2384 ord = (et == ord && sum == dp->dir[LDIR_Chksum] && pick_lfn(fs->lfnbuf, dp->dir)) ? ord - 1 : 0xFF;
2386 if (ord != 0 || sum != sum_sfn(dp->dir)) {
2387 dp->blk_ofs = 0xFFFFFFFF;
2393 if (et != DDEM && et !=
'.' && attr != AM_LFN && (
int)((attr & ~AM_ARC) == AM_VOL) == vol) {
2398 res = dir_next(dp, 0);
2399 if (res != FR_OK)
break;
2402 if (res != FR_OK) dp->sect = 0;
2414static FRESULT dir_find (
2419 FATFS *fs = dp->obj.fs;
2422 BYTE attr, ord, sum;
2425 res = dir_sdi(dp, 0);
2426 if (res != FR_OK)
return res;
2428 if (fs->fs_type == FS_EXFAT) {
2431 WORD hash = xname_sum(fs->lfnbuf);
2433 while ((res = DIR_READ_FILE(dp)) == FR_OK) {
2435 if (fs->dirbuf[XDIR_NumName] > FF_MAX_LFN)
continue;
2437 if (ld_16(fs->dirbuf + XDIR_NameHash) != hash)
continue;
2438 for (nc = fs->dirbuf[XDIR_NumName], di = SZDIRE * 2, ni = 0; nc; nc--, di += 2, ni++) {
2439 if ((di % SZDIRE) == 0) di += 2;
2440 if (ff_wtoupper(ld_16(fs->dirbuf + di)) != ff_wtoupper(fs->lfnbuf[ni]))
break;
2442 if (nc == 0 && !fs->lfnbuf[ni])
break;
2449 ord = sum = 0xFF; dp->blk_ofs = 0xFFFFFFFF;
2452 res = move_window(fs, dp->sect);
2453 if (res != FR_OK)
break;
2454 et = dp->dir[DIR_Name];
2455 if (et == 0) { res = FR_NO_FILE;
break; }
2457 dp->obj.attr = attr = dp->dir[DIR_Attr] & AM_MASK;
2458 if (et == DDEM || ((attr & AM_VOL) && attr != AM_LFN)) {
2459 ord = 0xFF; dp->blk_ofs = 0xFFFFFFFF;
2461 if (attr == AM_LFN) {
2462 if (!(dp->fn[NSFLAG] & NS_NOLFN)) {
2466 dp->blk_ofs = dp->dptr;
2467 sum = dp->dir[LDIR_Chksum];
2470 ord = (et == ord && sum == dp->dir[LDIR_Chksum] && cmp_lfn(fs->lfnbuf, dp->dir)) ? ord - 1 : 0xFF;
2473 if (ord == 0 && sum == sum_sfn(dp->dir))
break;
2474 if (!(dp->fn[NSFLAG] & NS_LOSS) && !memcmp(dp->dir, dp->fn, 11))
break;
2475 ord = 0xFF; dp->blk_ofs = 0xFFFFFFFF;
2479 dp->obj.attr = dp->dir[DIR_Attr] & AM_MASK;
2480 if (!(dp->dir[DIR_Attr] & AM_VOL) && !memcmp(dp->dir, dp->fn, 11))
break;
2482 res = dir_next(dp, 0);
2483 }
while (res == FR_OK);
2496static FRESULT dir_register (
2501 FATFS *fs = dp->obj.fs;
2507 if (dp->fn[NSFLAG] & (NS_DOT | NS_NONAME))
return FR_INVALID_NAME;
2508 for (len = 0; fs->lfnbuf[len]; len++) ;
2511 if (fs->fs_type == FS_EXFAT) {
2512 n_ent = (len + 14) / 15 + 2;
2513 res = dir_alloc(dp, n_ent);
2514 if (res != FR_OK)
return res;
2515 dp->blk_ofs = dp->dptr - SZDIRE * (n_ent - 1);
2517 if (dp->obj.stat & 4) {
2519 res = fill_first_frag(&dp->obj);
2520 if (res != FR_OK)
return res;
2521 res = fill_last_frag(&dp->obj, dp->clust, 0xFFFFFFFF);
2522 if (res != FR_OK)
return res;
2523 if (dp->obj.sclust != 0) {
2526 res = load_obj_xdir(&dj, &dp->obj);
2527 if (res != FR_OK)
return res;
2528 dp->obj.objsize += (DWORD)fs->csize * SS(fs);
2529 st_64(fs->dirbuf + XDIR_FileSize, dp->obj.objsize);
2530 st_64(fs->dirbuf + XDIR_ValidFileSize, dp->obj.objsize);
2531 fs->dirbuf[XDIR_GenFlags] = dp->obj.stat | 1;
2532 res = store_xdir(&dj);
2533 if (res != FR_OK)
return res;
2535 for (n = 1; n <= fs->xcwds.depth && dp->obj.sclust != fs->xcwds.tbl[n].d_scl; n++) ;
2536 if (n <= fs->xcwds.depth) {
2537 fs->xcwds.tbl[n].d_size = (DWORD)dp->obj.objsize | dp->obj.stat;
2543 create_xdir(fs->dirbuf, fs->lfnbuf);
2548 memcpy(sn, dp->fn, 12);
2549 if (sn[NSFLAG] & NS_LOSS) {
2550 dp->fn[NSFLAG] = NS_NOLFN;
2551 for (n = 1; n < 100; n++) {
2552 gen_numname(dp->fn, sn, fs->lfnbuf, (WORD)n);
2554 if (res != FR_OK)
break;
2556 if (n == 100)
return FR_DENIED;
2557 if (res != FR_NO_FILE)
return res;
2558 dp->fn[NSFLAG] = sn[NSFLAG];
2562 n_ent = (sn[NSFLAG] & NS_LFN) ? (len + 12) / 13 + 1 : 1;
2563 res = dir_alloc(dp, n_ent);
2564 if (res == FR_OK && --n_ent) {
2565 res = dir_sdi(dp, dp->dptr - n_ent * SZDIRE);
2567 BYTE sum = sum_sfn(dp->fn);
2570 res = move_window(fs, dp->sect);
2571 if (res != FR_OK)
break;
2572 put_lfn(fs->lfnbuf, dp->dir, (BYTE)n_ent, sum);
2574 res = dir_next(dp, 0);
2575 }
while (res == FR_OK && --n_ent);
2580 res = dir_alloc(dp, 1);
2586 res = move_window(fs, dp->sect);
2588 memset(dp->dir, 0, SZDIRE);
2589 memcpy(dp->dir + DIR_Name, dp->fn, 11);
2591 dp->dir[DIR_NTres] = dp->fn[NSFLAG] & (NS_BODY | NS_EXT);
2604#if !FF_FS_READONLY && FF_FS_MINIMIZE == 0
2609static FRESULT dir_remove (
2614 FATFS *fs = dp->obj.fs;
2616 DWORD last = dp->dptr;
2618 res = (dp->blk_ofs == 0xFFFFFFFF) ? FR_OK : dir_sdi(dp, dp->blk_ofs);
2621 res = move_window(fs, dp->sect);
2622 if (res != FR_OK)
break;
2623 if (FF_FS_EXFAT && fs->fs_type == FS_EXFAT) {
2624 dp->dir[XDIR_Type] &= 0x7F;
2626 dp->dir[DIR_Name] = DDEM;
2629 if (dp->dptr >= last)
break;
2630 res = dir_next(dp, 0);
2631 }
while (res == FR_OK);
2632 if (res == FR_NO_FILE) res = FR_INT_ERR;
2636 res = move_window(fs, dp->sect);
2638 dp->dir[DIR_Name] = DDEM;
2650#if FF_FS_MINIMIZE <= 1 || FF_FS_RPATH >= 2
2655static void get_fileinfo (
2663 FATFS *fs = dp->obj.fs;
2671 if (dp->sect == 0)
return;
2675 if (fs->fs_type == FS_EXFAT) {
2678 si = SZDIRE * 2; di = 0;
2680 while (nc < fs->dirbuf[XDIR_NumName]) {
2681 if (si >= MAXDIRB(FF_MAX_LFN)) {
2684 if ((si % SZDIRE) == 0) si += 2;
2685 wc = ld_16(fs->dirbuf + si); si += 2; nc++;
2686 if (hs == 0 && IsSurrogate(wc)) {
2689 nw = put_utf((DWORD)hs << 16 | wc, &fno->fname[di], FF_LFN_BUF - di);
2696 if (hs != 0) di = 0;
2697 if (di == 0) fno->fname[di++] =
'\?';
2699 fno->altname[0] = 0;
2701 fno->fattrib = fs->dirbuf[XDIR_Attr] & AM_MASKX;
2702 fno->fsize = (fno->fattrib & AM_DIR) ? 0 : ld_64(fs->dirbuf + XDIR_FileSize);
2703 fno->ftime = ld_16(fs->dirbuf + XDIR_ModTime + 0);
2704 fno->fdate = ld_16(fs->dirbuf + XDIR_ModTime + 2);
2706 fno->crtime = ld_16(fs->dirbuf + XDIR_CrtTime + 0);
2707 fno->crdate = ld_16(fs->dirbuf + XDIR_CrtTime + 2);
2713 if (dp->blk_ofs != 0xFFFFFFFF) {
2716 while (fs->lfnbuf[si] != 0) {
2717 wc = fs->lfnbuf[si++];
2718 if (hs == 0 && IsSurrogate(wc)) {
2721 nw = put_utf((DWORD)hs << 16 | wc, &fno->fname[di], FF_LFN_BUF - di);
2728 if (hs != 0) di = 0;
2736 if (wc ==
' ')
continue;
2737 if (wc == RDDEM) wc = DDEM;
2738 if (si == 9 && di < FF_SFN_BUF) fno->altname[di++] =
'.';
2739#if FF_LFN_UNICODE >= 1
2740 if (dbc_1st((BYTE)wc) && si != 8 && si != 11 && dbc_2nd(dp->dir[si])) {
2741 wc = wc << 8 | dp->dir[si++];
2743 wc = ff_oem2uni(wc, CODEPAGE);
2747 nw = put_utf(wc, &fno->altname[di], FF_SFN_BUF - di);
2753 fno->altname[di++] = (TCHAR)wc;
2756 fno->altname[di] = 0;
2758 if (!fno->fname[0]) {
2760 fno->fname[di++] =
'\?';
2762 BYTE lcflg = NS_BODY;
2764 for (si = di = 0; fno->altname[si]; si++, di++) {
2765 wc = (WCHAR)fno->altname[si];
2766 if (wc ==
'.') lcflg = NS_EXT;
2767 if (IsUpper(wc) && (dp->dir[DIR_NTres] & lcflg)) wc += 0x20;
2768 fno->fname[di] = (TCHAR)wc;
2772 if (!dp->dir[DIR_NTres]) fno->altname[0] = 0;
2778 c = (TCHAR)dp->dir[si++];
2779 if (c ==
' ')
continue;
2780 if (c == RDDEM) c = DDEM;
2781 if (si == 9) fno->fname[di++] =
'.';
2782 fno->fname[di++] = c;
2787 fno->fattrib = dp->dir[DIR_Attr] & AM_MASK;
2788 fno->fsize = ld_32(dp->dir + DIR_FileSize);
2789 fno->ftime = ld_16(dp->dir + DIR_ModTime + 0);
2790 fno->fdate = ld_16(dp->dir + DIR_ModTime + 2);
2792 fno->crtime = ld_16(dp->dir + DIR_CrtTime + 0);
2793 fno->crdate = ld_16(dp->dir + DIR_CrtTime + 2);
2801#if FF_USE_FIND && FF_FS_MINIMIZE <= 1
2806#define FIND_RECURS 4
2809static DWORD get_achar (
2816#if FF_USE_LFN && FF_LFN_UNICODE >= 1
2817 chr = tchar2uni(ptr);
2818 if (chr == 0xFFFFFFFF) chr = 0;
2819 chr = ff_wtoupper(chr);
2822 chr = (BYTE)*(*ptr)++;
2823 if (IsLower(chr)) chr -= 0x20;
2824#if FF_CODE_PAGE == 0
2825 if (ExCvt && chr >= 0x80) chr = ExCvt[chr - 0x80];
2826#elif FF_CODE_PAGE < 900
2827 if (chr >= 0x80) chr = ExCvt[chr - 0x80];
2829#if FF_CODE_PAGE == 0 || FF_CODE_PAGE >= 900
2830 if (dbc_1st((BYTE)chr)) {
2831 chr = dbc_2nd((BYTE)**ptr) ? chr << 8 | (BYTE)*(*ptr)++ : 0;
2840static int pattern_match (
2853 while ((skip & 0xFF) != 0) {
2854 if (!get_achar(&nam))
return 0;
2857 if (*pat == 0 && skip)
return 1;
2860 pptr = pat; nptr = nam;
2862 if (*pptr ==
'\?' || *pptr ==
'*') {
2863 if (recur == 0)
return 0;
2866 if (*pptr++ ==
'\?') {
2871 }
while (*pptr ==
'\?' || *pptr ==
'*');
2872 if (pattern_match(pptr, nptr, sk, recur - 1))
return 1;
2873 nchr = *nptr;
break;
2875 pchr = get_achar(&pptr);
2876 nchr = get_achar(&nptr);
2877 if (pchr != nchr)
break;
2878 if (pchr == 0)
return 1;
2881 }
while (skip && nchr);
2894static FRESULT create_name (
2909 p = *path; lfn = dp->obj.fs->lfnbuf; di = 0;
2912 if (uc == 0xFFFFFFFF)
return FR_INVALID_NAME;
2913 if (uc >= 0x10000) lfn[di++] = (WCHAR)(uc >> 16);
2915 if (wc <
' ' || IsSeparator(wc))
break;
2916 if (wc < 0x80 && strchr(
"*:<>|\"\?\x7F", (
int)wc))
return FR_INVALID_NAME;
2917 if (di >= FF_MAX_LFN)
return FR_INVALID_NAME;
2923 while (IsSeparator(*p)) p++;
2925 if (IsTerminator(*p)) cf = NS_LAST;
2930 if ((di == 1 && lfn[di - 1] ==
'.') ||
2931 (di == 2 && lfn[di - 1] ==
'.' && lfn[di - 2] ==
'.')) {
2933 for (i = 0; i < 11; i++) {
2934 dp->fn[i] = (i < di) ?
'.' :
' ';
2936 dp->fn[i] = cf | NS_DOT;
2942 if (wc !=
' ' && wc !=
'.')
break;
2946 if (di == 0)
return FR_INVALID_NAME;
2949 for (si = 0; lfn[si] ==
' '; si++) ;
2950 if (si > 0 || lfn[si] ==
'.') cf |= NS_LOSS | NS_LFN;
2951 while (di > 0 && lfn[di - 1] !=
'.') di--;
2953 memset(dp->fn,
' ', 11);
2958 if (wc ==
' ' || (wc ==
'.' && si != di)) {
2959 cf |= NS_LOSS | NS_LFN;
2963 if (i >= ni || si == di) {
2965 cf |= NS_LOSS | NS_LFN;
2968 if (si != di) cf |= NS_LOSS | NS_LFN;
2970 si = di; i = 8; ni = 11; b <<= 2;
2976#if FF_CODE_PAGE == 0
2978 wc = ff_uni2oem(wc, CODEPAGE);
2979 if (wc & 0x80) wc = ExCvt[wc & 0x7F];
2981 wc = ff_uni2oem(ff_wtoupper(wc), CODEPAGE);
2983#elif FF_CODE_PAGE < 900
2984 wc = ff_uni2oem(wc, CODEPAGE);
2985 if (wc & 0x80) wc = ExCvt[wc & 0x7F];
2987 wc = ff_uni2oem(ff_wtoupper(wc), CODEPAGE);
2993 cf |= NS_LOSS | NS_LFN;
2996 dp->fn[i++] = (BYTE)(wc >> 8);
2998 if (wc == 0 || strchr(
"+,;=[]", (
int)wc)) {
2999 wc =
'_'; cf |= NS_LOSS | NS_LFN;
3009 dp->fn[i++] = (BYTE)wc;
3012 if (dp->fn[0] == DDEM) dp->fn[0] = RDDEM;
3014 if (ni == 8) b <<= 2;
3015 if ((b & 0x0C) == 0x0C || (b & 0x03) == 0x03) cf |= NS_LFN;
3016 if (!(cf & NS_LFN)) {
3017 if (b & 0x01) cf |= NS_EXT;
3018 if (b & 0x04) cf |= NS_BODY;
3021 dp->fn[NSFLAG] = cf;
3033 p = *path; sfn = dp->fn;
3034 memset(sfn,
' ', 11);
3040 if (c !=
'.' || si >= 3)
break;
3043 if (IsSeparator(c)) {
3044 while (IsSeparator(p[si])) si++;
3045 if ((BYTE)p[si] <=
' ') c = 0;
3046 }
else if (c >
' ') {
3047 return FR_INVALID_NAME;
3050 sfn[NSFLAG] = (c <=
' ') ? NS_LAST | NS_DOT : NS_DOT;
3056 if (c <=
' ')
break;
3057 if (IsSeparator(c)) {
3058 while (IsSeparator(p[si])) si++;
3061 if (c ==
'.' || i >= ni) {
3062 if (ni == 11 || c !=
'.')
return FR_INVALID_NAME;
3066#if FF_CODE_PAGE == 0
3067 if (ExCvt && c >= 0x80) {
3068 c = ExCvt[c & 0x7F];
3070#elif FF_CODE_PAGE < 900
3072 c = ExCvt[c & 0x7F];
3077 if (!dbc_2nd(d) || i >= ni - 1)
return FR_INVALID_NAME;
3081 if (strchr(
"*+,:;<=>[]|\"\?\x7F", (
int)c))
return FR_INVALID_NAME;
3082 if (IsLower(c)) c -= 0x20;
3087 if (i == 0)
return FR_INVALID_NAME;
3089 if (sfn[0] == DDEM) sfn[0] = RDDEM;
3090 sfn[NSFLAG] = (c <=
' ' || p[si] <=
' ') ? NS_LAST : 0;
3103static FRESULT follow_path (
3110 FATFS *fs = dp->obj.fs;
3115 if (!IsSeparator(*path) && (FF_STR_VOLUME_ID != 2 || !IsTerminator(*path))) {
3116 dp->obj.sclust = fs->cdir;
3120 while (IsSeparator(*path)) path++;
3127 if (fs->fs_type == FS_EXFAT) {
3128 if (dp->obj.sclust) {
3130 memcpy(&fs->xcwds2, &fs->xcwds,
sizeof fs->xcwds2);
3131 dp->obj.stat = (BYTE)fs->xcwds2.tbl[fs->xcwds2.depth].d_size;
3132 dp->obj.objsize = fs->xcwds2.tbl[fs->xcwds2.depth].d_size & 0xFFFFFF00;
3133 dp->obj.c_scl = fs->xcwds2.tbl[fs->xcwds2.depth - 1].d_scl;
3134 dp->obj.c_size = fs->xcwds2.tbl[fs->xcwds2.depth - 1].d_size & 0xFFFFFF00;
3135 dp->obj.c_ofs = fs->xcwds2.tbl[fs->xcwds2.depth - 1].nxt_ofs;
3138 memset(&fs->xcwds2, 0,
sizeof fs->xcwds2);
3144 if ((UINT)*path <
' ') {
3145 dp->fn[NSFLAG] = NS_NONAME;
3146 res = dir_sdi(dp, 0);
3150 res = create_name(dp, &path);
3151 if (res != FR_OK)
break;
3152 ns = dp->fn[NSFLAG];
3153#if FF_FS_EXFAT && FF_FS_RPATH
3154 if (fs->fs_type == FS_EXFAT && (ns & NS_DOT)) {
3156 if (fs->lfnbuf[1] ==
'.' && fs->xcwds2.depth) {
3158 dp->obj.sclust = fs->xcwds2.tbl[fs->xcwds2.depth].d_scl;
3159 dp->obj.stat = (BYTE)fs->xcwds2.tbl[fs->xcwds2.depth].d_size;
3160 dp->obj.objsize = fs->xcwds2.tbl[fs->xcwds2.depth].d_size & 0xFFFFFF00;
3161 if (fs->xcwds2.depth) {
3162 dp->obj.c_scl = fs->xcwds2.tbl[fs->xcwds2.depth - 1].d_scl;
3163 dp->obj.c_size = fs->xcwds2.tbl[fs->xcwds2.depth - 1].d_size;
3164 dp->obj.c_ofs = fs->xcwds2.tbl[fs->xcwds2.depth - 1].nxt_ofs;
3167 dp->obj.attr |= AM_DIR;
3168 dp->fn[NSFLAG] |= NS_NONAME;
3169 if (ns & NS_LAST)
break;
3175 if (res == FR_NO_FILE) {
3176 if (FF_FS_RPATH && (ns & NS_DOT)) {
3177 if (!(ns & NS_LAST))
continue;
3178 dp->fn[NSFLAG] = NS_NONAME;
3181 if (!(ns & NS_LAST)) res = FR_NO_PATH;
3186#if FF_FS_EXFAT && FF_FS_RPATH
3187 if (fs->fs_type == FS_EXFAT && (dp->obj.attr & AM_DIR)) {
3188 fs->xcwds2.tbl[fs->xcwds2.depth].nxt_ofs = dp->blk_ofs;
3189 if (++fs->xcwds2.depth >=
sizeof fs->xcwds2.tbl /
sizeof fs->xcwds2.tbl[0]) {
3190 res = FR_NOT_ENOUGH_CORE;
break;
3192 fs->xcwds2.tbl[fs->xcwds2.depth].d_scl = ld_32(fs->dirbuf + XDIR_FstClus);
3193 fs->xcwds2.tbl[fs->xcwds2.depth].d_size = ld_32(fs->dirbuf + XDIR_FileSize) | (fs->dirbuf[XDIR_GenFlags] & 2);
3196 if (ns & NS_LAST)
break;
3198 if (!(dp->obj.attr & AM_DIR)) {
3199 res = FR_NO_PATH;
break;
3202 if (fs->fs_type == FS_EXFAT) {
3203 init_alloc_info(&dp->obj, dp);
3207 dp->obj.sclust = ld_clust(fs, fs->win + dp->dptr % SS(fs));
3222static int get_ldnumber (
3239 }
while (!IsTerminator(chr) && chr !=
':');
3243 if (IsDigit(*tp) && tp + 2 == tt) {
3246#if FF_STR_VOLUME_ID == 1
3250 vsp = VolumeStr[i]; tp = *path;
3252 vchr = *vsp++; chr = *tp++;
3253 if (IsLower(vchr)) vchr -= 0x20;
3254 if (IsLower(chr)) chr -= 0x20;
3255 }
while (vchr && (TCHAR)vchr == chr);
3256 }
while ((vchr || tp != tt) && ++i < FF_VOLUMES);
3259 if (i >= FF_VOLUMES)
return -1;
3263#if FF_STR_VOLUME_ID == 2
3265 while (*(tp + 1) ==
'/') tp++;
3268 vsp = VolumeStr[i]; tt = tp;
3270 vchr = *vsp++; chr = *(++tt);
3271 if (IsLower(vchr)) vchr -= 0x20;
3272 if (IsLower(chr)) chr -= 0x20;
3273 }
while (vchr && (TCHAR)vchr == chr);
3274 }
while ((vchr || (chr !=
'/' && !IsTerminator(chr))) && ++i < FF_VOLUMES);
3275 if (i >= FF_VOLUMES)
return -1;
3282 return (
int)CurrVol;
3307 for (b = 1; b; b <<= 1) {
3308 crc ^= (d & b) ? 1 : 0;
3309 crc = (crc & 1) ? crc >> 1 ^ 0xEDB88320 : crc >> 1;
3317static int test_gpt_header (
3325 if (memcmp(gpth + GPTH_Sign,
"EFI PART" "\0\0\1", 12))
return 0;
3326 hlen = ld_32(gpth + GPTH_Size);
3327 if (hlen < 92 || hlen > FF_MIN_SS)
return 0;
3328 for (i = 0, bcc = 0xFFFFFFFF; i < hlen; i++) {
3329 bcc = crc32(bcc, i - GPTH_Bcc < 4 ? 0 : gpth[i]);
3331 if (~bcc != ld_32(gpth + GPTH_Bcc))
return 0;
3332 if (ld_32(gpth + GPTH_PteSize) != SZ_GPTE)
return 0;
3333 if (ld_32(gpth + GPTH_PtNum) > 128)
return 0;
3338#if !FF_FS_READONLY && FF_USE_MKFS
3341static DWORD make_rand (
3350 if (seed == 0) seed = 1;
3352 for (r = 0; r < 8; r++) seed = seed & 1 ? seed >> 1 ^ 0xA3000000 : seed >> 1;
3353 *buff++ = (BYTE)seed;
3369static UINT check_fs (
3378 fs->wflag = 0; fs->winsect = (LBA_t)0 - 1;
3379 if (move_window(fs, sect) != FR_OK)
return 4;
3380 sign = ld_16(fs->win + BS_55AA);
3382 if (sign == 0xAA55 && !memcmp(fs->win + BS_JmpBoot,
"\xEB\x76\x90" "EXFAT ", 11))
return 1;
3384 b = fs->win[BS_JmpBoot];
3385 if (b == 0xEB || b == 0xE9 || b == 0xE8) {
3386 if (sign == 0xAA55 && !memcmp(fs->win + BS_FilSysType32,
"FAT32 ", 8)) {
3390 w = ld_16(fs->win + BPB_BytsPerSec);
3391 b = fs->win[BPB_SecPerClus];
3392 if ((w & (w - 1)) == 0 && w >= FF_MIN_SS && w <= FF_MAX_SS
3393 && b != 0 && (b & (b - 1)) == 0
3394 && ld_16(fs->win + BPB_RsvdSecCnt) != 0
3395 && (UINT)fs->win[BPB_NumFATs] - 1 <= 1
3396 && ld_16(fs->win + BPB_RootEntCnt) != 0
3397 && (ld_16(fs->win + BPB_TotSec16) >= 128 || ld_32(fs->win + BPB_TotSec32) >= 0x10000)
3398 && ld_16(fs->win + BPB_FATSz16) != 0) {
3402 return sign == 0xAA55 ? 2 : 3;
3409static UINT find_volume (
3418 fmt = check_fs(fs, 0);
3422 if (fmt != 2 && (fmt >= 3 || part == 0))
return fmt;
3427 if (fs->win[MBR_Table + PTE_System] == 0xEE) {
3428 DWORD n_ent, v_ent, ofs;
3431 if (move_window(fs, 1) != FR_OK)
return 4;
3432 if (!test_gpt_header(fs->win))
return 3;
3433 n_ent = ld_32(fs->win + GPTH_PtNum);
3434 pt_lba = ld_64(fs->win + GPTH_PtOfs);
3435 for (v_ent = i = 0; i < n_ent; i++) {
3436 if (move_window(fs, pt_lba + i * SZ_GPTE / SS(fs)) != FR_OK)
return 4;
3437 ofs = i * SZ_GPTE % SS(fs);
3438 if (!memcmp(fs->win + ofs + GPTE_PtGuid, GUID_MS_Basic, 16)) {
3440 fmt = check_fs(fs, ld_64(fs->win + ofs + GPTE_FstLba));
3441 if (part == 0 && fmt <= 1)
return fmt;
3442 if (part != 0 && v_ent == part)
return fmt;
3448 if (FF_MULTI_PARTITION && part > 4)
return 3;
3449 for (i = 0; i < 4; i++) {
3450 mbr_pt[i] = ld_32(fs->win + MBR_Table + i * SZ_PTE + PTE_StLba);
3452 i = part ? part - 1 : 0;
3454 fmt = mbr_pt[i] ? check_fs(fs, mbr_pt[i]) : 3;
3455 }
while (part == 0 && fmt >= 2 && ++i < 4);
3466static FRESULT mount_volume (
3481 vol = get_ldnumber(path);
3482 if (vol < 0)
return FR_INVALID_DRIVE;
3486 if (!fs)
return FR_NOT_ENABLED;
3488 if (!lock_volume(fs, 1))
return FR_TIMEOUT;
3492 mode &= (BYTE)~FA_READ;
3493 if (fs->fs_type != 0) {
3494 stat = disk_status(fs->pdrv);
3495 if (!(stat & STA_NOINIT)) {
3496 if (!FF_FS_READONLY && mode && (stat & STA_PROTECT)) {
3497 return FR_WRITE_PROTECTED;
3507 stat = disk_initialize(fs->pdrv);
3508 if (stat & STA_NOINIT) {
3509 return FR_NOT_READY;
3511 if (!FF_FS_READONLY && mode && (stat & STA_PROTECT)) {
3512 return FR_WRITE_PROTECTED;
3514#if FF_MAX_SS != FF_MIN_SS
3515 if (disk_ioctl(fs->pdrv, GET_SECTOR_SIZE, &SS(fs)) != RES_OK)
return FR_DISK_ERR;
3516 if (SS(fs) > FF_MAX_SS || SS(fs) < FF_MIN_SS || (SS(fs) & (SS(fs) - 1)))
return FR_DISK_ERR;
3520 fmt = find_volume(fs, LD2PT(vol));
3521 u64 winsect = fs->winsect;
3522 DEBUG_U64_2XU32(winsect);
3523 M_DEBUG(
"check_fs returned %d at LBA 0x%08x%08x\n", fmt, winsect_u32[1], winsect_u32[0]);
3524 if (fmt == 4)
return FR_DISK_ERR;
3525 if (fmt >= 2)
return FR_NO_FILESYSTEM;
3533 DWORD so, cv, bcl, ncl, i;
3535 for (i = BPB_ZeroedEx; i < BPB_ZeroedEx + 53 && fs->win[i] == 0; i++) ;
3536 if (i < BPB_ZeroedEx + 53)
return FR_NO_FILESYSTEM;
3538 if (ld_16(fs->win + BPB_FSVerEx) != 0x100)
return FR_NO_FILESYSTEM;
3540 if (1 << fs->win[BPB_BytsPerSecEx] != SS(fs)) {
3541 return FR_NO_FILESYSTEM;
3544 maxlba = ld_64(fs->win + BPB_TotSecEx) + bsect;
3545 if (!FF_LBA64 && maxlba >= 0x100000000)
return FR_NO_FILESYSTEM;
3547 fs->fsize = ld_32(fs->win + BPB_FatSzEx);
3549 fs->n_fats = fs->win[BPB_NumFATsEx];
3550 if (fs->n_fats != 1)
return FR_NO_FILESYSTEM;
3552 fs->csize = 1 << fs->win[BPB_SecPerClusEx];
3553 if (fs->csize == 0)
return FR_NO_FILESYSTEM;
3555 ncl = ld_32(fs->win + BPB_NumClusEx);
3556 if (ncl > MAX_EXFAT)
return FR_NO_FILESYSTEM;
3557 fs->n_fatent = ncl + 2;
3560 fs->volbase = bsect;
3561 fs->database = bsect + ld_32(fs->win + BPB_DataOfsEx);
3562 fs->fatbase = bsect + ld_32(fs->win + BPB_FatOfsEx);
3563 if (maxlba < (
QWORD)fs->database + ncl * fs->csize)
return FR_NO_FILESYSTEM;
3564 fs->dirbase = ld_32(fs->win + BPB_RootClusEx);
3570 if (so >= fs->csize)
return FR_NO_FILESYSTEM;
3571 if (move_window(fs, clst2sect(fs, (DWORD)fs->dirbase) + so) != FR_OK)
return FR_DISK_ERR;
3574 if (fs->win[i] == ET_BITMAP)
break;
3575 i = (i + SZDIRE) % SS(fs);
3577 bcl = ld_32(fs->win + i + 20);
3578 if (bcl < 2 || bcl >= fs->n_fatent)
return FR_NO_FILESYSTEM;
3579 fs->bitbase = fs->database + fs->csize * (bcl - 2);
3581 if (move_window(fs, fs->fatbase + bcl / (SS(fs) / 4)) != FR_OK)
return FR_DISK_ERR;
3582 cv = ld_32(fs->win + bcl % (SS(fs) / 4) * 4);
3583 if (cv == 0xFFFFFFFF)
break;
3584 if (cv != ++bcl)
return FR_NO_FILESYSTEM;
3587 fs->last_clst = fs->free_clst = 0xFFFFFFFF;
3594 DWORD tsect, sysect, fasize, nclst, szbfat;
3597 if (ld_16(fs->win + BPB_BytsPerSec) != SS(fs))
return FR_NO_FILESYSTEM;
3599 fasize = ld_16(fs->win + BPB_FATSz16);
3600 if (fasize == 0) fasize = ld_32(fs->win + BPB_FATSz32);
3603 fs->n_fats = fs->win[BPB_NumFATs];
3604 if (fs->n_fats != 1 && fs->n_fats != 2)
return FR_NO_FILESYSTEM;
3605 fasize *= fs->n_fats;
3607 fs->csize = fs->win[BPB_SecPerClus];
3608 if (fs->csize == 0 || (fs->csize & (fs->csize - 1)))
return FR_NO_FILESYSTEM;
3610 fs->n_rootdir = ld_16(fs->win + BPB_RootEntCnt);
3611 if (fs->n_rootdir % (SS(fs) / SZDIRE))
return FR_NO_FILESYSTEM;
3613 tsect = ld_16(fs->win + BPB_TotSec16);
3614 if (tsect == 0) tsect = ld_32(fs->win + BPB_TotSec32);
3616 nrsv = ld_16(fs->win + BPB_RsvdSecCnt);
3617 if (nrsv == 0)
return FR_NO_FILESYSTEM;
3620 sysect = nrsv + fasize + fs->n_rootdir / (SS(fs) / SZDIRE);
3621 if (tsect < sysect)
return FR_NO_FILESYSTEM;
3622 nclst = (tsect - sysect) / fs->csize;
3623 if (nclst == 0)
return FR_NO_FILESYSTEM;
3625 if (nclst <= MAX_FAT32) fmt = FS_FAT32;
3626 if (nclst <= MAX_FAT16) fmt = FS_FAT16;
3627 if (nclst <= MAX_FAT12) fmt = FS_FAT12;
3628 if (fmt == 0)
return FR_NO_FILESYSTEM;
3631 fs->n_fatent = nclst + 2;
3632 fs->volbase = bsect;
3633 fs->fatbase = bsect + nrsv;
3634 fs->database = bsect + sysect;
3635 if (fmt == FS_FAT32) {
3636 if (ld_16(fs->win + BPB_FSVer32) != 0)
return FR_NO_FILESYSTEM;
3637 if (fs->n_rootdir != 0)
return FR_NO_FILESYSTEM;
3638 fs->dirbase = ld_32(fs->win + BPB_RootClus32);
3639 szbfat = fs->n_fatent * 4;
3641 if (fs->n_rootdir == 0)
return FR_NO_FILESYSTEM;
3642 fs->dirbase = fs->fatbase + fasize;
3643 szbfat = (fmt == FS_FAT16) ?
3644 fs->n_fatent * 2 : fs->n_fatent * 3 / 2 + (fs->n_fatent & 1);
3646 if (fs->fsize < (szbfat + (SS(fs) - 1)) / SS(fs))
return FR_NO_FILESYSTEM;
3650 fs->last_clst = fs->free_clst = 0xFFFFFFFF;
3651 fs->fsi_flag = 0x80;
3653 && ld_16(fs->win + BPB_FSInfo32) == 1
3654 && move_window(fs, bsect + 1) == FR_OK)
3657 if ( ld_32(fs->win + FSI_LeadSig) == 0x41615252
3658 && ld_32(fs->win + FSI_StrucSig) == 0x61417272
3659 && ld_32(fs->win + FSI_TrailSig) == 0xAA550000)
3661#if (FF_FS_NOFSINFO & 1) == 0
3662 fs->free_clst = ld_32(fs->win + FSI_Free_Count);
3664#if (FF_FS_NOFSINFO & 2) == 0
3665 fs->last_clst = ld_32(fs->win + FSI_Nxt_Free);
3672 fs->fs_type = (BYTE)fmt;
3676 fs->lfnbuf = LfnBuf;
3678 fs->dirbuf = DirBuf;
3685 memset(&fs->xcwds, 0,
sizeof fs->xcwds);
3703static FRESULT validate (
3708 FRESULT res = FR_INVALID_OBJECT;
3711 if (obj && obj->fs && obj->fs->fs_type && obj->id == obj->fs->id) {
3713 if (lock_volume(obj->fs, 0)) {
3714 if (!(disk_status(obj->fs->pdrv) & STA_NOINIT)) {
3717 unlock_volume(obj->fs, FR_OK);
3723 if (!(disk_status(obj->fs->pdrv) & STA_NOINIT)) {
3728 *rfs = (res == FR_OK) ? obj->fs : 0;
3756 const TCHAR *rp = path;
3760 vol = get_ldnumber(&rp);
3761 if (vol < 0)
return FR_INVALID_DRIVE;
3770 ff_mutex_delete(vol);
3776 fs->pdrv = LD2PD(vol);
3778 fs->ldrv = (BYTE)vol;
3779 if (!ff_mutex_create(vol))
return FR_INT_ERR;
3782 if (!ff_mutex_create(FF_VOLUMES)) {
3783 ff_mutex_delete(vol);
3794 if (opt == 0)
return FR_OK;
3796 res = mount_volume(&path, &fs, 0);
3819 if (!fp)
return FR_INVALID_OBJECT;
3822 mode &= FF_FS_READONLY ? FA_READ : FA_READ | FA_WRITE | FA_CREATE_ALWAYS | FA_CREATE_NEW | FA_OPEN_ALWAYS | FA_OPEN_APPEND;
3823 res = mount_volume(&path, &fs, mode);
3829 res = follow_path(&dj, path);
3832 if (dj.fn[NSFLAG] & NS_NONAME) {
3833 res = FR_INVALID_NAME;
3837 res = chk_share(&dj, (mode & ~FA_READ) ? 1 : 0);
3842 if (mode & (FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW)) {
3844 if (res == FR_NO_FILE) {
3846 res = enq_share() ? dir_register(&dj) : FR_TOO_MANY_OPEN_FILES;
3848 res = dir_register(&dj);
3851 mode |= FA_CREATE_ALWAYS;
3854 if (mode & FA_CREATE_NEW) {
3857 if (dj.obj.attr & (AM_RDO | AM_DIR)) res = FR_DENIED;
3860 if (res == FR_OK && (mode & FA_CREATE_ALWAYS)) {
3861 DWORD tm = GET_FATTIME();
3863 if (fs->fs_type == FS_EXFAT) {
3865 init_alloc_info(&fp->obj, 0);
3867 memset(fs->dirbuf + 2, 0, 30);
3868 memset(fs->dirbuf + 38, 0, 26);
3869 fs->dirbuf[XDIR_Attr] = AM_ARC;
3870 st_32(fs->dirbuf + XDIR_CrtTime, tm);
3871 st_32(fs->dirbuf + XDIR_ModTime, tm);
3872 fs->dirbuf[XDIR_GenFlags] = 1;
3873 res = store_xdir(&dj);
3874 if (res == FR_OK && fp->obj.sclust != 0) {
3875 res = remove_chain(&fp->obj, fp->obj.sclust, 0);
3876 fs->last_clst = fp->obj.sclust - 1;
3883 st_32(dj.dir + DIR_CrtTime, tm);
3884 st_32(dj.dir + DIR_ModTime, tm);
3885 cl = ld_clust(fs, dj.dir);
3886 dj.dir[DIR_Attr] = AM_ARC;
3887 st_clust(fs, dj.dir, 0);
3888 st_32(dj.dir + DIR_FileSize, 0);
3891 LBA_t sc = fs->winsect;
3893 res = remove_chain(&dj.obj, cl, 0);
3895 res = move_window(fs, sc);
3896 fs->last_clst = cl - 1;
3904 if (dj.obj.attr & AM_DIR) {
3907 if ((mode & FA_WRITE) && (dj.obj.attr & AM_RDO)) {
3914 if (mode & FA_CREATE_ALWAYS) mode |= FA_MODIFIED;
3915 fp->dir_sect = fs->winsect;
3916 fp->dir_ptr = dj.dir;
3918 fp->obj.lockid = inc_share(&dj, (mode & ~FA_READ) ? 1 : 0);
3919 if (fp->obj.lockid == 0) res = FR_INT_ERR;
3925 if (dj.fn[NSFLAG] & NS_NONAME) {
3926 res = FR_INVALID_NAME;
3928 if (dj.obj.attr & AM_DIR) {
3937 if (fs->fs_type == FS_EXFAT) {
3938 init_alloc_info(&fp->obj, &dj);
3942 fp->obj.sclust = ld_clust(fs, dj.dir);
3943 fp->obj.objsize = ld_32(dj.dir + DIR_FileSize);
3948 fp->obj.id = fs->id;
3955 memset(fp->buf, 0,
sizeof fp->buf);
3957 if ((mode & FA_SEEKEND) && fp->obj.objsize > 0) {
3961 fp->fptr = fp->obj.objsize;
3962 bcs = (DWORD)fs->csize * SS(fs);
3963 clst = fp->obj.sclust;
3964 for (ofs = fp->obj.objsize; res == FR_OK && ofs > bcs; ofs -= bcs) {
3965 clst = get_fat(&fp->obj, clst);
3966 if (clst <= 1) res = FR_INT_ERR;
3967 if (clst == 0xFFFFFFFF) res = FR_DISK_ERR;
3970 if (res == FR_OK && ofs % SS(fs)) {
3971 LBA_t sec = clst2sect(fs, clst);
3976 fp->sect = sec + (DWORD)(ofs / SS(fs));
3978 if (disk_read(fs->pdrv, fp->buf, fp->sect, 1) != RES_OK) res = FR_DISK_ERR;
3983 if (res != FR_OK) dec_share(fp->obj.lockid);
3992 if (res != FR_OK) fp->obj.fs = 0;
4015 UINT rcnt, cc, csect;
4016 BYTE *rbuff = (BYTE*)buff;
4020 res = validate(&fp->obj, &fs);
4021 if (res != FR_OK || (res = (FRESULT)fp->err) != FR_OK) LEAVE_FF(fs, res);
4022 if (!(fp->flag & FA_READ)) LEAVE_FF(fs, FR_DENIED);
4023 remain = fp->obj.objsize - fp->fptr;
4024 if (btr > remain) btr = (UINT)remain;
4026 for ( ; btr > 0; btr -= rcnt, *br += rcnt, rbuff += rcnt, fp->fptr += rcnt) {
4027 if (fp->fptr % SS(fs) == 0) {
4028 csect = (UINT)(fp->fptr / SS(fs) & (fs->csize - 1));
4032 if (fp->fptr == 0) {
4033 clst = fp->obj.sclust;
4037 clst = clmt_clust(fp, fp->fptr);
4041 clst = get_fat(&fp->obj, fp->clust);
4044 if (clst < 2) ABORT(fs, FR_INT_ERR);
4045 if (clst == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR);
4048 sect = clst2sect(fs, fp->clust);
4049 if (sect == 0) ABORT(fs, FR_INT_ERR);
4053 if (csect + cc > fs->csize) {
4054 cc = fs->csize - csect;
4056 if (disk_read(fs->pdrv, rbuff, sect, cc) != RES_OK) ABORT(fs, FR_DISK_ERR);
4057#if !FF_FS_READONLY && FF_FS_MINIMIZE <= 2
4059 if (fs->wflag && fs->winsect - sect < cc) {
4060 memcpy(rbuff + ((fs->winsect - sect) * SS(fs)), fs->win, SS(fs));
4063 if ((fp->flag & FA_DIRTY) && fp->sect - sect < cc) {
4064 memcpy(rbuff + ((fp->sect - sect) * SS(fs)), fp->buf, SS(fs));
4072 if (fp->sect != sect) {
4074 if (fp->flag & FA_DIRTY) {
4075 if (disk_write(fs->pdrv, fp->buf, fp->sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR);
4076 fp->flag &= (BYTE)~FA_DIRTY;
4079 if (disk_read(fs->pdrv, fp->buf, sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR);
4084 rcnt = SS(fs) - (UINT)fp->fptr % SS(fs);
4085 if (rcnt > btr) rcnt = btr;
4087 if (move_window(fs, fp->sect) != FR_OK) ABORT(fs, FR_DISK_ERR);
4088 memcpy(rbuff, fs->win + fp->fptr % SS(fs), rcnt);
4090 memcpy(rbuff, fp->buf + fp->fptr % SS(fs), rcnt);
4094 LEAVE_FF(fs, FR_OK);
4116 UINT wcnt, cc, csect;
4117 const BYTE *wbuff = (
const BYTE*)buff;
4121 res = validate(&fp->obj, &fs);
4122 if (res != FR_OK || (res = (FRESULT)fp->err) != FR_OK) LEAVE_FF(fs, res);
4123 if (!(fp->flag & FA_WRITE)) LEAVE_FF(fs, FR_DENIED);
4126 if ((!FF_FS_EXFAT || fs->fs_type != FS_EXFAT) && (DWORD)(fp->fptr + btw) < (DWORD)fp->fptr) {
4127 btw = (UINT)(0xFFFFFFFF - (DWORD)fp->fptr);
4130 for ( ; btw > 0; btw -= wcnt, *bw += wcnt, wbuff += wcnt, fp->fptr += wcnt, fp->obj.objsize = (fp->fptr > fp->obj.objsize) ? fp->fptr : fp->obj.objsize) {
4131 if (fp->fptr % SS(fs) == 0) {
4132 csect = (UINT)(fp->fptr / SS(fs)) & (fs->csize - 1);
4134 if (fp->fptr == 0) {
4135 clst = fp->obj.sclust;
4137 clst = create_chain(&fp->obj, 0);
4142 clst = clmt_clust(fp, fp->fptr);
4146 clst = create_chain(&fp->obj, fp->clust);
4149 if (clst == 0)
break;
4150 if (clst == 1) ABORT(fs, FR_INT_ERR);
4151 if (clst == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR);
4153 if (fp->obj.sclust == 0) fp->obj.sclust = clst;
4156 if (fs->winsect == fp->sect && sync_window(fs) != FR_OK) ABORT(fs, FR_DISK_ERR);
4158 if (fp->flag & FA_DIRTY) {
4159 if (disk_write(fs->pdrv, fp->buf, fp->sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR);
4160 fp->flag &= (BYTE)~FA_DIRTY;
4163 sect = clst2sect(fs, fp->clust);
4164 if (sect == 0) ABORT(fs, FR_INT_ERR);
4168 if (csect + cc > fs->csize) {
4169 cc = fs->csize - csect;
4171 if (disk_write(fs->pdrv, wbuff, sect, cc) != RES_OK) ABORT(fs, FR_DISK_ERR);
4172#if FF_FS_MINIMIZE <= 2
4174 if (fs->winsect - sect < cc) {
4175 memcpy(fs->win, wbuff + ((fs->winsect - sect) * SS(fs)), SS(fs));
4179 if (fp->sect - sect < cc) {
4180 memcpy(fp->buf, wbuff + ((fp->sect - sect) * SS(fs)), SS(fs));
4181 fp->flag &= (BYTE)~FA_DIRTY;
4189 if (fp->fptr >= fp->obj.objsize) {
4190 if (sync_window(fs) != FR_OK) ABORT(fs, FR_DISK_ERR);
4194 if (fp->sect != sect &&
4195 fp->fptr < fp->obj.objsize &&
4196 disk_read(fs->pdrv, fp->buf, sect, 1) != RES_OK) {
4197 ABORT(fs, FR_DISK_ERR);
4202 wcnt = SS(fs) - (UINT)fp->fptr % SS(fs);
4203 if (wcnt > btw) wcnt = btw;
4205 if (move_window(fs, fp->sect) != FR_OK) ABORT(fs, FR_DISK_ERR);
4206 memcpy(fs->win + fp->fptr % SS(fs), wbuff, wcnt);
4209 memcpy(fp->buf + fp->fptr % SS(fs), wbuff, wcnt);
4210 fp->flag |= FA_DIRTY;
4214 fp->flag |= FA_MODIFIED;
4216 LEAVE_FF(fs, FR_OK);
4234 res = validate(&fp->obj, &fs);
4236 if (fp->flag & FA_MODIFIED) {
4238 if (fp->flag & FA_DIRTY) {
4239 if (disk_write(fs->pdrv, fp->buf, fp->sect, 1) != RES_OK) LEAVE_FF(fs, FR_DISK_ERR);
4240 fp->flag &= (BYTE)~FA_DIRTY;
4245 if (fs->fs_type == FS_EXFAT) {
4246 res = fill_first_frag(&fp->obj);
4248 res = fill_last_frag(&fp->obj, fp->clust, 0xFFFFFFFF);
4255 res = load_obj_xdir(&dj, &fp->obj);
4257 fs->dirbuf[XDIR_Attr] |= AM_ARC;
4258 fs->dirbuf[XDIR_GenFlags] = fp->obj.stat | 1;
4259 st_32(fs->dirbuf + XDIR_FstClus, fp->obj.sclust);
4260 st_64(fs->dirbuf + XDIR_FileSize, fp->obj.objsize);
4261 st_64(fs->dirbuf + XDIR_ValidFileSize, fp->obj.objsize);
4262 st_32(fs->dirbuf + XDIR_ModTime, GET_FATTIME());
4263 fs->dirbuf[XDIR_ModTime10] = 0;
4264 fs->dirbuf[XDIR_ModTZ] = 0;
4265 st_32(fs->dirbuf + XDIR_AccTime, 0);
4266 fs->dirbuf[XDIR_AccTZ] = 0;
4267 res = store_xdir(&dj);
4270 fp->flag &= (BYTE)~FA_MODIFIED;
4278 res = move_window(fs, fp->dir_sect);
4280 BYTE *dir = fp->dir_ptr;
4282 dir[DIR_Attr] |= AM_ARC;
4283 st_clust(fp->obj.fs, dir, fp->obj.sclust);
4284 st_32(dir + DIR_FileSize, (DWORD)fp->obj.objsize);
4285 st_32(dir + DIR_ModTime, GET_FATTIME());
4286 st_16(dir + DIR_LstAccDate, 0);
4289 fp->flag &= (BYTE)~FA_MODIFIED;
4319 res = validate(&fp->obj, &fs);
4322 res = dec_share(fp->obj.lockid);
4323 if (res == FR_OK) fp->obj.fs = 0;
4328 unlock_volume(fs, FR_OK);
4351 vol = get_ldnumber(&path);
4352 if (vol < 0)
return FR_INVALID_DRIVE;
4353 CurrVol = (BYTE)vol;
4375 res = mount_volume(&path, &fs, 0);
4379 res = follow_path(&dj, path);
4381 if (dj.fn[NSFLAG] & NS_NONAME) {
4383 if (fs->fs_type == FS_EXFAT) {
4384 memcpy(&fs->xcwds, &fs->xcwds2,
sizeof fs->xcwds);
4387 fs->cdir = dj.obj.sclust;
4389 if (dj.obj.attr & AM_DIR) {
4391 if (fs->fs_type == FS_EXFAT) {
4392 memcpy(&fs->xcwds, &fs->xcwds2,
sizeof fs->xcwds);
4393 fs->cdir = fs->xcwds.tbl[fs->xcwds.depth].d_scl;
4397 fs->cdir = ld_clust(fs, dj.dir);
4405 if (res == FR_NO_FILE) res = FR_NO_PATH;
4406#if FF_STR_VOLUME_ID == 2
4410 for (i = FF_VOLUMES - 1; i && fs != FatFs[i]; i--) ;
4446 res = mount_volume((
const TCHAR**)&buff, &fs, 0);
4451 if (fs->fs_type == FS_EXFAT) {
4456#if FF_STR_VOLUME_ID == 0
4457 if (wi < len) buff[wi++] =
'0' + CurrVol;
4459 if (FF_STR_VOLUME_ID == 2 && wi < len) buff[wi++] =
'/';
4460 for (vid = (
const char*)VolumeStr[CurrVol]; *vid && wi < len; buff[wi++] = *vid++) ;
4462 if (FF_STR_VOLUME_ID <= 1 && wi < len) buff[wi++] =
':';
4464 if (wi < len) buff[wi++] =
'/';
4465 for (di = 0; wi < len && di < fs->xcwds.depth; di++) {
4466 dj.obj.sclust = fs->xcwds.tbl[di].d_scl;
4467 dj.obj.stat = (BYTE)fs->xcwds.tbl[di].d_size;
4468 dj.obj.objsize = fs->xcwds.tbl[di].d_size & 0xFFFFFF00;
4469 res = dir_sdi(&dj, fs->xcwds.tbl[di].nxt_ofs);
4470 if (res != FR_OK)
break;
4471 res = DIR_READ_FILE(&dj);
4472 if (res != FR_OK)
break;
4473 get_fileinfo(&dj, &fno);
4474 if (di > 0 && wi < len) buff[wi++] =
'/';
4475 for (ni = 0; fno.fname[ni] && wi < len; buff[wi++] = fno.fname[ni++]) ;
4478 res = FR_NOT_ENOUGH_CORE;
4492 dj.obj.sclust = fs->cdir;
4493 while ((ccl = dj.obj.sclust) != 0) {
4494 res = dir_sdi(&dj, 1 * SZDIRE);
4495 if (res != FR_OK)
break;
4496 res = move_window(fs, dj.sect);
4497 if (res != FR_OK)
break;
4498 dj.obj.sclust = ld_clust(fs, dj.dir);
4499 res = dir_sdi(&dj, 0);
4500 if (res != FR_OK)
break;
4502 res = DIR_READ_FILE(&dj);
4503 if (res != FR_OK)
break;
4504 if (ccl == ld_clust(fs, dj.dir))
break;
4505 res = dir_next(&dj, 0);
4506 }
while (res == FR_OK);
4507 if (res == FR_NO_FILE) res = FR_INT_ERR;
4508 if (res != FR_OK)
break;
4509 get_fileinfo(&dj, &fno);
4510 for (nl = 0; fno.fname[nl]; nl++) ;
4512 res = FR_NOT_ENOUGH_CORE;
break;
4514 while (nl) buff[--i] = fno.fname[--nl];
4518 if (i == len) buff[--i] =
'/';
4521#if FF_STR_VOLUME_ID >= 1
4522 for (nl = 0, vid = (
const char*)VolumeStr[CurrVol]; vid[nl]; nl++) ;
4524 if (FF_STR_VOLUME_ID == 2) *tp++ =
'/';
4525 for (vl = 0; vl < nl; *tp++ = vid[vl], vl++) ;
4526 if (FF_STR_VOLUME_ID == 1) *tp++ =
':';
4531 *tp++ =
'0' + CurrVol;
4536 if (vl == 0) res = FR_NOT_ENOUGH_CORE;
4558#if FF_FS_MINIMIZE <= 2
4575 res = validate(&fp->obj, &fs);
4576 if (res == FR_OK) res = (FRESULT)fp->err;
4577#if FF_FS_EXFAT && !FF_FS_READONLY
4578 if (res == FR_OK && fs->fs_type == FS_EXFAT) {
4579 res = fill_last_frag(&fp->obj, fp->clust, 0xFFFFFFFF);
4582 if (res != FR_OK) LEAVE_FF(fs, res);
4586 DWORD cl, pcl, ncl, tcl, tlen, ulen;
4590 if (ofs == CREATE_LINKMAP) {
4592 tlen = *tbl++; ulen = 2;
4593 cl = fp->obj.sclust;
4597 tcl = cl; ncl = 0; ulen += 2;
4600 cl = get_fat(&fp->obj, cl);
4601 if (cl <= 1) ABORT(fs, FR_INT_ERR);
4602 if (cl == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR);
4603 }
while (cl == pcl + 1);
4605 *tbl++ = ncl; *tbl++ = tcl;
4607 }
while (cl < fs->n_fatent);
4613 res = FR_NOT_ENOUGH_CORE;
4616 if (ofs > fp->obj.objsize) ofs = fp->obj.objsize;
4619 fp->clust = clmt_clust(fp, ofs - 1);
4620 dsc = clst2sect(fs, fp->clust);
4621 if (dsc == 0) ABORT(fs, FR_INT_ERR);
4622 dsc += (DWORD)((ofs - 1) / SS(fs)) & (fs->csize - 1);
4623 if (fp->fptr % SS(fs) && dsc != fp->sect) {
4626 if (fp->flag & FA_DIRTY) {
4627 if (disk_write(fs->pdrv, fp->buf, fp->sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR);
4628 fp->flag &= (BYTE)~FA_DIRTY;
4631 if (disk_read(fs->pdrv, fp->buf, dsc, 1) != RES_OK) ABORT(fs, FR_DISK_ERR);
4643 if (fs->fs_type != FS_EXFAT && ofs >= 0x100000000) ofs = 0xFFFFFFFF;
4645 if (ofs > fp->obj.objsize && (FF_FS_READONLY || !(fp->flag & FA_WRITE))) {
4646 ofs = fp->obj.objsize;
4649 fp->fptr = nsect = 0;
4651 bcs = (DWORD)fs->csize * SS(fs);
4653 (ofs - 1) / bcs >= (ifptr - 1) / bcs) {
4654 fp->fptr = (ifptr - 1) & ~(FSIZE_t)(bcs - 1);
4658 clst = fp->obj.sclust;
4661 clst = create_chain(&fp->obj, 0);
4662 if (clst == 1) ABORT(fs, FR_INT_ERR);
4663 if (clst == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR);
4664 fp->obj.sclust = clst;
4671 ofs -= bcs; fp->fptr += bcs;
4673 if (fp->flag & FA_WRITE) {
4674 if (FF_FS_EXFAT && fp->fptr > fp->obj.objsize) {
4675 fp->obj.objsize = fp->fptr;
4676 fp->flag |= FA_MODIFIED;
4678 clst = create_chain(&fp->obj, clst);
4685 clst = get_fat(&fp->obj, clst);
4687 if (clst == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR);
4688 if (clst <= 1 || clst >= fs->n_fatent) ABORT(fs, FR_INT_ERR);
4693 nsect = clst2sect(fs, clst);
4694 if (nsect == 0) ABORT(fs, FR_INT_ERR);
4695 nsect += (DWORD)(ofs / SS(fs));
4699 if (!FF_FS_READONLY && fp->fptr > fp->obj.objsize) {
4700 fp->obj.objsize = fp->fptr;
4701 fp->flag |= FA_MODIFIED;
4703 if (fp->fptr % SS(fs) && nsect != fp->sect) {
4706 if (fp->flag & FA_DIRTY) {
4707 if (disk_write(fs->pdrv, fp->buf, fp->sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR);
4708 fp->flag &= (BYTE)~FA_DIRTY;
4711 if (disk_read(fs->pdrv, fp->buf, nsect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR);
4722#if FF_FS_MINIMIZE <= 1
4737 if (!dp)
return FR_INVALID_OBJECT;
4739 res = mount_volume(&path, &fs, 0);
4743 res = follow_path(dp, path);
4745 if (!(dp->fn[NSFLAG] & NS_NONAME)) {
4746 if (dp->obj.attr & AM_DIR) {
4748 if (fs->fs_type == FS_EXFAT) {
4749 init_alloc_info(&dp->obj, dp);
4753 dp->obj.sclust = ld_clust(fs, dp->dir);
4760 dp->obj.id = fs->id;
4761 res = dir_sdi(dp, 0);
4764 if (dp->obj.sclust) {
4765 dp->obj.lockid = inc_share(dp, 0);
4766 if (!dp->obj.lockid) res = FR_TOO_MANY_OPEN_FILES;
4775 if (res == FR_NO_FILE) res = FR_NO_PATH;
4777 if (res != FR_OK) dp->obj.fs = 0;
4797 res = validate(&dp->obj, &fs);
4800 if (dp->obj.lockid) res = dec_share(dp->obj.lockid);
4801 if (res == FR_OK) dp->obj.fs = 0;
4806 unlock_volume(fs, FR_OK);
4829 res = validate(&dp->obj, &fs);
4832 res = dir_sdi(dp, 0);
4836 res = DIR_READ_FILE(dp);
4837 if (res == FR_NO_FILE) res = FR_OK;
4839 get_fileinfo(dp, fno);
4840 res = dir_next(dp, 0);
4841 if (res == FR_NO_FILE) res = FR_OK;
4847 if (fno && res != FR_OK) fno->fname[0] = 0;
4867 res = f_readdir(dp, fno);
4868 if (res != FR_OK || !fno || !fno->fname[0])
break;
4869 if (pattern_match(dp->pat, fno->fname, 0, FIND_RECURS))
break;
4870#if FF_USE_LFN && FF_USE_FIND == 2
4871 if (pattern_match(dp->pat, fno->altname, 0, FIND_RECURS))
break;
4883FRESULT f_findfirst (
4887 const TCHAR* pattern
4894 res = f_opendir(dp, path);
4896 res = f_findnext(dp, fno);
4905#if FF_FS_MINIMIZE == 0
4921 res = mount_volume(&path, &dj.obj.fs, 0);
4924 INIT_NAMEBUFF(dj.obj.fs);
4925 res = follow_path(&dj, path);
4927 if (dj.fn[NSFLAG] & NS_NONAME) {
4928 res = FR_INVALID_NAME;
4930 if (fno) get_fileinfo(&dj, fno);
4936 if (fno && res != FR_OK) fno->fname[0] = 0;
4937 LEAVE_FF(dj.obj.fs, res);
4955 DWORD nfree, clst, stat;
4962 res = mount_volume(&path, &fs, 0);
4967 if (fs->free_clst <= fs->n_fatent - 2) {
4968 *nclst = fs->free_clst;
4972 if (fs->fs_type == FS_FAT12) {
4973 clst = 2; obj.fs = fs;
4975 stat = get_fat(&obj, clst);
4976 if (stat == 0xFFFFFFFF) {
4977 res = FR_DISK_ERR;
break;
4980 res = FR_INT_ERR;
break;
4982 if (stat == 0) nfree++;
4983 }
while (++clst < fs->n_fatent);
4986 if (fs->fs_type == FS_EXFAT) {
4990 clst = fs->n_fatent - 2;
4995 res = move_window(fs, sect++);
4996 if (res != FR_OK)
break;
4998 for (b = 8, bm = ~fs->win[i]; b && clst; b--, clst--) {
5002 i = (i + 1) % SS(fs);
5007 clst = fs->n_fatent;
5012 res = move_window(fs, sect++);
5013 if (res != FR_OK)
break;
5015 if (fs->fs_type == FS_FAT16) {
5016 if (ld_16(fs->win + i) == 0) nfree++;
5019 if ((ld_32(fs->win + i) & 0x0FFFFFFF) == 0) nfree++;
5028 fs->free_clst = nfree;
5054 res = validate(&fp->obj, &fs);
5055 if (res != FR_OK || (res = (FRESULT)fp->err) != FR_OK) LEAVE_FF(fs, res);
5056 if (!(fp->flag & FA_WRITE)) LEAVE_FF(fs, FR_DENIED);
5058 if (fp->fptr < fp->obj.objsize) {
5059 if (fp->fptr == 0) {
5060 res = remove_chain(&fp->obj, fp->obj.sclust, 0);
5063 ncl = get_fat(&fp->obj, fp->clust);
5065 if (ncl == 0xFFFFFFFF) res = FR_DISK_ERR;
5066 if (ncl == 1) res = FR_INT_ERR;
5067 if (res == FR_OK && ncl < fs->n_fatent) {
5068 res = remove_chain(&fp->obj, ncl, fp->clust);
5071 fp->obj.objsize = fp->fptr;
5072 fp->flag |= FA_MODIFIED;
5074 if (res == FR_OK && (fp->flag & FA_DIRTY)) {
5075 if (disk_write(fs->pdrv, fp->buf, fp->sect, 1) != RES_OK) {
5078 fp->flag &= (BYTE)~FA_DIRTY;
5082 if (res != FR_OK) ABORT(fs, res);
5109 res = mount_volume(&path, &fs, FA_WRITE);
5113 res = follow_path(&dj, path);
5115 if (dj.fn[NSFLAG] & (NS_DOT | NS_NONAME)) {
5116 res = FR_INVALID_NAME;
5117 }
else if (dj.obj.attr & AM_RDO) {
5121 res = chk_share(&dj, 2);
5128 if (fs->fs_type == FS_EXFAT) {
5129 init_alloc_info(&obj, 0);
5134 dclst = ld_clust(fs, dj.dir);
5136 if (dj.obj.attr & AM_DIR) {
5138 if (dclst == fs->cdir) {
5144 sdj.obj.sclust = dclst;
5146 if (fs->fs_type == FS_EXFAT) {
5147 sdj.obj.objsize = obj.objsize;
5148 sdj.obj.stat = obj.stat;
5151 res = dir_sdi(&sdj, 0);
5153 res = DIR_READ_FILE(&sdj);
5154 if (res == FR_OK) res = FR_DENIED;
5155 if (res == FR_NO_FILE) res = FR_OK;
5161 res = dir_remove(&dj);
5162 if (res == FR_OK && dclst != 0) {
5164 res = remove_chain(&obj, dclst, 0);
5166 res = remove_chain(&dj.obj, dclst, 0);
5169 if (res == FR_OK) res = sync_fs(fs);
5196 res = mount_volume(&path, &fs, FA_WRITE);
5200 res = follow_path(&dj, path);
5202 res = (dj.fn[NSFLAG] & (NS_DOT | NS_NONAME)) ? FR_INVALID_NAME : FR_EXIST;
5204 if (res == FR_NO_FILE) {
5206 dcl = create_chain(&sobj, 0);
5208 if (dcl == 0) res = FR_DENIED;
5209 if (dcl == 1) res = FR_INT_ERR;
5210 if (dcl == 0xFFFFFFFF) res = FR_DISK_ERR;
5213 res = dir_clear(fs, dcl);
5215 if (!FF_FS_EXFAT || fs->fs_type != FS_EXFAT) {
5216 memset(fs->win + DIR_Name,
' ', 11);
5217 fs->win[DIR_Name] =
'.';
5218 fs->win[DIR_Attr] = AM_DIR;
5219 st_32(fs->win + DIR_ModTime, tm);
5220 st_clust(fs, fs->win, dcl);
5221 memcpy(fs->win + SZDIRE, fs->win, SZDIRE);
5222 fs->win[SZDIRE + 1] =
'.'; pcl = dj.obj.sclust;
5223 st_clust(fs, fs->win + SZDIRE, pcl);
5226 res = dir_register(&dj);
5231 if (fs->fs_type == FS_EXFAT) {
5232 st_32(fs->dirbuf + XDIR_CrtTime, tm);
5233 st_32(fs->dirbuf + XDIR_ModTime, tm);
5234 st_32(fs->dirbuf + XDIR_FstClus, dcl);
5235 st_32(fs->dirbuf + XDIR_FileSize, (DWORD)fs->csize * SS(fs));
5236 st_32(fs->dirbuf + XDIR_ValidFileSize, (DWORD)fs->csize * SS(fs));
5237 fs->dirbuf[XDIR_GenFlags] = 3;
5238 fs->dirbuf[XDIR_Attr] = AM_DIR;
5239 res = store_xdir(&dj);
5243 st_32(dj.dir + DIR_CrtTime, tm);
5244 st_32(dj.dir + DIR_ModTime, tm);
5245 st_clust(fs, dj.dir, dcl);
5246 dj.dir[DIR_Attr] = AM_DIR;
5253 remove_chain(&sobj, dcl, 0);
5270 const TCHAR* path_old,
5271 const TCHAR* path_new
5277 BYTE buf[FF_FS_EXFAT ? SZDIRE * 2 : SZDIRE], *dir;
5281 get_ldnumber(&path_new);
5282 res = mount_volume(&path_old, &fs, FA_WRITE);
5286 res = follow_path(&djo, path_old);
5288 if (djo.fn[NSFLAG] & (NS_DOT | NS_NONAME)) {
5289 res = FR_INVALID_NAME;
5292 res = chk_share(&djo, 2);
5298 if (fs->fs_type == FS_EXFAT) {
5301 DWORD dscl = ld_32(fs->dirbuf + XDIR_FstClus);
5303 for (i = 1; i <= fs->xcwds.depth && dscl != fs->xcwds.tbl[i].d_scl; i++) ;
5304 if (i <= fs->xcwds.depth) {
5309 memcpy(buf, fs->dirbuf, SZDIRE * 2);
5310 memcpy(&djn, &djo,
sizeof djn);
5311 res = follow_path(&djn, path_new);
5314 res = (djn.obj.sclust == djo.obj.sclust && djn.dptr == djo.dptr) ? FR_NO_FILE : FR_EXIST;
5316 if (res == FR_NO_FILE) {
5317 res = dir_register(&djn);
5322 nf = fs->dirbuf[XDIR_NumSec]; nn = fs->dirbuf[XDIR_NumName];
5323 nh = ld_16(fs->dirbuf + XDIR_NameHash);
5324 memcpy(fs->dirbuf, buf, SZDIRE * 2);
5325 fs->dirbuf[XDIR_NumSec] = nf; fs->dirbuf[XDIR_NumName] = nn;
5326 st_16(fs->dirbuf + XDIR_NameHash, nh);
5327 if (!(fs->dirbuf[XDIR_Attr] & AM_DIR)) fs->dirbuf[XDIR_Attr] |= AM_ARC;
5329 res = store_xdir(&djn);
5335 memcpy(buf, djo.dir, SZDIRE);
5336 memcpy(&djn, &djo,
sizeof djn);
5337 res = follow_path(&djn, path_new);
5339 res = (djn.obj.sclust == djo.obj.sclust && djn.dptr == djo.dptr) ? FR_NO_FILE : FR_EXIST;
5341 if (res == FR_NO_FILE) {
5342 res = dir_register(&djn);
5345 memcpy(dir + 13, buf + 13, SZDIRE - 13);
5346 dir[DIR_Attr] = buf[DIR_Attr];
5347 if (!(dir[DIR_Attr] & AM_DIR)) dir[DIR_Attr] |= AM_ARC;
5349 if ((dir[DIR_Attr] & AM_DIR) && djo.obj.sclust != djn.obj.sclust) {
5350 LBA_t sect = clst2sect(fs, ld_clust(fs, dir));
5356 res = move_window(fs, sect);
5357 dir = fs->win + SZDIRE * 1;
5358 if (res == FR_OK && dir[1] ==
'.') {
5359 st_clust(fs, dir, djn.obj.sclust);
5368 res = dir_remove(&djo);
5388#if FF_USE_CHMOD && !FF_FS_READONLY
5406 res = mount_volume(&path, &fs, FA_WRITE);
5411 res = follow_path(&dj, path);
5412 if (res == FR_OK && (dj.fn[NSFLAG] & (NS_DOT | NS_NONAME))) res = FR_INVALID_NAME;
5414 mask &= AM_RDO|AM_HID|AM_SYS|AM_ARC;
5416 if (fs->fs_type == FS_EXFAT) {
5417 fs->dirbuf[XDIR_Attr] = (attr & mask) | (fs->dirbuf[XDIR_Attr] & (BYTE)~mask);
5418 res = store_xdir(&dj);
5422 dj.dir[DIR_Attr] = (attr & mask) | (dj.dir[DIR_Attr] & (BYTE)~mask);
5454 res = mount_volume(&path, &fs, FA_WRITE);
5459 res = follow_path(&dj, path);
5460 if (res == FR_OK && (dj.fn[NSFLAG] & (NS_DOT | NS_NONAME))) res = FR_INVALID_NAME;
5463 if (fs->fs_type == FS_EXFAT) {
5465 st_32(fs->dirbuf + XDIR_ModTime, (DWORD)fno->fdate << 16 | fno->ftime);
5466 fs->dirbuf[XDIR_ModTime10] = 0;
5467 fs->dirbuf[XDIR_ModTZ] = 0;
5471 st_32(fs->dirbuf + XDIR_CrtTime, (DWORD)fno->crdate << 16 | fno->crtime);
5472 fs->dirbuf[XDIR_CrtTime10] = 0;
5473 fs->dirbuf[XDIR_CrtTZ] = 0;
5476 res = store_xdir(&dj);
5481 st_32(dj.dir + DIR_ModTime, (DWORD)fno->fdate << 16 | fno->ftime);
5485 st_32(dj.dir + DIR_CrtTime, (DWORD)fno->crdate << 16 | fno->crtime);
5486 dj.dir[DIR_CrtTime10] = 0;
5523 res = mount_volume(&path, &fs, 0);
5526 if (res == FR_OK && label) {
5527 dj.obj.fs = fs; dj.obj.sclust = 0;
5528 res = dir_sdi(&dj, 0);
5530 res = DIR_READ_LABEL(&dj);
5533 if (fs->fs_type == FS_EXFAT) {
5537 for (si = di = hs = 0; si < dj.dir[XDIR_NumLabel]; si++) {
5538 wc = ld_16(dj.dir + XDIR_Label + si * 2);
5539 if (hs == 0 && IsSurrogate(wc)) {
5542 nw = put_utf((DWORD)hs << 16 | wc, &label[di], 4);
5549 if (hs != 0) di = 0;
5557#if FF_USE_LFN && FF_LFN_UNICODE >= 1
5558 if (dbc_1st((BYTE)wc) && si < 11) wc = wc << 8 | dj.dir[si++];
5559 wc = ff_oem2uni(wc, CODEPAGE);
5563 di += put_utf(wc, &label[di], 4);
5565 label[di++] = (TCHAR)wc;
5571 }
while (label[--di] ==
' ');
5575 if (res == FR_NO_FILE) {
5582 if (res == FR_OK && vsn) {
5583 res = move_window(fs, fs->volbase);
5585 switch (fs->fs_type) {
5595 di = fs->win[BS_BootSig] == 0x29 ? BS_VolID : 0;
5597 *vsn = di ? ld_32(fs->win + di) : 0;
5621 static const char badchr[18] =
"+.,;=[]" "/*:<>|\\\"\?\x7F";
5627 res = mount_volume(&label, &fs, FA_WRITE);
5628 if (res != FR_OK) LEAVE_FF(fs, res);
5629#if FF_STR_VOLUME_ID == 2
5630 for ( ; *label ==
'/'; label++) ;
5634 if (fs->fs_type == FS_EXFAT) {
5635 memset(dirvn, 0, 22);
5637 while ((UINT)*label >=
' ') {
5638 dc = tchar2uni(&label);
5639 if (dc >= 0x10000) {
5640 if (dc == 0xFFFFFFFF || di >= 10) {
5643 st_16(dirvn + di * 2, (WCHAR)(dc >> 16)); di++;
5646 if (dc == 0 || strchr(&badchr[7], (
int)dc) || di >= 11) {
5647 LEAVE_FF(fs, FR_INVALID_NAME);
5649 st_16(dirvn + di * 2, (WCHAR)dc); di++;
5654 memset(dirvn,
' ', 11);
5656 while ((UINT)*label >=
' ') {
5658 dc = tchar2uni(&label);
5659 wc = (dc < 0x10000) ? ff_uni2oem(ff_wtoupper(dc), CODEPAGE) : 0;
5661 wc = (BYTE)*label++;
5662 if (dbc_1st((BYTE)wc)) wc = dbc_2nd((BYTE)*label) ? wc << 8 | (BYTE)*label++ : 0;
5663 if (IsLower(wc)) wc -= 0x20;
5664#if FF_CODE_PAGE == 0
5665 if (ExCvt && wc >= 0x80) wc = ExCvt[wc - 0x80];
5666#elif FF_CODE_PAGE < 900
5667 if (wc >= 0x80) wc = ExCvt[wc - 0x80];
5670 if (wc == 0 || strchr(&badchr[0], (
int)wc) || di >= (UINT)((wc >= 0x100) ? 10 : 11)) {
5671 LEAVE_FF(fs, FR_INVALID_NAME);
5673 if (wc >= 0x100) dirvn[di++] = (BYTE)(wc >> 8);
5674 dirvn[di++] = (BYTE)wc;
5676 if (dirvn[0] == DDEM) LEAVE_FF(fs, FR_INVALID_NAME);
5677 while (di && dirvn[di - 1] ==
' ') di--;
5681 dj.obj.fs = fs; dj.obj.sclust = 0;
5682 res = dir_sdi(&dj, 0);
5684 res = DIR_READ_LABEL(&dj);
5686 if (FF_FS_EXFAT && fs->fs_type == FS_EXFAT) {
5687 dj.dir[XDIR_NumLabel] = (BYTE)di;
5688 memcpy(dj.dir + XDIR_Label, dirvn, 22);
5691 memcpy(dj.dir, dirvn, 11);
5693 dj.dir[DIR_Name] = DDEM;
5699 if (res == FR_NO_FILE) {
5702 res = dir_alloc(&dj, 1);
5704 memset(dj.dir, 0, SZDIRE);
5705 if (FF_FS_EXFAT && fs->fs_type == FS_EXFAT) {
5706 dj.dir[XDIR_Type] = ET_VLABEL;
5707 dj.dir[XDIR_NumLabel] = (BYTE)di;
5708 memcpy(dj.dir + XDIR_Label, dirvn, 22);
5710 dj.dir[DIR_Attr] = AM_VOL;
5711 memcpy(dj.dir, dirvn, 11);
5729#if FF_USE_EXPAND && !FF_FS_READONLY
5742 DWORD n, clst, stcl, scl, ncl, tcl, lclst;
5745 res = validate(&fp->obj, &fs);
5746 if (res != FR_OK || (res = (FRESULT)fp->err) != FR_OK) LEAVE_FF(fs, res);
5747 if (fsz == 0 || fp->obj.objsize != 0 || !(fp->flag & FA_WRITE)) LEAVE_FF(fs, FR_DENIED);
5749 if (fs->fs_type != FS_EXFAT && fsz >= 0x100000000) LEAVE_FF(fs, FR_DENIED);
5751 n = (DWORD)fs->csize * SS(fs);
5752 tcl = (DWORD)(fsz / n) + ((fsz & (n - 1)) ? 1 : 0);
5753 stcl = fs->last_clst; lclst = 0;
5754 if (stcl < 2 || stcl >= fs->n_fatent) stcl = 2;
5757 if (fs->fs_type == FS_EXFAT) {
5758 scl = find_bitmap(fs, stcl, tcl);
5759 if (scl == 0) res = FR_DENIED;
5760 if (scl == 0xFFFFFFFF) res = FR_DISK_ERR;
5763 res = change_bitmap(fs, scl, tcl, 1);
5764 lclst = scl + tcl - 1;
5772 scl = clst = stcl; ncl = 0;
5774 n = get_fat(&fp->obj, clst);
5775 if (++clst >= fs->n_fatent) clst = 2;
5777 res = FR_INT_ERR;
break;
5779 if (n == 0xFFFFFFFF) {
5780 res = FR_DISK_ERR;
break;
5783 if (++ncl == tcl)
break;
5785 scl = clst; ncl = 0;
5788 res = FR_DENIED;
break;
5793 for (clst = scl, n = tcl; n; clst++, n--) {
5794 res = put_fat(fs, clst, (n == 1) ? 0xFFFFFFFF : clst + 1);
5795 if (res != FR_OK)
break;
5805 fs->last_clst = lclst;
5807 fp->obj.sclust = scl;
5808 fp->obj.objsize = fsz;
5809 if (FF_FS_EXFAT) fp->obj.stat = 2;
5810 fp->flag |= FA_MODIFIED;
5811 if (fs->free_clst <= fs->n_fatent - 2) {
5812 fs->free_clst -= tcl;
5832 UINT (*func)(
const BYTE*,UINT),
5847 res = validate(&fp->obj, &fs);
5848 if (res != FR_OK || (res = (FRESULT)fp->err) != FR_OK) LEAVE_FF(fs, res);
5849 if (!(fp->flag & FA_READ)) LEAVE_FF(fs, FR_DENIED);
5851 remain = fp->obj.objsize - fp->fptr;
5852 if (btf > remain) btf = (UINT)remain;
5854 for ( ; btf > 0 && (*func)(0, 0); fp->fptr += rcnt, *bf += rcnt, btf -= rcnt) {
5855 csect = (UINT)(fp->fptr / SS(fs) & (fs->csize - 1));
5856 if (fp->fptr % SS(fs) == 0) {
5858 clst = (fp->fptr == 0) ?
5859 fp->obj.sclust : get_fat(&fp->obj, fp->clust);
5860 if (clst <= 1) ABORT(fs, FR_INT_ERR);
5861 if (clst == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR);
5865 sect = clst2sect(fs, fp->clust);
5866 if (sect == 0) ABORT(fs, FR_INT_ERR);
5869 if (move_window(fs, sect) != FR_OK) ABORT(fs, FR_DISK_ERR);
5872 if (fp->sect != sect) {
5874 if (fp->flag & FA_DIRTY) {
5875 if (disk_write(fs->pdrv, fp->buf, fp->sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR);
5876 fp->flag &= (BYTE)~FA_DIRTY;
5879 if (disk_read(fs->pdrv, fp->buf, sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR);
5884 rcnt = SS(fs) - (UINT)fp->fptr % SS(fs);
5885 if (rcnt > btf) rcnt = btf;
5886 rcnt = (*func)(dbuf + ((UINT)fp->fptr % SS(fs)), rcnt);
5887 if (rcnt == 0) ABORT(fs, FR_INT_ERR);
5890 LEAVE_FF(fs, FR_OK);
5896#if !FF_FS_READONLY && FF_USE_MKFS
5901#define N_SEC_TRACK 63
5902#define GPT_ALIGN 0x100000
5903#define GPT_ITEMS 128
5908static FRESULT create_partition (
5917 DWORD sz_drv32, nxt_alloc32, sz_part32;
5919 BYTE hd, n_hd, sc, n_sc;
5922 if (disk_ioctl(drv, GET_SECTOR_COUNT, &sz_drv) != RES_OK)
return FR_DISK_ERR;
5925 if (sz_drv >= FF_MIN_GPT) {
5927 UINT sz_ptbl, pi, si, ofs;
5928 DWORD bcc, rnd, align;
5929 QWORD nxt_alloc, sz_part, sz_pool, top_bpt;
5930 static const BYTE gpt_mbr[16] = {0x00, 0x00, 0x02, 0x00, 0xEE, 0xFE, 0xFF, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF};
5932#if FF_MAX_SS != FF_MIN_SS
5933 if (disk_ioctl(drv, GET_SECTOR_SIZE, &ss) != RES_OK)
return FR_DISK_ERR;
5934 if (ss > FF_MAX_SS || ss < FF_MIN_SS || (ss & (ss - 1)))
return FR_DISK_ERR;
5938 rnd = (DWORD)sz_drv + GET_FATTIME();
5939 align = GPT_ALIGN / ss;
5940 sz_ptbl = GPT_ITEMS * SZ_GPTE / ss;
5941 top_bpt = sz_drv - sz_ptbl - 1;
5942 nxt_alloc = 2 + sz_ptbl;
5943 sz_pool = top_bpt - nxt_alloc;
5944 bcc = 0xFFFFFFFF; sz_part = 1;
5947 if (pi * SZ_GPTE % ss == 0) memset(buf, 0, ss);
5949 nxt_alloc = (nxt_alloc + align - 1) & ((
QWORD)0 - align);
5950 sz_part = plst[si++];
5951 if (sz_part <= 100) {
5952 sz_part = sz_pool * sz_part / 100;
5953 sz_part = (sz_part + align - 1) & ((
QWORD)0 - align);
5955 if (nxt_alloc + sz_part > top_bpt) {
5956 sz_part = (nxt_alloc < top_bpt) ? top_bpt - nxt_alloc : 0;
5960 ofs = pi * SZ_GPTE % ss;
5961 memcpy(buf + ofs + GPTE_PtGuid, GUID_MS_Basic, 16);
5962 rnd = make_rand(rnd, buf + ofs + GPTE_UpGuid, 16);
5963 st_64(buf + ofs + GPTE_FstLba, nxt_alloc);
5964 st_64(buf + ofs + GPTE_LstLba, nxt_alloc + sz_part - 1);
5965 nxt_alloc += sz_part;
5967 if ((pi + 1) * SZ_GPTE % ss == 0) {
5968 for (i = 0; i < ss; bcc = crc32(bcc, buf[i++])) ;
5969 if (disk_write(drv, buf, 2 + pi * SZ_GPTE / ss, 1) != RES_OK)
return FR_DISK_ERR;
5970 if (disk_write(drv, buf, top_bpt + pi * SZ_GPTE / ss, 1) != RES_OK)
return FR_DISK_ERR;
5972 }
while (++pi < GPT_ITEMS);
5976 memcpy(buf + GPTH_Sign,
"EFI PART" "\0\0\1\0" "\x5C\0\0", 16);
5977 st_32(buf + GPTH_PtBcc, ~bcc);
5978 st_64(buf + GPTH_CurLba, 1);
5979 st_64(buf + GPTH_BakLba, sz_drv - 1);
5980 st_64(buf + GPTH_FstLba, 2 + sz_ptbl);
5981 st_64(buf + GPTH_LstLba, top_bpt - 1);
5982 st_32(buf + GPTH_PteSize, SZ_GPTE);
5983 st_32(buf + GPTH_PtNum, GPT_ITEMS);
5984 st_32(buf + GPTH_PtOfs, 2);
5985 rnd = make_rand(rnd, buf + GPTH_DskGuid, 16);
5986 for (i = 0, bcc= 0xFFFFFFFF; i < 92; bcc = crc32(bcc, buf[i++])) ;
5987 st_32(buf + GPTH_Bcc, ~bcc);
5988 if (disk_write(drv, buf, 1, 1) != RES_OK)
return FR_DISK_ERR;
5991 st_64(buf + GPTH_CurLba, sz_drv - 1);
5992 st_64(buf + GPTH_BakLba, 1);
5993 st_64(buf + GPTH_PtOfs, top_bpt);
5994 st_32(buf + GPTH_Bcc, 0);
5995 for (i = 0, bcc= 0xFFFFFFFF; i < 92; bcc = crc32(bcc, buf[i++])) ;
5996 st_32(buf + GPTH_Bcc, ~bcc);
5997 if (disk_write(drv, buf, sz_drv - 1, 1) != RES_OK)
return FR_DISK_ERR;
6001 memcpy(buf + MBR_Table, gpt_mbr, 16);
6002 st_16(buf + BS_55AA, 0xAA55);
6003 if (disk_write(drv, buf, 0, 1) != RES_OK)
return FR_DISK_ERR;
6008 sz_drv32 = (DWORD)sz_drv;
6010 for (n_hd = 8; n_hd != 0 && sz_drv32 / n_hd / n_sc > 1024; n_hd *= 2) ;
6011 if (n_hd == 0) n_hd = 255;
6013 memset(buf, 0, FF_MAX_SS);
6014 pte = buf + MBR_Table;
6015 for (i = 0, nxt_alloc32 = n_sc; i < 4 && nxt_alloc32 != 0 && nxt_alloc32 < sz_drv32; i++, nxt_alloc32 += sz_part32) {
6016 sz_part32 = (DWORD)plst[i];
6017 if (sz_part32 <= 100) sz_part32 = (sz_part32 == 100) ? sz_drv32 : sz_drv32 / 100 * sz_part32;
6018 if (nxt_alloc32 + sz_part32 > sz_drv32 || nxt_alloc32 + sz_part32 < nxt_alloc32) sz_part32 = sz_drv32 - nxt_alloc32;
6019 if (sz_part32 == 0)
break;
6021 st_32(pte + PTE_StLba, nxt_alloc32);
6022 st_32(pte + PTE_SizLba, sz_part32);
6023 pte[PTE_System] = sys;
6025 cy = (UINT)(nxt_alloc32 / n_sc / n_hd);
6026 hd = (BYTE)(nxt_alloc32 / n_sc % n_hd);
6027 sc = (BYTE)(nxt_alloc32 % n_sc + 1);
6028 pte[PTE_StHead] = hd;
6029 pte[PTE_StSec] = (BYTE)((cy >> 2 & 0xC0) | sc);
6030 pte[PTE_StCyl] = (BYTE)cy;
6032 cy = (UINT)((nxt_alloc32 + sz_part32 - 1) / n_sc / n_hd);
6033 hd = (BYTE)((nxt_alloc32 + sz_part32 - 1) / n_sc % n_hd);
6034 sc = (BYTE)((nxt_alloc32 + sz_part32 - 1) % n_sc + 1);
6035 pte[PTE_EdHead] = hd;
6036 pte[PTE_EdSec] = (BYTE)((cy >> 2 & 0xC0) | sc);
6037 pte[PTE_EdCyl] = (BYTE)cy;
6042 st_16(buf + BS_55AA, 0xAA55);
6043 if (disk_write(drv, buf, 0, 1) != RES_OK)
return FR_DISK_ERR;
6058 static const WORD cst[] = {1, 4, 16, 64, 256, 512, 0};
6059 static const WORD cst32[] = {1, 2, 4, 8, 16, 32, 0};
6060 static const MKFS_PARM defopt = {FM_ANY, 0, 0, 0, 0};
6061 BYTE fsopt, fsty, sys, pdrv, ipart;
6065 DWORD sz_buf, sz_blk, n_clst, pau, nsect, n, vsn;
6066 LBA_t sz_vol, b_vol, b_fat, b_data;
6068 DWORD sz_rsv, sz_fat, sz_dir, sz_au;
6069 UINT n_fat, n_root, i;
6076 vol = get_ldnumber(&path);
6077 if (vol < 0)
return FR_INVALID_DRIVE;
6078 if (FatFs[vol]) FatFs[vol]->fs_type = 0;
6083 ds = disk_initialize(pdrv);
6084 if (ds & STA_NOINIT)
return FR_NOT_READY;
6085 if (ds & STA_PROTECT)
return FR_WRITE_PROTECTED;
6088 if (!opt) opt = &defopt;
6089 sz_blk = opt->align;
6090 if (sz_blk == 0) disk_ioctl(pdrv, GET_BLOCK_SIZE, &sz_blk);
6091 if (sz_blk == 0 || sz_blk > 0x8000 || (sz_blk & (sz_blk - 1))) sz_blk = 1;
6092#if FF_MAX_SS != FF_MIN_SS
6093 if (disk_ioctl(pdrv, GET_SECTOR_SIZE, &ss) != RES_OK)
return FR_DISK_ERR;
6094 if (ss > FF_MAX_SS || ss < FF_MIN_SS || (ss & (ss - 1)))
return FR_DISK_ERR;
6100 fsopt = opt->fmt & (FM_ANY | FM_SFD);
6101 n_fat = (opt->n_fat >= 1 && opt->n_fat <= 2) ? opt->n_fat : 1;
6102 n_root = (opt->n_root >= 1 && opt->n_root <= 32768 && (opt->n_root % (ss / SZDIRE)) == 0) ? opt->n_root : 512;
6103 sz_au = (opt->au_size <= 0x1000000 && (opt->au_size & (opt->au_size - 1)) == 0) ? opt->au_size : 0;
6108 if (sz_buf == 0)
return FR_NOT_ENOUGH_CORE;
6111 if (!buf) buf = ff_memalloc(sz_buf * ss);
6113 if (!buf)
return FR_NOT_ENOUGH_CORE;
6117 if (FF_MULTI_PARTITION && ipart != 0) {
6119 if (disk_read(pdrv, buf, 0, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);
6120 if (ld_16(buf + BS_55AA) != 0xAA55) LEAVE_MKFS(FR_MKFS_ABORTED);
6122 if (buf[MBR_Table + PTE_System] == 0xEE) {
6127 if (disk_read(pdrv, buf, 1, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);
6128 if (!test_gpt_header(buf)) LEAVE_MKFS(FR_MKFS_ABORTED);
6129 n_ent = ld_32(buf + GPTH_PtNum);
6130 pt_lba = ld_64(buf + GPTH_PtOfs);
6133 if (ofs == 0 && disk_read(pdrv, buf, pt_lba++, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);
6134 if (!memcmp(buf + ofs + GPTE_PtGuid, GUID_MS_Basic, 16) && ++i == ipart) {
6135 b_vol = ld_64(buf + ofs + GPTE_FstLba);
6136 sz_vol = ld_64(buf + ofs + GPTE_LstLba) - b_vol + 1;
6139 n_ent--; ofs = (ofs + SZ_GPTE) % ss;
6141 if (n_ent == 0) LEAVE_MKFS(FR_MKFS_ABORTED);
6146 pte = buf + (MBR_Table + (ipart - 1) * SZ_PTE);
6147 if (ipart > 4 || pte[PTE_System] == 0) LEAVE_MKFS(FR_MKFS_ABORTED);
6148 b_vol = ld_32(pte + PTE_StLba);
6149 sz_vol = ld_32(pte + PTE_SizLba);
6152 if (disk_ioctl(pdrv, GET_SECTOR_COUNT, &sz_vol) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);
6153 if (!(fsopt & FM_SFD)) {
6156 if (sz_vol >= FF_MIN_GPT) {
6158 b_vol = GPT_ALIGN / ss; sz_vol -= b_vol + GPT_ITEMS * SZ_GPTE / ss + 1;
6162 if (sz_vol > N_SEC_TRACK) {
6163 b_vol = N_SEC_TRACK; sz_vol -= b_vol;
6168 if (sz_vol < 128) LEAVE_MKFS(FR_MKFS_ABORTED);
6173 if (FF_FS_EXFAT && (fsopt & FM_EXFAT)) {
6174 if ((fsopt & FM_ANY) == FM_EXFAT || sz_vol >= 0x4000000 || sz_au > 128) {
6175 fsty = FS_EXFAT;
break;
6179 if (sz_vol >= 0x100000000) LEAVE_MKFS(FR_MKFS_ABORTED);
6181 if (sz_au > 128) sz_au = 128;
6182 if (fsopt & FM_FAT32) {
6183 if (!(fsopt & FM_FAT)) {
6184 fsty = FS_FAT32;
break;
6187 if (!(fsopt & FM_FAT)) LEAVE_MKFS(FR_INVALID_PARAMETER);
6191 vsn = (DWORD)sz_vol + GET_FATTIME();
6194 if (fsty == FS_EXFAT) {
6195 DWORD szb_bit, szb_case, sum, nbit, clu, clen[3];
6199 if (sz_vol < 0x1000) LEAVE_MKFS(FR_MKFS_ABORTED);
6201 lba[0] = b_vol; lba[1] = b_vol + sz_vol - 1;
6202 disk_ioctl(pdrv, CTRL_TRIM, lba);
6207 if (sz_vol >= 0x80000) sz_au = 64;
6208 if (sz_vol >= 0x4000000) sz_au = 256;
6211 sz_fat = (DWORD)((sz_vol / sz_au + 2) * 4 + ss - 1) / ss;
6212 b_data = (b_fat + sz_fat + sz_blk - 1) & ~((LBA_t)sz_blk - 1);
6213 if (b_data - b_vol >= sz_vol / 2) LEAVE_MKFS(FR_MKFS_ABORTED);
6214 n_clst = (DWORD)((sz_vol - (b_data - b_vol)) / sz_au);
6215 if (n_clst <16) LEAVE_MKFS(FR_MKFS_ABORTED);
6216 if (n_clst > MAX_EXFAT) LEAVE_MKFS(FR_MKFS_ABORTED);
6218 szb_bit = (n_clst + 7) / 8;
6219 clen[0] = (szb_bit + sz_au * ss - 1) / (sz_au * ss);
6222 sect = b_data + sz_au * clen[0];
6224 st = 0; si = 0; i = 0; j = 0; szb_case = 0;
6228 ch = (WCHAR)ff_wtoupper(si);
6232 for (j = 1; (WCHAR)(si + j) && (WCHAR)(si + j) == ff_wtoupper((WCHAR)(si + j)); j++) ;
6234 ch = 0xFFFF; st = 2;
break;
6240 if (--j == 0) st = 0;
6244 ch = (WCHAR)j; si += (WCHAR)j;
6247 sum = xsum32(buf[i + 0] = (BYTE)ch, sum);
6248 sum = xsum32(buf[i + 1] = (BYTE)(ch >> 8), sum);
6249 i += 2; szb_case += 2;
6250 if (si == 0 || i == sz_buf * ss) {
6251 n = (i + ss - 1) / ss;
6252 if (disk_write(pdrv, buf, sect, n) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);
6256 clen[1] = (szb_case + sz_au * ss - 1) / (sz_au * ss);
6260 sect = b_data; nsect = (szb_bit + ss - 1) / ss;
6261 nbit = clen[0] + clen[1] + clen[2];
6263 memset(buf, 0, sz_buf * ss);
6264 for (i = 0; nbit != 0 && i / 8 < sz_buf * ss; buf[i / 8] |= 1 << (i % 8), i++, nbit--) ;
6265 n = (nsect > sz_buf) ? sz_buf : nsect;
6266 if (disk_write(pdrv, buf, sect, n) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);
6267 sect += n; nsect -= n;
6271 sect = b_fat; nsect = sz_fat;
6274 memset(buf, 0, sz_buf * ss); i = 0;
6276 st_32(buf + i, 0xFFFFFFF8); i += 4; clu++;
6277 st_32(buf + i, 0xFFFFFFFF); i += 4; clu++;
6281 while (nbit != 0 && i < sz_buf * ss) {
6282 st_32(buf + i, (nbit > 1) ? clu + 1 : 0xFFFFFFFF);
6283 i += 4; clu++; nbit--;
6285 if (nbit == 0 && j < 3) nbit = clen[j++];
6286 }
while (nbit != 0 && i < sz_buf * ss);
6287 n = (nsect > sz_buf) ? sz_buf : nsect;
6288 if (disk_write(pdrv, buf, sect, n) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);
6289 sect += n; nsect -= n;
6293 memset(buf, 0, sz_buf * ss);
6294 buf[SZDIRE * 0 + 0] = ET_VLABEL;
6295 buf[SZDIRE * 1 + 0] = ET_BITMAP;
6296 st_32(buf + SZDIRE * 1 + 20, 2);
6297 st_32(buf + SZDIRE * 1 + 24, szb_bit);
6298 buf[SZDIRE * 2 + 0] = ET_UPCASE;
6299 st_32(buf + SZDIRE * 2 + 4, sum);
6300 st_32(buf + SZDIRE * 2 + 20, 2 + clen[0]);
6301 st_32(buf + SZDIRE * 2 + 24, szb_case);
6302 sect = b_data + sz_au * (clen[0] + clen[1]); nsect = sz_au;
6304 n = (nsect > sz_buf) ? sz_buf : nsect;
6305 if (disk_write(pdrv, buf, sect, n) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);
6307 sect += n; nsect -= n;
6312 for (n = 0; n < 2; n++) {
6315 memcpy(buf + BS_JmpBoot,
"\xEB\x76\x90" "EXFAT ", 11);
6316 st_64(buf + BPB_VolOfsEx, b_vol);
6317 st_64(buf + BPB_TotSecEx, sz_vol);
6318 st_32(buf + BPB_FatOfsEx, (DWORD)(b_fat - b_vol));
6319 st_32(buf + BPB_FatSzEx, sz_fat);
6320 st_32(buf + BPB_DataOfsEx, (DWORD)(b_data - b_vol));
6321 st_32(buf + BPB_NumClusEx, n_clst);
6322 st_32(buf + BPB_RootClusEx, 2 + clen[0] + clen[1]);
6323 st_32(buf + BPB_VolIDEx, vsn);
6324 st_16(buf + BPB_FSVerEx, 0x100);
6325 for (buf[BPB_BytsPerSecEx] = 0, i = ss; i >>= 1; buf[BPB_BytsPerSecEx]++) ;
6326 for (buf[BPB_SecPerClusEx] = 0, i = sz_au; i >>= 1; buf[BPB_SecPerClusEx]++) ;
6327 buf[BPB_NumFATsEx] = 1;
6328 buf[BPB_DrvNumEx] = 0x80;
6329 st_16(buf + BS_BootCodeEx, 0xFEEB);
6330 st_16(buf + BS_55AA, 0xAA55);
6331 for (i = sum = 0; i < ss; i++) {
6332 if (i != BPB_VolFlagEx && i != BPB_VolFlagEx + 1 && i != BPB_PercInUseEx) sum = xsum32(buf[i], sum);
6334 if (disk_write(pdrv, buf, sect++, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);
6337 st_16(buf + ss - 2, 0xAA55);
6338 for (j = 1; j < 9; j++) {
6339 for (i = 0; i < ss; sum = xsum32(buf[i++], sum)) ;
6340 if (disk_write(pdrv, buf, sect++, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);
6344 for ( ; j < 11; j++) {
6345 for (i = 0; i < ss; sum = xsum32(buf[i++], sum)) ;
6346 if (disk_write(pdrv, buf, sect++, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);
6349 for (i = 0; i < ss; i += 4) st_32(buf + i, sum);
6350 if (disk_write(pdrv, buf, sect++, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);
6359 if (fsty == FS_FAT32) {
6361 n = (DWORD)sz_vol / 0x20000;
6362 for (i = 0, pau = 1; cst32[i] && cst32[i] <= n; i++, pau <<= 1) ;
6364 n_clst = (DWORD)sz_vol / pau;
6365 sz_fat = (n_clst * 4 + 8 + ss - 1) / ss;
6368 if (n_clst <= MAX_FAT16 || n_clst > MAX_FAT32) LEAVE_MKFS(FR_MKFS_ABORTED);
6371 n = (DWORD)sz_vol / 0x1000;
6372 for (i = 0, pau = 1; cst[i] && cst[i] <= n; i++, pau <<= 1) ;
6374 n_clst = (DWORD)sz_vol / pau;
6375 if (n_clst > MAX_FAT12) {
6379 n = (n_clst * 3 + 1) / 2 + 3;
6381 sz_fat = (n + ss - 1) / ss;
6383 sz_dir = (DWORD)n_root * SZDIRE / ss;
6385 b_fat = b_vol + sz_rsv;
6386 b_data = b_fat + sz_fat * n_fat + sz_dir;
6389 n = (DWORD)(((b_data + sz_blk - 1) & ~(sz_blk - 1)) - b_data);
6390 if (fsty == FS_FAT32) {
6391 sz_rsv += n; b_fat += n;
6394 n--; sz_rsv++; b_fat++;
6396 sz_fat += n / n_fat;
6400 if (sz_vol < b_data + pau * 16 - b_vol) LEAVE_MKFS(FR_MKFS_ABORTED);
6401 n_clst = ((DWORD)sz_vol - sz_rsv - sz_fat * n_fat - sz_dir) / pau;
6402 if (fsty == FS_FAT32) {
6403 if (n_clst <= MAX_FAT16) {
6404 if (sz_au == 0 && (sz_au = pau / 2) != 0)
continue;
6405 LEAVE_MKFS(FR_MKFS_ABORTED);
6408 if (fsty == FS_FAT16) {
6409 if (n_clst > MAX_FAT16) {
6410 if (sz_au == 0 && (pau * 2) <= 64) {
6411 sz_au = pau * 2;
continue;
6413 if ((fsopt & FM_FAT32)) {
6414 fsty = FS_FAT32;
continue;
6416 if (sz_au == 0 && (sz_au = pau * 2) <= 128)
continue;
6417 LEAVE_MKFS(FR_MKFS_ABORTED);
6419 if (n_clst <= MAX_FAT12) {
6420 if (sz_au == 0 && (sz_au = pau * 2) <= 128)
continue;
6421 LEAVE_MKFS(FR_MKFS_ABORTED);
6424 if (fsty == FS_FAT12 && n_clst > MAX_FAT12) LEAVE_MKFS(FR_MKFS_ABORTED);
6431 lba[0] = b_vol; lba[1] = b_vol + sz_vol - 1;
6432 disk_ioctl(pdrv, CTRL_TRIM, lba);
6436 memcpy(buf + BS_JmpBoot,
"\xEB\xFE\x90" "MSDOS5.0", 11);
6437 st_16(buf + BPB_BytsPerSec, ss);
6438 buf[BPB_SecPerClus] = (BYTE)pau;
6439 st_16(buf + BPB_RsvdSecCnt, (WORD)sz_rsv);
6440 buf[BPB_NumFATs] = (BYTE)n_fat;
6441 st_16(buf + BPB_RootEntCnt, (WORD)((fsty == FS_FAT32) ? 0 : n_root));
6442 if (sz_vol < 0x10000) {
6443 st_16(buf + BPB_TotSec16, (WORD)sz_vol);
6445 st_32(buf + BPB_TotSec32, (DWORD)sz_vol);
6447 buf[BPB_Media] = 0xF8;
6448 st_16(buf + BPB_SecPerTrk, 63);
6449 st_16(buf + BPB_NumHeads, 255);
6450 st_32(buf + BPB_HiddSec, (DWORD)b_vol);
6451 if (fsty == FS_FAT32) {
6452 st_32(buf + BS_VolID32, vsn);
6453 st_32(buf + BPB_FATSz32, sz_fat);
6454 st_32(buf + BPB_RootClus32, 2);
6455 st_16(buf + BPB_FSInfo32, 1);
6456 st_16(buf + BPB_BkBootSec32, 6);
6457 buf[BS_DrvNum32] = 0x80;
6458 buf[BS_BootSig32] = 0x29;
6459 memcpy(buf + BS_VolLab32,
"NO NAME " "FAT32 ", 19);
6461 st_32(buf + BS_VolID, vsn);
6462 st_16(buf + BPB_FATSz16, (WORD)sz_fat);
6463 buf[BS_DrvNum] = 0x80;
6464 buf[BS_BootSig] = 0x29;
6465 memcpy(buf + BS_VolLab,
"NO NAME " "FAT ", 19);
6467 st_16(buf + BS_55AA, 0xAA55);
6468 if (disk_write(pdrv, buf, b_vol, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);
6471 if (fsty == FS_FAT32) {
6472 disk_write(pdrv, buf, b_vol + 6, 1);
6474 st_32(buf + FSI_LeadSig, 0x41615252);
6475 st_32(buf + FSI_StrucSig, 0x61417272);
6476 st_32(buf + FSI_Free_Count, n_clst - 1);
6477 st_32(buf + FSI_Nxt_Free, 2);
6478 st_16(buf + BS_55AA, 0xAA55);
6479 disk_write(pdrv, buf, b_vol + 7, 1);
6480 disk_write(pdrv, buf, b_vol + 1, 1);
6484 memset(buf, 0, sz_buf * ss);
6486 for (i = 0; i < n_fat; i++) {
6487 if (fsty == FS_FAT32) {
6488 st_32(buf + 0, 0xFFFFFFF8);
6489 st_32(buf + 4, 0xFFFFFFFF);
6490 st_32(buf + 8, 0x0FFFFFFF);
6492 st_32(buf + 0, (fsty == FS_FAT12) ? 0xFFFFF8 : 0xFFFFFFF8);
6496 n = (nsect > sz_buf) ? sz_buf : nsect;
6497 if (disk_write(pdrv, buf, sect, (UINT)n) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);
6499 sect += n; nsect -= n;
6504 nsect = (fsty == FS_FAT32) ? pau : sz_dir;
6506 n = (nsect > sz_buf) ? sz_buf : nsect;
6507 if (disk_write(pdrv, buf, sect, (UINT)n) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);
6508 sect += n; nsect -= n;
6515 if (FF_FS_EXFAT && fsty == FS_EXFAT) {
6517 }
else if (fsty == FS_FAT32) {
6519 }
else if (sz_vol >= 0x10000) {
6521 }
else if (fsty == FS_FAT16) {
6528 if (FF_MULTI_PARTITION && ipart != 0) {
6529 if (!FF_LBA64 || !(fsopt & 0x80)) {
6531 if (disk_read(pdrv, buf, 0, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);
6532 buf[MBR_Table + (ipart - 1) * SZ_PTE + PTE_System] = sys;
6533 if (disk_write(pdrv, buf, 0, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);
6536 if (!(fsopt & FM_SFD)) {
6537 lba[0] = sz_vol; lba[1] = 0;
6538 res = create_partition(pdrv, lba, sys, buf);
6539 if (res != FR_OK) LEAVE_MKFS(res);
6543 if (disk_ioctl(pdrv, CTRL_SYNC, 0) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);
6551#if FF_MULTI_PARTITION
6562 BYTE *buf = (BYTE*)work;
6568 stat = disk_initialize(pdrv);
6569 if (stat & STA_NOINIT)
return FR_NOT_READY;
6570 if (stat & STA_PROTECT)
return FR_WRITE_PROTECTED;
6573 if (!buf) buf = ff_memalloc(FF_MAX_SS);
6575 if (!buf)
return FR_NOT_ENOUGH_CORE;
6577 res = create_partition(pdrv, ptbl, 0x07, buf);
6589#if FF_USE_LFN && FF_LFN_UNICODE && (FF_STRF_ENCODE < 0 || FF_STRF_ENCODE > 3)
6590#error Wrong FF_STRF_ENCODE setting
6607#if FF_USE_LFN && FF_LFN_UNICODE && FF_STRF_ENCODE <= 2
6610#if FF_USE_LFN && FF_LFN_UNICODE && FF_STRF_ENCODE == 3
6614#if FF_USE_LFN && FF_LFN_UNICODE
6616 if (FF_LFN_UNICODE == 1) len -= (FF_STRF_ENCODE == 0) ? 1 : 2;
6617 if (FF_LFN_UNICODE == 2) len -= (FF_STRF_ENCODE == 0) ? 3 : 4;
6618 if (FF_LFN_UNICODE == 3) len -= 1;
6620#if FF_STRF_ENCODE == 0
6621 f_read(fp, s, 1, &rc);
6624 if (dbc_1st((BYTE)wc)) {
6625 f_read(fp, s, 1, &rc);
6626 if (rc != 1 || !dbc_2nd(s[0]))
continue;
6627 wc = wc << 8 | s[0];
6629 dc = ff_oem2uni(wc, CODEPAGE);
6630 if (dc == 0)
continue;
6631#elif FF_STRF_ENCODE == 1 || FF_STRF_ENCODE == 2
6632 f_read(fp, s, 2, &rc);
6634 dc = (FF_STRF_ENCODE == 1) ? ld_16(s) : s[0] << 8 | s[1];
6635 if (IsSurrogateL(dc))
continue;
6636 if (IsSurrogateH(dc)) {
6637 f_read(fp, s, 2, &rc);
6639 wc = (FF_STRF_ENCODE == 1) ? ld_16(s) : s[0] << 8 | s[1];
6640 if (!IsSurrogateL(wc))
continue;
6641 dc = ((dc & 0x3FF) + 0x40) << 10 | (wc & 0x3FF);
6644 f_read(fp, s, 1, &rc);
6649 if ((dc & 0xE0) == 0xC0) {
6652 if ((dc & 0xF0) == 0xE0) {
6655 if ((dc & 0xF8) == 0xF0) {
6658 if (ct == 0)
continue;
6659 f_read(fp, s, ct, &rc);
6660 if (rc != ct)
break;
6663 if ((s[rc] & 0xC0) != 0x80)
break;
6664 dc = dc << 6 | (s[rc] & 0x3F);
6665 }
while (++rc < ct);
6666 if (rc != ct || dc < 0x80 || IsSurrogate(dc) || dc >= 0x110000)
continue;
6671 if (FF_USE_STRFUNC == 2 && dc ==
'\r')
continue;
6672#if FF_LFN_UNICODE == 1 || FF_LFN_UNICODE == 3
6673 if (FF_LFN_UNICODE == 1 && dc >= 0x10000) {
6674 *p++ = (TCHAR)(0xD800 | ((dc >> 10) - 0x40)); nc++;
6675 dc = 0xDC00 | (dc & 0x3FF);
6677 *p++ = (TCHAR)dc; nc++;
6678 if (dc ==
'\n')
break;
6679#elif FF_LFN_UNICODE == 2
6683 if (dc ==
'\n')
break;
6684 }
else if (dc < 0x800) {
6685 *p++ = (TCHAR)(0xC0 | (dc >> 6 & 0x1F));
6686 *p++ = (TCHAR)(0x80 | (dc >> 0 & 0x3F));
6688 }
else if (dc < 0x10000) {
6689 *p++ = (TCHAR)(0xE0 | (dc >> 12 & 0x0F));
6690 *p++ = (TCHAR)(0x80 | (dc >> 6 & 0x3F));
6691 *p++ = (TCHAR)(0x80 | (dc >> 0 & 0x3F));
6694 *p++ = (TCHAR)(0xF0 | (dc >> 18 & 0x07));
6695 *p++ = (TCHAR)(0x80 | (dc >> 12 & 0x3F));
6696 *p++ = (TCHAR)(0x80 | (dc >> 6 & 0x3F));
6697 *p++ = (TCHAR)(0x80 | (dc >> 0 & 0x3F));
6706 f_read(fp, s, 1, &rc);
6709 if (FF_USE_STRFUNC == 2 && dc ==
'\r')
continue;
6710 *p++ = (TCHAR)dc; nc++;
6711 if (dc ==
'\n')
break;
6716 return nc ? buff : 0;
6724#define SZ_PUTC_BUF 64
6725#define SZ_NUM_BUF 32
6736#if FF_USE_LFN && FF_LFN_UNICODE == 1
6738#elif FF_USE_LFN && FF_LFN_UNICODE == 2
6742 BYTE buf[SZ_PUTC_BUF];
6748static void putc_bfd (putbuff* pb, TCHAR c)
6752#if FF_USE_LFN && FF_LFN_UNICODE
6754#if FF_LFN_UNICODE == 2
6760 if (FF_USE_STRFUNC == 2 && c ==
'\n') {
6768#if FF_USE_LFN && FF_LFN_UNICODE
6769#if FF_LFN_UNICODE == 1
6770 if (IsSurrogateH(c)) {
6773 hs = pb->hs; pb->hs = 0;
6775 if (!IsSurrogateL(c)) hs = 0;
6777 if (IsSurrogateL(c))
return;
6780#elif FF_LFN_UNICODE == 2
6783 pb->bs[pb->wi = 0] = (BYTE)c;
6784 if ((BYTE)c < 0x80)
break;
6785 if (((BYTE)c & 0xE0) == 0xC0) pb->ct = 1;
6786 if (((BYTE)c & 0xF0) == 0xE0) pb->ct = 2;
6787 if (((BYTE)c & 0xF8) == 0xF0) pb->ct = 3;
6790 if (((BYTE)c & 0xC0) != 0x80) {
6791 pb->ct = 0;
continue;
6793 pb->bs[++pb->wi] = (BYTE)c;
6794 if (--pb->ct == 0)
break;
6798 tp = (
const TCHAR*)pb->bs;
6799 dc = tchar2uni(&tp);
6800 if (dc == 0xFFFFFFFF)
return;
6801 hs = (WCHAR)(dc >> 16);
6803#elif FF_LFN_UNICODE == 3
6804 if (IsSurrogate(c) || c >= 0x110000)
return;
6806 hs = (WCHAR)(0xD800 | ((c >> 10) - 0x40));
6807 wc = 0xDC00 | (c & 0x3FF);
6815#if FF_STRF_ENCODE == 1
6817 st_16(&pb->buf[i], hs);
6821 st_16(&pb->buf[i], wc);
6823#elif FF_STRF_ENCODE == 2
6825 pb->buf[i++] = (BYTE)(hs >> 8);
6826 pb->buf[i++] = (BYTE)hs;
6829 pb->buf[i++] = (BYTE)(wc >> 8);
6830 pb->buf[i++] = (BYTE)wc;
6831#elif FF_STRF_ENCODE == 3
6834 hs = (hs & 0x3FF) + 0x40;
6835 pb->buf[i++] = (BYTE)(0xF0 | hs >> 8);
6836 pb->buf[i++] = (BYTE)(0x80 | (hs >> 2 & 0x3F));
6837 pb->buf[i++] = (BYTE)(0x80 | (hs & 3) << 4 | (wc >> 6 & 0x0F));
6838 pb->buf[i++] = (BYTE)(0x80 | (wc & 0x3F));
6841 pb->buf[i++] = (BYTE)wc;
6845 pb->buf[i++] = (BYTE)(0xC0 | wc >> 6);
6848 pb->buf[i++] = (BYTE)(0xE0 | wc >> 12);
6849 pb->buf[i++] = (BYTE)(0x80 | (wc >> 6 & 0x3F));
6851 pb->buf[i++] = (BYTE)(0x80 | (wc & 0x3F));
6855 if (hs != 0)
return;
6856 wc = ff_uni2oem(wc, CODEPAGE);
6857 if (wc == 0)
return;
6859 pb->buf[i++] = (BYTE)(wc >> 8); nc++;
6861 pb->buf[i++] = (BYTE)wc;
6865 pb->buf[i++] = (BYTE)c;
6868 if (i >= (
int)(
sizeof pb->buf) - 4) {
6869 f_write(pb->fp, pb->buf, (UINT)i, &n);
6870 i = (n == (UINT)i) ? 0 : -1;
6879static int putc_flush (putbuff* pb)
6884 && f_write(pb->fp, pb->buf, (UINT)pb->idx, &nw) == FR_OK
6885 && (UINT)pb->idx == nw) {
6894static void putc_init (putbuff* pb,
FIL* fp)
6896 memset(pb, 0,
sizeof (putbuff));
6912 return putc_flush(&pb);
6931 while (*str) putc_bfd(&pb, *str++);
6932 return putc_flush(&pb);
6941#if FF_PRINT_FLOAT && FF_INTDEF == 2
6944static int ilog10 (
double n)
6950 n /= 100000; rv += 5;
6957 n *= 100000; rv -= 5;
6966static double i10x (
int n)
6972 rv *= 100000; n -= 5;
6979 rv /= 100000; n += 5;
6996 int exp = 0, mag = 0;
7000 const char ds = FF_PRINT_FLOAT == 2 ?
',' :
'.';
7006 if (prec < 0) prec = 6;
7008 val = 0 - val; sign =
'-';
7016 val += i10x(0 - prec) / 2;
7018 if (mag < 0) mag = 0;
7019 if (mag + prec + 3 >= SZ_NUM_BUF) er =
"OV";
7022 val += i10x(ilog10(val) - prec) / 2;
7024 if (exp > 99 || prec + 7 >= SZ_NUM_BUF) {
7027 if (exp < -99) exp = -99;
7034 if (sign ==
'-') *buf++ = sign;
7036 if (mag == -1) *buf++ = ds;
7038 digit = (int)(val / w); val -= digit * w;
7039 *buf++ = (char)(
'0' + digit);
7040 }
while (--mag >= -prec);
7044 exp = 0 - exp; *buf++ =
'-';
7048 *buf++ = (char)(
'0' + exp / 10);
7049 *buf++ = (char)(
'0' + exp % 10);
7054 if (sign) *buf++ = sign;
7073 UINT i, j, width, flag, radix;
7075#if FF_PRINT_LLI && FF_INTDEF == 2
7083 char digit, str[SZ_NUM_BUF];
7092 if (chr == 0)
break;
7097 flag = width = 0; pad =
' '; prec = -1;
7100 pad =
'0'; chr = *fmt++;
7101 }
else if (chr ==
'-') {
7102 flag = 2; chr = *fmt++;
7105 width = (UINT)va_arg(arp,
int);
7108 while (IsDigit(chr)) {
7109 width = width * 10 + chr -
'0';
7116 prec = va_arg(arp,
int);
7120 while (IsDigit(chr)) {
7121 prec = prec * 10 + chr -
'0';
7127 flag |= 4; chr = *fmt++;
7128#if FF_PRINT_LLI && FF_INTDEF == 2
7130 flag |= 8; chr = *fmt++;
7134 if (chr == 0)
break;
7151 putc_bfd(&pb, (TCHAR)va_arg(arp,
int));
7155 tp = va_arg(arp, TCHAR*);
7157 for (j = 0; tp[j]; j++) ;
7158 if (prec >= 0 && j > (UINT)prec) j = (UINT)prec;
7159 for ( ; !(flag & 2) && j < width; j++) putc_bfd(&pb, pad);
7160 while (*tp && prec--) putc_bfd(&pb, *tp++);
7161 while (j++ < width) putc_bfd(&pb,
' ');
7163#if FF_PRINT_FLOAT && FF_INTDEF == 2
7167 ftoa(str, va_arg(arp,
double), prec, chr);
7168 for (j = strlen(str); !(flag & 2) && j < width; j++) putc_bfd(&pb, pad);
7169 for (i = 0; str[i]; putc_bfd(&pb, str[i++])) ;
7170 while (j++ < width) putc_bfd(&pb,
' ');
7179#if FF_PRINT_LLI && FF_INTDEF == 2
7181 val = (
QWORD)va_arg(arp,
long long);
7182 }
else if (flag & 4) {
7183 val = (chr ==
'd') ? (
QWORD)(
long long)va_arg(arp,
long) : (
QWORD)va_arg(arp,
unsigned long);
7185 val = (chr ==
'd') ? (
QWORD)(
long long)va_arg(arp,
int) : (
QWORD)va_arg(arp,
unsigned int);
7187 if (chr ==
'd' && (val & 0x8000000000000000)) {
7188 val = 0 - val; flag |= 1;
7192 val = (DWORD)va_arg(arp,
long);
7194 val = (chr ==
'd') ? (DWORD)(long)va_arg(arp,
int) : (DWORD)va_arg(arp,
unsigned int);
7196 if (chr ==
'd' && (val & 0x80000000)) {
7197 val = 0 - val; flag |= 1;
7202 digit = (char)(val % radix) +
'0'; val /= radix;
7203 if (digit >
'9') digit += (chr ==
'x') ? 0x27 : 0x07;
7205 }
while (val && i < SZ_NUM_BUF);
7206 if (flag & 1) str[i++] =
'-';
7208 for (j = i; !(flag & 2) && j < width; j++) {
7212 putc_bfd(&pb, (TCHAR)str[--i]);
7214 while (j++ < width) {
7221 return putc_flush(&pb);
7229#if FF_CODE_PAGE == 0
7238 static const WORD validcp[22] = { 437, 720, 737, 771, 775, 850, 852, 855, 857, 860, 861, 862, 863, 864, 865, 866, 869, 932, 936, 949, 950, 0};
7239 static const BYTE *
const tables[22] = {Ct437, Ct720, Ct737, Ct771, Ct775, Ct850, Ct852, Ct855, Ct857, Ct860, Ct861, Ct862, Ct863, Ct864, Ct865, Ct866, Ct869, Dc932, Dc936, Dc949, Dc950, 0};
7243 for (i = 0; validcp[i] != 0 && validcp[i] != cp; i++) ;
7244 if (validcp[i] != cp)
return FR_INVALID_PARAMETER;