]> cloudbase.mooo.com Git - z180-stamp.git/blame - fatfs/src/ff.c
Import fatfs R0.12b
[z180-stamp.git] / fatfs / src / ff.c
CommitLineData
53668523 1/*----------------------------------------------------------------------------/\r
70702af1 2/ FatFs - Generic FAT file system module R0.12b /\r
53668523 3/-----------------------------------------------------------------------------/\r
53668523 4/\r
70702af1 5/ Copyright (C) 2016, ChaN, all right reserved.\r
53668523 6/\r
70702af1
L
7/ FatFs module is an open source software. Redistribution and use of FatFs in\r
8/ source and binary forms, with or without modification, are permitted provided\r
9/ that the following condition is met:\r
53668523 10\r
70702af1
L
11/ 1. Redistributions of source code must retain the above copyright notice,\r
12/ this condition and the following disclaimer.\r
13/\r
14/ This software is provided by the copyright holder and contributors "AS IS"\r
15/ and any warranties related to this software are DISCLAIMED.\r
16/ The copyright owner or contributors be NOT LIABLE for any damages caused\r
17/ by use of this software.\r
18/----------------------------------------------------------------------------*/\r
53668523
L
19\r
20\r
70702af1
L
21#include "ff.h" /* Declarations of FatFs API */\r
22#include "diskio.h" /* Declarations of device I/O functions */\r
53668523
L
23\r
24\r
25/*--------------------------------------------------------------------------\r
26\r
27 Module Private Definitions\r
28\r
29---------------------------------------------------------------------------*/\r
30\r
70702af1 31#if _FATFS != 68020 /* Revision ID */\r
53668523
L
32#error Wrong include file (ff.h).\r
33#endif\r
34\r
35\r
70702af1
L
36#define ABORT(fs, res) { fp->err = (BYTE)(res); LEAVE_FF(fs, res); }\r
37\r
38\r
53668523
L
39/* Reentrancy related */\r
40#if _FS_REENTRANT\r
41#if _USE_LFN == 1\r
7b78a5a2 42#error Static LFN work area cannot be used at thread-safe configuration\r
53668523
L
43#endif\r
44#define ENTER_FF(fs) { if (!lock_fs(fs)) return FR_TIMEOUT; }\r
45#define LEAVE_FF(fs, res) { unlock_fs(fs, res); return res; }\r
46#else\r
47#define ENTER_FF(fs)\r
48#define LEAVE_FF(fs, res) return res\r
49#endif\r
50\r
53668523
L
51\r
52\r
53/* Definitions of sector size */\r
54#if (_MAX_SS < _MIN_SS) || (_MAX_SS != 512 && _MAX_SS != 1024 && _MAX_SS != 2048 && _MAX_SS != 4096) || (_MIN_SS != 512 && _MIN_SS != 1024 && _MIN_SS != 2048 && _MIN_SS != 4096)\r
7b78a5a2 55#error Wrong sector size configuration\r
53668523
L
56#endif\r
57#if _MAX_SS == _MIN_SS\r
58#define SS(fs) ((UINT)_MAX_SS) /* Fixed sector size */\r
59#else\r
60#define SS(fs) ((fs)->ssize) /* Variable sector size */\r
61#endif\r
62\r
63\r
70702af1
L
64/* Timestamp */\r
65#if _FS_NORTC == 1\r
66#if _NORTC_YEAR < 1980 || _NORTC_YEAR > 2107 || _NORTC_MON < 1 || _NORTC_MON > 12 || _NORTC_MDAY < 1 || _NORTC_MDAY > 31\r
67#error Invalid _FS_NORTC settings\r
68#endif\r
69#define GET_FATTIME() ((DWORD)(_NORTC_YEAR - 1980) << 25 | (DWORD)_NORTC_MON << 21 | (DWORD)_NORTC_MDAY << 16)\r
7b78a5a2
L
70#else\r
71#define GET_FATTIME() get_fattime()\r
72#endif\r
73\r
74\r
70702af1
L
75/* File lock controls */\r
76#if _FS_LOCK != 0\r
53668523 77#if _FS_READONLY\r
7b78a5a2 78#error _FS_LOCK must be 0 at read-only configuration\r
53668523
L
79#endif\r
80typedef struct {\r
81 FATFS *fs; /* Object ID 1, volume (NULL:blank entry) */\r
82 DWORD clu; /* Object ID 2, directory (0:root) */\r
70702af1 83 DWORD ofs; /* Object ID 3, directory offset */\r
53668523
L
84 WORD ctr; /* Object open counter, 0:none, 0x01..0xFF:read mode open count, 0x100:write mode */\r
85} FILESEM;\r
86#endif\r
87\r
88\r
89\r
70702af1 90/* DBCS code ranges and SBCS upper conversion tables */\r
53668523
L
91\r
92#if _CODE_PAGE == 932 /* Japanese Shift-JIS */\r
93#define _DF1S 0x81 /* DBC 1st byte range 1 start */\r
94#define _DF1E 0x9F /* DBC 1st byte range 1 end */\r
95#define _DF2S 0xE0 /* DBC 1st byte range 2 start */\r
96#define _DF2E 0xFC /* DBC 1st byte range 2 end */\r
97#define _DS1S 0x40 /* DBC 2nd byte range 1 start */\r
98#define _DS1E 0x7E /* DBC 2nd byte range 1 end */\r
99#define _DS2S 0x80 /* DBC 2nd byte range 2 start */\r
100#define _DS2E 0xFC /* DBC 2nd byte range 2 end */\r
101\r
102#elif _CODE_PAGE == 936 /* Simplified Chinese GBK */\r
103#define _DF1S 0x81\r
104#define _DF1E 0xFE\r
105#define _DS1S 0x40\r
106#define _DS1E 0x7E\r
107#define _DS2S 0x80\r
108#define _DS2E 0xFE\r
109\r
110#elif _CODE_PAGE == 949 /* Korean */\r
111#define _DF1S 0x81\r
112#define _DF1E 0xFE\r
113#define _DS1S 0x41\r
114#define _DS1E 0x5A\r
115#define _DS2S 0x61\r
116#define _DS2E 0x7A\r
117#define _DS3S 0x81\r
118#define _DS3E 0xFE\r
119\r
120#elif _CODE_PAGE == 950 /* Traditional Chinese Big5 */\r
121#define _DF1S 0x81\r
122#define _DF1E 0xFE\r
123#define _DS1S 0x40\r
124#define _DS1E 0x7E\r
125#define _DS2S 0xA1\r
126#define _DS2E 0xFE\r
127\r
70702af1 128#elif _CODE_PAGE == 437 /* U.S. */\r
53668523 129#define _DF1S 0\r
70702af1
L
130#define _EXCVT {0x80,0x9A,0x45,0x41,0x8E,0x41,0x8F,0x80,0x45,0x45,0x45,0x49,0x49,0x49,0x8E,0x8F, \\r
131 0x90,0x92,0x92,0x4F,0x99,0x4F,0x55,0x55,0x59,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \\r
132 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \\r
133 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
134 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \\r
135 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
136 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \\r
137 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}\r
138\r
139#elif _CODE_PAGE == 720 /* Arabic */\r
53668523 140#define _DF1S 0\r
70702af1
L
141#define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \\r
142 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \\r
143 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \\r
144 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
145 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \\r
146 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
147 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \\r
148 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}\r
149\r
150#elif _CODE_PAGE == 737 /* Greek */\r
53668523 151#define _DF1S 0\r
70702af1
L
152#define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \\r
153 0x90,0x92,0x92,0x93,0x94,0x95,0x96,0x97,0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87, \\r
154 0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0xAA,0x92,0x93,0x94,0x95,0x96, \\r
155 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
156 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \\r
157 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
158 0x97,0xEA,0xEB,0xEC,0xE4,0xED,0xEE,0xEF,0xF5,0xF0,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \\r
159 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}\r
160\r
161#elif _CODE_PAGE == 771 /* KBL */\r
53668523 162#define _DF1S 0\r
70702af1
L
163#define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \\r
164 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \\r
165 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \\r
166 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
167 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \\r
168 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDC,0xDE,0xDE, \\r
169 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \\r
170 0xF0,0xF0,0xF2,0xF2,0xF4,0xF4,0xF6,0xF6,0xF8,0xF8,0xFA,0xFA,0xFC,0xFC,0xFE,0xFF}\r
171\r
172#elif _CODE_PAGE == 775 /* Baltic */\r
53668523 173#define _DF1S 0\r
70702af1
L
174#define _EXCVT {0x80,0x9A,0x91,0xA0,0x8E,0x95,0x8F,0x80,0xAD,0xED,0x8A,0x8A,0xA1,0x8D,0x8E,0x8F, \\r
175 0x90,0x92,0x92,0xE2,0x99,0x95,0x96,0x97,0x97,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9F, \\r
176 0xA0,0xA1,0xE0,0xA3,0xA3,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \\r
177 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
178 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \\r
179 0xB5,0xB6,0xB7,0xB8,0xBD,0xBE,0xC6,0xC7,0xA5,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
180 0xE0,0xE1,0xE2,0xE3,0xE5,0xE5,0xE6,0xE3,0xE8,0xE8,0xEA,0xEA,0xEE,0xED,0xEE,0xEF, \\r
181 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}\r
182\r
183#elif _CODE_PAGE == 850 /* Latin 1 */\r
53668523 184#define _DF1S 0\r
70702af1
L
185#define _EXCVT {0x43,0x55,0x45,0x41,0x41,0x41,0x41,0x43,0x45,0x45,0x45,0x49,0x49,0x49,0x41,0x41, \\r
186 0x45,0x92,0x92,0x4F,0x4F,0x4F,0x55,0x55,0x59,0x4F,0x55,0x4F,0x9C,0x4F,0x9E,0x9F, \\r
187 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \\r
188 0xB0,0xB1,0xB2,0xB3,0xB4,0x41,0x41,0x41,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
189 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0x41,0x41,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \\r
190 0xD1,0xD1,0x45,0x45,0x45,0x49,0x49,0x49,0x49,0xD9,0xDA,0xDB,0xDC,0xDD,0x49,0xDF, \\r
191 0x4F,0xE1,0x4F,0x4F,0x4F,0x4F,0xE6,0xE8,0xE8,0x55,0x55,0x55,0x59,0x59,0xEE,0xEF, \\r
192 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}\r
193\r
194#elif _CODE_PAGE == 852 /* Latin 2 */\r
53668523 195#define _DF1S 0\r
70702af1
L
196#define _EXCVT {0x80,0x9A,0x90,0xB6,0x8E,0xDE,0x8F,0x80,0x9D,0xD3,0x8A,0x8A,0xD7,0x8D,0x8E,0x8F, \\r
197 0x90,0x91,0x91,0xE2,0x99,0x95,0x95,0x97,0x97,0x99,0x9A,0x9B,0x9B,0x9D,0x9E,0xAC, \\r
198 0xB5,0xD6,0xE0,0xE9,0xA4,0xA4,0xA6,0xA6,0xA8,0xA8,0xAA,0x8D,0xAC,0xB8,0xAE,0xAF, \\r
199 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBD,0xBF, \\r
200 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC6,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \\r
201 0xD1,0xD1,0xD2,0xD3,0xD2,0xD5,0xD6,0xD7,0xB7,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
202 0xE0,0xE1,0xE2,0xE3,0xE3,0xD5,0xE6,0xE6,0xE8,0xE9,0xE8,0xEB,0xED,0xED,0xDD,0xEF, \\r
203 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xEB,0xFC,0xFC,0xFE,0xFF}\r
204\r
205#elif _CODE_PAGE == 855 /* Cyrillic */\r
53668523 206#define _DF1S 0\r
70702af1
L
207#define _EXCVT {0x81,0x81,0x83,0x83,0x85,0x85,0x87,0x87,0x89,0x89,0x8B,0x8B,0x8D,0x8D,0x8F,0x8F, \\r
208 0x91,0x91,0x93,0x93,0x95,0x95,0x97,0x97,0x99,0x99,0x9B,0x9B,0x9D,0x9D,0x9F,0x9F, \\r
209 0xA1,0xA1,0xA3,0xA3,0xA5,0xA5,0xA7,0xA7,0xA9,0xA9,0xAB,0xAB,0xAD,0xAD,0xAE,0xAF, \\r
210 0xB0,0xB1,0xB2,0xB3,0xB4,0xB6,0xB6,0xB8,0xB8,0xB9,0xBA,0xBB,0xBC,0xBE,0xBE,0xBF, \\r
211 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC7,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \\r
212 0xD1,0xD1,0xD3,0xD3,0xD5,0xD5,0xD7,0xD7,0xDD,0xD9,0xDA,0xDB,0xDC,0xDD,0xE0,0xDF, \\r
213 0xE0,0xE2,0xE2,0xE4,0xE4,0xE6,0xE6,0xE8,0xE8,0xEA,0xEA,0xEC,0xEC,0xEE,0xEE,0xEF, \\r
214 0xF0,0xF2,0xF2,0xF4,0xF4,0xF6,0xF6,0xF8,0xF8,0xFA,0xFA,0xFC,0xFC,0xFD,0xFE,0xFF}\r
215\r
216#elif _CODE_PAGE == 857 /* Turkish */\r
53668523 217#define _DF1S 0\r
70702af1
L
218#define _EXCVT {0x80,0x9A,0x90,0xB6,0x8E,0xB7,0x8F,0x80,0xD2,0xD3,0xD4,0xD8,0xD7,0x49,0x8E,0x8F, \\r
219 0x90,0x92,0x92,0xE2,0x99,0xE3,0xEA,0xEB,0x98,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9E, \\r
220 0xB5,0xD6,0xE0,0xE9,0xA5,0xA5,0xA6,0xA6,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \\r
221 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
222 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC7,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \\r
223 0xD0,0xD1,0xD2,0xD3,0xD4,0x49,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
224 0xE0,0xE1,0xE2,0xE3,0xE5,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xDE,0xED,0xEE,0xEF, \\r
225 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}\r
226\r
227#elif _CODE_PAGE == 860 /* Portuguese */\r
53668523 228#define _DF1S 0\r
70702af1
L
229#define _EXCVT {0x80,0x9A,0x90,0x8F,0x8E,0x91,0x86,0x80,0x89,0x89,0x92,0x8B,0x8C,0x98,0x8E,0x8F, \\r
230 0x90,0x91,0x92,0x8C,0x99,0xA9,0x96,0x9D,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \\r
231 0x86,0x8B,0x9F,0x96,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \\r
232 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
233 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \\r
234 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
235 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \\r
236 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}\r
237\r
238#elif _CODE_PAGE == 861 /* Icelandic */\r
53668523 239#define _DF1S 0\r
70702af1
L
240#define _EXCVT {0x80,0x9A,0x90,0x41,0x8E,0x41,0x8F,0x80,0x45,0x45,0x45,0x8B,0x8B,0x8D,0x8E,0x8F, \\r
241 0x90,0x92,0x92,0x4F,0x99,0x8D,0x55,0x97,0x97,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9F, \\r
242 0xA4,0xA5,0xA6,0xA7,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \\r
243 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
244 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \\r
245 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
246 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \\r
247 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}\r
248\r
249#elif _CODE_PAGE == 862 /* Hebrew */\r
53668523 250#define _DF1S 0\r
70702af1
L
251#define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \\r
252 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \\r
253 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \\r
254 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
255 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \\r
256 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
257 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \\r
258 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}\r
259\r
260#elif _CODE_PAGE == 863 /* Canadian-French */\r
53668523 261#define _DF1S 0\r
70702af1
L
262#define _EXCVT {0x43,0x55,0x45,0x41,0x41,0x41,0x86,0x43,0x45,0x45,0x45,0x49,0x49,0x8D,0x41,0x8F, \\r
263 0x45,0x45,0x45,0x4F,0x45,0x49,0x55,0x55,0x98,0x4F,0x55,0x9B,0x9C,0x55,0x55,0x9F, \\r
264 0xA0,0xA1,0x4F,0x55,0xA4,0xA5,0xA6,0xA7,0x49,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \\r
265 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
266 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \\r
267 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
268 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \\r
269 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}\r
270\r
271#elif _CODE_PAGE == 864 /* Arabic */\r
53668523 272#define _DF1S 0\r
70702af1
L
273#define _EXCVT {0x80,0x9A,0x45,0x41,0x8E,0x41,0x8F,0x80,0x45,0x45,0x45,0x49,0x49,0x49,0x8E,0x8F, \\r
274 0x90,0x92,0x92,0x4F,0x99,0x4F,0x55,0x55,0x59,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \\r
275 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \\r
276 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
277 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \\r
278 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
279 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \\r
280 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}\r
281\r
282#elif _CODE_PAGE == 865 /* Nordic */\r
53668523 283#define _DF1S 0\r
70702af1
L
284#define _EXCVT {0x80,0x9A,0x90,0x41,0x8E,0x41,0x8F,0x80,0x45,0x45,0x45,0x49,0x49,0x49,0x8E,0x8F, \\r
285 0x90,0x92,0x92,0x4F,0x99,0x4F,0x55,0x55,0x59,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \\r
286 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \\r
287 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
288 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \\r
289 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
290 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \\r
291 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}\r
292\r
293#elif _CODE_PAGE == 866 /* Russian */\r
53668523 294#define _DF1S 0\r
70702af1
L
295#define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \\r
296 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \\r
297 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \\r
298 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
299 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \\r
300 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
301 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \\r
302 0xF0,0xF0,0xF2,0xF2,0xF4,0xF4,0xF6,0xF6,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}\r
303\r
304#elif _CODE_PAGE == 869 /* Greek 2 */\r
53668523 305#define _DF1S 0\r
70702af1
L
306#define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \\r
307 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x86,0x9C,0x8D,0x8F,0x90, \\r
308 0x91,0x90,0x92,0x95,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \\r
309 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
310 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \\r
311 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xA4,0xA5,0xA6,0xD9,0xDA,0xDB,0xDC,0xA7,0xA8,0xDF, \\r
312 0xA9,0xAA,0xAC,0xAD,0xB5,0xB6,0xB7,0xB8,0xBD,0xBE,0xC6,0xC7,0xCF,0xCF,0xD0,0xEF, \\r
313 0xF0,0xF1,0xD1,0xD2,0xD3,0xF5,0xD4,0xF7,0xF8,0xF9,0xD5,0x96,0x95,0x98,0xFE,0xFF}\r
53668523
L
314\r
315#elif _CODE_PAGE == 1 /* ASCII (for only non-LFN cfg) */\r
70702af1
L
316#if _USE_LFN != 0\r
317#error Cannot enable LFN without valid code page.\r
53668523
L
318#endif\r
319#define _DF1S 0\r
320\r
321#else\r
322#error Unknown code page\r
323\r
324#endif\r
325\r
326\r
327/* Character code support macros */\r
328#define IsUpper(c) (((c)>='A')&&((c)<='Z'))\r
329#define IsLower(c) (((c)>='a')&&((c)<='z'))\r
330#define IsDigit(c) (((c)>='0')&&((c)<='9'))\r
331\r
70702af1 332#if _DF1S != 0 /* Code page is DBCS */\r
53668523
L
333\r
334#ifdef _DF2S /* Two 1st byte areas */\r
335#define IsDBCS1(c) (((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E) || ((BYTE)(c) >= _DF2S && (BYTE)(c) <= _DF2E))\r
336#else /* One 1st byte area */\r
337#define IsDBCS1(c) ((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E)\r
338#endif\r
339\r
340#ifdef _DS3S /* Three 2nd byte areas */\r
341#define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E) || ((BYTE)(c) >= _DS3S && (BYTE)(c) <= _DS3E))\r
342#else /* Two 2nd byte areas */\r
343#define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E))\r
344#endif\r
345\r
346#else /* Code page is SBCS */\r
347\r
348#define IsDBCS1(c) 0\r
349#define IsDBCS2(c) 0\r
350\r
351#endif /* _DF1S */\r
352\r
353\r
70702af1
L
354/* File attribute bits (internal use) */\r
355#define AM_VOL 0x08 /* Volume label */\r
356#define AM_LFN 0x0F /* LFN entry */\r
357#define AM_MASK 0x3F /* Mask of defined bits */\r
358\r
359\r
360/* File access control and file status flags (internal use) */\r
361#define FA_SEEKEND 0x20 /* Seek to end of the file on file open */\r
362#define FA_MODIFIED 0x40 /* File has been modified */\r
363#define FA_DIRTY 0x80 /* FIL.buf[] needs to be written-back */\r
364\r
365\r
53668523 366/* Name status flags */\r
7b78a5a2 367#define NSFLAG 11 /* Index of name status byte in fn[] */\r
53668523
L
368#define NS_LOSS 0x01 /* Out of 8.3 format */\r
369#define NS_LFN 0x02 /* Force to create LFN entry */\r
370#define NS_LAST 0x04 /* Last segment */\r
371#define NS_BODY 0x08 /* Lower case flag (body) */\r
372#define NS_EXT 0x10 /* Lower case flag (ext) */\r
373#define NS_DOT 0x20 /* Dot entry */\r
70702af1
L
374#define NS_NOLFN 0x40 /* Do not find LFN */\r
375#define NS_NONAME 0x80 /* Not followed */\r
53668523
L
376\r
377\r
70702af1
L
378/* Limits and boundaries (differ from specs but correct for real DOS/Windows) */\r
379#define MAX_FAT12 0xFF5 /* Maximum number of FAT12 clusters */\r
380#define MAX_FAT16 0xFFF5 /* Maximum number of FAT16 clusters */\r
381#define MAX_FAT32 0xFFFFFF5 /* Maximum number of FAT32 clusters */\r
382#define MAX_EXFAT 0x7FFFFFFD /* Maximum number of exFAT clusters (limited by implementation) */\r
383#define MAX_DIR 0x200000 /* Maximum size of FAT directory */\r
384#define MAX_DIR_EX 0x10000000 /* Maximum size of exFAT directory */\r
53668523
L
385\r
386\r
387/* FatFs refers the members in the FAT structures as byte array instead of\r
70702af1 388/ structure members because the structure is not binary compatible between\r
53668523
L
389/ different platforms */\r
390\r
70702af1
L
391#define BS_JmpBoot 0 /* x86 jump instruction (3-byte) */\r
392#define BS_OEMName 3 /* OEM name (8-byte) */\r
393#define BPB_BytsPerSec 11 /* Sector size [byte] (WORD) */\r
394#define BPB_SecPerClus 13 /* Cluster size [sector] (BYTE) */\r
395#define BPB_RsvdSecCnt 14 /* Size of reserved area [sector] (WORD) */\r
396#define BPB_NumFATs 16 /* Number of FATs (BYTE) */\r
397#define BPB_RootEntCnt 17 /* Size of root directory area for FAT12/16 [entry] (WORD) */\r
398#define BPB_TotSec16 19 /* Volume size (16-bit) [sector] (WORD) */\r
399#define BPB_Media 21 /* Media descriptor byte (BYTE) */\r
400#define BPB_FATSz16 22 /* FAT size (16-bit) [sector] (WORD) */\r
401#define BPB_SecPerTrk 24 /* Track size for int13h [sector] (WORD) */\r
402#define BPB_NumHeads 26 /* Number of heads for int13h (WORD) */\r
403#define BPB_HiddSec 28 /* Volume offset from top of the drive (DWORD) */\r
404#define BPB_TotSec32 32 /* Volume size (32-bit) [sector] (DWORD) */\r
405#define BS_DrvNum 36 /* Physical drive number for int13h (BYTE) */\r
406#define BS_NTres 37 /* Error flag (BYTE) */\r
407#define BS_BootSig 38 /* Extended boot signature (BYTE) */\r
408#define BS_VolID 39 /* Volume serial number (DWORD) */\r
409#define BS_VolLab 43 /* Volume label string (8-byte) */\r
410#define BS_FilSysType 54 /* File system type string (8-byte) */\r
411#define BS_BootCode 62 /* Boot code (448-byte) */\r
412#define BS_55AA 510 /* Signature word (WORD) */\r
413\r
414#define BPB_FATSz32 36 /* FAT32: FAT size [sector] (DWORD) */\r
415#define BPB_ExtFlags32 40 /* FAT32: Extended flags (WORD) */\r
416#define BPB_FSVer32 42 /* FAT32: File system version (WORD) */\r
417#define BPB_RootClus32 44 /* FAT32: Root directory cluster (DWORD) */\r
418#define BPB_FSInfo32 48 /* FAT32: Offset of FSINFO sector (WORD) */\r
419#define BPB_BkBootSec32 50 /* FAT32: Offset of backup boot sector (WORD) */\r
420#define BS_DrvNum32 64 /* FAT32: Physical drive number for int13h (BYTE) */\r
421#define BS_NTres32 65 /* FAT32: Error flag (BYTE) */\r
422#define BS_BootSig32 66 /* FAT32: Extended boot signature (BYTE) */\r
423#define BS_VolID32 67 /* FAT32: Volume serial number (DWORD) */\r
424#define BS_VolLab32 71 /* FAT32: Volume label string (8-byte) */\r
425#define BS_FilSysType32 82 /* FAT32: File system type string (8-byte) */\r
426#define BS_BootCode32 90 /* FAT32: Boot code (420-byte) */\r
427\r
428#define BPB_ZeroedEx 11 /* exFAT: MBZ field (53-byte) */\r
429#define BPB_VolOfsEx 64 /* exFAT: Volume offset from top of the drive [sector] (QWORD) */\r
430#define BPB_TotSecEx 72 /* exFAT: Volume size [sector] (QWORD) */\r
431#define BPB_FatOfsEx 80 /* exFAT: FAT offset from top of the volume [sector] (DWORD) */\r
432#define BPB_FatSzEx 84 /* exFAT: FAT size [sector] (DWORD) */\r
433#define BPB_DataOfsEx 88 /* exFAT: Data offset from top of the volume [sector] (DWORD) */\r
434#define BPB_NumClusEx 92 /* exFAT: Number of clusters (DWORD) */\r
435#define BPB_RootClusEx 96 /* exFAT: Root directory cluster (DWORD) */\r
436#define BPB_VolIDEx 100 /* exFAT: Volume serial number (DWORD) */\r
437#define BPB_FSVerEx 104 /* exFAT: File system version (WORD) */\r
438#define BPB_VolFlagEx 106 /* exFAT: Volume flags (BYTE) */\r
439#define BPB_ActFatEx 107 /* exFAT: Active FAT flags (BYTE) */\r
440#define BPB_BytsPerSecEx 108 /* exFAT: Log2 of sector size in byte (BYTE) */\r
441#define BPB_SecPerClusEx 109 /* exFAT: Log2 of cluster size in sector (BYTE) */\r
442#define BPB_NumFATsEx 110 /* exFAT: Number of FATs (BYTE) */\r
443#define BPB_DrvNumEx 111 /* exFAT: Physical drive number for int13h (BYTE) */\r
444#define BPB_PercInUseEx 112 /* exFAT: Percent in use (BYTE) */\r
445#define BPB_RsvdEx 113 /* exFAT: Reserved (7-byte) */\r
446#define BS_BootCodeEx 120 /* exFAT: Boot code (390-byte) */\r
447\r
448#define FSI_LeadSig 0 /* FAT32 FSI: Leading signature (DWORD) */\r
449#define FSI_StrucSig 484 /* FAT32 FSI: Structure signature (DWORD) */\r
450#define FSI_Free_Count 488 /* FAT32 FSI: Number of free clusters (DWORD) */\r
451#define FSI_Nxt_Free 492 /* FAT32 FSI: Last allocated cluster (DWORD) */\r
452\r
453#define MBR_Table 446 /* MBR: Offset of partition table in the MBR */\r
53668523 454#define SZ_PTE 16 /* MBR: Size of a partition table entry */\r
70702af1
L
455#define PTE_Boot 0 /* MBR PTE: Boot indicator */\r
456#define PTE_StHead 1 /* MBR PTE: Start head */\r
457#define PTE_StSec 2 /* MBR PTE: Start sector */\r
458#define PTE_StCyl 3 /* MBR PTE: Start cylinder */\r
459#define PTE_System 4 /* MBR PTE: System ID */\r
460#define PTE_EdHead 5 /* MBR PTE: End head */\r
461#define PTE_EdSec 6 /* MBR PTE: End sector */\r
462#define PTE_EdCyl 7 /* MBR PTE: End cylinder */\r
463#define PTE_StLba 8 /* MBR PTE: Start in LBA */\r
464#define PTE_SizLba 12 /* MBR PTE: Size in LBA */\r
465\r
466#define DIR_Name 0 /* Short file name (11-byte) */\r
467#define DIR_Attr 11 /* Attribute (BYTE) */\r
468#define DIR_NTres 12 /* Lower case flag (BYTE) */\r
469#define DIR_CrtTime10 13 /* Created time sub-second (BYTE) */\r
470#define DIR_CrtTime 14 /* Created time (DWORD) */\r
471#define DIR_LstAccDate 18 /* Last accessed date (WORD) */\r
472#define DIR_FstClusHI 20 /* Higher 16-bit of first cluster (WORD) */\r
473#define DIR_ModTime 22 /* Modified time (DWORD) */\r
474#define DIR_FstClusLO 26 /* Lower 16-bit of first cluster (WORD) */\r
475#define DIR_FileSize 28 /* File size (DWORD) */\r
476#define LDIR_Ord 0 /* LFN entry order and LLE flag (BYTE) */\r
477#define LDIR_Attr 11 /* LFN attribute (BYTE) */\r
478#define LDIR_Type 12 /* LFN type (BYTE) */\r
479#define LDIR_Chksum 13 /* Checksum of the SFN entry (BYTE) */\r
480#define LDIR_FstClusLO 26 /* Must be zero (WORD) */\r
481#define XDIR_Type 0 /* Type of exFAT directory entry (BYTE) */\r
482#define XDIR_NumLabel 1 /* Number of volume label characters (BYTE) */\r
483#define XDIR_Label 2 /* Volume label (11-WORD) */\r
484#define XDIR_CaseSum 4 /* Sum of case conversion table (DWORD) */\r
485#define XDIR_NumSec 1 /* Number of secondary entries (BYTE) */\r
486#define XDIR_SetSum 2 /* Sum of the set of directory entries (WORD) */\r
487#define XDIR_Attr 4 /* File attribute (WORD) */\r
488#define XDIR_CrtTime 8 /* Created time (DWORD) */\r
489#define XDIR_ModTime 12 /* Modified time (DWORD) */\r
490#define XDIR_AccTime 16 /* Last accessed time (DWORD) */\r
491#define XDIR_CrtTime10 20 /* Created time subsecond (BYTE) */\r
492#define XDIR_ModTime10 21 /* Modified time subsecond (BYTE) */\r
493#define XDIR_CrtTZ 22 /* Created timezone (BYTE) */\r
494#define XDIR_ModTZ 23 /* Modified timezone (BYTE) */\r
495#define XDIR_AccTZ 24 /* Last accessed timezone (BYTE) */\r
496#define XDIR_GenFlags 33 /* Gneral secondary flags (WORD) */\r
497#define XDIR_NumName 35 /* Number of file name characters (BYTE) */\r
498#define XDIR_NameHash 36 /* Hash of file name (WORD) */\r
499#define XDIR_ValidFileSize 40 /* Valid file size (QWORD) */\r
500#define XDIR_FstClus 52 /* First cluster of the file data (DWORD) */\r
501#define XDIR_FileSize 56 /* File/Directory size (QWORD) */\r
502\r
503#define SZDIRE 32 /* Size of a directory entry */\r
504#define LLEF 0x40 /* Last long entry flag in LDIR_Ord */\r
505#define DDEM 0xE5 /* Deleted directory entry mark set to DIR_Name[0] */\r
506#define RDDEM 0x05 /* Replacement of the character collides with DDEM */\r
507\r
508\r
509\r
510\r
511\r
512/*--------------------------------------------------------------------------\r
513\r
514 Module Private Work Area\r
515\r
516---------------------------------------------------------------------------*/\r
517\r
518/* Remark: Variables here without initial value shall be guaranteed zero/null\r
519/ at start-up. If not, either the linker or start-up routine being used is\r
520/ not compliance with C standard. */\r
53668523 521\r
7b78a5a2
L
522#if _VOLUMES < 1 || _VOLUMES > 9\r
523#error Wrong _VOLUMES setting\r
53668523 524#endif\r
7b78a5a2
L
525static FATFS *FatFs[_VOLUMES]; /* Pointer to the file system objects (logical drives) */\r
526static WORD Fsid; /* File system mount ID */\r
53668523 527\r
70702af1 528#if _FS_RPATH != 0 && _VOLUMES >= 2\r
7b78a5a2 529static BYTE CurrVol; /* Current drive */\r
53668523
L
530#endif\r
531\r
70702af1 532#if _FS_LOCK != 0\r
7b78a5a2 533static FILESEM Files[_FS_LOCK]; /* Open object lock semaphores */\r
53668523
L
534#endif\r
535\r
70702af1
L
536#if _USE_LFN == 0 /* Non-LFN configuration */\r
537#define DEF_NAMBUF\r
538#define INIT_NAMBUF(fs)\r
539#define FREE_NAMBUF()\r
7b78a5a2
L
540#else\r
541#if _MAX_LFN < 12 || _MAX_LFN > 255\r
542#error Wrong _MAX_LFN setting\r
543#endif\r
70702af1
L
544\r
545#if _USE_LFN == 1 /* LFN enabled with static working buffer */\r
546#if _FS_EXFAT\r
547static BYTE DirBuf[SZDIRE*19]; /* Directory entry block scratchpad buffer (19 entries in size) */\r
548#endif\r
549static WCHAR LfnBuf[_MAX_LFN+1]; /* LFN enabled with static working buffer */\r
550#define DEF_NAMBUF\r
551#define INIT_NAMBUF(fs)\r
552#define FREE_NAMBUF()\r
553\r
554#elif _USE_LFN == 2 /* LFN enabled with dynamic working buffer on the stack */\r
555#if _FS_EXFAT\r
556#define DEF_NAMBUF WCHAR lbuf[_MAX_LFN+1]; BYTE dbuf[SZDIRE*19];\r
557#define INIT_NAMBUF(fs) { (fs)->lfnbuf = lbuf; (fs)->dirbuf = dbuf; }\r
558#define FREE_NAMBUF()\r
559#else\r
560#define DEF_NAMBUF WCHAR lbuf[_MAX_LFN+1];\r
561#define INIT_NAMBUF(fs) { (fs)->lfnbuf = lbuf; }\r
562#define FREE_NAMBUF()\r
563#endif\r
564\r
565#elif _USE_LFN == 3 /* LFN enabled with dynamic working buffer on the heap */\r
566#if _FS_EXFAT\r
567#define DEF_NAMBUF WCHAR *lfn;\r
568#define INIT_NAMBUF(fs) { lfn = ff_memalloc((_MAX_LFN+1)*2 + SZDIRE*19); if (!lfn) LEAVE_FF(fs, FR_NOT_ENOUGH_CORE); (fs)->lfnbuf = lfn; (fs)->dirbuf = (BYTE*)(lfn+_MAX_LFN+1); }\r
569#define FREE_NAMBUF() ff_memfree(lfn)\r
570#else\r
571#define DEF_NAMBUF WCHAR *lfn;\r
572#define INIT_NAMBUF(fs) { lfn = ff_memalloc((_MAX_LFN+1)*2); if (!lfn) LEAVE_FF(fs, FR_NOT_ENOUGH_CORE); (fs)->lfnbuf = lfn; }\r
573#define FREE_NAMBUF() ff_memfree(lfn)\r
574#endif\r
575\r
53668523 576#else\r
7b78a5a2
L
577#error Wrong _USE_LFN setting\r
578#endif\r
53668523 579#endif\r
53668523
L
580\r
581#ifdef _EXCVT\r
70702af1 582static const BYTE ExCvt[] = _EXCVT; /* Upper conversion table for SBCS extended characters */\r
53668523
L
583#endif\r
584\r
585\r
586\r
587\r
588\r
589\r
590/*--------------------------------------------------------------------------\r
591\r
592 Module Private Functions\r
593\r
594---------------------------------------------------------------------------*/\r
595\r
596\r
70702af1
L
597/*-----------------------------------------------------------------------*/\r
598/* Load/Store multi-byte word in the FAT structure */\r
599/*-----------------------------------------------------------------------*/\r
600\r
601static\r
602WORD ld_word (const BYTE* ptr) /* Load a 2-byte little-endian word */\r
603{\r
604 WORD rv;\r
605\r
606 rv = ptr[1];\r
607 rv = rv << 8 | ptr[0];\r
608 return rv;\r
609}\r
610\r
611static\r
612DWORD ld_dword (const BYTE* ptr) /* Load a 4-byte little-endian word */\r
613{\r
614 DWORD rv;\r
615\r
616 rv = ptr[3];\r
617 rv = rv << 8 | ptr[2];\r
618 rv = rv << 8 | ptr[1];\r
619 rv = rv << 8 | ptr[0];\r
620 return rv;\r
621}\r
622\r
623#if _FS_EXFAT\r
624static\r
625QWORD ld_qword (const BYTE* ptr) /* Load an 8-byte little-endian word */\r
626{\r
627 QWORD rv;\r
628\r
629 rv = ptr[7];\r
630 rv = rv << 8 | ptr[6];\r
631 rv = rv << 8 | ptr[5];\r
632 rv = rv << 8 | ptr[4];\r
633 rv = rv << 8 | ptr[3];\r
634 rv = rv << 8 | ptr[2];\r
635 rv = rv << 8 | ptr[1];\r
636 rv = rv << 8 | ptr[0];\r
637 return rv;\r
638}\r
639#endif\r
640\r
641#if !_FS_READONLY\r
642static\r
643void st_word (BYTE* ptr, WORD val) /* Store a 2-byte word in little-endian */\r
644{\r
645 *ptr++ = (BYTE)val; val >>= 8;\r
646 *ptr++ = (BYTE)val;\r
647}\r
648\r
649static\r
650void st_dword (BYTE* ptr, DWORD val) /* Store a 4-byte word in little-endian */\r
651{\r
652 *ptr++ = (BYTE)val; val >>= 8;\r
653 *ptr++ = (BYTE)val; val >>= 8;\r
654 *ptr++ = (BYTE)val; val >>= 8;\r
655 *ptr++ = (BYTE)val;\r
656}\r
657\r
658#if _FS_EXFAT\r
659static\r
660void st_qword (BYTE* ptr, QWORD val) /* Store an 8-byte word in little-endian */\r
661{\r
662 *ptr++ = (BYTE)val; val >>= 8;\r
663 *ptr++ = (BYTE)val; val >>= 8;\r
664 *ptr++ = (BYTE)val; val >>= 8;\r
665 *ptr++ = (BYTE)val; val >>= 8;\r
666 *ptr++ = (BYTE)val; val >>= 8;\r
667 *ptr++ = (BYTE)val; val >>= 8;\r
668 *ptr++ = (BYTE)val; val >>= 8;\r
669 *ptr++ = (BYTE)val;\r
670}\r
671#endif\r
672#endif /* !_FS_READONLY */\r
673\r
674\r
675\r
53668523
L
676/*-----------------------------------------------------------------------*/\r
677/* String functions */\r
678/*-----------------------------------------------------------------------*/\r
679\r
680/* Copy memory to memory */\r
681static\r
682void mem_cpy (void* dst, const void* src, UINT cnt) {\r
683 BYTE *d = (BYTE*)dst;\r
684 const BYTE *s = (const BYTE*)src;\r
685\r
70702af1
L
686 if (cnt) {\r
687 do *d++ = *s++; while (--cnt);\r
53668523 688 }\r
53668523
L
689}\r
690\r
70702af1 691/* Fill memory block */\r
53668523
L
692static\r
693void mem_set (void* dst, int val, UINT cnt) {\r
694 BYTE *d = (BYTE*)dst;\r
695\r
70702af1 696 do *d++ = (BYTE)val; while (--cnt);\r
53668523
L
697}\r
698\r
70702af1 699/* Compare memory block */\r
53668523 700static\r
70702af1 701int mem_cmp (const void* dst, const void* src, UINT cnt) { /* ZR:same, NZ:different */\r
53668523
L
702 const BYTE *d = (const BYTE *)dst, *s = (const BYTE *)src;\r
703 int r = 0;\r
704\r
70702af1
L
705 do {\r
706 r = *d++ - *s++;\r
707 } while (--cnt && r == 0);\r
708\r
53668523
L
709 return r;\r
710}\r
711\r
712/* Check if chr is contained in the string */\r
713static\r
70702af1 714int chk_chr (const char* str, int chr) { /* NZ:contained, ZR:not contained */\r
53668523
L
715 while (*str && *str != chr) str++;\r
716 return *str;\r
717}\r
718\r
719\r
720\r
721\r
70702af1 722#if _FS_REENTRANT\r
53668523
L
723/*-----------------------------------------------------------------------*/\r
724/* Request/Release grant to access the volume */\r
725/*-----------------------------------------------------------------------*/\r
53668523
L
726static\r
727int lock_fs (\r
728 FATFS* fs /* File system object */\r
729)\r
730{\r
731 return ff_req_grant(fs->sobj);\r
732}\r
733\r
734\r
735static\r
736void unlock_fs (\r
737 FATFS* fs, /* File system object */\r
738 FRESULT res /* Result code to be returned */\r
739)\r
740{\r
70702af1 741 if (fs && res != FR_NOT_ENABLED && res != FR_INVALID_DRIVE && res != FR_TIMEOUT) {\r
53668523
L
742 ff_rel_grant(fs->sobj);\r
743 }\r
744}\r
53668523 745\r
70702af1 746#endif\r
53668523
L
747\r
748\r
749\r
70702af1 750#if _FS_LOCK != 0\r
53668523
L
751/*-----------------------------------------------------------------------*/\r
752/* File lock control functions */\r
753/*-----------------------------------------------------------------------*/\r
53668523
L
754\r
755static\r
756FRESULT chk_lock ( /* Check if the file can be accessed */\r
757 DIR* dp, /* Directory object pointing the file to be checked */\r
758 int acc /* Desired access type (0:Read, 1:Write, 2:Delete/Rename) */\r
759)\r
760{\r
761 UINT i, be;\r
762\r
763 /* Search file semaphore table */\r
764 for (i = be = 0; i < _FS_LOCK; i++) {\r
765 if (Files[i].fs) { /* Existing entry */\r
70702af1
L
766 if (Files[i].fs == dp->obj.fs && /* Check if the object matched with an open object */\r
767 Files[i].clu == dp->obj.sclust &&\r
768 Files[i].ofs == dp->dptr) break;\r
53668523
L
769 } else { /* Blank entry */\r
770 be = 1;\r
771 }\r
772 }\r
70702af1 773 if (i == _FS_LOCK) { /* The object is not opened */\r
53668523 774 return (be || acc == 2) ? FR_OK : FR_TOO_MANY_OPEN_FILES; /* Is there a blank entry for new object? */\r
70702af1 775 }\r
53668523
L
776\r
777 /* The object has been opened. Reject any open against writing file and all write mode open */\r
778 return (acc || Files[i].ctr == 0x100) ? FR_LOCKED : FR_OK;\r
779}\r
780\r
781\r
782static\r
783int enq_lock (void) /* Check if an entry is available for a new object */\r
784{\r
785 UINT i;\r
786\r
787 for (i = 0; i < _FS_LOCK && Files[i].fs; i++) ;\r
788 return (i == _FS_LOCK) ? 0 : 1;\r
789}\r
790\r
791\r
792static\r
793UINT inc_lock ( /* Increment object open counter and returns its index (0:Internal error) */\r
794 DIR* dp, /* Directory object pointing the file to register or increment */\r
795 int acc /* Desired access (0:Read, 1:Write, 2:Delete/Rename) */\r
796)\r
797{\r
798 UINT i;\r
799\r
800\r
801 for (i = 0; i < _FS_LOCK; i++) { /* Find the object */\r
70702af1
L
802 if (Files[i].fs == dp->obj.fs &&\r
803 Files[i].clu == dp->obj.sclust &&\r
804 Files[i].ofs == dp->dptr) break;\r
53668523
L
805 }\r
806\r
807 if (i == _FS_LOCK) { /* Not opened. Register it as new. */\r
808 for (i = 0; i < _FS_LOCK && Files[i].fs; i++) ;\r
809 if (i == _FS_LOCK) return 0; /* No free entry to register (int err) */\r
70702af1
L
810 Files[i].fs = dp->obj.fs;\r
811 Files[i].clu = dp->obj.sclust;\r
812 Files[i].ofs = dp->dptr;\r
53668523
L
813 Files[i].ctr = 0;\r
814 }\r
815\r
816 if (acc && Files[i].ctr) return 0; /* Access violation (int err) */\r
817\r
818 Files[i].ctr = acc ? 0x100 : Files[i].ctr + 1; /* Set semaphore value */\r
819\r
820 return i + 1;\r
821}\r
822\r
823\r
824static\r
825FRESULT dec_lock ( /* Decrement object open counter */\r
826 UINT i /* Semaphore index (1..) */\r
827)\r
828{\r
829 WORD n;\r
830 FRESULT res;\r
831\r
832\r
833 if (--i < _FS_LOCK) { /* Shift index number origin from 0 */\r
834 n = Files[i].ctr;\r
835 if (n == 0x100) n = 0; /* If write mode open, delete the entry */\r
70702af1 836 if (n > 0) n--; /* Decrement read mode open count */\r
53668523 837 Files[i].ctr = n;\r
70702af1 838 if (n == 0) Files[i].fs = 0; /* Delete the entry if open count gets zero */\r
53668523
L
839 res = FR_OK;\r
840 } else {\r
841 res = FR_INT_ERR; /* Invalid index nunber */\r
842 }\r
843 return res;\r
844}\r
845\r
846\r
847static\r
848void clear_lock ( /* Clear lock entries of the volume */\r
849 FATFS *fs\r
850)\r
851{\r
852 UINT i;\r
853\r
854 for (i = 0; i < _FS_LOCK; i++) {\r
855 if (Files[i].fs == fs) Files[i].fs = 0;\r
856 }\r
857}\r
53668523 858\r
70702af1 859#endif /* _FS_LOCK != 0 */\r
53668523
L
860\r
861\r
862\r
863/*-----------------------------------------------------------------------*/\r
864/* Move/Flush disk access window in the file system object */\r
865/*-----------------------------------------------------------------------*/\r
866#if !_FS_READONLY\r
867static\r
70702af1
L
868FRESULT sync_window ( /* Returns FR_OK or FR_DISK_ERROR */\r
869 FATFS* fs /* File system object */\r
53668523
L
870)\r
871{\r
872 DWORD wsect;\r
873 UINT nf;\r
7b78a5a2 874 FRESULT res = FR_OK;\r
53668523
L
875\r
876\r
877 if (fs->wflag) { /* Write back the sector if it is dirty */\r
878 wsect = fs->winsect; /* Current sector number */\r
7b78a5a2
L
879 if (disk_write(fs->drv, fs->win, wsect, 1) != RES_OK) {\r
880 res = FR_DISK_ERR;\r
881 } else {\r
882 fs->wflag = 0;\r
883 if (wsect - fs->fatbase < fs->fsize) { /* Is it in the FAT area? */\r
884 for (nf = fs->n_fats; nf >= 2; nf--) { /* Reflect the change to all FAT copies */\r
885 wsect += fs->fsize;\r
886 disk_write(fs->drv, fs->win, wsect, 1);\r
887 }\r
53668523
L
888 }\r
889 }\r
890 }\r
7b78a5a2 891 return res;\r
53668523
L
892}\r
893#endif\r
894\r
895\r
896static\r
70702af1
L
897FRESULT move_window ( /* Returns FR_OK or FR_DISK_ERROR */\r
898 FATFS* fs, /* File system object */\r
899 DWORD sector /* Sector number to make appearance in the fs->win[] */\r
53668523
L
900)\r
901{\r
7b78a5a2
L
902 FRESULT res = FR_OK;\r
903\r
904\r
905 if (sector != fs->winsect) { /* Window offset changed? */\r
53668523 906#if !_FS_READONLY\r
7b78a5a2 907 res = sync_window(fs); /* Write-back changes */\r
53668523 908#endif\r
7b78a5a2
L
909 if (res == FR_OK) { /* Fill sector window with new data */\r
910 if (disk_read(fs->drv, fs->win, sector, 1) != RES_OK) {\r
911 sector = 0xFFFFFFFF; /* Invalidate window if data is not reliable */\r
912 res = FR_DISK_ERR;\r
913 }\r
914 fs->winsect = sector;\r
915 }\r
53668523 916 }\r
7b78a5a2 917 return res;\r
53668523
L
918}\r
919\r
920\r
921\r
922\r
70702af1 923#if !_FS_READONLY\r
53668523
L
924/*-----------------------------------------------------------------------*/\r
925/* Synchronize file system and strage device */\r
926/*-----------------------------------------------------------------------*/\r
70702af1 927\r
53668523 928static\r
70702af1 929FRESULT sync_fs ( /* FR_OK:succeeded, !=0:error */\r
53668523
L
930 FATFS* fs /* File system object */\r
931)\r
932{\r
933 FRESULT res;\r
934\r
935\r
936 res = sync_window(fs);\r
937 if (res == FR_OK) {\r
70702af1 938 /* Update FSInfo sector if needed */\r
53668523 939 if (fs->fs_type == FS_FAT32 && fs->fsi_flag == 1) {\r
70702af1 940 /* Create FSInfo structure */\r
53668523 941 mem_set(fs->win, 0, SS(fs));\r
70702af1
L
942 st_word(fs->win + BS_55AA, 0xAA55);\r
943 st_dword(fs->win + FSI_LeadSig, 0x41615252);\r
944 st_dword(fs->win + FSI_StrucSig, 0x61417272);\r
945 st_dword(fs->win + FSI_Free_Count, fs->free_clst);\r
946 st_dword(fs->win + FSI_Nxt_Free, fs->last_clst);\r
947 /* Write it into the FSInfo sector */\r
53668523
L
948 fs->winsect = fs->volbase + 1;\r
949 disk_write(fs->drv, fs->win, fs->winsect, 1);\r
950 fs->fsi_flag = 0;\r
951 }\r
952 /* Make sure that no pending write process in the physical drive */\r
70702af1 953 if (disk_ioctl(fs->drv, CTRL_SYNC, 0) != RES_OK) res = FR_DISK_ERR;\r
53668523
L
954 }\r
955\r
956 return res;\r
957}\r
53668523 958\r
70702af1 959#endif\r
53668523
L
960\r
961\r
962\r
963/*-----------------------------------------------------------------------*/\r
964/* Get sector# from cluster# */\r
965/*-----------------------------------------------------------------------*/\r
53668523 966\r
70702af1
L
967static\r
968DWORD clust2sect ( /* !=0:Sector number, 0:Failed (invalid cluster#) */\r
53668523
L
969 FATFS* fs, /* File system object */\r
970 DWORD clst /* Cluster# to be converted */\r
971)\r
972{\r
973 clst -= 2;\r
7b78a5a2 974 if (clst >= fs->n_fatent - 2) return 0; /* Invalid cluster# */\r
53668523
L
975 return clst * fs->csize + fs->database;\r
976}\r
977\r
978\r
979\r
980\r
981/*-----------------------------------------------------------------------*/\r
982/* FAT access - Read value of a FAT entry */\r
983/*-----------------------------------------------------------------------*/\r
984\r
70702af1
L
985static\r
986DWORD get_fat ( /* 0xFFFFFFFF:Disk error, 1:Internal error, 2..0x7FFFFFFF:Cluster status */\r
987 _FDID* obj, /* Corresponding object */\r
988 DWORD clst /* Cluster number to get the value */\r
53668523
L
989)\r
990{\r
991 UINT wc, bc;\r
7b78a5a2 992 DWORD val;\r
70702af1 993 FATFS *fs = obj->fs;\r
53668523
L
994\r
995\r
70702af1 996 if (clst < 2 || clst >= fs->n_fatent) { /* Check if in valid range */\r
7b78a5a2 997 val = 1; /* Internal error */\r
53668523 998\r
7b78a5a2
L
999 } else {\r
1000 val = 0xFFFFFFFF; /* Default value falls on disk error */\r
53668523 1001\r
7b78a5a2
L
1002 switch (fs->fs_type) {\r
1003 case FS_FAT12 :\r
1004 bc = (UINT)clst; bc += bc / 2;\r
1005 if (move_window(fs, fs->fatbase + (bc / SS(fs))) != FR_OK) break;\r
1006 wc = fs->win[bc++ % SS(fs)];\r
1007 if (move_window(fs, fs->fatbase + (bc / SS(fs))) != FR_OK) break;\r
1008 wc |= fs->win[bc % SS(fs)] << 8;\r
70702af1 1009 val = (clst & 1) ? (wc >> 4) : (wc & 0xFFF);\r
7b78a5a2
L
1010 break;\r
1011\r
1012 case FS_FAT16 :\r
1013 if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 2))) != FR_OK) break;\r
70702af1 1014 val = ld_word(fs->win + clst * 2 % SS(fs));\r
7b78a5a2 1015 break;\r
53668523 1016\r
7b78a5a2
L
1017 case FS_FAT32 :\r
1018 if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 4))) != FR_OK) break;\r
70702af1 1019 val = ld_dword(fs->win + clst * 4 % SS(fs)) & 0x0FFFFFFF;\r
7b78a5a2 1020 break;\r
70702af1
L
1021#if _FS_EXFAT\r
1022 case FS_EXFAT :\r
1023 if (obj->objsize) {\r
1024 DWORD cofs = clst - obj->sclust; /* Offset from start cluster */\r
1025 DWORD clen = (DWORD)((obj->objsize - 1) / SS(fs)) / fs->csize; /* Number of clusters - 1 */\r
1026\r
1027 if (obj->stat == 2) { /* Is there no valid chain on the FAT? */\r
1028 if (cofs <= clen) {\r
1029 val = (cofs == clen) ? 0x7FFFFFFF : clst + 1; /* Generate the value */\r
1030 break;\r
1031 }\r
1032 }\r
1033 if (obj->stat == 3 && cofs < obj->n_cont) { /* Is it in the contiguous part? */\r
1034 val = clst + 1; /* Generate the value */\r
1035 break;\r
1036 }\r
1037 if (obj->stat != 2) { /* Get value from FAT if FAT chain is valid */\r
1038 if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 4))) != FR_OK) break;\r
1039 val = ld_dword(fs->win + clst * 4 % SS(fs)) & 0x7FFFFFFF;\r
1040 break;\r
1041 }\r
1042 }\r
1043 /* go next */\r
1044#endif\r
7b78a5a2
L
1045 default:\r
1046 val = 1; /* Internal error */\r
1047 }\r
53668523
L
1048 }\r
1049\r
7b78a5a2 1050 return val;\r
53668523
L
1051}\r
1052\r
1053\r
1054\r
1055\r
70702af1 1056#if !_FS_READONLY\r
53668523
L
1057/*-----------------------------------------------------------------------*/\r
1058/* FAT access - Change value of a FAT entry */\r
1059/*-----------------------------------------------------------------------*/\r
53668523 1060\r
70702af1
L
1061static\r
1062FRESULT put_fat ( /* FR_OK(0):succeeded, !=0:error */\r
1063 FATFS* fs, /* Corresponding file system object */\r
1064 DWORD clst, /* FAT index number (cluster number) to be changed */\r
1065 DWORD val /* New value to be set to the entry */\r
53668523
L
1066)\r
1067{\r
1068 UINT bc;\r
1069 BYTE *p;\r
70702af1 1070 FRESULT res = FR_INT_ERR;\r
53668523
L
1071\r
1072\r
70702af1 1073 if (clst >= 2 && clst < fs->n_fatent) { /* Check if in valid range */\r
53668523 1074 switch (fs->fs_type) {\r
70702af1 1075 case FS_FAT12 : /* Bitfield items */\r
53668523
L
1076 bc = (UINT)clst; bc += bc / 2;\r
1077 res = move_window(fs, fs->fatbase + (bc / SS(fs)));\r
1078 if (res != FR_OK) break;\r
70702af1 1079 p = fs->win + bc++ % SS(fs);\r
53668523 1080 *p = (clst & 1) ? ((*p & 0x0F) | ((BYTE)val << 4)) : (BYTE)val;\r
53668523
L
1081 fs->wflag = 1;\r
1082 res = move_window(fs, fs->fatbase + (bc / SS(fs)));\r
1083 if (res != FR_OK) break;\r
70702af1 1084 p = fs->win + bc % SS(fs);\r
53668523 1085 *p = (clst & 1) ? (BYTE)(val >> 4) : ((*p & 0xF0) | ((BYTE)(val >> 8) & 0x0F));\r
7b78a5a2 1086 fs->wflag = 1;\r
53668523
L
1087 break;\r
1088\r
70702af1 1089 case FS_FAT16 : /* WORD aligned items */\r
53668523
L
1090 res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 2)));\r
1091 if (res != FR_OK) break;\r
70702af1 1092 st_word(fs->win + clst * 2 % SS(fs), (WORD)val);\r
7b78a5a2 1093 fs->wflag = 1;\r
53668523
L
1094 break;\r
1095\r
70702af1
L
1096 case FS_FAT32 : /* DWORD aligned items */\r
1097#if _FS_EXFAT\r
1098 case FS_EXFAT :\r
1099#endif\r
53668523
L
1100 res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 4)));\r
1101 if (res != FR_OK) break;\r
70702af1
L
1102 if (!_FS_EXFAT || fs->fs_type != FS_EXFAT) {\r
1103 val = (val & 0x0FFFFFFF) | (ld_dword(fs->win + clst * 4 % SS(fs)) & 0xF0000000);\r
1104 }\r
1105 st_dword(fs->win + clst * 4 % SS(fs), val);\r
7b78a5a2 1106 fs->wflag = 1;\r
53668523 1107 break;\r
53668523 1108 }\r
53668523 1109 }\r
53668523
L
1110 return res;\r
1111}\r
70702af1 1112\r
53668523
L
1113#endif /* !_FS_READONLY */\r
1114\r
1115\r
1116\r
1117\r
70702af1 1118#if _FS_EXFAT && !_FS_READONLY\r
53668523 1119/*-----------------------------------------------------------------------*/\r
70702af1 1120/* exFAT: Accessing FAT and Allocation Bitmap */\r
53668523 1121/*-----------------------------------------------------------------------*/\r
70702af1
L
1122\r
1123/*---------------------------------------------*/\r
1124/* exFAT: Find a contiguous free cluster block */\r
1125/*---------------------------------------------*/\r
1126\r
53668523 1127static\r
70702af1
L
1128DWORD find_bitmap ( /* 0:No free cluster, 2..:Free cluster found, 0xFFFFFFFF:Disk error */\r
1129 FATFS* fs, /* File system object */\r
1130 DWORD clst, /* Cluster number to scan from */\r
1131 DWORD ncl /* Number of contiguous clusters to find (1..) */\r
1132)\r
1133{\r
1134 BYTE bm, bv;\r
1135 UINT i;\r
1136 DWORD val, scl, ctr;\r
1137\r
1138\r
1139 clst -= 2; /* The first bit in the bitmap corresponds to cluster #2 */\r
1140 if (clst >= fs->n_fatent - 2) clst = 0;\r
1141 scl = val = clst; ctr = 0;\r
1142 for (;;) {\r
1143 if (move_window(fs, fs->database + val / 8 / SS(fs)) != FR_OK) return 0xFFFFFFFF; /* (assuming bitmap is located top of the cluster heap) */\r
1144 i = val / 8 % SS(fs); bm = 1 << (val % 8);\r
1145 do {\r
1146 do {\r
1147 bv = fs->win[i] & bm; bm <<= 1; /* Get bit value */\r
1148 if (++val >= fs->n_fatent - 2) { /* Next cluster (with wrap-around) */\r
1149 val = 0; bm = 0; i = 4096;\r
1150 }\r
1151 if (!bv) { /* Is it a free cluster? */\r
1152 if (++ctr == ncl) return scl + 2; /* Check run length */\r
1153 } else {\r
1154 scl = val; ctr = 0; /* Encountered a live cluster, restart to scan */\r
1155 }\r
1156 if (val == clst) return 0; /* All cluster scanned? */\r
1157 } while (bm);\r
1158 bm = 1;\r
1159 } while (++i < SS(fs));\r
1160 }\r
1161}\r
1162\r
1163\r
1164/*------------------------------------*/\r
1165/* exFAT: Set/Clear a block of bitmap */\r
1166/*------------------------------------*/\r
1167\r
1168static\r
1169FRESULT change_bitmap (\r
1170 FATFS* fs, /* File system object */\r
1171 DWORD clst, /* Cluster number to change from */\r
1172 DWORD ncl, /* Number of clusters to be changed */\r
1173 int bv /* bit value to be set (0 or 1) */\r
1174)\r
1175{\r
1176 BYTE bm;\r
1177 UINT i;\r
1178 DWORD sect;\r
1179\r
1180\r
1181 clst -= 2; /* The first bit corresponds to cluster #2 */\r
1182 sect = fs->database + clst / 8 / SS(fs); /* Sector address (assuming bitmap is located top of the cluster heap) */\r
1183 i = clst / 8 % SS(fs); /* Byte offset in the sector */\r
1184 bm = 1 << (clst % 8); /* Bit mask in the byte */\r
1185 for (;;) {\r
1186 if (move_window(fs, sect++) != FR_OK) return FR_DISK_ERR;\r
1187 do {\r
1188 do {\r
1189 if (bv == (int)((fs->win[i] & bm) != 0)) return FR_INT_ERR; /* Is the bit expected value? */\r
1190 fs->win[i] ^= bm; /* Flip the bit */\r
1191 fs->wflag = 1;\r
1192 if (--ncl == 0) return FR_OK; /* All bits processed? */\r
1193 } while (bm <<= 1); /* Next bit */\r
1194 bm = 1;\r
1195 } while (++i < SS(fs)); /* Next byte */\r
1196 i = 0;\r
1197 }\r
1198}\r
1199\r
1200\r
1201/*---------------------------------------------*/\r
1202/* Complement contiguous part of the FAT chain */\r
1203/*---------------------------------------------*/\r
1204\r
1205static\r
1206FRESULT fill_fat_chain (\r
1207 _FDID* obj /* Pointer to the corresponding object */\r
53668523
L
1208)\r
1209{\r
1210 FRESULT res;\r
70702af1
L
1211 DWORD cl, n;\r
1212\r
1213 if (obj->stat == 3) { /* Has the object been changed 'fragmented'? */\r
1214 for (cl = obj->sclust, n = obj->n_cont; n; cl++, n--) { /* Create cluster chain on the FAT */\r
1215 res = put_fat(obj->fs, cl, cl + 1);\r
1216 if (res != FR_OK) return res;\r
1217 }\r
1218 obj->stat = 0; /* Change status 'FAT chain is valid' */\r
1219 }\r
1220 return FR_OK;\r
1221}\r
1222\r
1223#endif /* _FS_EXFAT && !_FS_READONLY */\r
1224\r
1225\r
1226\r
1227#if !_FS_READONLY\r
1228/*-----------------------------------------------------------------------*/\r
1229/* FAT handling - Remove a cluster chain */\r
1230/*-----------------------------------------------------------------------*/\r
1231static\r
1232FRESULT remove_chain ( /* FR_OK(0):succeeded, !=0:error */\r
1233 _FDID* obj, /* Corresponding object */\r
1234 DWORD clst, /* Cluster to remove a chain from */\r
1235 DWORD pclst /* Previous cluster of clst (0:an entire chain) */\r
1236)\r
1237{\r
1238 FRESULT res = FR_OK;\r
53668523 1239 DWORD nxt;\r
70702af1
L
1240 FATFS *fs = obj->fs;\r
1241#if _FS_EXFAT || _USE_TRIM\r
1242 DWORD scl = clst, ecl = clst;\r
1243#endif\r
7b78a5a2 1244#if _USE_TRIM\r
70702af1 1245 DWORD rt[2];\r
53668523
L
1246#endif\r
1247\r
70702af1 1248 if (clst < 2 || clst >= fs->n_fatent) return FR_INT_ERR; /* Check if in valid range */\r
53668523 1249\r
70702af1
L
1250 /* Mark the previous cluster 'EOC' on the FAT if it exists */\r
1251 if (pclst && (!_FS_EXFAT || fs->fs_type != FS_EXFAT || obj->stat != 2)) {\r
1252 res = put_fat(fs, pclst, 0xFFFFFFFF);\r
1253 if (res != FR_OK) return res;\r
1254 }\r
1255\r
1256 /* Remove the chain */\r
1257 do {\r
1258 nxt = get_fat(obj, clst); /* Get cluster status */\r
1259 if (nxt == 0) break; /* Empty cluster? */\r
1260 if (nxt == 1) return FR_INT_ERR; /* Internal error? */\r
1261 if (nxt == 0xFFFFFFFF) return FR_DISK_ERR; /* Disk error? */\r
1262 if (!_FS_EXFAT || fs->fs_type != FS_EXFAT) {\r
1263 res = put_fat(fs, clst, 0); /* Mark the cluster 'free' on the FAT */\r
1264 if (res != FR_OK) return res;\r
1265 }\r
1266 if (fs->free_clst < fs->n_fatent - 2) { /* Update FSINFO */\r
1267 fs->free_clst++;\r
1268 fs->fsi_flag |= 1;\r
1269 }\r
1270#if _FS_EXFAT || _USE_TRIM\r
1271 if (ecl + 1 == nxt) { /* Is next cluster contiguous? */\r
1272 ecl = nxt;\r
1273 } else { /* End of contiguous cluster block */ \r
1274#if _FS_EXFAT\r
1275 if (fs->fs_type == FS_EXFAT) {\r
1276 res = change_bitmap(fs, scl, ecl - scl + 1, 0); /* Mark the cluster block 'free' on the bitmap */\r
1277 if (res != FR_OK) return res;\r
53668523 1278 }\r
70702af1 1279#endif\r
7b78a5a2 1280#if _USE_TRIM\r
70702af1
L
1281 rt[0] = clust2sect(fs, scl); /* Start sector */\r
1282 rt[1] = clust2sect(fs, ecl) + fs->csize - 1; /* End sector */\r
1283 disk_ioctl(fs->drv, CTRL_TRIM, rt); /* Inform device the block can be erased */\r
53668523 1284#endif\r
70702af1 1285 scl = ecl = nxt;\r
53668523 1286 }\r
70702af1
L
1287#endif\r
1288 clst = nxt; /* Next cluster */\r
1289 } while (clst < fs->n_fatent); /* Repeat while not the last link */\r
53668523 1290\r
70702af1
L
1291#if _FS_EXFAT\r
1292 if (fs->fs_type == FS_EXFAT) {\r
1293 if (pclst == 0) { /* Does object have no chain? */\r
1294 obj->stat = 0; /* Change the object status 'initial' */\r
1295 } else {\r
1296 if (obj->stat == 3 && pclst >= obj->sclust && pclst <= obj->sclust + obj->n_cont) { /* Did the chain got contiguous? */\r
1297 obj->stat = 2; /* Change the object status 'contiguous' */\r
1298 }\r
1299 }\r
1300 }\r
53668523 1301#endif\r
70702af1
L
1302 return FR_OK;\r
1303}\r
53668523
L
1304\r
1305\r
1306\r
1307\r
1308/*-----------------------------------------------------------------------*/\r
70702af1 1309/* FAT handling - Stretch a chain or Create a new chain */\r
53668523 1310/*-----------------------------------------------------------------------*/\r
53668523
L
1311static\r
1312DWORD create_chain ( /* 0:No free cluster, 1:Internal error, 0xFFFFFFFF:Disk error, >=2:New cluster# */\r
70702af1
L
1313 _FDID* obj, /* Corresponding object */\r
1314 DWORD clst /* Cluster# to stretch, 0:Create a new chain */\r
53668523
L
1315)\r
1316{\r
1317 DWORD cs, ncl, scl;\r
1318 FRESULT res;\r
70702af1 1319 FATFS *fs = obj->fs;\r
53668523
L
1320\r
1321\r
70702af1
L
1322 if (clst == 0) { /* Create a new chain */\r
1323 scl = fs->last_clst; /* Get suggested cluster to start from */\r
1324 if (scl == 0 || scl >= fs->n_fatent) scl = 1;\r
53668523 1325 }\r
70702af1
L
1326 else { /* Stretch current chain */\r
1327 cs = get_fat(obj, clst); /* Check the cluster status */\r
1328 if (cs < 2) return 1; /* Invalid value */\r
53668523
L
1329 if (cs == 0xFFFFFFFF) return cs; /* A disk error occurred */\r
1330 if (cs < fs->n_fatent) return cs; /* It is already followed by next cluster */\r
1331 scl = clst;\r
1332 }\r
1333\r
70702af1
L
1334#if _FS_EXFAT\r
1335 if (fs->fs_type == FS_EXFAT) { /* On the exFAT volume */\r
1336 ncl = find_bitmap(fs, scl, 1); /* Find a free cluster */\r
1337 if (ncl == 0 || ncl == 0xFFFFFFFF) return ncl; /* No free cluster or hard error? */\r
1338 res = change_bitmap(fs, ncl, 1, 1); /* Mark the cluster 'in use' */\r
1339 if (res == FR_INT_ERR) return 1;\r
1340 if (res == FR_DISK_ERR) return 0xFFFFFFFF;\r
1341 if (clst == 0) { /* Is it a new chain? */\r
1342 obj->stat = 2; /* Set status 'contiguous chain' */\r
1343 } else { /* This is a stretched chain */\r
1344 if (obj->stat == 2 && ncl != scl + 1) { /* Is the chain got fragmented? */\r
1345 obj->n_cont = scl - obj->sclust; /* Set size of the contiguous part */\r
1346 obj->stat = 3; /* Change status 'just fragmented' */\r
1347 }\r
1348 }\r
1349 } else\r
1350#endif\r
1351 { /* On the FAT12/16/32 volume */\r
1352 ncl = scl; /* Start cluster */\r
1353 for (;;) {\r
1354 ncl++; /* Next cluster */\r
1355 if (ncl >= fs->n_fatent) { /* Check wrap-around */\r
1356 ncl = 2;\r
1357 if (ncl > scl) return 0; /* No free cluster */\r
1358 }\r
1359 cs = get_fat(obj, ncl); /* Get the cluster status */\r
1360 if (cs == 0) break; /* Found a free cluster */\r
1361 if (cs == 1 || cs == 0xFFFFFFFF) return cs; /* An error occurred */\r
1362 if (ncl == scl) return 0; /* No free cluster */\r
53668523 1363 }\r
53668523
L
1364 }\r
1365\r
70702af1
L
1366 if (_FS_EXFAT && fs->fs_type == FS_EXFAT && obj->stat == 2) { /* Is it a contiguous chain? */\r
1367 res = FR_OK; /* FAT does not need to be written */\r
1368 } else {\r
1369 res = put_fat(fs, ncl, 0xFFFFFFFF); /* Mark the new cluster 'EOC' */\r
1370 if (res == FR_OK && clst) {\r
1371 res = put_fat(fs, clst, ncl); /* Link it from the previous one if needed */\r
53668523 1372 }\r
70702af1
L
1373 }\r
1374\r
1375 if (res == FR_OK) { /* Update FSINFO if function succeeded. */\r
1376 fs->last_clst = ncl;\r
1377 if (fs->free_clst < fs->n_fatent - 2) fs->free_clst--;\r
1378 fs->fsi_flag |= 1;\r
53668523 1379 } else {\r
70702af1 1380 ncl = (res == FR_DISK_ERR) ? 0xFFFFFFFF : 1; /* Failed. Create error status */\r
53668523
L
1381 }\r
1382\r
70702af1 1383 return ncl; /* Return new cluster number or error status */\r
53668523 1384}\r
70702af1 1385\r
53668523
L
1386#endif /* !_FS_READONLY */\r
1387\r
1388\r
1389\r
1390\r
70702af1 1391#if _USE_FASTSEEK\r
53668523
L
1392/*-----------------------------------------------------------------------*/\r
1393/* FAT handling - Convert offset into cluster with link map table */\r
1394/*-----------------------------------------------------------------------*/\r
1395\r
53668523
L
1396static\r
1397DWORD clmt_clust ( /* <2:Error, >=2:Cluster number */\r
1398 FIL* fp, /* Pointer to the file object */\r
70702af1 1399 FSIZE_t ofs /* File offset to be converted to cluster# */\r
53668523
L
1400)\r
1401{\r
1402 DWORD cl, ncl, *tbl;\r
70702af1 1403 FATFS *fs = fp->obj.fs;\r
53668523
L
1404\r
1405\r
1406 tbl = fp->cltbl + 1; /* Top of CLMT */\r
70702af1 1407 cl = (DWORD)(ofs / SS(fs) / fs->csize); /* Cluster order from top of the file */\r
53668523
L
1408 for (;;) {\r
1409 ncl = *tbl++; /* Number of cluters in the fragment */\r
70702af1 1410 if (ncl == 0) return 0; /* End of table? (error) */\r
53668523
L
1411 if (cl < ncl) break; /* In this fragment? */\r
1412 cl -= ncl; tbl++; /* Next fragment */\r
1413 }\r
1414 return cl + *tbl; /* Return the cluster number */\r
1415}\r
70702af1 1416\r
53668523
L
1417#endif /* _USE_FASTSEEK */\r
1418\r
1419\r
1420\r
1421\r
1422/*-----------------------------------------------------------------------*/\r
1423/* Directory handling - Set directory index */\r
1424/*-----------------------------------------------------------------------*/\r
1425\r
1426static\r
70702af1 1427FRESULT dir_sdi ( /* FR_OK(0):succeeded, !=0:error */\r
53668523 1428 DIR* dp, /* Pointer to directory object */\r
70702af1 1429 DWORD ofs /* Offset of directory table */\r
53668523
L
1430)\r
1431{\r
70702af1
L
1432 DWORD csz, clst;\r
1433 FATFS *fs = dp->obj.fs;\r
53668523
L
1434\r
1435\r
70702af1 1436 if (ofs >= (DWORD)((_FS_EXFAT && fs->fs_type == FS_EXFAT) ? MAX_DIR_EX : MAX_DIR) || ofs % SZDIRE) { /* Check range of offset and alignment */\r
53668523 1437 return FR_INT_ERR;\r
70702af1
L
1438 }\r
1439 dp->dptr = ofs; /* Set current offset */\r
1440 clst = dp->obj.sclust; /* Table start cluster (0:root) */\r
1441 if (clst == 0 && fs->fs_type >= FS_FAT32) { /* Replace cluster# 0 with root cluster# */\r
1442 clst = fs->dirbase;\r
1443 if (_FS_EXFAT) dp->obj.stat = 0; /* exFAT: Root dir has an FAT chain */\r
1444 }\r
53668523
L
1445\r
1446 if (clst == 0) { /* Static table (root-directory in FAT12/16) */\r
70702af1
L
1447 if (ofs / SZDIRE >= fs->n_rootdir) return FR_INT_ERR; /* Is index out of range? */\r
1448 dp->sect = fs->dirbase;\r
1449\r
1450 } else { /* Dynamic table (sub-directory or root-directory in FAT32+) */\r
1451 csz = (DWORD)fs->csize * SS(fs); /* Bytes per cluster */\r
1452 while (ofs >= csz) { /* Follow cluster chain */\r
1453 clst = get_fat(&dp->obj, clst); /* Get next cluster */\r
53668523 1454 if (clst == 0xFFFFFFFF) return FR_DISK_ERR; /* Disk error */\r
70702af1
L
1455 if (clst < 2 || clst >= fs->n_fatent) return FR_INT_ERR; /* Reached to end of table or internal error */\r
1456 ofs -= csz;\r
53668523 1457 }\r
70702af1 1458 dp->sect = clust2sect(fs, clst);\r
53668523 1459 }\r
70702af1
L
1460 dp->clust = clst; /* Current cluster# */\r
1461 if (!dp->sect) return FR_INT_ERR;\r
1462 dp->sect += ofs / SS(fs); /* Sector# of the directory entry */\r
1463 dp->dir = fs->win + (ofs % SS(fs)); /* Pointer to the entry in the win[] */\r
53668523
L
1464\r
1465 return FR_OK;\r
1466}\r
1467\r
1468\r
1469\r
1470\r
1471/*-----------------------------------------------------------------------*/\r
1472/* Directory handling - Move directory table index next */\r
1473/*-----------------------------------------------------------------------*/\r
1474\r
1475static\r
70702af1 1476FRESULT dir_next ( /* FR_OK(0):succeeded, FR_NO_FILE:End of table, FR_DENIED:Could not stretch */\r
53668523
L
1477 DIR* dp, /* Pointer to the directory object */\r
1478 int stretch /* 0: Do not stretch table, 1: Stretch table if needed */\r
1479)\r
1480{\r
70702af1
L
1481 DWORD ofs, clst;\r
1482 FATFS *fs = dp->obj.fs;\r
1483#if !_FS_READONLY\r
1484 UINT n;\r
1485#endif\r
53668523 1486\r
70702af1
L
1487 ofs = dp->dptr + SZDIRE; /* Next entry */\r
1488 if (!dp->sect || ofs >= (DWORD)((_FS_EXFAT && fs->fs_type == FS_EXFAT) ? MAX_DIR_EX : MAX_DIR)) return FR_NO_FILE; /* Report EOT when offset has reached max value */\r
53668523 1489\r
70702af1
L
1490 if (ofs % SS(fs) == 0) { /* Sector changed? */\r
1491 dp->sect++; /* Next sector */\r
53668523
L
1492\r
1493 if (!dp->clust) { /* Static table */\r
70702af1
L
1494 if (ofs / SZDIRE >= fs->n_rootdir) { /* Report EOT if it reached end of static table */\r
1495 dp->sect = 0; return FR_NO_FILE;\r
1496 }\r
53668523
L
1497 }\r
1498 else { /* Dynamic table */\r
70702af1
L
1499 if ((ofs / SS(fs) & (fs->csize - 1)) == 0) { /* Cluster changed? */\r
1500 clst = get_fat(&dp->obj, dp->clust); /* Get next cluster */\r
1501 if (clst <= 1) return FR_INT_ERR; /* Internal error */\r
1502 if (clst == 0xFFFFFFFF) return FR_DISK_ERR; /* Disk error */\r
1503 if (clst >= fs->n_fatent) { /* Reached end of dynamic table */\r
53668523 1504#if !_FS_READONLY\r
70702af1
L
1505 if (!stretch) { /* If no stretch, report EOT */\r
1506 dp->sect = 0; return FR_NO_FILE;\r
1507 }\r
1508 clst = create_chain(&dp->obj, dp->clust); /* Allocate a cluster */\r
53668523 1509 if (clst == 0) return FR_DENIED; /* No free cluster */\r
70702af1
L
1510 if (clst == 1) return FR_INT_ERR; /* Internal error */\r
1511 if (clst == 0xFFFFFFFF) return FR_DISK_ERR; /* Disk error */\r
1512 /* Clean-up the stretched table */\r
1513 if (_FS_EXFAT) dp->obj.stat |= 4; /* The directory needs to be updated */\r
1514 if (sync_window(fs) != FR_OK) return FR_DISK_ERR; /* Flush disk access window */\r
1515 mem_set(fs->win, 0, SS(fs)); /* Clear window buffer */\r
1516 for (n = 0, fs->winsect = clust2sect(fs, clst); n < fs->csize; n++, fs->winsect++) { /* Fill the new cluster with 0 */\r
1517 fs->wflag = 1;\r
1518 if (sync_window(fs) != FR_OK) return FR_DISK_ERR;\r
53668523 1519 }\r
70702af1 1520 fs->winsect -= n; /* Restore window offset */\r
53668523 1521#else\r
70702af1
L
1522 if (!stretch) dp->sect = 0; /* If no stretch, report EOT (this is to suppress warning) */\r
1523 dp->sect = 0; return FR_NO_FILE; /* Report EOT */\r
53668523
L
1524#endif\r
1525 }\r
70702af1
L
1526 dp->clust = clst; /* Initialize data for new cluster */\r
1527 dp->sect = clust2sect(fs, clst);\r
53668523
L
1528 }\r
1529 }\r
1530 }\r
70702af1
L
1531 dp->dptr = ofs; /* Current entry */\r
1532 dp->dir = fs->win + ofs % SS(fs); /* Pointer to the entry in the win[] */\r
53668523
L
1533\r
1534 return FR_OK;\r
1535}\r
1536\r
1537\r
1538\r
1539\r
70702af1 1540#if !_FS_READONLY\r
53668523 1541/*-----------------------------------------------------------------------*/\r
70702af1 1542/* Directory handling - Reserve a block of directory entries */\r
53668523
L
1543/*-----------------------------------------------------------------------*/\r
1544\r
53668523 1545static\r
70702af1
L
1546FRESULT dir_alloc ( /* FR_OK(0):succeeded, !=0:error */\r
1547 DIR* dp, /* Pointer to the directory object */\r
1548 UINT nent /* Number of contiguous entries to allocate */\r
53668523
L
1549)\r
1550{\r
1551 FRESULT res;\r
1552 UINT n;\r
70702af1 1553 FATFS *fs = dp->obj.fs;\r
53668523
L
1554\r
1555\r
1556 res = dir_sdi(dp, 0);\r
1557 if (res == FR_OK) {\r
1558 n = 0;\r
1559 do {\r
70702af1 1560 res = move_window(fs, dp->sect);\r
53668523 1561 if (res != FR_OK) break;\r
70702af1
L
1562#if _FS_EXFAT\r
1563 if ((fs->fs_type == FS_EXFAT) ? (int)((dp->dir[XDIR_Type] & 0x80) == 0) : (int)(dp->dir[DIR_Name] == DDEM || dp->dir[DIR_Name] == 0)) {\r
1564#else\r
1565 if (dp->dir[DIR_Name] == DDEM || dp->dir[DIR_Name] == 0) {\r
1566#endif\r
7b78a5a2 1567 if (++n == nent) break; /* A block of contiguous free entries is found */\r
53668523
L
1568 } else {\r
1569 n = 0; /* Not a blank entry. Restart to search */\r
1570 }\r
70702af1
L
1571 res = dir_next(dp, 1);\r
1572 } while (res == FR_OK); /* Next entry with table stretch enabled */\r
53668523 1573 }\r
70702af1 1574\r
53668523
L
1575 if (res == FR_NO_FILE) res = FR_DENIED; /* No directory entry to allocate */\r
1576 return res;\r
1577}\r
70702af1
L
1578\r
1579#endif /* !_FS_READONLY */\r
53668523
L
1580\r
1581\r
1582\r
1583\r
1584/*-----------------------------------------------------------------------*/\r
70702af1 1585/* FAT: Directory handling - Load/Store start cluster number */\r
53668523
L
1586/*-----------------------------------------------------------------------*/\r
1587\r
1588static\r
70702af1
L
1589DWORD ld_clust ( /* Returns the top cluster value of the SFN entry */\r
1590 FATFS* fs, /* Pointer to the fs object */\r
1591 const BYTE* dir /* Pointer to the key entry */\r
53668523
L
1592)\r
1593{\r
1594 DWORD cl;\r
1595\r
70702af1
L
1596 cl = ld_word(dir + DIR_FstClusLO);\r
1597 if (fs->fs_type == FS_FAT32) {\r
1598 cl |= (DWORD)ld_word(dir + DIR_FstClusHI) << 16;\r
1599 }\r
53668523
L
1600\r
1601 return cl;\r
1602}\r
1603\r
1604\r
1605#if !_FS_READONLY\r
1606static\r
1607void st_clust (\r
70702af1
L
1608 FATFS* fs, /* Pointer to the fs object */\r
1609 BYTE* dir, /* Pointer to the key entry */\r
53668523
L
1610 DWORD cl /* Value to be set */\r
1611)\r
1612{\r
70702af1
L
1613 st_word(dir + DIR_FstClusLO, (WORD)cl);\r
1614 if (fs->fs_type == FS_FAT32) {\r
1615 st_word(dir + DIR_FstClusHI, (WORD)(cl >> 16));\r
1616 }\r
53668523
L
1617}\r
1618#endif\r
1619\r
1620\r
1621\r
70702af1
L
1622#if _USE_LFN != 0\r
1623/*------------------------------------------------------------------------*/\r
1624/* FAT-LFN: LFN handling */\r
1625/*------------------------------------------------------------------------*/\r
53668523
L
1626static\r
1627const BYTE LfnOfs[] = {1,3,5,7,9,14,16,18,20,22,24,28,30}; /* Offset of LFN characters in the directory entry */\r
1628\r
1629\r
70702af1
L
1630/*--------------------------------------------------------*/\r
1631/* FAT-LFN: Compare a part of file name with an LFN entry */\r
1632/*--------------------------------------------------------*/\r
53668523 1633static\r
70702af1
L
1634int cmp_lfn ( /* 1:matched, 0:not matched */\r
1635 const WCHAR* lfnbuf, /* Pointer to the LFN working buffer to be compared */\r
1636 BYTE* dir /* Pointer to the directory entry containing the part of LFN */\r
53668523
L
1637)\r
1638{\r
1639 UINT i, s;\r
1640 WCHAR wc, uc;\r
1641\r
1642\r
70702af1
L
1643 if (ld_word(dir + LDIR_FstClusLO) != 0) return 0; /* Check LDIR_FstClusLO */\r
1644\r
1645 i = ((dir[LDIR_Ord] & 0x3F) - 1) * 13; /* Offset in the LFN buffer */\r
1646\r
1647 for (wc = 1, s = 0; s < 13; s++) { /* Process all characters in the entry */\r
1648 uc = ld_word(dir + LfnOfs[s]); /* Pick an LFN character */\r
1649 if (wc) {\r
1650 if (i >= _MAX_LFN || ff_wtoupper(uc) != ff_wtoupper(lfnbuf[i++])) { /* Compare it */\r
1651 return 0; /* Not matched */\r
1652 }\r
1653 wc = uc;\r
53668523 1654 } else {\r
70702af1 1655 if (uc != 0xFFFF) return 0; /* Check filler */\r
53668523 1656 }\r
70702af1 1657 }\r
53668523 1658\r
70702af1 1659 if ((dir[LDIR_Ord] & LLEF) && wc && lfnbuf[i]) return 0; /* Last segment matched but different length */\r
53668523 1660\r
70702af1 1661 return 1; /* The part of LFN matched */\r
53668523
L
1662}\r
1663\r
1664\r
70702af1
L
1665#if _FS_MINIMIZE <= 1 || _FS_RPATH >= 2 || _USE_LABEL || _FS_EXFAT\r
1666/*-----------------------------------------------------*/\r
1667/* FAT-LFN: Pick a part of file name from an LFN entry */\r
1668/*-----------------------------------------------------*/\r
53668523 1669static\r
70702af1
L
1670int pick_lfn ( /* 1:succeeded, 0:buffer overflow or invalid LFN entry */\r
1671 WCHAR* lfnbuf, /* Pointer to the LFN working buffer */\r
1672 BYTE* dir /* Pointer to the LFN entry */\r
53668523
L
1673)\r
1674{\r
1675 UINT i, s;\r
1676 WCHAR wc, uc;\r
1677\r
1678\r
70702af1
L
1679 if (ld_word(dir + LDIR_FstClusLO) != 0) return 0; /* Check LDIR_FstClusLO */\r
1680\r
53668523
L
1681 i = ((dir[LDIR_Ord] & 0x3F) - 1) * 13; /* Offset in the LFN buffer */\r
1682\r
70702af1
L
1683 for (wc = 1, s = 0; s < 13; s++) { /* Process all characters in the entry */\r
1684 uc = ld_word(dir + LfnOfs[s]); /* Pick an LFN character */\r
1685 if (wc) {\r
53668523
L
1686 if (i >= _MAX_LFN) return 0; /* Buffer overflow? */\r
1687 lfnbuf[i++] = wc = uc; /* Store it */\r
1688 } else {\r
1689 if (uc != 0xFFFF) return 0; /* Check filler */\r
1690 }\r
70702af1 1691 }\r
53668523 1692\r
70702af1 1693 if (dir[LDIR_Ord] & LLEF) { /* Put terminator if it is the last LFN part */\r
53668523
L
1694 if (i >= _MAX_LFN) return 0; /* Buffer overflow? */\r
1695 lfnbuf[i] = 0;\r
1696 }\r
1697\r
70702af1 1698 return 1; /* The part of LFN is valid */\r
53668523 1699}\r
70702af1 1700#endif\r
53668523
L
1701\r
1702\r
1703#if !_FS_READONLY\r
70702af1
L
1704/*-----------------------------------------*/\r
1705/* FAT-LFN: Create an entry of LFN entries */\r
1706/*-----------------------------------------*/\r
53668523 1707static\r
70702af1
L
1708void put_lfn (\r
1709 const WCHAR* lfn, /* Pointer to the LFN */\r
1710 BYTE* dir, /* Pointer to the LFN entry to be created */\r
1711 BYTE ord, /* LFN order (1-20) */\r
1712 BYTE sum /* Checksum of the corresponding SFN */\r
53668523
L
1713)\r
1714{\r
1715 UINT i, s;\r
1716 WCHAR wc;\r
1717\r
1718\r
70702af1 1719 dir[LDIR_Chksum] = sum; /* Set checksum */\r
53668523
L
1720 dir[LDIR_Attr] = AM_LFN; /* Set attribute. LFN entry */\r
1721 dir[LDIR_Type] = 0;\r
70702af1 1722 st_word(dir + LDIR_FstClusLO, 0);\r
53668523 1723\r
70702af1 1724 i = (ord - 1) * 13; /* Get offset in the LFN working buffer */\r
53668523
L
1725 s = wc = 0;\r
1726 do {\r
70702af1
L
1727 if (wc != 0xFFFF) wc = lfn[i++]; /* Get an effective character */\r
1728 st_word(dir + LfnOfs[s], wc); /* Put it */\r
1729 if (wc == 0) wc = 0xFFFF; /* Padding characters for left locations */\r
53668523 1730 } while (++s < 13);\r
70702af1 1731 if (wc == 0xFFFF || !lfn[i]) ord |= LLEF; /* Last LFN part is the start of LFN sequence */\r
53668523
L
1732 dir[LDIR_Ord] = ord; /* Set the LFN order */\r
1733}\r
1734\r
70702af1
L
1735#endif /* !_FS_READONLY */\r
1736#endif /* _USE_LFN != 0 */\r
53668523
L
1737\r
1738\r
1739\r
70702af1 1740#if _USE_LFN != 0 && !_FS_READONLY\r
53668523 1741/*-----------------------------------------------------------------------*/\r
70702af1 1742/* FAT-LFN: Create a Numbered SFN */\r
53668523 1743/*-----------------------------------------------------------------------*/\r
70702af1 1744\r
53668523
L
1745static\r
1746void gen_numname (\r
1747 BYTE* dst, /* Pointer to the buffer to store numbered SFN */\r
1748 const BYTE* src, /* Pointer to SFN */\r
1749 const WCHAR* lfn, /* Pointer to LFN */\r
1750 UINT seq /* Sequence number */\r
1751)\r
1752{\r
1753 BYTE ns[8], c;\r
1754 UINT i, j;\r
70702af1
L
1755 WCHAR wc;\r
1756 DWORD sr;\r
53668523
L
1757\r
1758\r
1759 mem_cpy(dst, src, 11);\r
1760\r
70702af1
L
1761 if (seq > 5) { /* In case of many collisions, generate a hash number instead of sequential number */\r
1762 sr = seq;\r
53668523
L
1763 while (*lfn) { /* Create a CRC */\r
1764 wc = *lfn++;\r
1765 for (i = 0; i < 16; i++) {\r
1766 sr = (sr << 1) + (wc & 1);\r
1767 wc >>= 1;\r
1768 if (sr & 0x10000) sr ^= 0x11021;\r
1769 }\r
1770 }\r
1771 seq = (UINT)sr;\r
1772 }\r
1773\r
1774 /* itoa (hexdecimal) */\r
1775 i = 7;\r
1776 do {\r
70702af1 1777 c = (BYTE)((seq % 16) + '0');\r
53668523
L
1778 if (c > '9') c += 7;\r
1779 ns[i--] = c;\r
1780 seq /= 16;\r
1781 } while (seq);\r
1782 ns[i] = '~';\r
1783\r
1784 /* Append the number */\r
1785 for (j = 0; j < i && dst[j] != ' '; j++) {\r
1786 if (IsDBCS1(dst[j])) {\r
1787 if (j == i - 1) break;\r
1788 j++;\r
1789 }\r
1790 }\r
1791 do {\r
1792 dst[j++] = (i < 8) ? ns[i++] : ' ';\r
1793 } while (j < 8);\r
1794}\r
70702af1 1795#endif /* _USE_LFN != 0 && !_FS_READONLY */\r
53668523
L
1796\r
1797\r
1798\r
70702af1 1799#if _USE_LFN != 0\r
53668523 1800/*-----------------------------------------------------------------------*/\r
70702af1 1801/* FAT-LFN: Calculate checksum of an SFN entry */\r
53668523 1802/*-----------------------------------------------------------------------*/\r
70702af1 1803\r
53668523
L
1804static\r
1805BYTE sum_sfn (\r
1806 const BYTE* dir /* Pointer to the SFN entry */\r
1807)\r
1808{\r
1809 BYTE sum = 0;\r
1810 UINT n = 11;\r
1811\r
1812 do sum = (sum >> 1) + (sum << 7) + *dir++; while (--n);\r
1813 return sum;\r
1814}\r
53668523 1815\r
70702af1 1816#endif /* _USE_LFN != 0 */\r
53668523
L
1817\r
1818\r
1819\r
70702af1 1820#if _FS_EXFAT\r
53668523 1821/*-----------------------------------------------------------------------*/\r
70702af1 1822/* exFAT: Checksum */\r
53668523
L
1823/*-----------------------------------------------------------------------*/\r
1824\r
1825static\r
70702af1
L
1826WORD xdir_sum ( /* Get checksum of the directoly block */\r
1827 const BYTE* dir /* Directory entry block to be calculated */\r
53668523
L
1828)\r
1829{\r
70702af1
L
1830 UINT i, szblk;\r
1831 WORD sum;\r
1832\r
1833\r
1834 szblk = (dir[XDIR_NumSec] + 1) * SZDIRE;\r
1835 for (i = sum = 0; i < szblk; i++) {\r
1836 if (i == XDIR_SetSum) { /* Skip sum field */\r
1837 i++;\r
1838 } else {\r
1839 sum = ((sum & 1) ? 0x8000 : 0) + (sum >> 1) + dir[i];\r
1840 }\r
1841 }\r
1842 return sum;\r
1843}\r
1844\r
1845\r
1846\r
1847static\r
1848WORD xname_sum ( /* Get check sum (to be used as hash) of the name */\r
1849 const WCHAR* name /* File name to be calculated */\r
1850)\r
1851{\r
1852 WCHAR chr;\r
1853 WORD sum = 0;\r
1854\r
1855\r
1856 while ((chr = *name++) != 0) {\r
1857 chr = ff_wtoupper(chr); /* File name needs to be ignored case */\r
1858 sum = ((sum & 1) ? 0x8000 : 0) + (sum >> 1) + (chr & 0xFF);\r
1859 sum = ((sum & 1) ? 0x8000 : 0) + (sum >> 1) + (chr >> 8);\r
1860 }\r
1861 return sum;\r
1862}\r
1863\r
1864\r
1865#if !_FS_READONLY && _USE_MKFS\r
1866static\r
1867DWORD xsum32 (\r
1868 BYTE dat, /* Data to be sumed */\r
1869 DWORD sum /* Previous value */\r
1870)\r
1871{\r
1872 sum = ((sum & 1) ? 0x80000000 : 0) + (sum >> 1) + dat;\r
1873 return sum;\r
1874}\r
53668523
L
1875#endif\r
1876\r
70702af1
L
1877\r
1878#if _FS_MINIMIZE <= 1 || _FS_RPATH >= 2\r
1879/*------------------------------------------------------*/\r
1880/* exFAT: Get object information from a directory block */\r
1881/*------------------------------------------------------*/\r
1882\r
1883static\r
1884void get_xdir_info (\r
1885 BYTE* dirb, /* Pointer to the direcotry entry block 85+C0+C1s */\r
1886 FILINFO* fno /* Buffer to store the extracted file information */\r
1887)\r
1888{\r
1889 UINT di, si;\r
1890 WCHAR w;\r
1891#if !_LFN_UNICODE\r
1892 UINT nc;\r
1893#endif\r
1894\r
1895 /* Get file name */\r
1896#if _LFN_UNICODE\r
1897 if (dirb[XDIR_NumName] <= _MAX_LFN) {\r
1898 for (si = SZDIRE * 2, di = 0; di < dirb[XDIR_NumName]; si += 2, di++) {\r
1899 if ((si % SZDIRE) == 0) si += 2; /* Skip entry type field */\r
1900 w = ld_word(dirb + si); /* Get a character */\r
1901 fno->fname[di] = w; /* Store it */\r
1902 }\r
1903 } else {\r
1904 di = 0; /* Buffer overflow and inaccessible object */\r
1905 }\r
1906#else\r
1907 for (si = SZDIRE * 2, di = nc = 0; nc < dirb[XDIR_NumName]; si += 2, nc++) {\r
1908 if ((si % SZDIRE) == 0) si += 2; /* Skip entry type field */\r
1909 w = ld_word(dirb + si); /* Get a character */\r
1910 w = ff_convert(w, 0); /* Unicode -> OEM */\r
1911 if (w == 0) { di = 0; break; } /* Could not be converted and inaccessible object */\r
1912 if (_DF1S && w >= 0x100) { /* Put 1st byte if it is a DBC (always false at SBCS cfg) */\r
1913 fno->fname[di++] = (char)(w >> 8);\r
1914 }\r
1915 if (di >= _MAX_LFN) { di = 0; break; } /* Buffer overflow and inaccessible object */\r
1916 fno->fname[di++] = (char)w;\r
1917 }\r
1918#endif\r
1919 if (di == 0) fno->fname[di++] = '?'; /* Inaccessible object? */\r
1920 fno->fname[di] = 0; /* Terminate file name */\r
1921\r
1922 fno->altname[0] = 0; /* No SFN */\r
1923 fno->fattrib = dirb[XDIR_Attr]; /* Attribute */\r
1924 fno->fsize = (fno->fattrib & AM_DIR) ? 0 : ld_qword(dirb + XDIR_FileSize); /* Size */\r
1925 fno->ftime = ld_word(dirb + XDIR_ModTime + 0); /* Time */\r
1926 fno->fdate = ld_word(dirb + XDIR_ModTime + 2); /* Date */\r
1927}\r
1928\r
1929#endif /* _FS_MINIMIZE <= 1 || _FS_RPATH >= 2 */\r
1930\r
1931\r
1932/*-----------------------------------*/\r
1933/* exFAT: Get a directry entry block */\r
1934/*-----------------------------------*/\r
1935\r
1936static\r
1937FRESULT load_xdir ( /* FR_INT_ERR: invalid entry block */\r
1938 DIR* dp /* Pointer to the reading direcotry object pointing the 85 entry */\r
1939)\r
1940{\r
1941 FRESULT res;\r
1942 UINT i, nent;\r
1943 BYTE* dirb = dp->obj.fs->dirbuf; /* Pointer to the on-memory direcotry entry block 85+C0+C1s */\r
1944\r
1945\r
1946 /* Load 85 entry */\r
1947 res = move_window(dp->obj.fs, dp->sect);\r
1948 if (res != FR_OK) return res;\r
1949 if (dp->dir[XDIR_Type] != 0x85) return FR_INT_ERR;\r
1950 mem_cpy(dirb, dp->dir, SZDIRE);\r
1951 nent = dirb[XDIR_NumSec] + 1;\r
1952\r
1953 /* Load C0 entry */\r
1954 res = dir_next(dp, 0);\r
1955 if (res != FR_OK) return res;\r
1956 res = move_window(dp->obj.fs, dp->sect);\r
53668523 1957 if (res != FR_OK) return res;\r
70702af1
L
1958 if (dp->dir[XDIR_Type] != 0xC0) return FR_INT_ERR;\r
1959 mem_cpy(dirb + SZDIRE, dp->dir, SZDIRE);\r
1960\r
1961 /* Load C1 entries */\r
1962 if (nent < 3 || nent > 19) return FR_NO_FILE;\r
1963 i = SZDIRE * 2; nent *= SZDIRE;\r
1964 do {\r
1965 res = dir_next(dp, 0);\r
1966 if (res != FR_OK) return res;\r
1967 res = move_window(dp->obj.fs, dp->sect);\r
1968 if (res != FR_OK) return res;\r
1969 if (dp->dir[XDIR_Type] != 0xC1) return FR_INT_ERR;\r
1970 mem_cpy(dirb + i, dp->dir, SZDIRE);\r
1971 i += SZDIRE;\r
1972 } while (i < nent);\r
1973\r
1974 /* Sanity check */\r
1975 if (xdir_sum(dirb) != ld_word(dirb + XDIR_SetSum)) return FR_INT_ERR;\r
1976\r
1977 return FR_OK;\r
1978}\r
1979\r
53668523 1980\r
70702af1
L
1981#if !_FS_READONLY || _FS_RPATH != 0 \r
1982/*------------------------------------------------*/\r
1983/* exFAT: Load the object's directory entry block */\r
1984/*------------------------------------------------*/\r
1985static\r
1986FRESULT load_obj_dir ( \r
1987 DIR* dp, /* Blank directory object to be used to access containing direcotry */\r
1988 const _FDID* obj /* Object with containing directory information */\r
1989)\r
1990{\r
1991 FRESULT res;\r
1992\r
1993\r
1994 /* Open object containing directory */\r
1995 dp->obj.fs = obj->fs;\r
1996 dp->obj.sclust = obj->c_scl;\r
1997 dp->obj.stat = (BYTE)obj->c_size;\r
1998 dp->obj.objsize = obj->c_size & 0xFFFFFF00;\r
1999 dp->blk_ofs = obj->c_ofs;\r
2000\r
2001 res = dir_sdi(dp, dp->blk_ofs); /* Goto the block location */\r
2002 if (res == FR_OK) {\r
2003 res = load_xdir(dp); /* Load the object's entry block */\r
2004 }\r
2005 return res;\r
2006}\r
53668523 2007#endif\r
70702af1
L
2008\r
2009\r
2010#if !_FS_READONLY\r
2011/*-----------------------------------------------*/\r
2012/* exFAT: Store the directory block to the media */\r
2013/*-----------------------------------------------*/\r
2014static\r
2015FRESULT store_xdir (\r
2016 DIR* dp /* Pointer to the direcotry object */\r
2017)\r
2018{\r
2019 FRESULT res;\r
2020 UINT nent;\r
2021 BYTE* dirb = dp->obj.fs->dirbuf; /* Pointer to the direcotry entry block 85+C0+C1s */\r
2022\r
2023 /* Create set sum */\r
2024 st_word(dirb + XDIR_SetSum, xdir_sum(dirb));\r
2025 nent = dirb[XDIR_NumSec] + 1;\r
2026\r
2027 /* Store the set of directory to the volume */\r
2028 res = dir_sdi(dp, dp->blk_ofs);\r
2029 while (res == FR_OK) {\r
2030 res = move_window(dp->obj.fs, dp->sect);\r
2031 if (res != FR_OK) break;\r
2032 mem_cpy(dp->dir, dirb, SZDIRE);\r
2033 dp->obj.fs->wflag = 1;\r
2034 if (--nent == 0) break;\r
2035 dirb += SZDIRE;\r
2036 res = dir_next(dp, 0);\r
2037 }\r
2038 return (res == FR_OK || res == FR_DISK_ERR) ? res : FR_INT_ERR;\r
2039}\r
2040\r
2041\r
2042\r
2043/*-------------------------------------------*/\r
2044/* exFAT: Create a new directory enrty block */\r
2045/*-------------------------------------------*/\r
2046\r
2047static\r
2048void create_xdir (\r
2049 BYTE* dirb, /* Pointer to the direcotry entry block buffer */\r
2050 const WCHAR* lfn /* Pointer to the nul terminated file name */\r
2051)\r
2052{\r
2053 UINT i;\r
2054 BYTE nb, nc;\r
2055 WCHAR chr;\r
2056\r
2057\r
2058 mem_set(dirb, 0, 2 * SZDIRE); /* Initialize 85+C0 entry */\r
2059 dirb[XDIR_Type] = 0x85;\r
2060 dirb[XDIR_Type + SZDIRE] = 0xC0;\r
2061 st_word(dirb + XDIR_NameHash, xname_sum(lfn)); /* Set name hash */\r
2062\r
2063 i = SZDIRE * 2; /* C1 offset */\r
2064 nc = 0; nb = 1; chr = 1;\r
53668523 2065 do {\r
70702af1
L
2066 dirb[i++] = 0xC1; dirb[i++] = 0; /* Entry type C1 */\r
2067 do { /* Fill name field */\r
2068 if (chr && (chr = lfn[nc]) != 0) nc++; /* Get a character if exist */\r
2069 st_word(dirb + i, chr); i += 2; /* Store it */\r
2070 } while (i % SZDIRE);\r
2071 nb++;\r
2072 } while (lfn[nc]); /* Fill next entry if any char follows */\r
2073\r
2074 dirb[XDIR_NumName] = nc; /* Set name length */\r
2075 dirb[XDIR_NumSec] = nb; /* Set number of C0+C1s */\r
2076}\r
2077\r
2078#endif /* !_FS_READONLY */\r
2079#endif /* _FS_EXFAT */\r
2080\r
2081\r
2082\r
2083#if _FS_MINIMIZE <= 1 || _FS_RPATH >= 2 || _USE_LABEL || _FS_EXFAT\r
2084/*-----------------------------------------------------------------------*/\r
2085/* Read an object from the directory */\r
2086/*-----------------------------------------------------------------------*/\r
2087\r
2088static\r
2089FRESULT dir_read (\r
2090 DIR* dp, /* Pointer to the directory object */\r
2091 int vol /* Filtered by 0:file/directory or 1:volume label */\r
2092)\r
2093{\r
2094 FRESULT res = FR_NO_FILE;\r
2095 FATFS *fs = dp->obj.fs;\r
2096 BYTE a, c;\r
2097#if _USE_LFN != 0\r
2098 BYTE ord = 0xFF, sum = 0xFF;\r
2099#endif\r
2100\r
2101 while (dp->sect) {\r
2102 res = move_window(fs, dp->sect);\r
53668523 2103 if (res != FR_OK) break;\r
70702af1
L
2104 c = dp->dir[DIR_Name]; /* Test for the entry type */\r
2105 if (c == 0) { res = FR_NO_FILE; break; } /* Reached to end of the directory */\r
2106#if _FS_EXFAT\r
2107 if (fs->fs_type == FS_EXFAT) { /* On the exFAT volume */\r
2108 if (_USE_LABEL && vol) {\r
2109 if (c == 0x83) break; /* Volume label entry? */\r
2110 } else {\r
2111 if (c == 0x85) { /* Start of the file entry block? */\r
2112 dp->blk_ofs = dp->dptr; /* Get location of the block */\r
2113 res = load_xdir(dp); /* Load the entry block */\r
2114 if (res == FR_OK) {\r
2115 dp->obj.attr = fs->dirbuf[XDIR_Attr] & AM_MASK; /* Get attribute */\r
53668523 2116 }\r
70702af1
L
2117 break;\r
2118 }\r
2119 }\r
2120 } else\r
2121#endif\r
2122 { /* On the FAT12/16/32 volume */\r
2123 dp->obj.attr = a = dp->dir[DIR_Attr] & AM_MASK; /* Get attribute */\r
2124#if _USE_LFN != 0 /* LFN configuration */\r
2125 if (c == DDEM || c == '.' || (int)((a & ~AM_ARC) == AM_VOL) != vol) { /* An entry without valid data */\r
2126 ord = 0xFF;\r
2127 } else {\r
2128 if (a == AM_LFN) { /* An LFN entry is found */\r
2129 if (c & LLEF) { /* Is it start of an LFN sequence? */\r
2130 sum = dp->dir[LDIR_Chksum];\r
2131 c &= (BYTE)~LLEF; ord = c;\r
2132 dp->blk_ofs = dp->dptr;\r
2133 }\r
2134 /* Check LFN validity and capture it */\r
2135 ord = (c == ord && sum == dp->dir[LDIR_Chksum] && pick_lfn(fs->lfnbuf, dp->dir)) ? ord - 1 : 0xFF;\r
2136 } else { /* An SFN entry is found */\r
2137 if (ord || sum != sum_sfn(dp->dir)) { /* Is there a valid LFN? */\r
2138 dp->blk_ofs = 0xFFFFFFFF; /* It has no LFN. */\r
2139 }\r
2140 break;\r
53668523 2141 }\r
53668523 2142 }\r
53668523 2143#else /* Non LFN configuration */\r
70702af1
L
2144 if (c != DDEM && c != '.' && a != AM_LFN && (int)((a & ~AM_ARC) == AM_VOL) == vol) { /* Is it a valid entry? */\r
2145 break;\r
2146 }\r
53668523 2147#endif\r
70702af1 2148 }\r
53668523 2149 res = dir_next(dp, 0); /* Next entry */\r
70702af1
L
2150 if (res != FR_OK) break;\r
2151 }\r
53668523 2152\r
70702af1 2153 if (res != FR_OK) dp->sect = 0; /* Terminate the read operation on error or EOT */\r
53668523
L
2154 return res;\r
2155}\r
2156\r
70702af1 2157#endif /* _FS_MINIMIZE <= 1 || _USE_LABEL || _FS_RPATH >= 2 */\r
53668523
L
2158\r
2159\r
2160\r
2161/*-----------------------------------------------------------------------*/\r
70702af1 2162/* Directory handling - Find an object in the directory */\r
53668523 2163/*-----------------------------------------------------------------------*/\r
70702af1 2164\r
53668523 2165static\r
70702af1
L
2166FRESULT dir_find ( /* FR_OK(0):succeeded, !=0:error */\r
2167 DIR* dp /* Pointer to the directory object with the file name */\r
53668523
L
2168)\r
2169{\r
2170 FRESULT res;\r
70702af1
L
2171 FATFS *fs = dp->obj.fs;\r
2172 BYTE c;\r
2173#if _USE_LFN != 0\r
2174 BYTE a, ord, sum;\r
53668523
L
2175#endif\r
2176\r
70702af1
L
2177 res = dir_sdi(dp, 0); /* Rewind directory object */\r
2178 if (res != FR_OK) return res;\r
2179#if _FS_EXFAT\r
2180 if (fs->fs_type == FS_EXFAT) { /* On the exFAT volume */\r
2181 BYTE nc;\r
2182 UINT di, ni;\r
2183 WORD hash = xname_sum(fs->lfnbuf); /* Hash value of the name to find */\r
2184\r
2185 while ((res = dir_read(dp, 0)) == FR_OK) { /* Read an item */\r
2186 if (ld_word(fs->dirbuf + XDIR_NameHash) != hash) continue; /* Skip the comparison if hash value mismatched */\r
2187 for (nc = fs->dirbuf[XDIR_NumName], di = SZDIRE * 2, ni = 0; nc; nc--, di += 2, ni++) { /* Compare the name */\r
2188 if ((di % SZDIRE) == 0) di += 2;\r
2189 if (ff_wtoupper(ld_word(fs->dirbuf + di)) != ff_wtoupper(fs->lfnbuf[ni])) break;\r
2190 }\r
2191 if (nc == 0 && !fs->lfnbuf[ni]) break; /* Name matched? */\r
2192 }\r
2193 return res;\r
2194 }\r
2195#endif\r
2196 /* On the FAT12/16/32 volume */\r
2197#if _USE_LFN != 0\r
2198 ord = sum = 0xFF; dp->blk_ofs = 0xFFFFFFFF; /* Reset LFN sequence */\r
2199#endif\r
2200 do {\r
2201 res = move_window(fs, dp->sect);\r
53668523 2202 if (res != FR_OK) break;\r
70702af1 2203 c = dp->dir[DIR_Name];\r
53668523 2204 if (c == 0) { res = FR_NO_FILE; break; } /* Reached to end of table */\r
70702af1
L
2205#if _USE_LFN != 0 /* LFN configuration */\r
2206 dp->obj.attr = a = dp->dir[DIR_Attr] & AM_MASK;\r
2207 if (c == DDEM || ((a & AM_VOL) && a != AM_LFN)) { /* An entry without valid data */\r
2208 ord = 0xFF; dp->blk_ofs = 0xFFFFFFFF; /* Reset LFN sequence */\r
53668523
L
2209 } else {\r
2210 if (a == AM_LFN) { /* An LFN entry is found */\r
70702af1
L
2211 if (!(dp->fn[NSFLAG] & NS_NOLFN)) {\r
2212 if (c & LLEF) { /* Is it start of LFN sequence? */\r
2213 sum = dp->dir[LDIR_Chksum];\r
2214 c &= (BYTE)~LLEF; ord = c; /* LFN start order */\r
2215 dp->blk_ofs = dp->dptr; /* Start offset of LFN */\r
2216 }\r
2217 /* Check validity of the LFN entry and compare it with given name */\r
2218 ord = (c == ord && sum == dp->dir[LDIR_Chksum] && cmp_lfn(fs->lfnbuf, dp->dir)) ? ord - 1 : 0xFF;\r
53668523 2219 }\r
53668523 2220 } else { /* An SFN entry is found */\r
70702af1
L
2221 if (!ord && sum == sum_sfn(dp->dir)) break; /* LFN matched? */\r
2222 if (!(dp->fn[NSFLAG] & NS_LOSS) && !mem_cmp(dp->dir, dp->fn, 11)) break; /* SFN matched? */\r
2223 ord = 0xFF; dp->blk_ofs = 0xFFFFFFFF; /* Reset LFN sequence */\r
53668523
L
2224 }\r
2225 }\r
2226#else /* Non LFN configuration */\r
70702af1
L
2227 dp->obj.attr = dp->dir[DIR_Attr] & AM_MASK;\r
2228 if (!(dp->dir[DIR_Attr] & AM_VOL) && !mem_cmp(dp->dir, dp->fn, 11)) break; /* Is it a valid entry? */\r
53668523 2229#endif\r
70702af1
L
2230 res = dir_next(dp, 0); /* Next entry */\r
2231 } while (res == FR_OK);\r
53668523
L
2232\r
2233 return res;\r
2234}\r
53668523
L
2235\r
2236\r
2237\r
2238\r
70702af1 2239#if !_FS_READONLY\r
53668523
L
2240/*-----------------------------------------------------------------------*/\r
2241/* Register an object to the directory */\r
2242/*-----------------------------------------------------------------------*/\r
70702af1 2243\r
53668523 2244static\r
70702af1 2245FRESULT dir_register ( /* FR_OK:succeeded, FR_DENIED:no free entry or too many SFN collision, FR_DISK_ERR:disk error */\r
53668523
L
2246 DIR* dp /* Target directory with object name to be created */\r
2247)\r
2248{\r
2249 FRESULT res;\r
70702af1
L
2250 FATFS *fs = dp->obj.fs;\r
2251#if _USE_LFN != 0 /* LFN configuration */\r
2252 UINT n, nlen, nent;\r
2253 BYTE sn[12], sum;\r
2254\r
2255\r
2256 if (dp->fn[NSFLAG] & (NS_DOT | NS_NONAME)) return FR_INVALID_NAME; /* Check name validity */\r
2257 for (nlen = 0; fs->lfnbuf[nlen]; nlen++) ; /* Get lfn length */\r
2258\r
2259#if _FS_EXFAT\r
2260 if (fs->fs_type == FS_EXFAT) { /* On the exFAT volume */\r
2261 DIR dj;\r
2262\r
2263 nent = (nlen + 14) / 15 + 2; /* Number of entries to allocate (85+C0+C1s) */\r
2264 res = dir_alloc(dp, nent); /* Allocate entries */\r
2265 if (res != FR_OK) return res;\r
2266 dp->blk_ofs = dp->dptr - SZDIRE * (nent - 1); /* Set block position */\r
2267\r
2268 if (dp->obj.sclust != 0 && (dp->obj.stat & 4)) { /* Has the sub-directory been stretched? */\r
2269 dp->obj.stat &= 3;\r
2270 dp->obj.objsize += (DWORD)fs->csize * SS(fs); /* Increase object size by cluster size */\r
2271 res = fill_fat_chain(&dp->obj); /* Complement FAT chain if needed */\r
2272 if (res != FR_OK) return res;\r
2273 res = load_obj_dir(&dj, &dp->obj);\r
2274 if (res != FR_OK) return res; /* Load the object status */\r
2275 st_qword(fs->dirbuf + XDIR_FileSize, dp->obj.objsize); /* Update the allocation status */\r
2276 st_qword(fs->dirbuf + XDIR_ValidFileSize, dp->obj.objsize);\r
2277 fs->dirbuf[XDIR_GenFlags] = dp->obj.stat | 1;\r
2278 res = store_xdir(&dj); /* Store the object status */\r
2279 if (res != FR_OK) return res;\r
2280 }\r
53668523 2281\r
70702af1
L
2282 create_xdir(fs->dirbuf, fs->lfnbuf); /* Create on-memory directory block to be written later */\r
2283 return FR_OK;\r
2284 }\r
2285#endif\r
2286 /* On the FAT12/16/32 volume */\r
2287 mem_cpy(sn, dp->fn, 12);\r
7b78a5a2 2288 if (sn[NSFLAG] & NS_LOSS) { /* When LFN is out of 8.3 format, generate a numbered name */\r
70702af1 2289 dp->fn[NSFLAG] = NS_NOLFN; /* Find only SFN */\r
53668523 2290 for (n = 1; n < 100; n++) {\r
70702af1 2291 gen_numname(dp->fn, sn, fs->lfnbuf, n); /* Generate a numbered name */\r
53668523
L
2292 res = dir_find(dp); /* Check if the name collides with existing SFN */\r
2293 if (res != FR_OK) break;\r
2294 }\r
2295 if (n == 100) return FR_DENIED; /* Abort if too many collisions */\r
2296 if (res != FR_NO_FILE) return res; /* Abort if the result is other than 'not collided' */\r
70702af1 2297 dp->fn[NSFLAG] = sn[NSFLAG];\r
53668523
L
2298 }\r
2299\r
70702af1
L
2300 /* Create an SFN with/without LFNs. */\r
2301 nent = (sn[NSFLAG] & NS_LFN) ? (nlen + 12) / 13 + 1 : 1; /* Number of entries to allocate */\r
53668523 2302 res = dir_alloc(dp, nent); /* Allocate entries */\r
53668523 2303 if (res == FR_OK && --nent) { /* Set LFN entry if needed */\r
70702af1 2304 res = dir_sdi(dp, dp->dptr - nent * SZDIRE);\r
53668523 2305 if (res == FR_OK) {\r
70702af1 2306 sum = sum_sfn(dp->fn); /* Checksum value of the SFN tied to the LFN */\r
53668523 2307 do { /* Store LFN entries in bottom first */\r
70702af1 2308 res = move_window(fs, dp->sect);\r
53668523 2309 if (res != FR_OK) break;\r
70702af1
L
2310 put_lfn(fs->lfnbuf, dp->dir, (BYTE)nent, sum);\r
2311 fs->wflag = 1;\r
53668523
L
2312 res = dir_next(dp, 0); /* Next entry */\r
2313 } while (res == FR_OK && --nent);\r
2314 }\r
2315 }\r
70702af1 2316\r
53668523
L
2317#else /* Non LFN configuration */\r
2318 res = dir_alloc(dp, 1); /* Allocate an entry for SFN */\r
70702af1 2319\r
53668523
L
2320#endif\r
2321\r
70702af1
L
2322 /* Set SFN entry */\r
2323 if (res == FR_OK) {\r
2324 res = move_window(fs, dp->sect);\r
53668523 2325 if (res == FR_OK) {\r
70702af1
L
2326 mem_set(dp->dir, 0, SZDIRE); /* Clean the entry */\r
2327 mem_cpy(dp->dir + DIR_Name, dp->fn, 11); /* Put SFN */\r
2328#if _USE_LFN != 0\r
7b78a5a2 2329 dp->dir[DIR_NTres] = dp->fn[NSFLAG] & (NS_BODY | NS_EXT); /* Put NT flag */\r
53668523 2330#endif\r
70702af1 2331 fs->wflag = 1;\r
53668523
L
2332 }\r
2333 }\r
2334\r
2335 return res;\r
2336}\r
53668523 2337\r
70702af1 2338#endif /* !_FS_READONLY */\r
53668523
L
2339\r
2340\r
2341\r
70702af1 2342#if !_FS_READONLY && _FS_MINIMIZE == 0\r
53668523
L
2343/*-----------------------------------------------------------------------*/\r
2344/* Remove an object from the directory */\r
2345/*-----------------------------------------------------------------------*/\r
70702af1 2346\r
53668523 2347static\r
70702af1 2348FRESULT dir_remove ( /* FR_OK:Succeeded, FR_DISK_ERR:A disk error */\r
53668523
L
2349 DIR* dp /* Directory object pointing the entry to be removed */\r
2350)\r
2351{\r
2352 FRESULT res;\r
70702af1
L
2353 FATFS *fs = dp->obj.fs;\r
2354#if _USE_LFN != 0 /* LFN configuration */\r
2355 DWORD last = dp->dptr;\r
53668523 2356\r
70702af1 2357 res = (dp->blk_ofs == 0xFFFFFFFF) ? FR_OK : dir_sdi(dp, dp->blk_ofs); /* Goto top of the entry block if LFN is exist */\r
53668523
L
2358 if (res == FR_OK) {\r
2359 do {\r
70702af1 2360 res = move_window(fs, dp->sect);\r
53668523 2361 if (res != FR_OK) break;\r
70702af1
L
2362 /* Mark an entry 'deleted' */\r
2363 if (_FS_EXFAT && fs->fs_type == FS_EXFAT) { /* On the exFAT volume */\r
2364 dp->dir[XDIR_Type] &= 0x7F;\r
2365 } else { /* On the FAT12/16/32 volume */\r
2366 dp->dir[DIR_Name] = DDEM;\r
2367 }\r
2368 fs->wflag = 1;\r
2369 if (dp->dptr >= last) break; /* If reached last entry then all entries of the object has been deleted. */\r
2370 res = dir_next(dp, 0); /* Next entry */\r
53668523
L
2371 } while (res == FR_OK);\r
2372 if (res == FR_NO_FILE) res = FR_INT_ERR;\r
2373 }\r
53668523 2374#else /* Non LFN configuration */\r
70702af1
L
2375\r
2376 res = move_window(fs, dp->sect);\r
53668523 2377 if (res == FR_OK) {\r
70702af1
L
2378 dp->dir[DIR_Name] = DDEM;\r
2379 fs->wflag = 1;\r
53668523
L
2380 }\r
2381#endif\r
2382\r
2383 return res;\r
2384}\r
53668523 2385\r
70702af1 2386#endif /* !_FS_READONLY && _FS_MINIMIZE == 0 */\r
53668523
L
2387\r
2388\r
2389\r
70702af1 2390#if _FS_MINIMIZE <= 1 || _FS_RPATH >= 2\r
53668523
L
2391/*-----------------------------------------------------------------------*/\r
2392/* Get file information from directory entry */\r
2393/*-----------------------------------------------------------------------*/\r
70702af1 2394\r
53668523
L
2395static\r
2396void get_fileinfo ( /* No return code */\r
2397 DIR* dp, /* Pointer to the directory object */\r
2398 FILINFO* fno /* Pointer to the file information to be filled */\r
2399)\r
2400{\r
70702af1
L
2401 UINT i, j;\r
2402 TCHAR c;\r
2403 DWORD tm;\r
2404#if _USE_LFN != 0\r
2405 WCHAR w, lfv;\r
2406 FATFS *fs = dp->obj.fs;\r
2407#endif\r
53668523
L
2408\r
2409\r
70702af1
L
2410 fno->fname[0] = 0; /* Invaidate file info */\r
2411 if (!dp->sect) return; /* Exit if read pointer has reached end of directory */\r
53668523 2412\r
70702af1
L
2413#if _USE_LFN != 0 /* LFN configuration */\r
2414#if _FS_EXFAT\r
2415 if (fs->fs_type == FS_EXFAT) { /* On the exFAT volume */\r
2416 get_xdir_info(fs->dirbuf, fno);\r
2417 return;\r
2418 } else\r
53668523 2419#endif\r
70702af1
L
2420 { /* On the FAT12/16/32 volume */\r
2421 if (dp->blk_ofs != 0xFFFFFFFF) { /* Get LFN if available */\r
2422 i = j = 0;\r
2423 while ((w = fs->lfnbuf[j++]) != 0) { /* Get an LFN character */\r
2424#if !_LFN_UNICODE\r
2425 w = ff_convert(w, 0); /* Unicode -> OEM */\r
2426 if (w == 0) { i = 0; break; } /* No LFN if it could not be converted */\r
2427 if (_DF1S && w >= 0x100) { /* Put 1st byte if it is a DBC (always false at SBCS cfg) */\r
2428 fno->fname[i++] = (char)(w >> 8);\r
2429 }\r
53668523 2430#endif\r
70702af1
L
2431 if (i >= _MAX_LFN) { i = 0; break; } /* No LFN if buffer overflow */\r
2432 fno->fname[i++] = (TCHAR)w;\r
2433 }\r
2434 fno->fname[i] = 0; /* Terminate the LFN */\r
53668523 2435 }\r
53668523 2436 }\r
53668523 2437\r
70702af1
L
2438 i = j = 0;\r
2439 lfv = fno->fname[i]; /* LFN is exist if non-zero */\r
2440 while (i < 11) { /* Copy name body and extension */\r
2441 c = (TCHAR)dp->dir[i++];\r
2442 if (c == ' ') continue; /* Skip padding spaces */\r
2443 if (c == RDDEM) c = (TCHAR)DDEM; /* Restore replaced DDEM character */\r
2444 if (i == 9) { /* Insert a . if extension is exist */\r
2445 if (!lfv) fno->fname[j] = '.';\r
2446 fno->altname[j++] = '.';\r
2447 }\r
2448#if _LFN_UNICODE\r
2449 if (IsDBCS1(c) && i != 8 && i != 11 && IsDBCS2(dp->dir[i])) {\r
2450 c = c << 8 | dp->dir[i++];\r
2451 }\r
2452 c = ff_convert(c, 1); /* OEM -> Unicode */\r
2453 if (!c) c = '?';\r
53668523 2454#endif\r
70702af1
L
2455 fno->altname[j] = c;\r
2456 if (!lfv) {\r
2457 if (IsUpper(c) && (dp->dir[DIR_NTres] & (i >= 9 ? NS_EXT : NS_BODY))) {\r
2458 c += 0x20; /* To lower */\r
53668523 2459 }\r
70702af1 2460 fno->fname[j] = c;\r
53668523 2461 }\r
70702af1 2462 j++;\r
53668523 2463 }\r
70702af1
L
2464 if (!lfv) {\r
2465 fno->fname[j] = 0;\r
2466 if (!dp->dir[DIR_NTres]) j = 0; /* Altname is no longer needed if neither LFN nor case info is exist. */\r
2467 }\r
2468 fno->altname[j] = 0; /* Terminate the SFN */\r
2469\r
2470#else /* Non-LFN configuration */\r
2471 i = j = 0;\r
2472 while (i < 11) { /* Copy name body and extension */\r
2473 c = (TCHAR)dp->dir[i++];\r
2474 if (c == ' ') continue; /* Skip padding spaces */\r
2475 if (c == RDDEM) c = (TCHAR)DDEM; /* Restore replaced DDEM character */\r
2476 if (i == 9) fno->fname[j++] = '.'; /* Insert a . if extension is exist */\r
2477 fno->fname[j++] = c;\r
2478 }\r
2479 fno->fname[j] = 0;\r
53668523 2480#endif\r
70702af1
L
2481\r
2482 fno->fattrib = dp->dir[DIR_Attr]; /* Attribute */\r
2483 fno->fsize = ld_dword(dp->dir + DIR_FileSize); /* Size */\r
2484 tm = ld_dword(dp->dir + DIR_ModTime); /* Timestamp */\r
2485 fno->ftime = (WORD)tm; fno->fdate = (WORD)(tm >> 16);\r
53668523 2486}\r
53668523 2487\r
70702af1 2488#endif /* _FS_MINIMIZE <= 1 || _FS_RPATH >= 2 */\r
53668523
L
2489\r
2490\r
2491\r
70702af1 2492#if _USE_FIND && _FS_MINIMIZE <= 1\r
53668523 2493/*-----------------------------------------------------------------------*/\r
70702af1 2494/* Pattern matching */\r
53668523
L
2495/*-----------------------------------------------------------------------*/\r
2496\r
2497static\r
70702af1
L
2498WCHAR get_achar ( /* Get a character and advances ptr 1 or 2 */\r
2499 const TCHAR** ptr /* Pointer to pointer to the SBCS/DBCS/Unicode string */\r
2500)\r
2501{\r
2502#if !_LFN_UNICODE\r
2503 WCHAR chr;\r
2504\r
2505 chr = (BYTE)*(*ptr)++; /* Get a byte */\r
2506 if (IsLower(chr)) chr -= 0x20; /* To upper ASCII char */\r
2507#ifdef _EXCVT\r
2508 if (chr >= 0x80) chr = ExCvt[chr - 0x80]; /* To upper SBCS extended char */\r
2509#else\r
2510 if (IsDBCS1(chr) && IsDBCS2(**ptr)) { /* Get DBC 2nd byte if needed */\r
2511 chr = chr << 8 | (BYTE)*(*ptr)++;\r
2512 }\r
2513#endif\r
2514 return chr;\r
2515#else\r
2516 return ff_wtoupper(*(*ptr)++); /* Get a word and to upper */\r
2517#endif\r
2518}\r
2519\r
2520\r
2521static\r
2522int pattern_matching ( /* 0:not matched, 1:matched */\r
2523 const TCHAR* pat, /* Matching pattern */\r
2524 const TCHAR* nam, /* String to be tested */\r
2525 int skip, /* Number of pre-skip chars (number of ?s) */\r
2526 int inf /* Infinite search (* specified) */\r
2527)\r
2528{\r
2529 const TCHAR *pp, *np;\r
2530 WCHAR pc, nc;\r
2531 int nm, nx;\r
2532\r
2533\r
2534 while (skip--) { /* Pre-skip name chars */\r
2535 if (!get_achar(&nam)) return 0; /* Branch mismatched if less name chars */\r
2536 }\r
2537 if (!*pat && inf) return 1; /* (short circuit) */\r
2538\r
2539 do {\r
2540 pp = pat; np = nam; /* Top of pattern and name to match */\r
2541 for (;;) {\r
2542 if (*pp == '?' || *pp == '*') { /* Wildcard? */\r
2543 nm = nx = 0;\r
2544 do { /* Analyze the wildcard chars */\r
2545 if (*pp++ == '?') nm++; else nx = 1;\r
2546 } while (*pp == '?' || *pp == '*');\r
2547 if (pattern_matching(pp, np, nm, nx)) return 1; /* Test new branch (recurs upto number of wildcard blocks in the pattern) */\r
2548 nc = *np; break; /* Branch mismatched */\r
2549 }\r
2550 pc = get_achar(&pp); /* Get a pattern char */\r
2551 nc = get_achar(&np); /* Get a name char */\r
2552 if (pc != nc) break; /* Branch mismatched? */\r
2553 if (pc == 0) return 1; /* Branch matched? (matched at end of both strings) */\r
2554 }\r
2555 get_achar(&nam); /* nam++ */\r
2556 } while (inf && nc); /* Retry until end of name if infinite search is specified */\r
2557\r
2558 return 0;\r
2559}\r
2560\r
2561#endif /* _USE_FIND && _FS_MINIMIZE <= 1 */\r
2562\r
2563\r
2564\r
2565/*-----------------------------------------------------------------------*/\r
2566/* Pick a top segment and create the object name in directory form */\r
2567/*-----------------------------------------------------------------------*/\r
2568\r
2569static\r
2570FRESULT create_name ( /* FR_OK: successful, FR_INVALID_NAME: could not create */\r
53668523
L
2571 DIR* dp, /* Pointer to the directory object */\r
2572 const TCHAR** path /* Pointer to pointer to the segment in the path string */\r
2573)\r
2574{\r
70702af1 2575#if _USE_LFN != 0 /* LFN configuration */\r
53668523
L
2576 BYTE b, cf;\r
2577 WCHAR w, *lfn;\r
2578 UINT i, ni, si, di;\r
2579 const TCHAR *p;\r
2580\r
2581 /* Create LFN in Unicode */\r
70702af1 2582 p = *path; lfn = dp->obj.fs->lfnbuf; si = di = 0;\r
53668523
L
2583 for (;;) {\r
2584 w = p[si++]; /* Get a character */\r
70702af1
L
2585 if (w < ' ') break; /* Break if end of the path name */\r
2586 if (w == '/' || w == '\\') { /* Break if a separator is found */\r
2587 while (p[si] == '/' || p[si] == '\\') si++; /* Skip duplicated separator if exist */\r
2588 break;\r
2589 }\r
2590 if (di >= _MAX_LFN) return FR_INVALID_NAME; /* Reject too long name */\r
53668523
L
2591#if !_LFN_UNICODE\r
2592 w &= 0xFF;\r
2593 if (IsDBCS1(w)) { /* Check if it is a DBC 1st byte (always false on SBCS cfg) */\r
2594 b = (BYTE)p[si++]; /* Get 2nd byte */\r
53668523 2595 w = (w << 8) + b; /* Create a DBC */\r
70702af1 2596 if (!IsDBCS2(b)) return FR_INVALID_NAME; /* Reject invalid sequence */\r
53668523
L
2597 }\r
2598 w = ff_convert(w, 1); /* Convert ANSI/OEM to Unicode */\r
2599 if (!w) return FR_INVALID_NAME; /* Reject invalid code */\r
2600#endif\r
70702af1 2601 if (w < 0x80 && chk_chr("\"*:<>\?|\x7F", w)) return FR_INVALID_NAME; /* Reject illegal characters for LFN */\r
53668523
L
2602 lfn[di++] = w; /* Store the Unicode character */\r
2603 }\r
2604 *path = &p[si]; /* Return pointer to the next segment */\r
70702af1
L
2605 cf = (w < ' ') ? NS_LAST : 0; /* Set last segment flag if end of the path */\r
2606#if _FS_RPATH != 0\r
2607 if ((di == 1 && lfn[di - 1] == '.') ||\r
2608 (di == 2 && lfn[di - 1] == '.' && lfn[di - 2] == '.')) { /* Is this segment a dot name? */\r
53668523 2609 lfn[di] = 0;\r
70702af1 2610 for (i = 0; i < 11; i++) /* Create dot name for SFN entry */\r
53668523
L
2611 dp->fn[i] = (i < di) ? '.' : ' ';\r
2612 dp->fn[i] = cf | NS_DOT; /* This is a dot entry */\r
2613 return FR_OK;\r
2614 }\r
2615#endif\r
70702af1
L
2616 while (di) { /* Snip off trailing spaces and dots if exist */\r
2617 w = lfn[di - 1];\r
53668523
L
2618 if (w != ' ' && w != '.') break;\r
2619 di--;\r
2620 }\r
53668523 2621 lfn[di] = 0; /* LFN is created */\r
70702af1 2622 if (di == 0) return FR_INVALID_NAME; /* Reject nul name */\r
53668523
L
2623\r
2624 /* Create SFN in directory form */\r
2625 mem_set(dp->fn, ' ', 11);\r
2626 for (si = 0; lfn[si] == ' ' || lfn[si] == '.'; si++) ; /* Strip leading spaces and dots */\r
2627 if (si) cf |= NS_LOSS | NS_LFN;\r
2628 while (di && lfn[di - 1] != '.') di--; /* Find extension (di<=si: no extension) */\r
2629\r
70702af1 2630 i = b = 0; ni = 8;\r
53668523
L
2631 for (;;) {\r
2632 w = lfn[si++]; /* Get an LFN character */\r
2633 if (!w) break; /* Break on end of the LFN */\r
2634 if (w == ' ' || (w == '.' && si != di)) { /* Remove spaces and dots */\r
2635 cf |= NS_LOSS | NS_LFN; continue;\r
2636 }\r
2637\r
2638 if (i >= ni || si == di) { /* Extension or end of SFN */\r
2639 if (ni == 11) { /* Long extension */\r
2640 cf |= NS_LOSS | NS_LFN; break;\r
2641 }\r
2642 if (si != di) cf |= NS_LOSS | NS_LFN; /* Out of 8.3 format */\r
2643 if (si > di) break; /* No extension */\r
2644 si = di; i = 8; ni = 11; /* Enter extension section */\r
2645 b <<= 2; continue;\r
2646 }\r
2647\r
2648 if (w >= 0x80) { /* Non ASCII character */\r
2649#ifdef _EXCVT\r
2650 w = ff_convert(w, 0); /* Unicode -> OEM code */\r
2651 if (w) w = ExCvt[w - 0x80]; /* Convert extended character to upper (SBCS) */\r
2652#else\r
2653 w = ff_convert(ff_wtoupper(w), 0); /* Upper converted Unicode -> OEM code */\r
2654#endif\r
2655 cf |= NS_LFN; /* Force create LFN entry */\r
2656 }\r
2657\r
70702af1 2658 if (_DF1S && w >= 0x100) { /* Is this DBC? (always false at SBCS cfg) */\r
53668523
L
2659 if (i >= ni - 1) {\r
2660 cf |= NS_LOSS | NS_LFN; i = ni; continue;\r
2661 }\r
2662 dp->fn[i++] = (BYTE)(w >> 8);\r
70702af1 2663 } else { /* SBC */\r
53668523
L
2664 if (!w || chk_chr("+,;=[]", w)) { /* Replace illegal characters for SFN */\r
2665 w = '_'; cf |= NS_LOSS | NS_LFN;/* Lossy conversion */\r
2666 } else {\r
2667 if (IsUpper(w)) { /* ASCII large capital */\r
2668 b |= 2;\r
2669 } else {\r
2670 if (IsLower(w)) { /* ASCII small capital */\r
2671 b |= 1; w -= 0x20;\r
2672 }\r
2673 }\r
2674 }\r
2675 }\r
2676 dp->fn[i++] = (BYTE)w;\r
2677 }\r
2678\r
70702af1 2679 if (dp->fn[0] == DDEM) dp->fn[0] = RDDEM; /* If the first character collides with DDEM, replace it with RDDEM */\r
53668523
L
2680\r
2681 if (ni == 8) b <<= 2;\r
70702af1 2682 if ((b & 0x0C) == 0x0C || (b & 0x03) == 0x03) cf |= NS_LFN; /* Create LFN entry when there are composite capitals */\r
53668523
L
2683 if (!(cf & NS_LFN)) { /* When LFN is in 8.3 format without extended character, NT flags are created */\r
2684 if ((b & 0x03) == 0x01) cf |= NS_EXT; /* NT flag (Extension has only small capital) */\r
2685 if ((b & 0x0C) == 0x04) cf |= NS_BODY; /* NT flag (Filename has only small capital) */\r
2686 }\r
2687\r
7b78a5a2 2688 dp->fn[NSFLAG] = cf; /* SFN is created */\r
53668523
L
2689\r
2690 return FR_OK;\r
2691\r
2692\r
70702af1
L
2693#else /* _USE_LFN != 0 : Non-LFN configuration */\r
2694 BYTE c, d, *sfn;\r
53668523
L
2695 UINT ni, si, i;\r
2696 const char *p;\r
2697\r
2698 /* Create file name in directory form */\r
70702af1 2699 p = *path; sfn = dp->fn;\r
53668523 2700 mem_set(sfn, ' ', 11);\r
70702af1
L
2701 si = i = 0; ni = 8;\r
2702#if _FS_RPATH != 0\r
53668523
L
2703 if (p[si] == '.') { /* Is this a dot entry? */\r
2704 for (;;) {\r
2705 c = (BYTE)p[si++];\r
2706 if (c != '.' || si >= 3) break;\r
2707 sfn[i++] = c;\r
2708 }\r
2709 if (c != '/' && c != '\\' && c > ' ') return FR_INVALID_NAME;\r
70702af1
L
2710 *path = p + si; /* Return pointer to the next segment */\r
2711 sfn[NSFLAG] = (c <= ' ') ? NS_LAST | NS_DOT : NS_DOT; /* Set last segment flag if end of the path */\r
53668523
L
2712 return FR_OK;\r
2713 }\r
2714#endif\r
2715 for (;;) {\r
2716 c = (BYTE)p[si++];\r
70702af1
L
2717 if (c <= ' ') break; /* Break if end of the path name */\r
2718 if (c == '/' || c == '\\') { /* Break if a separator is found */\r
2719 while (p[si] == '/' || p[si] == '\\') si++; /* Skip duplicated separator if exist */\r
2720 break;\r
2721 }\r
2722 if (c == '.' || i >= ni) { /* End of body or over size? */\r
2723 if (ni == 11 || c != '.') return FR_INVALID_NAME; /* Over size or invalid dot */\r
2724 i = 8; ni = 11; /* Goto extension */\r
2725 continue;\r
53668523
L
2726 }\r
2727 if (c >= 0x80) { /* Extended character? */\r
53668523
L
2728#ifdef _EXCVT\r
2729 c = ExCvt[c - 0x80]; /* To upper extended characters (SBCS cfg) */\r
2730#else\r
2731#if !_DF1S\r
70702af1 2732 return FR_INVALID_NAME; /* Reject extended characters (ASCII only cfg) */\r
53668523
L
2733#endif\r
2734#endif\r
2735 }\r
70702af1 2736 if (IsDBCS1(c)) { /* Check if it is a DBC 1st byte (always false at SBCS cfg.) */\r
53668523 2737 d = (BYTE)p[si++]; /* Get 2nd byte */\r
70702af1 2738 if (!IsDBCS2(d) || i >= ni - 1) return FR_INVALID_NAME; /* Reject invalid DBC */\r
53668523
L
2739 sfn[i++] = c;\r
2740 sfn[i++] = d;\r
70702af1
L
2741 } else { /* SBC */\r
2742 if (chk_chr("\"*+,:;<=>\?[]|\x7F", c)) return FR_INVALID_NAME; /* Reject illegal chrs for SFN */\r
2743 if (IsLower(c)) c -= 0x20; /* To upper */\r
53668523
L
2744 sfn[i++] = c;\r
2745 }\r
2746 }\r
70702af1
L
2747 *path = p + si; /* Return pointer to the next segment */\r
2748 if (i == 0) return FR_INVALID_NAME; /* Reject nul string */\r
53668523 2749\r
70702af1
L
2750 if (sfn[0] == DDEM) sfn[0] = RDDEM; /* If the first character collides with DDEM, replace it with RDDEM */\r
2751 sfn[NSFLAG] = (c <= ' ') ? NS_LAST : 0; /* Set last segment flag if end of the path */\r
53668523
L
2752\r
2753 return FR_OK;\r
70702af1 2754#endif /* _USE_LFN != 0 */\r
53668523
L
2755}\r
2756\r
2757\r
2758\r
2759\r
2760/*-----------------------------------------------------------------------*/\r
2761/* Follow a file path */\r
2762/*-----------------------------------------------------------------------*/\r
2763\r
2764static\r
2765FRESULT follow_path ( /* FR_OK(0): successful, !=0: error code */\r
2766 DIR* dp, /* Directory object to return last directory and found object */\r
2767 const TCHAR* path /* Full-path string to find a file or directory */\r
2768)\r
2769{\r
2770 FRESULT res;\r
70702af1
L
2771 BYTE ns;\r
2772 _FDID *obj = &dp->obj;\r
2773 FATFS *fs = obj->fs;\r
53668523
L
2774\r
2775\r
70702af1
L
2776#if _FS_RPATH != 0\r
2777 if (*path != '/' && *path != '\\') { /* Without heading separator */\r
2778 obj->sclust = fs->cdir; /* Start from the current directory */\r
2779 } else\r
2780#endif\r
2781 { /* With heading separator */\r
2782 while (*path == '/' || *path == '\\') path++; /* Strip heading separator */\r
2783 obj->sclust = 0; /* Start from the root directory */\r
2784 }\r
2785#if _FS_EXFAT && _FS_RPATH != 0\r
2786 if (fs->fs_type == FS_EXFAT && obj->sclust) { /* Retrieve the sub-directory status if needed */\r
2787 DIR dj;\r
2788\r
2789 obj->c_scl = fs->cdc_scl;\r
2790 obj->c_size = fs->cdc_size;\r
2791 obj->c_ofs = fs->cdc_ofs;\r
2792 res = load_obj_dir(&dj, obj);\r
2793 if (res != FR_OK) return res;\r
2794 obj->objsize = ld_dword(fs->dirbuf + XDIR_FileSize);\r
2795 obj->stat = fs->dirbuf[XDIR_GenFlags] & 2;\r
53668523 2796 }\r
53668523
L
2797#endif\r
2798\r
2799 if ((UINT)*path < ' ') { /* Null path name is the origin directory itself */\r
70702af1 2800 dp->fn[NSFLAG] = NS_NONAME;\r
53668523 2801 res = dir_sdi(dp, 0);\r
70702af1 2802\r
53668523
L
2803 } else { /* Follow path */\r
2804 for (;;) {\r
2805 res = create_name(dp, &path); /* Get a segment name of the path */\r
2806 if (res != FR_OK) break;\r
70702af1 2807 res = dir_find(dp); /* Find an object with the segment name */\r
7b78a5a2 2808 ns = dp->fn[NSFLAG];\r
53668523
L
2809 if (res != FR_OK) { /* Failed to find the object */\r
2810 if (res == FR_NO_FILE) { /* Object is not found */\r
70702af1 2811 if (_FS_RPATH && (ns & NS_DOT)) { /* If dot entry is not exist, stay there */\r
53668523 2812 if (!(ns & NS_LAST)) continue; /* Continue to follow if not last segment */\r
70702af1
L
2813 dp->fn[NSFLAG] = NS_NONAME;\r
2814 res = FR_OK;\r
53668523
L
2815 } else { /* Could not find the object */\r
2816 if (!(ns & NS_LAST)) res = FR_NO_PATH; /* Adjust error code if not last segment */\r
2817 }\r
2818 }\r
2819 break;\r
2820 }\r
2821 if (ns & NS_LAST) break; /* Last segment matched. Function completed. */\r
70702af1
L
2822 /* Get into the sub-directory */\r
2823 if (!(obj->attr & AM_DIR)) { /* It is not a sub-directory and cannot follow */\r
53668523
L
2824 res = FR_NO_PATH; break;\r
2825 }\r
70702af1
L
2826#if _FS_EXFAT\r
2827 if (fs->fs_type == FS_EXFAT) {\r
2828 obj->c_scl = obj->sclust; /* Save containing directory information for next dir */\r
2829 obj->c_size = ((DWORD)obj->objsize & 0xFFFFFF00) | obj->stat;\r
2830 obj->c_ofs = dp->blk_ofs;\r
2831 obj->sclust = ld_dword(fs->dirbuf + XDIR_FstClus); /* Open next directory */\r
2832 obj->stat = fs->dirbuf[XDIR_GenFlags] & 2;\r
2833 obj->objsize = ld_qword(fs->dirbuf + XDIR_FileSize);\r
2834 } else\r
2835#endif\r
2836 {\r
2837 obj->sclust = ld_clust(fs, fs->win + dp->dptr % SS(fs)); /* Open next directory */\r
2838 }\r
53668523
L
2839 }\r
2840 }\r
2841\r
2842 return res;\r
2843}\r
2844\r
2845\r
2846\r
2847\r
2848/*-----------------------------------------------------------------------*/\r
2849/* Get logical drive number from path name */\r
2850/*-----------------------------------------------------------------------*/\r
2851\r
2852static\r
2853int get_ldnumber ( /* Returns logical drive number (-1:invalid drive) */\r
2854 const TCHAR** path /* Pointer to pointer to the path name */\r
2855)\r
2856{\r
2857 const TCHAR *tp, *tt;\r
2858 UINT i;\r
2859 int vol = -1;\r
70702af1
L
2860#if _STR_VOLUME_ID /* Find string drive id */\r
2861 static const char* const str[] = {_VOLUME_STRS};\r
2862 const char *sp;\r
2863 char c;\r
2864 TCHAR tc;\r
2865#endif\r
53668523
L
2866\r
2867\r
2868 if (*path) { /* If the pointer is not a null */\r
2869 for (tt = *path; (UINT)*tt >= (_USE_LFN ? ' ' : '!') && *tt != ':'; tt++) ; /* Find ':' in the path */\r
2870 if (*tt == ':') { /* If a ':' is exist in the path name */\r
2871 tp = *path;\r
2872 i = *tp++ - '0'; \r
2873 if (i < 10 && tp == tt) { /* Is there a numeric drive id? */\r
2874 if (i < _VOLUMES) { /* If a drive id is found, get the value and strip it */\r
2875 vol = (int)i;\r
2876 *path = ++tt;\r
2877 }\r
70702af1
L
2878 }\r
2879#if _STR_VOLUME_ID\r
2880 else { /* No numeric drive number, find string drive id */\r
53668523
L
2881 i = 0; tt++;\r
2882 do {\r
2883 sp = str[i]; tp = *path;\r
2884 do { /* Compare a string drive id with path name */\r
2885 c = *sp++; tc = *tp++;\r
2886 if (IsLower(tc)) tc -= 0x20;\r
2887 } while (c && (TCHAR)c == tc);\r
2888 } while ((c || tp != tt) && ++i < _VOLUMES); /* Repeat for each id until pattern match */\r
2889 if (i < _VOLUMES) { /* If a drive id is found, get the value and strip it */\r
2890 vol = (int)i;\r
2891 *path = tt;\r
2892 }\r
53668523 2893 }\r
70702af1 2894#endif\r
53668523
L
2895 return vol;\r
2896 }\r
70702af1 2897#if _FS_RPATH != 0 && _VOLUMES >= 2\r
53668523
L
2898 vol = CurrVol; /* Current drive */\r
2899#else\r
2900 vol = 0; /* Drive 0 */\r
2901#endif\r
2902 }\r
2903 return vol;\r
2904}\r
2905\r
2906\r
2907\r
2908\r
2909/*-----------------------------------------------------------------------*/\r
2910/* Load a sector and check if it is an FAT boot sector */\r
2911/*-----------------------------------------------------------------------*/\r
2912\r
2913static\r
70702af1 2914BYTE check_fs ( /* 0:FAT, 1:exFAT, 2:Valid BS but not FAT, 3:Not a BS, 4:Disk error */\r
53668523 2915 FATFS* fs, /* File system object */\r
70702af1 2916 DWORD sect /* Sector# (lba) to check if it is an FAT-VBR or not */\r
53668523
L
2917)\r
2918{\r
70702af1
L
2919 fs->wflag = 0; fs->winsect = 0xFFFFFFFF; /* Invaidate window */\r
2920 if (move_window(fs, sect) != FR_OK) return 4; /* Load boot record */\r
53668523 2921\r
70702af1 2922 if (ld_word(fs->win + BS_55AA) != 0xAA55) return 3; /* Check boot record signature (always placed at offset 510 even if the sector size is >512) */\r
53668523 2923\r
70702af1
L
2924 if (fs->win[BS_JmpBoot] == 0xE9 || (fs->win[BS_JmpBoot] == 0xEB && fs->win[BS_JmpBoot + 2] == 0x90)) {\r
2925 if ((ld_dword(fs->win + BS_FilSysType) & 0xFFFFFF) == 0x544146) return 0; /* Check "FAT" string */\r
2926 if (ld_dword(fs->win + BS_FilSysType32) == 0x33544146) return 0; /* Check "FAT3" string */\r
2927 }\r
2928#if _FS_EXFAT\r
2929 if (!mem_cmp(fs->win + BS_JmpBoot, "\xEB\x76\x90" "EXFAT ", 11)) return 1;\r
2930#endif\r
2931 return 2;\r
53668523
L
2932}\r
2933\r
2934\r
2935\r
2936\r
2937/*-----------------------------------------------------------------------*/\r
2938/* Find logical drive and check if the volume is mounted */\r
2939/*-----------------------------------------------------------------------*/\r
2940\r
2941static\r
2942FRESULT find_volume ( /* FR_OK(0): successful, !=0: any error occurred */\r
53668523 2943 const TCHAR** path, /* Pointer to pointer to the path name (drive number) */\r
70702af1
L
2944 FATFS** rfs, /* Pointer to pointer to the found file system object */\r
2945 BYTE mode /* !=0: Check write protection for write access */\r
53668523
L
2946)\r
2947{\r
70702af1 2948 BYTE fmt, *pt;\r
53668523
L
2949 int vol;\r
2950 DSTATUS stat;\r
70702af1 2951 DWORD bsect, fasize, tsect, sysect, nclst, szbfat, br[4];\r
53668523
L
2952 WORD nrsv;\r
2953 FATFS *fs;\r
70702af1 2954 UINT i;\r
53668523
L
2955\r
2956\r
70702af1 2957 /* Get logical drive number */\r
53668523
L
2958 *rfs = 0;\r
2959 vol = get_ldnumber(path);\r
2960 if (vol < 0) return FR_INVALID_DRIVE;\r
2961\r
2962 /* Check if the file system object is valid or not */\r
2963 fs = FatFs[vol]; /* Get pointer to the file system object */\r
2964 if (!fs) return FR_NOT_ENABLED; /* Is the file system object available? */\r
2965\r
2966 ENTER_FF(fs); /* Lock the volume */\r
2967 *rfs = fs; /* Return pointer to the file system object */\r
2968\r
70702af1 2969 mode &= (BYTE)~FA_READ; /* Desired access mode, write access or not */\r
53668523
L
2970 if (fs->fs_type) { /* If the volume has been mounted */\r
2971 stat = disk_status(fs->drv);\r
2972 if (!(stat & STA_NOINIT)) { /* and the physical drive is kept initialized */\r
70702af1 2973 if (!_FS_READONLY && mode && (stat & STA_PROTECT)) { /* Check write protection if needed */\r
53668523 2974 return FR_WRITE_PROTECTED;\r
70702af1 2975 }\r
53668523
L
2976 return FR_OK; /* The file system object is valid */\r
2977 }\r
2978 }\r
2979\r
2980 /* The file system object is not valid. */\r
2981 /* Following code attempts to mount the volume. (analyze BPB and initialize the fs object) */\r
2982\r
2983 fs->fs_type = 0; /* Clear the file system object */\r
2984 fs->drv = LD2PD(vol); /* Bind the logical drive and a physical drive */\r
2985 stat = disk_initialize(fs->drv); /* Initialize the physical drive */\r
70702af1 2986 if (stat & STA_NOINIT) { /* Check if the initialization succeeded */\r
53668523 2987 return FR_NOT_READY; /* Failed to initialize due to no medium or hard error */\r
70702af1
L
2988 }\r
2989 if (!_FS_READONLY && mode && (stat & STA_PROTECT)) { /* Check disk write protection if needed */\r
53668523 2990 return FR_WRITE_PROTECTED;\r
70702af1 2991 }\r
53668523 2992#if _MAX_SS != _MIN_SS /* Get sector size (multiple sector size cfg only) */\r
70702af1
L
2993 if (disk_ioctl(fs->drv, GET_SECTOR_SIZE, &SS(fs)) != RES_OK) return FR_DISK_ERR;\r
2994 if (SS(fs) > _MAX_SS || SS(fs) < _MIN_SS || (SS(fs) & (SS(fs) - 1))) return FR_DISK_ERR;\r
53668523
L
2995#endif\r
2996 /* Find an FAT partition on the drive. Supports only generic partitioning, FDISK and SFD. */\r
2997 bsect = 0;\r
70702af1
L
2998 fmt = check_fs(fs, bsect); /* Load sector 0 and check if it is an FAT-VBR as SFD */\r
2999 if (fmt == 2 || (fmt < 2 && LD2PT(vol) != 0)) { /* Not an FAT-VBR or forced partition number */\r
53668523 3000 for (i = 0; i < 4; i++) { /* Get partition offset */\r
70702af1
L
3001 pt = fs->win + (MBR_Table + i * SZ_PTE);\r
3002 br[i] = pt[PTE_System] ? ld_dword(pt + PTE_StLba) : 0;\r
53668523
L
3003 }\r
3004 i = LD2PT(vol); /* Partition number: 0:auto, 1-4:forced */\r
3005 if (i) i--;\r
3006 do { /* Find an FAT volume */\r
3007 bsect = br[i];\r
70702af1
L
3008 fmt = bsect ? check_fs(fs, bsect) : 3; /* Check the partition */\r
3009 } while (!LD2PT(vol) && fmt >= 2 && ++i < 4);\r
53668523 3010 }\r
70702af1
L
3011 if (fmt == 4) return FR_DISK_ERR; /* An error occured in the disk I/O layer */\r
3012 if (fmt >= 2) return FR_NO_FILESYSTEM; /* No FAT volume is found */\r
53668523
L
3013\r
3014 /* An FAT volume is found. Following code initializes the file system object */\r
3015\r
70702af1
L
3016#if _FS_EXFAT\r
3017 if (fmt == 1) {\r
3018 QWORD maxlba;\r
3019\r
3020 for (i = BPB_ZeroedEx; i < BPB_ZeroedEx + 53 && fs->win[i] == 0; i++) ; /* Check zero filler */\r
3021 if (i < BPB_ZeroedEx + 53) return FR_NO_FILESYSTEM;\r
3022\r
3023 if (ld_word(fs->win + BPB_FSVerEx) != 0x100) return FR_NO_FILESYSTEM; /* Check exFAT revision (Must be 1.0) */\r
3024\r
3025 if (1 << fs->win[BPB_BytsPerSecEx] != SS(fs)) /* (BPB_BytsPerSecEx must be equal to the physical sector size) */\r
3026 return FR_NO_FILESYSTEM;\r
3027\r
3028 maxlba = ld_qword(fs->win + BPB_TotSecEx) + bsect; /* Last LBA + 1 of the volume */\r
3029 if (maxlba >= 0x100000000) return FR_NO_FILESYSTEM; /* (It cannot be handled in 32-bit LBA) */\r
3030\r
3031 fs->fsize = ld_dword(fs->win + BPB_FatSzEx); /* Number of sectors per FAT */\r
3032\r
3033 fs->n_fats = fs->win[BPB_NumFATsEx]; /* Number of FATs */\r
3034 if (fs->n_fats != 1) return FR_NO_FILESYSTEM; /* (Supports only 1 FAT) */\r
3035\r
3036 fs->csize = 1 << fs->win[BPB_SecPerClusEx]; /* Cluster size */\r
3037 if (fs->csize == 0) return FR_NO_FILESYSTEM; /* (Must be 1..32768) */\r
3038\r
3039 nclst = ld_dword(fs->win + BPB_NumClusEx); /* Number of clusters */\r
3040 if (nclst > MAX_EXFAT) return FR_NO_FILESYSTEM; /* (Too many clusters) */\r
3041 fs->n_fatent = nclst + 2;\r
53668523 3042\r
70702af1
L
3043 /* Boundaries and Limits */\r
3044 fs->volbase = bsect;\r
3045 fs->database = bsect + ld_dword(fs->win + BPB_DataOfsEx);\r
3046 fs->fatbase = bsect + ld_dword(fs->win + BPB_FatOfsEx);\r
3047 if (maxlba < (QWORD)fs->database + nclst * fs->csize) return FR_NO_FILESYSTEM; /* (Volume size must not be smaller than the size requiered) */\r
3048 fs->dirbase = ld_dword(fs->win + BPB_RootClusEx);\r
3049\r
3050 /* Check if bitmap location is in assumption (at the first cluster) */\r
3051 if (move_window(fs, clust2sect(fs, fs->dirbase)) != FR_OK) return FR_DISK_ERR;\r
3052 for (i = 0; i < SS(fs); i += SZDIRE) {\r
3053 if (fs->win[i] == 0x81 && ld_dword(fs->win + i + 20) == 2) break; /* 81 entry with cluster #2? */\r
3054 }\r
3055 if (i == SS(fs)) return FR_NO_FILESYSTEM;\r
53668523 3056#if !_FS_READONLY\r
70702af1
L
3057 fs->last_clst = fs->free_clst = 0xFFFFFFFF; /* Initialize cluster allocation information */\r
3058#endif\r
3059 fmt = FS_EXFAT; /* FAT sub-type */\r
3060 } else\r
3061#endif /* _FS_EXFAT */\r
3062 {\r
3063 if (ld_word(fs->win + BPB_BytsPerSec) != SS(fs)) return FR_NO_FILESYSTEM; /* (BPB_BytsPerSec must be equal to the physical sector size) */\r
3064\r
3065 fasize = ld_word(fs->win + BPB_FATSz16); /* Number of sectors per FAT */\r
3066 if (fasize == 0) fasize = ld_dword(fs->win + BPB_FATSz32);\r
3067 fs->fsize = fasize;\r
3068\r
3069 fs->n_fats = fs->win[BPB_NumFATs]; /* Number of FATs */\r
3070 if (fs->n_fats != 1 && fs->n_fats != 2) return FR_NO_FILESYSTEM; /* (Must be 1 or 2) */\r
3071 fasize *= fs->n_fats; /* Number of sectors for FAT area */\r
3072\r
3073 fs->csize = fs->win[BPB_SecPerClus]; /* Cluster size */\r
3074 if (fs->csize == 0 || (fs->csize & (fs->csize - 1))) return FR_NO_FILESYSTEM; /* (Must be power of 2) */\r
3075\r
3076 fs->n_rootdir = ld_word(fs->win + BPB_RootEntCnt); /* Number of root directory entries */\r
3077 if (fs->n_rootdir % (SS(fs) / SZDIRE)) return FR_NO_FILESYSTEM; /* (Must be sector aligned) */\r
3078\r
3079 tsect = ld_word(fs->win + BPB_TotSec16); /* Number of sectors on the volume */\r
3080 if (tsect == 0) tsect = ld_dword(fs->win + BPB_TotSec32);\r
3081\r
3082 nrsv = ld_word(fs->win + BPB_RsvdSecCnt); /* Number of reserved sectors */\r
3083 if (nrsv == 0) return FR_NO_FILESYSTEM; /* (Must not be 0) */\r
3084\r
3085 /* Determine the FAT sub type */\r
3086 sysect = nrsv + fasize + fs->n_rootdir / (SS(fs) / SZDIRE); /* RSV + FAT + DIR */\r
3087 if (tsect < sysect) return FR_NO_FILESYSTEM; /* (Invalid volume size) */\r
3088 nclst = (tsect - sysect) / fs->csize; /* Number of clusters */\r
3089 if (nclst == 0) return FR_NO_FILESYSTEM; /* (Invalid volume size) */\r
3090 fmt = FS_FAT32;\r
3091 if (nclst <= MAX_FAT16) fmt = FS_FAT16;\r
3092 if (nclst <= MAX_FAT12) fmt = FS_FAT12;\r
3093\r
3094 /* Boundaries and Limits */\r
3095 fs->n_fatent = nclst + 2; /* Number of FAT entries */\r
3096 fs->volbase = bsect; /* Volume start sector */\r
3097 fs->fatbase = bsect + nrsv; /* FAT start sector */\r
3098 fs->database = bsect + sysect; /* Data start sector */\r
3099 if (fmt == FS_FAT32) {\r
3100 if (ld_word(fs->win + BPB_FSVer32) != 0) return FR_NO_FILESYSTEM; /* (Must be FAT32 revision 0.0) */\r
3101 if (fs->n_rootdir) return FR_NO_FILESYSTEM; /* (BPB_RootEntCnt must be 0) */\r
3102 fs->dirbase = ld_dword(fs->win + BPB_RootClus32); /* Root directory start cluster */\r
3103 szbfat = fs->n_fatent * 4; /* (Needed FAT size) */\r
3104 } else {\r
3105 if (fs->n_rootdir == 0) return FR_NO_FILESYSTEM;/* (BPB_RootEntCnt must not be 0) */\r
3106 fs->dirbase = fs->fatbase + fasize; /* Root directory start sector */\r
3107 szbfat = (fmt == FS_FAT16) ? /* (Needed FAT size) */\r
3108 fs->n_fatent * 2 : fs->n_fatent * 3 / 2 + (fs->n_fatent & 1);\r
3109 }\r
3110 if (fs->fsize < (szbfat + (SS(fs) - 1)) / SS(fs)) return FR_NO_FILESYSTEM; /* (BPB_FATSz must not be less than the size needed) */\r
53668523 3111\r
70702af1
L
3112#if !_FS_READONLY\r
3113 /* Get FSINFO if available */\r
3114 fs->last_clst = fs->free_clst = 0xFFFFFFFF; /* Initialize cluster allocation information */\r
3115 fs->fsi_flag = 0x80;\r
53668523 3116#if (_FS_NOFSINFO & 3) != 3\r
70702af1
L
3117 if (fmt == FS_FAT32 /* Enable FSINFO only if FAT32 and BPB_FSInfo32 == 1 */\r
3118 && ld_word(fs->win + BPB_FSInfo32) == 1\r
3119 && move_window(fs, bsect + 1) == FR_OK)\r
53668523 3120 {\r
70702af1
L
3121 fs->fsi_flag = 0;\r
3122 if (ld_word(fs->win + BS_55AA) == 0xAA55 /* Load FSINFO data if available */\r
3123 && ld_dword(fs->win + FSI_LeadSig) == 0x41615252\r
3124 && ld_dword(fs->win + FSI_StrucSig) == 0x61417272)\r
3125 {\r
53668523 3126#if (_FS_NOFSINFO & 1) == 0\r
70702af1 3127 fs->free_clst = ld_dword(fs->win + FSI_Free_Count);\r
53668523
L
3128#endif\r
3129#if (_FS_NOFSINFO & 2) == 0\r
70702af1 3130 fs->last_clst = ld_dword(fs->win + FSI_Nxt_Free);\r
53668523 3131#endif\r
70702af1 3132 }\r
53668523 3133 }\r
70702af1
L
3134#endif /* (_FS_NOFSINFO & 3) != 3 */\r
3135#endif /* !_FS_READONLY */\r
53668523 3136 }\r
70702af1 3137\r
53668523
L
3138 fs->fs_type = fmt; /* FAT sub-type */\r
3139 fs->id = ++Fsid; /* File system mount ID */\r
70702af1
L
3140#if _USE_LFN == 1\r
3141 fs->lfnbuf = LfnBuf; /* Static LFN working buffer */\r
3142#if _FS_EXFAT\r
3143 fs->dirbuf = DirBuf; /* Static directory block working buuffer */\r
3144#endif\r
53668523 3145#endif\r
70702af1
L
3146#if _FS_RPATH != 0\r
3147 fs->cdir = 0; /* Initialize current directory */\r
3148#endif\r
3149#if _FS_LOCK != 0 /* Clear file lock semaphores */\r
53668523
L
3150 clear_lock(fs);\r
3151#endif\r
53668523
L
3152 return FR_OK;\r
3153}\r
3154\r
3155\r
3156\r
3157\r
3158/*-----------------------------------------------------------------------*/\r
3159/* Check if the file/directory object is valid or not */\r
3160/*-----------------------------------------------------------------------*/\r
3161\r
3162static\r
70702af1
L
3163FRESULT validate ( /* Returns FR_OK or FR_INVALID_OBJECT */\r
3164 _FDID* obj, /* Pointer to the _OBJ, the 1st member in the FIL/DIR object, to check validity */\r
3165 FATFS** fs /* Pointer to pointer to the owner file system object to return */\r
53668523
L
3166)\r
3167{\r
70702af1 3168 FRESULT res;\r
53668523 3169\r
53668523 3170\r
70702af1
L
3171 if (!obj || !obj->fs || !obj->fs->fs_type || obj->fs->id != obj->id || (disk_status(obj->fs->drv) & STA_NOINIT)) {\r
3172 *fs = 0; /* The object is invalid */\r
3173 res = FR_INVALID_OBJECT;\r
3174 } else {\r
3175 *fs = obj->fs; /* Owner file sytem object */\r
3176 ENTER_FF(obj->fs); /* Lock file system */\r
3177 res = FR_OK;\r
3178 }\r
3179 return res;\r
53668523
L
3180}\r
3181\r
3182\r
3183\r
3184\r
70702af1 3185/*---------------------------------------------------------------------------\r
53668523 3186\r
70702af1 3187 Public Functions (FatFs API)\r
53668523 3188\r
70702af1 3189----------------------------------------------------------------------------*/\r
53668523
L
3190\r
3191\r
3192\r
3193/*-----------------------------------------------------------------------*/\r
3194/* Mount/Unmount a Logical Drive */\r
3195/*-----------------------------------------------------------------------*/\r
3196\r
3197FRESULT f_mount (\r
3198 FATFS* fs, /* Pointer to the file system object (NULL:unmount)*/\r
3199 const TCHAR* path, /* Logical drive number to be mounted/unmounted */\r
70702af1 3200 BYTE opt /* Mode option 0:Do not mount (delayed mount), 1:Mount immediately */\r
53668523
L
3201)\r
3202{\r
3203 FATFS *cfs;\r
3204 int vol;\r
3205 FRESULT res;\r
3206 const TCHAR *rp = path;\r
3207\r
3208\r
70702af1 3209 /* Get logical drive number */\r
53668523
L
3210 vol = get_ldnumber(&rp);\r
3211 if (vol < 0) return FR_INVALID_DRIVE;\r
3212 cfs = FatFs[vol]; /* Pointer to fs object */\r
3213\r
3214 if (cfs) {\r
70702af1 3215#if _FS_LOCK != 0\r
53668523
L
3216 clear_lock(cfs);\r
3217#endif\r
3218#if _FS_REENTRANT /* Discard sync object of the current volume */\r
3219 if (!ff_del_syncobj(cfs->sobj)) return FR_INT_ERR;\r
3220#endif\r
3221 cfs->fs_type = 0; /* Clear old fs object */\r
3222 }\r
3223\r
3224 if (fs) {\r
3225 fs->fs_type = 0; /* Clear new fs object */\r
3226#if _FS_REENTRANT /* Create sync object for the new volume */\r
3227 if (!ff_cre_syncobj((BYTE)vol, &fs->sobj)) return FR_INT_ERR;\r
3228#endif\r
3229 }\r
3230 FatFs[vol] = fs; /* Register new fs object */\r
3231\r
3232 if (!fs || opt != 1) return FR_OK; /* Do not mount now, it will be mounted later */\r
3233\r
70702af1 3234 res = find_volume(&path, &fs, 0); /* Force mounted the volume */\r
53668523
L
3235 LEAVE_FF(fs, res);\r
3236}\r
3237\r
3238\r
3239\r
3240\r
3241/*-----------------------------------------------------------------------*/\r
3242/* Open or Create a File */\r
3243/*-----------------------------------------------------------------------*/\r
3244\r
3245FRESULT f_open (\r
3246 FIL* fp, /* Pointer to the blank file object */\r
3247 const TCHAR* path, /* Pointer to the file name */\r
3248 BYTE mode /* Access mode and file open mode flags */\r
3249)\r
3250{\r
3251 FRESULT res;\r
3252 DIR dj;\r
70702af1
L
3253 FATFS *fs;\r
3254#if !_FS_READONLY\r
3255 DWORD dw, cl, bcs, clst, sc;\r
3256 FSIZE_t ofs;\r
3257#endif\r
3258 DEF_NAMBUF\r
53668523
L
3259\r
3260\r
3261 if (!fp) return FR_INVALID_OBJECT;\r
53668523 3262\r
70702af1
L
3263 /* Get logical drive */\r
3264 mode &= _FS_READONLY ? FA_READ : FA_READ | FA_WRITE | FA_CREATE_ALWAYS | FA_CREATE_NEW | FA_OPEN_ALWAYS | FA_OPEN_APPEND | FA_SEEKEND;\r
3265 res = find_volume(&path, &fs, mode);\r
53668523 3266 if (res == FR_OK) {\r
70702af1
L
3267 dj.obj.fs = fs;\r
3268 INIT_NAMBUF(fs);\r
53668523 3269 res = follow_path(&dj, path); /* Follow the file path */\r
53668523
L
3270#if !_FS_READONLY /* R/W configuration */\r
3271 if (res == FR_OK) {\r
70702af1 3272 if (dj.fn[NSFLAG] & NS_NONAME) { /* Origin directory itself? */\r
53668523 3273 res = FR_INVALID_NAME;\r
70702af1
L
3274 }\r
3275#if _FS_LOCK != 0\r
3276 else {\r
53668523 3277 res = chk_lock(&dj, (mode & ~FA_READ) ? 1 : 0);\r
70702af1 3278 }\r
53668523
L
3279#endif\r
3280 }\r
3281 /* Create or Open a file */\r
3282 if (mode & (FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW)) {\r
53668523
L
3283 if (res != FR_OK) { /* No file, create new */\r
3284 if (res == FR_NO_FILE) /* There is no file to open, create a new entry */\r
70702af1 3285#if _FS_LOCK != 0\r
53668523
L
3286 res = enq_lock() ? dir_register(&dj) : FR_TOO_MANY_OPEN_FILES;\r
3287#else\r
3288 res = dir_register(&dj);\r
3289#endif\r
3290 mode |= FA_CREATE_ALWAYS; /* File is created */\r
53668523
L
3291 }\r
3292 else { /* Any object is already existing */\r
70702af1 3293 if (dj.obj.attr & (AM_RDO | AM_DIR)) { /* Cannot overwrite it (R/O or DIR) */\r
53668523
L
3294 res = FR_DENIED;\r
3295 } else {\r
70702af1 3296 if (mode & FA_CREATE_NEW) res = FR_EXIST; /* Cannot create as new file */\r
53668523
L
3297 }\r
3298 }\r
3299 if (res == FR_OK && (mode & FA_CREATE_ALWAYS)) { /* Truncate it if overwrite mode */\r
70702af1
L
3300 dw = GET_FATTIME();\r
3301#if _FS_EXFAT\r
3302 if (fs->fs_type == FS_EXFAT) {\r
3303 /* Get current allocation info */\r
3304 fp->obj.fs = fs;\r
3305 fp->obj.sclust = ld_dword(fs->dirbuf + XDIR_FstClus);\r
3306 fp->obj.objsize = ld_qword(fs->dirbuf + XDIR_FileSize);\r
3307 fp->obj.stat = fs->dirbuf[XDIR_GenFlags] & 2;\r
3308 /* Initialize directory entry block */\r
3309 st_dword(fs->dirbuf + XDIR_CrtTime, dw); /* Set created time */\r
3310 fs->dirbuf[XDIR_CrtTime10] = 0;\r
3311 st_dword(fs->dirbuf + XDIR_ModTime, dw); /* Set modified time */\r
3312 fs->dirbuf[XDIR_ModTime10] = 0;\r
3313 fs->dirbuf[XDIR_Attr] = AM_ARC; /* Reset attribute */\r
3314 st_dword(fs->dirbuf + XDIR_FstClus, 0); /* Reset file allocation info */\r
3315 st_qword(fs->dirbuf + XDIR_FileSize, 0);\r
3316 st_qword(fs->dirbuf + XDIR_ValidFileSize, 0);\r
3317 fs->dirbuf[XDIR_GenFlags] = 1;\r
3318 res = store_xdir(&dj);\r
3319 if (res == FR_OK && fp->obj.sclust) { /* Remove the cluster chain if exist */\r
3320 res = remove_chain(&fp->obj, fp->obj.sclust, 0);\r
3321 fs->last_clst = fp->obj.sclust - 1; /* Reuse the cluster hole */\r
3322 }\r
3323 } else\r
3324#endif\r
3325 {\r
3326 /* Clean directory info */\r
3327 st_dword(dj.dir + DIR_CrtTime, dw); /* Set created time */\r
3328 st_dword(dj.dir + DIR_ModTime, dw); /* Set modified time */\r
3329 dj.dir[DIR_Attr] = AM_ARC; /* Reset attribute */\r
3330 cl = ld_clust(fs, dj.dir); /* Get cluster chain */\r
3331 st_clust(fs, dj.dir, 0); /* Reset file allocation info */\r
3332 st_dword(dj.dir + DIR_FileSize, 0);\r
3333 fs->wflag = 1;\r
3334\r
3335 if (cl) { /* Remove the cluster chain if exist */\r
3336 dw = fs->winsect;\r
3337 res = remove_chain(&dj.obj, cl, 0);\r
3338 if (res == FR_OK) {\r
3339 res = move_window(fs, dw);\r
3340 fs->last_clst = cl - 1; /* Reuse the cluster hole */\r
3341 }\r
53668523
L
3342 }\r
3343 }\r
3344 }\r
3345 }\r
3346 else { /* Open an existing file */\r
70702af1
L
3347 if (res == FR_OK) { /* Following succeeded */\r
3348 if (dj.obj.attr & AM_DIR) { /* It is a directory */\r
53668523
L
3349 res = FR_NO_FILE;\r
3350 } else {\r
70702af1 3351 if ((mode & FA_WRITE) && (dj.obj.attr & AM_RDO)) { /* R/O violation */\r
53668523 3352 res = FR_DENIED;\r
70702af1 3353 }\r
53668523
L
3354 }\r
3355 }\r
3356 }\r
3357 if (res == FR_OK) {\r
3358 if (mode & FA_CREATE_ALWAYS) /* Set file change flag if created or overwritten */\r
70702af1
L
3359 mode |= FA_MODIFIED;\r
3360 fp->dir_sect = fs->winsect; /* Pointer to the directory entry */\r
3361 fp->dir_ptr = dj.dir;\r
3362#if _FS_LOCK != 0\r
3363 fp->obj.lockid = inc_lock(&dj, (mode & ~FA_READ) ? 1 : 0);\r
3364 if (!fp->obj.lockid) res = FR_INT_ERR;\r
53668523
L
3365#endif\r
3366 }\r
70702af1
L
3367#else /* R/O configuration */\r
3368 if (res == FR_OK) {\r
3369 if (dj.fn[NSFLAG] & NS_NONAME) { /* Origin directory itself? */\r
53668523
L
3370 res = FR_INVALID_NAME;\r
3371 } else {\r
70702af1 3372 if (dj.obj.attr & AM_DIR) { /* It is a directory */\r
53668523 3373 res = FR_NO_FILE;\r
70702af1 3374 }\r
53668523
L
3375 }\r
3376 }\r
3377#endif\r
70702af1
L
3378\r
3379 if (res == FR_OK) {\r
3380#if _FS_EXFAT\r
3381 if (fs->fs_type == FS_EXFAT) {\r
3382 fp->obj.sclust = ld_dword(fs->dirbuf + XDIR_FstClus); /* Get allocation info */\r
3383 fp->obj.objsize = ld_qword(fs->dirbuf + XDIR_FileSize);\r
3384 fp->obj.stat = fs->dirbuf[XDIR_GenFlags] & 2;\r
3385 fp->obj.c_scl = dj.obj.sclust;\r
3386 fp->obj.c_size = ((DWORD)dj.obj.objsize & 0xFFFFFF00) | dj.obj.stat;\r
3387 fp->obj.c_ofs = dj.blk_ofs;\r
3388 } else\r
3389#endif\r
3390 {\r
3391 fp->obj.sclust = ld_clust(fs, dj.dir); /* Get allocation info */\r
3392 fp->obj.objsize = ld_dword(dj.dir + DIR_FileSize);\r
3393 }\r
3394#if _USE_FASTSEEK\r
3395 fp->cltbl = 0; /* Disable fast seek mode */\r
3396#endif\r
3397 fp->obj.fs = fs; /* Validate the file object */\r
3398 fp->obj.id = fs->id;\r
3399 fp->flag = mode; /* Set file access mode */\r
3400 fp->err = 0; /* Clear error flag */\r
3401 fp->sect = 0; /* Invalidate current data sector */\r
3402 fp->fptr = 0; /* Set file pointer top of the file */\r
3403#if !_FS_READONLY\r
3404#if !_FS_TINY\r
3405 mem_set(fp->buf, 0, _MAX_SS); /* Clear sector buffer */\r
3406#endif\r
3407 if ((mode & FA_SEEKEND) && fp->obj.objsize > 0) { /* Seek to end of file if FA_OPEN_APPEND is specified */\r
3408 fp->fptr = fp->obj.objsize; /* Offset to seek */\r
3409 bcs = (DWORD)fs->csize * SS(fs); /* Cluster size in byte */\r
3410 clst = fp->obj.sclust; /* Follow the cluster chain */\r
3411 for (ofs = fp->obj.objsize; res == FR_OK && ofs > bcs; ofs -= bcs) {\r
3412 clst = get_fat(&fp->obj, clst);\r
3413 if (clst <= 1) res = FR_INT_ERR;\r
3414 if (clst == 0xFFFFFFFF) res = FR_DISK_ERR;\r
3415 }\r
3416 fp->clust = clst;\r
3417 if (res == FR_OK && ofs % SS(fs)) { /* Fill sector buffer if not on the sector boundary */\r
3418 if ((sc = clust2sect(fs, clst)) == 0) {\r
3419 res = FR_INT_ERR;\r
3420 } else {\r
3421 fp->sect = sc + (DWORD)(ofs / SS(fs));\r
3422#if !_FS_TINY\r
3423 if (disk_read(fs->drv, fp->buf, fp->sect, 1) != RES_OK) res = FR_DISK_ERR;\r
3424#endif\r
3425 }\r
3426 }\r
3427 }\r
53668523 3428#endif\r
53668523 3429 }\r
70702af1
L
3430\r
3431 FREE_NAMBUF();\r
53668523
L
3432 }\r
3433\r
70702af1
L
3434 if (res != FR_OK) fp->obj.fs = 0; /* Invalidate file object on error */\r
3435\r
3436 LEAVE_FF(fs, res);\r
53668523
L
3437}\r
3438\r
3439\r
3440\r
3441\r
3442/*-----------------------------------------------------------------------*/\r
3443/* Read File */\r
3444/*-----------------------------------------------------------------------*/\r
3445\r
3446FRESULT f_read (\r
70702af1
L
3447 FIL* fp, /* Pointer to the file object */\r
3448 void* buff, /* Pointer to data buffer */\r
3449 UINT btr, /* Number of bytes to read */\r
3450 UINT* br /* Pointer to number of bytes read */\r
53668523
L
3451)\r
3452{\r
3453 FRESULT res;\r
70702af1
L
3454 FATFS *fs;\r
3455 DWORD clst, sect;\r
3456 FSIZE_t remain;\r
3457 UINT rcnt, cc, csect;\r
3458 BYTE *rbuff = (BYTE*)buff;\r
53668523
L
3459\r
3460\r
3461 *br = 0; /* Clear read byte counter */\r
70702af1
L
3462 res = validate(&fp->obj, &fs); /* Check validity of the file object */\r
3463 if (res != FR_OK || (res = (FRESULT)fp->err) != FR_OK) LEAVE_FF(fs, res); /* Check validity */\r
3464 if (!(fp->flag & FA_READ)) LEAVE_FF(fs, FR_DENIED); /* Check access mode */\r
3465 remain = fp->obj.objsize - fp->fptr;\r
53668523
L
3466 if (btr > remain) btr = (UINT)remain; /* Truncate btr by remaining bytes */\r
3467\r
3468 for ( ; btr; /* Repeat until all data read */\r
3469 rbuff += rcnt, fp->fptr += rcnt, *br += rcnt, btr -= rcnt) {\r
70702af1
L
3470 if (fp->fptr % SS(fs) == 0) { /* On the sector boundary? */\r
3471 csect = (UINT)(fp->fptr / SS(fs) & (fs->csize - 1)); /* Sector offset in the cluster */\r
3472 if (csect == 0) { /* On the cluster boundary? */\r
53668523 3473 if (fp->fptr == 0) { /* On the top of the file? */\r
70702af1 3474 clst = fp->obj.sclust; /* Follow cluster chain from the origin */\r
53668523
L
3475 } else { /* Middle or end of the file */\r
3476#if _USE_FASTSEEK\r
70702af1 3477 if (fp->cltbl) {\r
53668523 3478 clst = clmt_clust(fp, fp->fptr); /* Get cluster# from the CLMT */\r
70702af1 3479 } else\r
53668523 3480#endif\r
70702af1
L
3481 {\r
3482 clst = get_fat(&fp->obj, fp->clust); /* Follow cluster chain on the FAT */\r
3483 }\r
53668523 3484 }\r
70702af1
L
3485 if (clst < 2) ABORT(fs, FR_INT_ERR);\r
3486 if (clst == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR);\r
53668523
L
3487 fp->clust = clst; /* Update current cluster */\r
3488 }\r
70702af1
L
3489 sect = clust2sect(fs, fp->clust); /* Get current sector */\r
3490 if (!sect) ABORT(fs, FR_INT_ERR);\r
53668523 3491 sect += csect;\r
70702af1 3492 cc = btr / SS(fs); /* When remaining bytes >= sector size, */\r
53668523 3493 if (cc) { /* Read maximum contiguous sectors directly */\r
70702af1
L
3494 if (csect + cc > fs->csize) { /* Clip at cluster boundary */\r
3495 cc = fs->csize - csect;\r
3496 }\r
3497 if (disk_read(fs->drv, rbuff, sect, cc) != RES_OK) ABORT(fs, FR_DISK_ERR);\r
53668523
L
3498#if !_FS_READONLY && _FS_MINIMIZE <= 2 /* Replace one of the read sectors with cached data if it contains a dirty sector */\r
3499#if _FS_TINY\r
70702af1
L
3500 if (fs->wflag && fs->winsect - sect < cc) {\r
3501 mem_cpy(rbuff + ((fs->winsect - sect) * SS(fs)), fs->win, SS(fs));\r
3502 }\r
53668523 3503#else\r
70702af1
L
3504 if ((fp->flag & FA_DIRTY) && fp->sect - sect < cc) {\r
3505 mem_cpy(rbuff + ((fp->sect - sect) * SS(fs)), fp->buf, SS(fs));\r
3506 }\r
53668523
L
3507#endif\r
3508#endif\r
70702af1 3509 rcnt = SS(fs) * cc; /* Number of bytes transferred */\r
53668523
L
3510 continue;\r
3511 }\r
3512#if !_FS_TINY\r
70702af1 3513 if (fp->sect != sect) { /* Load data sector if not in cache */\r
53668523 3514#if !_FS_READONLY\r
70702af1
L
3515 if (fp->flag & FA_DIRTY) { /* Write-back dirty sector cache */\r
3516 if (disk_write(fs->drv, fp->buf, fp->sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR);\r
3517 fp->flag &= (BYTE)~FA_DIRTY;\r
53668523
L
3518 }\r
3519#endif\r
70702af1 3520 if (disk_read(fs->drv, fp->buf, sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR); /* Fill sector cache */\r
53668523
L
3521 }\r
3522#endif\r
70702af1 3523 fp->sect = sect;\r
53668523 3524 }\r
70702af1
L
3525 rcnt = SS(fs) - (UINT)fp->fptr % SS(fs); /* Number of bytes left in the sector */\r
3526 if (rcnt > btr) rcnt = btr; /* Clip it by btr if needed */\r
53668523 3527#if _FS_TINY\r
70702af1
L
3528 if (move_window(fs, fp->sect) != FR_OK) ABORT(fs, FR_DISK_ERR); /* Move sector window */\r
3529 mem_cpy(rbuff, fs->win + fp->fptr % SS(fs), rcnt); /* Extract partial sector */\r
53668523 3530#else\r
70702af1 3531 mem_cpy(rbuff, fp->buf + fp->fptr % SS(fs), rcnt); /* Extract partial sector */\r
53668523
L
3532#endif\r
3533 }\r
3534\r
70702af1 3535 LEAVE_FF(fs, FR_OK);\r
53668523
L
3536}\r
3537\r
3538\r
3539\r
3540\r
3541#if !_FS_READONLY\r
3542/*-----------------------------------------------------------------------*/\r
3543/* Write File */\r
3544/*-----------------------------------------------------------------------*/\r
3545\r
3546FRESULT f_write (\r
3547 FIL* fp, /* Pointer to the file object */\r
70702af1 3548 const void* buff, /* Pointer to the data to be written */\r
53668523
L
3549 UINT btw, /* Number of bytes to write */\r
3550 UINT* bw /* Pointer to number of bytes written */\r
3551)\r
3552{\r
3553 FRESULT res;\r
70702af1 3554 FATFS *fs;\r
53668523 3555 DWORD clst, sect;\r
70702af1 3556 UINT wcnt, cc, csect;\r
53668523 3557 const BYTE *wbuff = (const BYTE*)buff;\r
53668523
L
3558\r
3559\r
3560 *bw = 0; /* Clear write byte counter */\r
70702af1
L
3561 res = validate(&fp->obj, &fs); /* Check validity of the file object */\r
3562 if (res != FR_OK || (res = (FRESULT)fp->err) != FR_OK) LEAVE_FF(fs, res); /* Check validity */\r
3563 if (!(fp->flag & FA_WRITE)) LEAVE_FF(fs, FR_DENIED); /* Check access mode */\r
53668523 3564\r
70702af1
L
3565 /* Check fptr wrap-around (file size cannot reach 4GiB on FATxx) */\r
3566 if ((!_FS_EXFAT || fs->fs_type != FS_EXFAT) && (DWORD)(fp->fptr + btw) < (DWORD)fp->fptr) {\r
3567 btw = (UINT)(0xFFFFFFFF - (DWORD)fp->fptr);\r
3568 }\r
53668523
L
3569\r
3570 for ( ; btw; /* Repeat until all data written */\r
70702af1
L
3571 wbuff += wcnt, fp->fptr += wcnt, fp->obj.objsize = (fp->fptr > fp->obj.objsize) ? fp->fptr : fp->obj.objsize, *bw += wcnt, btw -= wcnt) {\r
3572 if (fp->fptr % SS(fs) == 0) { /* On the sector boundary? */\r
3573 csect = (UINT)(fp->fptr / SS(fs)) & (fs->csize - 1); /* Sector offset in the cluster */\r
3574 if (csect == 0) { /* On the cluster boundary? */\r
53668523 3575 if (fp->fptr == 0) { /* On the top of the file? */\r
70702af1
L
3576 clst = fp->obj.sclust; /* Follow from the origin */\r
3577 if (clst == 0) { /* If no cluster is allocated, */\r
3578 clst = create_chain(&fp->obj, 0); /* create a new cluster chain */\r
3579 }\r
3580 } else { /* On the middle or end of the file */\r
53668523 3581#if _USE_FASTSEEK\r
70702af1 3582 if (fp->cltbl) {\r
53668523 3583 clst = clmt_clust(fp, fp->fptr); /* Get cluster# from the CLMT */\r
70702af1 3584 } else\r
53668523 3585#endif\r
70702af1
L
3586 {\r
3587 clst = create_chain(&fp->obj, fp->clust); /* Follow or stretch cluster chain on the FAT */\r
3588 }\r
53668523
L
3589 }\r
3590 if (clst == 0) break; /* Could not allocate a new cluster (disk full) */\r
70702af1
L
3591 if (clst == 1) ABORT(fs, FR_INT_ERR);\r
3592 if (clst == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR);\r
53668523 3593 fp->clust = clst; /* Update current cluster */\r
70702af1 3594 if (fp->obj.sclust == 0) fp->obj.sclust = clst; /* Set start cluster if the first write */\r
53668523
L
3595 }\r
3596#if _FS_TINY\r
70702af1 3597 if (fs->winsect == fp->sect && sync_window(fs) != FR_OK) ABORT(fs, FR_DISK_ERR); /* Write-back sector cache */\r
53668523 3598#else\r
70702af1
L
3599 if (fp->flag & FA_DIRTY) { /* Write-back sector cache */\r
3600 if (disk_write(fs->drv, fp->buf, fp->sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR);\r
3601 fp->flag &= (BYTE)~FA_DIRTY;\r
53668523
L
3602 }\r
3603#endif\r
70702af1
L
3604 sect = clust2sect(fs, fp->clust); /* Get current sector */\r
3605 if (!sect) ABORT(fs, FR_INT_ERR);\r
53668523 3606 sect += csect;\r
70702af1 3607 cc = btw / SS(fs); /* When remaining bytes >= sector size, */\r
53668523 3608 if (cc) { /* Write maximum contiguous sectors directly */\r
70702af1
L
3609 if (csect + cc > fs->csize) { /* Clip at cluster boundary */\r
3610 cc = fs->csize - csect;\r
3611 }\r
3612 if (disk_write(fs->drv, wbuff, sect, cc) != RES_OK) ABORT(fs, FR_DISK_ERR);\r
53668523
L
3613#if _FS_MINIMIZE <= 2\r
3614#if _FS_TINY\r
70702af1
L
3615 if (fs->winsect - sect < cc) { /* Refill sector cache if it gets invalidated by the direct write */\r
3616 mem_cpy(fs->win, wbuff + ((fs->winsect - sect) * SS(fs)), SS(fs));\r
3617 fs->wflag = 0;\r
53668523
L
3618 }\r
3619#else\r
70702af1
L
3620 if (fp->sect - sect < cc) { /* Refill sector cache if it gets invalidated by the direct write */\r
3621 mem_cpy(fp->buf, wbuff + ((fp->sect - sect) * SS(fs)), SS(fs));\r
3622 fp->flag &= (BYTE)~FA_DIRTY;\r
53668523
L
3623 }\r
3624#endif\r
3625#endif\r
70702af1 3626 wcnt = SS(fs) * cc; /* Number of bytes transferred */\r
53668523
L
3627 continue;\r
3628 }\r
3629#if _FS_TINY\r
70702af1
L
3630 if (fp->fptr >= fp->obj.objsize) { /* Avoid silly cache filling on the growing edge */\r
3631 if (sync_window(fs) != FR_OK) ABORT(fs, FR_DISK_ERR);\r
3632 fs->winsect = sect;\r
53668523
L
3633 }\r
3634#else\r
70702af1
L
3635 if (fp->sect != sect && /* Fill sector cache with file data */\r
3636 fp->fptr < fp->obj.objsize &&\r
3637 disk_read(fs->drv, fp->buf, sect, 1) != RES_OK) {\r
3638 ABORT(fs, FR_DISK_ERR);\r
53668523
L
3639 }\r
3640#endif\r
70702af1 3641 fp->sect = sect;\r
53668523 3642 }\r
70702af1
L
3643 wcnt = SS(fs) - (UINT)fp->fptr % SS(fs); /* Number of bytes left in the sector */\r
3644 if (wcnt > btw) wcnt = btw; /* Clip it by btw if needed */\r
53668523 3645#if _FS_TINY\r
70702af1
L
3646 if (move_window(fs, fp->sect) != FR_OK) ABORT(fs, FR_DISK_ERR); /* Move sector window */\r
3647 mem_cpy(fs->win + fp->fptr % SS(fs), wbuff, wcnt); /* Fit data to the sector */\r
3648 fs->wflag = 1;\r
53668523 3649#else\r
70702af1
L
3650 mem_cpy(fp->buf + fp->fptr % SS(fs), wbuff, wcnt); /* Fit data to the sector */\r
3651 fp->flag |= FA_DIRTY;\r
53668523
L
3652#endif\r
3653 }\r
3654\r
70702af1 3655 fp->flag |= FA_MODIFIED; /* Set file change flag */\r
53668523 3656\r
70702af1 3657 LEAVE_FF(fs, FR_OK);\r
53668523
L
3658}\r
3659\r
3660\r
3661\r
3662\r
3663/*-----------------------------------------------------------------------*/\r
3664/* Synchronize the File */\r
3665/*-----------------------------------------------------------------------*/\r
3666\r
3667FRESULT f_sync (\r
3668 FIL* fp /* Pointer to the file object */\r
3669)\r
3670{\r
3671 FRESULT res;\r
70702af1 3672 FATFS *fs;\r
53668523
L
3673 DWORD tm;\r
3674 BYTE *dir;\r
70702af1 3675 DEF_NAMBUF\r
53668523
L
3676\r
3677\r
70702af1 3678 res = validate(&fp->obj, &fs); /* Check validity of the file object */\r
53668523 3679 if (res == FR_OK) {\r
70702af1 3680 if (fp->flag & FA_MODIFIED) { /* Is there any change to the file? */\r
53668523 3681#if !_FS_TINY\r
70702af1
L
3682 if (fp->flag & FA_DIRTY) { /* Write-back cached data if needed */\r
3683 if (disk_write(fs->drv, fp->buf, fp->sect, 1) != RES_OK) LEAVE_FF(fs, FR_DISK_ERR);\r
3684 fp->flag &= (BYTE)~FA_DIRTY;\r
53668523
L
3685 }\r
3686#endif\r
3687 /* Update the directory entry */\r
70702af1
L
3688 tm = GET_FATTIME(); /* Modified time */\r
3689#if _FS_EXFAT\r
3690 if (fs->fs_type == FS_EXFAT) {\r
3691 res = fill_fat_chain(&fp->obj); /* Create FAT chain if needed */\r
3692 if (res == FR_OK) {\r
3693 DIR dj;\r
3694\r
3695 INIT_NAMBUF(fs);\r
3696 res = load_obj_dir(&dj, &fp->obj); /* Load directory entry block */\r
3697 if (res == FR_OK) {\r
3698 fs->dirbuf[XDIR_Attr] |= AM_ARC; /* Set archive bit */\r
3699 fs->dirbuf[XDIR_GenFlags] = fp->obj.stat | 1; /* Update file allocation info */\r
3700 st_dword(fs->dirbuf + XDIR_FstClus, fp->obj.sclust);\r
3701 st_qword(fs->dirbuf + XDIR_FileSize, fp->obj.objsize);\r
3702 st_qword(fs->dirbuf + XDIR_ValidFileSize, fp->obj.objsize);\r
3703 st_dword(fs->dirbuf + XDIR_ModTime, tm); /* Update modified time */\r
3704 fs->dirbuf[XDIR_ModTime10] = 0;\r
3705 st_dword(fs->dirbuf + XDIR_AccTime, 0);\r
3706 res = store_xdir(&dj); /* Restore it to the directory */\r
3707 if (res == FR_OK) {\r
3708 res = sync_fs(fs);\r
3709 fp->flag &= (BYTE)~FA_MODIFIED;\r
3710 }\r
3711 }\r
3712 FREE_NAMBUF();\r
3713 }\r
3714 } else\r
3715#endif\r
3716 {\r
3717 res = move_window(fs, fp->dir_sect);\r
3718 if (res == FR_OK) {\r
3719 dir = fp->dir_ptr;\r
3720 dir[DIR_Attr] |= AM_ARC; /* Set archive bit */\r
3721 st_clust(fp->obj.fs, dir, fp->obj.sclust); /* Update file allocation info */\r
3722 st_dword(dir + DIR_FileSize, (DWORD)fp->obj.objsize); /* Update file size */\r
3723 st_dword(dir + DIR_ModTime, tm); /* Update modified time */\r
3724 st_word(dir + DIR_LstAccDate, 0);\r
3725 fs->wflag = 1;\r
3726 res = sync_fs(fs); /* Restore it to the directory */\r
3727 fp->flag &= (BYTE)~FA_MODIFIED;\r
3728 }\r
53668523
L
3729 }\r
3730 }\r
3731 }\r
3732\r
70702af1 3733 LEAVE_FF(fs, res);\r
53668523
L
3734}\r
3735\r
3736#endif /* !_FS_READONLY */\r
3737\r
3738\r
3739\r
3740\r
3741/*-----------------------------------------------------------------------*/\r
3742/* Close File */\r
3743/*-----------------------------------------------------------------------*/\r
3744\r
3745FRESULT f_close (\r
70702af1 3746 FIL* fp /* Pointer to the file object to be closed */\r
53668523
L
3747)\r
3748{\r
3749 FRESULT res;\r
70702af1 3750 FATFS *fs;\r
53668523
L
3751\r
3752#if !_FS_READONLY\r
3753 res = f_sync(fp); /* Flush cached data */\r
3754 if (res == FR_OK)\r
3755#endif\r
3756 {\r
70702af1 3757 res = validate(&fp->obj, &fs); /* Lock volume */\r
53668523 3758 if (res == FR_OK) {\r
70702af1
L
3759#if _FS_LOCK != 0\r
3760 res = dec_lock(fp->obj.lockid); /* Decrement file open counter */\r
53668523
L
3761 if (res == FR_OK)\r
3762#endif\r
70702af1
L
3763 {\r
3764 fp->obj.fs = 0; /* Invalidate file object */\r
3765 }\r
53668523
L
3766#if _FS_REENTRANT\r
3767 unlock_fs(fs, FR_OK); /* Unlock volume */\r
3768#endif\r
3769 }\r
3770 }\r
3771 return res;\r
3772}\r
3773\r
3774\r
3775\r
3776\r
70702af1 3777#if _FS_RPATH >= 1\r
53668523
L
3778/*-----------------------------------------------------------------------*/\r
3779/* Change Current Directory or Current Drive, Get Current Directory */\r
3780/*-----------------------------------------------------------------------*/\r
3781\r
53668523
L
3782#if _VOLUMES >= 2\r
3783FRESULT f_chdrive (\r
3784 const TCHAR* path /* Drive number */\r
3785)\r
3786{\r
3787 int vol;\r
3788\r
3789\r
70702af1 3790 /* Get logical drive number */\r
53668523
L
3791 vol = get_ldnumber(&path);\r
3792 if (vol < 0) return FR_INVALID_DRIVE;\r
3793\r
70702af1 3794 CurrVol = (BYTE)vol; /* Set it as current volume */\r
53668523
L
3795\r
3796 return FR_OK;\r
3797}\r
3798#endif\r
3799\r
3800\r
3801FRESULT f_chdir (\r
3802 const TCHAR* path /* Pointer to the directory path */\r
3803)\r
3804{\r
3805 FRESULT res;\r
3806 DIR dj;\r
70702af1
L
3807 FATFS *fs;\r
3808 DEF_NAMBUF\r
53668523 3809\r
70702af1
L
3810 /* Get logical drive */\r
3811 res = find_volume(&path, &fs, 0);\r
53668523 3812 if (res == FR_OK) {\r
70702af1
L
3813 dj.obj.fs = fs;\r
3814 INIT_NAMBUF(fs);\r
53668523 3815 res = follow_path(&dj, path); /* Follow the path */\r
53668523 3816 if (res == FR_OK) { /* Follow completed */\r
70702af1
L
3817 if (dj.fn[NSFLAG] & NS_NONAME) {\r
3818 fs->cdir = dj.obj.sclust; /* It is the start directory itself */\r
3819#if _FS_EXFAT\r
3820 if (fs->fs_type == FS_EXFAT) {\r
3821 fs->cdc_scl = dj.obj.c_scl;\r
3822 fs->cdc_size = dj.obj.c_size;\r
3823 fs->cdc_ofs = dj.obj.c_ofs;\r
3824 }\r
3825#endif\r
53668523 3826 } else {\r
70702af1
L
3827 if (dj.obj.attr & AM_DIR) { /* It is a sub-directory */\r
3828#if _FS_EXFAT\r
3829 if (fs->fs_type == FS_EXFAT) {\r
3830 fs->cdir = ld_dword(fs->dirbuf + XDIR_FstClus); /* Sub-directory cluster */\r
3831 fs->cdc_scl = dj.obj.sclust; /* Save containing directory information */\r
3832 fs->cdc_size = ((DWORD)dj.obj.objsize & 0xFFFFFF00) | dj.obj.stat;\r
3833 fs->cdc_ofs = dj.blk_ofs;\r
3834 } else\r
3835#endif\r
3836 {\r
3837 fs->cdir = ld_clust(fs, dj.dir); /* Sub-directory cluster */\r
3838 }\r
3839 } else {\r
53668523 3840 res = FR_NO_PATH; /* Reached but a file */\r
70702af1 3841 }\r
53668523
L
3842 }\r
3843 }\r
70702af1 3844 FREE_NAMBUF();\r
53668523
L
3845 if (res == FR_NO_FILE) res = FR_NO_PATH;\r
3846 }\r
3847\r
70702af1 3848 LEAVE_FF(fs, res);\r
53668523
L
3849}\r
3850\r
3851\r
3852#if _FS_RPATH >= 2\r
3853FRESULT f_getcwd (\r
3854 TCHAR* buff, /* Pointer to the directory path */\r
3855 UINT len /* Size of path */\r
3856)\r
3857{\r
3858 FRESULT res;\r
3859 DIR dj;\r
70702af1 3860 FATFS *fs;\r
53668523
L
3861 UINT i, n;\r
3862 DWORD ccl;\r
3863 TCHAR *tp;\r
3864 FILINFO fno;\r
70702af1 3865 DEF_NAMBUF\r
53668523
L
3866\r
3867\r
3868 *buff = 0;\r
70702af1
L
3869 /* Get logical drive */\r
3870 res = find_volume((const TCHAR**)&buff, &fs, 0); /* Get current volume */\r
53668523 3871 if (res == FR_OK) {\r
70702af1
L
3872 dj.obj.fs = fs;\r
3873 INIT_NAMBUF(fs);\r
53668523 3874 i = len; /* Bottom of buffer (directory stack base) */\r
70702af1
L
3875 if (!_FS_EXFAT || fs->fs_type != FS_EXFAT) { /* (Cannot do getcwd on exFAT and returns root path) */\r
3876 dj.obj.sclust = fs->cdir; /* Start to follow upper directory from current directory */\r
3877 while ((ccl = dj.obj.sclust) != 0) { /* Repeat while current directory is a sub-directory */\r
3878 res = dir_sdi(&dj, 1 * SZDIRE); /* Get parent directory */\r
53668523 3879 if (res != FR_OK) break;\r
70702af1
L
3880 res = move_window(fs, dj.sect);\r
3881 if (res != FR_OK) break;\r
3882 dj.obj.sclust = ld_clust(fs, dj.dir); /* Goto parent directory */\r
3883 res = dir_sdi(&dj, 0);\r
3884 if (res != FR_OK) break;\r
3885 do { /* Find the entry links to the child directory */\r
3886 res = dir_read(&dj, 0);\r
3887 if (res != FR_OK) break;\r
3888 if (ccl == ld_clust(fs, dj.dir)) break; /* Found the entry */\r
3889 res = dir_next(&dj, 0);\r
3890 } while (res == FR_OK);\r
3891 if (res == FR_NO_FILE) res = FR_INT_ERR;/* It cannot be 'not found'. */\r
3892 if (res != FR_OK) break;\r
3893 get_fileinfo(&dj, &fno); /* Get the directory name and push it to the buffer */\r
3894 for (n = 0; fno.fname[n]; n++) ;\r
3895 if (i < n + 3) {\r
3896 res = FR_NOT_ENOUGH_CORE; break;\r
3897 }\r
3898 while (n) buff[--i] = fno.fname[--n];\r
3899 buff[--i] = '/';\r
53668523 3900 }\r
53668523
L
3901 }\r
3902 tp = buff;\r
3903 if (res == FR_OK) {\r
3904#if _VOLUMES >= 2\r
3905 *tp++ = '0' + CurrVol; /* Put drive number */\r
3906 *tp++ = ':';\r
3907#endif\r
3908 if (i == len) { /* Root-directory */\r
3909 *tp++ = '/';\r
3910 } else { /* Sub-directroy */\r
3911 do /* Add stacked path str */\r
3912 *tp++ = buff[i++];\r
3913 while (i < len);\r
3914 }\r
3915 }\r
3916 *tp = 0;\r
70702af1 3917 FREE_NAMBUF();\r
53668523
L
3918 }\r
3919\r
70702af1 3920 LEAVE_FF(fs, res);\r
53668523 3921}\r
70702af1 3922\r
53668523
L
3923#endif /* _FS_RPATH >= 2 */\r
3924#endif /* _FS_RPATH >= 1 */\r
3925\r
3926\r
3927\r
3928#if _FS_MINIMIZE <= 2\r
3929/*-----------------------------------------------------------------------*/\r
3930/* Seek File R/W Pointer */\r
3931/*-----------------------------------------------------------------------*/\r
3932\r
3933FRESULT f_lseek (\r
3934 FIL* fp, /* Pointer to the file object */\r
70702af1 3935 FSIZE_t ofs /* File pointer from top of file */\r
53668523
L
3936)\r
3937{\r
3938 FRESULT res;\r
70702af1
L
3939 FATFS *fs;\r
3940 DWORD clst, bcs, nsect;\r
3941 FSIZE_t ifptr;\r
3942#if _USE_FASTSEEK\r
3943 DWORD cl, pcl, ncl, tcl, dsc, tlen, ulen, *tbl;\r
3944#endif\r
53668523 3945\r
70702af1
L
3946 res = validate(&fp->obj, &fs); /* Check validity of the file object */\r
3947 if (res != FR_OK || (res = (FRESULT)fp->err) != FR_OK) LEAVE_FF(fs, res); /* Check validity */\r
53668523
L
3948#if _USE_FASTSEEK\r
3949 if (fp->cltbl) { /* Fast seek */\r
53668523
L
3950 if (ofs == CREATE_LINKMAP) { /* Create CLMT */\r
3951 tbl = fp->cltbl;\r
3952 tlen = *tbl++; ulen = 2; /* Given table size and required table size */\r
70702af1 3953 cl = fp->obj.sclust; /* Origin of the chain */\r
53668523
L
3954 if (cl) {\r
3955 do {\r
3956 /* Get a fragment */\r
3957 tcl = cl; ncl = 0; ulen += 2; /* Top, length and used items */\r
3958 do {\r
3959 pcl = cl; ncl++;\r
70702af1
L
3960 cl = get_fat(&fp->obj, cl);\r
3961 if (cl <= 1) ABORT(fs, FR_INT_ERR);\r
3962 if (cl == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR);\r
53668523
L
3963 } while (cl == pcl + 1);\r
3964 if (ulen <= tlen) { /* Store the length and top of the fragment */\r
3965 *tbl++ = ncl; *tbl++ = tcl;\r
3966 }\r
70702af1 3967 } while (cl < fs->n_fatent); /* Repeat until end of chain */\r
53668523
L
3968 }\r
3969 *fp->cltbl = ulen; /* Number of items used */\r
70702af1 3970 if (ulen <= tlen) {\r
53668523 3971 *tbl = 0; /* Terminate table */\r
70702af1 3972 } else {\r
53668523 3973 res = FR_NOT_ENOUGH_CORE; /* Given table size is smaller than required */\r
70702af1 3974 }\r
53668523 3975 } else { /* Fast seek */\r
70702af1 3976 if (ofs > fp->obj.objsize) ofs = fp->obj.objsize; /* Clip offset at the file size */\r
53668523
L
3977 fp->fptr = ofs; /* Set file pointer */\r
3978 if (ofs) {\r
3979 fp->clust = clmt_clust(fp, ofs - 1);\r
70702af1
L
3980 dsc = clust2sect(fs, fp->clust);\r
3981 if (!dsc) ABORT(fs, FR_INT_ERR);\r
3982 dsc += (DWORD)((ofs - 1) / SS(fs)) & (fs->csize - 1);\r
3983 if (fp->fptr % SS(fs) && dsc != fp->sect) { /* Refill sector cache if needed */\r
53668523
L
3984#if !_FS_TINY\r
3985#if !_FS_READONLY\r
70702af1
L
3986 if (fp->flag & FA_DIRTY) { /* Write-back dirty sector cache */\r
3987 if (disk_write(fs->drv, fp->buf, fp->sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR);\r
3988 fp->flag &= (BYTE)~FA_DIRTY;\r
53668523
L
3989 }\r
3990#endif\r
70702af1 3991 if (disk_read(fs->drv, fp->buf, dsc, 1) != RES_OK) ABORT(fs, FR_DISK_ERR); /* Load current sector */\r
53668523 3992#endif\r
70702af1 3993 fp->sect = dsc;\r
53668523
L
3994 }\r
3995 }\r
3996 }\r
3997 } else\r
3998#endif\r
3999\r
4000 /* Normal Seek */\r
4001 {\r
70702af1
L
4002#if _FS_EXFAT\r
4003 if (fs->fs_type != FS_EXFAT && ofs >= 0x100000000) ofs = 0xFFFFFFFF; /* Clip at 4GiB-1 if at FATxx */\r
53668523 4004#endif\r
70702af1
L
4005 if (ofs > fp->obj.objsize && (_FS_READONLY || !(fp->flag & FA_WRITE))) { /* In read-only mode, clip offset with the file size */\r
4006 ofs = fp->obj.objsize;\r
4007 }\r
53668523
L
4008 ifptr = fp->fptr;\r
4009 fp->fptr = nsect = 0;\r
4010 if (ofs) {\r
70702af1 4011 bcs = (DWORD)fs->csize * SS(fs); /* Cluster size (byte) */\r
53668523
L
4012 if (ifptr > 0 &&\r
4013 (ofs - 1) / bcs >= (ifptr - 1) / bcs) { /* When seek to same or following cluster, */\r
70702af1 4014 fp->fptr = (ifptr - 1) & ~(FSIZE_t)(bcs - 1); /* start from the current cluster */\r
53668523
L
4015 ofs -= fp->fptr;\r
4016 clst = fp->clust;\r
4017 } else { /* When seek to back cluster, */\r
70702af1 4018 clst = fp->obj.sclust; /* start from the first cluster */\r
53668523
L
4019#if !_FS_READONLY\r
4020 if (clst == 0) { /* If no cluster chain, create a new chain */\r
70702af1
L
4021 clst = create_chain(&fp->obj, 0);\r
4022 if (clst == 1) ABORT(fs, FR_INT_ERR);\r
4023 if (clst == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR);\r
4024 fp->obj.sclust = clst;\r
53668523
L
4025 }\r
4026#endif\r
4027 fp->clust = clst;\r
4028 }\r
4029 if (clst != 0) {\r
4030 while (ofs > bcs) { /* Cluster following loop */\r
70702af1 4031 ofs -= bcs; fp->fptr += bcs;\r
53668523
L
4032#if !_FS_READONLY\r
4033 if (fp->flag & FA_WRITE) { /* Check if in write mode or not */\r
70702af1
L
4034 if (_FS_EXFAT && fp->fptr > fp->obj.objsize) { /* No FAT chain object needs correct objsize to generate FAT value */\r
4035 fp->obj.objsize = fp->fptr;\r
4036 fp->flag |= FA_MODIFIED;\r
4037 }\r
4038 clst = create_chain(&fp->obj, clst); /* Follow chain with forceed stretch */\r
4039 if (clst == 0) { /* Clip file size in case of disk full */\r
4040 ofs = 0; break;\r
53668523
L
4041 }\r
4042 } else\r
4043#endif\r
70702af1
L
4044 {\r
4045 clst = get_fat(&fp->obj, clst); /* Follow cluster chain if not in write mode */\r
4046 }\r
4047 if (clst == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR);\r
4048 if (clst <= 1 || clst >= fs->n_fatent) ABORT(fs, FR_INT_ERR);\r
53668523 4049 fp->clust = clst;\r
53668523
L
4050 }\r
4051 fp->fptr += ofs;\r
70702af1
L
4052 if (ofs % SS(fs)) {\r
4053 nsect = clust2sect(fs, clst); /* Current sector */\r
4054 if (!nsect) ABORT(fs, FR_INT_ERR);\r
4055 nsect += (DWORD)(ofs / SS(fs));\r
53668523
L
4056 }\r
4057 }\r
4058 }\r
70702af1
L
4059 if (!_FS_READONLY && fp->fptr > fp->obj.objsize) { /* Set file change flag if the file size is extended */\r
4060 fp->obj.objsize = fp->fptr;\r
4061 fp->flag |= FA_MODIFIED;\r
4062 }\r
4063 if (fp->fptr % SS(fs) && nsect != fp->sect) { /* Fill sector cache if needed */\r
53668523
L
4064#if !_FS_TINY\r
4065#if !_FS_READONLY\r
70702af1
L
4066 if (fp->flag & FA_DIRTY) { /* Write-back dirty sector cache */\r
4067 if (disk_write(fs->drv, fp->buf, fp->sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR);\r
4068 fp->flag &= (BYTE)~FA_DIRTY;\r
53668523
L
4069 }\r
4070#endif\r
70702af1 4071 if (disk_read(fs->drv, fp->buf, nsect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR); /* Fill sector cache */\r
53668523 4072#endif\r
70702af1 4073 fp->sect = nsect;\r
53668523 4074 }\r
53668523
L
4075 }\r
4076\r
70702af1 4077 LEAVE_FF(fs, res);\r
53668523
L
4078}\r
4079\r
4080\r
4081\r
4082#if _FS_MINIMIZE <= 1\r
4083/*-----------------------------------------------------------------------*/\r
4084/* Create a Directory Object */\r
4085/*-----------------------------------------------------------------------*/\r
4086\r
4087FRESULT f_opendir (\r
4088 DIR* dp, /* Pointer to directory object to create */\r
4089 const TCHAR* path /* Pointer to the directory path */\r
4090)\r
4091{\r
4092 FRESULT res;\r
70702af1
L
4093 FATFS *fs;\r
4094 _FDID *obj;\r
4095 DEF_NAMBUF\r
53668523
L
4096\r
4097\r
4098 if (!dp) return FR_INVALID_OBJECT;\r
4099\r
70702af1
L
4100 /* Get logical drive */\r
4101 obj = &dp->obj;\r
4102 res = find_volume(&path, &fs, 0);\r
53668523 4103 if (res == FR_OK) {\r
70702af1
L
4104 obj->fs = fs;\r
4105 INIT_NAMBUF(fs);\r
53668523 4106 res = follow_path(dp, path); /* Follow the path to the directory */\r
53668523 4107 if (res == FR_OK) { /* Follow completed */\r
70702af1
L
4108 if (!(dp->fn[NSFLAG] & NS_NONAME)) { /* It is not the origin directory itself */\r
4109 if (obj->attr & AM_DIR) { /* This object is a sub-directory */\r
4110#if _FS_EXFAT\r
4111 if (fs->fs_type == FS_EXFAT) {\r
4112 obj->c_scl = obj->sclust; /* Save containing directory inforamation */\r
4113 obj->c_size = ((DWORD)obj->objsize & 0xFFFFFF00) | obj->stat;\r
4114 obj->c_ofs = dp->blk_ofs;\r
4115 obj->sclust = ld_dword(fs->dirbuf + XDIR_FstClus); /* Get object location and status */\r
4116 obj->objsize = ld_qword(fs->dirbuf + XDIR_FileSize);\r
4117 obj->stat = fs->dirbuf[XDIR_GenFlags] & 2;\r
4118 } else\r
4119#endif\r
4120 {\r
4121 obj->sclust = ld_clust(fs, dp->dir); /* Get object location */\r
4122 }\r
4123 } else { /* This object is a file */\r
53668523 4124 res = FR_NO_PATH;\r
70702af1 4125 }\r
53668523
L
4126 }\r
4127 if (res == FR_OK) {\r
70702af1 4128 obj->id = fs->id;\r
53668523 4129 res = dir_sdi(dp, 0); /* Rewind directory */\r
70702af1 4130#if _FS_LOCK != 0\r
53668523 4131 if (res == FR_OK) {\r
70702af1
L
4132 if (obj->sclust) {\r
4133 obj->lockid = inc_lock(dp, 0); /* Lock the sub directory */\r
4134 if (!obj->lockid) res = FR_TOO_MANY_OPEN_FILES;\r
53668523 4135 } else {\r
70702af1 4136 obj->lockid = 0; /* Root directory need not to be locked */\r
53668523
L
4137 }\r
4138 }\r
4139#endif\r
4140 }\r
4141 }\r
70702af1 4142 FREE_NAMBUF();\r
53668523
L
4143 if (res == FR_NO_FILE) res = FR_NO_PATH;\r
4144 }\r
70702af1 4145 if (res != FR_OK) obj->fs = 0; /* Invalidate the directory object if function faild */\r
53668523
L
4146\r
4147 LEAVE_FF(fs, res);\r
4148}\r
4149\r
4150\r
4151\r
4152\r
4153/*-----------------------------------------------------------------------*/\r
4154/* Close Directory */\r
4155/*-----------------------------------------------------------------------*/\r
4156\r
4157FRESULT f_closedir (\r
4158 DIR *dp /* Pointer to the directory object to be closed */\r
4159)\r
4160{\r
4161 FRESULT res;\r
70702af1 4162 FATFS *fs;\r
53668523
L
4163\r
4164\r
70702af1 4165 res = validate(&dp->obj, &fs); /* Check validity of the file object */\r
53668523 4166 if (res == FR_OK) {\r
70702af1
L
4167#if _FS_LOCK != 0\r
4168 if (dp->obj.lockid) { /* Decrement sub-directory open counter */\r
4169 res = dec_lock(dp->obj.lockid);\r
4170 }\r
53668523
L
4171 if (res == FR_OK)\r
4172#endif\r
70702af1
L
4173 {\r
4174 dp->obj.fs = 0; /* Invalidate directory object */\r
4175 }\r
53668523
L
4176#if _FS_REENTRANT\r
4177 unlock_fs(fs, FR_OK); /* Unlock volume */\r
4178#endif\r
4179 }\r
4180 return res;\r
4181}\r
4182\r
4183\r
4184\r
4185\r
4186/*-----------------------------------------------------------------------*/\r
4187/* Read Directory Entries in Sequence */\r
4188/*-----------------------------------------------------------------------*/\r
4189\r
4190FRESULT f_readdir (\r
4191 DIR* dp, /* Pointer to the open directory object */\r
4192 FILINFO* fno /* Pointer to file information to return */\r
4193)\r
4194{\r
4195 FRESULT res;\r
70702af1
L
4196 FATFS *fs;\r
4197 DEF_NAMBUF\r
53668523
L
4198\r
4199\r
70702af1 4200 res = validate(&dp->obj, &fs); /* Check validity of the directory object */\r
53668523
L
4201 if (res == FR_OK) {\r
4202 if (!fno) {\r
4203 res = dir_sdi(dp, 0); /* Rewind the directory object */\r
4204 } else {\r
70702af1 4205 INIT_NAMBUF(fs);\r
53668523 4206 res = dir_read(dp, 0); /* Read an item */\r
70702af1 4207 if (res == FR_NO_FILE) res = FR_OK; /* Ignore end of directory */\r
53668523
L
4208 if (res == FR_OK) { /* A valid entry is found */\r
4209 get_fileinfo(dp, fno); /* Get the object information */\r
4210 res = dir_next(dp, 0); /* Increment index for next */\r
70702af1 4211 if (res == FR_NO_FILE) res = FR_OK; /* Ignore end of directory now */\r
53668523 4212 }\r
70702af1 4213 FREE_NAMBUF();\r
53668523
L
4214 }\r
4215 }\r
70702af1
L
4216 LEAVE_FF(fs, res);\r
4217}\r
4218\r
4219\r
4220\r
4221#if _USE_FIND\r
4222/*-----------------------------------------------------------------------*/\r
4223/* Find Next File */\r
4224/*-----------------------------------------------------------------------*/\r
4225\r
4226FRESULT f_findnext (\r
4227 DIR* dp, /* Pointer to the open directory object */\r
4228 FILINFO* fno /* Pointer to the file information structure */\r
4229)\r
4230{\r
4231 FRESULT res;\r
4232\r
4233\r
4234 for (;;) {\r
4235 res = f_readdir(dp, fno); /* Get a directory item */\r
4236 if (res != FR_OK || !fno || !fno->fname[0]) break; /* Terminate if any error or end of directory */\r
4237 if (pattern_matching(dp->pat, fno->fname, 0, 0)) break; /* Test for the file name */\r
4238#if _USE_LFN != 0 && _USE_FIND == 2\r
4239 if (pattern_matching(dp->pat, fno->altname, 0, 0)) break; /* Test for alternative name if exist */\r
4240#endif\r
4241 }\r
4242 return res;\r
4243}\r
4244\r
4245\r
4246\r
4247/*-----------------------------------------------------------------------*/\r
4248/* Find First File */\r
4249/*-----------------------------------------------------------------------*/\r
53668523 4250\r
70702af1
L
4251FRESULT f_findfirst (\r
4252 DIR* dp, /* Pointer to the blank directory object */\r
4253 FILINFO* fno, /* Pointer to the file information structure */\r
4254 const TCHAR* path, /* Pointer to the directory to open */\r
4255 const TCHAR* pattern /* Pointer to the matching pattern */\r
4256)\r
4257{\r
4258 FRESULT res;\r
4259\r
4260\r
4261 dp->pat = pattern; /* Save pointer to pattern string */\r
4262 res = f_opendir(dp, path); /* Open the target directory */\r
4263 if (res == FR_OK) {\r
4264 res = f_findnext(dp, fno); /* Find the first item */\r
4265 }\r
4266 return res;\r
53668523
L
4267}\r
4268\r
70702af1
L
4269#endif /* _USE_FIND */\r
4270\r
53668523
L
4271\r
4272\r
4273#if _FS_MINIMIZE == 0\r
4274/*-----------------------------------------------------------------------*/\r
4275/* Get File Status */\r
4276/*-----------------------------------------------------------------------*/\r
4277\r
4278FRESULT f_stat (\r
4279 const TCHAR* path, /* Pointer to the file path */\r
4280 FILINFO* fno /* Pointer to file information to return */\r
4281)\r
4282{\r
4283 FRESULT res;\r
4284 DIR dj;\r
70702af1 4285 DEF_NAMBUF\r
53668523
L
4286\r
4287\r
70702af1
L
4288 /* Get logical drive */\r
4289 res = find_volume(&path, &dj.obj.fs, 0);\r
53668523 4290 if (res == FR_OK) {\r
70702af1 4291 INIT_NAMBUF(dj.obj.fs);\r
53668523
L
4292 res = follow_path(&dj, path); /* Follow the file path */\r
4293 if (res == FR_OK) { /* Follow completed */\r
70702af1 4294 if (dj.fn[NSFLAG] & NS_NONAME) { /* It is origin directory */\r
53668523 4295 res = FR_INVALID_NAME;\r
70702af1
L
4296 } else { /* Found an object */\r
4297 if (fno) get_fileinfo(&dj, fno);\r
53668523
L
4298 }\r
4299 }\r
70702af1 4300 FREE_NAMBUF();\r
53668523
L
4301 }\r
4302\r
70702af1 4303 LEAVE_FF(dj.obj.fs, res);\r
53668523
L
4304}\r
4305\r
4306\r
4307\r
4308#if !_FS_READONLY\r
4309/*-----------------------------------------------------------------------*/\r
4310/* Get Number of Free Clusters */\r
4311/*-----------------------------------------------------------------------*/\r
4312\r
4313FRESULT f_getfree (\r
4314 const TCHAR* path, /* Path name of the logical drive number */\r
4315 DWORD* nclst, /* Pointer to a variable to return number of free clusters */\r
4316 FATFS** fatfs /* Pointer to return pointer to corresponding file system object */\r
4317)\r
4318{\r
4319 FRESULT res;\r
4320 FATFS *fs;\r
70702af1 4321 DWORD nfree, clst, sect, stat;\r
53668523 4322 UINT i;\r
70702af1
L
4323 BYTE *p;\r
4324 _FDID obj;\r
53668523
L
4325\r
4326\r
70702af1
L
4327 /* Get logical drive */\r
4328 res = find_volume(&path, &fs, 0);\r
53668523 4329 if (res == FR_OK) {\r
70702af1
L
4330 *fatfs = fs; /* Return ptr to the fs object */\r
4331 /* If free_clst is valid, return it without full cluster scan */\r
4332 if (fs->free_clst <= fs->n_fatent - 2) {\r
4333 *nclst = fs->free_clst;\r
53668523
L
4334 } else {\r
4335 /* Get number of free clusters */\r
70702af1
L
4336 nfree = 0;\r
4337 if (fs->fs_type == FS_FAT12) { /* FAT12: Sector unalighed FAT entries */\r
4338 clst = 2; obj.fs = fs;\r
53668523 4339 do {\r
70702af1 4340 stat = get_fat(&obj, clst);\r
53668523
L
4341 if (stat == 0xFFFFFFFF) { res = FR_DISK_ERR; break; }\r
4342 if (stat == 1) { res = FR_INT_ERR; break; }\r
70702af1 4343 if (stat == 0) nfree++;\r
53668523
L
4344 } while (++clst < fs->n_fatent);\r
4345 } else {\r
70702af1
L
4346#if _FS_EXFAT\r
4347 if (fs->fs_type == FS_EXFAT) { /* exFAT: Scan bitmap table */\r
4348 BYTE bm;\r
4349 UINT b;\r
4350\r
4351 clst = fs->n_fatent - 2;\r
4352 sect = fs->database;\r
4353 i = 0;\r
4354 do {\r
4355 if (i == 0 && (res = move_window(fs, sect++)) != FR_OK) break;\r
4356 for (b = 8, bm = fs->win[i]; b && clst; b--, clst--) {\r
4357 if (!(bm & 1)) nfree++;\r
4358 bm >>= 1;\r
4359 }\r
4360 i = (i + 1) % SS(fs);\r
4361 } while (clst);\r
4362 } else\r
4363#endif\r
4364 { /* FAT16/32: Sector alighed FAT entries */\r
4365 clst = fs->n_fatent; sect = fs->fatbase;\r
4366 i = 0; p = 0;\r
4367 do {\r
4368 if (i == 0) {\r
4369 res = move_window(fs, sect++);\r
4370 if (res != FR_OK) break;\r
4371 p = fs->win;\r
4372 i = SS(fs);\r
4373 }\r
4374 if (fs->fs_type == FS_FAT16) {\r
4375 if (ld_word(p) == 0) nfree++;\r
4376 p += 2; i -= 2;\r
4377 } else {\r
4378 if ((ld_dword(p) & 0x0FFFFFFF) == 0) nfree++;\r
4379 p += 4; i -= 4;\r
4380 }\r
4381 } while (--clst);\r
4382 }\r
53668523 4383 }\r
70702af1
L
4384 *nclst = nfree; /* Return the free clusters */\r
4385 fs->free_clst = nfree; /* Now free_clst is valid */\r
4386 fs->fsi_flag |= 1; /* FSInfo is to be updated */\r
53668523
L
4387 }\r
4388 }\r
70702af1 4389\r
53668523
L
4390 LEAVE_FF(fs, res);\r
4391}\r
4392\r
4393\r
4394\r
4395\r
4396/*-----------------------------------------------------------------------*/\r
4397/* Truncate File */\r
4398/*-----------------------------------------------------------------------*/\r
4399\r
4400FRESULT f_truncate (\r
4401 FIL* fp /* Pointer to the file object */\r
4402)\r
4403{\r
4404 FRESULT res;\r
70702af1 4405 FATFS *fs;\r
53668523
L
4406 DWORD ncl;\r
4407\r
4408\r
70702af1
L
4409 res = validate(&fp->obj, &fs); /* Check validity of the file object */\r
4410 if (res != FR_OK || (res = (FRESULT)fp->err) != FR_OK) LEAVE_FF(fs, res);\r
4411 if (!(fp->flag & FA_WRITE)) LEAVE_FF(fs, FR_DENIED); /* Check access mode */\r
4412\r
4413 if (fp->obj.objsize > fp->fptr) {\r
4414 if (fp->fptr == 0) { /* When set file size to zero, remove entire cluster chain */\r
4415 res = remove_chain(&fp->obj, fp->obj.sclust, 0);\r
4416 fp->obj.sclust = 0;\r
4417 } else { /* When truncate a part of the file, remove remaining clusters */\r
4418 ncl = get_fat(&fp->obj, fp->clust);\r
4419 res = FR_OK;\r
4420 if (ncl == 0xFFFFFFFF) res = FR_DISK_ERR;\r
4421 if (ncl == 1) res = FR_INT_ERR;\r
4422 if (res == FR_OK && ncl < fs->n_fatent) {\r
4423 res = remove_chain(&fp->obj, ncl, fp->clust);\r
53668523 4424 }\r
70702af1
L
4425 }\r
4426 fp->obj.objsize = fp->fptr; /* Set file size to current R/W point */\r
4427 fp->flag |= FA_MODIFIED;\r
53668523 4428#if !_FS_TINY\r
70702af1
L
4429 if (res == FR_OK && (fp->flag & FA_DIRTY)) {\r
4430 if (disk_write(fs->drv, fp->buf, fp->sect, 1) != RES_OK) {\r
4431 res = FR_DISK_ERR;\r
4432 } else {\r
4433 fp->flag &= (BYTE)~FA_DIRTY;\r
53668523 4434 }\r
53668523 4435 }\r
70702af1
L
4436#endif\r
4437 if (res != FR_OK) ABORT(fs, res);\r
53668523
L
4438 }\r
4439\r
70702af1 4440 LEAVE_FF(fs, res);\r
53668523
L
4441}\r
4442\r
4443\r
4444\r
4445\r
4446/*-----------------------------------------------------------------------*/\r
70702af1 4447/* Delete a File/Directory */\r
53668523
L
4448/*-----------------------------------------------------------------------*/\r
4449\r
4450FRESULT f_unlink (\r
4451 const TCHAR* path /* Pointer to the file or directory path */\r
4452)\r
4453{\r
4454 FRESULT res;\r
4455 DIR dj, sdj;\r
7b78a5a2 4456 DWORD dclst = 0;\r
70702af1
L
4457 FATFS *fs;\r
4458#if _FS_EXFAT\r
4459 _FDID obj;\r
4460#endif\r
4461 DEF_NAMBUF\r
53668523
L
4462\r
4463\r
70702af1
L
4464 /* Get logical drive */\r
4465 res = find_volume(&path, &fs, FA_WRITE);\r
4466 dj.obj.fs = fs;\r
53668523 4467 if (res == FR_OK) {\r
70702af1 4468 INIT_NAMBUF(fs);\r
53668523 4469 res = follow_path(&dj, path); /* Follow the file path */\r
70702af1 4470 if (_FS_RPATH && res == FR_OK && (dj.fn[NSFLAG] & NS_DOT)) {\r
53668523 4471 res = FR_INVALID_NAME; /* Cannot remove dot entry */\r
70702af1
L
4472 }\r
4473#if _FS_LOCK != 0\r
4474 if (res == FR_OK) res = chk_lock(&dj, 2); /* Check if it is an open object */\r
53668523
L
4475#endif\r
4476 if (res == FR_OK) { /* The object is accessible */\r
70702af1 4477 if (dj.fn[NSFLAG] & NS_NONAME) {\r
7b78a5a2 4478 res = FR_INVALID_NAME; /* Cannot remove the origin directory */\r
53668523 4479 } else {\r
70702af1 4480 if (dj.obj.attr & AM_RDO) {\r
53668523 4481 res = FR_DENIED; /* Cannot remove R/O object */\r
70702af1 4482 }\r
53668523 4483 }\r
70702af1
L
4484 if (res == FR_OK) {\r
4485#if _FS_EXFAT\r
4486 obj.fs = fs;\r
4487 if (fs->fs_type == FS_EXFAT) {\r
4488 obj.sclust = dclst = ld_dword(fs->dirbuf + XDIR_FstClus);\r
4489 obj.objsize = ld_qword(fs->dirbuf + XDIR_FileSize);\r
4490 obj.stat = fs->dirbuf[XDIR_GenFlags] & 2;\r
4491 } else\r
4492#endif\r
4493 {\r
4494 dclst = ld_clust(fs, dj.dir);\r
4495 }\r
4496 if (dj.obj.attr & AM_DIR) { /* Is it a sub-directory ? */\r
4497#if _FS_RPATH != 0\r
4498 if (dclst == fs->cdir) { /* Is it the current directory? */\r
4499 res = FR_DENIED;\r
4500 } else\r
4501#endif\r
4502 {\r
4503 sdj.obj.fs = fs; /* Open the sub-directory */\r
4504 sdj.obj.sclust = dclst;\r
4505#if _FS_EXFAT\r
4506 if (fs->fs_type == FS_EXFAT) {\r
4507 sdj.obj.objsize = obj.objsize;\r
4508 sdj.obj.stat = obj.stat;\r
4509 }\r
53668523 4510#endif\r
70702af1
L
4511 res = dir_sdi(&sdj, 0);\r
4512 if (res == FR_OK) {\r
4513 res = dir_read(&sdj, 0); /* Read an item */\r
4514 if (res == FR_OK) res = FR_DENIED; /* Not empty? */\r
4515 if (res == FR_NO_FILE) res = FR_OK; /* Empty? */\r
4516 }\r
53668523
L
4517 }\r
4518 }\r
4519 }\r
4520 if (res == FR_OK) {\r
70702af1
L
4521 res = dir_remove(&dj); /* Remove the directory entry */\r
4522 if (res == FR_OK && dclst) { /* Remove the cluster chain if exist */\r
4523#if _FS_EXFAT\r
4524 res = remove_chain(&obj, dclst, 0);\r
4525#else\r
4526 res = remove_chain(&dj.obj, dclst, 0);\r
4527#endif\r
4528 }\r
4529 if (res == FR_OK) res = sync_fs(fs);\r
53668523
L
4530 }\r
4531 }\r
70702af1 4532 FREE_NAMBUF();\r
53668523
L
4533 }\r
4534\r
70702af1 4535 LEAVE_FF(fs, res);\r
53668523
L
4536}\r
4537\r
4538\r
4539\r
4540\r
4541/*-----------------------------------------------------------------------*/\r
4542/* Create a Directory */\r
4543/*-----------------------------------------------------------------------*/\r
4544\r
4545FRESULT f_mkdir (\r
4546 const TCHAR* path /* Pointer to the directory path */\r
4547)\r
4548{\r
4549 FRESULT res;\r
4550 DIR dj;\r
70702af1
L
4551 FATFS *fs;\r
4552 BYTE *dir;\r
4553 UINT n;\r
4554 DWORD dsc, dcl, pcl, tm;\r
4555 DEF_NAMBUF\r
53668523
L
4556\r
4557\r
70702af1
L
4558 /* Get logical drive */\r
4559 res = find_volume(&path, &fs, FA_WRITE);\r
4560 dj.obj.fs = fs;\r
53668523 4561 if (res == FR_OK) {\r
70702af1 4562 INIT_NAMBUF(fs);\r
53668523
L
4563 res = follow_path(&dj, path); /* Follow the file path */\r
4564 if (res == FR_OK) res = FR_EXIST; /* Any object with same name is already existing */\r
70702af1 4565 if (_FS_RPATH && res == FR_NO_FILE && (dj.fn[NSFLAG] & NS_DOT)) {\r
53668523 4566 res = FR_INVALID_NAME;\r
70702af1 4567 }\r
53668523 4568 if (res == FR_NO_FILE) { /* Can create a new directory */\r
70702af1
L
4569 dcl = create_chain(&dj.obj, 0); /* Allocate a cluster for the new directory table */\r
4570 dj.obj.objsize = (DWORD)fs->csize * SS(fs);\r
53668523
L
4571 res = FR_OK;\r
4572 if (dcl == 0) res = FR_DENIED; /* No space to allocate a new cluster */\r
4573 if (dcl == 1) res = FR_INT_ERR;\r
4574 if (dcl == 0xFFFFFFFF) res = FR_DISK_ERR;\r
70702af1
L
4575 if (res == FR_OK) res = sync_window(fs); /* Flush FAT */\r
4576 tm = GET_FATTIME();\r
53668523 4577 if (res == FR_OK) { /* Initialize the new directory table */\r
70702af1
L
4578 dsc = clust2sect(fs, dcl);\r
4579 dir = fs->win;\r
4580 mem_set(dir, 0, SS(fs));\r
4581 if (!_FS_EXFAT || fs->fs_type != FS_EXFAT) {\r
4582 mem_set(dir + DIR_Name, ' ', 11); /* Create "." entry */\r
4583 dir[DIR_Name] = '.';\r
4584 dir[DIR_Attr] = AM_DIR;\r
4585 st_dword(dir + DIR_ModTime, tm);\r
4586 st_clust(fs, dir, dcl);\r
4587 mem_cpy(dir + SZDIRE, dir, SZDIRE); /* Create ".." entry */\r
4588 dir[SZDIRE + 1] = '.'; pcl = dj.obj.sclust;\r
4589 if (fs->fs_type == FS_FAT32 && pcl == fs->dirbase) pcl = 0;\r
4590 st_clust(fs, dir + SZDIRE, pcl);\r
4591 }\r
4592 for (n = fs->csize; n; n--) { /* Write dot entries and clear following sectors */\r
4593 fs->winsect = dsc++;\r
4594 fs->wflag = 1;\r
4595 res = sync_window(fs);\r
53668523 4596 if (res != FR_OK) break;\r
70702af1 4597 mem_set(dir, 0, SS(fs));\r
53668523
L
4598 }\r
4599 }\r
4600 if (res == FR_OK) res = dir_register(&dj); /* Register the object to the directoy */\r
70702af1
L
4601 if (res == FR_OK) {\r
4602#if _FS_EXFAT\r
4603 if (fs->fs_type == FS_EXFAT) { /* Initialize directory entry block */\r
4604 st_dword(fs->dirbuf + XDIR_ModTime, tm); /* Created time */\r
4605 st_dword(fs->dirbuf + XDIR_FstClus, dcl); /* Table start cluster */\r
4606 st_dword(fs->dirbuf + XDIR_FileSize, (DWORD)dj.obj.objsize); /* File size needs to be valid */\r
4607 st_dword(fs->dirbuf + XDIR_ValidFileSize, (DWORD)dj.obj.objsize);\r
4608 fs->dirbuf[XDIR_GenFlags] = 3; /* Initialize the object flag (contiguous) */\r
4609 fs->dirbuf[XDIR_Attr] = AM_DIR; /* Attribute */\r
4610 res = store_xdir(&dj);\r
4611 } else\r
4612#endif\r
4613 {\r
4614 dir = dj.dir;\r
4615 st_dword(dir + DIR_ModTime, tm); /* Created time */\r
4616 st_clust(fs, dir, dcl); /* Table start cluster */\r
4617 dir[DIR_Attr] = AM_DIR; /* Attribute */\r
4618 fs->wflag = 1;\r
4619 }\r
4620 if (res == FR_OK) res = sync_fs(fs);\r
53668523 4621 } else {\r
70702af1 4622 remove_chain(&dj.obj, dcl, 0); /* Could not register, remove cluster chain */\r
53668523
L
4623 }\r
4624 }\r
70702af1 4625 FREE_NAMBUF();\r
53668523
L
4626 }\r
4627\r
70702af1 4628 LEAVE_FF(fs, res);\r
53668523
L
4629}\r
4630\r
4631\r
4632\r
4633\r
4634/*-----------------------------------------------------------------------*/\r
70702af1 4635/* Rename a File/Directory */\r
53668523
L
4636/*-----------------------------------------------------------------------*/\r
4637\r
70702af1
L
4638FRESULT f_rename (\r
4639 const TCHAR* path_old, /* Pointer to the object name to be renamed */\r
4640 const TCHAR* path_new /* Pointer to the new name */\r
53668523
L
4641)\r
4642{\r
4643 FRESULT res;\r
70702af1
L
4644 DIR djo, djn;\r
4645 FATFS *fs;\r
4646 BYTE buf[_FS_EXFAT ? SZDIRE * 2 : 24], *dir;\r
4647 DWORD dw;\r
4648 DEF_NAMBUF\r
53668523
L
4649\r
4650\r
70702af1
L
4651 get_ldnumber(&path_new); /* Ignore drive number of new name */\r
4652 res = find_volume(&path_old, &fs, FA_WRITE); /* Get logical drive of the old object */\r
53668523 4653 if (res == FR_OK) {\r
70702af1
L
4654 djo.obj.fs = fs;\r
4655 INIT_NAMBUF(fs);\r
4656 res = follow_path(&djo, path_old); /* Check old object */\r
4657 if (res == FR_OK && (djo.fn[NSFLAG] & (NS_DOT | NS_NONAME))) res = FR_INVALID_NAME; /* Check validity of name */\r
4658#if _FS_LOCK != 0\r
4659 if (res == FR_OK) res = chk_lock(&djo, 2);\r
4660#endif\r
4661 if (res == FR_OK) { /* Object to be renamed is found */\r
4662#if _FS_EXFAT\r
4663 if (fs->fs_type == FS_EXFAT) { /* At exFAT */\r
4664 BYTE nf, nn;\r
4665 WORD nh;\r
4666\r
4667 mem_cpy(buf, fs->dirbuf, SZDIRE * 2); /* Save 85+C0 entry of old object */\r
4668 mem_cpy(&djn, &djo, sizeof djo);\r
4669 res = follow_path(&djn, path_new); /* Make sure if new object name is not in use */\r
4670 if (res == FR_OK) { /* Is new name already in use by any other object? */\r
4671 res = (djn.obj.sclust == djo.obj.sclust && djn.dptr == djo.dptr) ? FR_NO_FILE : FR_EXIST;\r
4672 }\r
4673 if (res == FR_NO_FILE) { /* It is a valid path and no name collision */\r
4674 res = dir_register(&djn); /* Register the new entry */\r
4675 if (res == FR_OK) {\r
4676 nf = fs->dirbuf[XDIR_NumSec]; nn = fs->dirbuf[XDIR_NumName];\r
4677 nh = ld_word(fs->dirbuf + XDIR_NameHash);\r
4678 mem_cpy(fs->dirbuf, buf, SZDIRE * 2);\r
4679 fs->dirbuf[XDIR_NumSec] = nf; fs->dirbuf[XDIR_NumName] = nn;\r
4680 st_word(fs->dirbuf + XDIR_NameHash, nh);\r
4681/* Start of critical section where any interruption can cause a cross-link */\r
4682 res = store_xdir(&djn);\r
4683 }\r
4684 }\r
4685 } else\r
4686#endif\r
4687 { /* At FAT12/FAT16/FAT32 */\r
4688 mem_cpy(buf, djo.dir + DIR_Attr, 21); /* Save information about the object except name */\r
4689 mem_cpy(&djn, &djo, sizeof (DIR)); /* Duplicate the directory object */\r
4690 res = follow_path(&djn, path_new); /* Make sure if new object name is not in use */\r
4691 if (res == FR_OK) { /* Is new name already in use by any other object? */\r
4692 res = (djn.obj.sclust == djo.obj.sclust && djn.dptr == djo.dptr) ? FR_NO_FILE : FR_EXIST;\r
4693 }\r
4694 if (res == FR_NO_FILE) { /* It is a valid path and no name collision */\r
4695 res = dir_register(&djn); /* Register the new entry */\r
4696 if (res == FR_OK) {\r
4697 dir = djn.dir; /* Copy information about object except name */\r
4698 mem_cpy(dir + 13, buf + 2, 19);\r
4699 dir[DIR_Attr] = buf[0] | AM_ARC;\r
4700 fs->wflag = 1;\r
4701 if ((dir[DIR_Attr] & AM_DIR) && djo.obj.sclust != djn.obj.sclust) { /* Update .. entry in the sub-directory if needed */\r
4702 dw = clust2sect(fs, ld_clust(fs, dir));\r
4703 if (!dw) {\r
4704 res = FR_INT_ERR;\r
4705 } else {\r
4706/* Start of critical section where any interruption can cause a cross-link */\r
4707 res = move_window(fs, dw);\r
4708 dir = fs->win + SZDIRE * 1; /* Ptr to .. entry */\r
4709 if (res == FR_OK && dir[1] == '.') {\r
4710 st_clust(fs, dir, djn.obj.sclust);\r
4711 fs->wflag = 1;\r
4712 }\r
4713 }\r
4714 }\r
4715 }\r
4716 }\r
4717 }\r
4718 if (res == FR_OK) {\r
4719 res = dir_remove(&djo); /* Remove old entry */\r
4720 if (res == FR_OK) {\r
4721 res = sync_fs(fs);\r
4722 }\r
53668523 4723 }\r
70702af1 4724/* End of critical section */\r
53668523 4725 }\r
70702af1 4726 FREE_NAMBUF();\r
53668523
L
4727 }\r
4728\r
70702af1 4729 LEAVE_FF(fs, res);\r
53668523
L
4730}\r
4731\r
70702af1
L
4732#endif /* !_FS_READONLY */\r
4733#endif /* _FS_MINIMIZE == 0 */\r
4734#endif /* _FS_MINIMIZE <= 1 */\r
4735#endif /* _FS_MINIMIZE <= 2 */\r
53668523
L
4736\r
4737\r
4738\r
70702af1 4739#if _USE_CHMOD && !_FS_READONLY\r
53668523 4740/*-----------------------------------------------------------------------*/\r
70702af1 4741/* Change Attribute */\r
53668523
L
4742/*-----------------------------------------------------------------------*/\r
4743\r
70702af1
L
4744FRESULT f_chmod (\r
4745 const TCHAR* path, /* Pointer to the file path */\r
4746 BYTE attr, /* Attribute bits */\r
4747 BYTE mask /* Attribute mask to change */\r
53668523
L
4748)\r
4749{\r
4750 FRESULT res;\r
70702af1
L
4751 DIR dj;\r
4752 FATFS *fs;\r
4753 DEF_NAMBUF\r
53668523
L
4754\r
4755\r
70702af1
L
4756 res = find_volume(&path, &fs, FA_WRITE); /* Get logical drive */\r
4757 dj.obj.fs = fs;\r
53668523 4758 if (res == FR_OK) {\r
70702af1
L
4759 INIT_NAMBUF(fs);\r
4760 res = follow_path(&dj, path); /* Follow the file path */\r
4761 if (res == FR_OK && (dj.fn[NSFLAG] & (NS_DOT | NS_NONAME))) res = FR_INVALID_NAME; /* Check object validity */\r
4762 if (res == FR_OK) {\r
4763 mask &= AM_RDO|AM_HID|AM_SYS|AM_ARC; /* Valid attribute mask */\r
4764#if _FS_EXFAT\r
4765 if (fs->fs_type == FS_EXFAT) {\r
4766 fs->dirbuf[XDIR_Attr] = (attr & mask) | (fs->dirbuf[XDIR_Attr] & (BYTE)~mask); /* Apply attribute change */\r
4767 res = store_xdir(&dj);\r
4768 } else\r
53668523 4769#endif\r
70702af1
L
4770 {\r
4771 dj.dir[DIR_Attr] = (attr & mask) | (dj.dir[DIR_Attr] & (BYTE)~mask); /* Apply attribute change */\r
4772 fs->wflag = 1;\r
53668523 4773 }\r
70702af1 4774 if (res == FR_OK) res = sync_fs(fs);\r
53668523 4775 }\r
70702af1 4776 FREE_NAMBUF();\r
53668523
L
4777 }\r
4778\r
70702af1 4779 LEAVE_FF(fs, res);\r
53668523
L
4780}\r
4781\r
7b78a5a2
L
4782\r
4783\r
4784\r
4785/*-----------------------------------------------------------------------*/\r
4786/* Change Timestamp */\r
4787/*-----------------------------------------------------------------------*/\r
4788\r
4789FRESULT f_utime (\r
4790 const TCHAR* path, /* Pointer to the file/directory name */\r
4791 const FILINFO* fno /* Pointer to the time stamp to be set */\r
4792)\r
4793{\r
4794 FRESULT res;\r
4795 DIR dj;\r
70702af1
L
4796 FATFS *fs;\r
4797 DEF_NAMBUF\r
7b78a5a2
L
4798\r
4799\r
70702af1
L
4800 res = find_volume(&path, &fs, FA_WRITE); /* Get logical drive */\r
4801 dj.obj.fs = fs;\r
7b78a5a2 4802 if (res == FR_OK) {\r
70702af1 4803 INIT_NAMBUF(fs);\r
7b78a5a2 4804 res = follow_path(&dj, path); /* Follow the file path */\r
70702af1 4805 if (res == FR_OK && (dj.fn[NSFLAG] & (NS_DOT | NS_NONAME))) res = FR_INVALID_NAME; /* Check object validity */\r
7b78a5a2 4806 if (res == FR_OK) {\r
70702af1
L
4807#if _FS_EXFAT\r
4808 if (fs->fs_type == FS_EXFAT) {\r
4809 st_dword(fs->dirbuf + XDIR_ModTime, (DWORD)fno->fdate << 16 | fno->ftime);\r
4810 res = store_xdir(&dj);\r
4811 } else\r
4812#endif\r
4813 {\r
4814 st_dword(dj.dir + DIR_ModTime, (DWORD)fno->fdate << 16 | fno->ftime);\r
4815 fs->wflag = 1;\r
7b78a5a2 4816 }\r
70702af1 4817 if (res == FR_OK) res = sync_fs(fs);\r
7b78a5a2 4818 }\r
70702af1 4819 FREE_NAMBUF();\r
7b78a5a2
L
4820 }\r
4821\r
70702af1 4822 LEAVE_FF(fs, res);\r
7b78a5a2
L
4823}\r
4824\r
70702af1 4825#endif /* _USE_CHMOD && !_FS_READONLY */\r
53668523
L
4826\r
4827\r
4828\r
4829#if _USE_LABEL\r
4830/*-----------------------------------------------------------------------*/\r
70702af1 4831/* Get Volume Label */\r
53668523
L
4832/*-----------------------------------------------------------------------*/\r
4833\r
4834FRESULT f_getlabel (\r
4835 const TCHAR* path, /* Path name of the logical drive number */\r
4836 TCHAR* label, /* Pointer to a buffer to return the volume label */\r
4837 DWORD* vsn /* Pointer to a variable to return the volume serial number */\r
4838)\r
4839{\r
4840 FRESULT res;\r
4841 DIR dj;\r
70702af1
L
4842 FATFS *fs;\r
4843 UINT si, di;\r
4844#if _LFN_UNICODE || _FS_EXFAT\r
4845 WCHAR w;\r
4846#endif\r
53668523 4847\r
70702af1
L
4848 /* Get logical drive */\r
4849 res = find_volume(&path, &fs, 0);\r
53668523
L
4850\r
4851 /* Get volume label */\r
4852 if (res == FR_OK && label) {\r
70702af1 4853 dj.obj.fs = fs; dj.obj.sclust = 0; /* Open root directory */\r
53668523
L
4854 res = dir_sdi(&dj, 0);\r
4855 if (res == FR_OK) {\r
70702af1
L
4856 res = dir_read(&dj, 1); /* Find a volume label entry */\r
4857 if (res == FR_OK) {\r
4858#if _FS_EXFAT\r
4859 if (fs->fs_type == FS_EXFAT) {\r
4860 for (si = di = 0; si < dj.dir[XDIR_NumLabel]; si++) { /* Extract volume label from 83 entry */\r
4861 w = ld_word(dj.dir + XDIR_Label + si * 2);\r
4862#if _LFN_UNICODE\r
4863 label[di++] = w;\r
53668523 4864#else\r
70702af1
L
4865 w = ff_convert(w, 0); /* Unicode -> OEM */\r
4866 if (w == 0) w = '?'; /* Replace wrong character */\r
4867 if (_DF1S && w >= 0x100) label[di++] = (char)(w >> 8);\r
4868 label[di++] = (char)w;\r
53668523 4869#endif\r
70702af1
L
4870 }\r
4871 label[di] = 0;\r
4872 } else\r
4873#endif\r
4874 {\r
4875 si = di = 0; /* Extract volume label from AM_VOL entry with code comversion */\r
4876 do {\r
4877#if _LFN_UNICODE\r
4878 w = (si < 11) ? dj.dir[si++] : ' ';\r
4879 if (IsDBCS1(w) && si < 11 && IsDBCS2(dj.dir[si])) {\r
4880 w = w << 8 | dj.dir[si++];\r
4881 }\r
4882 label[di++] = ff_convert(w, 1); /* OEM -> Unicode */\r
4883#else\r
4884 label[di++] = dj.dir[si++];\r
4885#endif\r
4886 } while (di < 11);\r
4887 do { /* Truncate trailing spaces */\r
4888 label[di] = 0;\r
4889 if (di == 0) break;\r
4890 } while (label[--di] == ' ');\r
4891 }\r
53668523
L
4892 }\r
4893 }\r
70702af1
L
4894 if (res == FR_NO_FILE) { /* No label entry and return nul string */\r
4895 label[0] = 0;\r
4896 res = FR_OK;\r
4897 }\r
53668523
L
4898 }\r
4899\r
4900 /* Get volume serial number */\r
4901 if (res == FR_OK && vsn) {\r
70702af1 4902 res = move_window(fs, fs->volbase);\r
53668523 4903 if (res == FR_OK) {\r
70702af1
L
4904 switch (fs->fs_type) {\r
4905 case FS_EXFAT: di = BPB_VolIDEx; break;\r
4906 case FS_FAT32: di = BS_VolID32; break;\r
4907 default: di = BS_VolID;\r
4908 }\r
4909 *vsn = ld_dword(fs->win + di);\r
53668523
L
4910 }\r
4911 }\r
4912\r
70702af1 4913 LEAVE_FF(fs, res);\r
53668523
L
4914}\r
4915\r
4916\r
4917\r
4918#if !_FS_READONLY\r
4919/*-----------------------------------------------------------------------*/\r
70702af1 4920/* Set Volume Label */\r
53668523
L
4921/*-----------------------------------------------------------------------*/\r
4922\r
4923FRESULT f_setlabel (\r
4924 const TCHAR* label /* Pointer to the volume label to set */\r
4925)\r
4926{\r
4927 FRESULT res;\r
4928 DIR dj;\r
70702af1
L
4929 FATFS *fs;\r
4930 BYTE dirvn[22];\r
4931 UINT i, j, slen;\r
53668523 4932 WCHAR w;\r
70702af1 4933 static const char badchr[] = "\"*+,.:;<=>\?[]|\x7F";\r
53668523
L
4934\r
4935\r
70702af1
L
4936 /* Get logical drive */\r
4937 res = find_volume(&label, &fs, FA_WRITE);\r
4938 if (res != FR_OK) LEAVE_FF(fs, res);\r
4939 dj.obj.fs = fs;\r
4940\r
4941 /* Get length of given volume label */\r
4942 for (slen = 0; (UINT)label[slen] >= ' '; slen++) ; /* Get name length */\r
4943\r
4944#if _FS_EXFAT\r
4945 if (fs->fs_type == FS_EXFAT) { /* On the exFAT volume */\r
4946 for (i = j = 0; i < slen; ) { /* Create volume label in directory form */\r
4947 w = label[i++];\r
4948#if !_LFN_UNICODE\r
4949 if (IsDBCS1(w)) {\r
4950 w = (i < slen && IsDBCS2(label[i])) ? w << 8 | (BYTE)label[i++] : 0;\r
4951 }\r
4952 w = ff_convert(w, 1);\r
4953#endif\r
4954 if (w == 0 || chk_chr(badchr, w) || j == 22) { /* Check validity check validity of the volume label */\r
4955 LEAVE_FF(fs, FR_INVALID_NAME);\r
4956 }\r
4957 st_word(dirvn + j, w); j += 2;\r
4958 }\r
4959 slen = j;\r
4960 } else\r
4961#endif\r
4962 { /* On the FAT12/16/32 volume */\r
4963 for ( ; slen && label[slen - 1] == ' '; slen--) ; /* Remove trailing spaces */\r
4964 if (slen) { /* Is there a volume label to be set? */\r
4965 dirvn[0] = 0; i = j = 0; /* Create volume label in directory form */\r
4966 do {\r
4967#if _LFN_UNICODE\r
4968 w = ff_convert(ff_wtoupper(label[i++]), 0);\r
53668523 4969#else\r
70702af1
L
4970 w = (BYTE)label[i++];\r
4971 if (IsDBCS1(w)) {\r
4972 w = (j < 10 && i < slen && IsDBCS2(label[i])) ? w << 8 | (BYTE)label[i++] : 0;\r
4973 }\r
4974#if _USE_LFN != 0\r
4975 w = ff_convert(ff_wtoupper(ff_convert(w, 1)), 0);\r
53668523 4976#else\r
70702af1 4977 if (IsLower(w)) w -= 0x20; /* To upper ASCII characters */\r
53668523 4978#ifdef _EXCVT\r
70702af1 4979 if (w >= 0x80) w = ExCvt[w - 0x80]; /* To upper extended characters (SBCS cfg) */\r
53668523 4980#else\r
70702af1 4981 if (!_DF1S && w >= 0x80) w = 0; /* Reject extended characters (ASCII cfg) */\r
53668523
L
4982#endif\r
4983#endif\r
4984#endif\r
70702af1
L
4985 if (w == 0 || chk_chr(badchr, w) || j >= (UINT)((w >= 0x100) ? 10 : 11)) { /* Reject invalid characters for volume label */\r
4986 LEAVE_FF(fs, FR_INVALID_NAME);\r
4987 }\r
4988 if (w >= 0x100) dirvn[j++] = (BYTE)(w >> 8);\r
4989 dirvn[j++] = (BYTE)w;\r
4990 } while (i < slen);\r
4991 while (j < 11) dirvn[j++] = ' '; /* Fill remaining name field */\r
4992 if (dirvn[0] == DDEM) LEAVE_FF(fs, FR_INVALID_NAME); /* Reject illegal name (heading DDEM) */\r
4993 }\r
53668523
L
4994 }\r
4995\r
4996 /* Set volume label */\r
70702af1 4997 dj.obj.sclust = 0; /* Open root directory */\r
53668523
L
4998 res = dir_sdi(&dj, 0);\r
4999 if (res == FR_OK) {\r
70702af1
L
5000 res = dir_read(&dj, 1); /* Get volume label entry */\r
5001 if (res == FR_OK) {\r
5002 if (_FS_EXFAT && fs->fs_type == FS_EXFAT) {\r
5003 dj.dir[XDIR_NumLabel] = (BYTE)(slen / 2); /* Change the volume label */\r
5004 mem_cpy(dj.dir + XDIR_Label, dirvn, slen);\r
53668523 5005 } else {\r
70702af1
L
5006 if (slen) {\r
5007 mem_cpy(dj.dir, dirvn, 11); /* Change the volume label */\r
5008 } else {\r
5009 dj.dir[DIR_Name] = DDEM; /* Remove the volume label */\r
5010 }\r
53668523 5011 }\r
70702af1
L
5012 fs->wflag = 1;\r
5013 res = sync_fs(fs);\r
5014 } else { /* No volume label entry is found or error */\r
53668523
L
5015 if (res == FR_NO_FILE) {\r
5016 res = FR_OK;\r
70702af1
L
5017 if (slen) { /* Create a volume label entry */\r
5018 res = dir_alloc(&dj, 1); /* Allocate an entry */\r
53668523 5019 if (res == FR_OK) {\r
70702af1
L
5020 mem_set(dj.dir, 0, SZDIRE); /* Clear the entry */\r
5021 if (_FS_EXFAT && fs->fs_type == FS_EXFAT) {\r
5022 dj.dir[XDIR_Type] = 0x83; /* Create 83 entry */\r
5023 dj.dir[XDIR_NumLabel] = (BYTE)(slen / 2);\r
5024 mem_cpy(dj.dir + XDIR_Label, dirvn, slen);\r
5025 } else {\r
5026 dj.dir[DIR_Attr] = AM_VOL; /* Create volume label entry */\r
5027 mem_cpy(dj.dir, dirvn, 11);\r
5028 }\r
5029 fs->wflag = 1;\r
5030 res = sync_fs(fs);\r
53668523
L
5031 }\r
5032 }\r
5033 }\r
5034 }\r
5035 }\r
5036\r
70702af1 5037 LEAVE_FF(fs, res);\r
53668523
L
5038}\r
5039\r
5040#endif /* !_FS_READONLY */\r
5041#endif /* _USE_LABEL */\r
5042\r
5043\r
5044\r
70702af1
L
5045#if _USE_EXPAND && !_FS_READONLY\r
5046/*-----------------------------------------------------------------------*/\r
5047/* Allocate a Contiguous Blocks to the File */\r
5048/*-----------------------------------------------------------------------*/\r
5049\r
5050FRESULT f_expand (\r
5051 FIL* fp, /* Pointer to the file object */\r
5052 FSIZE_t fsz, /* File size to be expanded to */\r
5053 BYTE opt /* Operation mode 0:Find and prepare or 1:Find and allocate */\r
5054)\r
5055{\r
5056 FRESULT res;\r
5057 FATFS *fs;\r
5058 DWORD n, clst, stcl, scl, ncl, tcl, lclst;\r
5059\r
5060\r
5061 res = validate(&fp->obj, &fs); /* Check validity of the file object */\r
5062 if (res != FR_OK || (res = (FRESULT)fp->err) != FR_OK) LEAVE_FF(fs, res);\r
5063 if (fsz == 0 || fp->obj.objsize != 0 || !(fp->flag & FA_WRITE)) LEAVE_FF(fs, FR_DENIED);\r
5064#if _FS_EXFAT\r
5065 if (fs->fs_type != FS_EXFAT && fsz >= 0x100000000) LEAVE_FF(fs, FR_DENIED); /* Check if in size limit */\r
5066#endif\r
5067 n = (DWORD)fs->csize * SS(fs); /* Cluster size */\r
5068 tcl = (DWORD)(fsz / n) + ((fsz & (n - 1)) ? 1 : 0); /* Number of clusters required */\r
5069 stcl = fs->last_clst; lclst = 0;\r
5070 if (stcl < 2 || stcl >= fs->n_fatent) stcl = 2;\r
5071\r
5072#if _FS_EXFAT\r
5073 if (fs->fs_type == FS_EXFAT) {\r
5074 scl = find_bitmap(fs, stcl, tcl); /* Find a contiguous cluster block */\r
5075 if (scl == 0) res = FR_DENIED; /* No contiguous cluster block was found */\r
5076 if (scl == 0xFFFFFFFF) res = FR_DISK_ERR;\r
5077 if (res == FR_OK) {\r
5078 if (opt) {\r
5079 res = change_bitmap(fs, scl, tcl, 1); /* Mark the cluster block 'in use' */\r
5080 lclst = scl + tcl - 1;\r
5081 } else {\r
5082 lclst = scl - 1;\r
5083 }\r
5084 }\r
5085 } else\r
5086#endif\r
5087 {\r
5088 scl = clst = stcl; ncl = 0;\r
5089 for (;;) { /* Find a contiguous cluster block */\r
5090 n = get_fat(&fp->obj, clst);\r
5091 if (++clst >= fs->n_fatent) clst = 2;\r
5092 if (n == 1) { res = FR_INT_ERR; break; }\r
5093 if (n == 0xFFFFFFFF) { res = FR_DISK_ERR; break; }\r
5094 if (n == 0) { /* Is it a free cluster? */\r
5095 if (++ncl == tcl) break; /* Break if a contiguous cluster block is found */\r
5096 } else {\r
5097 scl = clst; ncl = 0; /* Not a free cluster */\r
5098 }\r
5099 if (clst == stcl) { res = FR_DENIED; break; } /* No contiguous cluster? */\r
5100 }\r
5101 if (res == FR_OK) {\r
5102 if (opt) {\r
5103 for (clst = scl, n = tcl; n; clst++, n--) { /* Create a cluster chain on the FAT */\r
5104 res = put_fat(fs, clst, (n == 1) ? 0xFFFFFFFF : clst + 1);\r
5105 if (res != FR_OK) break;\r
5106 lclst = clst;\r
5107 }\r
5108 } else {\r
5109 lclst = scl - 1;\r
5110 }\r
5111 }\r
5112 }\r
5113\r
5114 if (res == FR_OK) {\r
5115 fs->last_clst = lclst; /* Set suggested start cluster to start next */\r
5116 if (opt) {\r
5117 fp->obj.sclust = scl; /* Update object allocation information */\r
5118 fp->obj.objsize = fsz;\r
5119 if (_FS_EXFAT) fp->obj.stat = 2; /* Set status 'contiguous chain' */\r
5120 fp->flag |= FA_MODIFIED;\r
5121 if (fs->free_clst < fs->n_fatent - 2) { /* Update FSINFO */\r
5122 fs->free_clst -= tcl;\r
5123 fs->fsi_flag |= 1;\r
5124 }\r
5125 }\r
5126 }\r
5127\r
5128 LEAVE_FF(fs, res);\r
5129}\r
5130\r
5131#endif /* _USE_EXPAND && !_FS_READONLY */\r
5132\r
5133\r
5134\r
5135#if _USE_FORWARD\r
53668523 5136/*-----------------------------------------------------------------------*/\r
70702af1 5137/* Forward data to the stream directly */\r
53668523 5138/*-----------------------------------------------------------------------*/\r
53668523
L
5139\r
5140FRESULT f_forward (\r
5141 FIL* fp, /* Pointer to the file object */\r
5142 UINT (*func)(const BYTE*,UINT), /* Pointer to the streaming function */\r
5143 UINT btf, /* Number of bytes to forward */\r
5144 UINT* bf /* Pointer to number of bytes forwarded */\r
5145)\r
5146{\r
5147 FRESULT res;\r
70702af1
L
5148 FATFS *fs;\r
5149 DWORD clst, sect;\r
5150 FSIZE_t remain;\r
5151 UINT rcnt, csect;\r
5152 BYTE *dbuf;\r
53668523
L
5153\r
5154\r
5155 *bf = 0; /* Clear transfer byte counter */\r
70702af1
L
5156 res = validate(&fp->obj, &fs); /* Check validity of the file object */\r
5157 if (res != FR_OK || (res = (FRESULT)fp->err) != FR_OK) LEAVE_FF(fs, res);\r
5158 if (!(fp->flag & FA_READ)) LEAVE_FF(fs, FR_DENIED); /* Check access mode */\r
53668523 5159\r
70702af1 5160 remain = fp->obj.objsize - fp->fptr;\r
53668523
L
5161 if (btf > remain) btf = (UINT)remain; /* Truncate btf by remaining bytes */\r
5162\r
70702af1 5163 for ( ; btf && (*func)(0, 0); /* Repeat until all data transferred or stream goes busy */\r
53668523 5164 fp->fptr += rcnt, *bf += rcnt, btf -= rcnt) {\r
70702af1
L
5165 csect = (UINT)(fp->fptr / SS(fs) & (fs->csize - 1)); /* Sector offset in the cluster */\r
5166 if (fp->fptr % SS(fs) == 0) { /* On the sector boundary? */\r
5167 if (csect == 0) { /* On the cluster boundary? */\r
53668523 5168 clst = (fp->fptr == 0) ? /* On the top of the file? */\r
70702af1
L
5169 fp->obj.sclust : get_fat(&fp->obj, fp->clust);\r
5170 if (clst <= 1) ABORT(fs, FR_INT_ERR);\r
5171 if (clst == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR);\r
53668523
L
5172 fp->clust = clst; /* Update current cluster */\r
5173 }\r
5174 }\r
70702af1
L
5175 sect = clust2sect(fs, fp->clust); /* Get current data sector */\r
5176 if (!sect) ABORT(fs, FR_INT_ERR);\r
53668523 5177 sect += csect;\r
70702af1
L
5178#if _FS_TINY\r
5179 if (move_window(fs, sect) != FR_OK) ABORT(fs, FR_DISK_ERR); /* Move sector window to the file data */\r
5180 dbuf = fs->win;\r
5181#else\r
5182 if (fp->sect != sect) { /* Fill sector cache with file data */\r
5183#if !_FS_READONLY\r
5184 if (fp->flag & FA_DIRTY) { /* Write-back dirty sector cache */\r
5185 if (disk_write(fs->drv, fp->buf, fp->sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR);\r
5186 fp->flag &= (BYTE)~FA_DIRTY;\r
5187 }\r
5188#endif\r
5189 if (disk_read(fs->drv, fp->buf, sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR);\r
5190 }\r
5191 dbuf = fp->buf;\r
5192#endif\r
5193 fp->sect = sect;\r
5194 rcnt = SS(fs) - (UINT)fp->fptr % SS(fs); /* Number of bytes left in the sector */\r
5195 if (rcnt > btf) rcnt = btf; /* Clip it by btr if needed */\r
5196 rcnt = (*func)(dbuf + ((UINT)fp->fptr % SS(fs)), rcnt); /* Forward the file data */\r
5197 if (!rcnt) ABORT(fs, FR_INT_ERR);\r
53668523
L
5198 }\r
5199\r
70702af1 5200 LEAVE_FF(fs, FR_OK);\r
53668523
L
5201}\r
5202#endif /* _USE_FORWARD */\r
5203\r
5204\r
5205\r
5206#if _USE_MKFS && !_FS_READONLY\r
5207/*-----------------------------------------------------------------------*/\r
70702af1 5208/* Create FAT file system on the logical drive */\r
53668523 5209/*-----------------------------------------------------------------------*/\r
53668523
L
5210\r
5211FRESULT f_mkfs (\r
5212 const TCHAR* path, /* Logical drive number */\r
70702af1
L
5213 BYTE opt, /* Format option */\r
5214 DWORD au, /* Size of allocation unit [byte] */\r
5215 void* work, /* Pointer to working buffer */\r
5216 UINT len /* Size of working buffer */\r
53668523
L
5217)\r
5218{\r
70702af1
L
5219 const UINT n_fats = 1; /* Number of FATs for FAT12/16/32 volume (1 or 2) */\r
5220 const UINT n_rootdir = 512; /* Number of root directory entries for FAT12/16 volume */\r
5221 static const WORD cst[] = {1, 4, 16, 64, 256, 512, 0}; /* Cluster size boundary for FAT12/16 volume (4Ks unit) */\r
5222 static const WORD cst32[] = {1, 2, 4, 8, 16, 32, 0}; /* Cluster size boundary for FAT32 volume (128Ks unit) */\r
5223 BYTE fmt, sys, *buf, *pte, pdrv, part;\r
5224 WORD ss;\r
5225 DWORD szb_buf, sz_buf, sz_blk, n_clst, pau, sect, nsect, n;\r
5226 DWORD b_vol, b_fat, b_data; /* Base LBA for volume, fat, data */\r
5227 DWORD sz_vol, sz_rsv, sz_fat, sz_dir; /* Size for volume, fat, dir, data */\r
53668523 5228 UINT i;\r
70702af1 5229 int vol;\r
53668523 5230 DSTATUS stat;\r
70702af1
L
5231#if _USE_TRIM || _FS_EXFAT\r
5232 DWORD tbl[3];\r
5233#endif\r
53668523
L
5234\r
5235\r
5236 /* Check mounted drive and clear work area */\r
70702af1 5237 vol = get_ldnumber(&path); /* Get target logical drive */\r
53668523 5238 if (vol < 0) return FR_INVALID_DRIVE;\r
70702af1 5239 if (FatFs[vol]) FatFs[vol]->fs_type = 0; /* Clear mounted volume */\r
53668523 5240 pdrv = LD2PD(vol); /* Physical drive */\r
70702af1 5241 part = LD2PT(vol); /* Partition (0:create as new, 1-4:get from partition table) */\r
53668523 5242\r
70702af1 5243 /* Check physical drive status */\r
53668523
L
5244 stat = disk_initialize(pdrv);\r
5245 if (stat & STA_NOINIT) return FR_NOT_READY;\r
5246 if (stat & STA_PROTECT) return FR_WRITE_PROTECTED;\r
70702af1
L
5247 if (disk_ioctl(pdrv, GET_BLOCK_SIZE, &sz_blk) != RES_OK || !sz_blk || sz_blk > 32768 || (sz_blk & (sz_blk - 1))) sz_blk = 1; /* Erase block to align data area */\r
5248#if _MAX_SS != _MIN_SS /* Get sector size of the medium */\r
5249 if (disk_ioctl(pdrv, GET_SECTOR_SIZE, &ss) != RES_OK) return FR_DISK_ERR;\r
5250 if (ss > _MAX_SS || ss < _MIN_SS || (ss & (ss - 1))) return FR_DISK_ERR;\r
5251#else\r
5252 ss = _MAX_SS;\r
53668523 5253#endif\r
70702af1
L
5254 if ((au != 0 && au < ss) || au > 0x1000000 || (au & (au - 1))) return FR_INVALID_PARAMETER; /* Check if au is valid */\r
5255 au /= ss; /* Cluster size in unit of sector */\r
5256\r
5257 /* Get working buffer */\r
5258 buf = (BYTE*)work; /* Working buffer */\r
5259 sz_buf = len / ss; /* Size of working buffer (sector) */\r
5260 szb_buf = sz_buf * ss; /* Size of working buffer (byte) */\r
5261 if (!szb_buf) return FR_MKFS_ABORTED;\r
5262\r
5263 /* Determine where the volume to be located (b_vol, sz_vol) */\r
5264 if (_MULTI_PARTITION && part != 0) {\r
53668523 5265 /* Get partition information from partition table in the MBR */\r
70702af1
L
5266 if (disk_read(pdrv, buf, 0, 1) != RES_OK) return FR_DISK_ERR; /* Load MBR */\r
5267 if (ld_word(buf + BS_55AA) != 0xAA55) return FR_MKFS_ABORTED; /* Check if MBR is valid */\r
5268 pte = buf + (MBR_Table + (part - 1) * SZ_PTE);\r
5269 if (!pte[PTE_System]) return FR_MKFS_ABORTED; /* No partition? */\r
5270 b_vol = ld_dword(pte + PTE_StLba); /* Get volume start sector */\r
5271 sz_vol = ld_dword(pte + PTE_SizLba); /* Get volume size */\r
53668523 5272 } else {\r
70702af1
L
5273 /* Create a single-partition in this function */\r
5274 if (disk_ioctl(pdrv, GET_SECTOR_COUNT, &sz_vol) != RES_OK) return FR_DISK_ERR;\r
5275 b_vol = (opt & FM_SFD) ? 0 : 63; /* Volume start sector */\r
5276 if (sz_vol < b_vol) return FR_MKFS_ABORTED;\r
5277 sz_vol -= b_vol; /* Volume size */\r
53668523 5278 }\r
70702af1 5279 if (sz_vol < 128) return FR_MKFS_ABORTED; /* Check if volume size is >=128s */\r
53668523 5280\r
70702af1
L
5281 /* Pre-determine the FAT type */\r
5282 do {\r
5283 if (_FS_EXFAT && (opt & FM_EXFAT)) { /* exFAT possible? */\r
5284 if ((opt & FM_ANY) == FM_EXFAT || sz_vol >= 0x4000000 || au > 128) { /* exFAT only, vol >= 64Ms or au > 128s ? */\r
5285 fmt = FS_EXFAT; break;\r
5286 }\r
5287 }\r
5288 if (au > 128) return FR_INVALID_PARAMETER; /* Too large au for FAT/FAT32 */\r
5289 if (opt & FM_FAT32) { /* FAT32 possible? */\r
5290 if ((opt & FM_ANY) == FM_FAT32 || !(opt & FM_FAT)) { /* FAT32 only or no-FAT? */\r
5291 fmt = FS_FAT32; break;\r
5292 }\r
5293 }\r
5294 if (!(opt & FM_FAT)) return FR_INVALID_PARAMETER; /* no-FAT? */\r
5295 fmt = FS_FAT16;\r
5296 } while (0);\r
5297\r
5298#if _FS_EXFAT\r
5299 if (fmt == FS_EXFAT) { /* Create an exFAT volume */\r
5300 DWORD szb_bit, szb_case, sum, nb, cl;\r
5301 WCHAR ch, si;\r
5302 UINT j, st;\r
5303 BYTE b;\r
5304\r
5305 if (sz_vol < 0x1000) return FR_MKFS_ABORTED; /* Too small volume? */\r
5306#if _USE_TRIM\r
5307 tbl[0] = b_vol; tbl[1] = b_vol + sz_vol - 1; /* Inform the device the volume area can be erased */\r
5308 disk_ioctl(pdrv, CTRL_TRIM, tbl);\r
5309#endif\r
5310 /* Determine FAT location, data location and number of clusters */\r
5311 if (!au) { /* au auto-selection */\r
5312 au = 8;\r
5313 if (sz_vol >= 0x80000) au = 64; /* >= 512Ks */\r
5314 if (sz_vol >= 0x4000000) au = 256; /* >= 64Ms */\r
5315 }\r
5316 b_fat = b_vol + 32; /* FAT start at offset 32 */\r
5317 sz_fat = ((sz_vol / au + 2) * 4 + ss - 1) / ss; /* Number of FAT sectors */\r
5318 b_data = (b_fat + sz_fat + sz_blk - 1) & ~(sz_blk - 1); /* Align data area to the erase block boundary */\r
5319 if (b_data >= sz_vol / 2) return FR_MKFS_ABORTED; /* Too small volume? */\r
5320 n_clst = (sz_vol - (b_data - b_vol)) / au; /* Number of clusters */\r
5321 if (n_clst <16) return FR_MKFS_ABORTED; /* Too few clusters? */\r
5322 if (n_clst > MAX_EXFAT) return FR_MKFS_ABORTED; /* Too many clusters? */\r
5323\r
5324 szb_bit = (n_clst + 7) / 8; /* Size of allocation bitmap */\r
5325 tbl[0] = (szb_bit + au * ss - 1) / (au * ss); /* Number of allocation bitmap clusters */\r
5326\r
5327 /* Create a compressed up-case table */\r
5328 sect = b_data + au * tbl[0]; /* Table start sector */\r
5329 sum = 0; /* Table checksum to be stored in the 82 entry */\r
5330 st = si = i = j = szb_case = 0;\r
5331 do {\r
5332 switch (st) {\r
5333 case 0:\r
5334 ch = ff_wtoupper(si); /* Get an up-case char */\r
5335 if (ch != si) {\r
5336 si++; break; /* Store the up-case char if exist */\r
5337 }\r
5338 for (j = 1; (WCHAR)(si + j) && (WCHAR)(si + j) == ff_wtoupper((WCHAR)(si + j)); j++) ; /* Get run length of no-case block */\r
5339 if (j >= 128) {\r
5340 ch = 0xFFFF; st = 2; break; /* Compress the no-case block if run is >= 128 */\r
5341 }\r
5342 st = 1; /* Do not compress short run */\r
5343 /* continue */\r
5344 case 1:\r
5345 ch = si++; /* Fill the short run */\r
5346 if (--j == 0) st = 0;\r
5347 break;\r
5348 default:\r
5349 ch = (WCHAR)j; si += j; /* Number of chars to skip */\r
5350 st = 0;\r
5351 }\r
5352 sum = xsum32(buf[i + 0] = (BYTE)ch, sum); /* Put it into the write buffer */\r
5353 sum = xsum32(buf[i + 1] = (BYTE)(ch >> 8), sum);\r
5354 i += 2; szb_case += 2;\r
5355 if (!si || i == szb_buf) { /* Write buffered data when buffer full or end of process */\r
5356 n = (i + ss - 1) / ss;\r
5357 if (disk_write(pdrv, buf, sect, n) != RES_OK) return FR_DISK_ERR;\r
5358 sect += n; i = 0;\r
5359 }\r
5360 } while (si);\r
5361 tbl[1] = (szb_case + au * ss - 1) / (au * ss); /* Number of up-case table clusters */\r
5362 tbl[2] = 1; /* Number of root dir clusters */\r
53668523 5363\r
70702af1
L
5364 /* Initialize the allocation bitmap */\r
5365 sect = b_data; nsect = (szb_bit + ss - 1) / ss; /* Start of bitmap and number of sectors */\r
5366 nb = tbl[0] + tbl[1] + tbl[2]; /* Number of clusters in-use by system */\r
5367 do {\r
5368 mem_set(buf, 0, szb_buf);\r
5369 for (i = 0; nb >= 8 && i < szb_buf; buf[i++] = 0xFF, nb -= 8) ;\r
5370 for (b = 1; nb && i < szb_buf; buf[i] |= b, b <<= 1, nb--) ;\r
5371 n = (nsect > sz_buf) ? sz_buf : nsect; /* Write the buffered data */\r
5372 if (disk_write(pdrv, buf, sect, n) != RES_OK) return FR_DISK_ERR;\r
5373 sect += n; nsect -= n;\r
5374 } while (nsect);\r
5375\r
5376 /* Initialize the FAT */\r
5377 sect = b_fat; nsect = sz_fat; /* Start of FAT and number of FAT sectors */\r
5378 j = nb = cl = 0;\r
5379 do {\r
5380 mem_set(buf, 0, szb_buf); i = 0; /* Clear work area and reset write index */\r
5381 if (cl == 0) { /* Set entry 0 and 1 */\r
5382 st_dword(buf + i, 0xFFFFFFF8); i += 4; cl++;\r
5383 st_dword(buf + i, 0xFFFFFFFF); i += 4; cl++;\r
5384 }\r
5385 do { /* Create chains of bitmap, up-case and root dir */\r
5386 while (nb && i < szb_buf) { /* Create a chain */\r
5387 st_dword(buf + i, (nb > 1) ? cl + 1 : 0xFFFFFFFF);\r
5388 i += 4; cl++; nb--;\r
5389 }\r
5390 if (!nb && j < 3) nb = tbl[j++]; /* Next chain */\r
5391 } while (nb && i < szb_buf);\r
5392 n = (nsect > sz_buf) ? sz_buf : nsect; /* Write the buffered data */\r
5393 if (disk_write(pdrv, buf, sect, n) != RES_OK) return FR_DISK_ERR;\r
5394 sect += n; nsect -= n;\r
5395 } while (nsect);\r
5396\r
5397 /* Initialize the root directory */\r
5398 mem_set(buf, 0, szb_buf);\r
5399 buf[SZDIRE * 0 + 0] = 0x83; /* 83 entry (volume label) */\r
5400 buf[SZDIRE * 1 + 0] = 0x81; /* 81 entry (allocation bitmap) */\r
5401 st_dword(buf + SZDIRE * 1 + 20, 2);\r
5402 st_dword(buf + SZDIRE * 1 + 24, szb_bit);\r
5403 buf[SZDIRE * 2 + 0] = 0x82; /* 82 entry (up-case table) */\r
5404 st_dword(buf + SZDIRE * 2 + 4, sum);\r
5405 st_dword(buf + SZDIRE * 2 + 20, 2 + tbl[0]);\r
5406 st_dword(buf + SZDIRE * 2 + 24, szb_case);\r
5407 sect = b_data + au * (tbl[0] + tbl[1]); nsect = au; /* Start of the root directory and number of sectors */\r
5408 do { /* Fill root directory sectors */\r
5409 n = (nsect > sz_buf) ? sz_buf : nsect;\r
5410 if (disk_write(pdrv, buf, sect, n) != RES_OK) return FR_DISK_ERR;\r
5411 mem_set(buf, 0, ss);\r
5412 sect += n; nsect -= n;\r
5413 } while (nsect);\r
5414\r
5415 /* Create two set of the exFAT VBR blocks */\r
5416 sect = b_vol;\r
5417 for (n = 0; n < 2; n++) {\r
5418 /* Main record (+0) */\r
5419 mem_set(buf, 0, ss);\r
5420 mem_cpy(buf + BS_JmpBoot, "\xEB\x76\x90" "EXFAT ", 11); /* Boot jump code (x86), OEM name */\r
5421 st_dword(buf + BPB_VolOfsEx, b_vol); /* Volume offset in the physical drive [sector] */\r
5422 st_dword(buf + BPB_TotSecEx, sz_vol); /* Volume size [sector] */\r
5423 st_dword(buf + BPB_FatOfsEx, b_fat - b_vol); /* FAT offset [sector] */\r
5424 st_dword(buf + BPB_FatSzEx, sz_fat); /* FAT size [sector] */\r
5425 st_dword(buf + BPB_DataOfsEx, b_data - b_vol); /* Data offset [sector] */\r
5426 st_dword(buf + BPB_NumClusEx, n_clst); /* Number of clusters */\r
5427 st_dword(buf + BPB_RootClusEx, 2 + tbl[0] + tbl[1]); /* Root dir cluster # */\r
5428 st_dword(buf + BPB_VolIDEx, GET_FATTIME()); /* VSN */\r
5429 st_word(buf + BPB_FSVerEx, 0x100); /* File system version (1.00) */\r
5430 for (buf[BPB_BytsPerSecEx] = 0, i = ss; i >>= 1; buf[BPB_BytsPerSecEx]++) ; /* Log2 of sector size [byte] */\r
5431 for (buf[BPB_SecPerClusEx] = 0, i = au; i >>= 1; buf[BPB_SecPerClusEx]++) ; /* Log2 of cluster size [sector] */\r
5432 buf[BPB_NumFATsEx] = 1; /* Number of FATs */\r
5433 buf[BPB_DrvNumEx] = 0x80; /* Drive number (for int13) */\r
5434 st_word(buf + BS_BootCodeEx, 0xFEEB); /* Boot code (x86) */\r
5435 st_word(buf + BS_55AA, 0xAA55); /* Signature (placed here regardless of sector size) */\r
5436 for (i = sum = 0; i < ss; i++) { /* VBR checksum */\r
5437 if (i != BPB_VolFlagEx && i != BPB_VolFlagEx + 1 && i != BPB_PercInUseEx) sum = xsum32(buf[i], sum);\r
5438 }\r
5439 if (disk_write(pdrv, buf, sect++, 1) != RES_OK) return FR_DISK_ERR;\r
5440 /* Extended bootstrap record (+1..+8) */\r
5441 mem_set(buf, 0, ss);\r
5442 st_word(buf + ss - 2, 0xAA55); /* Signature (placed at end of sector) */\r
5443 for (j = 1; j < 9; j++) {\r
5444 for (i = 0; i < ss; sum = xsum32(buf[i++], sum)) ; /* VBR checksum */\r
5445 if (disk_write(pdrv, buf, sect++, 1) != RES_OK) return FR_DISK_ERR;\r
5446 }\r
5447 /* OEM/Reserved record (+9..+10) */\r
5448 mem_set(buf, 0, ss);\r
5449 for ( ; j < 11; j++) {\r
5450 for (i = 0; i < ss; sum = xsum32(buf[i++], sum)) ; /* VBR checksum */\r
5451 if (disk_write(pdrv, buf, sect++, 1) != RES_OK) return FR_DISK_ERR;\r
5452 }\r
5453 /* Sum record (+11) */\r
5454 for (i = 0; i < ss; i += 4) st_dword(buf + i, sum); /* Fill with checksum value */\r
5455 if (disk_write(pdrv, buf, sect++, 1) != RES_OK) return FR_DISK_ERR;\r
5456 }\r
53668523 5457\r
70702af1
L
5458 } else\r
5459#endif /* _FS_EXFAT */\r
5460 { /* Create an FAT12/16/32 volume */\r
5461 do {\r
5462 pau = au;\r
5463 /* Pre-determine number of clusters and FAT sub-type */\r
5464 if (fmt == FS_FAT32) { /* FAT32 volume */\r
5465 if (!pau) { /* au auto-selection */\r
5466 n = sz_vol / 0x20000; /* Volume size in unit of 128KS */\r
5467 for (i = 0, pau = 1; cst32[i] && cst32[i] <= n; i++, pau <<= 1) ; /* Get from table */\r
5468 }\r
5469 n_clst = sz_vol / pau; /* Number of clusters */\r
5470 sz_fat = (n_clst * 4 + 8 + ss - 1) / ss; /* FAT size [sector] */\r
5471 sz_rsv = 32; /* Number of reserved sectors */\r
5472 sz_dir = 0; /* No static directory */\r
5473 if (n_clst <= MAX_FAT16 || n_clst > MAX_FAT32) return FR_MKFS_ABORTED;\r
5474 } else { /* FAT12/16 volume */\r
5475 if (!pau) { /* au auto-selection */\r
5476 n = sz_vol / 0x1000; /* Volume size in unit of 4KS */\r
5477 for (i = 0, pau = 1; cst[i] && cst[i] <= n; i++, pau <<= 1) ; /* Get from table */\r
5478 }\r
5479 n_clst = sz_vol / pau;\r
5480 if (n_clst > MAX_FAT12) {\r
5481 n = n_clst * 2 + 4; /* FAT size [byte] */\r
5482 } else {\r
5483 fmt = FS_FAT12;\r
5484 n = (n_clst * 3 + 1) / 2 + 3; /* FAT size [byte] */\r
5485 }\r
5486 sz_fat = (n + ss - 1) / ss; /* FAT size [sector] */\r
5487 sz_rsv = 1; /* Number of reserved sectors */\r
5488 sz_dir = (DWORD)n_rootdir * SZDIRE / ss; /* Rootdir size [sector] */\r
5489 }\r
5490 b_fat = b_vol + sz_rsv; /* FAT base */\r
5491 b_data = b_fat + sz_fat * n_fats + sz_dir; /* Data base */\r
5492\r
5493 /* Align data base to erase block boundary (for flash memory media) */\r
5494 n = ((b_data + sz_blk - 1) & ~(sz_blk - 1)) - b_data; /* Next nearest erase block from current data base */\r
5495 if (fmt == FS_FAT32) { /* FAT32: Move FAT base */\r
5496 sz_rsv += n; b_fat += n;\r
5497 } else { /* FAT12/16: Expand FAT size */\r
5498 sz_fat += n / n_fats;\r
5499 }\r
5500\r
5501 /* Determine number of clusters and final check of validity of the FAT sub-type */\r
5502 if (sz_vol < b_data + pau * 16 - b_vol) return FR_MKFS_ABORTED; /* Too small volume */\r
5503 n_clst = (sz_vol - sz_rsv - sz_fat * n_fats - sz_dir) / pau;\r
5504 if (fmt == FS_FAT32) {\r
5505 if (n_clst <= MAX_FAT16) { /* Too few clusters for FAT32 */\r
5506 if (!au && (au = pau / 2) != 0) continue; /* Adjust cluster size and retry */\r
5507 return FR_MKFS_ABORTED;\r
5508 }\r
5509 }\r
5510 if (fmt == FS_FAT16) {\r
5511 if (n_clst > MAX_FAT16) { /* Too many clusters for FAT16 */\r
5512 if (!au && (pau * 2) <= 64) {\r
5513 au = pau * 2; continue; /* Adjust cluster size and retry */\r
5514 }\r
5515 if ((opt & FM_FAT32)) {\r
5516 fmt = FS_FAT32; continue; /* Switch type to FAT32 and retry */\r
5517 }\r
5518 if (!au && (au = pau * 2) <= 128) continue; /* Adjust cluster size and retry */\r
5519 return FR_MKFS_ABORTED;\r
5520 }\r
5521 if (n_clst <= MAX_FAT12) { /* Too few clusters for FAT16 */\r
5522 if (!au && (au = pau * 2) <= 128) continue; /* Adjust cluster size and retry */\r
5523 return FR_MKFS_ABORTED;\r
5524 }\r
5525 }\r
5526 if (fmt == FS_FAT12 && n_clst > MAX_FAT12) return FR_MKFS_ABORTED; /* Too many clusters for FAT12 */\r
5527\r
5528 /* Ok, it is the valid cluster configuration */\r
5529 break;\r
5530 } while (1);\r
5531\r
5532#if _USE_TRIM\r
5533 tbl[0] = b_vol; tbl[1] = b_vol + sz_vol - 1; /* Inform the device the volume area can be erased */\r
5534 disk_ioctl(pdrv, CTRL_TRIM, tbl);\r
5535#endif\r
5536 /* Create FAT VBR */\r
5537 mem_set(buf, 0, ss);\r
5538 mem_cpy(buf + BS_JmpBoot, "\xEB\xFE\x90" "MSDOS5.0", 11);/* Boot jump code (x86), OEM name */\r
5539 st_word(buf + BPB_BytsPerSec, ss); /* Sector size [byte] */\r
5540 buf[BPB_SecPerClus] = (BYTE)pau; /* Cluster size [sector] */\r
5541 st_word(buf + BPB_RsvdSecCnt, (WORD)sz_rsv); /* Size of reserved area */\r
5542 buf[BPB_NumFATs] = (BYTE)n_fats; /* Number of FATs */\r
5543 st_word(buf + BPB_RootEntCnt, (WORD)((fmt == FS_FAT32) ? 0 : n_rootdir)); /* Number of root directory entries */\r
5544 if (sz_vol < 0x10000) {\r
5545 st_word(buf + BPB_TotSec16, (WORD)sz_vol); /* Volume size in 16-bit LBA */\r
53668523 5546 } else {\r
70702af1
L
5547 st_dword(buf + BPB_TotSec32, sz_vol); /* Volume size in 32-bit LBA */\r
5548 }\r
5549 buf[BPB_Media] = 0xF8; /* Media descriptor byte */\r
5550 st_word(buf + BPB_SecPerTrk, 63); /* Number of sectors per track (for int13) */\r
5551 st_word(buf + BPB_NumHeads, 255); /* Number of heads (for int13) */\r
5552 st_dword(buf + BPB_HiddSec, b_vol); /* Volume offset in the physical drive [sector] */\r
5553 if (fmt == FS_FAT32) {\r
5554 st_dword(buf + BS_VolID32, GET_FATTIME()); /* VSN */\r
5555 st_dword(buf + BPB_FATSz32, sz_fat); /* FAT size [sector] */\r
5556 st_dword(buf + BPB_RootClus32, 2); /* Root directory cluster # (2) */\r
5557 st_word(buf + BPB_FSInfo32, 1); /* Offset of FSINFO sector (VBR + 1) */\r
5558 st_word(buf + BPB_BkBootSec32, 6); /* Offset of backup VBR (VBR + 6) */\r
5559 buf[BS_DrvNum32] = 0x80; /* Drive number (for int13) */\r
5560 buf[BS_BootSig32] = 0x29; /* Extended boot signature */\r
5561 mem_cpy(buf + BS_VolLab32, "NO NAME " "FAT32 ", 19); /* Volume label, FAT signature */\r
5562 } else {\r
5563 st_dword(buf + BS_VolID, GET_FATTIME()); /* VSN */\r
5564 st_word(buf + BPB_FATSz16, (WORD)sz_fat); /* FAT size [sector] */\r
5565 buf[BS_DrvNum] = 0x80; /* Drive number (for int13) */\r
5566 buf[BS_BootSig] = 0x29; /* Extended boot signature */\r
5567 mem_cpy(buf + BS_VolLab, "NO NAME " "FAT ", 19); /* Volume label, FAT signature */\r
5568 }\r
5569 st_word(buf + BS_55AA, 0xAA55); /* Signature (offset is fixed here regardless of sector size) */\r
5570 if (disk_write(pdrv, buf, b_vol, 1) != RES_OK) return FR_DISK_ERR; /* Write it to the VBR sector */\r
5571\r
5572 /* Create FSINFO record if needed */\r
5573 if (fmt == FS_FAT32) {\r
5574 disk_write(pdrv, buf, b_vol + 6, 1); /* Write backup VBR (VBR + 6) */\r
5575 mem_set(buf, 0, ss);\r
5576 st_dword(buf + FSI_LeadSig, 0x41615252);\r
5577 st_dword(buf + FSI_StrucSig, 0x61417272);\r
5578 st_dword(buf + FSI_Free_Count, n_clst - 1); /* Number of free clusters */\r
5579 st_dword(buf + FSI_Nxt_Free, 2); /* Last allocated cluster# */\r
5580 st_word(buf + BS_55AA, 0xAA55);\r
5581 disk_write(pdrv, buf, b_vol + 7, 1); /* Write backup FSINFO (VBR + 7) */\r
5582 disk_write(pdrv, buf, b_vol + 1, 1); /* Write original FSINFO (VBR + 1) */\r
53668523 5583 }\r
53668523 5584\r
70702af1
L
5585 /* Initialize FAT area */\r
5586 mem_set(buf, 0, (UINT)szb_buf);\r
5587 sect = b_fat; /* FAT start sector */\r
5588 for (i = 0; i < n_fats; i++) { /* Initialize FATs each */\r
5589 if (fmt == FS_FAT32) {\r
5590 st_dword(buf + 0, 0xFFFFFFF8); /* Entry 0 */\r
5591 st_dword(buf + 4, 0xFFFFFFFF); /* Entry 1 */\r
5592 st_dword(buf + 8, 0x0FFFFFFF); /* Entry 2 (root directory) */\r
5593 } else {\r
5594 st_dword(buf + 0, (fmt == FS_FAT12) ? 0xFFFFF8 : 0xFFFFFFF8); /* Entry 0 and 1 */\r
5595 }\r
5596 nsect = sz_fat; /* Number of FAT sectors */\r
5597 do { /* Fill FAT sectors */\r
5598 n = (nsect > sz_buf) ? sz_buf : nsect;\r
5599 if (disk_write(pdrv, buf, sect, (UINT)n) != RES_OK) return FR_DISK_ERR;\r
5600 mem_set(buf, 0, ss);\r
5601 sect += n; nsect -= n;\r
5602 } while (nsect);\r
53668523 5603 }\r
53668523 5604\r
70702af1
L
5605 /* Initialize root directory (fill with zero) */\r
5606 nsect = (fmt == FS_FAT32) ? pau : sz_dir; /* Number of root directory sectors */\r
5607 do {\r
5608 n = (nsect > sz_buf) ? sz_buf : nsect;\r
5609 if (disk_write(pdrv, buf, sect, (UINT)n) != RES_OK) return FR_DISK_ERR;\r
5610 sect += n; nsect -= n;\r
5611 } while (nsect);\r
53668523 5612 }\r
70702af1
L
5613\r
5614 /* Determine system ID in the partition table */\r
5615 if (_FS_EXFAT && fmt == FS_EXFAT) {\r
5616 sys = 0x07; /* HPFS/NTFS/exFAT */\r
53668523 5617 } else {\r
70702af1
L
5618 if (fmt == FS_FAT32) {\r
5619 sys = 0x0C; /* FAT32X */\r
53668523 5620 } else {\r
70702af1
L
5621 if (sz_vol >= 0x10000) {\r
5622 sys = 0x06; /* FAT12/16 (>=64KS) */\r
5623 } else {\r
5624 sys = (fmt == FS_FAT16) ? 0x04 : 0x01; /* FAT16 (<64KS) : FAT12 (<64KS) */\r
5625 }\r
53668523
L
5626 }\r
5627 }\r
5628\r
70702af1
L
5629 if (_MULTI_PARTITION && part != 0) {\r
5630 /* Update system ID in the partition table */\r
5631 if (disk_read(pdrv, buf, 0, 1) != RES_OK) return FR_DISK_ERR; /* Read the MBR */\r
5632 buf[MBR_Table + (part - 1) * SZ_PTE + PTE_System] = sys; /* Set system type */\r
5633 if (disk_write(pdrv, buf, 0, 1) != RES_OK) return FR_DISK_ERR; /* Write it back to the MBR */\r
5634 } else {\r
5635 if (!(opt & FM_SFD)) {\r
5636 /* Create partition table in FDISK format */\r
5637 mem_set(buf, 0, ss);\r
5638 st_word(buf + BS_55AA, 0xAA55); /* MBR signature */\r
5639 pte = buf + MBR_Table; /* Create partition table for single partition in the drive */\r
5640 pte[PTE_Boot] = 0; /* Boot indicator */\r
5641 pte[PTE_StHead] = 1; /* Start head */\r
5642 pte[PTE_StSec] = 1; /* Start sector */\r
5643 pte[PTE_StCyl] = 0; /* Start cylinder */\r
5644 pte[PTE_System] = sys; /* System type */\r
5645 n = (b_vol + sz_vol) / (63 * 255); /* (End CHS is incorrect) */\r
5646 pte[PTE_EdHead] = 254; /* End head */\r
5647 pte[PTE_EdSec] = (BYTE)(n >> 2 | 63); /* End sector */\r
5648 pte[PTE_EdCyl] = (BYTE)n; /* End cylinder */\r
5649 st_dword(pte + PTE_StLba, b_vol); /* Start offset in LBA */\r
5650 st_dword(pte + PTE_SizLba, sz_vol); /* Size in sectors */\r
5651 if (disk_write(pdrv, buf, 0, 1) != RES_OK) return FR_DISK_ERR; /* Write it to the MBR */\r
5652 }\r
53668523 5653 }\r
53668523 5654\r
70702af1 5655 if (disk_ioctl(pdrv, CTRL_SYNC, 0) != RES_OK) return FR_DISK_ERR;\r
53668523 5656\r
70702af1 5657 return FR_OK;\r
53668523
L
5658}\r
5659\r
5660\r
5661\r
5662#if _MULTI_PARTITION\r
5663/*-----------------------------------------------------------------------*/\r
70702af1 5664/* Create partition table on the physical drive */\r
53668523
L
5665/*-----------------------------------------------------------------------*/\r
5666\r
5667FRESULT f_fdisk (\r
5668 BYTE pdrv, /* Physical drive number */\r
70702af1 5669 const DWORD* szt, /* Pointer to the size table for each partitions */\r
53668523
L
5670 void* work /* Pointer to the working buffer */\r
5671)\r
5672{\r
5673 UINT i, n, sz_cyl, tot_cyl, b_cyl, e_cyl, p_cyl;\r
5674 BYTE s_hd, e_hd, *p, *buf = (BYTE*)work;\r
5675 DSTATUS stat;\r
5676 DWORD sz_disk, sz_part, s_part;\r
5677\r
5678\r
5679 stat = disk_initialize(pdrv);\r
5680 if (stat & STA_NOINIT) return FR_NOT_READY;\r
5681 if (stat & STA_PROTECT) return FR_WRITE_PROTECTED;\r
5682 if (disk_ioctl(pdrv, GET_SECTOR_COUNT, &sz_disk)) return FR_DISK_ERR;\r
5683\r
70702af1 5684 /* Determine the CHS without any care of the drive geometry */\r
53668523
L
5685 for (n = 16; n < 256 && sz_disk / n / 63 > 1024; n *= 2) ;\r
5686 if (n == 256) n--;\r
5687 e_hd = n - 1;\r
5688 sz_cyl = 63 * n;\r
5689 tot_cyl = sz_disk / sz_cyl;\r
5690\r
5691 /* Create partition table */\r
5692 mem_set(buf, 0, _MAX_SS);\r
5693 p = buf + MBR_Table; b_cyl = 0;\r
5694 for (i = 0; i < 4; i++, p += SZ_PTE) {\r
5695 p_cyl = (szt[i] <= 100U) ? (DWORD)tot_cyl * szt[i] / 100 : szt[i] / sz_cyl;\r
5696 if (!p_cyl) continue;\r
5697 s_part = (DWORD)sz_cyl * b_cyl;\r
5698 sz_part = (DWORD)sz_cyl * p_cyl;\r
5699 if (i == 0) { /* Exclude first track of cylinder 0 */\r
5700 s_hd = 1;\r
5701 s_part += 63; sz_part -= 63;\r
5702 } else {\r
5703 s_hd = 0;\r
5704 }\r
5705 e_cyl = b_cyl + p_cyl - 1;\r
5706 if (e_cyl >= tot_cyl) return FR_INVALID_PARAMETER;\r
5707\r
5708 /* Set partition table */\r
5709 p[1] = s_hd; /* Start head */\r
5710 p[2] = (BYTE)((b_cyl >> 2) + 1); /* Start sector */\r
5711 p[3] = (BYTE)b_cyl; /* Start cylinder */\r
5712 p[4] = 0x06; /* System type (temporary setting) */\r
5713 p[5] = e_hd; /* End head */\r
5714 p[6] = (BYTE)((e_cyl >> 2) + 63); /* End sector */\r
5715 p[7] = (BYTE)e_cyl; /* End cylinder */\r
70702af1
L
5716 st_dword(p + 8, s_part); /* Start sector in LBA */\r
5717 st_dword(p + 12, sz_part); /* Partition size */\r
53668523
L
5718\r
5719 /* Next partition */\r
5720 b_cyl += p_cyl;\r
5721 }\r
70702af1 5722 st_word(p, 0xAA55);\r
53668523
L
5723\r
5724 /* Write it to the MBR */\r
7b78a5a2 5725 return (disk_write(pdrv, buf, 0, 1) != RES_OK || disk_ioctl(pdrv, CTRL_SYNC, 0) != RES_OK) ? FR_DISK_ERR : FR_OK;\r
53668523
L
5726}\r
5727\r
53668523
L
5728#endif /* _MULTI_PARTITION */\r
5729#endif /* _USE_MKFS && !_FS_READONLY */\r
5730\r
5731\r
5732\r
5733\r
5734#if _USE_STRFUNC\r
5735/*-----------------------------------------------------------------------*/\r
5736/* Get a string from the file */\r
5737/*-----------------------------------------------------------------------*/\r
5738\r
5739TCHAR* f_gets (\r
5740 TCHAR* buff, /* Pointer to the string buffer to read */\r
5741 int len, /* Size of string buffer (characters) */\r
5742 FIL* fp /* Pointer to the file object */\r
5743)\r
5744{\r
5745 int n = 0;\r
5746 TCHAR c, *p = buff;\r
5747 BYTE s[2];\r
5748 UINT rc;\r
5749\r
5750\r
5751 while (n < len - 1) { /* Read characters until buffer gets filled */\r
70702af1 5752#if _LFN_UNICODE\r
53668523
L
5753#if _STRF_ENCODE == 3 /* Read a character in UTF-8 */\r
5754 f_read(fp, s, 1, &rc);\r
5755 if (rc != 1) break;\r
5756 c = s[0];\r
5757 if (c >= 0x80) {\r
5758 if (c < 0xC0) continue; /* Skip stray trailer */\r
5759 if (c < 0xE0) { /* Two-byte sequence */\r
5760 f_read(fp, s, 1, &rc);\r
5761 if (rc != 1) break;\r
5762 c = (c & 0x1F) << 6 | (s[0] & 0x3F);\r
5763 if (c < 0x80) c = '?';\r
5764 } else {\r
5765 if (c < 0xF0) { /* Three-byte sequence */\r
5766 f_read(fp, s, 2, &rc);\r
5767 if (rc != 2) break;\r
5768 c = c << 12 | (s[0] & 0x3F) << 6 | (s[1] & 0x3F);\r
5769 if (c < 0x800) c = '?';\r
5770 } else { /* Reject four-byte sequence */\r
5771 c = '?';\r
5772 }\r
5773 }\r
5774 }\r
5775#elif _STRF_ENCODE == 2 /* Read a character in UTF-16BE */\r
5776 f_read(fp, s, 2, &rc);\r
5777 if (rc != 2) break;\r
5778 c = s[1] + (s[0] << 8);\r
5779#elif _STRF_ENCODE == 1 /* Read a character in UTF-16LE */\r
5780 f_read(fp, s, 2, &rc);\r
5781 if (rc != 2) break;\r
5782 c = s[0] + (s[1] << 8);\r
5783#else /* Read a character in ANSI/OEM */\r
5784 f_read(fp, s, 1, &rc);\r
5785 if (rc != 1) break;\r
5786 c = s[0];\r
5787 if (IsDBCS1(c)) {\r
5788 f_read(fp, s, 1, &rc);\r
5789 if (rc != 1) break;\r
5790 c = (c << 8) + s[0];\r
5791 }\r
5792 c = ff_convert(c, 1); /* OEM -> Unicode */\r
5793 if (!c) c = '?';\r
5794#endif\r
5795#else /* Read a character without conversion */\r
5796 f_read(fp, s, 1, &rc);\r
5797 if (rc != 1) break;\r
5798 c = s[0];\r
5799#endif\r
5800 if (_USE_STRFUNC == 2 && c == '\r') continue; /* Strip '\r' */\r
5801 *p++ = c;\r
5802 n++;\r
5803 if (c == '\n') break; /* Break on EOL */\r
5804 }\r
5805 *p = 0;\r
5806 return n ? buff : 0; /* When no data read (eof or error), return with error. */\r
5807}\r
5808\r
5809\r
5810\r
70702af1 5811\r
53668523
L
5812#if !_FS_READONLY\r
5813#include <stdarg.h>\r
5814/*-----------------------------------------------------------------------*/\r
5815/* Put a character to the file */\r
5816/*-----------------------------------------------------------------------*/\r
5817\r
5818typedef struct {\r
70702af1
L
5819 FIL *fp; /* Ptr to the writing file */\r
5820 int idx, nchr; /* Write index of buf[] (-1:error), number of chars written */\r
5821 BYTE buf[64]; /* Write buffer */\r
53668523
L
5822} putbuff;\r
5823\r
5824\r
5825static\r
70702af1 5826void putc_bfd ( /* Buffered write with code conversion */\r
53668523
L
5827 putbuff* pb,\r
5828 TCHAR c\r
5829)\r
5830{\r
5831 UINT bw;\r
5832 int i;\r
5833\r
5834\r
70702af1 5835 if (_USE_STRFUNC == 2 && c == '\n') { /* LF -> CRLF conversion */\r
53668523 5836 putc_bfd(pb, '\r');\r
70702af1 5837 }\r
53668523 5838\r
70702af1 5839 i = pb->idx; /* Write index of pb->buf[] */\r
53668523
L
5840 if (i < 0) return;\r
5841\r
70702af1 5842#if _LFN_UNICODE\r
53668523
L
5843#if _STRF_ENCODE == 3 /* Write a character in UTF-8 */\r
5844 if (c < 0x80) { /* 7-bit */\r
5845 pb->buf[i++] = (BYTE)c;\r
5846 } else {\r
5847 if (c < 0x800) { /* 11-bit */\r
5848 pb->buf[i++] = (BYTE)(0xC0 | c >> 6);\r
5849 } else { /* 16-bit */\r
5850 pb->buf[i++] = (BYTE)(0xE0 | c >> 12);\r
5851 pb->buf[i++] = (BYTE)(0x80 | (c >> 6 & 0x3F));\r
5852 }\r
5853 pb->buf[i++] = (BYTE)(0x80 | (c & 0x3F));\r
5854 }\r
5855#elif _STRF_ENCODE == 2 /* Write a character in UTF-16BE */\r
5856 pb->buf[i++] = (BYTE)(c >> 8);\r
5857 pb->buf[i++] = (BYTE)c;\r
5858#elif _STRF_ENCODE == 1 /* Write a character in UTF-16LE */\r
5859 pb->buf[i++] = (BYTE)c;\r
5860 pb->buf[i++] = (BYTE)(c >> 8);\r
5861#else /* Write a character in ANSI/OEM */\r
5862 c = ff_convert(c, 0); /* Unicode -> OEM */\r
5863 if (!c) c = '?';\r
5864 if (c >= 0x100)\r
5865 pb->buf[i++] = (BYTE)(c >> 8);\r
5866 pb->buf[i++] = (BYTE)c;\r
5867#endif\r
5868#else /* Write a character without conversion */\r
5869 pb->buf[i++] = (BYTE)c;\r
5870#endif\r
5871\r
5872 if (i >= (int)(sizeof pb->buf) - 3) { /* Write buffered characters to the file */\r
5873 f_write(pb->fp, pb->buf, (UINT)i, &bw);\r
5874 i = (bw == (UINT)i) ? 0 : -1;\r
5875 }\r
5876 pb->idx = i;\r
5877 pb->nchr++;\r
5878}\r
5879\r
5880\r
70702af1
L
5881static\r
5882int putc_flush ( /* Flush left characters in the buffer */\r
5883 putbuff* pb\r
5884)\r
5885{\r
5886 UINT nw;\r
5887\r
5888 if ( pb->idx >= 0 /* Flush buffered characters to the file */\r
5889 && f_write(pb->fp, pb->buf, (UINT)pb->idx, &nw) == FR_OK\r
5890 && (UINT)pb->idx == nw) return pb->nchr;\r
5891 return EOF;\r
5892}\r
5893\r
5894\r
5895static\r
5896void putc_init ( /* Initialize write buffer */\r
5897 putbuff* pb,\r
5898 FIL* fp\r
5899)\r
5900{\r
5901 pb->fp = fp;\r
5902 pb->nchr = pb->idx = 0;\r
5903}\r
5904\r
5905\r
53668523
L
5906\r
5907int f_putc (\r
5908 TCHAR c, /* A character to be output */\r
5909 FIL* fp /* Pointer to the file object */\r
5910)\r
5911{\r
5912 putbuff pb;\r
53668523 5913\r
53668523 5914\r
70702af1
L
5915 putc_init(&pb, fp);\r
5916 putc_bfd(&pb, c); /* Put the character */\r
5917 return putc_flush(&pb);\r
53668523
L
5918}\r
5919\r
5920\r
5921\r
5922\r
5923/*-----------------------------------------------------------------------*/\r
5924/* Put a string to the file */\r
5925/*-----------------------------------------------------------------------*/\r
5926\r
5927int f_puts (\r
5928 const TCHAR* str, /* Pointer to the string to be output */\r
5929 FIL* fp /* Pointer to the file object */\r
5930)\r
5931{\r
5932 putbuff pb;\r
53668523 5933\r
53668523 5934\r
70702af1
L
5935 putc_init(&pb, fp);\r
5936 while (*str) putc_bfd(&pb, *str++); /* Put the string */\r
5937 return putc_flush(&pb);\r
53668523
L
5938}\r
5939\r
5940\r
5941\r
5942\r
5943/*-----------------------------------------------------------------------*/\r
5944/* Put a formatted string to the file */\r
5945/*-----------------------------------------------------------------------*/\r
5946\r
5947int f_printf (\r
5948 FIL* fp, /* Pointer to the file object */\r
5949 const TCHAR* fmt, /* Pointer to the format string */\r
5950 ... /* Optional arguments... */\r
5951)\r
5952{\r
5953 va_list arp;\r
70702af1 5954 putbuff pb;\r
53668523 5955 BYTE f, r;\r
70702af1 5956 UINT i, j, w;\r
53668523 5957 DWORD v;\r
70702af1 5958 TCHAR c, d, str[32], *p;\r
53668523
L
5959\r
5960\r
70702af1 5961 putc_init(&pb, fp);\r
53668523
L
5962\r
5963 va_start(arp, fmt);\r
5964\r
5965 for (;;) {\r
5966 c = *fmt++;\r
5967 if (c == 0) break; /* End of string */\r
5968 if (c != '%') { /* Non escape character */\r
5969 putc_bfd(&pb, c);\r
5970 continue;\r
5971 }\r
5972 w = f = 0;\r
5973 c = *fmt++;\r
5974 if (c == '0') { /* Flag: '0' padding */\r
5975 f = 1; c = *fmt++;\r
5976 } else {\r
5977 if (c == '-') { /* Flag: left justified */\r
5978 f = 2; c = *fmt++;\r
5979 }\r
5980 }\r
5981 while (IsDigit(c)) { /* Precision */\r
5982 w = w * 10 + c - '0';\r
5983 c = *fmt++;\r
5984 }\r
5985 if (c == 'l' || c == 'L') { /* Prefix: Size is long int */\r
5986 f |= 4; c = *fmt++;\r
5987 }\r
5988 if (!c) break;\r
5989 d = c;\r
5990 if (IsLower(d)) d -= 0x20;\r
5991 switch (d) { /* Type is... */\r
5992 case 'S' : /* String */\r
5993 p = va_arg(arp, TCHAR*);\r
5994 for (j = 0; p[j]; j++) ;\r
5995 if (!(f & 2)) {\r
5996 while (j++ < w) putc_bfd(&pb, ' ');\r
5997 }\r
5998 while (*p) putc_bfd(&pb, *p++);\r
5999 while (j++ < w) putc_bfd(&pb, ' ');\r
6000 continue;\r
6001 case 'C' : /* Character */\r
6002 putc_bfd(&pb, (TCHAR)va_arg(arp, int)); continue;\r
6003 case 'B' : /* Binary */\r
6004 r = 2; break;\r
6005 case 'O' : /* Octal */\r
6006 r = 8; break;\r
6007 case 'D' : /* Signed decimal */\r
6008 case 'U' : /* Unsigned decimal */\r
6009 r = 10; break;\r
6010 case 'X' : /* Hexdecimal */\r
6011 r = 16; break;\r
6012 default: /* Unknown type (pass-through) */\r
6013 putc_bfd(&pb, c); continue;\r
6014 }\r
6015\r
6016 /* Get an argument and put it in numeral */\r
6017 v = (f & 4) ? (DWORD)va_arg(arp, long) : ((d == 'D') ? (DWORD)(long)va_arg(arp, int) : (DWORD)va_arg(arp, unsigned int));\r
6018 if (d == 'D' && (v & 0x80000000)) {\r
6019 v = 0 - v;\r
6020 f |= 8;\r
6021 }\r
6022 i = 0;\r
6023 do {\r
6024 d = (TCHAR)(v % r); v /= r;\r
6025 if (d > 9) d += (c == 'x') ? 0x27 : 0x07;\r
70702af1
L
6026 str[i++] = d + '0';\r
6027 } while (v && i < sizeof str / sizeof str[0]);\r
6028 if (f & 8) str[i++] = '-';\r
53668523
L
6029 j = i; d = (f & 1) ? '0' : ' ';\r
6030 while (!(f & 2) && j++ < w) putc_bfd(&pb, d);\r
70702af1 6031 do putc_bfd(&pb, str[--i]); while (i);\r
53668523
L
6032 while (j++ < w) putc_bfd(&pb, d);\r
6033 }\r
6034\r
6035 va_end(arp);\r
6036\r
70702af1 6037 return putc_flush(&pb);\r
53668523
L
6038}\r
6039\r
6040#endif /* !_FS_READONLY */\r
6041#endif /* _USE_STRFUNC */\r