Implementation of a LocalFileSystem using S25FL216K serial flash memory. Currently only 256kB available!
Fork of S25FL216K_USBFileSystem by
FATFileSystem/ChaN/ff.cpp@5:c8918e47c566, 2014-12-23 (annotated)
- Committer:
- mkilivan
- Date:
- Tue Dec 23 21:36:32 2014 +0000
- Revision:
- 5:c8918e47c566
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mkilivan | 5:c8918e47c566 | 1 | /*----------------------------------------------------------------------------/ |
mkilivan | 5:c8918e47c566 | 2 | / FatFs - FAT file system module R0.09a (C)ChaN, 2012 |
mkilivan | 5:c8918e47c566 | 3 | /-----------------------------------------------------------------------------/ |
mkilivan | 5:c8918e47c566 | 4 | / FatFs module is a generic FAT file system module for small embedded systems. |
mkilivan | 5:c8918e47c566 | 5 | / This is a free software that opened for education, research and commercial |
mkilivan | 5:c8918e47c566 | 6 | / developments under license policy of following terms. |
mkilivan | 5:c8918e47c566 | 7 | / |
mkilivan | 5:c8918e47c566 | 8 | / Copyright (C) 2012, ChaN, all right reserved. |
mkilivan | 5:c8918e47c566 | 9 | / |
mkilivan | 5:c8918e47c566 | 10 | / * The FatFs module is a free software and there is NO WARRANTY. |
mkilivan | 5:c8918e47c566 | 11 | / * No restriction on use. You can use, modify and redistribute it for |
mkilivan | 5:c8918e47c566 | 12 | / personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY. |
mkilivan | 5:c8918e47c566 | 13 | / * Redistributions of source code must retain the above copyright notice. |
mkilivan | 5:c8918e47c566 | 14 | / |
mkilivan | 5:c8918e47c566 | 15 | /-----------------------------------------------------------------------------/ |
mkilivan | 5:c8918e47c566 | 16 | / Feb 26,'06 R0.00 Prototype. |
mkilivan | 5:c8918e47c566 | 17 | / |
mkilivan | 5:c8918e47c566 | 18 | / Apr 29,'06 R0.01 First stable version. |
mkilivan | 5:c8918e47c566 | 19 | / |
mkilivan | 5:c8918e47c566 | 20 | / Jun 01,'06 R0.02 Added FAT12 support. |
mkilivan | 5:c8918e47c566 | 21 | / Removed unbuffered mode. |
mkilivan | 5:c8918e47c566 | 22 | / Fixed a problem on small (<32M) partition. |
mkilivan | 5:c8918e47c566 | 23 | / Jun 10,'06 R0.02a Added a configuration option (_FS_MINIMUM). |
mkilivan | 5:c8918e47c566 | 24 | / |
mkilivan | 5:c8918e47c566 | 25 | / Sep 22,'06 R0.03 Added f_rename(). |
mkilivan | 5:c8918e47c566 | 26 | / Changed option _FS_MINIMUM to _FS_MINIMIZE. |
mkilivan | 5:c8918e47c566 | 27 | / Dec 11,'06 R0.03a Improved cluster scan algorithm to write files fast. |
mkilivan | 5:c8918e47c566 | 28 | / Fixed f_mkdir() creates incorrect directory on FAT32. |
mkilivan | 5:c8918e47c566 | 29 | / |
mkilivan | 5:c8918e47c566 | 30 | / Feb 04,'07 R0.04 Supported multiple drive system. |
mkilivan | 5:c8918e47c566 | 31 | / Changed some interfaces for multiple drive system. |
mkilivan | 5:c8918e47c566 | 32 | / Changed f_mountdrv() to f_mount(). |
mkilivan | 5:c8918e47c566 | 33 | / Added f_mkfs(). |
mkilivan | 5:c8918e47c566 | 34 | / Apr 01,'07 R0.04a Supported multiple partitions on a physical drive. |
mkilivan | 5:c8918e47c566 | 35 | / Added a capability of extending file size to f_lseek(). |
mkilivan | 5:c8918e47c566 | 36 | / Added minimization level 3. |
mkilivan | 5:c8918e47c566 | 37 | / Fixed an endian sensitive code in f_mkfs(). |
mkilivan | 5:c8918e47c566 | 38 | / May 05,'07 R0.04b Added a configuration option _USE_NTFLAG. |
mkilivan | 5:c8918e47c566 | 39 | / Added FSInfo support. |
mkilivan | 5:c8918e47c566 | 40 | / Fixed DBCS name can result FR_INVALID_NAME. |
mkilivan | 5:c8918e47c566 | 41 | / Fixed short seek (<= csize) collapses the file object. |
mkilivan | 5:c8918e47c566 | 42 | / |
mkilivan | 5:c8918e47c566 | 43 | / Aug 25,'07 R0.05 Changed arguments of f_read(), f_write() and f_mkfs(). |
mkilivan | 5:c8918e47c566 | 44 | / Fixed f_mkfs() on FAT32 creates incorrect FSInfo. |
mkilivan | 5:c8918e47c566 | 45 | / Fixed f_mkdir() on FAT32 creates incorrect directory. |
mkilivan | 5:c8918e47c566 | 46 | / Feb 03,'08 R0.05a Added f_truncate() and f_utime(). |
mkilivan | 5:c8918e47c566 | 47 | / Fixed off by one error at FAT sub-type determination. |
mkilivan | 5:c8918e47c566 | 48 | / Fixed btr in f_read() can be mistruncated. |
mkilivan | 5:c8918e47c566 | 49 | / Fixed cached sector is not flushed when create and close without write. |
mkilivan | 5:c8918e47c566 | 50 | / |
mkilivan | 5:c8918e47c566 | 51 | / Apr 01,'08 R0.06 Added fputc(), fputs(), fprintf() and fgets(). |
mkilivan | 5:c8918e47c566 | 52 | / Improved performance of f_lseek() on moving to the same or following cluster. |
mkilivan | 5:c8918e47c566 | 53 | / |
mkilivan | 5:c8918e47c566 | 54 | / Apr 01,'09 R0.07 Merged Tiny-FatFs as a configuration option. (_FS_TINY) |
mkilivan | 5:c8918e47c566 | 55 | / Added long file name feature. |
mkilivan | 5:c8918e47c566 | 56 | / Added multiple code page feature. |
mkilivan | 5:c8918e47c566 | 57 | / Added re-entrancy for multitask operation. |
mkilivan | 5:c8918e47c566 | 58 | / Added auto cluster size selection to f_mkfs(). |
mkilivan | 5:c8918e47c566 | 59 | / Added rewind option to f_readdir(). |
mkilivan | 5:c8918e47c566 | 60 | / Changed result code of critical errors. |
mkilivan | 5:c8918e47c566 | 61 | / Renamed string functions to avoid name collision. |
mkilivan | 5:c8918e47c566 | 62 | / Apr 14,'09 R0.07a Separated out OS dependent code on reentrant cfg. |
mkilivan | 5:c8918e47c566 | 63 | / Added multiple sector size feature. |
mkilivan | 5:c8918e47c566 | 64 | / Jun 21,'09 R0.07c Fixed f_unlink() can return FR_OK on error. |
mkilivan | 5:c8918e47c566 | 65 | / Fixed wrong cache control in f_lseek(). |
mkilivan | 5:c8918e47c566 | 66 | / Added relative path feature. |
mkilivan | 5:c8918e47c566 | 67 | / Added f_chdir() and f_chdrive(). |
mkilivan | 5:c8918e47c566 | 68 | / Added proper case conversion to extended char. |
mkilivan | 5:c8918e47c566 | 69 | / Nov 03,'09 R0.07e Separated out configuration options from ff.h to ffconf.h. |
mkilivan | 5:c8918e47c566 | 70 | / Fixed f_unlink() fails to remove a sub-dir on _FS_RPATH. |
mkilivan | 5:c8918e47c566 | 71 | / Fixed name matching error on the 13 char boundary. |
mkilivan | 5:c8918e47c566 | 72 | / Added a configuration option, _LFN_UNICODE. |
mkilivan | 5:c8918e47c566 | 73 | / Changed f_readdir() to return the SFN with always upper case on non-LFN cfg. |
mkilivan | 5:c8918e47c566 | 74 | / |
mkilivan | 5:c8918e47c566 | 75 | / May 15,'10 R0.08 Added a memory configuration option. (_USE_LFN = 3) |
mkilivan | 5:c8918e47c566 | 76 | / Added file lock feature. (_FS_SHARE) |
mkilivan | 5:c8918e47c566 | 77 | / Added fast seek feature. (_USE_FASTSEEK) |
mkilivan | 5:c8918e47c566 | 78 | / Changed some types on the API, XCHAR->TCHAR. |
mkilivan | 5:c8918e47c566 | 79 | / Changed fname member in the FILINFO structure on Unicode cfg. |
mkilivan | 5:c8918e47c566 | 80 | / String functions support UTF-8 encoding files on Unicode cfg. |
mkilivan | 5:c8918e47c566 | 81 | / Aug 16,'10 R0.08a Added f_getcwd(). (_FS_RPATH = 2) |
mkilivan | 5:c8918e47c566 | 82 | / Added sector erase feature. (_USE_ERASE) |
mkilivan | 5:c8918e47c566 | 83 | / Moved file lock semaphore table from fs object to the bss. |
mkilivan | 5:c8918e47c566 | 84 | / Fixed a wrong directory entry is created on non-LFN cfg when the given name contains ';'. |
mkilivan | 5:c8918e47c566 | 85 | / Fixed f_mkfs() creates wrong FAT32 volume. |
mkilivan | 5:c8918e47c566 | 86 | / Jan 15,'11 R0.08b Fast seek feature is also applied to f_read() and f_write(). |
mkilivan | 5:c8918e47c566 | 87 | / f_lseek() reports required table size on creating CLMP. |
mkilivan | 5:c8918e47c566 | 88 | / Extended format syntax of f_printf function. |
mkilivan | 5:c8918e47c566 | 89 | / Ignores duplicated directory separators in given path name. |
mkilivan | 5:c8918e47c566 | 90 | / |
mkilivan | 5:c8918e47c566 | 91 | / Sep 06,'11 R0.09 f_mkfs() supports multiple partition to finish the multiple partition feature. |
mkilivan | 5:c8918e47c566 | 92 | / Added f_fdisk(). (_MULTI_PARTITION = 2) |
mkilivan | 5:c8918e47c566 | 93 | / Aug 27,'12 R0.09a Fixed assertion failure due to OS/2 EA on FAT12/16 volume. |
mkilivan | 5:c8918e47c566 | 94 | / Changed f_open() and f_opendir reject null object pointer to avoid crash. |
mkilivan | 5:c8918e47c566 | 95 | / Changed option name _FS_SHARE to _FS_LOCK. |
mkilivan | 5:c8918e47c566 | 96 | /---------------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 97 | |
mkilivan | 5:c8918e47c566 | 98 | #include "ff.h" /* FatFs configurations and declarations */ |
mkilivan | 5:c8918e47c566 | 99 | #include "diskio.h" /* Declarations of low level disk I/O functions */ |
mkilivan | 5:c8918e47c566 | 100 | |
mkilivan | 5:c8918e47c566 | 101 | /*-------------------------------------------------------------------------- |
mkilivan | 5:c8918e47c566 | 102 | |
mkilivan | 5:c8918e47c566 | 103 | Module Private Definitions |
mkilivan | 5:c8918e47c566 | 104 | |
mkilivan | 5:c8918e47c566 | 105 | ---------------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 106 | |
mkilivan | 5:c8918e47c566 | 107 | #if _FATFS != 4004 /* Revision ID */ |
mkilivan | 5:c8918e47c566 | 108 | #error Wrong include file (ff.h). |
mkilivan | 5:c8918e47c566 | 109 | #endif |
mkilivan | 5:c8918e47c566 | 110 | |
mkilivan | 5:c8918e47c566 | 111 | |
mkilivan | 5:c8918e47c566 | 112 | /* Definitions on sector size */ |
mkilivan | 5:c8918e47c566 | 113 | #if _MAX_SS != 512 && _MAX_SS != 1024 && _MAX_SS != 2048 && _MAX_SS != 4096 |
mkilivan | 5:c8918e47c566 | 114 | #error Wrong sector size. |
mkilivan | 5:c8918e47c566 | 115 | #endif |
mkilivan | 5:c8918e47c566 | 116 | #if _MAX_SS != 512 |
mkilivan | 5:c8918e47c566 | 117 | #define SS(fs) ((fs)->ssize) /* Variable sector size */ |
mkilivan | 5:c8918e47c566 | 118 | #else |
mkilivan | 5:c8918e47c566 | 119 | #define SS(fs) 512U /* Fixed sector size */ |
mkilivan | 5:c8918e47c566 | 120 | #endif |
mkilivan | 5:c8918e47c566 | 121 | |
mkilivan | 5:c8918e47c566 | 122 | |
mkilivan | 5:c8918e47c566 | 123 | /* Reentrancy related */ |
mkilivan | 5:c8918e47c566 | 124 | #if _FS_REENTRANT |
mkilivan | 5:c8918e47c566 | 125 | #if _USE_LFN == 1 |
mkilivan | 5:c8918e47c566 | 126 | #error Static LFN work area must not be used in re-entrant configuration. |
mkilivan | 5:c8918e47c566 | 127 | #endif |
mkilivan | 5:c8918e47c566 | 128 | #define ENTER_FF(fs) { if (!lock_fs(fs)) return FR_TIMEOUT; } |
mkilivan | 5:c8918e47c566 | 129 | #define LEAVE_FF(fs, res) { unlock_fs(fs, res); return res; } |
mkilivan | 5:c8918e47c566 | 130 | #else |
mkilivan | 5:c8918e47c566 | 131 | #define ENTER_FF(fs) |
mkilivan | 5:c8918e47c566 | 132 | #define LEAVE_FF(fs, res) return res |
mkilivan | 5:c8918e47c566 | 133 | #endif |
mkilivan | 5:c8918e47c566 | 134 | |
mkilivan | 5:c8918e47c566 | 135 | #define ABORT(fs, res) { fp->flag |= FA__ERROR; LEAVE_FF(fs, res); } |
mkilivan | 5:c8918e47c566 | 136 | |
mkilivan | 5:c8918e47c566 | 137 | |
mkilivan | 5:c8918e47c566 | 138 | /* File access control feature */ |
mkilivan | 5:c8918e47c566 | 139 | #if _FS_LOCK |
mkilivan | 5:c8918e47c566 | 140 | #if _FS_READONLY |
mkilivan | 5:c8918e47c566 | 141 | #error _FS_LOCK must be 0 on read-only cfg. |
mkilivan | 5:c8918e47c566 | 142 | #endif |
mkilivan | 5:c8918e47c566 | 143 | typedef struct { |
mkilivan | 5:c8918e47c566 | 144 | FATFS *fs; /* File ID 1, volume (NULL:blank entry) */ |
mkilivan | 5:c8918e47c566 | 145 | DWORD clu; /* File ID 2, directory */ |
mkilivan | 5:c8918e47c566 | 146 | WORD idx; /* File ID 3, directory index */ |
mkilivan | 5:c8918e47c566 | 147 | WORD ctr; /* File open counter, 0:none, 0x01..0xFF:read open count, 0x100:write mode */ |
mkilivan | 5:c8918e47c566 | 148 | } FILESEM; |
mkilivan | 5:c8918e47c566 | 149 | #endif |
mkilivan | 5:c8918e47c566 | 150 | |
mkilivan | 5:c8918e47c566 | 151 | |
mkilivan | 5:c8918e47c566 | 152 | |
mkilivan | 5:c8918e47c566 | 153 | /* DBCS code ranges and SBCS extend char conversion table */ |
mkilivan | 5:c8918e47c566 | 154 | |
mkilivan | 5:c8918e47c566 | 155 | #if _CODE_PAGE == 932 /* Japanese Shift-JIS */ |
mkilivan | 5:c8918e47c566 | 156 | #define _DF1S 0x81 /* DBC 1st byte range 1 start */ |
mkilivan | 5:c8918e47c566 | 157 | #define _DF1E 0x9F /* DBC 1st byte range 1 end */ |
mkilivan | 5:c8918e47c566 | 158 | #define _DF2S 0xE0 /* DBC 1st byte range 2 start */ |
mkilivan | 5:c8918e47c566 | 159 | #define _DF2E 0xFC /* DBC 1st byte range 2 end */ |
mkilivan | 5:c8918e47c566 | 160 | #define _DS1S 0x40 /* DBC 2nd byte range 1 start */ |
mkilivan | 5:c8918e47c566 | 161 | #define _DS1E 0x7E /* DBC 2nd byte range 1 end */ |
mkilivan | 5:c8918e47c566 | 162 | #define _DS2S 0x80 /* DBC 2nd byte range 2 start */ |
mkilivan | 5:c8918e47c566 | 163 | #define _DS2E 0xFC /* DBC 2nd byte range 2 end */ |
mkilivan | 5:c8918e47c566 | 164 | |
mkilivan | 5:c8918e47c566 | 165 | #elif _CODE_PAGE == 936 /* Simplified Chinese GBK */ |
mkilivan | 5:c8918e47c566 | 166 | #define _DF1S 0x81 |
mkilivan | 5:c8918e47c566 | 167 | #define _DF1E 0xFE |
mkilivan | 5:c8918e47c566 | 168 | #define _DS1S 0x40 |
mkilivan | 5:c8918e47c566 | 169 | #define _DS1E 0x7E |
mkilivan | 5:c8918e47c566 | 170 | #define _DS2S 0x80 |
mkilivan | 5:c8918e47c566 | 171 | #define _DS2E 0xFE |
mkilivan | 5:c8918e47c566 | 172 | |
mkilivan | 5:c8918e47c566 | 173 | #elif _CODE_PAGE == 949 /* Korean */ |
mkilivan | 5:c8918e47c566 | 174 | #define _DF1S 0x81 |
mkilivan | 5:c8918e47c566 | 175 | #define _DF1E 0xFE |
mkilivan | 5:c8918e47c566 | 176 | #define _DS1S 0x41 |
mkilivan | 5:c8918e47c566 | 177 | #define _DS1E 0x5A |
mkilivan | 5:c8918e47c566 | 178 | #define _DS2S 0x61 |
mkilivan | 5:c8918e47c566 | 179 | #define _DS2E 0x7A |
mkilivan | 5:c8918e47c566 | 180 | #define _DS3S 0x81 |
mkilivan | 5:c8918e47c566 | 181 | #define _DS3E 0xFE |
mkilivan | 5:c8918e47c566 | 182 | |
mkilivan | 5:c8918e47c566 | 183 | #elif _CODE_PAGE == 950 /* Traditional Chinese Big5 */ |
mkilivan | 5:c8918e47c566 | 184 | #define _DF1S 0x81 |
mkilivan | 5:c8918e47c566 | 185 | #define _DF1E 0xFE |
mkilivan | 5:c8918e47c566 | 186 | #define _DS1S 0x40 |
mkilivan | 5:c8918e47c566 | 187 | #define _DS1E 0x7E |
mkilivan | 5:c8918e47c566 | 188 | #define _DS2S 0xA1 |
mkilivan | 5:c8918e47c566 | 189 | #define _DS2E 0xFE |
mkilivan | 5:c8918e47c566 | 190 | |
mkilivan | 5:c8918e47c566 | 191 | #elif _CODE_PAGE == 437 /* U.S. (OEM) */ |
mkilivan | 5:c8918e47c566 | 192 | #define _DF1S 0 |
mkilivan | 5:c8918e47c566 | 193 | #define _EXCVT {0x80,0x9A,0x90,0x41,0x8E,0x41,0x8F,0x80,0x45,0x45,0x45,0x49,0x49,0x49,0x8E,0x8F,0x90,0x92,0x92,0x4F,0x99,0x4F,0x55,0x55,0x59,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \ |
mkilivan | 5:c8918e47c566 | 194 | 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0x21,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \ |
mkilivan | 5:c8918e47c566 | 195 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ |
mkilivan | 5:c8918e47c566 | 196 | 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF} |
mkilivan | 5:c8918e47c566 | 197 | |
mkilivan | 5:c8918e47c566 | 198 | #elif _CODE_PAGE == 720 /* Arabic (OEM) */ |
mkilivan | 5:c8918e47c566 | 199 | #define _DF1S 0 |
mkilivan | 5:c8918e47c566 | 200 | #define _EXCVT {0x80,0x81,0x45,0x41,0x84,0x41,0x86,0x43,0x45,0x45,0x45,0x49,0x49,0x8D,0x8E,0x8F,0x90,0x92,0x92,0x93,0x94,0x95,0x49,0x49,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \ |
mkilivan | 5:c8918e47c566 | 201 | 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \ |
mkilivan | 5:c8918e47c566 | 202 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ |
mkilivan | 5:c8918e47c566 | 203 | 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF} |
mkilivan | 5:c8918e47c566 | 204 | |
mkilivan | 5:c8918e47c566 | 205 | #elif _CODE_PAGE == 737 /* Greek (OEM) */ |
mkilivan | 5:c8918e47c566 | 206 | #define _DF1S 0 |
mkilivan | 5:c8918e47c566 | 207 | #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x92,0x92,0x93,0x94,0x95,0x96,0x97,0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87, \ |
mkilivan | 5:c8918e47c566 | 208 | 0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0xAA,0x92,0x93,0x94,0x95,0x96,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \ |
mkilivan | 5:c8918e47c566 | 209 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ |
mkilivan | 5:c8918e47c566 | 210 | 0x97,0xEA,0xEB,0xEC,0xE4,0xED,0xEE,0xE7,0xE8,0xF1,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF} |
mkilivan | 5:c8918e47c566 | 211 | |
mkilivan | 5:c8918e47c566 | 212 | #elif _CODE_PAGE == 775 /* Baltic (OEM) */ |
mkilivan | 5:c8918e47c566 | 213 | #define _DF1S 0 |
mkilivan | 5:c8918e47c566 | 214 | #define _EXCVT {0x80,0x9A,0x91,0xA0,0x8E,0x95,0x8F,0x80,0xAD,0xED,0x8A,0x8A,0xA1,0x8D,0x8E,0x8F,0x90,0x92,0x92,0xE2,0x99,0x95,0x96,0x97,0x97,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9F, \ |
mkilivan | 5:c8918e47c566 | 215 | 0xA0,0xA1,0xE0,0xA3,0xA3,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \ |
mkilivan | 5:c8918e47c566 | 216 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xB5,0xB6,0xB7,0xB8,0xBD,0xBE,0xC6,0xC7,0xA5,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ |
mkilivan | 5:c8918e47c566 | 217 | 0xE0,0xE1,0xE2,0xE3,0xE5,0xE5,0xE6,0xE3,0xE8,0xE8,0xEA,0xEA,0xEE,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF} |
mkilivan | 5:c8918e47c566 | 218 | |
mkilivan | 5:c8918e47c566 | 219 | #elif _CODE_PAGE == 850 /* Multilingual Latin 1 (OEM) */ |
mkilivan | 5:c8918e47c566 | 220 | #define _DF1S 0 |
mkilivan | 5:c8918e47c566 | 221 | #define _EXCVT {0x80,0x9A,0x90,0xB6,0x8E,0xB7,0x8F,0x80,0xD2,0xD3,0xD4,0xD8,0xD7,0xDE,0x8E,0x8F,0x90,0x92,0x92,0xE2,0x99,0xE3,0xEA,0xEB,0x59,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9F, \ |
mkilivan | 5:c8918e47c566 | 222 | 0xB5,0xD6,0xE0,0xE9,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0x21,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \ |
mkilivan | 5:c8918e47c566 | 223 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC7,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ |
mkilivan | 5:c8918e47c566 | 224 | 0xE0,0xE1,0xE2,0xE3,0xE5,0xE5,0xE6,0xE7,0xE7,0xE9,0xEA,0xEB,0xED,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF} |
mkilivan | 5:c8918e47c566 | 225 | |
mkilivan | 5:c8918e47c566 | 226 | #elif _CODE_PAGE == 852 /* Latin 2 (OEM) */ |
mkilivan | 5:c8918e47c566 | 227 | #define _DF1S 0 |
mkilivan | 5:c8918e47c566 | 228 | #define _EXCVT {0x80,0x9A,0x90,0xB6,0x8E,0xDE,0x8F,0x80,0x9D,0xD3,0x8A,0x8A,0xD7,0x8D,0x8E,0x8F,0x90,0x91,0x91,0xE2,0x99,0x95,0x95,0x97,0x97,0x99,0x9A,0x9B,0x9B,0x9D,0x9E,0x9F, \ |
mkilivan | 5:c8918e47c566 | 229 | 0xB5,0xD6,0xE0,0xE9,0xA4,0xA4,0xA6,0xA6,0xA8,0xA8,0xAA,0x8D,0xAC,0xB8,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBD,0xBF, \ |
mkilivan | 5:c8918e47c566 | 230 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC6,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD1,0xD1,0xD2,0xD3,0xD2,0xD5,0xD6,0xD7,0xB7,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ |
mkilivan | 5:c8918e47c566 | 231 | 0xE0,0xE1,0xE2,0xE3,0xE3,0xD5,0xE6,0xE6,0xE8,0xE9,0xE8,0xEB,0xED,0xED,0xDD,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xEB,0xFC,0xFC,0xFE,0xFF} |
mkilivan | 5:c8918e47c566 | 232 | |
mkilivan | 5:c8918e47c566 | 233 | #elif _CODE_PAGE == 855 /* Cyrillic (OEM) */ |
mkilivan | 5:c8918e47c566 | 234 | #define _DF1S 0 |
mkilivan | 5:c8918e47c566 | 235 | #define _EXCVT {0x81,0x81,0x83,0x83,0x85,0x85,0x87,0x87,0x89,0x89,0x8B,0x8B,0x8D,0x8D,0x8F,0x8F,0x91,0x91,0x93,0x93,0x95,0x95,0x97,0x97,0x99,0x99,0x9B,0x9B,0x9D,0x9D,0x9F,0x9F, \ |
mkilivan | 5:c8918e47c566 | 236 | 0xA1,0xA1,0xA3,0xA3,0xA5,0xA5,0xA7,0xA7,0xA9,0xA9,0xAB,0xAB,0xAD,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB6,0xB6,0xB8,0xB8,0xB9,0xBA,0xBB,0xBC,0xBE,0xBE,0xBF, \ |
mkilivan | 5:c8918e47c566 | 237 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC7,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD1,0xD1,0xD3,0xD3,0xD5,0xD5,0xD7,0xD7,0xDD,0xD9,0xDA,0xDB,0xDC,0xDD,0xE0,0xDF, \ |
mkilivan | 5:c8918e47c566 | 238 | 0xE0,0xE2,0xE2,0xE4,0xE4,0xE6,0xE6,0xE8,0xE8,0xEA,0xEA,0xEC,0xEC,0xEE,0xEE,0xEF,0xF0,0xF2,0xF2,0xF4,0xF4,0xF6,0xF6,0xF8,0xF8,0xFA,0xFA,0xFC,0xFC,0xFD,0xFE,0xFF} |
mkilivan | 5:c8918e47c566 | 239 | |
mkilivan | 5:c8918e47c566 | 240 | #elif _CODE_PAGE == 857 /* Turkish (OEM) */ |
mkilivan | 5:c8918e47c566 | 241 | #define _DF1S 0 |
mkilivan | 5:c8918e47c566 | 242 | #define _EXCVT {0x80,0x9A,0x90,0xB6,0x8E,0xB7,0x8F,0x80,0xD2,0xD3,0xD4,0xD8,0xD7,0x98,0x8E,0x8F,0x90,0x92,0x92,0xE2,0x99,0xE3,0xEA,0xEB,0x98,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9E, \ |
mkilivan | 5:c8918e47c566 | 243 | 0xB5,0xD6,0xE0,0xE9,0xA5,0xA5,0xA6,0xA6,0xA8,0xA9,0xAA,0xAB,0xAC,0x21,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \ |
mkilivan | 5:c8918e47c566 | 244 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC7,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ |
mkilivan | 5:c8918e47c566 | 245 | 0xE0,0xE1,0xE2,0xE3,0xE5,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xDE,0x59,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF} |
mkilivan | 5:c8918e47c566 | 246 | |
mkilivan | 5:c8918e47c566 | 247 | #elif _CODE_PAGE == 858 /* Multilingual Latin 1 + Euro (OEM) */ |
mkilivan | 5:c8918e47c566 | 248 | #define _DF1S 0 |
mkilivan | 5:c8918e47c566 | 249 | #define _EXCVT {0x80,0x9A,0x90,0xB6,0x8E,0xB7,0x8F,0x80,0xD2,0xD3,0xD4,0xD8,0xD7,0xDE,0x8E,0x8F,0x90,0x92,0x92,0xE2,0x99,0xE3,0xEA,0xEB,0x59,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9F, \ |
mkilivan | 5:c8918e47c566 | 250 | 0xB5,0xD6,0xE0,0xE9,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0x21,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \ |
mkilivan | 5:c8918e47c566 | 251 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC7,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD1,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ |
mkilivan | 5:c8918e47c566 | 252 | 0xE0,0xE1,0xE2,0xE3,0xE5,0xE5,0xE6,0xE7,0xE7,0xE9,0xEA,0xEB,0xED,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF} |
mkilivan | 5:c8918e47c566 | 253 | |
mkilivan | 5:c8918e47c566 | 254 | #elif _CODE_PAGE == 862 /* Hebrew (OEM) */ |
mkilivan | 5:c8918e47c566 | 255 | #define _DF1S 0 |
mkilivan | 5:c8918e47c566 | 256 | #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \ |
mkilivan | 5:c8918e47c566 | 257 | 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0x21,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \ |
mkilivan | 5:c8918e47c566 | 258 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ |
mkilivan | 5:c8918e47c566 | 259 | 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF} |
mkilivan | 5:c8918e47c566 | 260 | |
mkilivan | 5:c8918e47c566 | 261 | #elif _CODE_PAGE == 866 /* Russian (OEM) */ |
mkilivan | 5:c8918e47c566 | 262 | #define _DF1S 0 |
mkilivan | 5:c8918e47c566 | 263 | #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \ |
mkilivan | 5:c8918e47c566 | 264 | 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \ |
mkilivan | 5:c8918e47c566 | 265 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ |
mkilivan | 5:c8918e47c566 | 266 | 0x90,0x91,0x92,0x93,0x9d,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F,0xF0,0xF0,0xF2,0xF2,0xF4,0xF4,0xF6,0xF6,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF} |
mkilivan | 5:c8918e47c566 | 267 | |
mkilivan | 5:c8918e47c566 | 268 | #elif _CODE_PAGE == 874 /* Thai (OEM, Windows) */ |
mkilivan | 5:c8918e47c566 | 269 | #define _DF1S 0 |
mkilivan | 5:c8918e47c566 | 270 | #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \ |
mkilivan | 5:c8918e47c566 | 271 | 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \ |
mkilivan | 5:c8918e47c566 | 272 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ |
mkilivan | 5:c8918e47c566 | 273 | 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF} |
mkilivan | 5:c8918e47c566 | 274 | |
mkilivan | 5:c8918e47c566 | 275 | #elif _CODE_PAGE == 1250 /* Central Europe (Windows) */ |
mkilivan | 5:c8918e47c566 | 276 | #define _DF1S 0 |
mkilivan | 5:c8918e47c566 | 277 | #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x8A,0x9B,0x8C,0x8D,0x8E,0x8F, \ |
mkilivan | 5:c8918e47c566 | 278 | 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xA3,0xB4,0xB5,0xB6,0xB7,0xB8,0xA5,0xAA,0xBB,0xBC,0xBD,0xBC,0xAF, \ |
mkilivan | 5:c8918e47c566 | 279 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ |
mkilivan | 5:c8918e47c566 | 280 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xF7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xFF} |
mkilivan | 5:c8918e47c566 | 281 | |
mkilivan | 5:c8918e47c566 | 282 | #elif _CODE_PAGE == 1251 /* Cyrillic (Windows) */ |
mkilivan | 5:c8918e47c566 | 283 | #define _DF1S 0 |
mkilivan | 5:c8918e47c566 | 284 | #define _EXCVT {0x80,0x81,0x82,0x82,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x80,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x8A,0x9B,0x8C,0x8D,0x8E,0x8F, \ |
mkilivan | 5:c8918e47c566 | 285 | 0xA0,0xA2,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB2,0xA5,0xB5,0xB6,0xB7,0xA8,0xB9,0xAA,0xBB,0xA3,0xBD,0xBD,0xAF, \ |
mkilivan | 5:c8918e47c566 | 286 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ |
mkilivan | 5:c8918e47c566 | 287 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF} |
mkilivan | 5:c8918e47c566 | 288 | |
mkilivan | 5:c8918e47c566 | 289 | #elif _CODE_PAGE == 1252 /* Latin 1 (Windows) */ |
mkilivan | 5:c8918e47c566 | 290 | #define _DF1S 0 |
mkilivan | 5:c8918e47c566 | 291 | #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0xAd,0x9B,0x8C,0x9D,0xAE,0x9F, \ |
mkilivan | 5:c8918e47c566 | 292 | 0xA0,0x21,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \ |
mkilivan | 5:c8918e47c566 | 293 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ |
mkilivan | 5:c8918e47c566 | 294 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xF7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0x9F} |
mkilivan | 5:c8918e47c566 | 295 | |
mkilivan | 5:c8918e47c566 | 296 | #elif _CODE_PAGE == 1253 /* Greek (Windows) */ |
mkilivan | 5:c8918e47c566 | 297 | #define _DF1S 0 |
mkilivan | 5:c8918e47c566 | 298 | #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \ |
mkilivan | 5:c8918e47c566 | 299 | 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \ |
mkilivan | 5:c8918e47c566 | 300 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xA2,0xB8,0xB9,0xBA, \ |
mkilivan | 5:c8918e47c566 | 301 | 0xE0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xF2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xFB,0xBC,0xFD,0xBF,0xFF} |
mkilivan | 5:c8918e47c566 | 302 | |
mkilivan | 5:c8918e47c566 | 303 | #elif _CODE_PAGE == 1254 /* Turkish (Windows) */ |
mkilivan | 5:c8918e47c566 | 304 | #define _DF1S 0 |
mkilivan | 5:c8918e47c566 | 305 | #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x8A,0x9B,0x8C,0x9D,0x9E,0x9F, \ |
mkilivan | 5:c8918e47c566 | 306 | 0xA0,0x21,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \ |
mkilivan | 5:c8918e47c566 | 307 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ |
mkilivan | 5:c8918e47c566 | 308 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xF7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0x9F} |
mkilivan | 5:c8918e47c566 | 309 | |
mkilivan | 5:c8918e47c566 | 310 | #elif _CODE_PAGE == 1255 /* Hebrew (Windows) */ |
mkilivan | 5:c8918e47c566 | 311 | #define _DF1S 0 |
mkilivan | 5:c8918e47c566 | 312 | #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \ |
mkilivan | 5:c8918e47c566 | 313 | 0xA0,0x21,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \ |
mkilivan | 5:c8918e47c566 | 314 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ |
mkilivan | 5:c8918e47c566 | 315 | 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF} |
mkilivan | 5:c8918e47c566 | 316 | |
mkilivan | 5:c8918e47c566 | 317 | #elif _CODE_PAGE == 1256 /* Arabic (Windows) */ |
mkilivan | 5:c8918e47c566 | 318 | #define _DF1S 0 |
mkilivan | 5:c8918e47c566 | 319 | #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x8C,0x9D,0x9E,0x9F, \ |
mkilivan | 5:c8918e47c566 | 320 | 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \ |
mkilivan | 5:c8918e47c566 | 321 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ |
mkilivan | 5:c8918e47c566 | 322 | 0x41,0xE1,0x41,0xE3,0xE4,0xE5,0xE6,0x43,0x45,0x45,0x45,0x45,0xEC,0xED,0x49,0x49,0xF0,0xF1,0xF2,0xF3,0x4F,0xF5,0xF6,0xF7,0xF8,0x55,0xFA,0x55,0x55,0xFD,0xFE,0xFF} |
mkilivan | 5:c8918e47c566 | 323 | |
mkilivan | 5:c8918e47c566 | 324 | #elif _CODE_PAGE == 1257 /* Baltic (Windows) */ |
mkilivan | 5:c8918e47c566 | 325 | #define _DF1S 0 |
mkilivan | 5:c8918e47c566 | 326 | #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \ |
mkilivan | 5:c8918e47c566 | 327 | 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xA8,0xB9,0xAA,0xBB,0xBC,0xBD,0xBE,0xAF, \ |
mkilivan | 5:c8918e47c566 | 328 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ |
mkilivan | 5:c8918e47c566 | 329 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xF7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xFF} |
mkilivan | 5:c8918e47c566 | 330 | |
mkilivan | 5:c8918e47c566 | 331 | #elif _CODE_PAGE == 1258 /* Vietnam (OEM, Windows) */ |
mkilivan | 5:c8918e47c566 | 332 | #define _DF1S 0 |
mkilivan | 5:c8918e47c566 | 333 | #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0xAC,0x9D,0x9E,0x9F, \ |
mkilivan | 5:c8918e47c566 | 334 | 0xA0,0x21,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \ |
mkilivan | 5:c8918e47c566 | 335 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ |
mkilivan | 5:c8918e47c566 | 336 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xEC,0xCD,0xCE,0xCF,0xD0,0xD1,0xF2,0xD3,0xD4,0xD5,0xD6,0xF7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xFE,0x9F} |
mkilivan | 5:c8918e47c566 | 337 | |
mkilivan | 5:c8918e47c566 | 338 | #elif _CODE_PAGE == 1 /* ASCII (for only non-LFN cfg) */ |
mkilivan | 5:c8918e47c566 | 339 | #if _USE_LFN |
mkilivan | 5:c8918e47c566 | 340 | #error Cannot use LFN feature without valid code page. |
mkilivan | 5:c8918e47c566 | 341 | #endif |
mkilivan | 5:c8918e47c566 | 342 | #define _DF1S 0 |
mkilivan | 5:c8918e47c566 | 343 | |
mkilivan | 5:c8918e47c566 | 344 | #else |
mkilivan | 5:c8918e47c566 | 345 | #error Unknown code page |
mkilivan | 5:c8918e47c566 | 346 | |
mkilivan | 5:c8918e47c566 | 347 | #endif |
mkilivan | 5:c8918e47c566 | 348 | |
mkilivan | 5:c8918e47c566 | 349 | |
mkilivan | 5:c8918e47c566 | 350 | /* Character code support macros */ |
mkilivan | 5:c8918e47c566 | 351 | #define IsUpper(c) (((c)>='A')&&((c)<='Z')) |
mkilivan | 5:c8918e47c566 | 352 | #define IsLower(c) (((c)>='a')&&((c)<='z')) |
mkilivan | 5:c8918e47c566 | 353 | #define IsDigit(c) (((c)>='0')&&((c)<='9')) |
mkilivan | 5:c8918e47c566 | 354 | |
mkilivan | 5:c8918e47c566 | 355 | #if _DF1S /* Code page is DBCS */ |
mkilivan | 5:c8918e47c566 | 356 | |
mkilivan | 5:c8918e47c566 | 357 | #ifdef _DF2S /* Two 1st byte areas */ |
mkilivan | 5:c8918e47c566 | 358 | #define IsDBCS1(c) (((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E) || ((BYTE)(c) >= _DF2S && (BYTE)(c) <= _DF2E)) |
mkilivan | 5:c8918e47c566 | 359 | #else /* One 1st byte area */ |
mkilivan | 5:c8918e47c566 | 360 | #define IsDBCS1(c) ((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E) |
mkilivan | 5:c8918e47c566 | 361 | #endif |
mkilivan | 5:c8918e47c566 | 362 | |
mkilivan | 5:c8918e47c566 | 363 | #ifdef _DS3S /* Three 2nd byte areas */ |
mkilivan | 5:c8918e47c566 | 364 | #define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E) || ((BYTE)(c) >= _DS3S && (BYTE)(c) <= _DS3E)) |
mkilivan | 5:c8918e47c566 | 365 | #else /* Two 2nd byte areas */ |
mkilivan | 5:c8918e47c566 | 366 | #define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E)) |
mkilivan | 5:c8918e47c566 | 367 | #endif |
mkilivan | 5:c8918e47c566 | 368 | |
mkilivan | 5:c8918e47c566 | 369 | #else /* Code page is SBCS */ |
mkilivan | 5:c8918e47c566 | 370 | |
mkilivan | 5:c8918e47c566 | 371 | #define IsDBCS1(c) 0 |
mkilivan | 5:c8918e47c566 | 372 | #define IsDBCS2(c) 0 |
mkilivan | 5:c8918e47c566 | 373 | |
mkilivan | 5:c8918e47c566 | 374 | #endif /* _DF1S */ |
mkilivan | 5:c8918e47c566 | 375 | |
mkilivan | 5:c8918e47c566 | 376 | |
mkilivan | 5:c8918e47c566 | 377 | /* Name status flags */ |
mkilivan | 5:c8918e47c566 | 378 | #define NS 11 /* Index of name status byte in fn[] */ |
mkilivan | 5:c8918e47c566 | 379 | #define NS_LOSS 0x01 /* Out of 8.3 format */ |
mkilivan | 5:c8918e47c566 | 380 | #define NS_LFN 0x02 /* Force to create LFN entry */ |
mkilivan | 5:c8918e47c566 | 381 | #define NS_LAST 0x04 /* Last segment */ |
mkilivan | 5:c8918e47c566 | 382 | #define NS_BODY 0x08 /* Lower case flag (body) */ |
mkilivan | 5:c8918e47c566 | 383 | #define NS_EXT 0x10 /* Lower case flag (ext) */ |
mkilivan | 5:c8918e47c566 | 384 | #define NS_DOT 0x20 /* Dot entry */ |
mkilivan | 5:c8918e47c566 | 385 | |
mkilivan | 5:c8918e47c566 | 386 | |
mkilivan | 5:c8918e47c566 | 387 | /* FAT sub-type boundaries */ |
mkilivan | 5:c8918e47c566 | 388 | /* Note that the FAT spec by Microsoft says 4085 but Windows works with 4087! */ |
mkilivan | 5:c8918e47c566 | 389 | #define MIN_FAT16 4086 /* Minimum number of clusters for FAT16 */ |
mkilivan | 5:c8918e47c566 | 390 | #define MIN_FAT32 65526 /* Minimum number of clusters for FAT32 */ |
mkilivan | 5:c8918e47c566 | 391 | |
mkilivan | 5:c8918e47c566 | 392 | |
mkilivan | 5:c8918e47c566 | 393 | /* FatFs refers the members in the FAT structures as byte array instead of |
mkilivan | 5:c8918e47c566 | 394 | / structure member because the structure is not binary compatible between |
mkilivan | 5:c8918e47c566 | 395 | / different platforms */ |
mkilivan | 5:c8918e47c566 | 396 | |
mkilivan | 5:c8918e47c566 | 397 | #define BS_jmpBoot 0 /* Jump instruction (3) */ |
mkilivan | 5:c8918e47c566 | 398 | #define BS_OEMName 3 /* OEM name (8) */ |
mkilivan | 5:c8918e47c566 | 399 | #define BPB_BytsPerSec 11 /* Sector size [byte] (2) */ |
mkilivan | 5:c8918e47c566 | 400 | #define BPB_SecPerClus 13 /* Cluster size [sector] (1) */ |
mkilivan | 5:c8918e47c566 | 401 | #define BPB_RsvdSecCnt 14 /* Size of reserved area [sector] (2) */ |
mkilivan | 5:c8918e47c566 | 402 | #define BPB_NumFATs 16 /* Number of FAT copies (1) */ |
mkilivan | 5:c8918e47c566 | 403 | #define BPB_RootEntCnt 17 /* Number of root dir entries for FAT12/16 (2) */ |
mkilivan | 5:c8918e47c566 | 404 | #define BPB_TotSec16 19 /* Volume size [sector] (2) */ |
mkilivan | 5:c8918e47c566 | 405 | #define BPB_Media 21 /* Media descriptor (1) */ |
mkilivan | 5:c8918e47c566 | 406 | #define BPB_FATSz16 22 /* FAT size [sector] (2) */ |
mkilivan | 5:c8918e47c566 | 407 | #define BPB_SecPerTrk 24 /* Track size [sector] (2) */ |
mkilivan | 5:c8918e47c566 | 408 | #define BPB_NumHeads 26 /* Number of heads (2) */ |
mkilivan | 5:c8918e47c566 | 409 | #define BPB_HiddSec 28 /* Number of special hidden sectors (4) */ |
mkilivan | 5:c8918e47c566 | 410 | #define BPB_TotSec32 32 /* Volume size [sector] (4) */ |
mkilivan | 5:c8918e47c566 | 411 | #define BS_DrvNum 36 /* Physical drive number (2) */ |
mkilivan | 5:c8918e47c566 | 412 | #define BS_BootSig 38 /* Extended boot signature (1) */ |
mkilivan | 5:c8918e47c566 | 413 | #define BS_VolID 39 /* Volume serial number (4) */ |
mkilivan | 5:c8918e47c566 | 414 | #define BS_VolLab 43 /* Volume label (8) */ |
mkilivan | 5:c8918e47c566 | 415 | #define BS_FilSysType 54 /* File system type (1) */ |
mkilivan | 5:c8918e47c566 | 416 | #define BPB_FATSz32 36 /* FAT size [sector] (4) */ |
mkilivan | 5:c8918e47c566 | 417 | #define BPB_ExtFlags 40 /* Extended flags (2) */ |
mkilivan | 5:c8918e47c566 | 418 | #define BPB_FSVer 42 /* File system version (2) */ |
mkilivan | 5:c8918e47c566 | 419 | #define BPB_RootClus 44 /* Root dir first cluster (4) */ |
mkilivan | 5:c8918e47c566 | 420 | #define BPB_FSInfo 48 /* Offset of FSInfo sector (2) */ |
mkilivan | 5:c8918e47c566 | 421 | #define BPB_BkBootSec 50 /* Offset of backup boot sector (2) */ |
mkilivan | 5:c8918e47c566 | 422 | #define BS_DrvNum32 64 /* Physical drive number (2) */ |
mkilivan | 5:c8918e47c566 | 423 | #define BS_BootSig32 66 /* Extended boot signature (1) */ |
mkilivan | 5:c8918e47c566 | 424 | #define BS_VolID32 67 /* Volume serial number (4) */ |
mkilivan | 5:c8918e47c566 | 425 | #define BS_VolLab32 71 /* Volume label (8) */ |
mkilivan | 5:c8918e47c566 | 426 | #define BS_FilSysType32 82 /* File system type (1) */ |
mkilivan | 5:c8918e47c566 | 427 | #define FSI_LeadSig 0 /* FSI: Leading signature (4) */ |
mkilivan | 5:c8918e47c566 | 428 | #define FSI_StrucSig 484 /* FSI: Structure signature (4) */ |
mkilivan | 5:c8918e47c566 | 429 | #define FSI_Free_Count 488 /* FSI: Number of free clusters (4) */ |
mkilivan | 5:c8918e47c566 | 430 | #define FSI_Nxt_Free 492 /* FSI: Last allocated cluster (4) */ |
mkilivan | 5:c8918e47c566 | 431 | #define MBR_Table 446 /* MBR: Partition table offset (2) */ |
mkilivan | 5:c8918e47c566 | 432 | #define SZ_PTE 16 /* MBR: Size of a partition table entry */ |
mkilivan | 5:c8918e47c566 | 433 | #define BS_55AA 510 /* Boot sector signature (2) */ |
mkilivan | 5:c8918e47c566 | 434 | |
mkilivan | 5:c8918e47c566 | 435 | #define DIR_Name 0 /* Short file name (11) */ |
mkilivan | 5:c8918e47c566 | 436 | #define DIR_Attr 11 /* Attribute (1) */ |
mkilivan | 5:c8918e47c566 | 437 | #define DIR_NTres 12 /* NT flag (1) */ |
mkilivan | 5:c8918e47c566 | 438 | #define DIR_CrtTimeTenth 13 /* Created time sub-second (1) */ |
mkilivan | 5:c8918e47c566 | 439 | #define DIR_CrtTime 14 /* Created time (2) */ |
mkilivan | 5:c8918e47c566 | 440 | #define DIR_CrtDate 16 /* Created date (2) */ |
mkilivan | 5:c8918e47c566 | 441 | #define DIR_LstAccDate 18 /* Last accessed date (2) */ |
mkilivan | 5:c8918e47c566 | 442 | #define DIR_FstClusHI 20 /* Higher 16-bit of first cluster (2) */ |
mkilivan | 5:c8918e47c566 | 443 | #define DIR_WrtTime 22 /* Modified time (2) */ |
mkilivan | 5:c8918e47c566 | 444 | #define DIR_WrtDate 24 /* Modified date (2) */ |
mkilivan | 5:c8918e47c566 | 445 | #define DIR_FstClusLO 26 /* Lower 16-bit of first cluster (2) */ |
mkilivan | 5:c8918e47c566 | 446 | #define DIR_FileSize 28 /* File size (4) */ |
mkilivan | 5:c8918e47c566 | 447 | #define LDIR_Ord 0 /* LFN entry order and LLE flag (1) */ |
mkilivan | 5:c8918e47c566 | 448 | #define LDIR_Attr 11 /* LFN attribute (1) */ |
mkilivan | 5:c8918e47c566 | 449 | #define LDIR_Type 12 /* LFN type (1) */ |
mkilivan | 5:c8918e47c566 | 450 | #define LDIR_Chksum 13 /* Sum of corresponding SFN entry */ |
mkilivan | 5:c8918e47c566 | 451 | #define LDIR_FstClusLO 26 /* Filled by zero (0) */ |
mkilivan | 5:c8918e47c566 | 452 | #define SZ_DIR 32 /* Size of a directory entry */ |
mkilivan | 5:c8918e47c566 | 453 | #define LLE 0x40 /* Last long entry flag in LDIR_Ord */ |
mkilivan | 5:c8918e47c566 | 454 | #define DDE 0xE5 /* Deleted directory entry mark in DIR_Name[0] */ |
mkilivan | 5:c8918e47c566 | 455 | #define NDDE 0x05 /* Replacement of the character collides with DDE */ |
mkilivan | 5:c8918e47c566 | 456 | |
mkilivan | 5:c8918e47c566 | 457 | |
mkilivan | 5:c8918e47c566 | 458 | /*------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 459 | /* Module private work area */ |
mkilivan | 5:c8918e47c566 | 460 | /*------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 461 | /* Note that uninitialized variables with static duration are |
mkilivan | 5:c8918e47c566 | 462 | / zeroed/nulled at start-up. If not, the compiler or start-up |
mkilivan | 5:c8918e47c566 | 463 | / routine is out of ANSI-C standard. |
mkilivan | 5:c8918e47c566 | 464 | */ |
mkilivan | 5:c8918e47c566 | 465 | |
mkilivan | 5:c8918e47c566 | 466 | #if _VOLUMES |
mkilivan | 5:c8918e47c566 | 467 | static |
mkilivan | 5:c8918e47c566 | 468 | FATFS *FatFs[_VOLUMES]; /* Pointer to the file system objects (logical drives) */ |
mkilivan | 5:c8918e47c566 | 469 | #else |
mkilivan | 5:c8918e47c566 | 470 | #error Number of volumes must not be 0. |
mkilivan | 5:c8918e47c566 | 471 | #endif |
mkilivan | 5:c8918e47c566 | 472 | |
mkilivan | 5:c8918e47c566 | 473 | static |
mkilivan | 5:c8918e47c566 | 474 | WORD Fsid; /* File system mount ID */ |
mkilivan | 5:c8918e47c566 | 475 | |
mkilivan | 5:c8918e47c566 | 476 | #if _FS_RPATH |
mkilivan | 5:c8918e47c566 | 477 | static |
mkilivan | 5:c8918e47c566 | 478 | BYTE CurrVol; /* Current drive */ |
mkilivan | 5:c8918e47c566 | 479 | #endif |
mkilivan | 5:c8918e47c566 | 480 | |
mkilivan | 5:c8918e47c566 | 481 | #if _FS_LOCK |
mkilivan | 5:c8918e47c566 | 482 | static |
mkilivan | 5:c8918e47c566 | 483 | FILESEM Files[_FS_LOCK]; /* File lock semaphores */ |
mkilivan | 5:c8918e47c566 | 484 | #endif |
mkilivan | 5:c8918e47c566 | 485 | |
mkilivan | 5:c8918e47c566 | 486 | #if _USE_LFN == 0 /* No LFN feature */ |
mkilivan | 5:c8918e47c566 | 487 | #define DEF_NAMEBUF BYTE sfn[12] |
mkilivan | 5:c8918e47c566 | 488 | #define INIT_BUF(dobj) (dobj).fn = sfn |
mkilivan | 5:c8918e47c566 | 489 | #define FREE_BUF() |
mkilivan | 5:c8918e47c566 | 490 | |
mkilivan | 5:c8918e47c566 | 491 | #elif _USE_LFN == 1 /* LFN feature with static working buffer */ |
mkilivan | 5:c8918e47c566 | 492 | static WCHAR LfnBuf[_MAX_LFN+1]; |
mkilivan | 5:c8918e47c566 | 493 | #define DEF_NAMEBUF BYTE sfn[12] |
mkilivan | 5:c8918e47c566 | 494 | #define INIT_BUF(dobj) { (dobj).fn = sfn; (dobj).lfn = LfnBuf; } |
mkilivan | 5:c8918e47c566 | 495 | #define FREE_BUF() |
mkilivan | 5:c8918e47c566 | 496 | |
mkilivan | 5:c8918e47c566 | 497 | #elif _USE_LFN == 2 /* LFN feature with dynamic working buffer on the stack */ |
mkilivan | 5:c8918e47c566 | 498 | #define DEF_NAMEBUF BYTE sfn[12]; WCHAR lbuf[_MAX_LFN+1] |
mkilivan | 5:c8918e47c566 | 499 | #define INIT_BUF(dobj) { (dobj).fn = sfn; (dobj).lfn = lbuf; } |
mkilivan | 5:c8918e47c566 | 500 | #define FREE_BUF() |
mkilivan | 5:c8918e47c566 | 501 | |
mkilivan | 5:c8918e47c566 | 502 | #elif _USE_LFN == 3 /* LFN feature with dynamic working buffer on the heap */ |
mkilivan | 5:c8918e47c566 | 503 | #define DEF_NAMEBUF BYTE sfn[12]; WCHAR *lfn |
mkilivan | 5:c8918e47c566 | 504 | #define INIT_BUF(dobj) { lfn = ff_memalloc((_MAX_LFN + 1) * 2); \ |
mkilivan | 5:c8918e47c566 | 505 | if (!lfn) LEAVE_FF((dobj).fs, FR_NOT_ENOUGH_CORE); \ |
mkilivan | 5:c8918e47c566 | 506 | (dobj).lfn = lfn; (dobj).fn = sfn; } |
mkilivan | 5:c8918e47c566 | 507 | #define FREE_BUF() ff_memfree(lfn) |
mkilivan | 5:c8918e47c566 | 508 | |
mkilivan | 5:c8918e47c566 | 509 | #else |
mkilivan | 5:c8918e47c566 | 510 | #error Wrong LFN configuration. |
mkilivan | 5:c8918e47c566 | 511 | #endif |
mkilivan | 5:c8918e47c566 | 512 | |
mkilivan | 5:c8918e47c566 | 513 | |
mkilivan | 5:c8918e47c566 | 514 | |
mkilivan | 5:c8918e47c566 | 515 | |
mkilivan | 5:c8918e47c566 | 516 | /*-------------------------------------------------------------------------- |
mkilivan | 5:c8918e47c566 | 517 | |
mkilivan | 5:c8918e47c566 | 518 | Module Private Functions |
mkilivan | 5:c8918e47c566 | 519 | |
mkilivan | 5:c8918e47c566 | 520 | ---------------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 521 | |
mkilivan | 5:c8918e47c566 | 522 | |
mkilivan | 5:c8918e47c566 | 523 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 524 | /* String functions */ |
mkilivan | 5:c8918e47c566 | 525 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 526 | |
mkilivan | 5:c8918e47c566 | 527 | /* Copy memory to memory */ |
mkilivan | 5:c8918e47c566 | 528 | static |
mkilivan | 5:c8918e47c566 | 529 | void mem_cpy (void* dst, const void* src, UINT cnt) { |
mkilivan | 5:c8918e47c566 | 530 | BYTE *d = (BYTE*)dst; |
mkilivan | 5:c8918e47c566 | 531 | const BYTE *s = (const BYTE*)src; |
mkilivan | 5:c8918e47c566 | 532 | |
mkilivan | 5:c8918e47c566 | 533 | #if _WORD_ACCESS == 1 |
mkilivan | 5:c8918e47c566 | 534 | while (cnt >= sizeof (int)) { |
mkilivan | 5:c8918e47c566 | 535 | *(int*)d = *(int*)s; |
mkilivan | 5:c8918e47c566 | 536 | d += sizeof (int); s += sizeof (int); |
mkilivan | 5:c8918e47c566 | 537 | cnt -= sizeof (int); |
mkilivan | 5:c8918e47c566 | 538 | } |
mkilivan | 5:c8918e47c566 | 539 | #endif |
mkilivan | 5:c8918e47c566 | 540 | while (cnt--) |
mkilivan | 5:c8918e47c566 | 541 | *d++ = *s++; |
mkilivan | 5:c8918e47c566 | 542 | } |
mkilivan | 5:c8918e47c566 | 543 | |
mkilivan | 5:c8918e47c566 | 544 | /* Fill memory */ |
mkilivan | 5:c8918e47c566 | 545 | static |
mkilivan | 5:c8918e47c566 | 546 | void mem_set (void* dst, int val, UINT cnt) { |
mkilivan | 5:c8918e47c566 | 547 | BYTE *d = (BYTE*)dst; |
mkilivan | 5:c8918e47c566 | 548 | |
mkilivan | 5:c8918e47c566 | 549 | while (cnt--) |
mkilivan | 5:c8918e47c566 | 550 | *d++ = (BYTE)val; |
mkilivan | 5:c8918e47c566 | 551 | } |
mkilivan | 5:c8918e47c566 | 552 | |
mkilivan | 5:c8918e47c566 | 553 | /* Compare memory to memory */ |
mkilivan | 5:c8918e47c566 | 554 | static |
mkilivan | 5:c8918e47c566 | 555 | int mem_cmp (const void* dst, const void* src, UINT cnt) { |
mkilivan | 5:c8918e47c566 | 556 | const BYTE *d = (const BYTE *)dst, *s = (const BYTE *)src; |
mkilivan | 5:c8918e47c566 | 557 | int r = 0; |
mkilivan | 5:c8918e47c566 | 558 | |
mkilivan | 5:c8918e47c566 | 559 | while (cnt-- && (r = *d++ - *s++) == 0) ; |
mkilivan | 5:c8918e47c566 | 560 | return r; |
mkilivan | 5:c8918e47c566 | 561 | } |
mkilivan | 5:c8918e47c566 | 562 | |
mkilivan | 5:c8918e47c566 | 563 | /* Check if chr is contained in the string */ |
mkilivan | 5:c8918e47c566 | 564 | static |
mkilivan | 5:c8918e47c566 | 565 | int chk_chr (const char* str, int chr) { |
mkilivan | 5:c8918e47c566 | 566 | while (*str && *str != chr) str++; |
mkilivan | 5:c8918e47c566 | 567 | return *str; |
mkilivan | 5:c8918e47c566 | 568 | } |
mkilivan | 5:c8918e47c566 | 569 | |
mkilivan | 5:c8918e47c566 | 570 | |
mkilivan | 5:c8918e47c566 | 571 | |
mkilivan | 5:c8918e47c566 | 572 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 573 | /* Request/Release grant to access the volume */ |
mkilivan | 5:c8918e47c566 | 574 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 575 | #if _FS_REENTRANT |
mkilivan | 5:c8918e47c566 | 576 | |
mkilivan | 5:c8918e47c566 | 577 | static |
mkilivan | 5:c8918e47c566 | 578 | int lock_fs ( |
mkilivan | 5:c8918e47c566 | 579 | FATFS *fs /* File system object */ |
mkilivan | 5:c8918e47c566 | 580 | ) |
mkilivan | 5:c8918e47c566 | 581 | { |
mkilivan | 5:c8918e47c566 | 582 | return ff_req_grant(fs->sobj); |
mkilivan | 5:c8918e47c566 | 583 | } |
mkilivan | 5:c8918e47c566 | 584 | |
mkilivan | 5:c8918e47c566 | 585 | |
mkilivan | 5:c8918e47c566 | 586 | static |
mkilivan | 5:c8918e47c566 | 587 | void unlock_fs ( |
mkilivan | 5:c8918e47c566 | 588 | FATFS *fs, /* File system object */ |
mkilivan | 5:c8918e47c566 | 589 | FRESULT res /* Result code to be returned */ |
mkilivan | 5:c8918e47c566 | 590 | ) |
mkilivan | 5:c8918e47c566 | 591 | { |
mkilivan | 5:c8918e47c566 | 592 | if (fs && |
mkilivan | 5:c8918e47c566 | 593 | res != FR_NOT_ENABLED && |
mkilivan | 5:c8918e47c566 | 594 | res != FR_INVALID_DRIVE && |
mkilivan | 5:c8918e47c566 | 595 | res != FR_INVALID_OBJECT && |
mkilivan | 5:c8918e47c566 | 596 | res != FR_TIMEOUT) { |
mkilivan | 5:c8918e47c566 | 597 | ff_rel_grant(fs->sobj); |
mkilivan | 5:c8918e47c566 | 598 | } |
mkilivan | 5:c8918e47c566 | 599 | } |
mkilivan | 5:c8918e47c566 | 600 | #endif |
mkilivan | 5:c8918e47c566 | 601 | |
mkilivan | 5:c8918e47c566 | 602 | |
mkilivan | 5:c8918e47c566 | 603 | |
mkilivan | 5:c8918e47c566 | 604 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 605 | /* File lock control functions */ |
mkilivan | 5:c8918e47c566 | 606 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 607 | #if _FS_LOCK |
mkilivan | 5:c8918e47c566 | 608 | |
mkilivan | 5:c8918e47c566 | 609 | static |
mkilivan | 5:c8918e47c566 | 610 | FRESULT chk_lock ( /* Check if the file can be accessed */ |
mkilivan | 5:c8918e47c566 | 611 | DIR* dj, /* Directory object pointing the file to be checked */ |
mkilivan | 5:c8918e47c566 | 612 | int acc /* Desired access (0:Read, 1:Write, 2:Delete/Rename) */ |
mkilivan | 5:c8918e47c566 | 613 | ) |
mkilivan | 5:c8918e47c566 | 614 | { |
mkilivan | 5:c8918e47c566 | 615 | UINT i, be; |
mkilivan | 5:c8918e47c566 | 616 | |
mkilivan | 5:c8918e47c566 | 617 | /* Search file semaphore table */ |
mkilivan | 5:c8918e47c566 | 618 | for (i = be = 0; i < _FS_LOCK; i++) { |
mkilivan | 5:c8918e47c566 | 619 | if (Files[i].fs) { /* Existing entry */ |
mkilivan | 5:c8918e47c566 | 620 | if (Files[i].fs == dj->fs && /* Check if the file matched with an open file */ |
mkilivan | 5:c8918e47c566 | 621 | Files[i].clu == dj->sclust && |
mkilivan | 5:c8918e47c566 | 622 | Files[i].idx == dj->index) break; |
mkilivan | 5:c8918e47c566 | 623 | } else { /* Blank entry */ |
mkilivan | 5:c8918e47c566 | 624 | be++; |
mkilivan | 5:c8918e47c566 | 625 | } |
mkilivan | 5:c8918e47c566 | 626 | } |
mkilivan | 5:c8918e47c566 | 627 | if (i == _FS_LOCK) /* The file is not opened */ |
mkilivan | 5:c8918e47c566 | 628 | return (be || acc == 2) ? FR_OK : FR_TOO_MANY_OPEN_FILES; /* Is there a blank entry for new file? */ |
mkilivan | 5:c8918e47c566 | 629 | |
mkilivan | 5:c8918e47c566 | 630 | /* The file has been opened. Reject any open against writing file and all write mode open */ |
mkilivan | 5:c8918e47c566 | 631 | return (acc || Files[i].ctr == 0x100) ? FR_LOCKED : FR_OK; |
mkilivan | 5:c8918e47c566 | 632 | } |
mkilivan | 5:c8918e47c566 | 633 | |
mkilivan | 5:c8918e47c566 | 634 | |
mkilivan | 5:c8918e47c566 | 635 | static |
mkilivan | 5:c8918e47c566 | 636 | int enq_lock (void) /* Check if an entry is available for a new file */ |
mkilivan | 5:c8918e47c566 | 637 | { |
mkilivan | 5:c8918e47c566 | 638 | UINT i; |
mkilivan | 5:c8918e47c566 | 639 | |
mkilivan | 5:c8918e47c566 | 640 | for (i = 0; i < _FS_LOCK && Files[i].fs; i++) ; |
mkilivan | 5:c8918e47c566 | 641 | return (i == _FS_LOCK) ? 0 : 1; |
mkilivan | 5:c8918e47c566 | 642 | } |
mkilivan | 5:c8918e47c566 | 643 | |
mkilivan | 5:c8918e47c566 | 644 | |
mkilivan | 5:c8918e47c566 | 645 | static |
mkilivan | 5:c8918e47c566 | 646 | UINT inc_lock ( /* Increment file open counter and returns its index (0:int error) */ |
mkilivan | 5:c8918e47c566 | 647 | DIR* dj, /* Directory object pointing the file to register or increment */ |
mkilivan | 5:c8918e47c566 | 648 | int acc /* Desired access mode (0:Read, !0:Write) */ |
mkilivan | 5:c8918e47c566 | 649 | ) |
mkilivan | 5:c8918e47c566 | 650 | { |
mkilivan | 5:c8918e47c566 | 651 | UINT i; |
mkilivan | 5:c8918e47c566 | 652 | |
mkilivan | 5:c8918e47c566 | 653 | |
mkilivan | 5:c8918e47c566 | 654 | for (i = 0; i < _FS_LOCK; i++) { /* Find the file */ |
mkilivan | 5:c8918e47c566 | 655 | if (Files[i].fs == dj->fs && |
mkilivan | 5:c8918e47c566 | 656 | Files[i].clu == dj->sclust && |
mkilivan | 5:c8918e47c566 | 657 | Files[i].idx == dj->index) break; |
mkilivan | 5:c8918e47c566 | 658 | } |
mkilivan | 5:c8918e47c566 | 659 | |
mkilivan | 5:c8918e47c566 | 660 | if (i == _FS_LOCK) { /* Not opened. Register it as new. */ |
mkilivan | 5:c8918e47c566 | 661 | for (i = 0; i < _FS_LOCK && Files[i].fs; i++) ; |
mkilivan | 5:c8918e47c566 | 662 | if (i == _FS_LOCK) return 0; /* No space to register (int err) */ |
mkilivan | 5:c8918e47c566 | 663 | Files[i].fs = dj->fs; |
mkilivan | 5:c8918e47c566 | 664 | Files[i].clu = dj->sclust; |
mkilivan | 5:c8918e47c566 | 665 | Files[i].idx = dj->index; |
mkilivan | 5:c8918e47c566 | 666 | Files[i].ctr = 0; |
mkilivan | 5:c8918e47c566 | 667 | } |
mkilivan | 5:c8918e47c566 | 668 | |
mkilivan | 5:c8918e47c566 | 669 | if (acc && Files[i].ctr) return 0; /* Access violation (int err) */ |
mkilivan | 5:c8918e47c566 | 670 | |
mkilivan | 5:c8918e47c566 | 671 | Files[i].ctr = acc ? 0x100 : Files[i].ctr + 1; /* Set semaphore value */ |
mkilivan | 5:c8918e47c566 | 672 | |
mkilivan | 5:c8918e47c566 | 673 | return i + 1; |
mkilivan | 5:c8918e47c566 | 674 | } |
mkilivan | 5:c8918e47c566 | 675 | |
mkilivan | 5:c8918e47c566 | 676 | |
mkilivan | 5:c8918e47c566 | 677 | static |
mkilivan | 5:c8918e47c566 | 678 | FRESULT dec_lock ( /* Decrement file open counter */ |
mkilivan | 5:c8918e47c566 | 679 | UINT i /* Semaphore index */ |
mkilivan | 5:c8918e47c566 | 680 | ) |
mkilivan | 5:c8918e47c566 | 681 | { |
mkilivan | 5:c8918e47c566 | 682 | WORD n; |
mkilivan | 5:c8918e47c566 | 683 | FRESULT res; |
mkilivan | 5:c8918e47c566 | 684 | |
mkilivan | 5:c8918e47c566 | 685 | |
mkilivan | 5:c8918e47c566 | 686 | if (--i < _FS_LOCK) { |
mkilivan | 5:c8918e47c566 | 687 | n = Files[i].ctr; |
mkilivan | 5:c8918e47c566 | 688 | if (n == 0x100) n = 0; |
mkilivan | 5:c8918e47c566 | 689 | if (n) n--; |
mkilivan | 5:c8918e47c566 | 690 | Files[i].ctr = n; |
mkilivan | 5:c8918e47c566 | 691 | if (!n) Files[i].fs = 0; |
mkilivan | 5:c8918e47c566 | 692 | res = FR_OK; |
mkilivan | 5:c8918e47c566 | 693 | } else { |
mkilivan | 5:c8918e47c566 | 694 | res = FR_INT_ERR; |
mkilivan | 5:c8918e47c566 | 695 | } |
mkilivan | 5:c8918e47c566 | 696 | return res; |
mkilivan | 5:c8918e47c566 | 697 | } |
mkilivan | 5:c8918e47c566 | 698 | |
mkilivan | 5:c8918e47c566 | 699 | |
mkilivan | 5:c8918e47c566 | 700 | static |
mkilivan | 5:c8918e47c566 | 701 | void clear_lock ( /* Clear lock entries of the volume */ |
mkilivan | 5:c8918e47c566 | 702 | FATFS *fs |
mkilivan | 5:c8918e47c566 | 703 | ) |
mkilivan | 5:c8918e47c566 | 704 | { |
mkilivan | 5:c8918e47c566 | 705 | UINT i; |
mkilivan | 5:c8918e47c566 | 706 | |
mkilivan | 5:c8918e47c566 | 707 | for (i = 0; i < _FS_LOCK; i++) { |
mkilivan | 5:c8918e47c566 | 708 | if (Files[i].fs == fs) Files[i].fs = 0; |
mkilivan | 5:c8918e47c566 | 709 | } |
mkilivan | 5:c8918e47c566 | 710 | } |
mkilivan | 5:c8918e47c566 | 711 | #endif |
mkilivan | 5:c8918e47c566 | 712 | |
mkilivan | 5:c8918e47c566 | 713 | |
mkilivan | 5:c8918e47c566 | 714 | |
mkilivan | 5:c8918e47c566 | 715 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 716 | /* Change window offset */ |
mkilivan | 5:c8918e47c566 | 717 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 718 | |
mkilivan | 5:c8918e47c566 | 719 | static |
mkilivan | 5:c8918e47c566 | 720 | FRESULT move_window ( |
mkilivan | 5:c8918e47c566 | 721 | FATFS *fs, /* File system object */ |
mkilivan | 5:c8918e47c566 | 722 | DWORD sector /* Sector number to make appearance in the fs->win[] */ |
mkilivan | 5:c8918e47c566 | 723 | ) /* Move to zero only writes back dirty window */ |
mkilivan | 5:c8918e47c566 | 724 | { |
mkilivan | 5:c8918e47c566 | 725 | DWORD wsect; |
mkilivan | 5:c8918e47c566 | 726 | |
mkilivan | 5:c8918e47c566 | 727 | |
mkilivan | 5:c8918e47c566 | 728 | wsect = fs->winsect; |
mkilivan | 5:c8918e47c566 | 729 | if (wsect != sector) { /* Changed current window */ |
mkilivan | 5:c8918e47c566 | 730 | #if !_FS_READONLY |
mkilivan | 5:c8918e47c566 | 731 | if (fs->wflag) { /* Write back dirty window if needed */ |
mkilivan | 5:c8918e47c566 | 732 | if (disk_write(fs->drv, fs->win, wsect, 1) != RES_OK) |
mkilivan | 5:c8918e47c566 | 733 | return FR_DISK_ERR; |
mkilivan | 5:c8918e47c566 | 734 | fs->wflag = 0; |
mkilivan | 5:c8918e47c566 | 735 | if (wsect < (fs->fatbase + fs->fsize)) { /* In FAT area */ |
mkilivan | 5:c8918e47c566 | 736 | BYTE nf; |
mkilivan | 5:c8918e47c566 | 737 | for (nf = fs->n_fats; nf > 1; nf--) { /* Reflect the change to all FAT copies */ |
mkilivan | 5:c8918e47c566 | 738 | wsect += fs->fsize; |
mkilivan | 5:c8918e47c566 | 739 | disk_write(fs->drv, fs->win, wsect, 1); |
mkilivan | 5:c8918e47c566 | 740 | } |
mkilivan | 5:c8918e47c566 | 741 | } |
mkilivan | 5:c8918e47c566 | 742 | } |
mkilivan | 5:c8918e47c566 | 743 | #endif |
mkilivan | 5:c8918e47c566 | 744 | if (sector) { |
mkilivan | 5:c8918e47c566 | 745 | if (disk_read(fs->drv, fs->win, sector, 1) != RES_OK) |
mkilivan | 5:c8918e47c566 | 746 | return FR_DISK_ERR; |
mkilivan | 5:c8918e47c566 | 747 | fs->winsect = sector; |
mkilivan | 5:c8918e47c566 | 748 | } |
mkilivan | 5:c8918e47c566 | 749 | } |
mkilivan | 5:c8918e47c566 | 750 | |
mkilivan | 5:c8918e47c566 | 751 | return FR_OK; |
mkilivan | 5:c8918e47c566 | 752 | } |
mkilivan | 5:c8918e47c566 | 753 | |
mkilivan | 5:c8918e47c566 | 754 | |
mkilivan | 5:c8918e47c566 | 755 | |
mkilivan | 5:c8918e47c566 | 756 | |
mkilivan | 5:c8918e47c566 | 757 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 758 | /* Clean-up cached data */ |
mkilivan | 5:c8918e47c566 | 759 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 760 | #if !_FS_READONLY |
mkilivan | 5:c8918e47c566 | 761 | static |
mkilivan | 5:c8918e47c566 | 762 | FRESULT sync ( /* FR_OK: successful, FR_DISK_ERR: failed */ |
mkilivan | 5:c8918e47c566 | 763 | FATFS *fs /* File system object */ |
mkilivan | 5:c8918e47c566 | 764 | ) |
mkilivan | 5:c8918e47c566 | 765 | { |
mkilivan | 5:c8918e47c566 | 766 | FRESULT res; |
mkilivan | 5:c8918e47c566 | 767 | |
mkilivan | 5:c8918e47c566 | 768 | |
mkilivan | 5:c8918e47c566 | 769 | res = move_window(fs, 0); |
mkilivan | 5:c8918e47c566 | 770 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 771 | /* Update FSInfo sector if needed */ |
mkilivan | 5:c8918e47c566 | 772 | if (fs->fs_type == FS_FAT32 && fs->fsi_flag) { |
mkilivan | 5:c8918e47c566 | 773 | fs->winsect = 0; |
mkilivan | 5:c8918e47c566 | 774 | /* Create FSInfo structure */ |
mkilivan | 5:c8918e47c566 | 775 | mem_set(fs->win, 0, 512); |
mkilivan | 5:c8918e47c566 | 776 | ST_WORD(fs->win+BS_55AA, 0xAA55); |
mkilivan | 5:c8918e47c566 | 777 | ST_DWORD(fs->win+FSI_LeadSig, 0x41615252); |
mkilivan | 5:c8918e47c566 | 778 | ST_DWORD(fs->win+FSI_StrucSig, 0x61417272); |
mkilivan | 5:c8918e47c566 | 779 | ST_DWORD(fs->win+FSI_Free_Count, fs->free_clust); |
mkilivan | 5:c8918e47c566 | 780 | ST_DWORD(fs->win+FSI_Nxt_Free, fs->last_clust); |
mkilivan | 5:c8918e47c566 | 781 | /* Write it into the FSInfo sector */ |
mkilivan | 5:c8918e47c566 | 782 | disk_write(fs->drv, fs->win, fs->fsi_sector, 1); |
mkilivan | 5:c8918e47c566 | 783 | fs->fsi_flag = 0; |
mkilivan | 5:c8918e47c566 | 784 | } |
mkilivan | 5:c8918e47c566 | 785 | /* Make sure that no pending write process in the physical drive */ |
mkilivan | 5:c8918e47c566 | 786 | if (disk_ioctl(fs->drv, CTRL_SYNC, 0) != RES_OK) |
mkilivan | 5:c8918e47c566 | 787 | res = FR_DISK_ERR; |
mkilivan | 5:c8918e47c566 | 788 | } |
mkilivan | 5:c8918e47c566 | 789 | |
mkilivan | 5:c8918e47c566 | 790 | return res; |
mkilivan | 5:c8918e47c566 | 791 | } |
mkilivan | 5:c8918e47c566 | 792 | #endif |
mkilivan | 5:c8918e47c566 | 793 | |
mkilivan | 5:c8918e47c566 | 794 | |
mkilivan | 5:c8918e47c566 | 795 | |
mkilivan | 5:c8918e47c566 | 796 | |
mkilivan | 5:c8918e47c566 | 797 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 798 | /* Get sector# from cluster# */ |
mkilivan | 5:c8918e47c566 | 799 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 800 | |
mkilivan | 5:c8918e47c566 | 801 | |
mkilivan | 5:c8918e47c566 | 802 | DWORD clust2sect ( /* !=0: Sector number, 0: Failed - invalid cluster# */ |
mkilivan | 5:c8918e47c566 | 803 | FATFS *fs, /* File system object */ |
mkilivan | 5:c8918e47c566 | 804 | DWORD clst /* Cluster# to be converted */ |
mkilivan | 5:c8918e47c566 | 805 | ) |
mkilivan | 5:c8918e47c566 | 806 | { |
mkilivan | 5:c8918e47c566 | 807 | clst -= 2; |
mkilivan | 5:c8918e47c566 | 808 | if (clst >= (fs->n_fatent - 2)) return 0; /* Invalid cluster# */ |
mkilivan | 5:c8918e47c566 | 809 | return clst * fs->csize + fs->database; |
mkilivan | 5:c8918e47c566 | 810 | } |
mkilivan | 5:c8918e47c566 | 811 | |
mkilivan | 5:c8918e47c566 | 812 | |
mkilivan | 5:c8918e47c566 | 813 | |
mkilivan | 5:c8918e47c566 | 814 | |
mkilivan | 5:c8918e47c566 | 815 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 816 | /* FAT access - Read value of a FAT entry */ |
mkilivan | 5:c8918e47c566 | 817 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 818 | |
mkilivan | 5:c8918e47c566 | 819 | |
mkilivan | 5:c8918e47c566 | 820 | DWORD get_fat ( /* 0xFFFFFFFF:Disk error, 1:Internal error, Else:Cluster status */ |
mkilivan | 5:c8918e47c566 | 821 | FATFS *fs, /* File system object */ |
mkilivan | 5:c8918e47c566 | 822 | DWORD clst /* Cluster# to get the link information */ |
mkilivan | 5:c8918e47c566 | 823 | ) |
mkilivan | 5:c8918e47c566 | 824 | { |
mkilivan | 5:c8918e47c566 | 825 | UINT wc, bc; |
mkilivan | 5:c8918e47c566 | 826 | BYTE *p; |
mkilivan | 5:c8918e47c566 | 827 | |
mkilivan | 5:c8918e47c566 | 828 | |
mkilivan | 5:c8918e47c566 | 829 | if (clst < 2 || clst >= fs->n_fatent) /* Check range */ |
mkilivan | 5:c8918e47c566 | 830 | return 1; |
mkilivan | 5:c8918e47c566 | 831 | |
mkilivan | 5:c8918e47c566 | 832 | switch (fs->fs_type) { |
mkilivan | 5:c8918e47c566 | 833 | case FS_FAT12 : |
mkilivan | 5:c8918e47c566 | 834 | bc = (UINT)clst; bc += bc / 2; |
mkilivan | 5:c8918e47c566 | 835 | if (move_window(fs, fs->fatbase + (bc / SS(fs)))) break; |
mkilivan | 5:c8918e47c566 | 836 | wc = fs->win[bc % SS(fs)]; bc++; |
mkilivan | 5:c8918e47c566 | 837 | if (move_window(fs, fs->fatbase + (bc / SS(fs)))) break; |
mkilivan | 5:c8918e47c566 | 838 | wc |= fs->win[bc % SS(fs)] << 8; |
mkilivan | 5:c8918e47c566 | 839 | return (clst & 1) ? (wc >> 4) : (wc & 0xFFF); |
mkilivan | 5:c8918e47c566 | 840 | |
mkilivan | 5:c8918e47c566 | 841 | case FS_FAT16 : |
mkilivan | 5:c8918e47c566 | 842 | if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 2)))) break; |
mkilivan | 5:c8918e47c566 | 843 | p = &fs->win[clst * 2 % SS(fs)]; |
mkilivan | 5:c8918e47c566 | 844 | return LD_WORD(p); |
mkilivan | 5:c8918e47c566 | 845 | |
mkilivan | 5:c8918e47c566 | 846 | case FS_FAT32 : |
mkilivan | 5:c8918e47c566 | 847 | if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 4)))) break; |
mkilivan | 5:c8918e47c566 | 848 | p = &fs->win[clst * 4 % SS(fs)]; |
mkilivan | 5:c8918e47c566 | 849 | return LD_DWORD(p) & 0x0FFFFFFF; |
mkilivan | 5:c8918e47c566 | 850 | } |
mkilivan | 5:c8918e47c566 | 851 | |
mkilivan | 5:c8918e47c566 | 852 | return 0xFFFFFFFF; /* An error occurred at the disk I/O layer */ |
mkilivan | 5:c8918e47c566 | 853 | } |
mkilivan | 5:c8918e47c566 | 854 | |
mkilivan | 5:c8918e47c566 | 855 | |
mkilivan | 5:c8918e47c566 | 856 | |
mkilivan | 5:c8918e47c566 | 857 | |
mkilivan | 5:c8918e47c566 | 858 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 859 | /* FAT access - Change value of a FAT entry */ |
mkilivan | 5:c8918e47c566 | 860 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 861 | #if !_FS_READONLY |
mkilivan | 5:c8918e47c566 | 862 | |
mkilivan | 5:c8918e47c566 | 863 | FRESULT put_fat ( |
mkilivan | 5:c8918e47c566 | 864 | FATFS *fs, /* File system object */ |
mkilivan | 5:c8918e47c566 | 865 | DWORD clst, /* Cluster# to be changed in range of 2 to fs->n_fatent - 1 */ |
mkilivan | 5:c8918e47c566 | 866 | DWORD val /* New value to mark the cluster */ |
mkilivan | 5:c8918e47c566 | 867 | ) |
mkilivan | 5:c8918e47c566 | 868 | { |
mkilivan | 5:c8918e47c566 | 869 | UINT bc; |
mkilivan | 5:c8918e47c566 | 870 | BYTE *p; |
mkilivan | 5:c8918e47c566 | 871 | FRESULT res; |
mkilivan | 5:c8918e47c566 | 872 | |
mkilivan | 5:c8918e47c566 | 873 | |
mkilivan | 5:c8918e47c566 | 874 | if (clst < 2 || clst >= fs->n_fatent) { /* Check range */ |
mkilivan | 5:c8918e47c566 | 875 | res = FR_INT_ERR; |
mkilivan | 5:c8918e47c566 | 876 | |
mkilivan | 5:c8918e47c566 | 877 | } else { |
mkilivan | 5:c8918e47c566 | 878 | switch (fs->fs_type) { |
mkilivan | 5:c8918e47c566 | 879 | case FS_FAT12 : |
mkilivan | 5:c8918e47c566 | 880 | bc = (UINT)clst; bc += bc / 2; |
mkilivan | 5:c8918e47c566 | 881 | res = move_window(fs, fs->fatbase + (bc / SS(fs))); |
mkilivan | 5:c8918e47c566 | 882 | if (res != FR_OK) break; |
mkilivan | 5:c8918e47c566 | 883 | p = &fs->win[bc % SS(fs)]; |
mkilivan | 5:c8918e47c566 | 884 | *p = (clst & 1) ? ((*p & 0x0F) | ((BYTE)val << 4)) : (BYTE)val; |
mkilivan | 5:c8918e47c566 | 885 | bc++; |
mkilivan | 5:c8918e47c566 | 886 | fs->wflag = 1; |
mkilivan | 5:c8918e47c566 | 887 | res = move_window(fs, fs->fatbase + (bc / SS(fs))); |
mkilivan | 5:c8918e47c566 | 888 | if (res != FR_OK) break; |
mkilivan | 5:c8918e47c566 | 889 | p = &fs->win[bc % SS(fs)]; |
mkilivan | 5:c8918e47c566 | 890 | *p = (clst & 1) ? (BYTE)(val >> 4) : ((*p & 0xF0) | ((BYTE)(val >> 8) & 0x0F)); |
mkilivan | 5:c8918e47c566 | 891 | break; |
mkilivan | 5:c8918e47c566 | 892 | |
mkilivan | 5:c8918e47c566 | 893 | case FS_FAT16 : |
mkilivan | 5:c8918e47c566 | 894 | res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 2))); |
mkilivan | 5:c8918e47c566 | 895 | if (res != FR_OK) break; |
mkilivan | 5:c8918e47c566 | 896 | p = &fs->win[clst * 2 % SS(fs)]; |
mkilivan | 5:c8918e47c566 | 897 | ST_WORD(p, (WORD)val); |
mkilivan | 5:c8918e47c566 | 898 | break; |
mkilivan | 5:c8918e47c566 | 899 | |
mkilivan | 5:c8918e47c566 | 900 | case FS_FAT32 : |
mkilivan | 5:c8918e47c566 | 901 | res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 4))); |
mkilivan | 5:c8918e47c566 | 902 | if (res != FR_OK) break; |
mkilivan | 5:c8918e47c566 | 903 | p = &fs->win[clst * 4 % SS(fs)]; |
mkilivan | 5:c8918e47c566 | 904 | val |= LD_DWORD(p) & 0xF0000000; |
mkilivan | 5:c8918e47c566 | 905 | ST_DWORD(p, val); |
mkilivan | 5:c8918e47c566 | 906 | break; |
mkilivan | 5:c8918e47c566 | 907 | |
mkilivan | 5:c8918e47c566 | 908 | default : |
mkilivan | 5:c8918e47c566 | 909 | res = FR_INT_ERR; |
mkilivan | 5:c8918e47c566 | 910 | } |
mkilivan | 5:c8918e47c566 | 911 | fs->wflag = 1; |
mkilivan | 5:c8918e47c566 | 912 | } |
mkilivan | 5:c8918e47c566 | 913 | |
mkilivan | 5:c8918e47c566 | 914 | return res; |
mkilivan | 5:c8918e47c566 | 915 | } |
mkilivan | 5:c8918e47c566 | 916 | #endif /* !_FS_READONLY */ |
mkilivan | 5:c8918e47c566 | 917 | |
mkilivan | 5:c8918e47c566 | 918 | |
mkilivan | 5:c8918e47c566 | 919 | |
mkilivan | 5:c8918e47c566 | 920 | |
mkilivan | 5:c8918e47c566 | 921 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 922 | /* FAT handling - Remove a cluster chain */ |
mkilivan | 5:c8918e47c566 | 923 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 924 | #if !_FS_READONLY |
mkilivan | 5:c8918e47c566 | 925 | static |
mkilivan | 5:c8918e47c566 | 926 | FRESULT remove_chain ( |
mkilivan | 5:c8918e47c566 | 927 | FATFS *fs, /* File system object */ |
mkilivan | 5:c8918e47c566 | 928 | DWORD clst /* Cluster# to remove a chain from */ |
mkilivan | 5:c8918e47c566 | 929 | ) |
mkilivan | 5:c8918e47c566 | 930 | { |
mkilivan | 5:c8918e47c566 | 931 | FRESULT res; |
mkilivan | 5:c8918e47c566 | 932 | DWORD nxt; |
mkilivan | 5:c8918e47c566 | 933 | #if _USE_ERASE |
mkilivan | 5:c8918e47c566 | 934 | DWORD scl = clst, ecl = clst, rt[2]; |
mkilivan | 5:c8918e47c566 | 935 | #endif |
mkilivan | 5:c8918e47c566 | 936 | |
mkilivan | 5:c8918e47c566 | 937 | if (clst < 2 || clst >= fs->n_fatent) { /* Check range */ |
mkilivan | 5:c8918e47c566 | 938 | res = FR_INT_ERR; |
mkilivan | 5:c8918e47c566 | 939 | |
mkilivan | 5:c8918e47c566 | 940 | } else { |
mkilivan | 5:c8918e47c566 | 941 | res = FR_OK; |
mkilivan | 5:c8918e47c566 | 942 | while (clst < fs->n_fatent) { /* Not a last link? */ |
mkilivan | 5:c8918e47c566 | 943 | nxt = get_fat(fs, clst); /* Get cluster status */ |
mkilivan | 5:c8918e47c566 | 944 | if (nxt == 0) break; /* Empty cluster? */ |
mkilivan | 5:c8918e47c566 | 945 | if (nxt == 1) { res = FR_INT_ERR; break; } /* Internal error? */ |
mkilivan | 5:c8918e47c566 | 946 | if (nxt == 0xFFFFFFFF) { res = FR_DISK_ERR; break; } /* Disk error? */ |
mkilivan | 5:c8918e47c566 | 947 | res = put_fat(fs, clst, 0); /* Mark the cluster "empty" */ |
mkilivan | 5:c8918e47c566 | 948 | if (res != FR_OK) break; |
mkilivan | 5:c8918e47c566 | 949 | if (fs->free_clust != 0xFFFFFFFF) { /* Update FSInfo */ |
mkilivan | 5:c8918e47c566 | 950 | fs->free_clust++; |
mkilivan | 5:c8918e47c566 | 951 | fs->fsi_flag = 1; |
mkilivan | 5:c8918e47c566 | 952 | } |
mkilivan | 5:c8918e47c566 | 953 | #if _USE_ERASE |
mkilivan | 5:c8918e47c566 | 954 | if (ecl + 1 == nxt) { /* Is next cluster contiguous? */ |
mkilivan | 5:c8918e47c566 | 955 | ecl = nxt; |
mkilivan | 5:c8918e47c566 | 956 | } else { /* End of contiguous clusters */ |
mkilivan | 5:c8918e47c566 | 957 | rt[0] = clust2sect(fs, scl); /* Start sector */ |
mkilivan | 5:c8918e47c566 | 958 | rt[1] = clust2sect(fs, ecl) + fs->csize - 1; /* End sector */ |
mkilivan | 5:c8918e47c566 | 959 | disk_ioctl(fs->drv, CTRL_ERASE_SECTOR, rt); /* Erase the block */ |
mkilivan | 5:c8918e47c566 | 960 | scl = ecl = nxt; |
mkilivan | 5:c8918e47c566 | 961 | } |
mkilivan | 5:c8918e47c566 | 962 | #endif |
mkilivan | 5:c8918e47c566 | 963 | clst = nxt; /* Next cluster */ |
mkilivan | 5:c8918e47c566 | 964 | } |
mkilivan | 5:c8918e47c566 | 965 | } |
mkilivan | 5:c8918e47c566 | 966 | |
mkilivan | 5:c8918e47c566 | 967 | return res; |
mkilivan | 5:c8918e47c566 | 968 | } |
mkilivan | 5:c8918e47c566 | 969 | #endif |
mkilivan | 5:c8918e47c566 | 970 | |
mkilivan | 5:c8918e47c566 | 971 | |
mkilivan | 5:c8918e47c566 | 972 | |
mkilivan | 5:c8918e47c566 | 973 | |
mkilivan | 5:c8918e47c566 | 974 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 975 | /* FAT handling - Stretch or Create a cluster chain */ |
mkilivan | 5:c8918e47c566 | 976 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 977 | #if !_FS_READONLY |
mkilivan | 5:c8918e47c566 | 978 | static |
mkilivan | 5:c8918e47c566 | 979 | DWORD create_chain ( /* 0:No free cluster, 1:Internal error, 0xFFFFFFFF:Disk error, >=2:New cluster# */ |
mkilivan | 5:c8918e47c566 | 980 | FATFS *fs, /* File system object */ |
mkilivan | 5:c8918e47c566 | 981 | DWORD clst /* Cluster# to stretch. 0 means create a new chain. */ |
mkilivan | 5:c8918e47c566 | 982 | ) |
mkilivan | 5:c8918e47c566 | 983 | { |
mkilivan | 5:c8918e47c566 | 984 | DWORD cs, ncl, scl; |
mkilivan | 5:c8918e47c566 | 985 | FRESULT res; |
mkilivan | 5:c8918e47c566 | 986 | |
mkilivan | 5:c8918e47c566 | 987 | |
mkilivan | 5:c8918e47c566 | 988 | if (clst == 0) { /* Create a new chain */ |
mkilivan | 5:c8918e47c566 | 989 | scl = fs->last_clust; /* Get suggested start point */ |
mkilivan | 5:c8918e47c566 | 990 | if (!scl || scl >= fs->n_fatent) scl = 1; |
mkilivan | 5:c8918e47c566 | 991 | } |
mkilivan | 5:c8918e47c566 | 992 | else { /* Stretch the current chain */ |
mkilivan | 5:c8918e47c566 | 993 | cs = get_fat(fs, clst); /* Check the cluster status */ |
mkilivan | 5:c8918e47c566 | 994 | if (cs < 2) return 1; /* It is an invalid cluster */ |
mkilivan | 5:c8918e47c566 | 995 | if (cs < fs->n_fatent) return cs; /* It is already followed by next cluster */ |
mkilivan | 5:c8918e47c566 | 996 | scl = clst; |
mkilivan | 5:c8918e47c566 | 997 | } |
mkilivan | 5:c8918e47c566 | 998 | |
mkilivan | 5:c8918e47c566 | 999 | ncl = scl; /* Start cluster */ |
mkilivan | 5:c8918e47c566 | 1000 | for (;;) { |
mkilivan | 5:c8918e47c566 | 1001 | ncl++; /* Next cluster */ |
mkilivan | 5:c8918e47c566 | 1002 | if (ncl >= fs->n_fatent) { /* Wrap around */ |
mkilivan | 5:c8918e47c566 | 1003 | ncl = 2; |
mkilivan | 5:c8918e47c566 | 1004 | if (ncl > scl) return 0; /* No free cluster */ |
mkilivan | 5:c8918e47c566 | 1005 | } |
mkilivan | 5:c8918e47c566 | 1006 | cs = get_fat(fs, ncl); /* Get the cluster status */ |
mkilivan | 5:c8918e47c566 | 1007 | if (cs == 0) break; /* Found a free cluster */ |
mkilivan | 5:c8918e47c566 | 1008 | if (cs == 0xFFFFFFFF || cs == 1)/* An error occurred */ |
mkilivan | 5:c8918e47c566 | 1009 | return cs; |
mkilivan | 5:c8918e47c566 | 1010 | if (ncl == scl) return 0; /* No free cluster */ |
mkilivan | 5:c8918e47c566 | 1011 | } |
mkilivan | 5:c8918e47c566 | 1012 | |
mkilivan | 5:c8918e47c566 | 1013 | res = put_fat(fs, ncl, 0x0FFFFFFF); /* Mark the new cluster "last link" */ |
mkilivan | 5:c8918e47c566 | 1014 | if (res == FR_OK && clst != 0) { |
mkilivan | 5:c8918e47c566 | 1015 | res = put_fat(fs, clst, ncl); /* Link it to the previous one if needed */ |
mkilivan | 5:c8918e47c566 | 1016 | } |
mkilivan | 5:c8918e47c566 | 1017 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 1018 | fs->last_clust = ncl; /* Update FSINFO */ |
mkilivan | 5:c8918e47c566 | 1019 | if (fs->free_clust != 0xFFFFFFFF) { |
mkilivan | 5:c8918e47c566 | 1020 | fs->free_clust--; |
mkilivan | 5:c8918e47c566 | 1021 | fs->fsi_flag = 1; |
mkilivan | 5:c8918e47c566 | 1022 | } |
mkilivan | 5:c8918e47c566 | 1023 | } else { |
mkilivan | 5:c8918e47c566 | 1024 | ncl = (res == FR_DISK_ERR) ? 0xFFFFFFFF : 1; |
mkilivan | 5:c8918e47c566 | 1025 | } |
mkilivan | 5:c8918e47c566 | 1026 | |
mkilivan | 5:c8918e47c566 | 1027 | return ncl; /* Return new cluster number or error code */ |
mkilivan | 5:c8918e47c566 | 1028 | } |
mkilivan | 5:c8918e47c566 | 1029 | #endif /* !_FS_READONLY */ |
mkilivan | 5:c8918e47c566 | 1030 | |
mkilivan | 5:c8918e47c566 | 1031 | |
mkilivan | 5:c8918e47c566 | 1032 | |
mkilivan | 5:c8918e47c566 | 1033 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 1034 | /* FAT handling - Convert offset into cluster with link map table */ |
mkilivan | 5:c8918e47c566 | 1035 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 1036 | |
mkilivan | 5:c8918e47c566 | 1037 | #if _USE_FASTSEEK |
mkilivan | 5:c8918e47c566 | 1038 | static |
mkilivan | 5:c8918e47c566 | 1039 | DWORD clmt_clust ( /* <2:Error, >=2:Cluster number */ |
mkilivan | 5:c8918e47c566 | 1040 | FIL* fp, /* Pointer to the file object */ |
mkilivan | 5:c8918e47c566 | 1041 | DWORD ofs /* File offset to be converted to cluster# */ |
mkilivan | 5:c8918e47c566 | 1042 | ) |
mkilivan | 5:c8918e47c566 | 1043 | { |
mkilivan | 5:c8918e47c566 | 1044 | DWORD cl, ncl, *tbl; |
mkilivan | 5:c8918e47c566 | 1045 | |
mkilivan | 5:c8918e47c566 | 1046 | |
mkilivan | 5:c8918e47c566 | 1047 | tbl = fp->cltbl + 1; /* Top of CLMT */ |
mkilivan | 5:c8918e47c566 | 1048 | cl = ofs / SS(fp->fs) / fp->fs->csize; /* Cluster order from top of the file */ |
mkilivan | 5:c8918e47c566 | 1049 | for (;;) { |
mkilivan | 5:c8918e47c566 | 1050 | ncl = *tbl++; /* Number of cluters in the fragment */ |
mkilivan | 5:c8918e47c566 | 1051 | if (!ncl) return 0; /* End of table? (error) */ |
mkilivan | 5:c8918e47c566 | 1052 | if (cl < ncl) break; /* In this fragment? */ |
mkilivan | 5:c8918e47c566 | 1053 | cl -= ncl; tbl++; /* Next fragment */ |
mkilivan | 5:c8918e47c566 | 1054 | } |
mkilivan | 5:c8918e47c566 | 1055 | return cl + *tbl; /* Return the cluster number */ |
mkilivan | 5:c8918e47c566 | 1056 | } |
mkilivan | 5:c8918e47c566 | 1057 | #endif /* _USE_FASTSEEK */ |
mkilivan | 5:c8918e47c566 | 1058 | |
mkilivan | 5:c8918e47c566 | 1059 | |
mkilivan | 5:c8918e47c566 | 1060 | |
mkilivan | 5:c8918e47c566 | 1061 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 1062 | /* Directory handling - Set directory index */ |
mkilivan | 5:c8918e47c566 | 1063 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 1064 | |
mkilivan | 5:c8918e47c566 | 1065 | static |
mkilivan | 5:c8918e47c566 | 1066 | FRESULT dir_sdi ( |
mkilivan | 5:c8918e47c566 | 1067 | FATFS_DIR *dj, /* Pointer to directory object */ |
mkilivan | 5:c8918e47c566 | 1068 | WORD idx /* Index of directory table */ |
mkilivan | 5:c8918e47c566 | 1069 | ) |
mkilivan | 5:c8918e47c566 | 1070 | { |
mkilivan | 5:c8918e47c566 | 1071 | DWORD clst; |
mkilivan | 5:c8918e47c566 | 1072 | WORD ic; |
mkilivan | 5:c8918e47c566 | 1073 | |
mkilivan | 5:c8918e47c566 | 1074 | |
mkilivan | 5:c8918e47c566 | 1075 | dj->index = idx; |
mkilivan | 5:c8918e47c566 | 1076 | clst = dj->sclust; |
mkilivan | 5:c8918e47c566 | 1077 | if (clst == 1 || clst >= dj->fs->n_fatent) /* Check start cluster range */ |
mkilivan | 5:c8918e47c566 | 1078 | return FR_INT_ERR; |
mkilivan | 5:c8918e47c566 | 1079 | if (!clst && dj->fs->fs_type == FS_FAT32) /* Replace cluster# 0 with root cluster# if in FAT32 */ |
mkilivan | 5:c8918e47c566 | 1080 | clst = dj->fs->dirbase; |
mkilivan | 5:c8918e47c566 | 1081 | |
mkilivan | 5:c8918e47c566 | 1082 | if (clst == 0) { /* Static table (root-dir in FAT12/16) */ |
mkilivan | 5:c8918e47c566 | 1083 | dj->clust = clst; |
mkilivan | 5:c8918e47c566 | 1084 | if (idx >= dj->fs->n_rootdir) /* Index is out of range */ |
mkilivan | 5:c8918e47c566 | 1085 | return FR_INT_ERR; |
mkilivan | 5:c8918e47c566 | 1086 | dj->sect = dj->fs->dirbase + idx / (SS(dj->fs) / SZ_DIR); /* Sector# */ |
mkilivan | 5:c8918e47c566 | 1087 | } |
mkilivan | 5:c8918e47c566 | 1088 | else { /* Dynamic table (sub-dirs or root-dir in FAT32) */ |
mkilivan | 5:c8918e47c566 | 1089 | ic = SS(dj->fs) / SZ_DIR * dj->fs->csize; /* Entries per cluster */ |
mkilivan | 5:c8918e47c566 | 1090 | while (idx >= ic) { /* Follow cluster chain */ |
mkilivan | 5:c8918e47c566 | 1091 | clst = get_fat(dj->fs, clst); /* Get next cluster */ |
mkilivan | 5:c8918e47c566 | 1092 | if (clst == 0xFFFFFFFF) return FR_DISK_ERR; /* Disk error */ |
mkilivan | 5:c8918e47c566 | 1093 | if (clst < 2 || clst >= dj->fs->n_fatent) /* Reached to end of table or int error */ |
mkilivan | 5:c8918e47c566 | 1094 | return FR_INT_ERR; |
mkilivan | 5:c8918e47c566 | 1095 | idx -= ic; |
mkilivan | 5:c8918e47c566 | 1096 | } |
mkilivan | 5:c8918e47c566 | 1097 | dj->clust = clst; |
mkilivan | 5:c8918e47c566 | 1098 | dj->sect = clust2sect(dj->fs, clst) + idx / (SS(dj->fs) / SZ_DIR); /* Sector# */ |
mkilivan | 5:c8918e47c566 | 1099 | } |
mkilivan | 5:c8918e47c566 | 1100 | |
mkilivan | 5:c8918e47c566 | 1101 | dj->dir = dj->fs->win + (idx % (SS(dj->fs) / SZ_DIR)) * SZ_DIR; /* Ptr to the entry in the sector */ |
mkilivan | 5:c8918e47c566 | 1102 | |
mkilivan | 5:c8918e47c566 | 1103 | return FR_OK; /* Seek succeeded */ |
mkilivan | 5:c8918e47c566 | 1104 | } |
mkilivan | 5:c8918e47c566 | 1105 | |
mkilivan | 5:c8918e47c566 | 1106 | |
mkilivan | 5:c8918e47c566 | 1107 | |
mkilivan | 5:c8918e47c566 | 1108 | |
mkilivan | 5:c8918e47c566 | 1109 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 1110 | /* Directory handling - Move directory table index next */ |
mkilivan | 5:c8918e47c566 | 1111 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 1112 | |
mkilivan | 5:c8918e47c566 | 1113 | static |
mkilivan | 5:c8918e47c566 | 1114 | FRESULT dir_next ( /* FR_OK:Succeeded, FR_NO_FILE:End of table, FR_DENIED:EOT and could not stretch */ |
mkilivan | 5:c8918e47c566 | 1115 | FATFS_DIR *dj, /* Pointer to directory object */ |
mkilivan | 5:c8918e47c566 | 1116 | int stretch /* 0: Do not stretch table, 1: Stretch table if needed */ |
mkilivan | 5:c8918e47c566 | 1117 | ) |
mkilivan | 5:c8918e47c566 | 1118 | { |
mkilivan | 5:c8918e47c566 | 1119 | DWORD clst; |
mkilivan | 5:c8918e47c566 | 1120 | WORD i; |
mkilivan | 5:c8918e47c566 | 1121 | |
mkilivan | 5:c8918e47c566 | 1122 | |
mkilivan | 5:c8918e47c566 | 1123 | stretch = stretch; /* To suppress warning on read-only cfg. */ |
mkilivan | 5:c8918e47c566 | 1124 | i = dj->index + 1; |
mkilivan | 5:c8918e47c566 | 1125 | if (!i || !dj->sect) /* Report EOT when index has reached 65535 */ |
mkilivan | 5:c8918e47c566 | 1126 | return FR_NO_FILE; |
mkilivan | 5:c8918e47c566 | 1127 | |
mkilivan | 5:c8918e47c566 | 1128 | if (!(i % (SS(dj->fs) / SZ_DIR))) { /* Sector changed? */ |
mkilivan | 5:c8918e47c566 | 1129 | dj->sect++; /* Next sector */ |
mkilivan | 5:c8918e47c566 | 1130 | |
mkilivan | 5:c8918e47c566 | 1131 | if (dj->clust == 0) { /* Static table */ |
mkilivan | 5:c8918e47c566 | 1132 | if (i >= dj->fs->n_rootdir) /* Report EOT when end of table */ |
mkilivan | 5:c8918e47c566 | 1133 | return FR_NO_FILE; |
mkilivan | 5:c8918e47c566 | 1134 | } |
mkilivan | 5:c8918e47c566 | 1135 | else { /* Dynamic table */ |
mkilivan | 5:c8918e47c566 | 1136 | if (((i / (SS(dj->fs) / SZ_DIR)) & (dj->fs->csize - 1)) == 0) { /* Cluster changed? */ |
mkilivan | 5:c8918e47c566 | 1137 | clst = get_fat(dj->fs, dj->clust); /* Get next cluster */ |
mkilivan | 5:c8918e47c566 | 1138 | if (clst <= 1) return FR_INT_ERR; |
mkilivan | 5:c8918e47c566 | 1139 | if (clst == 0xFFFFFFFF) return FR_DISK_ERR; |
mkilivan | 5:c8918e47c566 | 1140 | if (clst >= dj->fs->n_fatent) { /* When it reached end of dynamic table */ |
mkilivan | 5:c8918e47c566 | 1141 | #if !_FS_READONLY |
mkilivan | 5:c8918e47c566 | 1142 | BYTE c; |
mkilivan | 5:c8918e47c566 | 1143 | if (!stretch) return FR_NO_FILE; /* When do not stretch, report EOT */ |
mkilivan | 5:c8918e47c566 | 1144 | clst = create_chain(dj->fs, dj->clust); /* Stretch cluster chain */ |
mkilivan | 5:c8918e47c566 | 1145 | if (clst == 0) return FR_DENIED; /* No free cluster */ |
mkilivan | 5:c8918e47c566 | 1146 | if (clst == 1) return FR_INT_ERR; |
mkilivan | 5:c8918e47c566 | 1147 | if (clst == 0xFFFFFFFF) return FR_DISK_ERR; |
mkilivan | 5:c8918e47c566 | 1148 | /* Clean-up stretched table */ |
mkilivan | 5:c8918e47c566 | 1149 | if (move_window(dj->fs, 0)) return FR_DISK_ERR; /* Flush active window */ |
mkilivan | 5:c8918e47c566 | 1150 | mem_set(dj->fs->win, 0, SS(dj->fs)); /* Clear window buffer */ |
mkilivan | 5:c8918e47c566 | 1151 | dj->fs->winsect = clust2sect(dj->fs, clst); /* Cluster start sector */ |
mkilivan | 5:c8918e47c566 | 1152 | for (c = 0; c < dj->fs->csize; c++) { /* Fill the new cluster with 0 */ |
mkilivan | 5:c8918e47c566 | 1153 | dj->fs->wflag = 1; |
mkilivan | 5:c8918e47c566 | 1154 | if (move_window(dj->fs, 0)) return FR_DISK_ERR; |
mkilivan | 5:c8918e47c566 | 1155 | dj->fs->winsect++; |
mkilivan | 5:c8918e47c566 | 1156 | } |
mkilivan | 5:c8918e47c566 | 1157 | dj->fs->winsect -= c; /* Rewind window address */ |
mkilivan | 5:c8918e47c566 | 1158 | #else |
mkilivan | 5:c8918e47c566 | 1159 | return FR_NO_FILE; /* Report EOT */ |
mkilivan | 5:c8918e47c566 | 1160 | #endif |
mkilivan | 5:c8918e47c566 | 1161 | } |
mkilivan | 5:c8918e47c566 | 1162 | dj->clust = clst; /* Initialize data for new cluster */ |
mkilivan | 5:c8918e47c566 | 1163 | dj->sect = clust2sect(dj->fs, clst); |
mkilivan | 5:c8918e47c566 | 1164 | } |
mkilivan | 5:c8918e47c566 | 1165 | } |
mkilivan | 5:c8918e47c566 | 1166 | } |
mkilivan | 5:c8918e47c566 | 1167 | |
mkilivan | 5:c8918e47c566 | 1168 | dj->index = i; |
mkilivan | 5:c8918e47c566 | 1169 | dj->dir = dj->fs->win + (i % (SS(dj->fs) / SZ_DIR)) * SZ_DIR; |
mkilivan | 5:c8918e47c566 | 1170 | |
mkilivan | 5:c8918e47c566 | 1171 | return FR_OK; |
mkilivan | 5:c8918e47c566 | 1172 | } |
mkilivan | 5:c8918e47c566 | 1173 | |
mkilivan | 5:c8918e47c566 | 1174 | |
mkilivan | 5:c8918e47c566 | 1175 | |
mkilivan | 5:c8918e47c566 | 1176 | |
mkilivan | 5:c8918e47c566 | 1177 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 1178 | /* Directory handling - Load/Store start cluster number */ |
mkilivan | 5:c8918e47c566 | 1179 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 1180 | |
mkilivan | 5:c8918e47c566 | 1181 | static |
mkilivan | 5:c8918e47c566 | 1182 | DWORD ld_clust ( |
mkilivan | 5:c8918e47c566 | 1183 | FATFS *fs, /* Pointer to the fs object */ |
mkilivan | 5:c8918e47c566 | 1184 | BYTE *dir /* Pointer to the directory entry */ |
mkilivan | 5:c8918e47c566 | 1185 | ) |
mkilivan | 5:c8918e47c566 | 1186 | { |
mkilivan | 5:c8918e47c566 | 1187 | DWORD cl; |
mkilivan | 5:c8918e47c566 | 1188 | |
mkilivan | 5:c8918e47c566 | 1189 | cl = LD_WORD(dir+DIR_FstClusLO); |
mkilivan | 5:c8918e47c566 | 1190 | if (fs->fs_type == FS_FAT32) |
mkilivan | 5:c8918e47c566 | 1191 | cl |= (DWORD)LD_WORD(dir+DIR_FstClusHI) << 16; |
mkilivan | 5:c8918e47c566 | 1192 | |
mkilivan | 5:c8918e47c566 | 1193 | return cl; |
mkilivan | 5:c8918e47c566 | 1194 | } |
mkilivan | 5:c8918e47c566 | 1195 | |
mkilivan | 5:c8918e47c566 | 1196 | |
mkilivan | 5:c8918e47c566 | 1197 | #if !_FS_READONLY |
mkilivan | 5:c8918e47c566 | 1198 | static |
mkilivan | 5:c8918e47c566 | 1199 | void st_clust ( |
mkilivan | 5:c8918e47c566 | 1200 | BYTE *dir, /* Pointer to the directory entry */ |
mkilivan | 5:c8918e47c566 | 1201 | DWORD cl /* Value to be set */ |
mkilivan | 5:c8918e47c566 | 1202 | ) |
mkilivan | 5:c8918e47c566 | 1203 | { |
mkilivan | 5:c8918e47c566 | 1204 | ST_WORD(dir+DIR_FstClusLO, cl); |
mkilivan | 5:c8918e47c566 | 1205 | ST_WORD(dir+DIR_FstClusHI, cl >> 16); |
mkilivan | 5:c8918e47c566 | 1206 | } |
mkilivan | 5:c8918e47c566 | 1207 | #endif |
mkilivan | 5:c8918e47c566 | 1208 | |
mkilivan | 5:c8918e47c566 | 1209 | |
mkilivan | 5:c8918e47c566 | 1210 | |
mkilivan | 5:c8918e47c566 | 1211 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 1212 | /* LFN handling - Test/Pick/Fit an LFN segment from/to directory entry */ |
mkilivan | 5:c8918e47c566 | 1213 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 1214 | #if _USE_LFN |
mkilivan | 5:c8918e47c566 | 1215 | static |
mkilivan | 5:c8918e47c566 | 1216 | const BYTE LfnOfs[] = {1,3,5,7,9,14,16,18,20,22,24,28,30}; /* Offset of LFN chars in the directory entry */ |
mkilivan | 5:c8918e47c566 | 1217 | |
mkilivan | 5:c8918e47c566 | 1218 | |
mkilivan | 5:c8918e47c566 | 1219 | static |
mkilivan | 5:c8918e47c566 | 1220 | int cmp_lfn ( /* 1:Matched, 0:Not matched */ |
mkilivan | 5:c8918e47c566 | 1221 | WCHAR *lfnbuf, /* Pointer to the LFN to be compared */ |
mkilivan | 5:c8918e47c566 | 1222 | BYTE *dir /* Pointer to the directory entry containing a part of LFN */ |
mkilivan | 5:c8918e47c566 | 1223 | ) |
mkilivan | 5:c8918e47c566 | 1224 | { |
mkilivan | 5:c8918e47c566 | 1225 | UINT i, s; |
mkilivan | 5:c8918e47c566 | 1226 | WCHAR wc, uc; |
mkilivan | 5:c8918e47c566 | 1227 | |
mkilivan | 5:c8918e47c566 | 1228 | |
mkilivan | 5:c8918e47c566 | 1229 | i = ((dir[LDIR_Ord] & ~LLE) - 1) * 13; /* Get offset in the LFN buffer */ |
mkilivan | 5:c8918e47c566 | 1230 | s = 0; wc = 1; |
mkilivan | 5:c8918e47c566 | 1231 | do { |
mkilivan | 5:c8918e47c566 | 1232 | uc = LD_WORD(dir+LfnOfs[s]); /* Pick an LFN character from the entry */ |
mkilivan | 5:c8918e47c566 | 1233 | if (wc) { /* Last char has not been processed */ |
mkilivan | 5:c8918e47c566 | 1234 | wc = ff_wtoupper(uc); /* Convert it to upper case */ |
mkilivan | 5:c8918e47c566 | 1235 | if (i >= _MAX_LFN || wc != ff_wtoupper(lfnbuf[i++])) /* Compare it */ |
mkilivan | 5:c8918e47c566 | 1236 | return 0; /* Not matched */ |
mkilivan | 5:c8918e47c566 | 1237 | } else { |
mkilivan | 5:c8918e47c566 | 1238 | if (uc != 0xFFFF) return 0; /* Check filler */ |
mkilivan | 5:c8918e47c566 | 1239 | } |
mkilivan | 5:c8918e47c566 | 1240 | } while (++s < 13); /* Repeat until all chars in the entry are checked */ |
mkilivan | 5:c8918e47c566 | 1241 | |
mkilivan | 5:c8918e47c566 | 1242 | if ((dir[LDIR_Ord] & LLE) && wc && lfnbuf[i]) /* Last segment matched but different length */ |
mkilivan | 5:c8918e47c566 | 1243 | return 0; |
mkilivan | 5:c8918e47c566 | 1244 | |
mkilivan | 5:c8918e47c566 | 1245 | return 1; /* The part of LFN matched */ |
mkilivan | 5:c8918e47c566 | 1246 | } |
mkilivan | 5:c8918e47c566 | 1247 | |
mkilivan | 5:c8918e47c566 | 1248 | |
mkilivan | 5:c8918e47c566 | 1249 | |
mkilivan | 5:c8918e47c566 | 1250 | static |
mkilivan | 5:c8918e47c566 | 1251 | int pick_lfn ( /* 1:Succeeded, 0:Buffer overflow */ |
mkilivan | 5:c8918e47c566 | 1252 | WCHAR *lfnbuf, /* Pointer to the Unicode-LFN buffer */ |
mkilivan | 5:c8918e47c566 | 1253 | BYTE *dir /* Pointer to the directory entry */ |
mkilivan | 5:c8918e47c566 | 1254 | ) |
mkilivan | 5:c8918e47c566 | 1255 | { |
mkilivan | 5:c8918e47c566 | 1256 | UINT i, s; |
mkilivan | 5:c8918e47c566 | 1257 | WCHAR wc, uc; |
mkilivan | 5:c8918e47c566 | 1258 | |
mkilivan | 5:c8918e47c566 | 1259 | |
mkilivan | 5:c8918e47c566 | 1260 | i = ((dir[LDIR_Ord] & 0x3F) - 1) * 13; /* Offset in the LFN buffer */ |
mkilivan | 5:c8918e47c566 | 1261 | |
mkilivan | 5:c8918e47c566 | 1262 | s = 0; wc = 1; |
mkilivan | 5:c8918e47c566 | 1263 | do { |
mkilivan | 5:c8918e47c566 | 1264 | uc = LD_WORD(dir+LfnOfs[s]); /* Pick an LFN character from the entry */ |
mkilivan | 5:c8918e47c566 | 1265 | if (wc) { /* Last char has not been processed */ |
mkilivan | 5:c8918e47c566 | 1266 | if (i >= _MAX_LFN) return 0; /* Buffer overflow? */ |
mkilivan | 5:c8918e47c566 | 1267 | lfnbuf[i++] = wc = uc; /* Store it */ |
mkilivan | 5:c8918e47c566 | 1268 | } else { |
mkilivan | 5:c8918e47c566 | 1269 | if (uc != 0xFFFF) return 0; /* Check filler */ |
mkilivan | 5:c8918e47c566 | 1270 | } |
mkilivan | 5:c8918e47c566 | 1271 | } while (++s < 13); /* Read all character in the entry */ |
mkilivan | 5:c8918e47c566 | 1272 | |
mkilivan | 5:c8918e47c566 | 1273 | if (dir[LDIR_Ord] & LLE) { /* Put terminator if it is the last LFN part */ |
mkilivan | 5:c8918e47c566 | 1274 | if (i >= _MAX_LFN) return 0; /* Buffer overflow? */ |
mkilivan | 5:c8918e47c566 | 1275 | lfnbuf[i] = 0; |
mkilivan | 5:c8918e47c566 | 1276 | } |
mkilivan | 5:c8918e47c566 | 1277 | |
mkilivan | 5:c8918e47c566 | 1278 | return 1; |
mkilivan | 5:c8918e47c566 | 1279 | } |
mkilivan | 5:c8918e47c566 | 1280 | |
mkilivan | 5:c8918e47c566 | 1281 | |
mkilivan | 5:c8918e47c566 | 1282 | #if !_FS_READONLY |
mkilivan | 5:c8918e47c566 | 1283 | static |
mkilivan | 5:c8918e47c566 | 1284 | void fit_lfn ( |
mkilivan | 5:c8918e47c566 | 1285 | const WCHAR *lfnbuf, /* Pointer to the LFN buffer */ |
mkilivan | 5:c8918e47c566 | 1286 | BYTE *dir, /* Pointer to the directory entry */ |
mkilivan | 5:c8918e47c566 | 1287 | BYTE ord, /* LFN order (1-20) */ |
mkilivan | 5:c8918e47c566 | 1288 | BYTE sum /* SFN sum */ |
mkilivan | 5:c8918e47c566 | 1289 | ) |
mkilivan | 5:c8918e47c566 | 1290 | { |
mkilivan | 5:c8918e47c566 | 1291 | UINT i, s; |
mkilivan | 5:c8918e47c566 | 1292 | WCHAR wc; |
mkilivan | 5:c8918e47c566 | 1293 | |
mkilivan | 5:c8918e47c566 | 1294 | |
mkilivan | 5:c8918e47c566 | 1295 | dir[LDIR_Chksum] = sum; /* Set check sum */ |
mkilivan | 5:c8918e47c566 | 1296 | dir[LDIR_Attr] = AM_LFN; /* Set attribute. LFN entry */ |
mkilivan | 5:c8918e47c566 | 1297 | dir[LDIR_Type] = 0; |
mkilivan | 5:c8918e47c566 | 1298 | ST_WORD(dir+LDIR_FstClusLO, 0); |
mkilivan | 5:c8918e47c566 | 1299 | |
mkilivan | 5:c8918e47c566 | 1300 | i = (ord - 1) * 13; /* Get offset in the LFN buffer */ |
mkilivan | 5:c8918e47c566 | 1301 | s = wc = 0; |
mkilivan | 5:c8918e47c566 | 1302 | do { |
mkilivan | 5:c8918e47c566 | 1303 | if (wc != 0xFFFF) wc = lfnbuf[i++]; /* Get an effective char */ |
mkilivan | 5:c8918e47c566 | 1304 | ST_WORD(dir+LfnOfs[s], wc); /* Put it */ |
mkilivan | 5:c8918e47c566 | 1305 | if (!wc) wc = 0xFFFF; /* Padding chars following last char */ |
mkilivan | 5:c8918e47c566 | 1306 | } while (++s < 13); |
mkilivan | 5:c8918e47c566 | 1307 | if (wc == 0xFFFF || !lfnbuf[i]) ord |= LLE; /* Bottom LFN part is the start of LFN sequence */ |
mkilivan | 5:c8918e47c566 | 1308 | dir[LDIR_Ord] = ord; /* Set the LFN order */ |
mkilivan | 5:c8918e47c566 | 1309 | } |
mkilivan | 5:c8918e47c566 | 1310 | |
mkilivan | 5:c8918e47c566 | 1311 | #endif |
mkilivan | 5:c8918e47c566 | 1312 | #endif |
mkilivan | 5:c8918e47c566 | 1313 | |
mkilivan | 5:c8918e47c566 | 1314 | |
mkilivan | 5:c8918e47c566 | 1315 | |
mkilivan | 5:c8918e47c566 | 1316 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 1317 | /* Create numbered name */ |
mkilivan | 5:c8918e47c566 | 1318 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 1319 | #if _USE_LFN |
mkilivan | 5:c8918e47c566 | 1320 | void gen_numname ( |
mkilivan | 5:c8918e47c566 | 1321 | BYTE *dst, /* Pointer to generated SFN */ |
mkilivan | 5:c8918e47c566 | 1322 | const BYTE *src, /* Pointer to source SFN to be modified */ |
mkilivan | 5:c8918e47c566 | 1323 | const WCHAR *lfn, /* Pointer to LFN */ |
mkilivan | 5:c8918e47c566 | 1324 | WORD seq /* Sequence number */ |
mkilivan | 5:c8918e47c566 | 1325 | ) |
mkilivan | 5:c8918e47c566 | 1326 | { |
mkilivan | 5:c8918e47c566 | 1327 | BYTE ns[8], c; |
mkilivan | 5:c8918e47c566 | 1328 | UINT i, j; |
mkilivan | 5:c8918e47c566 | 1329 | |
mkilivan | 5:c8918e47c566 | 1330 | |
mkilivan | 5:c8918e47c566 | 1331 | mem_cpy(dst, src, 11); |
mkilivan | 5:c8918e47c566 | 1332 | |
mkilivan | 5:c8918e47c566 | 1333 | if (seq > 5) { /* On many collisions, generate a hash number instead of sequential number */ |
mkilivan | 5:c8918e47c566 | 1334 | do seq = (seq >> 1) + (seq << 15) + (WORD)*lfn++; while (*lfn); |
mkilivan | 5:c8918e47c566 | 1335 | } |
mkilivan | 5:c8918e47c566 | 1336 | |
mkilivan | 5:c8918e47c566 | 1337 | /* itoa (hexdecimal) */ |
mkilivan | 5:c8918e47c566 | 1338 | i = 7; |
mkilivan | 5:c8918e47c566 | 1339 | do { |
mkilivan | 5:c8918e47c566 | 1340 | c = (seq % 16) + '0'; |
mkilivan | 5:c8918e47c566 | 1341 | if (c > '9') c += 7; |
mkilivan | 5:c8918e47c566 | 1342 | ns[i--] = c; |
mkilivan | 5:c8918e47c566 | 1343 | seq /= 16; |
mkilivan | 5:c8918e47c566 | 1344 | } while (seq); |
mkilivan | 5:c8918e47c566 | 1345 | ns[i] = '~'; |
mkilivan | 5:c8918e47c566 | 1346 | |
mkilivan | 5:c8918e47c566 | 1347 | /* Append the number */ |
mkilivan | 5:c8918e47c566 | 1348 | for (j = 0; j < i && dst[j] != ' '; j++) { |
mkilivan | 5:c8918e47c566 | 1349 | if (IsDBCS1(dst[j])) { |
mkilivan | 5:c8918e47c566 | 1350 | if (j == i - 1) break; |
mkilivan | 5:c8918e47c566 | 1351 | j++; |
mkilivan | 5:c8918e47c566 | 1352 | } |
mkilivan | 5:c8918e47c566 | 1353 | } |
mkilivan | 5:c8918e47c566 | 1354 | do { |
mkilivan | 5:c8918e47c566 | 1355 | dst[j++] = (i < 8) ? ns[i++] : ' '; |
mkilivan | 5:c8918e47c566 | 1356 | } while (j < 8); |
mkilivan | 5:c8918e47c566 | 1357 | } |
mkilivan | 5:c8918e47c566 | 1358 | #endif |
mkilivan | 5:c8918e47c566 | 1359 | |
mkilivan | 5:c8918e47c566 | 1360 | |
mkilivan | 5:c8918e47c566 | 1361 | |
mkilivan | 5:c8918e47c566 | 1362 | |
mkilivan | 5:c8918e47c566 | 1363 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 1364 | /* Calculate sum of an SFN */ |
mkilivan | 5:c8918e47c566 | 1365 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 1366 | #if _USE_LFN |
mkilivan | 5:c8918e47c566 | 1367 | static |
mkilivan | 5:c8918e47c566 | 1368 | BYTE sum_sfn ( |
mkilivan | 5:c8918e47c566 | 1369 | const BYTE *dir /* Ptr to directory entry */ |
mkilivan | 5:c8918e47c566 | 1370 | ) |
mkilivan | 5:c8918e47c566 | 1371 | { |
mkilivan | 5:c8918e47c566 | 1372 | BYTE sum = 0; |
mkilivan | 5:c8918e47c566 | 1373 | UINT n = 11; |
mkilivan | 5:c8918e47c566 | 1374 | |
mkilivan | 5:c8918e47c566 | 1375 | do sum = (sum >> 1) + (sum << 7) + *dir++; while (--n); |
mkilivan | 5:c8918e47c566 | 1376 | return sum; |
mkilivan | 5:c8918e47c566 | 1377 | } |
mkilivan | 5:c8918e47c566 | 1378 | #endif |
mkilivan | 5:c8918e47c566 | 1379 | |
mkilivan | 5:c8918e47c566 | 1380 | |
mkilivan | 5:c8918e47c566 | 1381 | |
mkilivan | 5:c8918e47c566 | 1382 | |
mkilivan | 5:c8918e47c566 | 1383 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 1384 | /* Directory handling - Find an object in the directory */ |
mkilivan | 5:c8918e47c566 | 1385 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 1386 | |
mkilivan | 5:c8918e47c566 | 1387 | static |
mkilivan | 5:c8918e47c566 | 1388 | FRESULT dir_find ( |
mkilivan | 5:c8918e47c566 | 1389 | FATFS_DIR *dj /* Pointer to the directory object linked to the file name */ |
mkilivan | 5:c8918e47c566 | 1390 | ) |
mkilivan | 5:c8918e47c566 | 1391 | { |
mkilivan | 5:c8918e47c566 | 1392 | FRESULT res; |
mkilivan | 5:c8918e47c566 | 1393 | BYTE c, *dir; |
mkilivan | 5:c8918e47c566 | 1394 | #if _USE_LFN |
mkilivan | 5:c8918e47c566 | 1395 | BYTE a, ord, sum; |
mkilivan | 5:c8918e47c566 | 1396 | #endif |
mkilivan | 5:c8918e47c566 | 1397 | |
mkilivan | 5:c8918e47c566 | 1398 | res = dir_sdi(dj, 0); /* Rewind directory object */ |
mkilivan | 5:c8918e47c566 | 1399 | if (res != FR_OK) return res; |
mkilivan | 5:c8918e47c566 | 1400 | |
mkilivan | 5:c8918e47c566 | 1401 | #if _USE_LFN |
mkilivan | 5:c8918e47c566 | 1402 | ord = sum = 0xFF; |
mkilivan | 5:c8918e47c566 | 1403 | #endif |
mkilivan | 5:c8918e47c566 | 1404 | do { |
mkilivan | 5:c8918e47c566 | 1405 | res = move_window(dj->fs, dj->sect); |
mkilivan | 5:c8918e47c566 | 1406 | if (res != FR_OK) break; |
mkilivan | 5:c8918e47c566 | 1407 | dir = dj->dir; /* Ptr to the directory entry of current index */ |
mkilivan | 5:c8918e47c566 | 1408 | c = dir[DIR_Name]; |
mkilivan | 5:c8918e47c566 | 1409 | if (c == 0) { res = FR_NO_FILE; break; } /* Reached to end of table */ |
mkilivan | 5:c8918e47c566 | 1410 | #if _USE_LFN /* LFN configuration */ |
mkilivan | 5:c8918e47c566 | 1411 | a = dir[DIR_Attr] & AM_MASK; |
mkilivan | 5:c8918e47c566 | 1412 | if (c == DDE || ((a & AM_VOL) && a != AM_LFN)) { /* An entry without valid data */ |
mkilivan | 5:c8918e47c566 | 1413 | ord = 0xFF; |
mkilivan | 5:c8918e47c566 | 1414 | } else { |
mkilivan | 5:c8918e47c566 | 1415 | if (a == AM_LFN) { /* An LFN entry is found */ |
mkilivan | 5:c8918e47c566 | 1416 | if (dj->lfn) { |
mkilivan | 5:c8918e47c566 | 1417 | if (c & LLE) { /* Is it start of LFN sequence? */ |
mkilivan | 5:c8918e47c566 | 1418 | sum = dir[LDIR_Chksum]; |
mkilivan | 5:c8918e47c566 | 1419 | c &= ~LLE; ord = c; /* LFN start order */ |
mkilivan | 5:c8918e47c566 | 1420 | dj->lfn_idx = dj->index; |
mkilivan | 5:c8918e47c566 | 1421 | } |
mkilivan | 5:c8918e47c566 | 1422 | /* Check validity of the LFN entry and compare it with given name */ |
mkilivan | 5:c8918e47c566 | 1423 | ord = (c == ord && sum == dir[LDIR_Chksum] && cmp_lfn(dj->lfn, dir)) ? ord - 1 : 0xFF; |
mkilivan | 5:c8918e47c566 | 1424 | } |
mkilivan | 5:c8918e47c566 | 1425 | } else { /* An SFN entry is found */ |
mkilivan | 5:c8918e47c566 | 1426 | if (!ord && sum == sum_sfn(dir)) break; /* LFN matched? */ |
mkilivan | 5:c8918e47c566 | 1427 | ord = 0xFF; dj->lfn_idx = 0xFFFF; /* Reset LFN sequence */ |
mkilivan | 5:c8918e47c566 | 1428 | if (!(dj->fn[NS] & NS_LOSS) && !mem_cmp(dir, dj->fn, 11)) break; /* SFN matched? */ |
mkilivan | 5:c8918e47c566 | 1429 | } |
mkilivan | 5:c8918e47c566 | 1430 | } |
mkilivan | 5:c8918e47c566 | 1431 | #else /* Non LFN configuration */ |
mkilivan | 5:c8918e47c566 | 1432 | if (!(dir[DIR_Attr] & AM_VOL) && !mem_cmp(dir, dj->fn, 11)) /* Is it a valid entry? */ |
mkilivan | 5:c8918e47c566 | 1433 | break; |
mkilivan | 5:c8918e47c566 | 1434 | #endif |
mkilivan | 5:c8918e47c566 | 1435 | res = dir_next(dj, 0); /* Next entry */ |
mkilivan | 5:c8918e47c566 | 1436 | } while (res == FR_OK); |
mkilivan | 5:c8918e47c566 | 1437 | |
mkilivan | 5:c8918e47c566 | 1438 | return res; |
mkilivan | 5:c8918e47c566 | 1439 | } |
mkilivan | 5:c8918e47c566 | 1440 | |
mkilivan | 5:c8918e47c566 | 1441 | |
mkilivan | 5:c8918e47c566 | 1442 | |
mkilivan | 5:c8918e47c566 | 1443 | |
mkilivan | 5:c8918e47c566 | 1444 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 1445 | /* Read an object from the directory */ |
mkilivan | 5:c8918e47c566 | 1446 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 1447 | #if _FS_MINIMIZE <= 1 |
mkilivan | 5:c8918e47c566 | 1448 | static |
mkilivan | 5:c8918e47c566 | 1449 | FRESULT dir_read ( |
mkilivan | 5:c8918e47c566 | 1450 | FATFS_DIR *dj /* Pointer to the directory object that pointing the entry to be read */ |
mkilivan | 5:c8918e47c566 | 1451 | ) |
mkilivan | 5:c8918e47c566 | 1452 | { |
mkilivan | 5:c8918e47c566 | 1453 | FRESULT res; |
mkilivan | 5:c8918e47c566 | 1454 | BYTE c, *dir; |
mkilivan | 5:c8918e47c566 | 1455 | #if _USE_LFN |
mkilivan | 5:c8918e47c566 | 1456 | BYTE a, ord = 0xFF, sum = 0xFF; |
mkilivan | 5:c8918e47c566 | 1457 | #endif |
mkilivan | 5:c8918e47c566 | 1458 | |
mkilivan | 5:c8918e47c566 | 1459 | res = FR_NO_FILE; |
mkilivan | 5:c8918e47c566 | 1460 | while (dj->sect) { |
mkilivan | 5:c8918e47c566 | 1461 | res = move_window(dj->fs, dj->sect); |
mkilivan | 5:c8918e47c566 | 1462 | if (res != FR_OK) break; |
mkilivan | 5:c8918e47c566 | 1463 | dir = dj->dir; /* Ptr to the directory entry of current index */ |
mkilivan | 5:c8918e47c566 | 1464 | c = dir[DIR_Name]; |
mkilivan | 5:c8918e47c566 | 1465 | if (c == 0) { res = FR_NO_FILE; break; } /* Reached to end of table */ |
mkilivan | 5:c8918e47c566 | 1466 | #if _USE_LFN /* LFN configuration */ |
mkilivan | 5:c8918e47c566 | 1467 | a = dir[DIR_Attr] & AM_MASK; |
mkilivan | 5:c8918e47c566 | 1468 | if (c == DDE || (!_FS_RPATH && c == '.') || ((a & AM_VOL) && a != AM_LFN)) { /* An entry without valid data */ |
mkilivan | 5:c8918e47c566 | 1469 | ord = 0xFF; |
mkilivan | 5:c8918e47c566 | 1470 | } else { |
mkilivan | 5:c8918e47c566 | 1471 | if (a == AM_LFN) { /* An LFN entry is found */ |
mkilivan | 5:c8918e47c566 | 1472 | if (c & LLE) { /* Is it start of LFN sequence? */ |
mkilivan | 5:c8918e47c566 | 1473 | sum = dir[LDIR_Chksum]; |
mkilivan | 5:c8918e47c566 | 1474 | c &= ~LLE; ord = c; |
mkilivan | 5:c8918e47c566 | 1475 | dj->lfn_idx = dj->index; |
mkilivan | 5:c8918e47c566 | 1476 | } |
mkilivan | 5:c8918e47c566 | 1477 | /* Check LFN validity and capture it */ |
mkilivan | 5:c8918e47c566 | 1478 | ord = (c == ord && sum == dir[LDIR_Chksum] && pick_lfn(dj->lfn, dir)) ? ord - 1 : 0xFF; |
mkilivan | 5:c8918e47c566 | 1479 | } else { /* An SFN entry is found */ |
mkilivan | 5:c8918e47c566 | 1480 | if (ord || sum != sum_sfn(dir)) /* Is there a valid LFN? */ |
mkilivan | 5:c8918e47c566 | 1481 | dj->lfn_idx = 0xFFFF; /* It has no LFN. */ |
mkilivan | 5:c8918e47c566 | 1482 | break; |
mkilivan | 5:c8918e47c566 | 1483 | } |
mkilivan | 5:c8918e47c566 | 1484 | } |
mkilivan | 5:c8918e47c566 | 1485 | #else /* Non LFN configuration */ |
mkilivan | 5:c8918e47c566 | 1486 | if (c != DDE && (_FS_RPATH || c != '.') && !(dir[DIR_Attr] & AM_VOL)) /* Is it a valid entry? */ |
mkilivan | 5:c8918e47c566 | 1487 | break; |
mkilivan | 5:c8918e47c566 | 1488 | #endif |
mkilivan | 5:c8918e47c566 | 1489 | res = dir_next(dj, 0); /* Next entry */ |
mkilivan | 5:c8918e47c566 | 1490 | if (res != FR_OK) break; |
mkilivan | 5:c8918e47c566 | 1491 | } |
mkilivan | 5:c8918e47c566 | 1492 | |
mkilivan | 5:c8918e47c566 | 1493 | if (res != FR_OK) dj->sect = 0; |
mkilivan | 5:c8918e47c566 | 1494 | |
mkilivan | 5:c8918e47c566 | 1495 | return res; |
mkilivan | 5:c8918e47c566 | 1496 | } |
mkilivan | 5:c8918e47c566 | 1497 | #endif |
mkilivan | 5:c8918e47c566 | 1498 | |
mkilivan | 5:c8918e47c566 | 1499 | |
mkilivan | 5:c8918e47c566 | 1500 | |
mkilivan | 5:c8918e47c566 | 1501 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 1502 | /* Register an object to the directory */ |
mkilivan | 5:c8918e47c566 | 1503 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 1504 | #if !_FS_READONLY |
mkilivan | 5:c8918e47c566 | 1505 | static |
mkilivan | 5:c8918e47c566 | 1506 | FRESULT dir_register ( /* FR_OK:Successful, FR_DENIED:No free entry or too many SFN collision, FR_DISK_ERR:Disk error */ |
mkilivan | 5:c8918e47c566 | 1507 | FATFS_DIR *dj /* Target directory with object name to be created */ |
mkilivan | 5:c8918e47c566 | 1508 | ) |
mkilivan | 5:c8918e47c566 | 1509 | { |
mkilivan | 5:c8918e47c566 | 1510 | FRESULT res; |
mkilivan | 5:c8918e47c566 | 1511 | BYTE c, *dir; |
mkilivan | 5:c8918e47c566 | 1512 | #if _USE_LFN /* LFN configuration */ |
mkilivan | 5:c8918e47c566 | 1513 | WORD n, ne, is; |
mkilivan | 5:c8918e47c566 | 1514 | BYTE sn[12], *fn, sum; |
mkilivan | 5:c8918e47c566 | 1515 | WCHAR *lfn; |
mkilivan | 5:c8918e47c566 | 1516 | |
mkilivan | 5:c8918e47c566 | 1517 | |
mkilivan | 5:c8918e47c566 | 1518 | fn = dj->fn; lfn = dj->lfn; |
mkilivan | 5:c8918e47c566 | 1519 | mem_cpy(sn, fn, 12); |
mkilivan | 5:c8918e47c566 | 1520 | |
mkilivan | 5:c8918e47c566 | 1521 | if (_FS_RPATH && (sn[NS] & NS_DOT)) /* Cannot create dot entry */ |
mkilivan | 5:c8918e47c566 | 1522 | return FR_INVALID_NAME; |
mkilivan | 5:c8918e47c566 | 1523 | |
mkilivan | 5:c8918e47c566 | 1524 | if (sn[NS] & NS_LOSS) { /* When LFN is out of 8.3 format, generate a numbered name */ |
mkilivan | 5:c8918e47c566 | 1525 | fn[NS] = 0; dj->lfn = 0; /* Find only SFN */ |
mkilivan | 5:c8918e47c566 | 1526 | for (n = 1; n < 100; n++) { |
mkilivan | 5:c8918e47c566 | 1527 | gen_numname(fn, sn, lfn, n); /* Generate a numbered name */ |
mkilivan | 5:c8918e47c566 | 1528 | res = dir_find(dj); /* Check if the name collides with existing SFN */ |
mkilivan | 5:c8918e47c566 | 1529 | if (res != FR_OK) break; |
mkilivan | 5:c8918e47c566 | 1530 | } |
mkilivan | 5:c8918e47c566 | 1531 | if (n == 100) return FR_DENIED; /* Abort if too many collisions */ |
mkilivan | 5:c8918e47c566 | 1532 | if (res != FR_NO_FILE) return res; /* Abort if the result is other than 'not collided' */ |
mkilivan | 5:c8918e47c566 | 1533 | fn[NS] = sn[NS]; dj->lfn = lfn; |
mkilivan | 5:c8918e47c566 | 1534 | } |
mkilivan | 5:c8918e47c566 | 1535 | |
mkilivan | 5:c8918e47c566 | 1536 | if (sn[NS] & NS_LFN) { /* When LFN is to be created, reserve an SFN + LFN entries. */ |
mkilivan | 5:c8918e47c566 | 1537 | for (ne = 0; lfn[ne]; ne++) ; |
mkilivan | 5:c8918e47c566 | 1538 | ne = (ne + 25) / 13; |
mkilivan | 5:c8918e47c566 | 1539 | } else { /* Otherwise reserve only an SFN entry. */ |
mkilivan | 5:c8918e47c566 | 1540 | ne = 1; |
mkilivan | 5:c8918e47c566 | 1541 | } |
mkilivan | 5:c8918e47c566 | 1542 | |
mkilivan | 5:c8918e47c566 | 1543 | /* Reserve contiguous entries */ |
mkilivan | 5:c8918e47c566 | 1544 | res = dir_sdi(dj, 0); |
mkilivan | 5:c8918e47c566 | 1545 | if (res != FR_OK) return res; |
mkilivan | 5:c8918e47c566 | 1546 | n = is = 0; |
mkilivan | 5:c8918e47c566 | 1547 | do { |
mkilivan | 5:c8918e47c566 | 1548 | res = move_window(dj->fs, dj->sect); |
mkilivan | 5:c8918e47c566 | 1549 | if (res != FR_OK) break; |
mkilivan | 5:c8918e47c566 | 1550 | c = *dj->dir; /* Check the entry status */ |
mkilivan | 5:c8918e47c566 | 1551 | if (c == DDE || c == 0) { /* Is it a blank entry? */ |
mkilivan | 5:c8918e47c566 | 1552 | if (n == 0) is = dj->index; /* First index of the contiguous entry */ |
mkilivan | 5:c8918e47c566 | 1553 | if (++n == ne) break; /* A contiguous entry that required count is found */ |
mkilivan | 5:c8918e47c566 | 1554 | } else { |
mkilivan | 5:c8918e47c566 | 1555 | n = 0; /* Not a blank entry. Restart to search */ |
mkilivan | 5:c8918e47c566 | 1556 | } |
mkilivan | 5:c8918e47c566 | 1557 | res = dir_next(dj, 1); /* Next entry with table stretch */ |
mkilivan | 5:c8918e47c566 | 1558 | } while (res == FR_OK); |
mkilivan | 5:c8918e47c566 | 1559 | |
mkilivan | 5:c8918e47c566 | 1560 | if (res == FR_OK && ne > 1) { /* Initialize LFN entry if needed */ |
mkilivan | 5:c8918e47c566 | 1561 | res = dir_sdi(dj, is); |
mkilivan | 5:c8918e47c566 | 1562 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 1563 | sum = sum_sfn(dj->fn); /* Sum of the SFN tied to the LFN */ |
mkilivan | 5:c8918e47c566 | 1564 | ne--; |
mkilivan | 5:c8918e47c566 | 1565 | do { /* Store LFN entries in bottom first */ |
mkilivan | 5:c8918e47c566 | 1566 | res = move_window(dj->fs, dj->sect); |
mkilivan | 5:c8918e47c566 | 1567 | if (res != FR_OK) break; |
mkilivan | 5:c8918e47c566 | 1568 | fit_lfn(dj->lfn, dj->dir, (BYTE)ne, sum); |
mkilivan | 5:c8918e47c566 | 1569 | dj->fs->wflag = 1; |
mkilivan | 5:c8918e47c566 | 1570 | res = dir_next(dj, 0); /* Next entry */ |
mkilivan | 5:c8918e47c566 | 1571 | } while (res == FR_OK && --ne); |
mkilivan | 5:c8918e47c566 | 1572 | } |
mkilivan | 5:c8918e47c566 | 1573 | } |
mkilivan | 5:c8918e47c566 | 1574 | |
mkilivan | 5:c8918e47c566 | 1575 | #else /* Non LFN configuration */ |
mkilivan | 5:c8918e47c566 | 1576 | res = dir_sdi(dj, 0); |
mkilivan | 5:c8918e47c566 | 1577 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 1578 | do { /* Find a blank entry for the SFN */ |
mkilivan | 5:c8918e47c566 | 1579 | res = move_window(dj->fs, dj->sect); |
mkilivan | 5:c8918e47c566 | 1580 | if (res != FR_OK) break; |
mkilivan | 5:c8918e47c566 | 1581 | c = *dj->dir; |
mkilivan | 5:c8918e47c566 | 1582 | if (c == DDE || c == 0) break; /* Is it a blank entry? */ |
mkilivan | 5:c8918e47c566 | 1583 | res = dir_next(dj, 1); /* Next entry with table stretch */ |
mkilivan | 5:c8918e47c566 | 1584 | } while (res == FR_OK); |
mkilivan | 5:c8918e47c566 | 1585 | } |
mkilivan | 5:c8918e47c566 | 1586 | #endif |
mkilivan | 5:c8918e47c566 | 1587 | |
mkilivan | 5:c8918e47c566 | 1588 | if (res == FR_OK) { /* Initialize the SFN entry */ |
mkilivan | 5:c8918e47c566 | 1589 | res = move_window(dj->fs, dj->sect); |
mkilivan | 5:c8918e47c566 | 1590 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 1591 | dir = dj->dir; |
mkilivan | 5:c8918e47c566 | 1592 | mem_set(dir, 0, SZ_DIR); /* Clean the entry */ |
mkilivan | 5:c8918e47c566 | 1593 | mem_cpy(dir, dj->fn, 11); /* Put SFN */ |
mkilivan | 5:c8918e47c566 | 1594 | #if _USE_LFN |
mkilivan | 5:c8918e47c566 | 1595 | dir[DIR_NTres] = *(dj->fn+NS) & (NS_BODY | NS_EXT); /* Put NT flag */ |
mkilivan | 5:c8918e47c566 | 1596 | #endif |
mkilivan | 5:c8918e47c566 | 1597 | dj->fs->wflag = 1; |
mkilivan | 5:c8918e47c566 | 1598 | } |
mkilivan | 5:c8918e47c566 | 1599 | } |
mkilivan | 5:c8918e47c566 | 1600 | |
mkilivan | 5:c8918e47c566 | 1601 | return res; |
mkilivan | 5:c8918e47c566 | 1602 | } |
mkilivan | 5:c8918e47c566 | 1603 | #endif /* !_FS_READONLY */ |
mkilivan | 5:c8918e47c566 | 1604 | |
mkilivan | 5:c8918e47c566 | 1605 | |
mkilivan | 5:c8918e47c566 | 1606 | |
mkilivan | 5:c8918e47c566 | 1607 | |
mkilivan | 5:c8918e47c566 | 1608 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 1609 | /* Remove an object from the directory */ |
mkilivan | 5:c8918e47c566 | 1610 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 1611 | #if !_FS_READONLY && !_FS_MINIMIZE |
mkilivan | 5:c8918e47c566 | 1612 | static |
mkilivan | 5:c8918e47c566 | 1613 | FRESULT dir_remove ( /* FR_OK: Successful, FR_DISK_ERR: A disk error */ |
mkilivan | 5:c8918e47c566 | 1614 | FATFS_DIR *dj /* Directory object pointing the entry to be removed */ |
mkilivan | 5:c8918e47c566 | 1615 | ) |
mkilivan | 5:c8918e47c566 | 1616 | { |
mkilivan | 5:c8918e47c566 | 1617 | FRESULT res; |
mkilivan | 5:c8918e47c566 | 1618 | #if _USE_LFN /* LFN configuration */ |
mkilivan | 5:c8918e47c566 | 1619 | WORD i; |
mkilivan | 5:c8918e47c566 | 1620 | |
mkilivan | 5:c8918e47c566 | 1621 | i = dj->index; /* SFN index */ |
mkilivan | 5:c8918e47c566 | 1622 | res = dir_sdi(dj, (WORD)((dj->lfn_idx == 0xFFFF) ? i : dj->lfn_idx)); /* Goto the SFN or top of the LFN entries */ |
mkilivan | 5:c8918e47c566 | 1623 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 1624 | do { |
mkilivan | 5:c8918e47c566 | 1625 | res = move_window(dj->fs, dj->sect); |
mkilivan | 5:c8918e47c566 | 1626 | if (res != FR_OK) break; |
mkilivan | 5:c8918e47c566 | 1627 | *dj->dir = DDE; /* Mark the entry "deleted" */ |
mkilivan | 5:c8918e47c566 | 1628 | dj->fs->wflag = 1; |
mkilivan | 5:c8918e47c566 | 1629 | if (dj->index >= i) break; /* When reached SFN, all entries of the object has been deleted. */ |
mkilivan | 5:c8918e47c566 | 1630 | res = dir_next(dj, 0); /* Next entry */ |
mkilivan | 5:c8918e47c566 | 1631 | } while (res == FR_OK); |
mkilivan | 5:c8918e47c566 | 1632 | if (res == FR_NO_FILE) res = FR_INT_ERR; |
mkilivan | 5:c8918e47c566 | 1633 | } |
mkilivan | 5:c8918e47c566 | 1634 | |
mkilivan | 5:c8918e47c566 | 1635 | #else /* Non LFN configuration */ |
mkilivan | 5:c8918e47c566 | 1636 | res = dir_sdi(dj, dj->index); |
mkilivan | 5:c8918e47c566 | 1637 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 1638 | res = move_window(dj->fs, dj->sect); |
mkilivan | 5:c8918e47c566 | 1639 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 1640 | *dj->dir = DDE; /* Mark the entry "deleted" */ |
mkilivan | 5:c8918e47c566 | 1641 | dj->fs->wflag = 1; |
mkilivan | 5:c8918e47c566 | 1642 | } |
mkilivan | 5:c8918e47c566 | 1643 | } |
mkilivan | 5:c8918e47c566 | 1644 | #endif |
mkilivan | 5:c8918e47c566 | 1645 | |
mkilivan | 5:c8918e47c566 | 1646 | return res; |
mkilivan | 5:c8918e47c566 | 1647 | } |
mkilivan | 5:c8918e47c566 | 1648 | #endif /* !_FS_READONLY */ |
mkilivan | 5:c8918e47c566 | 1649 | |
mkilivan | 5:c8918e47c566 | 1650 | |
mkilivan | 5:c8918e47c566 | 1651 | |
mkilivan | 5:c8918e47c566 | 1652 | |
mkilivan | 5:c8918e47c566 | 1653 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 1654 | /* Pick a segment and create the object name in directory form */ |
mkilivan | 5:c8918e47c566 | 1655 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 1656 | |
mkilivan | 5:c8918e47c566 | 1657 | static |
mkilivan | 5:c8918e47c566 | 1658 | FRESULT create_name ( |
mkilivan | 5:c8918e47c566 | 1659 | FATFS_DIR *dj, /* Pointer to the directory object */ |
mkilivan | 5:c8918e47c566 | 1660 | const TCHAR **path /* Pointer to pointer to the segment in the path string */ |
mkilivan | 5:c8918e47c566 | 1661 | ) |
mkilivan | 5:c8918e47c566 | 1662 | { |
mkilivan | 5:c8918e47c566 | 1663 | #ifdef _EXCVT |
mkilivan | 5:c8918e47c566 | 1664 | static const BYTE excvt[] = _EXCVT; /* Upper conversion table for extended chars */ |
mkilivan | 5:c8918e47c566 | 1665 | #endif |
mkilivan | 5:c8918e47c566 | 1666 | |
mkilivan | 5:c8918e47c566 | 1667 | #if _USE_LFN /* LFN configuration */ |
mkilivan | 5:c8918e47c566 | 1668 | BYTE b, cf; |
mkilivan | 5:c8918e47c566 | 1669 | WCHAR w, *lfn; |
mkilivan | 5:c8918e47c566 | 1670 | UINT i, ni, si, di; |
mkilivan | 5:c8918e47c566 | 1671 | const TCHAR *p; |
mkilivan | 5:c8918e47c566 | 1672 | |
mkilivan | 5:c8918e47c566 | 1673 | /* Create LFN in Unicode */ |
mkilivan | 5:c8918e47c566 | 1674 | for (p = *path; *p == '/' || *p == '\\'; p++) ; /* Strip duplicated separator */ |
mkilivan | 5:c8918e47c566 | 1675 | lfn = dj->lfn; |
mkilivan | 5:c8918e47c566 | 1676 | si = di = 0; |
mkilivan | 5:c8918e47c566 | 1677 | for (;;) { |
mkilivan | 5:c8918e47c566 | 1678 | w = p[si++]; /* Get a character */ |
mkilivan | 5:c8918e47c566 | 1679 | if (w < ' ' || w == '/' || w == '\\') break; /* Break on end of segment */ |
mkilivan | 5:c8918e47c566 | 1680 | if (di >= _MAX_LFN) /* Reject too long name */ |
mkilivan | 5:c8918e47c566 | 1681 | return FR_INVALID_NAME; |
mkilivan | 5:c8918e47c566 | 1682 | #if !_LFN_UNICODE |
mkilivan | 5:c8918e47c566 | 1683 | w &= 0xFF; |
mkilivan | 5:c8918e47c566 | 1684 | if (IsDBCS1(w)) { /* Check if it is a DBC 1st byte (always false on SBCS cfg) */ |
mkilivan | 5:c8918e47c566 | 1685 | b = (BYTE)p[si++]; /* Get 2nd byte */ |
mkilivan | 5:c8918e47c566 | 1686 | if (!IsDBCS2(b)) |
mkilivan | 5:c8918e47c566 | 1687 | return FR_INVALID_NAME; /* Reject invalid sequence */ |
mkilivan | 5:c8918e47c566 | 1688 | w = (w << 8) + b; /* Create a DBC */ |
mkilivan | 5:c8918e47c566 | 1689 | } |
mkilivan | 5:c8918e47c566 | 1690 | w = ff_convert(w, 1); /* Convert ANSI/OEM to Unicode */ |
mkilivan | 5:c8918e47c566 | 1691 | if (!w) return FR_INVALID_NAME; /* Reject invalid code */ |
mkilivan | 5:c8918e47c566 | 1692 | #endif |
mkilivan | 5:c8918e47c566 | 1693 | if (w < 0x80 && chk_chr("\"*:<>\?|\x7F", w)) /* Reject illegal chars for LFN */ |
mkilivan | 5:c8918e47c566 | 1694 | return FR_INVALID_NAME; |
mkilivan | 5:c8918e47c566 | 1695 | lfn[di++] = w; /* Store the Unicode char */ |
mkilivan | 5:c8918e47c566 | 1696 | } |
mkilivan | 5:c8918e47c566 | 1697 | *path = &p[si]; /* Return pointer to the next segment */ |
mkilivan | 5:c8918e47c566 | 1698 | cf = (w < ' ') ? NS_LAST : 0; /* Set last segment flag if end of path */ |
mkilivan | 5:c8918e47c566 | 1699 | #if _FS_RPATH |
mkilivan | 5:c8918e47c566 | 1700 | if ((di == 1 && lfn[di-1] == '.') || /* Is this a dot entry? */ |
mkilivan | 5:c8918e47c566 | 1701 | (di == 2 && lfn[di-1] == '.' && lfn[di-2] == '.')) { |
mkilivan | 5:c8918e47c566 | 1702 | lfn[di] = 0; |
mkilivan | 5:c8918e47c566 | 1703 | for (i = 0; i < 11; i++) |
mkilivan | 5:c8918e47c566 | 1704 | dj->fn[i] = (i < di) ? '.' : ' '; |
mkilivan | 5:c8918e47c566 | 1705 | dj->fn[i] = cf | NS_DOT; /* This is a dot entry */ |
mkilivan | 5:c8918e47c566 | 1706 | return FR_OK; |
mkilivan | 5:c8918e47c566 | 1707 | } |
mkilivan | 5:c8918e47c566 | 1708 | #endif |
mkilivan | 5:c8918e47c566 | 1709 | while (di) { /* Strip trailing spaces and dots */ |
mkilivan | 5:c8918e47c566 | 1710 | w = lfn[di-1]; |
mkilivan | 5:c8918e47c566 | 1711 | if (w != ' ' && w != '.') break; |
mkilivan | 5:c8918e47c566 | 1712 | di--; |
mkilivan | 5:c8918e47c566 | 1713 | } |
mkilivan | 5:c8918e47c566 | 1714 | if (!di) return FR_INVALID_NAME; /* Reject nul string */ |
mkilivan | 5:c8918e47c566 | 1715 | |
mkilivan | 5:c8918e47c566 | 1716 | lfn[di] = 0; /* LFN is created */ |
mkilivan | 5:c8918e47c566 | 1717 | |
mkilivan | 5:c8918e47c566 | 1718 | /* Create SFN in directory form */ |
mkilivan | 5:c8918e47c566 | 1719 | mem_set(dj->fn, ' ', 11); |
mkilivan | 5:c8918e47c566 | 1720 | for (si = 0; lfn[si] == ' ' || lfn[si] == '.'; si++) ; /* Strip leading spaces and dots */ |
mkilivan | 5:c8918e47c566 | 1721 | if (si) cf |= NS_LOSS | NS_LFN; |
mkilivan | 5:c8918e47c566 | 1722 | while (di && lfn[di - 1] != '.') di--; /* Find extension (di<=si: no extension) */ |
mkilivan | 5:c8918e47c566 | 1723 | |
mkilivan | 5:c8918e47c566 | 1724 | b = i = 0; ni = 8; |
mkilivan | 5:c8918e47c566 | 1725 | for (;;) { |
mkilivan | 5:c8918e47c566 | 1726 | w = lfn[si++]; /* Get an LFN char */ |
mkilivan | 5:c8918e47c566 | 1727 | if (!w) break; /* Break on end of the LFN */ |
mkilivan | 5:c8918e47c566 | 1728 | if (w == ' ' || (w == '.' && si != di)) { /* Remove spaces and dots */ |
mkilivan | 5:c8918e47c566 | 1729 | cf |= NS_LOSS | NS_LFN; continue; |
mkilivan | 5:c8918e47c566 | 1730 | } |
mkilivan | 5:c8918e47c566 | 1731 | |
mkilivan | 5:c8918e47c566 | 1732 | if (i >= ni || si == di) { /* Extension or end of SFN */ |
mkilivan | 5:c8918e47c566 | 1733 | if (ni == 11) { /* Long extension */ |
mkilivan | 5:c8918e47c566 | 1734 | cf |= NS_LOSS | NS_LFN; break; |
mkilivan | 5:c8918e47c566 | 1735 | } |
mkilivan | 5:c8918e47c566 | 1736 | if (si != di) cf |= NS_LOSS | NS_LFN; /* Out of 8.3 format */ |
mkilivan | 5:c8918e47c566 | 1737 | if (si > di) break; /* No extension */ |
mkilivan | 5:c8918e47c566 | 1738 | si = di; i = 8; ni = 11; /* Enter extension section */ |
mkilivan | 5:c8918e47c566 | 1739 | b <<= 2; continue; |
mkilivan | 5:c8918e47c566 | 1740 | } |
mkilivan | 5:c8918e47c566 | 1741 | |
mkilivan | 5:c8918e47c566 | 1742 | if (w >= 0x80) { /* Non ASCII char */ |
mkilivan | 5:c8918e47c566 | 1743 | #ifdef _EXCVT |
mkilivan | 5:c8918e47c566 | 1744 | w = ff_convert(w, 0); /* Unicode -> OEM code */ |
mkilivan | 5:c8918e47c566 | 1745 | if (w) w = excvt[w - 0x80]; /* Convert extended char to upper (SBCS) */ |
mkilivan | 5:c8918e47c566 | 1746 | #else |
mkilivan | 5:c8918e47c566 | 1747 | w = ff_convert(ff_wtoupper(w), 0); /* Upper converted Unicode -> OEM code */ |
mkilivan | 5:c8918e47c566 | 1748 | #endif |
mkilivan | 5:c8918e47c566 | 1749 | cf |= NS_LFN; /* Force create LFN entry */ |
mkilivan | 5:c8918e47c566 | 1750 | } |
mkilivan | 5:c8918e47c566 | 1751 | |
mkilivan | 5:c8918e47c566 | 1752 | if (_DF1S && w >= 0x100) { /* Double byte char (always false on SBCS cfg) */ |
mkilivan | 5:c8918e47c566 | 1753 | if (i >= ni - 1) { |
mkilivan | 5:c8918e47c566 | 1754 | cf |= NS_LOSS | NS_LFN; i = ni; continue; |
mkilivan | 5:c8918e47c566 | 1755 | } |
mkilivan | 5:c8918e47c566 | 1756 | dj->fn[i++] = (BYTE)(w >> 8); |
mkilivan | 5:c8918e47c566 | 1757 | } else { /* Single byte char */ |
mkilivan | 5:c8918e47c566 | 1758 | if (!w || chk_chr("+,;=[]", w)) { /* Replace illegal chars for SFN */ |
mkilivan | 5:c8918e47c566 | 1759 | w = '_'; cf |= NS_LOSS | NS_LFN;/* Lossy conversion */ |
mkilivan | 5:c8918e47c566 | 1760 | } else { |
mkilivan | 5:c8918e47c566 | 1761 | if (IsUpper(w)) { /* ASCII large capital */ |
mkilivan | 5:c8918e47c566 | 1762 | b |= 2; |
mkilivan | 5:c8918e47c566 | 1763 | } else { |
mkilivan | 5:c8918e47c566 | 1764 | if (IsLower(w)) { /* ASCII small capital */ |
mkilivan | 5:c8918e47c566 | 1765 | b |= 1; w -= 0x20; |
mkilivan | 5:c8918e47c566 | 1766 | } |
mkilivan | 5:c8918e47c566 | 1767 | } |
mkilivan | 5:c8918e47c566 | 1768 | } |
mkilivan | 5:c8918e47c566 | 1769 | } |
mkilivan | 5:c8918e47c566 | 1770 | dj->fn[i++] = (BYTE)w; |
mkilivan | 5:c8918e47c566 | 1771 | } |
mkilivan | 5:c8918e47c566 | 1772 | |
mkilivan | 5:c8918e47c566 | 1773 | if (dj->fn[0] == DDE) dj->fn[0] = NDDE; /* If the first char collides with deleted mark, replace it with 0x05 */ |
mkilivan | 5:c8918e47c566 | 1774 | |
mkilivan | 5:c8918e47c566 | 1775 | if (ni == 8) b <<= 2; |
mkilivan | 5:c8918e47c566 | 1776 | if ((b & 0x0C) == 0x0C || (b & 0x03) == 0x03) /* Create LFN entry when there are composite capitals */ |
mkilivan | 5:c8918e47c566 | 1777 | cf |= NS_LFN; |
mkilivan | 5:c8918e47c566 | 1778 | if (!(cf & NS_LFN)) { /* When LFN is in 8.3 format without extended char, NT flags are created */ |
mkilivan | 5:c8918e47c566 | 1779 | if ((b & 0x03) == 0x01) cf |= NS_EXT; /* NT flag (Extension has only small capital) */ |
mkilivan | 5:c8918e47c566 | 1780 | if ((b & 0x0C) == 0x04) cf |= NS_BODY; /* NT flag (Filename has only small capital) */ |
mkilivan | 5:c8918e47c566 | 1781 | } |
mkilivan | 5:c8918e47c566 | 1782 | |
mkilivan | 5:c8918e47c566 | 1783 | dj->fn[NS] = cf; /* SFN is created */ |
mkilivan | 5:c8918e47c566 | 1784 | |
mkilivan | 5:c8918e47c566 | 1785 | return FR_OK; |
mkilivan | 5:c8918e47c566 | 1786 | |
mkilivan | 5:c8918e47c566 | 1787 | |
mkilivan | 5:c8918e47c566 | 1788 | #else /* Non-LFN configuration */ |
mkilivan | 5:c8918e47c566 | 1789 | BYTE b, c, d, *sfn; |
mkilivan | 5:c8918e47c566 | 1790 | UINT ni, si, i; |
mkilivan | 5:c8918e47c566 | 1791 | const char *p; |
mkilivan | 5:c8918e47c566 | 1792 | |
mkilivan | 5:c8918e47c566 | 1793 | /* Create file name in directory form */ |
mkilivan | 5:c8918e47c566 | 1794 | for (p = *path; *p == '/' || *p == '\\'; p++) ; /* Strip duplicated separator */ |
mkilivan | 5:c8918e47c566 | 1795 | sfn = dj->fn; |
mkilivan | 5:c8918e47c566 | 1796 | mem_set(sfn, ' ', 11); |
mkilivan | 5:c8918e47c566 | 1797 | si = i = b = 0; ni = 8; |
mkilivan | 5:c8918e47c566 | 1798 | #if _FS_RPATH |
mkilivan | 5:c8918e47c566 | 1799 | if (p[si] == '.') { /* Is this a dot entry? */ |
mkilivan | 5:c8918e47c566 | 1800 | for (;;) { |
mkilivan | 5:c8918e47c566 | 1801 | c = (BYTE)p[si++]; |
mkilivan | 5:c8918e47c566 | 1802 | if (c != '.' || si >= 3) break; |
mkilivan | 5:c8918e47c566 | 1803 | sfn[i++] = c; |
mkilivan | 5:c8918e47c566 | 1804 | } |
mkilivan | 5:c8918e47c566 | 1805 | if (c != '/' && c != '\\' && c > ' ') return FR_INVALID_NAME; |
mkilivan | 5:c8918e47c566 | 1806 | *path = &p[si]; /* Return pointer to the next segment */ |
mkilivan | 5:c8918e47c566 | 1807 | sfn[NS] = (c <= ' ') ? NS_LAST | NS_DOT : NS_DOT; /* Set last segment flag if end of path */ |
mkilivan | 5:c8918e47c566 | 1808 | return FR_OK; |
mkilivan | 5:c8918e47c566 | 1809 | } |
mkilivan | 5:c8918e47c566 | 1810 | #endif |
mkilivan | 5:c8918e47c566 | 1811 | for (;;) { |
mkilivan | 5:c8918e47c566 | 1812 | c = (BYTE)p[si++]; |
mkilivan | 5:c8918e47c566 | 1813 | if (c <= ' ' || c == '/' || c == '\\') break; /* Break on end of segment */ |
mkilivan | 5:c8918e47c566 | 1814 | if (c == '.' || i >= ni) { |
mkilivan | 5:c8918e47c566 | 1815 | if (ni != 8 || c != '.') return FR_INVALID_NAME; |
mkilivan | 5:c8918e47c566 | 1816 | i = 8; ni = 11; |
mkilivan | 5:c8918e47c566 | 1817 | b <<= 2; continue; |
mkilivan | 5:c8918e47c566 | 1818 | } |
mkilivan | 5:c8918e47c566 | 1819 | if (c >= 0x80) { /* Extended char? */ |
mkilivan | 5:c8918e47c566 | 1820 | b |= 3; /* Eliminate NT flag */ |
mkilivan | 5:c8918e47c566 | 1821 | #ifdef _EXCVT |
mkilivan | 5:c8918e47c566 | 1822 | c = excvt[c - 0x80]; /* Upper conversion (SBCS) */ |
mkilivan | 5:c8918e47c566 | 1823 | #else |
mkilivan | 5:c8918e47c566 | 1824 | #if !_DF1S /* ASCII only cfg */ |
mkilivan | 5:c8918e47c566 | 1825 | return FR_INVALID_NAME; |
mkilivan | 5:c8918e47c566 | 1826 | #endif |
mkilivan | 5:c8918e47c566 | 1827 | #endif |
mkilivan | 5:c8918e47c566 | 1828 | } |
mkilivan | 5:c8918e47c566 | 1829 | if (IsDBCS1(c)) { /* Check if it is a DBC 1st byte (always false on SBCS cfg) */ |
mkilivan | 5:c8918e47c566 | 1830 | d = (BYTE)p[si++]; /* Get 2nd byte */ |
mkilivan | 5:c8918e47c566 | 1831 | if (!IsDBCS2(d) || i >= ni - 1) /* Reject invalid DBC */ |
mkilivan | 5:c8918e47c566 | 1832 | return FR_INVALID_NAME; |
mkilivan | 5:c8918e47c566 | 1833 | sfn[i++] = c; |
mkilivan | 5:c8918e47c566 | 1834 | sfn[i++] = d; |
mkilivan | 5:c8918e47c566 | 1835 | } else { /* Single byte code */ |
mkilivan | 5:c8918e47c566 | 1836 | if (chk_chr("\"*+,:;<=>\?[]|\x7F", c)) /* Reject illegal chrs for SFN */ |
mkilivan | 5:c8918e47c566 | 1837 | return FR_INVALID_NAME; |
mkilivan | 5:c8918e47c566 | 1838 | if (IsUpper(c)) { /* ASCII large capital? */ |
mkilivan | 5:c8918e47c566 | 1839 | b |= 2; |
mkilivan | 5:c8918e47c566 | 1840 | } else { |
mkilivan | 5:c8918e47c566 | 1841 | if (IsLower(c)) { /* ASCII small capital? */ |
mkilivan | 5:c8918e47c566 | 1842 | b |= 1; c -= 0x20; |
mkilivan | 5:c8918e47c566 | 1843 | } |
mkilivan | 5:c8918e47c566 | 1844 | } |
mkilivan | 5:c8918e47c566 | 1845 | sfn[i++] = c; |
mkilivan | 5:c8918e47c566 | 1846 | } |
mkilivan | 5:c8918e47c566 | 1847 | } |
mkilivan | 5:c8918e47c566 | 1848 | *path = &p[si]; /* Return pointer to the next segment */ |
mkilivan | 5:c8918e47c566 | 1849 | c = (c <= ' ') ? NS_LAST : 0; /* Set last segment flag if end of path */ |
mkilivan | 5:c8918e47c566 | 1850 | |
mkilivan | 5:c8918e47c566 | 1851 | if (!i) return FR_INVALID_NAME; /* Reject nul string */ |
mkilivan | 5:c8918e47c566 | 1852 | if (sfn[0] == DDE) sfn[0] = NDDE; /* When first char collides with DDE, replace it with 0x05 */ |
mkilivan | 5:c8918e47c566 | 1853 | |
mkilivan | 5:c8918e47c566 | 1854 | if (ni == 8) b <<= 2; |
mkilivan | 5:c8918e47c566 | 1855 | if ((b & 0x03) == 0x01) c |= NS_EXT; /* NT flag (Name extension has only small capital) */ |
mkilivan | 5:c8918e47c566 | 1856 | if ((b & 0x0C) == 0x04) c |= NS_BODY; /* NT flag (Name body has only small capital) */ |
mkilivan | 5:c8918e47c566 | 1857 | |
mkilivan | 5:c8918e47c566 | 1858 | sfn[NS] = c; /* Store NT flag, File name is created */ |
mkilivan | 5:c8918e47c566 | 1859 | |
mkilivan | 5:c8918e47c566 | 1860 | return FR_OK; |
mkilivan | 5:c8918e47c566 | 1861 | #endif |
mkilivan | 5:c8918e47c566 | 1862 | } |
mkilivan | 5:c8918e47c566 | 1863 | |
mkilivan | 5:c8918e47c566 | 1864 | |
mkilivan | 5:c8918e47c566 | 1865 | |
mkilivan | 5:c8918e47c566 | 1866 | |
mkilivan | 5:c8918e47c566 | 1867 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 1868 | /* Get file information from directory entry */ |
mkilivan | 5:c8918e47c566 | 1869 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 1870 | #if _FS_MINIMIZE <= 1 |
mkilivan | 5:c8918e47c566 | 1871 | static |
mkilivan | 5:c8918e47c566 | 1872 | void get_fileinfo ( /* No return code */ |
mkilivan | 5:c8918e47c566 | 1873 | FATFS_DIR *dj, /* Pointer to the directory object */ |
mkilivan | 5:c8918e47c566 | 1874 | FILINFO *fno /* Pointer to the file information to be filled */ |
mkilivan | 5:c8918e47c566 | 1875 | ) |
mkilivan | 5:c8918e47c566 | 1876 | { |
mkilivan | 5:c8918e47c566 | 1877 | UINT i; |
mkilivan | 5:c8918e47c566 | 1878 | BYTE nt, *dir; |
mkilivan | 5:c8918e47c566 | 1879 | TCHAR *p, c; |
mkilivan | 5:c8918e47c566 | 1880 | |
mkilivan | 5:c8918e47c566 | 1881 | |
mkilivan | 5:c8918e47c566 | 1882 | p = fno->fname; |
mkilivan | 5:c8918e47c566 | 1883 | if (dj->sect) { |
mkilivan | 5:c8918e47c566 | 1884 | dir = dj->dir; |
mkilivan | 5:c8918e47c566 | 1885 | nt = dir[DIR_NTres]; /* NT flag */ |
mkilivan | 5:c8918e47c566 | 1886 | for (i = 0; i < 8; i++) { /* Copy name body */ |
mkilivan | 5:c8918e47c566 | 1887 | c = dir[i]; |
mkilivan | 5:c8918e47c566 | 1888 | if (c == ' ') break; |
mkilivan | 5:c8918e47c566 | 1889 | if (c == NDDE) c = (TCHAR)DDE; |
mkilivan | 5:c8918e47c566 | 1890 | if (_USE_LFN && (nt & NS_BODY) && IsUpper(c)) c += 0x20; |
mkilivan | 5:c8918e47c566 | 1891 | #if _LFN_UNICODE |
mkilivan | 5:c8918e47c566 | 1892 | if (IsDBCS1(c) && i < 7 && IsDBCS2(dir[i+1])) |
mkilivan | 5:c8918e47c566 | 1893 | c = (c << 8) | dir[++i]; |
mkilivan | 5:c8918e47c566 | 1894 | c = ff_convert(c, 1); |
mkilivan | 5:c8918e47c566 | 1895 | if (!c) c = '?'; |
mkilivan | 5:c8918e47c566 | 1896 | #endif |
mkilivan | 5:c8918e47c566 | 1897 | *p++ = c; |
mkilivan | 5:c8918e47c566 | 1898 | } |
mkilivan | 5:c8918e47c566 | 1899 | if (dir[8] != ' ') { /* Copy name extension */ |
mkilivan | 5:c8918e47c566 | 1900 | *p++ = '.'; |
mkilivan | 5:c8918e47c566 | 1901 | for (i = 8; i < 11; i++) { |
mkilivan | 5:c8918e47c566 | 1902 | c = dir[i]; |
mkilivan | 5:c8918e47c566 | 1903 | if (c == ' ') break; |
mkilivan | 5:c8918e47c566 | 1904 | if (_USE_LFN && (nt & NS_EXT) && IsUpper(c)) c += 0x20; |
mkilivan | 5:c8918e47c566 | 1905 | #if _LFN_UNICODE |
mkilivan | 5:c8918e47c566 | 1906 | if (IsDBCS1(c) && i < 10 && IsDBCS2(dir[i+1])) |
mkilivan | 5:c8918e47c566 | 1907 | c = (c << 8) | dir[++i]; |
mkilivan | 5:c8918e47c566 | 1908 | c = ff_convert(c, 1); |
mkilivan | 5:c8918e47c566 | 1909 | if (!c) c = '?'; |
mkilivan | 5:c8918e47c566 | 1910 | #endif |
mkilivan | 5:c8918e47c566 | 1911 | *p++ = c; |
mkilivan | 5:c8918e47c566 | 1912 | } |
mkilivan | 5:c8918e47c566 | 1913 | } |
mkilivan | 5:c8918e47c566 | 1914 | fno->fattrib = dir[DIR_Attr]; /* Attribute */ |
mkilivan | 5:c8918e47c566 | 1915 | fno->fsize = LD_DWORD(dir+DIR_FileSize); /* Size */ |
mkilivan | 5:c8918e47c566 | 1916 | fno->fdate = LD_WORD(dir+DIR_WrtDate); /* Date */ |
mkilivan | 5:c8918e47c566 | 1917 | fno->ftime = LD_WORD(dir+DIR_WrtTime); /* Time */ |
mkilivan | 5:c8918e47c566 | 1918 | } |
mkilivan | 5:c8918e47c566 | 1919 | *p = 0; /* Terminate SFN str by a \0 */ |
mkilivan | 5:c8918e47c566 | 1920 | |
mkilivan | 5:c8918e47c566 | 1921 | #if _USE_LFN |
mkilivan | 5:c8918e47c566 | 1922 | if (fno->lfname && fno->lfsize) { |
mkilivan | 5:c8918e47c566 | 1923 | TCHAR *tp = fno->lfname; |
mkilivan | 5:c8918e47c566 | 1924 | WCHAR w, *lfn; |
mkilivan | 5:c8918e47c566 | 1925 | |
mkilivan | 5:c8918e47c566 | 1926 | i = 0; |
mkilivan | 5:c8918e47c566 | 1927 | if (dj->sect && dj->lfn_idx != 0xFFFF) {/* Get LFN if available */ |
mkilivan | 5:c8918e47c566 | 1928 | lfn = dj->lfn; |
mkilivan | 5:c8918e47c566 | 1929 | while ((w = *lfn++) != 0) { /* Get an LFN char */ |
mkilivan | 5:c8918e47c566 | 1930 | #if !_LFN_UNICODE |
mkilivan | 5:c8918e47c566 | 1931 | w = ff_convert(w, 0); /* Unicode -> OEM conversion */ |
mkilivan | 5:c8918e47c566 | 1932 | if (!w) { i = 0; break; } /* Could not convert, no LFN */ |
mkilivan | 5:c8918e47c566 | 1933 | if (_DF1S && w >= 0x100) /* Put 1st byte if it is a DBC (always false on SBCS cfg) */ |
mkilivan | 5:c8918e47c566 | 1934 | tp[i++] = (TCHAR)(w >> 8); |
mkilivan | 5:c8918e47c566 | 1935 | #endif |
mkilivan | 5:c8918e47c566 | 1936 | if (i >= fno->lfsize - 1) { i = 0; break; } /* Buffer overflow, no LFN */ |
mkilivan | 5:c8918e47c566 | 1937 | tp[i++] = (TCHAR)w; |
mkilivan | 5:c8918e47c566 | 1938 | } |
mkilivan | 5:c8918e47c566 | 1939 | } |
mkilivan | 5:c8918e47c566 | 1940 | tp[i] = 0; /* Terminate the LFN str by a \0 */ |
mkilivan | 5:c8918e47c566 | 1941 | } |
mkilivan | 5:c8918e47c566 | 1942 | #endif |
mkilivan | 5:c8918e47c566 | 1943 | } |
mkilivan | 5:c8918e47c566 | 1944 | #endif /* _FS_MINIMIZE <= 1 */ |
mkilivan | 5:c8918e47c566 | 1945 | |
mkilivan | 5:c8918e47c566 | 1946 | |
mkilivan | 5:c8918e47c566 | 1947 | |
mkilivan | 5:c8918e47c566 | 1948 | |
mkilivan | 5:c8918e47c566 | 1949 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 1950 | /* Follow a file path */ |
mkilivan | 5:c8918e47c566 | 1951 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 1952 | |
mkilivan | 5:c8918e47c566 | 1953 | static |
mkilivan | 5:c8918e47c566 | 1954 | FRESULT follow_path ( /* FR_OK(0): successful, !=0: error code */ |
mkilivan | 5:c8918e47c566 | 1955 | FATFS_DIR *dj, /* Directory object to return last directory and found object */ |
mkilivan | 5:c8918e47c566 | 1956 | const TCHAR *path /* Full-path string to find a file or directory */ |
mkilivan | 5:c8918e47c566 | 1957 | ) |
mkilivan | 5:c8918e47c566 | 1958 | { |
mkilivan | 5:c8918e47c566 | 1959 | FRESULT res; |
mkilivan | 5:c8918e47c566 | 1960 | BYTE *dir, ns; |
mkilivan | 5:c8918e47c566 | 1961 | |
mkilivan | 5:c8918e47c566 | 1962 | |
mkilivan | 5:c8918e47c566 | 1963 | #if _FS_RPATH |
mkilivan | 5:c8918e47c566 | 1964 | if (*path == '/' || *path == '\\') { /* There is a heading separator */ |
mkilivan | 5:c8918e47c566 | 1965 | path++; dj->sclust = 0; /* Strip it and start from the root dir */ |
mkilivan | 5:c8918e47c566 | 1966 | } else { /* No heading separator */ |
mkilivan | 5:c8918e47c566 | 1967 | dj->sclust = dj->fs->cdir; /* Start from the current dir */ |
mkilivan | 5:c8918e47c566 | 1968 | } |
mkilivan | 5:c8918e47c566 | 1969 | #else |
mkilivan | 5:c8918e47c566 | 1970 | if (*path == '/' || *path == '\\') /* Strip heading separator if exist */ |
mkilivan | 5:c8918e47c566 | 1971 | path++; |
mkilivan | 5:c8918e47c566 | 1972 | dj->sclust = 0; /* Start from the root dir */ |
mkilivan | 5:c8918e47c566 | 1973 | #endif |
mkilivan | 5:c8918e47c566 | 1974 | |
mkilivan | 5:c8918e47c566 | 1975 | if ((UINT)*path < ' ') { /* Nul path means the start directory itself */ |
mkilivan | 5:c8918e47c566 | 1976 | res = dir_sdi(dj, 0); |
mkilivan | 5:c8918e47c566 | 1977 | dj->dir = 0; |
mkilivan | 5:c8918e47c566 | 1978 | } else { /* Follow path */ |
mkilivan | 5:c8918e47c566 | 1979 | for (;;) { |
mkilivan | 5:c8918e47c566 | 1980 | res = create_name(dj, &path); /* Get a segment */ |
mkilivan | 5:c8918e47c566 | 1981 | if (res != FR_OK) break; |
mkilivan | 5:c8918e47c566 | 1982 | res = dir_find(dj); /* Find it */ |
mkilivan | 5:c8918e47c566 | 1983 | ns = *(dj->fn+NS); |
mkilivan | 5:c8918e47c566 | 1984 | if (res != FR_OK) { /* Failed to find the object */ |
mkilivan | 5:c8918e47c566 | 1985 | if (res != FR_NO_FILE) break; /* Abort if any hard error occurred */ |
mkilivan | 5:c8918e47c566 | 1986 | /* Object not found */ |
mkilivan | 5:c8918e47c566 | 1987 | if (_FS_RPATH && (ns & NS_DOT)) { /* If dot entry is not exit */ |
mkilivan | 5:c8918e47c566 | 1988 | dj->sclust = 0; dj->dir = 0; /* It is the root dir */ |
mkilivan | 5:c8918e47c566 | 1989 | res = FR_OK; |
mkilivan | 5:c8918e47c566 | 1990 | if (!(ns & NS_LAST)) continue; |
mkilivan | 5:c8918e47c566 | 1991 | } else { /* Could not find the object */ |
mkilivan | 5:c8918e47c566 | 1992 | if (!(ns & NS_LAST)) res = FR_NO_PATH; |
mkilivan | 5:c8918e47c566 | 1993 | } |
mkilivan | 5:c8918e47c566 | 1994 | break; |
mkilivan | 5:c8918e47c566 | 1995 | } |
mkilivan | 5:c8918e47c566 | 1996 | if (ns & NS_LAST) break; /* Last segment match. Function completed. */ |
mkilivan | 5:c8918e47c566 | 1997 | dir = dj->dir; /* There is next segment. Follow the sub directory */ |
mkilivan | 5:c8918e47c566 | 1998 | if (!(dir[DIR_Attr] & AM_DIR)) { /* Cannot follow because it is a file */ |
mkilivan | 5:c8918e47c566 | 1999 | res = FR_NO_PATH; break; |
mkilivan | 5:c8918e47c566 | 2000 | } |
mkilivan | 5:c8918e47c566 | 2001 | dj->sclust = ld_clust(dj->fs, dir); |
mkilivan | 5:c8918e47c566 | 2002 | } |
mkilivan | 5:c8918e47c566 | 2003 | } |
mkilivan | 5:c8918e47c566 | 2004 | |
mkilivan | 5:c8918e47c566 | 2005 | return res; |
mkilivan | 5:c8918e47c566 | 2006 | } |
mkilivan | 5:c8918e47c566 | 2007 | |
mkilivan | 5:c8918e47c566 | 2008 | |
mkilivan | 5:c8918e47c566 | 2009 | |
mkilivan | 5:c8918e47c566 | 2010 | |
mkilivan | 5:c8918e47c566 | 2011 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 2012 | /* Load a sector and check if it is an FAT Volume Boot Record */ |
mkilivan | 5:c8918e47c566 | 2013 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 2014 | |
mkilivan | 5:c8918e47c566 | 2015 | static |
mkilivan | 5:c8918e47c566 | 2016 | BYTE check_fs ( /* 0:FAT-VBR, 1:Any BR but not FAT, 2:Not a BR, 3:Disk error */ |
mkilivan | 5:c8918e47c566 | 2017 | FATFS *fs, /* File system object */ |
mkilivan | 5:c8918e47c566 | 2018 | DWORD sect /* Sector# (lba) to check if it is an FAT boot record or not */ |
mkilivan | 5:c8918e47c566 | 2019 | ) |
mkilivan | 5:c8918e47c566 | 2020 | { |
mkilivan | 5:c8918e47c566 | 2021 | if (disk_read(fs->drv, fs->win, sect, 1) != RES_OK) /* Load boot record */ |
mkilivan | 5:c8918e47c566 | 2022 | return 3; |
mkilivan | 5:c8918e47c566 | 2023 | if (LD_WORD(&fs->win[BS_55AA]) != 0xAA55) /* Check record signature (always placed at offset 510 even if the sector size is >512) */ |
mkilivan | 5:c8918e47c566 | 2024 | return 2; |
mkilivan | 5:c8918e47c566 | 2025 | |
mkilivan | 5:c8918e47c566 | 2026 | if ((LD_DWORD(&fs->win[BS_FilSysType]) & 0xFFFFFF) == 0x544146) /* Check "FAT" string */ |
mkilivan | 5:c8918e47c566 | 2027 | return 0; |
mkilivan | 5:c8918e47c566 | 2028 | if ((LD_DWORD(&fs->win[BS_FilSysType32]) & 0xFFFFFF) == 0x544146) |
mkilivan | 5:c8918e47c566 | 2029 | return 0; |
mkilivan | 5:c8918e47c566 | 2030 | |
mkilivan | 5:c8918e47c566 | 2031 | return 1; |
mkilivan | 5:c8918e47c566 | 2032 | } |
mkilivan | 5:c8918e47c566 | 2033 | |
mkilivan | 5:c8918e47c566 | 2034 | |
mkilivan | 5:c8918e47c566 | 2035 | |
mkilivan | 5:c8918e47c566 | 2036 | |
mkilivan | 5:c8918e47c566 | 2037 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 2038 | /* Check if the file system object is valid or not */ |
mkilivan | 5:c8918e47c566 | 2039 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 2040 | |
mkilivan | 5:c8918e47c566 | 2041 | static |
mkilivan | 5:c8918e47c566 | 2042 | FRESULT chk_mounted ( /* FR_OK(0): successful, !=0: any error occurred */ |
mkilivan | 5:c8918e47c566 | 2043 | const TCHAR **path, /* Pointer to pointer to the path name (drive number) */ |
mkilivan | 5:c8918e47c566 | 2044 | FATFS **rfs, /* Pointer to pointer to the found file system object */ |
mkilivan | 5:c8918e47c566 | 2045 | BYTE wmode /* !=0: Check write protection for write access */ |
mkilivan | 5:c8918e47c566 | 2046 | ) |
mkilivan | 5:c8918e47c566 | 2047 | { |
mkilivan | 5:c8918e47c566 | 2048 | BYTE fmt, b, pi, *tbl; |
mkilivan | 5:c8918e47c566 | 2049 | UINT vol; |
mkilivan | 5:c8918e47c566 | 2050 | DSTATUS stat; |
mkilivan | 5:c8918e47c566 | 2051 | DWORD bsect, fasize, tsect, sysect, nclst, szbfat; |
mkilivan | 5:c8918e47c566 | 2052 | WORD nrsv; |
mkilivan | 5:c8918e47c566 | 2053 | const TCHAR *p = *path; |
mkilivan | 5:c8918e47c566 | 2054 | FATFS *fs; |
mkilivan | 5:c8918e47c566 | 2055 | |
mkilivan | 5:c8918e47c566 | 2056 | |
mkilivan | 5:c8918e47c566 | 2057 | /* Get logical drive number from the path name */ |
mkilivan | 5:c8918e47c566 | 2058 | vol = p[0] - '0'; /* Is there a drive number? */ |
mkilivan | 5:c8918e47c566 | 2059 | if (vol <= 9 && p[1] == ':') { /* Found a drive number, get and strip it */ |
mkilivan | 5:c8918e47c566 | 2060 | p += 2; *path = p; /* Return pointer to the path name */ |
mkilivan | 5:c8918e47c566 | 2061 | } else { /* No drive number is given */ |
mkilivan | 5:c8918e47c566 | 2062 | #if _FS_RPATH |
mkilivan | 5:c8918e47c566 | 2063 | vol = CurrVol; /* Use current drive */ |
mkilivan | 5:c8918e47c566 | 2064 | #else |
mkilivan | 5:c8918e47c566 | 2065 | vol = 0; /* Use drive 0 */ |
mkilivan | 5:c8918e47c566 | 2066 | #endif |
mkilivan | 5:c8918e47c566 | 2067 | } |
mkilivan | 5:c8918e47c566 | 2068 | |
mkilivan | 5:c8918e47c566 | 2069 | /* Check if the file system object is valid or not */ |
mkilivan | 5:c8918e47c566 | 2070 | *rfs = 0; |
mkilivan | 5:c8918e47c566 | 2071 | if (vol >= _VOLUMES) /* Is the drive number valid? */ |
mkilivan | 5:c8918e47c566 | 2072 | return FR_INVALID_DRIVE; |
mkilivan | 5:c8918e47c566 | 2073 | fs = FatFs[vol]; /* Get corresponding file system object */ |
mkilivan | 5:c8918e47c566 | 2074 | if (!fs) return FR_NOT_ENABLED; /* Is the file system object available? */ |
mkilivan | 5:c8918e47c566 | 2075 | |
mkilivan | 5:c8918e47c566 | 2076 | ENTER_FF(fs); /* Lock file system */ |
mkilivan | 5:c8918e47c566 | 2077 | |
mkilivan | 5:c8918e47c566 | 2078 | *rfs = fs; /* Return pointer to the corresponding file system object */ |
mkilivan | 5:c8918e47c566 | 2079 | if (fs->fs_type) { /* If the volume has been mounted */ |
mkilivan | 5:c8918e47c566 | 2080 | stat = disk_status(fs->drv); |
mkilivan | 5:c8918e47c566 | 2081 | if (!(stat & STA_NOINIT)) { /* and the physical drive is kept initialized (has not been changed), */ |
mkilivan | 5:c8918e47c566 | 2082 | if (!_FS_READONLY && wmode && (stat & STA_PROTECT)) /* Check write protection if needed */ |
mkilivan | 5:c8918e47c566 | 2083 | return FR_WRITE_PROTECTED; |
mkilivan | 5:c8918e47c566 | 2084 | return FR_OK; /* The file system object is valid */ |
mkilivan | 5:c8918e47c566 | 2085 | } |
mkilivan | 5:c8918e47c566 | 2086 | } |
mkilivan | 5:c8918e47c566 | 2087 | |
mkilivan | 5:c8918e47c566 | 2088 | /* The file system object is not valid. */ |
mkilivan | 5:c8918e47c566 | 2089 | /* Following code attempts to mount the volume. (analyze BPB and initialize the fs object) */ |
mkilivan | 5:c8918e47c566 | 2090 | |
mkilivan | 5:c8918e47c566 | 2091 | fs->fs_type = 0; /* Clear the file system object */ |
mkilivan | 5:c8918e47c566 | 2092 | fs->drv = LD2PD(vol); /* Bind the logical drive and a physical drive */ |
mkilivan | 5:c8918e47c566 | 2093 | stat = disk_initialize(fs->drv); /* Initialize the physical drive */ |
mkilivan | 5:c8918e47c566 | 2094 | if (stat & STA_NOINIT) /* Check if the initialization succeeded */ |
mkilivan | 5:c8918e47c566 | 2095 | return FR_NOT_READY; /* Failed to initialize due to no medium or hard error */ |
mkilivan | 5:c8918e47c566 | 2096 | if (!_FS_READONLY && wmode && (stat & STA_PROTECT)) /* Check disk write protection if needed */ |
mkilivan | 5:c8918e47c566 | 2097 | return FR_WRITE_PROTECTED; |
mkilivan | 5:c8918e47c566 | 2098 | #if _MAX_SS != 512 /* Get disk sector size (variable sector size cfg only) */ |
mkilivan | 5:c8918e47c566 | 2099 | if (disk_ioctl(fs->drv, GET_SECTOR_SIZE, &fs->ssize) != RES_OK) |
mkilivan | 5:c8918e47c566 | 2100 | return FR_DISK_ERR; |
mkilivan | 5:c8918e47c566 | 2101 | #endif |
mkilivan | 5:c8918e47c566 | 2102 | /* Search FAT partition on the drive. Supports only generic partitions, FDISK and SFD. */ |
mkilivan | 5:c8918e47c566 | 2103 | fmt = check_fs(fs, bsect = 0); /* Load sector 0 and check if it is an FAT-VBR (in SFD) */ |
mkilivan | 5:c8918e47c566 | 2104 | if (LD2PT(vol) && !fmt) fmt = 1; /* Force non-SFD if the volume is forced partition */ |
mkilivan | 5:c8918e47c566 | 2105 | if (fmt == 1) { /* Not an FAT-VBR, the physical drive can be partitioned */ |
mkilivan | 5:c8918e47c566 | 2106 | /* Check the partition listed in the partition table */ |
mkilivan | 5:c8918e47c566 | 2107 | pi = LD2PT(vol); |
mkilivan | 5:c8918e47c566 | 2108 | if (pi) pi--; |
mkilivan | 5:c8918e47c566 | 2109 | tbl = &fs->win[MBR_Table + pi * SZ_PTE];/* Partition table */ |
mkilivan | 5:c8918e47c566 | 2110 | if (tbl[4]) { /* Is the partition existing? */ |
mkilivan | 5:c8918e47c566 | 2111 | bsect = LD_DWORD(&tbl[8]); /* Partition offset in LBA */ |
mkilivan | 5:c8918e47c566 | 2112 | fmt = check_fs(fs, bsect); /* Check the partition */ |
mkilivan | 5:c8918e47c566 | 2113 | } |
mkilivan | 5:c8918e47c566 | 2114 | } |
mkilivan | 5:c8918e47c566 | 2115 | if (fmt == 3) return FR_DISK_ERR; |
mkilivan | 5:c8918e47c566 | 2116 | if (fmt) return FR_NO_FILESYSTEM; /* No FAT volume is found */ |
mkilivan | 5:c8918e47c566 | 2117 | |
mkilivan | 5:c8918e47c566 | 2118 | /* An FAT volume is found. Following code initializes the file system object */ |
mkilivan | 5:c8918e47c566 | 2119 | |
mkilivan | 5:c8918e47c566 | 2120 | if (LD_WORD(fs->win+BPB_BytsPerSec) != SS(fs)) /* (BPB_BytsPerSec must be equal to the physical sector size) */ |
mkilivan | 5:c8918e47c566 | 2121 | return FR_NO_FILESYSTEM; |
mkilivan | 5:c8918e47c566 | 2122 | |
mkilivan | 5:c8918e47c566 | 2123 | fasize = LD_WORD(fs->win+BPB_FATSz16); /* Number of sectors per FAT */ |
mkilivan | 5:c8918e47c566 | 2124 | if (!fasize) fasize = LD_DWORD(fs->win+BPB_FATSz32); |
mkilivan | 5:c8918e47c566 | 2125 | fs->fsize = fasize; |
mkilivan | 5:c8918e47c566 | 2126 | |
mkilivan | 5:c8918e47c566 | 2127 | fs->n_fats = b = fs->win[BPB_NumFATs]; /* Number of FAT copies */ |
mkilivan | 5:c8918e47c566 | 2128 | if (b != 1 && b != 2) return FR_NO_FILESYSTEM; /* (Must be 1 or 2) */ |
mkilivan | 5:c8918e47c566 | 2129 | fasize *= b; /* Number of sectors for FAT area */ |
mkilivan | 5:c8918e47c566 | 2130 | |
mkilivan | 5:c8918e47c566 | 2131 | fs->csize = b = fs->win[BPB_SecPerClus]; /* Number of sectors per cluster */ |
mkilivan | 5:c8918e47c566 | 2132 | if (!b || (b & (b - 1))) return FR_NO_FILESYSTEM; /* (Must be power of 2) */ |
mkilivan | 5:c8918e47c566 | 2133 | |
mkilivan | 5:c8918e47c566 | 2134 | fs->n_rootdir = LD_WORD(fs->win+BPB_RootEntCnt); /* Number of root directory entries */ |
mkilivan | 5:c8918e47c566 | 2135 | if (fs->n_rootdir % (SS(fs) / SZ_DIR)) return FR_NO_FILESYSTEM; /* (BPB_RootEntCnt must be sector aligned) */ |
mkilivan | 5:c8918e47c566 | 2136 | |
mkilivan | 5:c8918e47c566 | 2137 | tsect = LD_WORD(fs->win+BPB_TotSec16); /* Number of sectors on the volume */ |
mkilivan | 5:c8918e47c566 | 2138 | if (!tsect) tsect = LD_DWORD(fs->win+BPB_TotSec32); |
mkilivan | 5:c8918e47c566 | 2139 | |
mkilivan | 5:c8918e47c566 | 2140 | nrsv = LD_WORD(fs->win+BPB_RsvdSecCnt); /* Number of reserved sectors */ |
mkilivan | 5:c8918e47c566 | 2141 | if (!nrsv) return FR_NO_FILESYSTEM; /* (BPB_RsvdSecCnt must not be 0) */ |
mkilivan | 5:c8918e47c566 | 2142 | |
mkilivan | 5:c8918e47c566 | 2143 | /* Determine the FAT sub type */ |
mkilivan | 5:c8918e47c566 | 2144 | sysect = nrsv + fasize + fs->n_rootdir / (SS(fs) / SZ_DIR); /* RSV+FAT+DIR */ |
mkilivan | 5:c8918e47c566 | 2145 | if (tsect < sysect) return FR_NO_FILESYSTEM; /* (Invalid volume size) */ |
mkilivan | 5:c8918e47c566 | 2146 | nclst = (tsect - sysect) / fs->csize; /* Number of clusters */ |
mkilivan | 5:c8918e47c566 | 2147 | if (!nclst) return FR_NO_FILESYSTEM; /* (Invalid volume size) */ |
mkilivan | 5:c8918e47c566 | 2148 | fmt = FS_FAT12; |
mkilivan | 5:c8918e47c566 | 2149 | if (nclst >= MIN_FAT16) fmt = FS_FAT16; |
mkilivan | 5:c8918e47c566 | 2150 | if (nclst >= MIN_FAT32) fmt = FS_FAT32; |
mkilivan | 5:c8918e47c566 | 2151 | |
mkilivan | 5:c8918e47c566 | 2152 | /* Boundaries and Limits */ |
mkilivan | 5:c8918e47c566 | 2153 | fs->n_fatent = nclst + 2; /* Number of FAT entries */ |
mkilivan | 5:c8918e47c566 | 2154 | fs->database = bsect + sysect; /* Data start sector */ |
mkilivan | 5:c8918e47c566 | 2155 | fs->fatbase = bsect + nrsv; /* FAT start sector */ |
mkilivan | 5:c8918e47c566 | 2156 | if (fmt == FS_FAT32) { |
mkilivan | 5:c8918e47c566 | 2157 | if (fs->n_rootdir) return FR_NO_FILESYSTEM; /* (BPB_RootEntCnt must be 0) */ |
mkilivan | 5:c8918e47c566 | 2158 | fs->dirbase = LD_DWORD(fs->win+BPB_RootClus); /* Root directory start cluster */ |
mkilivan | 5:c8918e47c566 | 2159 | szbfat = fs->n_fatent * 4; /* (Required FAT size) */ |
mkilivan | 5:c8918e47c566 | 2160 | } else { |
mkilivan | 5:c8918e47c566 | 2161 | if (!fs->n_rootdir) return FR_NO_FILESYSTEM; /* (BPB_RootEntCnt must not be 0) */ |
mkilivan | 5:c8918e47c566 | 2162 | fs->dirbase = fs->fatbase + fasize; /* Root directory start sector */ |
mkilivan | 5:c8918e47c566 | 2163 | szbfat = (fmt == FS_FAT16) ? /* (Required FAT size) */ |
mkilivan | 5:c8918e47c566 | 2164 | fs->n_fatent * 2 : fs->n_fatent * 3 / 2 + (fs->n_fatent & 1); |
mkilivan | 5:c8918e47c566 | 2165 | } |
mkilivan | 5:c8918e47c566 | 2166 | if (fs->fsize < (szbfat + (SS(fs) - 1)) / SS(fs)) /* (BPB_FATSz must not be less than required) */ |
mkilivan | 5:c8918e47c566 | 2167 | return FR_NO_FILESYSTEM; |
mkilivan | 5:c8918e47c566 | 2168 | |
mkilivan | 5:c8918e47c566 | 2169 | #if !_FS_READONLY |
mkilivan | 5:c8918e47c566 | 2170 | /* Initialize cluster allocation information */ |
mkilivan | 5:c8918e47c566 | 2171 | fs->free_clust = 0xFFFFFFFF; |
mkilivan | 5:c8918e47c566 | 2172 | fs->last_clust = 0; |
mkilivan | 5:c8918e47c566 | 2173 | |
mkilivan | 5:c8918e47c566 | 2174 | /* Get fsinfo if available */ |
mkilivan | 5:c8918e47c566 | 2175 | if (fmt == FS_FAT32) { |
mkilivan | 5:c8918e47c566 | 2176 | fs->fsi_flag = 0; |
mkilivan | 5:c8918e47c566 | 2177 | fs->fsi_sector = bsect + LD_WORD(fs->win+BPB_FSInfo); |
mkilivan | 5:c8918e47c566 | 2178 | if (disk_read(fs->drv, fs->win, fs->fsi_sector, 1) == RES_OK && |
mkilivan | 5:c8918e47c566 | 2179 | LD_WORD(fs->win+BS_55AA) == 0xAA55 && |
mkilivan | 5:c8918e47c566 | 2180 | LD_DWORD(fs->win+FSI_LeadSig) == 0x41615252 && |
mkilivan | 5:c8918e47c566 | 2181 | LD_DWORD(fs->win+FSI_StrucSig) == 0x61417272) { |
mkilivan | 5:c8918e47c566 | 2182 | fs->last_clust = LD_DWORD(fs->win+FSI_Nxt_Free); |
mkilivan | 5:c8918e47c566 | 2183 | fs->free_clust = LD_DWORD(fs->win+FSI_Free_Count); |
mkilivan | 5:c8918e47c566 | 2184 | } |
mkilivan | 5:c8918e47c566 | 2185 | } |
mkilivan | 5:c8918e47c566 | 2186 | #endif |
mkilivan | 5:c8918e47c566 | 2187 | fs->fs_type = fmt; /* FAT sub-type */ |
mkilivan | 5:c8918e47c566 | 2188 | fs->id = ++Fsid; /* File system mount ID */ |
mkilivan | 5:c8918e47c566 | 2189 | fs->winsect = 0; /* Invalidate sector cache */ |
mkilivan | 5:c8918e47c566 | 2190 | fs->wflag = 0; |
mkilivan | 5:c8918e47c566 | 2191 | #if _FS_RPATH |
mkilivan | 5:c8918e47c566 | 2192 | fs->cdir = 0; /* Current directory (root dir) */ |
mkilivan | 5:c8918e47c566 | 2193 | #endif |
mkilivan | 5:c8918e47c566 | 2194 | #if _FS_LOCK /* Clear file lock semaphores */ |
mkilivan | 5:c8918e47c566 | 2195 | clear_lock(fs); |
mkilivan | 5:c8918e47c566 | 2196 | #endif |
mkilivan | 5:c8918e47c566 | 2197 | |
mkilivan | 5:c8918e47c566 | 2198 | return FR_OK; |
mkilivan | 5:c8918e47c566 | 2199 | } |
mkilivan | 5:c8918e47c566 | 2200 | |
mkilivan | 5:c8918e47c566 | 2201 | |
mkilivan | 5:c8918e47c566 | 2202 | |
mkilivan | 5:c8918e47c566 | 2203 | |
mkilivan | 5:c8918e47c566 | 2204 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 2205 | /* Check if the file/dir object is valid or not */ |
mkilivan | 5:c8918e47c566 | 2206 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 2207 | |
mkilivan | 5:c8918e47c566 | 2208 | static |
mkilivan | 5:c8918e47c566 | 2209 | FRESULT validate ( /* FR_OK(0): The object is valid, !=0: Invalid */ |
mkilivan | 5:c8918e47c566 | 2210 | void* obj /* Pointer to the object FIL/DIR to check validity */ |
mkilivan | 5:c8918e47c566 | 2211 | ) |
mkilivan | 5:c8918e47c566 | 2212 | { |
mkilivan | 5:c8918e47c566 | 2213 | FIL *fil; |
mkilivan | 5:c8918e47c566 | 2214 | |
mkilivan | 5:c8918e47c566 | 2215 | |
mkilivan | 5:c8918e47c566 | 2216 | fil = (FIL*)obj; /* Assuming offset of fs and id in the FIL/DIR is identical */ |
mkilivan | 5:c8918e47c566 | 2217 | if (!fil->fs || !fil->fs->fs_type || fil->fs->id != fil->id) |
mkilivan | 5:c8918e47c566 | 2218 | return FR_INVALID_OBJECT; |
mkilivan | 5:c8918e47c566 | 2219 | |
mkilivan | 5:c8918e47c566 | 2220 | ENTER_FF(fil->fs); /* Lock file system */ |
mkilivan | 5:c8918e47c566 | 2221 | |
mkilivan | 5:c8918e47c566 | 2222 | if (disk_status(fil->fs->drv) & STA_NOINIT) |
mkilivan | 5:c8918e47c566 | 2223 | return FR_NOT_READY; |
mkilivan | 5:c8918e47c566 | 2224 | |
mkilivan | 5:c8918e47c566 | 2225 | return FR_OK; |
mkilivan | 5:c8918e47c566 | 2226 | } |
mkilivan | 5:c8918e47c566 | 2227 | |
mkilivan | 5:c8918e47c566 | 2228 | |
mkilivan | 5:c8918e47c566 | 2229 | |
mkilivan | 5:c8918e47c566 | 2230 | |
mkilivan | 5:c8918e47c566 | 2231 | /*-------------------------------------------------------------------------- |
mkilivan | 5:c8918e47c566 | 2232 | |
mkilivan | 5:c8918e47c566 | 2233 | Public Functions |
mkilivan | 5:c8918e47c566 | 2234 | |
mkilivan | 5:c8918e47c566 | 2235 | --------------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 2236 | |
mkilivan | 5:c8918e47c566 | 2237 | |
mkilivan | 5:c8918e47c566 | 2238 | |
mkilivan | 5:c8918e47c566 | 2239 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 2240 | /* Mount/Unmount a Logical Drive */ |
mkilivan | 5:c8918e47c566 | 2241 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 2242 | |
mkilivan | 5:c8918e47c566 | 2243 | FRESULT f_mount ( |
mkilivan | 5:c8918e47c566 | 2244 | BYTE vol, /* Logical drive number to be mounted/unmounted */ |
mkilivan | 5:c8918e47c566 | 2245 | FATFS *fs /* Pointer to new file system object (NULL for unmount)*/ |
mkilivan | 5:c8918e47c566 | 2246 | ) |
mkilivan | 5:c8918e47c566 | 2247 | { |
mkilivan | 5:c8918e47c566 | 2248 | FATFS *rfs; |
mkilivan | 5:c8918e47c566 | 2249 | |
mkilivan | 5:c8918e47c566 | 2250 | |
mkilivan | 5:c8918e47c566 | 2251 | if (vol >= _VOLUMES) /* Check if the drive number is valid */ |
mkilivan | 5:c8918e47c566 | 2252 | return FR_INVALID_DRIVE; |
mkilivan | 5:c8918e47c566 | 2253 | rfs = FatFs[vol]; /* Get current fs object */ |
mkilivan | 5:c8918e47c566 | 2254 | |
mkilivan | 5:c8918e47c566 | 2255 | if (rfs) { |
mkilivan | 5:c8918e47c566 | 2256 | #if _FS_LOCK |
mkilivan | 5:c8918e47c566 | 2257 | clear_lock(rfs); |
mkilivan | 5:c8918e47c566 | 2258 | #endif |
mkilivan | 5:c8918e47c566 | 2259 | #if _FS_REENTRANT /* Discard sync object of the current volume */ |
mkilivan | 5:c8918e47c566 | 2260 | if (!ff_del_syncobj(rfs->sobj)) return FR_INT_ERR; |
mkilivan | 5:c8918e47c566 | 2261 | #endif |
mkilivan | 5:c8918e47c566 | 2262 | rfs->fs_type = 0; /* Clear old fs object */ |
mkilivan | 5:c8918e47c566 | 2263 | } |
mkilivan | 5:c8918e47c566 | 2264 | |
mkilivan | 5:c8918e47c566 | 2265 | if (fs) { |
mkilivan | 5:c8918e47c566 | 2266 | fs->fs_type = 0; /* Clear new fs object */ |
mkilivan | 5:c8918e47c566 | 2267 | #if _FS_REENTRANT /* Create sync object for the new volume */ |
mkilivan | 5:c8918e47c566 | 2268 | if (!ff_cre_syncobj(vol, &fs->sobj)) return FR_INT_ERR; |
mkilivan | 5:c8918e47c566 | 2269 | #endif |
mkilivan | 5:c8918e47c566 | 2270 | } |
mkilivan | 5:c8918e47c566 | 2271 | FatFs[vol] = fs; /* Register new fs object */ |
mkilivan | 5:c8918e47c566 | 2272 | |
mkilivan | 5:c8918e47c566 | 2273 | return FR_OK; |
mkilivan | 5:c8918e47c566 | 2274 | } |
mkilivan | 5:c8918e47c566 | 2275 | |
mkilivan | 5:c8918e47c566 | 2276 | |
mkilivan | 5:c8918e47c566 | 2277 | |
mkilivan | 5:c8918e47c566 | 2278 | |
mkilivan | 5:c8918e47c566 | 2279 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 2280 | /* Open or Create a File */ |
mkilivan | 5:c8918e47c566 | 2281 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 2282 | |
mkilivan | 5:c8918e47c566 | 2283 | FRESULT f_open ( |
mkilivan | 5:c8918e47c566 | 2284 | FIL *fp, /* Pointer to the blank file object */ |
mkilivan | 5:c8918e47c566 | 2285 | const TCHAR *path, /* Pointer to the file name */ |
mkilivan | 5:c8918e47c566 | 2286 | BYTE mode /* Access mode and file open mode flags */ |
mkilivan | 5:c8918e47c566 | 2287 | ) |
mkilivan | 5:c8918e47c566 | 2288 | { |
mkilivan | 5:c8918e47c566 | 2289 | FRESULT res; |
mkilivan | 5:c8918e47c566 | 2290 | FATFS_DIR dj; |
mkilivan | 5:c8918e47c566 | 2291 | BYTE *dir; |
mkilivan | 5:c8918e47c566 | 2292 | DEF_NAMEBUF; |
mkilivan | 5:c8918e47c566 | 2293 | |
mkilivan | 5:c8918e47c566 | 2294 | |
mkilivan | 5:c8918e47c566 | 2295 | if (!fp) return FR_INVALID_OBJECT; |
mkilivan | 5:c8918e47c566 | 2296 | fp->fs = 0; /* Clear file object */ |
mkilivan | 5:c8918e47c566 | 2297 | |
mkilivan | 5:c8918e47c566 | 2298 | #if !_FS_READONLY |
mkilivan | 5:c8918e47c566 | 2299 | mode &= FA_READ | FA_WRITE | FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW; |
mkilivan | 5:c8918e47c566 | 2300 | res = chk_mounted(&path, &dj.fs, (BYTE)(mode & ~FA_READ)); |
mkilivan | 5:c8918e47c566 | 2301 | #else |
mkilivan | 5:c8918e47c566 | 2302 | mode &= FA_READ; |
mkilivan | 5:c8918e47c566 | 2303 | res = chk_mounted(&path, &dj.fs, 0); |
mkilivan | 5:c8918e47c566 | 2304 | #endif |
mkilivan | 5:c8918e47c566 | 2305 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 2306 | INIT_BUF(dj); |
mkilivan | 5:c8918e47c566 | 2307 | res = follow_path(&dj, path); /* Follow the file path */ |
mkilivan | 5:c8918e47c566 | 2308 | dir = dj.dir; |
mkilivan | 5:c8918e47c566 | 2309 | #if !_FS_READONLY /* R/W configuration */ |
mkilivan | 5:c8918e47c566 | 2310 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 2311 | if (!dir) /* Current dir itself */ |
mkilivan | 5:c8918e47c566 | 2312 | res = FR_INVALID_NAME; |
mkilivan | 5:c8918e47c566 | 2313 | #if _FS_LOCK |
mkilivan | 5:c8918e47c566 | 2314 | else |
mkilivan | 5:c8918e47c566 | 2315 | res = chk_lock(&dj, (mode & ~FA_READ) ? 1 : 0); |
mkilivan | 5:c8918e47c566 | 2316 | #endif |
mkilivan | 5:c8918e47c566 | 2317 | } |
mkilivan | 5:c8918e47c566 | 2318 | /* Create or Open a file */ |
mkilivan | 5:c8918e47c566 | 2319 | if (mode & (FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW)) { |
mkilivan | 5:c8918e47c566 | 2320 | DWORD dw, cl; |
mkilivan | 5:c8918e47c566 | 2321 | |
mkilivan | 5:c8918e47c566 | 2322 | if (res != FR_OK) { /* No file, create new */ |
mkilivan | 5:c8918e47c566 | 2323 | if (res == FR_NO_FILE) /* There is no file to open, create a new entry */ |
mkilivan | 5:c8918e47c566 | 2324 | #if _FS_LOCK |
mkilivan | 5:c8918e47c566 | 2325 | res = enq_lock() ? dir_register(&dj) : FR_TOO_MANY_OPEN_FILES; |
mkilivan | 5:c8918e47c566 | 2326 | #else |
mkilivan | 5:c8918e47c566 | 2327 | res = dir_register(&dj); |
mkilivan | 5:c8918e47c566 | 2328 | #endif |
mkilivan | 5:c8918e47c566 | 2329 | mode |= FA_CREATE_ALWAYS; /* File is created */ |
mkilivan | 5:c8918e47c566 | 2330 | dir = dj.dir; /* New entry */ |
mkilivan | 5:c8918e47c566 | 2331 | } |
mkilivan | 5:c8918e47c566 | 2332 | else { /* Any object is already existing */ |
mkilivan | 5:c8918e47c566 | 2333 | if (dir[DIR_Attr] & (AM_RDO | AM_DIR)) { /* Cannot overwrite it (R/O or DIR) */ |
mkilivan | 5:c8918e47c566 | 2334 | res = FR_DENIED; |
mkilivan | 5:c8918e47c566 | 2335 | } else { |
mkilivan | 5:c8918e47c566 | 2336 | if (mode & FA_CREATE_NEW) /* Cannot create as new file */ |
mkilivan | 5:c8918e47c566 | 2337 | res = FR_EXIST; |
mkilivan | 5:c8918e47c566 | 2338 | } |
mkilivan | 5:c8918e47c566 | 2339 | } |
mkilivan | 5:c8918e47c566 | 2340 | if (res == FR_OK && (mode & FA_CREATE_ALWAYS)) { /* Truncate it if overwrite mode */ |
mkilivan | 5:c8918e47c566 | 2341 | dw = get_fattime(); /* Created time */ |
mkilivan | 5:c8918e47c566 | 2342 | ST_DWORD(dir+DIR_CrtTime, dw); |
mkilivan | 5:c8918e47c566 | 2343 | dir[DIR_Attr] = 0; /* Reset attribute */ |
mkilivan | 5:c8918e47c566 | 2344 | ST_DWORD(dir+DIR_FileSize, 0); /* size = 0 */ |
mkilivan | 5:c8918e47c566 | 2345 | cl = ld_clust(dj.fs, dir); /* Get start cluster */ |
mkilivan | 5:c8918e47c566 | 2346 | st_clust(dir, 0); /* cluster = 0 */ |
mkilivan | 5:c8918e47c566 | 2347 | dj.fs->wflag = 1; |
mkilivan | 5:c8918e47c566 | 2348 | if (cl) { /* Remove the cluster chain if exist */ |
mkilivan | 5:c8918e47c566 | 2349 | dw = dj.fs->winsect; |
mkilivan | 5:c8918e47c566 | 2350 | res = remove_chain(dj.fs, cl); |
mkilivan | 5:c8918e47c566 | 2351 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 2352 | dj.fs->last_clust = cl - 1; /* Reuse the cluster hole */ |
mkilivan | 5:c8918e47c566 | 2353 | res = move_window(dj.fs, dw); |
mkilivan | 5:c8918e47c566 | 2354 | } |
mkilivan | 5:c8918e47c566 | 2355 | } |
mkilivan | 5:c8918e47c566 | 2356 | } |
mkilivan | 5:c8918e47c566 | 2357 | } |
mkilivan | 5:c8918e47c566 | 2358 | else { /* Open an existing file */ |
mkilivan | 5:c8918e47c566 | 2359 | if (res == FR_OK) { /* Follow succeeded */ |
mkilivan | 5:c8918e47c566 | 2360 | if (dir[DIR_Attr] & AM_DIR) { /* It is a directory */ |
mkilivan | 5:c8918e47c566 | 2361 | res = FR_NO_FILE; |
mkilivan | 5:c8918e47c566 | 2362 | } else { |
mkilivan | 5:c8918e47c566 | 2363 | if ((mode & FA_WRITE) && (dir[DIR_Attr] & AM_RDO)) /* R/O violation */ |
mkilivan | 5:c8918e47c566 | 2364 | res = FR_DENIED; |
mkilivan | 5:c8918e47c566 | 2365 | } |
mkilivan | 5:c8918e47c566 | 2366 | } |
mkilivan | 5:c8918e47c566 | 2367 | } |
mkilivan | 5:c8918e47c566 | 2368 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 2369 | if (mode & FA_CREATE_ALWAYS) /* Set file change flag if created or overwritten */ |
mkilivan | 5:c8918e47c566 | 2370 | mode |= FA__WRITTEN; |
mkilivan | 5:c8918e47c566 | 2371 | fp->dir_sect = dj.fs->winsect; /* Pointer to the directory entry */ |
mkilivan | 5:c8918e47c566 | 2372 | fp->dir_ptr = dir; |
mkilivan | 5:c8918e47c566 | 2373 | #if _FS_LOCK |
mkilivan | 5:c8918e47c566 | 2374 | fp->lockid = inc_lock(&dj, (mode & ~FA_READ) ? 1 : 0); |
mkilivan | 5:c8918e47c566 | 2375 | if (!fp->lockid) res = FR_INT_ERR; |
mkilivan | 5:c8918e47c566 | 2376 | #endif |
mkilivan | 5:c8918e47c566 | 2377 | } |
mkilivan | 5:c8918e47c566 | 2378 | |
mkilivan | 5:c8918e47c566 | 2379 | #else /* R/O configuration */ |
mkilivan | 5:c8918e47c566 | 2380 | if (res == FR_OK) { /* Follow succeeded */ |
mkilivan | 5:c8918e47c566 | 2381 | dir = dj.dir; |
mkilivan | 5:c8918e47c566 | 2382 | if (!dir) { /* Current dir itself */ |
mkilivan | 5:c8918e47c566 | 2383 | res = FR_INVALID_NAME; |
mkilivan | 5:c8918e47c566 | 2384 | } else { |
mkilivan | 5:c8918e47c566 | 2385 | if (dir[DIR_Attr] & AM_DIR) /* It is a directory */ |
mkilivan | 5:c8918e47c566 | 2386 | res = FR_NO_FILE; |
mkilivan | 5:c8918e47c566 | 2387 | } |
mkilivan | 5:c8918e47c566 | 2388 | } |
mkilivan | 5:c8918e47c566 | 2389 | #endif |
mkilivan | 5:c8918e47c566 | 2390 | FREE_BUF(); |
mkilivan | 5:c8918e47c566 | 2391 | |
mkilivan | 5:c8918e47c566 | 2392 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 2393 | fp->flag = mode; /* File access mode */ |
mkilivan | 5:c8918e47c566 | 2394 | fp->sclust = ld_clust(dj.fs, dir); /* File start cluster */ |
mkilivan | 5:c8918e47c566 | 2395 | fp->fsize = LD_DWORD(dir+DIR_FileSize); /* File size */ |
mkilivan | 5:c8918e47c566 | 2396 | fp->fptr = 0; /* File pointer */ |
mkilivan | 5:c8918e47c566 | 2397 | fp->dsect = 0; |
mkilivan | 5:c8918e47c566 | 2398 | #if _USE_FASTSEEK |
mkilivan | 5:c8918e47c566 | 2399 | fp->cltbl = 0; /* Normal seek mode */ |
mkilivan | 5:c8918e47c566 | 2400 | #endif |
mkilivan | 5:c8918e47c566 | 2401 | fp->fs = dj.fs; fp->id = dj.fs->id; /* Validate file object */ |
mkilivan | 5:c8918e47c566 | 2402 | } |
mkilivan | 5:c8918e47c566 | 2403 | } |
mkilivan | 5:c8918e47c566 | 2404 | |
mkilivan | 5:c8918e47c566 | 2405 | LEAVE_FF(dj.fs, res); |
mkilivan | 5:c8918e47c566 | 2406 | } |
mkilivan | 5:c8918e47c566 | 2407 | |
mkilivan | 5:c8918e47c566 | 2408 | |
mkilivan | 5:c8918e47c566 | 2409 | |
mkilivan | 5:c8918e47c566 | 2410 | |
mkilivan | 5:c8918e47c566 | 2411 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 2412 | /* Read File */ |
mkilivan | 5:c8918e47c566 | 2413 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 2414 | |
mkilivan | 5:c8918e47c566 | 2415 | FRESULT f_read ( |
mkilivan | 5:c8918e47c566 | 2416 | FIL *fp, /* Pointer to the file object */ |
mkilivan | 5:c8918e47c566 | 2417 | void *buff, /* Pointer to data buffer */ |
mkilivan | 5:c8918e47c566 | 2418 | UINT btr, /* Number of bytes to read */ |
mkilivan | 5:c8918e47c566 | 2419 | UINT *br /* Pointer to number of bytes read */ |
mkilivan | 5:c8918e47c566 | 2420 | ) |
mkilivan | 5:c8918e47c566 | 2421 | { |
mkilivan | 5:c8918e47c566 | 2422 | FRESULT res; |
mkilivan | 5:c8918e47c566 | 2423 | DWORD clst, sect, remain; |
mkilivan | 5:c8918e47c566 | 2424 | UINT rcnt, cc; |
mkilivan | 5:c8918e47c566 | 2425 | BYTE csect, *rbuff = (BYTE *)buff; |
mkilivan | 5:c8918e47c566 | 2426 | |
mkilivan | 5:c8918e47c566 | 2427 | |
mkilivan | 5:c8918e47c566 | 2428 | *br = 0; /* Clear read byte counter */ |
mkilivan | 5:c8918e47c566 | 2429 | |
mkilivan | 5:c8918e47c566 | 2430 | res = validate(fp); /* Check validity */ |
mkilivan | 5:c8918e47c566 | 2431 | if (res != FR_OK) LEAVE_FF(fp->fs, res); |
mkilivan | 5:c8918e47c566 | 2432 | if (fp->flag & FA__ERROR) /* Aborted file? */ |
mkilivan | 5:c8918e47c566 | 2433 | LEAVE_FF(fp->fs, FR_INT_ERR); |
mkilivan | 5:c8918e47c566 | 2434 | if (!(fp->flag & FA_READ)) /* Check access mode */ |
mkilivan | 5:c8918e47c566 | 2435 | LEAVE_FF(fp->fs, FR_DENIED); |
mkilivan | 5:c8918e47c566 | 2436 | remain = fp->fsize - fp->fptr; |
mkilivan | 5:c8918e47c566 | 2437 | if (btr > remain) btr = (UINT)remain; /* Truncate btr by remaining bytes */ |
mkilivan | 5:c8918e47c566 | 2438 | |
mkilivan | 5:c8918e47c566 | 2439 | for ( ; btr; /* Repeat until all data read */ |
mkilivan | 5:c8918e47c566 | 2440 | rbuff += rcnt, fp->fptr += rcnt, *br += rcnt, btr -= rcnt) { |
mkilivan | 5:c8918e47c566 | 2441 | if ((fp->fptr % SS(fp->fs)) == 0) { /* On the sector boundary? */ |
mkilivan | 5:c8918e47c566 | 2442 | csect = (BYTE)(fp->fptr / SS(fp->fs) & (fp->fs->csize - 1)); /* Sector offset in the cluster */ |
mkilivan | 5:c8918e47c566 | 2443 | if (!csect) { /* On the cluster boundary? */ |
mkilivan | 5:c8918e47c566 | 2444 | if (fp->fptr == 0) { /* On the top of the file? */ |
mkilivan | 5:c8918e47c566 | 2445 | clst = fp->sclust; /* Follow from the origin */ |
mkilivan | 5:c8918e47c566 | 2446 | } else { /* Middle or end of the file */ |
mkilivan | 5:c8918e47c566 | 2447 | #if _USE_FASTSEEK |
mkilivan | 5:c8918e47c566 | 2448 | if (fp->cltbl) |
mkilivan | 5:c8918e47c566 | 2449 | clst = clmt_clust(fp, fp->fptr); /* Get cluster# from the CLMT */ |
mkilivan | 5:c8918e47c566 | 2450 | else |
mkilivan | 5:c8918e47c566 | 2451 | #endif |
mkilivan | 5:c8918e47c566 | 2452 | clst = get_fat(fp->fs, fp->clust); /* Follow cluster chain on the FAT */ |
mkilivan | 5:c8918e47c566 | 2453 | } |
mkilivan | 5:c8918e47c566 | 2454 | if (clst < 2) ABORT(fp->fs, FR_INT_ERR); |
mkilivan | 5:c8918e47c566 | 2455 | if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR); |
mkilivan | 5:c8918e47c566 | 2456 | fp->clust = clst; /* Update current cluster */ |
mkilivan | 5:c8918e47c566 | 2457 | } |
mkilivan | 5:c8918e47c566 | 2458 | sect = clust2sect(fp->fs, fp->clust); /* Get current sector */ |
mkilivan | 5:c8918e47c566 | 2459 | if (!sect) ABORT(fp->fs, FR_INT_ERR); |
mkilivan | 5:c8918e47c566 | 2460 | sect += csect; |
mkilivan | 5:c8918e47c566 | 2461 | cc = btr / SS(fp->fs); /* When remaining bytes >= sector size, */ |
mkilivan | 5:c8918e47c566 | 2462 | if (cc) { /* Read maximum contiguous sectors directly */ |
mkilivan | 5:c8918e47c566 | 2463 | if (csect + cc > fp->fs->csize) /* Clip at cluster boundary */ |
mkilivan | 5:c8918e47c566 | 2464 | cc = fp->fs->csize - csect; |
mkilivan | 5:c8918e47c566 | 2465 | if (disk_read(fp->fs->drv, rbuff, sect, (BYTE)cc) != RES_OK) |
mkilivan | 5:c8918e47c566 | 2466 | ABORT(fp->fs, FR_DISK_ERR); |
mkilivan | 5:c8918e47c566 | 2467 | #if !_FS_READONLY && _FS_MINIMIZE <= 2 /* Replace one of the read sectors with cached data if it contains a dirty sector */ |
mkilivan | 5:c8918e47c566 | 2468 | #if _FS_TINY |
mkilivan | 5:c8918e47c566 | 2469 | if (fp->fs->wflag && fp->fs->winsect - sect < cc) |
mkilivan | 5:c8918e47c566 | 2470 | mem_cpy(rbuff + ((fp->fs->winsect - sect) * SS(fp->fs)), fp->fs->win, SS(fp->fs)); |
mkilivan | 5:c8918e47c566 | 2471 | #else |
mkilivan | 5:c8918e47c566 | 2472 | if ((fp->flag & FA__DIRTY) && fp->dsect - sect < cc) |
mkilivan | 5:c8918e47c566 | 2473 | mem_cpy(rbuff + ((fp->dsect - sect) * SS(fp->fs)), fp->buf, SS(fp->fs)); |
mkilivan | 5:c8918e47c566 | 2474 | #endif |
mkilivan | 5:c8918e47c566 | 2475 | #endif |
mkilivan | 5:c8918e47c566 | 2476 | rcnt = SS(fp->fs) * cc; /* Number of bytes transferred */ |
mkilivan | 5:c8918e47c566 | 2477 | continue; |
mkilivan | 5:c8918e47c566 | 2478 | } |
mkilivan | 5:c8918e47c566 | 2479 | #if !_FS_TINY |
mkilivan | 5:c8918e47c566 | 2480 | if (fp->dsect != sect) { /* Load data sector if not in cache */ |
mkilivan | 5:c8918e47c566 | 2481 | #if !_FS_READONLY |
mkilivan | 5:c8918e47c566 | 2482 | if (fp->flag & FA__DIRTY) { /* Write-back dirty sector cache */ |
mkilivan | 5:c8918e47c566 | 2483 | if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK) |
mkilivan | 5:c8918e47c566 | 2484 | ABORT(fp->fs, FR_DISK_ERR); |
mkilivan | 5:c8918e47c566 | 2485 | fp->flag &= ~FA__DIRTY; |
mkilivan | 5:c8918e47c566 | 2486 | } |
mkilivan | 5:c8918e47c566 | 2487 | #endif |
mkilivan | 5:c8918e47c566 | 2488 | if (disk_read(fp->fs->drv, fp->buf, sect, 1) != RES_OK) /* Fill sector cache */ |
mkilivan | 5:c8918e47c566 | 2489 | ABORT(fp->fs, FR_DISK_ERR); |
mkilivan | 5:c8918e47c566 | 2490 | } |
mkilivan | 5:c8918e47c566 | 2491 | #endif |
mkilivan | 5:c8918e47c566 | 2492 | fp->dsect = sect; |
mkilivan | 5:c8918e47c566 | 2493 | } |
mkilivan | 5:c8918e47c566 | 2494 | rcnt = SS(fp->fs) - ((UINT)fp->fptr % SS(fp->fs)); /* Get partial sector data from sector buffer */ |
mkilivan | 5:c8918e47c566 | 2495 | if (rcnt > btr) rcnt = btr; |
mkilivan | 5:c8918e47c566 | 2496 | #if _FS_TINY |
mkilivan | 5:c8918e47c566 | 2497 | if (move_window(fp->fs, fp->dsect)) /* Move sector window */ |
mkilivan | 5:c8918e47c566 | 2498 | ABORT(fp->fs, FR_DISK_ERR); |
mkilivan | 5:c8918e47c566 | 2499 | mem_cpy(rbuff, &fp->fs->win[fp->fptr % SS(fp->fs)], rcnt); /* Pick partial sector */ |
mkilivan | 5:c8918e47c566 | 2500 | #else |
mkilivan | 5:c8918e47c566 | 2501 | mem_cpy(rbuff, &fp->buf[fp->fptr % SS(fp->fs)], rcnt); /* Pick partial sector */ |
mkilivan | 5:c8918e47c566 | 2502 | #endif |
mkilivan | 5:c8918e47c566 | 2503 | } |
mkilivan | 5:c8918e47c566 | 2504 | |
mkilivan | 5:c8918e47c566 | 2505 | LEAVE_FF(fp->fs, FR_OK); |
mkilivan | 5:c8918e47c566 | 2506 | } |
mkilivan | 5:c8918e47c566 | 2507 | |
mkilivan | 5:c8918e47c566 | 2508 | |
mkilivan | 5:c8918e47c566 | 2509 | |
mkilivan | 5:c8918e47c566 | 2510 | |
mkilivan | 5:c8918e47c566 | 2511 | #if !_FS_READONLY |
mkilivan | 5:c8918e47c566 | 2512 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 2513 | /* Write File */ |
mkilivan | 5:c8918e47c566 | 2514 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 2515 | |
mkilivan | 5:c8918e47c566 | 2516 | FRESULT f_write ( |
mkilivan | 5:c8918e47c566 | 2517 | FIL *fp, /* Pointer to the file object */ |
mkilivan | 5:c8918e47c566 | 2518 | const void *buff, /* Pointer to the data to be written */ |
mkilivan | 5:c8918e47c566 | 2519 | UINT btw, /* Number of bytes to write */ |
mkilivan | 5:c8918e47c566 | 2520 | UINT *bw /* Pointer to number of bytes written */ |
mkilivan | 5:c8918e47c566 | 2521 | ) |
mkilivan | 5:c8918e47c566 | 2522 | { |
mkilivan | 5:c8918e47c566 | 2523 | FRESULT res; |
mkilivan | 5:c8918e47c566 | 2524 | DWORD clst, sect; |
mkilivan | 5:c8918e47c566 | 2525 | UINT wcnt, cc; |
mkilivan | 5:c8918e47c566 | 2526 | const BYTE *wbuff = (const BYTE *)buff; |
mkilivan | 5:c8918e47c566 | 2527 | BYTE csect; |
mkilivan | 5:c8918e47c566 | 2528 | |
mkilivan | 5:c8918e47c566 | 2529 | |
mkilivan | 5:c8918e47c566 | 2530 | *bw = 0; /* Clear write byte counter */ |
mkilivan | 5:c8918e47c566 | 2531 | |
mkilivan | 5:c8918e47c566 | 2532 | res = validate(fp); /* Check validity */ |
mkilivan | 5:c8918e47c566 | 2533 | if (res != FR_OK) LEAVE_FF(fp->fs, res); |
mkilivan | 5:c8918e47c566 | 2534 | if (fp->flag & FA__ERROR) /* Aborted file? */ |
mkilivan | 5:c8918e47c566 | 2535 | LEAVE_FF(fp->fs, FR_INT_ERR); |
mkilivan | 5:c8918e47c566 | 2536 | if (!(fp->flag & FA_WRITE)) /* Check access mode */ |
mkilivan | 5:c8918e47c566 | 2537 | LEAVE_FF(fp->fs, FR_DENIED); |
mkilivan | 5:c8918e47c566 | 2538 | if ((DWORD)(fp->fsize + btw) < fp->fsize) btw = 0; /* File size cannot reach 4GB */ |
mkilivan | 5:c8918e47c566 | 2539 | |
mkilivan | 5:c8918e47c566 | 2540 | for ( ; btw; /* Repeat until all data written */ |
mkilivan | 5:c8918e47c566 | 2541 | wbuff += wcnt, fp->fptr += wcnt, *bw += wcnt, btw -= wcnt) { |
mkilivan | 5:c8918e47c566 | 2542 | if ((fp->fptr % SS(fp->fs)) == 0) { /* On the sector boundary? */ |
mkilivan | 5:c8918e47c566 | 2543 | csect = (BYTE)(fp->fptr / SS(fp->fs) & (fp->fs->csize - 1)); /* Sector offset in the cluster */ |
mkilivan | 5:c8918e47c566 | 2544 | if (!csect) { /* On the cluster boundary? */ |
mkilivan | 5:c8918e47c566 | 2545 | if (fp->fptr == 0) { /* On the top of the file? */ |
mkilivan | 5:c8918e47c566 | 2546 | clst = fp->sclust; /* Follow from the origin */ |
mkilivan | 5:c8918e47c566 | 2547 | if (clst == 0) /* When no cluster is allocated, */ |
mkilivan | 5:c8918e47c566 | 2548 | fp->sclust = clst = create_chain(fp->fs, 0); /* Create a new cluster chain */ |
mkilivan | 5:c8918e47c566 | 2549 | } else { /* Middle or end of the file */ |
mkilivan | 5:c8918e47c566 | 2550 | #if _USE_FASTSEEK |
mkilivan | 5:c8918e47c566 | 2551 | if (fp->cltbl) |
mkilivan | 5:c8918e47c566 | 2552 | clst = clmt_clust(fp, fp->fptr); /* Get cluster# from the CLMT */ |
mkilivan | 5:c8918e47c566 | 2553 | else |
mkilivan | 5:c8918e47c566 | 2554 | #endif |
mkilivan | 5:c8918e47c566 | 2555 | clst = create_chain(fp->fs, fp->clust); /* Follow or stretch cluster chain on the FAT */ |
mkilivan | 5:c8918e47c566 | 2556 | } |
mkilivan | 5:c8918e47c566 | 2557 | if (clst == 0) break; /* Could not allocate a new cluster (disk full) */ |
mkilivan | 5:c8918e47c566 | 2558 | if (clst == 1) ABORT(fp->fs, FR_INT_ERR); |
mkilivan | 5:c8918e47c566 | 2559 | if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR); |
mkilivan | 5:c8918e47c566 | 2560 | fp->clust = clst; /* Update current cluster */ |
mkilivan | 5:c8918e47c566 | 2561 | } |
mkilivan | 5:c8918e47c566 | 2562 | #if _FS_TINY |
mkilivan | 5:c8918e47c566 | 2563 | if (fp->fs->winsect == fp->dsect && move_window(fp->fs, 0)) /* Write-back sector cache */ |
mkilivan | 5:c8918e47c566 | 2564 | ABORT(fp->fs, FR_DISK_ERR); |
mkilivan | 5:c8918e47c566 | 2565 | #else |
mkilivan | 5:c8918e47c566 | 2566 | if (fp->flag & FA__DIRTY) { /* Write-back sector cache */ |
mkilivan | 5:c8918e47c566 | 2567 | if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK) |
mkilivan | 5:c8918e47c566 | 2568 | ABORT(fp->fs, FR_DISK_ERR); |
mkilivan | 5:c8918e47c566 | 2569 | fp->flag &= ~FA__DIRTY; |
mkilivan | 5:c8918e47c566 | 2570 | } |
mkilivan | 5:c8918e47c566 | 2571 | #endif |
mkilivan | 5:c8918e47c566 | 2572 | sect = clust2sect(fp->fs, fp->clust); /* Get current sector */ |
mkilivan | 5:c8918e47c566 | 2573 | if (!sect) ABORT(fp->fs, FR_INT_ERR); |
mkilivan | 5:c8918e47c566 | 2574 | sect += csect; |
mkilivan | 5:c8918e47c566 | 2575 | cc = btw / SS(fp->fs); /* When remaining bytes >= sector size, */ |
mkilivan | 5:c8918e47c566 | 2576 | if (cc) { /* Write maximum contiguous sectors directly */ |
mkilivan | 5:c8918e47c566 | 2577 | if (csect + cc > fp->fs->csize) /* Clip at cluster boundary */ |
mkilivan | 5:c8918e47c566 | 2578 | cc = fp->fs->csize - csect; |
mkilivan | 5:c8918e47c566 | 2579 | if (disk_write(fp->fs->drv, wbuff, sect, (BYTE)cc) != RES_OK) |
mkilivan | 5:c8918e47c566 | 2580 | ABORT(fp->fs, FR_DISK_ERR); |
mkilivan | 5:c8918e47c566 | 2581 | #if _FS_TINY |
mkilivan | 5:c8918e47c566 | 2582 | if (fp->fs->winsect - sect < cc) { /* Refill sector cache if it gets invalidated by the direct write */ |
mkilivan | 5:c8918e47c566 | 2583 | mem_cpy(fp->fs->win, wbuff + ((fp->fs->winsect - sect) * SS(fp->fs)), SS(fp->fs)); |
mkilivan | 5:c8918e47c566 | 2584 | fp->fs->wflag = 0; |
mkilivan | 5:c8918e47c566 | 2585 | } |
mkilivan | 5:c8918e47c566 | 2586 | #else |
mkilivan | 5:c8918e47c566 | 2587 | if (fp->dsect - sect < cc) { /* Refill sector cache if it gets invalidated by the direct write */ |
mkilivan | 5:c8918e47c566 | 2588 | mem_cpy(fp->buf, wbuff + ((fp->dsect - sect) * SS(fp->fs)), SS(fp->fs)); |
mkilivan | 5:c8918e47c566 | 2589 | fp->flag &= ~FA__DIRTY; |
mkilivan | 5:c8918e47c566 | 2590 | } |
mkilivan | 5:c8918e47c566 | 2591 | #endif |
mkilivan | 5:c8918e47c566 | 2592 | wcnt = SS(fp->fs) * cc; /* Number of bytes transferred */ |
mkilivan | 5:c8918e47c566 | 2593 | continue; |
mkilivan | 5:c8918e47c566 | 2594 | } |
mkilivan | 5:c8918e47c566 | 2595 | #if _FS_TINY |
mkilivan | 5:c8918e47c566 | 2596 | if (fp->fptr >= fp->fsize) { /* Avoid silly cache filling at growing edge */ |
mkilivan | 5:c8918e47c566 | 2597 | if (move_window(fp->fs, 0)) ABORT(fp->fs, FR_DISK_ERR); |
mkilivan | 5:c8918e47c566 | 2598 | fp->fs->winsect = sect; |
mkilivan | 5:c8918e47c566 | 2599 | } |
mkilivan | 5:c8918e47c566 | 2600 | #else |
mkilivan | 5:c8918e47c566 | 2601 | if (fp->dsect != sect) { /* Fill sector cache with file data */ |
mkilivan | 5:c8918e47c566 | 2602 | if (fp->fptr < fp->fsize && |
mkilivan | 5:c8918e47c566 | 2603 | disk_read(fp->fs->drv, fp->buf, sect, 1) != RES_OK) |
mkilivan | 5:c8918e47c566 | 2604 | ABORT(fp->fs, FR_DISK_ERR); |
mkilivan | 5:c8918e47c566 | 2605 | } |
mkilivan | 5:c8918e47c566 | 2606 | #endif |
mkilivan | 5:c8918e47c566 | 2607 | fp->dsect = sect; |
mkilivan | 5:c8918e47c566 | 2608 | } |
mkilivan | 5:c8918e47c566 | 2609 | wcnt = SS(fp->fs) - ((UINT)fp->fptr % SS(fp->fs));/* Put partial sector into file I/O buffer */ |
mkilivan | 5:c8918e47c566 | 2610 | if (wcnt > btw) wcnt = btw; |
mkilivan | 5:c8918e47c566 | 2611 | #if _FS_TINY |
mkilivan | 5:c8918e47c566 | 2612 | if (move_window(fp->fs, fp->dsect)) /* Move sector window */ |
mkilivan | 5:c8918e47c566 | 2613 | ABORT(fp->fs, FR_DISK_ERR); |
mkilivan | 5:c8918e47c566 | 2614 | mem_cpy(&fp->fs->win[fp->fptr % SS(fp->fs)], wbuff, wcnt); /* Fit partial sector */ |
mkilivan | 5:c8918e47c566 | 2615 | fp->fs->wflag = 1; |
mkilivan | 5:c8918e47c566 | 2616 | #else |
mkilivan | 5:c8918e47c566 | 2617 | mem_cpy(&fp->buf[fp->fptr % SS(fp->fs)], wbuff, wcnt); /* Fit partial sector */ |
mkilivan | 5:c8918e47c566 | 2618 | fp->flag |= FA__DIRTY; |
mkilivan | 5:c8918e47c566 | 2619 | #endif |
mkilivan | 5:c8918e47c566 | 2620 | } |
mkilivan | 5:c8918e47c566 | 2621 | |
mkilivan | 5:c8918e47c566 | 2622 | if (fp->fptr > fp->fsize) fp->fsize = fp->fptr; /* Update file size if needed */ |
mkilivan | 5:c8918e47c566 | 2623 | fp->flag |= FA__WRITTEN; /* Set file change flag */ |
mkilivan | 5:c8918e47c566 | 2624 | |
mkilivan | 5:c8918e47c566 | 2625 | LEAVE_FF(fp->fs, FR_OK); |
mkilivan | 5:c8918e47c566 | 2626 | } |
mkilivan | 5:c8918e47c566 | 2627 | |
mkilivan | 5:c8918e47c566 | 2628 | |
mkilivan | 5:c8918e47c566 | 2629 | |
mkilivan | 5:c8918e47c566 | 2630 | |
mkilivan | 5:c8918e47c566 | 2631 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 2632 | /* Synchronize the File Object */ |
mkilivan | 5:c8918e47c566 | 2633 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 2634 | |
mkilivan | 5:c8918e47c566 | 2635 | FRESULT f_sync ( |
mkilivan | 5:c8918e47c566 | 2636 | FIL *fp /* Pointer to the file object */ |
mkilivan | 5:c8918e47c566 | 2637 | ) |
mkilivan | 5:c8918e47c566 | 2638 | { |
mkilivan | 5:c8918e47c566 | 2639 | FRESULT res; |
mkilivan | 5:c8918e47c566 | 2640 | DWORD tim; |
mkilivan | 5:c8918e47c566 | 2641 | BYTE *dir; |
mkilivan | 5:c8918e47c566 | 2642 | |
mkilivan | 5:c8918e47c566 | 2643 | |
mkilivan | 5:c8918e47c566 | 2644 | res = validate(fp); /* Check validity of the object */ |
mkilivan | 5:c8918e47c566 | 2645 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 2646 | if (fp->flag & FA__WRITTEN) { /* Has the file been written? */ |
mkilivan | 5:c8918e47c566 | 2647 | #if !_FS_TINY /* Write-back dirty buffer */ |
mkilivan | 5:c8918e47c566 | 2648 | if (fp->flag & FA__DIRTY) { |
mkilivan | 5:c8918e47c566 | 2649 | if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK) |
mkilivan | 5:c8918e47c566 | 2650 | LEAVE_FF(fp->fs, FR_DISK_ERR); |
mkilivan | 5:c8918e47c566 | 2651 | fp->flag &= ~FA__DIRTY; |
mkilivan | 5:c8918e47c566 | 2652 | } |
mkilivan | 5:c8918e47c566 | 2653 | #endif |
mkilivan | 5:c8918e47c566 | 2654 | /* Update the directory entry */ |
mkilivan | 5:c8918e47c566 | 2655 | res = move_window(fp->fs, fp->dir_sect); |
mkilivan | 5:c8918e47c566 | 2656 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 2657 | dir = fp->dir_ptr; |
mkilivan | 5:c8918e47c566 | 2658 | dir[DIR_Attr] |= AM_ARC; /* Set archive bit */ |
mkilivan | 5:c8918e47c566 | 2659 | ST_DWORD(dir+DIR_FileSize, fp->fsize); /* Update file size */ |
mkilivan | 5:c8918e47c566 | 2660 | st_clust(dir, fp->sclust); /* Update start cluster */ |
mkilivan | 5:c8918e47c566 | 2661 | tim = get_fattime(); /* Update updated time */ |
mkilivan | 5:c8918e47c566 | 2662 | ST_DWORD(dir+DIR_WrtTime, tim); |
mkilivan | 5:c8918e47c566 | 2663 | ST_WORD(dir+DIR_LstAccDate, 0); |
mkilivan | 5:c8918e47c566 | 2664 | fp->flag &= ~FA__WRITTEN; |
mkilivan | 5:c8918e47c566 | 2665 | fp->fs->wflag = 1; |
mkilivan | 5:c8918e47c566 | 2666 | res = sync(fp->fs); |
mkilivan | 5:c8918e47c566 | 2667 | } |
mkilivan | 5:c8918e47c566 | 2668 | } |
mkilivan | 5:c8918e47c566 | 2669 | } |
mkilivan | 5:c8918e47c566 | 2670 | |
mkilivan | 5:c8918e47c566 | 2671 | LEAVE_FF(fp->fs, res); |
mkilivan | 5:c8918e47c566 | 2672 | } |
mkilivan | 5:c8918e47c566 | 2673 | |
mkilivan | 5:c8918e47c566 | 2674 | #endif /* !_FS_READONLY */ |
mkilivan | 5:c8918e47c566 | 2675 | |
mkilivan | 5:c8918e47c566 | 2676 | |
mkilivan | 5:c8918e47c566 | 2677 | |
mkilivan | 5:c8918e47c566 | 2678 | |
mkilivan | 5:c8918e47c566 | 2679 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 2680 | /* Close File */ |
mkilivan | 5:c8918e47c566 | 2681 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 2682 | |
mkilivan | 5:c8918e47c566 | 2683 | FRESULT f_close ( |
mkilivan | 5:c8918e47c566 | 2684 | FIL *fp /* Pointer to the file object to be closed */ |
mkilivan | 5:c8918e47c566 | 2685 | ) |
mkilivan | 5:c8918e47c566 | 2686 | { |
mkilivan | 5:c8918e47c566 | 2687 | FRESULT res; |
mkilivan | 5:c8918e47c566 | 2688 | |
mkilivan | 5:c8918e47c566 | 2689 | |
mkilivan | 5:c8918e47c566 | 2690 | #if _FS_READONLY |
mkilivan | 5:c8918e47c566 | 2691 | res = validate(fp); |
mkilivan | 5:c8918e47c566 | 2692 | { |
mkilivan | 5:c8918e47c566 | 2693 | #if _FS_REENTRANT |
mkilivan | 5:c8918e47c566 | 2694 | FATFS *fs = fp->fs; |
mkilivan | 5:c8918e47c566 | 2695 | #endif |
mkilivan | 5:c8918e47c566 | 2696 | if (res == FR_OK) fp->fs = 0; /* Discard file object */ |
mkilivan | 5:c8918e47c566 | 2697 | LEAVE_FF(fs, res); |
mkilivan | 5:c8918e47c566 | 2698 | } |
mkilivan | 5:c8918e47c566 | 2699 | #else |
mkilivan | 5:c8918e47c566 | 2700 | res = f_sync(fp); /* Flush cached data */ |
mkilivan | 5:c8918e47c566 | 2701 | #if _FS_LOCK |
mkilivan | 5:c8918e47c566 | 2702 | if (res == FR_OK) { /* Decrement open counter */ |
mkilivan | 5:c8918e47c566 | 2703 | #if _FS_REENTRANT |
mkilivan | 5:c8918e47c566 | 2704 | FATFS *fs = fp->fs;; |
mkilivan | 5:c8918e47c566 | 2705 | res = validate(fp); |
mkilivan | 5:c8918e47c566 | 2706 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 2707 | res = dec_lock(fp->lockid); |
mkilivan | 5:c8918e47c566 | 2708 | unlock_fs(fs, FR_OK); |
mkilivan | 5:c8918e47c566 | 2709 | } |
mkilivan | 5:c8918e47c566 | 2710 | #else |
mkilivan | 5:c8918e47c566 | 2711 | res = dec_lock(fp->lockid); |
mkilivan | 5:c8918e47c566 | 2712 | #endif |
mkilivan | 5:c8918e47c566 | 2713 | } |
mkilivan | 5:c8918e47c566 | 2714 | #endif |
mkilivan | 5:c8918e47c566 | 2715 | if (res == FR_OK) fp->fs = 0; /* Discard file object */ |
mkilivan | 5:c8918e47c566 | 2716 | return res; |
mkilivan | 5:c8918e47c566 | 2717 | #endif |
mkilivan | 5:c8918e47c566 | 2718 | } |
mkilivan | 5:c8918e47c566 | 2719 | |
mkilivan | 5:c8918e47c566 | 2720 | |
mkilivan | 5:c8918e47c566 | 2721 | |
mkilivan | 5:c8918e47c566 | 2722 | |
mkilivan | 5:c8918e47c566 | 2723 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 2724 | /* Current Drive/Directory Handlings */ |
mkilivan | 5:c8918e47c566 | 2725 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 2726 | |
mkilivan | 5:c8918e47c566 | 2727 | #if _FS_RPATH >= 1 |
mkilivan | 5:c8918e47c566 | 2728 | |
mkilivan | 5:c8918e47c566 | 2729 | FRESULT f_chdrive ( |
mkilivan | 5:c8918e47c566 | 2730 | BYTE drv /* Drive number */ |
mkilivan | 5:c8918e47c566 | 2731 | ) |
mkilivan | 5:c8918e47c566 | 2732 | { |
mkilivan | 5:c8918e47c566 | 2733 | if (drv >= _VOLUMES) return FR_INVALID_DRIVE; |
mkilivan | 5:c8918e47c566 | 2734 | |
mkilivan | 5:c8918e47c566 | 2735 | CurrVol = drv; |
mkilivan | 5:c8918e47c566 | 2736 | |
mkilivan | 5:c8918e47c566 | 2737 | return FR_OK; |
mkilivan | 5:c8918e47c566 | 2738 | } |
mkilivan | 5:c8918e47c566 | 2739 | |
mkilivan | 5:c8918e47c566 | 2740 | |
mkilivan | 5:c8918e47c566 | 2741 | |
mkilivan | 5:c8918e47c566 | 2742 | FRESULT f_chdir ( |
mkilivan | 5:c8918e47c566 | 2743 | const TCHAR *path /* Pointer to the directory path */ |
mkilivan | 5:c8918e47c566 | 2744 | ) |
mkilivan | 5:c8918e47c566 | 2745 | { |
mkilivan | 5:c8918e47c566 | 2746 | FRESULT res; |
mkilivan | 5:c8918e47c566 | 2747 | DIR dj; |
mkilivan | 5:c8918e47c566 | 2748 | DEF_NAMEBUF; |
mkilivan | 5:c8918e47c566 | 2749 | |
mkilivan | 5:c8918e47c566 | 2750 | |
mkilivan | 5:c8918e47c566 | 2751 | res = chk_mounted(&path, &dj.fs, 0); |
mkilivan | 5:c8918e47c566 | 2752 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 2753 | INIT_BUF(dj); |
mkilivan | 5:c8918e47c566 | 2754 | res = follow_path(&dj, path); /* Follow the path */ |
mkilivan | 5:c8918e47c566 | 2755 | FREE_BUF(); |
mkilivan | 5:c8918e47c566 | 2756 | if (res == FR_OK) { /* Follow completed */ |
mkilivan | 5:c8918e47c566 | 2757 | if (!dj.dir) { |
mkilivan | 5:c8918e47c566 | 2758 | dj.fs->cdir = dj.sclust; /* Start directory itself */ |
mkilivan | 5:c8918e47c566 | 2759 | } else { |
mkilivan | 5:c8918e47c566 | 2760 | if (dj.dir[DIR_Attr] & AM_DIR) /* Reached to the directory */ |
mkilivan | 5:c8918e47c566 | 2761 | dj.fs->cdir = ld_clust(dj.fs, dj.dir); |
mkilivan | 5:c8918e47c566 | 2762 | else |
mkilivan | 5:c8918e47c566 | 2763 | res = FR_NO_PATH; /* Reached but a file */ |
mkilivan | 5:c8918e47c566 | 2764 | } |
mkilivan | 5:c8918e47c566 | 2765 | } |
mkilivan | 5:c8918e47c566 | 2766 | if (res == FR_NO_FILE) res = FR_NO_PATH; |
mkilivan | 5:c8918e47c566 | 2767 | } |
mkilivan | 5:c8918e47c566 | 2768 | |
mkilivan | 5:c8918e47c566 | 2769 | LEAVE_FF(dj.fs, res); |
mkilivan | 5:c8918e47c566 | 2770 | } |
mkilivan | 5:c8918e47c566 | 2771 | |
mkilivan | 5:c8918e47c566 | 2772 | |
mkilivan | 5:c8918e47c566 | 2773 | #if _FS_RPATH >= 2 |
mkilivan | 5:c8918e47c566 | 2774 | FRESULT f_getcwd ( |
mkilivan | 5:c8918e47c566 | 2775 | TCHAR *path, /* Pointer to the directory path */ |
mkilivan | 5:c8918e47c566 | 2776 | UINT sz_path /* Size of path */ |
mkilivan | 5:c8918e47c566 | 2777 | ) |
mkilivan | 5:c8918e47c566 | 2778 | { |
mkilivan | 5:c8918e47c566 | 2779 | FRESULT res; |
mkilivan | 5:c8918e47c566 | 2780 | DIR dj; |
mkilivan | 5:c8918e47c566 | 2781 | UINT i, n; |
mkilivan | 5:c8918e47c566 | 2782 | DWORD ccl; |
mkilivan | 5:c8918e47c566 | 2783 | TCHAR *tp; |
mkilivan | 5:c8918e47c566 | 2784 | FILINFO fno; |
mkilivan | 5:c8918e47c566 | 2785 | DEF_NAMEBUF; |
mkilivan | 5:c8918e47c566 | 2786 | |
mkilivan | 5:c8918e47c566 | 2787 | |
mkilivan | 5:c8918e47c566 | 2788 | *path = 0; |
mkilivan | 5:c8918e47c566 | 2789 | res = chk_mounted((const TCHAR**)&path, &dj.fs, 0); /* Get current volume */ |
mkilivan | 5:c8918e47c566 | 2790 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 2791 | INIT_BUF(dj); |
mkilivan | 5:c8918e47c566 | 2792 | i = sz_path; /* Bottom of buffer (dir stack base) */ |
mkilivan | 5:c8918e47c566 | 2793 | dj.sclust = dj.fs->cdir; /* Start to follow upper dir from current dir */ |
mkilivan | 5:c8918e47c566 | 2794 | while ((ccl = dj.sclust) != 0) { /* Repeat while current dir is a sub-dir */ |
mkilivan | 5:c8918e47c566 | 2795 | res = dir_sdi(&dj, 1); /* Get parent dir */ |
mkilivan | 5:c8918e47c566 | 2796 | if (res != FR_OK) break; |
mkilivan | 5:c8918e47c566 | 2797 | res = dir_read(&dj); |
mkilivan | 5:c8918e47c566 | 2798 | if (res != FR_OK) break; |
mkilivan | 5:c8918e47c566 | 2799 | dj.sclust = ld_clust(dj.fs, dj.dir); /* Goto parent dir */ |
mkilivan | 5:c8918e47c566 | 2800 | res = dir_sdi(&dj, 0); |
mkilivan | 5:c8918e47c566 | 2801 | if (res != FR_OK) break; |
mkilivan | 5:c8918e47c566 | 2802 | do { /* Find the entry links to the child dir */ |
mkilivan | 5:c8918e47c566 | 2803 | res = dir_read(&dj); |
mkilivan | 5:c8918e47c566 | 2804 | if (res != FR_OK) break; |
mkilivan | 5:c8918e47c566 | 2805 | if (ccl == ld_clust(dj.fs, dj.dir)) break; /* Found the entry */ |
mkilivan | 5:c8918e47c566 | 2806 | res = dir_next(&dj, 0); |
mkilivan | 5:c8918e47c566 | 2807 | } while (res == FR_OK); |
mkilivan | 5:c8918e47c566 | 2808 | if (res == FR_NO_FILE) res = FR_INT_ERR;/* It cannot be 'not found'. */ |
mkilivan | 5:c8918e47c566 | 2809 | if (res != FR_OK) break; |
mkilivan | 5:c8918e47c566 | 2810 | #if _USE_LFN |
mkilivan | 5:c8918e47c566 | 2811 | fno.lfname = path; |
mkilivan | 5:c8918e47c566 | 2812 | fno.lfsize = i; |
mkilivan | 5:c8918e47c566 | 2813 | #endif |
mkilivan | 5:c8918e47c566 | 2814 | get_fileinfo(&dj, &fno); /* Get the dir name and push it to the buffer */ |
mkilivan | 5:c8918e47c566 | 2815 | tp = fno.fname; |
mkilivan | 5:c8918e47c566 | 2816 | if (_USE_LFN && *path) tp = path; |
mkilivan | 5:c8918e47c566 | 2817 | for (n = 0; tp[n]; n++) ; |
mkilivan | 5:c8918e47c566 | 2818 | if (i < n + 3) { |
mkilivan | 5:c8918e47c566 | 2819 | res = FR_NOT_ENOUGH_CORE; break; |
mkilivan | 5:c8918e47c566 | 2820 | } |
mkilivan | 5:c8918e47c566 | 2821 | while (n) path[--i] = tp[--n]; |
mkilivan | 5:c8918e47c566 | 2822 | path[--i] = '/'; |
mkilivan | 5:c8918e47c566 | 2823 | } |
mkilivan | 5:c8918e47c566 | 2824 | tp = path; |
mkilivan | 5:c8918e47c566 | 2825 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 2826 | *tp++ = '0' + CurrVol; /* Put drive number */ |
mkilivan | 5:c8918e47c566 | 2827 | *tp++ = ':'; |
mkilivan | 5:c8918e47c566 | 2828 | if (i == sz_path) { /* Root-dir */ |
mkilivan | 5:c8918e47c566 | 2829 | *tp++ = '/'; |
mkilivan | 5:c8918e47c566 | 2830 | } else { /* Sub-dir */ |
mkilivan | 5:c8918e47c566 | 2831 | do /* Add stacked path str */ |
mkilivan | 5:c8918e47c566 | 2832 | *tp++ = path[i++]; |
mkilivan | 5:c8918e47c566 | 2833 | while (i < sz_path); |
mkilivan | 5:c8918e47c566 | 2834 | } |
mkilivan | 5:c8918e47c566 | 2835 | } |
mkilivan | 5:c8918e47c566 | 2836 | *tp = 0; |
mkilivan | 5:c8918e47c566 | 2837 | FREE_BUF(); |
mkilivan | 5:c8918e47c566 | 2838 | } |
mkilivan | 5:c8918e47c566 | 2839 | |
mkilivan | 5:c8918e47c566 | 2840 | LEAVE_FF(dj.fs, res); |
mkilivan | 5:c8918e47c566 | 2841 | } |
mkilivan | 5:c8918e47c566 | 2842 | #endif /* _FS_RPATH >= 2 */ |
mkilivan | 5:c8918e47c566 | 2843 | #endif /* _FS_RPATH >= 1 */ |
mkilivan | 5:c8918e47c566 | 2844 | |
mkilivan | 5:c8918e47c566 | 2845 | |
mkilivan | 5:c8918e47c566 | 2846 | |
mkilivan | 5:c8918e47c566 | 2847 | #if _FS_MINIMIZE <= 2 |
mkilivan | 5:c8918e47c566 | 2848 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 2849 | /* Seek File R/W Pointer */ |
mkilivan | 5:c8918e47c566 | 2850 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 2851 | |
mkilivan | 5:c8918e47c566 | 2852 | FRESULT f_lseek ( |
mkilivan | 5:c8918e47c566 | 2853 | FIL *fp, /* Pointer to the file object */ |
mkilivan | 5:c8918e47c566 | 2854 | DWORD ofs /* File pointer from top of file */ |
mkilivan | 5:c8918e47c566 | 2855 | ) |
mkilivan | 5:c8918e47c566 | 2856 | { |
mkilivan | 5:c8918e47c566 | 2857 | FRESULT res; |
mkilivan | 5:c8918e47c566 | 2858 | |
mkilivan | 5:c8918e47c566 | 2859 | |
mkilivan | 5:c8918e47c566 | 2860 | res = validate(fp); /* Check validity of the object */ |
mkilivan | 5:c8918e47c566 | 2861 | if (res != FR_OK) LEAVE_FF(fp->fs, res); |
mkilivan | 5:c8918e47c566 | 2862 | if (fp->flag & FA__ERROR) /* Check abort flag */ |
mkilivan | 5:c8918e47c566 | 2863 | LEAVE_FF(fp->fs, FR_INT_ERR); |
mkilivan | 5:c8918e47c566 | 2864 | |
mkilivan | 5:c8918e47c566 | 2865 | #if _USE_FASTSEEK |
mkilivan | 5:c8918e47c566 | 2866 | if (fp->cltbl) { /* Fast seek */ |
mkilivan | 5:c8918e47c566 | 2867 | DWORD cl, pcl, ncl, tcl, dsc, tlen, ulen, *tbl; |
mkilivan | 5:c8918e47c566 | 2868 | |
mkilivan | 5:c8918e47c566 | 2869 | if (ofs == CREATE_LINKMAP) { /* Create CLMT */ |
mkilivan | 5:c8918e47c566 | 2870 | tbl = fp->cltbl; |
mkilivan | 5:c8918e47c566 | 2871 | tlen = *tbl++; ulen = 2; /* Given table size and required table size */ |
mkilivan | 5:c8918e47c566 | 2872 | cl = fp->sclust; /* Top of the chain */ |
mkilivan | 5:c8918e47c566 | 2873 | if (cl) { |
mkilivan | 5:c8918e47c566 | 2874 | do { |
mkilivan | 5:c8918e47c566 | 2875 | /* Get a fragment */ |
mkilivan | 5:c8918e47c566 | 2876 | tcl = cl; ncl = 0; ulen += 2; /* Top, length and used items */ |
mkilivan | 5:c8918e47c566 | 2877 | do { |
mkilivan | 5:c8918e47c566 | 2878 | pcl = cl; ncl++; |
mkilivan | 5:c8918e47c566 | 2879 | cl = get_fat(fp->fs, cl); |
mkilivan | 5:c8918e47c566 | 2880 | if (cl <= 1) ABORT(fp->fs, FR_INT_ERR); |
mkilivan | 5:c8918e47c566 | 2881 | if (cl == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR); |
mkilivan | 5:c8918e47c566 | 2882 | } while (cl == pcl + 1); |
mkilivan | 5:c8918e47c566 | 2883 | if (ulen <= tlen) { /* Store the length and top of the fragment */ |
mkilivan | 5:c8918e47c566 | 2884 | *tbl++ = ncl; *tbl++ = tcl; |
mkilivan | 5:c8918e47c566 | 2885 | } |
mkilivan | 5:c8918e47c566 | 2886 | } while (cl < fp->fs->n_fatent); /* Repeat until end of chain */ |
mkilivan | 5:c8918e47c566 | 2887 | } |
mkilivan | 5:c8918e47c566 | 2888 | *fp->cltbl = ulen; /* Number of items used */ |
mkilivan | 5:c8918e47c566 | 2889 | if (ulen <= tlen) |
mkilivan | 5:c8918e47c566 | 2890 | *tbl = 0; /* Terminate table */ |
mkilivan | 5:c8918e47c566 | 2891 | else |
mkilivan | 5:c8918e47c566 | 2892 | res = FR_NOT_ENOUGH_CORE; /* Given table size is smaller than required */ |
mkilivan | 5:c8918e47c566 | 2893 | |
mkilivan | 5:c8918e47c566 | 2894 | } else { /* Fast seek */ |
mkilivan | 5:c8918e47c566 | 2895 | if (ofs > fp->fsize) /* Clip offset at the file size */ |
mkilivan | 5:c8918e47c566 | 2896 | ofs = fp->fsize; |
mkilivan | 5:c8918e47c566 | 2897 | fp->fptr = ofs; /* Set file pointer */ |
mkilivan | 5:c8918e47c566 | 2898 | if (ofs) { |
mkilivan | 5:c8918e47c566 | 2899 | fp->clust = clmt_clust(fp, ofs - 1); |
mkilivan | 5:c8918e47c566 | 2900 | dsc = clust2sect(fp->fs, fp->clust); |
mkilivan | 5:c8918e47c566 | 2901 | if (!dsc) ABORT(fp->fs, FR_INT_ERR); |
mkilivan | 5:c8918e47c566 | 2902 | dsc += (ofs - 1) / SS(fp->fs) & (fp->fs->csize - 1); |
mkilivan | 5:c8918e47c566 | 2903 | if (fp->fptr % SS(fp->fs) && dsc != fp->dsect) { /* Refill sector cache if needed */ |
mkilivan | 5:c8918e47c566 | 2904 | #if !_FS_TINY |
mkilivan | 5:c8918e47c566 | 2905 | #if !_FS_READONLY |
mkilivan | 5:c8918e47c566 | 2906 | if (fp->flag & FA__DIRTY) { /* Write-back dirty sector cache */ |
mkilivan | 5:c8918e47c566 | 2907 | if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK) |
mkilivan | 5:c8918e47c566 | 2908 | ABORT(fp->fs, FR_DISK_ERR); |
mkilivan | 5:c8918e47c566 | 2909 | fp->flag &= ~FA__DIRTY; |
mkilivan | 5:c8918e47c566 | 2910 | } |
mkilivan | 5:c8918e47c566 | 2911 | #endif |
mkilivan | 5:c8918e47c566 | 2912 | if (disk_read(fp->fs->drv, fp->buf, dsc, 1) != RES_OK) /* Load current sector */ |
mkilivan | 5:c8918e47c566 | 2913 | ABORT(fp->fs, FR_DISK_ERR); |
mkilivan | 5:c8918e47c566 | 2914 | #endif |
mkilivan | 5:c8918e47c566 | 2915 | fp->dsect = dsc; |
mkilivan | 5:c8918e47c566 | 2916 | } |
mkilivan | 5:c8918e47c566 | 2917 | } |
mkilivan | 5:c8918e47c566 | 2918 | } |
mkilivan | 5:c8918e47c566 | 2919 | } else |
mkilivan | 5:c8918e47c566 | 2920 | #endif |
mkilivan | 5:c8918e47c566 | 2921 | |
mkilivan | 5:c8918e47c566 | 2922 | /* Normal Seek */ |
mkilivan | 5:c8918e47c566 | 2923 | { |
mkilivan | 5:c8918e47c566 | 2924 | DWORD clst, bcs, nsect, ifptr; |
mkilivan | 5:c8918e47c566 | 2925 | |
mkilivan | 5:c8918e47c566 | 2926 | if (ofs > fp->fsize /* In read-only mode, clip offset with the file size */ |
mkilivan | 5:c8918e47c566 | 2927 | #if !_FS_READONLY |
mkilivan | 5:c8918e47c566 | 2928 | && !(fp->flag & FA_WRITE) |
mkilivan | 5:c8918e47c566 | 2929 | #endif |
mkilivan | 5:c8918e47c566 | 2930 | ) ofs = fp->fsize; |
mkilivan | 5:c8918e47c566 | 2931 | |
mkilivan | 5:c8918e47c566 | 2932 | ifptr = fp->fptr; |
mkilivan | 5:c8918e47c566 | 2933 | fp->fptr = nsect = 0; |
mkilivan | 5:c8918e47c566 | 2934 | if (ofs) { |
mkilivan | 5:c8918e47c566 | 2935 | bcs = (DWORD)fp->fs->csize * SS(fp->fs); /* Cluster size (byte) */ |
mkilivan | 5:c8918e47c566 | 2936 | if (ifptr > 0 && |
mkilivan | 5:c8918e47c566 | 2937 | (ofs - 1) / bcs >= (ifptr - 1) / bcs) { /* When seek to same or following cluster, */ |
mkilivan | 5:c8918e47c566 | 2938 | fp->fptr = (ifptr - 1) & ~(bcs - 1); /* start from the current cluster */ |
mkilivan | 5:c8918e47c566 | 2939 | ofs -= fp->fptr; |
mkilivan | 5:c8918e47c566 | 2940 | clst = fp->clust; |
mkilivan | 5:c8918e47c566 | 2941 | } else { /* When seek to back cluster, */ |
mkilivan | 5:c8918e47c566 | 2942 | clst = fp->sclust; /* start from the first cluster */ |
mkilivan | 5:c8918e47c566 | 2943 | #if !_FS_READONLY |
mkilivan | 5:c8918e47c566 | 2944 | if (clst == 0) { /* If no cluster chain, create a new chain */ |
mkilivan | 5:c8918e47c566 | 2945 | clst = create_chain(fp->fs, 0); |
mkilivan | 5:c8918e47c566 | 2946 | if (clst == 1) ABORT(fp->fs, FR_INT_ERR); |
mkilivan | 5:c8918e47c566 | 2947 | if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR); |
mkilivan | 5:c8918e47c566 | 2948 | fp->sclust = clst; |
mkilivan | 5:c8918e47c566 | 2949 | } |
mkilivan | 5:c8918e47c566 | 2950 | #endif |
mkilivan | 5:c8918e47c566 | 2951 | fp->clust = clst; |
mkilivan | 5:c8918e47c566 | 2952 | } |
mkilivan | 5:c8918e47c566 | 2953 | if (clst != 0) { |
mkilivan | 5:c8918e47c566 | 2954 | while (ofs > bcs) { /* Cluster following loop */ |
mkilivan | 5:c8918e47c566 | 2955 | #if !_FS_READONLY |
mkilivan | 5:c8918e47c566 | 2956 | if (fp->flag & FA_WRITE) { /* Check if in write mode or not */ |
mkilivan | 5:c8918e47c566 | 2957 | clst = create_chain(fp->fs, clst); /* Force stretch if in write mode */ |
mkilivan | 5:c8918e47c566 | 2958 | if (clst == 0) { /* When disk gets full, clip file size */ |
mkilivan | 5:c8918e47c566 | 2959 | ofs = bcs; break; |
mkilivan | 5:c8918e47c566 | 2960 | } |
mkilivan | 5:c8918e47c566 | 2961 | } else |
mkilivan | 5:c8918e47c566 | 2962 | #endif |
mkilivan | 5:c8918e47c566 | 2963 | clst = get_fat(fp->fs, clst); /* Follow cluster chain if not in write mode */ |
mkilivan | 5:c8918e47c566 | 2964 | if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR); |
mkilivan | 5:c8918e47c566 | 2965 | if (clst <= 1 || clst >= fp->fs->n_fatent) ABORT(fp->fs, FR_INT_ERR); |
mkilivan | 5:c8918e47c566 | 2966 | fp->clust = clst; |
mkilivan | 5:c8918e47c566 | 2967 | fp->fptr += bcs; |
mkilivan | 5:c8918e47c566 | 2968 | ofs -= bcs; |
mkilivan | 5:c8918e47c566 | 2969 | } |
mkilivan | 5:c8918e47c566 | 2970 | fp->fptr += ofs; |
mkilivan | 5:c8918e47c566 | 2971 | if (ofs % SS(fp->fs)) { |
mkilivan | 5:c8918e47c566 | 2972 | nsect = clust2sect(fp->fs, clst); /* Current sector */ |
mkilivan | 5:c8918e47c566 | 2973 | if (!nsect) ABORT(fp->fs, FR_INT_ERR); |
mkilivan | 5:c8918e47c566 | 2974 | nsect += ofs / SS(fp->fs); |
mkilivan | 5:c8918e47c566 | 2975 | } |
mkilivan | 5:c8918e47c566 | 2976 | } |
mkilivan | 5:c8918e47c566 | 2977 | } |
mkilivan | 5:c8918e47c566 | 2978 | if (fp->fptr % SS(fp->fs) && nsect != fp->dsect) { /* Fill sector cache if needed */ |
mkilivan | 5:c8918e47c566 | 2979 | #if !_FS_TINY |
mkilivan | 5:c8918e47c566 | 2980 | #if !_FS_READONLY |
mkilivan | 5:c8918e47c566 | 2981 | if (fp->flag & FA__DIRTY) { /* Write-back dirty sector cache */ |
mkilivan | 5:c8918e47c566 | 2982 | if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK) |
mkilivan | 5:c8918e47c566 | 2983 | ABORT(fp->fs, FR_DISK_ERR); |
mkilivan | 5:c8918e47c566 | 2984 | fp->flag &= ~FA__DIRTY; |
mkilivan | 5:c8918e47c566 | 2985 | } |
mkilivan | 5:c8918e47c566 | 2986 | #endif |
mkilivan | 5:c8918e47c566 | 2987 | if (disk_read(fp->fs->drv, fp->buf, nsect, 1) != RES_OK) /* Fill sector cache */ |
mkilivan | 5:c8918e47c566 | 2988 | ABORT(fp->fs, FR_DISK_ERR); |
mkilivan | 5:c8918e47c566 | 2989 | #endif |
mkilivan | 5:c8918e47c566 | 2990 | fp->dsect = nsect; |
mkilivan | 5:c8918e47c566 | 2991 | } |
mkilivan | 5:c8918e47c566 | 2992 | #if !_FS_READONLY |
mkilivan | 5:c8918e47c566 | 2993 | if (fp->fptr > fp->fsize) { /* Set file change flag if the file size is extended */ |
mkilivan | 5:c8918e47c566 | 2994 | fp->fsize = fp->fptr; |
mkilivan | 5:c8918e47c566 | 2995 | fp->flag |= FA__WRITTEN; |
mkilivan | 5:c8918e47c566 | 2996 | } |
mkilivan | 5:c8918e47c566 | 2997 | #endif |
mkilivan | 5:c8918e47c566 | 2998 | } |
mkilivan | 5:c8918e47c566 | 2999 | |
mkilivan | 5:c8918e47c566 | 3000 | LEAVE_FF(fp->fs, res); |
mkilivan | 5:c8918e47c566 | 3001 | } |
mkilivan | 5:c8918e47c566 | 3002 | |
mkilivan | 5:c8918e47c566 | 3003 | |
mkilivan | 5:c8918e47c566 | 3004 | |
mkilivan | 5:c8918e47c566 | 3005 | #if _FS_MINIMIZE <= 1 |
mkilivan | 5:c8918e47c566 | 3006 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 3007 | /* Create a Directory Object */ |
mkilivan | 5:c8918e47c566 | 3008 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 3009 | |
mkilivan | 5:c8918e47c566 | 3010 | FRESULT f_opendir ( |
mkilivan | 5:c8918e47c566 | 3011 | FATFS_DIR *dj, /* Pointer to directory object to create */ |
mkilivan | 5:c8918e47c566 | 3012 | const TCHAR *path /* Pointer to the directory path */ |
mkilivan | 5:c8918e47c566 | 3013 | ) |
mkilivan | 5:c8918e47c566 | 3014 | { |
mkilivan | 5:c8918e47c566 | 3015 | FRESULT res; |
mkilivan | 5:c8918e47c566 | 3016 | FATFS *fs; |
mkilivan | 5:c8918e47c566 | 3017 | DEF_NAMEBUF; |
mkilivan | 5:c8918e47c566 | 3018 | |
mkilivan | 5:c8918e47c566 | 3019 | |
mkilivan | 5:c8918e47c566 | 3020 | if (!dj) return FR_INVALID_OBJECT; |
mkilivan | 5:c8918e47c566 | 3021 | |
mkilivan | 5:c8918e47c566 | 3022 | res = chk_mounted(&path, &dj->fs, 0); |
mkilivan | 5:c8918e47c566 | 3023 | fs = dj->fs; |
mkilivan | 5:c8918e47c566 | 3024 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 3025 | INIT_BUF(*dj); |
mkilivan | 5:c8918e47c566 | 3026 | res = follow_path(dj, path); /* Follow the path to the directory */ |
mkilivan | 5:c8918e47c566 | 3027 | FREE_BUF(); |
mkilivan | 5:c8918e47c566 | 3028 | if (res == FR_OK) { /* Follow completed */ |
mkilivan | 5:c8918e47c566 | 3029 | if (dj->dir) { /* It is not the root dir */ |
mkilivan | 5:c8918e47c566 | 3030 | if (dj->dir[DIR_Attr] & AM_DIR) { /* The object is a directory */ |
mkilivan | 5:c8918e47c566 | 3031 | dj->sclust = ld_clust(fs, dj->dir); |
mkilivan | 5:c8918e47c566 | 3032 | } else { /* The object is not a directory */ |
mkilivan | 5:c8918e47c566 | 3033 | res = FR_NO_PATH; |
mkilivan | 5:c8918e47c566 | 3034 | } |
mkilivan | 5:c8918e47c566 | 3035 | } |
mkilivan | 5:c8918e47c566 | 3036 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 3037 | dj->id = fs->id; |
mkilivan | 5:c8918e47c566 | 3038 | res = dir_sdi(dj, 0); /* Rewind dir */ |
mkilivan | 5:c8918e47c566 | 3039 | } |
mkilivan | 5:c8918e47c566 | 3040 | } |
mkilivan | 5:c8918e47c566 | 3041 | if (res == FR_NO_FILE) res = FR_NO_PATH; |
mkilivan | 5:c8918e47c566 | 3042 | if (res != FR_OK) dj->fs = 0; /* Invalidate the dir object if function faild */ |
mkilivan | 5:c8918e47c566 | 3043 | } else { |
mkilivan | 5:c8918e47c566 | 3044 | dj->fs = 0; |
mkilivan | 5:c8918e47c566 | 3045 | } |
mkilivan | 5:c8918e47c566 | 3046 | |
mkilivan | 5:c8918e47c566 | 3047 | LEAVE_FF(fs, res); |
mkilivan | 5:c8918e47c566 | 3048 | } |
mkilivan | 5:c8918e47c566 | 3049 | |
mkilivan | 5:c8918e47c566 | 3050 | |
mkilivan | 5:c8918e47c566 | 3051 | |
mkilivan | 5:c8918e47c566 | 3052 | |
mkilivan | 5:c8918e47c566 | 3053 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 3054 | /* Read Directory Entry in Sequence */ |
mkilivan | 5:c8918e47c566 | 3055 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 3056 | |
mkilivan | 5:c8918e47c566 | 3057 | FRESULT f_readdir ( |
mkilivan | 5:c8918e47c566 | 3058 | FATFS_DIR *dj, /* Pointer to the open directory object */ |
mkilivan | 5:c8918e47c566 | 3059 | FILINFO *fno /* Pointer to file information to return */ |
mkilivan | 5:c8918e47c566 | 3060 | ) |
mkilivan | 5:c8918e47c566 | 3061 | { |
mkilivan | 5:c8918e47c566 | 3062 | FRESULT res; |
mkilivan | 5:c8918e47c566 | 3063 | DEF_NAMEBUF; |
mkilivan | 5:c8918e47c566 | 3064 | |
mkilivan | 5:c8918e47c566 | 3065 | |
mkilivan | 5:c8918e47c566 | 3066 | res = validate(dj); /* Check validity of the object */ |
mkilivan | 5:c8918e47c566 | 3067 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 3068 | if (!fno) { |
mkilivan | 5:c8918e47c566 | 3069 | res = dir_sdi(dj, 0); /* Rewind the directory object */ |
mkilivan | 5:c8918e47c566 | 3070 | } else { |
mkilivan | 5:c8918e47c566 | 3071 | INIT_BUF(*dj); |
mkilivan | 5:c8918e47c566 | 3072 | res = dir_read(dj); /* Read an directory item */ |
mkilivan | 5:c8918e47c566 | 3073 | if (res == FR_NO_FILE) { /* Reached end of dir */ |
mkilivan | 5:c8918e47c566 | 3074 | dj->sect = 0; |
mkilivan | 5:c8918e47c566 | 3075 | res = FR_OK; |
mkilivan | 5:c8918e47c566 | 3076 | } |
mkilivan | 5:c8918e47c566 | 3077 | if (res == FR_OK) { /* A valid entry is found */ |
mkilivan | 5:c8918e47c566 | 3078 | get_fileinfo(dj, fno); /* Get the object information */ |
mkilivan | 5:c8918e47c566 | 3079 | res = dir_next(dj, 0); /* Increment index for next */ |
mkilivan | 5:c8918e47c566 | 3080 | if (res == FR_NO_FILE) { |
mkilivan | 5:c8918e47c566 | 3081 | dj->sect = 0; |
mkilivan | 5:c8918e47c566 | 3082 | res = FR_OK; |
mkilivan | 5:c8918e47c566 | 3083 | } |
mkilivan | 5:c8918e47c566 | 3084 | } |
mkilivan | 5:c8918e47c566 | 3085 | FREE_BUF(); |
mkilivan | 5:c8918e47c566 | 3086 | } |
mkilivan | 5:c8918e47c566 | 3087 | } |
mkilivan | 5:c8918e47c566 | 3088 | |
mkilivan | 5:c8918e47c566 | 3089 | LEAVE_FF(dj->fs, res); |
mkilivan | 5:c8918e47c566 | 3090 | } |
mkilivan | 5:c8918e47c566 | 3091 | |
mkilivan | 5:c8918e47c566 | 3092 | |
mkilivan | 5:c8918e47c566 | 3093 | |
mkilivan | 5:c8918e47c566 | 3094 | #if _FS_MINIMIZE == 0 |
mkilivan | 5:c8918e47c566 | 3095 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 3096 | /* Get File Status */ |
mkilivan | 5:c8918e47c566 | 3097 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 3098 | |
mkilivan | 5:c8918e47c566 | 3099 | FRESULT f_stat ( |
mkilivan | 5:c8918e47c566 | 3100 | const TCHAR *path, /* Pointer to the file path */ |
mkilivan | 5:c8918e47c566 | 3101 | FILINFO *fno /* Pointer to file information to return */ |
mkilivan | 5:c8918e47c566 | 3102 | ) |
mkilivan | 5:c8918e47c566 | 3103 | { |
mkilivan | 5:c8918e47c566 | 3104 | FRESULT res; |
mkilivan | 5:c8918e47c566 | 3105 | FATFS_DIR dj; |
mkilivan | 5:c8918e47c566 | 3106 | DEF_NAMEBUF; |
mkilivan | 5:c8918e47c566 | 3107 | |
mkilivan | 5:c8918e47c566 | 3108 | |
mkilivan | 5:c8918e47c566 | 3109 | res = chk_mounted(&path, &dj.fs, 0); |
mkilivan | 5:c8918e47c566 | 3110 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 3111 | INIT_BUF(dj); |
mkilivan | 5:c8918e47c566 | 3112 | res = follow_path(&dj, path); /* Follow the file path */ |
mkilivan | 5:c8918e47c566 | 3113 | if (res == FR_OK) { /* Follow completed */ |
mkilivan | 5:c8918e47c566 | 3114 | if (dj.dir) /* Found an object */ |
mkilivan | 5:c8918e47c566 | 3115 | get_fileinfo(&dj, fno); |
mkilivan | 5:c8918e47c566 | 3116 | else /* It is root dir */ |
mkilivan | 5:c8918e47c566 | 3117 | res = FR_INVALID_NAME; |
mkilivan | 5:c8918e47c566 | 3118 | } |
mkilivan | 5:c8918e47c566 | 3119 | FREE_BUF(); |
mkilivan | 5:c8918e47c566 | 3120 | } |
mkilivan | 5:c8918e47c566 | 3121 | |
mkilivan | 5:c8918e47c566 | 3122 | LEAVE_FF(dj.fs, res); |
mkilivan | 5:c8918e47c566 | 3123 | } |
mkilivan | 5:c8918e47c566 | 3124 | |
mkilivan | 5:c8918e47c566 | 3125 | |
mkilivan | 5:c8918e47c566 | 3126 | |
mkilivan | 5:c8918e47c566 | 3127 | #if !_FS_READONLY |
mkilivan | 5:c8918e47c566 | 3128 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 3129 | /* Get Number of Free Clusters */ |
mkilivan | 5:c8918e47c566 | 3130 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 3131 | |
mkilivan | 5:c8918e47c566 | 3132 | FRESULT f_getfree ( |
mkilivan | 5:c8918e47c566 | 3133 | const TCHAR *path, /* Pointer to the logical drive number (root dir) */ |
mkilivan | 5:c8918e47c566 | 3134 | DWORD *nclst, /* Pointer to the variable to return number of free clusters */ |
mkilivan | 5:c8918e47c566 | 3135 | FATFS **fatfs /* Pointer to pointer to corresponding file system object to return */ |
mkilivan | 5:c8918e47c566 | 3136 | ) |
mkilivan | 5:c8918e47c566 | 3137 | { |
mkilivan | 5:c8918e47c566 | 3138 | FRESULT res; |
mkilivan | 5:c8918e47c566 | 3139 | FATFS *fs; |
mkilivan | 5:c8918e47c566 | 3140 | DWORD n, clst, sect, stat; |
mkilivan | 5:c8918e47c566 | 3141 | UINT i; |
mkilivan | 5:c8918e47c566 | 3142 | BYTE fat, *p; |
mkilivan | 5:c8918e47c566 | 3143 | |
mkilivan | 5:c8918e47c566 | 3144 | |
mkilivan | 5:c8918e47c566 | 3145 | /* Get drive number */ |
mkilivan | 5:c8918e47c566 | 3146 | res = chk_mounted(&path, fatfs, 0); |
mkilivan | 5:c8918e47c566 | 3147 | fs = *fatfs; |
mkilivan | 5:c8918e47c566 | 3148 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 3149 | /* If free_clust is valid, return it without full cluster scan */ |
mkilivan | 5:c8918e47c566 | 3150 | if (fs->free_clust <= fs->n_fatent - 2) { |
mkilivan | 5:c8918e47c566 | 3151 | *nclst = fs->free_clust; |
mkilivan | 5:c8918e47c566 | 3152 | } else { |
mkilivan | 5:c8918e47c566 | 3153 | /* Get number of free clusters */ |
mkilivan | 5:c8918e47c566 | 3154 | fat = fs->fs_type; |
mkilivan | 5:c8918e47c566 | 3155 | n = 0; |
mkilivan | 5:c8918e47c566 | 3156 | if (fat == FS_FAT12) { |
mkilivan | 5:c8918e47c566 | 3157 | clst = 2; |
mkilivan | 5:c8918e47c566 | 3158 | do { |
mkilivan | 5:c8918e47c566 | 3159 | stat = get_fat(fs, clst); |
mkilivan | 5:c8918e47c566 | 3160 | if (stat == 0xFFFFFFFF) { res = FR_DISK_ERR; break; } |
mkilivan | 5:c8918e47c566 | 3161 | if (stat == 1) { res = FR_INT_ERR; break; } |
mkilivan | 5:c8918e47c566 | 3162 | if (stat == 0) n++; |
mkilivan | 5:c8918e47c566 | 3163 | } while (++clst < fs->n_fatent); |
mkilivan | 5:c8918e47c566 | 3164 | } else { |
mkilivan | 5:c8918e47c566 | 3165 | clst = fs->n_fatent; |
mkilivan | 5:c8918e47c566 | 3166 | sect = fs->fatbase; |
mkilivan | 5:c8918e47c566 | 3167 | i = 0; p = 0; |
mkilivan | 5:c8918e47c566 | 3168 | do { |
mkilivan | 5:c8918e47c566 | 3169 | if (!i) { |
mkilivan | 5:c8918e47c566 | 3170 | res = move_window(fs, sect++); |
mkilivan | 5:c8918e47c566 | 3171 | if (res != FR_OK) break; |
mkilivan | 5:c8918e47c566 | 3172 | p = fs->win; |
mkilivan | 5:c8918e47c566 | 3173 | i = SS(fs); |
mkilivan | 5:c8918e47c566 | 3174 | } |
mkilivan | 5:c8918e47c566 | 3175 | if (fat == FS_FAT16) { |
mkilivan | 5:c8918e47c566 | 3176 | if (LD_WORD(p) == 0) n++; |
mkilivan | 5:c8918e47c566 | 3177 | p += 2; i -= 2; |
mkilivan | 5:c8918e47c566 | 3178 | } else { |
mkilivan | 5:c8918e47c566 | 3179 | if ((LD_DWORD(p) & 0x0FFFFFFF) == 0) n++; |
mkilivan | 5:c8918e47c566 | 3180 | p += 4; i -= 4; |
mkilivan | 5:c8918e47c566 | 3181 | } |
mkilivan | 5:c8918e47c566 | 3182 | } while (--clst); |
mkilivan | 5:c8918e47c566 | 3183 | } |
mkilivan | 5:c8918e47c566 | 3184 | fs->free_clust = n; |
mkilivan | 5:c8918e47c566 | 3185 | if (fat == FS_FAT32) fs->fsi_flag = 1; |
mkilivan | 5:c8918e47c566 | 3186 | *nclst = n; |
mkilivan | 5:c8918e47c566 | 3187 | } |
mkilivan | 5:c8918e47c566 | 3188 | } |
mkilivan | 5:c8918e47c566 | 3189 | LEAVE_FF(fs, res); |
mkilivan | 5:c8918e47c566 | 3190 | } |
mkilivan | 5:c8918e47c566 | 3191 | |
mkilivan | 5:c8918e47c566 | 3192 | |
mkilivan | 5:c8918e47c566 | 3193 | |
mkilivan | 5:c8918e47c566 | 3194 | |
mkilivan | 5:c8918e47c566 | 3195 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 3196 | /* Truncate File */ |
mkilivan | 5:c8918e47c566 | 3197 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 3198 | |
mkilivan | 5:c8918e47c566 | 3199 | FRESULT f_truncate ( |
mkilivan | 5:c8918e47c566 | 3200 | FIL *fp /* Pointer to the file object */ |
mkilivan | 5:c8918e47c566 | 3201 | ) |
mkilivan | 5:c8918e47c566 | 3202 | { |
mkilivan | 5:c8918e47c566 | 3203 | FRESULT res; |
mkilivan | 5:c8918e47c566 | 3204 | DWORD ncl; |
mkilivan | 5:c8918e47c566 | 3205 | |
mkilivan | 5:c8918e47c566 | 3206 | |
mkilivan | 5:c8918e47c566 | 3207 | if (!fp) return FR_INVALID_OBJECT; |
mkilivan | 5:c8918e47c566 | 3208 | |
mkilivan | 5:c8918e47c566 | 3209 | res = validate(fp); /* Check validity of the object */ |
mkilivan | 5:c8918e47c566 | 3210 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 3211 | if (fp->flag & FA__ERROR) { /* Check abort flag */ |
mkilivan | 5:c8918e47c566 | 3212 | res = FR_INT_ERR; |
mkilivan | 5:c8918e47c566 | 3213 | } else { |
mkilivan | 5:c8918e47c566 | 3214 | if (!(fp->flag & FA_WRITE)) /* Check access mode */ |
mkilivan | 5:c8918e47c566 | 3215 | res = FR_DENIED; |
mkilivan | 5:c8918e47c566 | 3216 | } |
mkilivan | 5:c8918e47c566 | 3217 | } |
mkilivan | 5:c8918e47c566 | 3218 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 3219 | if (fp->fsize > fp->fptr) { |
mkilivan | 5:c8918e47c566 | 3220 | fp->fsize = fp->fptr; /* Set file size to current R/W point */ |
mkilivan | 5:c8918e47c566 | 3221 | fp->flag |= FA__WRITTEN; |
mkilivan | 5:c8918e47c566 | 3222 | if (fp->fptr == 0) { /* When set file size to zero, remove entire cluster chain */ |
mkilivan | 5:c8918e47c566 | 3223 | res = remove_chain(fp->fs, fp->sclust); |
mkilivan | 5:c8918e47c566 | 3224 | fp->sclust = 0; |
mkilivan | 5:c8918e47c566 | 3225 | } else { /* When truncate a part of the file, remove remaining clusters */ |
mkilivan | 5:c8918e47c566 | 3226 | ncl = get_fat(fp->fs, fp->clust); |
mkilivan | 5:c8918e47c566 | 3227 | res = FR_OK; |
mkilivan | 5:c8918e47c566 | 3228 | if (ncl == 0xFFFFFFFF) res = FR_DISK_ERR; |
mkilivan | 5:c8918e47c566 | 3229 | if (ncl == 1) res = FR_INT_ERR; |
mkilivan | 5:c8918e47c566 | 3230 | if (res == FR_OK && ncl < fp->fs->n_fatent) { |
mkilivan | 5:c8918e47c566 | 3231 | res = put_fat(fp->fs, fp->clust, 0x0FFFFFFF); |
mkilivan | 5:c8918e47c566 | 3232 | if (res == FR_OK) res = remove_chain(fp->fs, ncl); |
mkilivan | 5:c8918e47c566 | 3233 | } |
mkilivan | 5:c8918e47c566 | 3234 | } |
mkilivan | 5:c8918e47c566 | 3235 | } |
mkilivan | 5:c8918e47c566 | 3236 | if (res != FR_OK) fp->flag |= FA__ERROR; |
mkilivan | 5:c8918e47c566 | 3237 | } |
mkilivan | 5:c8918e47c566 | 3238 | |
mkilivan | 5:c8918e47c566 | 3239 | LEAVE_FF(fp->fs, res); |
mkilivan | 5:c8918e47c566 | 3240 | } |
mkilivan | 5:c8918e47c566 | 3241 | |
mkilivan | 5:c8918e47c566 | 3242 | |
mkilivan | 5:c8918e47c566 | 3243 | |
mkilivan | 5:c8918e47c566 | 3244 | |
mkilivan | 5:c8918e47c566 | 3245 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 3246 | /* Delete a File or Directory */ |
mkilivan | 5:c8918e47c566 | 3247 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 3248 | |
mkilivan | 5:c8918e47c566 | 3249 | FRESULT f_unlink ( |
mkilivan | 5:c8918e47c566 | 3250 | const TCHAR *path /* Pointer to the file or directory path */ |
mkilivan | 5:c8918e47c566 | 3251 | ) |
mkilivan | 5:c8918e47c566 | 3252 | { |
mkilivan | 5:c8918e47c566 | 3253 | FRESULT res; |
mkilivan | 5:c8918e47c566 | 3254 | FATFS_DIR dj, sdj; |
mkilivan | 5:c8918e47c566 | 3255 | BYTE *dir; |
mkilivan | 5:c8918e47c566 | 3256 | DWORD dclst; |
mkilivan | 5:c8918e47c566 | 3257 | DEF_NAMEBUF; |
mkilivan | 5:c8918e47c566 | 3258 | |
mkilivan | 5:c8918e47c566 | 3259 | |
mkilivan | 5:c8918e47c566 | 3260 | res = chk_mounted(&path, &dj.fs, 1); |
mkilivan | 5:c8918e47c566 | 3261 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 3262 | INIT_BUF(dj); |
mkilivan | 5:c8918e47c566 | 3263 | res = follow_path(&dj, path); /* Follow the file path */ |
mkilivan | 5:c8918e47c566 | 3264 | if (_FS_RPATH && res == FR_OK && (dj.fn[NS] & NS_DOT)) |
mkilivan | 5:c8918e47c566 | 3265 | res = FR_INVALID_NAME; /* Cannot remove dot entry */ |
mkilivan | 5:c8918e47c566 | 3266 | #if _FS_LOCK |
mkilivan | 5:c8918e47c566 | 3267 | if (res == FR_OK) res = chk_lock(&dj, 2); /* Cannot remove open file */ |
mkilivan | 5:c8918e47c566 | 3268 | #endif |
mkilivan | 5:c8918e47c566 | 3269 | if (res == FR_OK) { /* The object is accessible */ |
mkilivan | 5:c8918e47c566 | 3270 | dir = dj.dir; |
mkilivan | 5:c8918e47c566 | 3271 | if (!dir) { |
mkilivan | 5:c8918e47c566 | 3272 | res = FR_INVALID_NAME; /* Cannot remove the start directory */ |
mkilivan | 5:c8918e47c566 | 3273 | } else { |
mkilivan | 5:c8918e47c566 | 3274 | if (dir[DIR_Attr] & AM_RDO) |
mkilivan | 5:c8918e47c566 | 3275 | res = FR_DENIED; /* Cannot remove R/O object */ |
mkilivan | 5:c8918e47c566 | 3276 | } |
mkilivan | 5:c8918e47c566 | 3277 | dclst = ld_clust(dj.fs, dir); |
mkilivan | 5:c8918e47c566 | 3278 | if (res == FR_OK && (dir[DIR_Attr] & AM_DIR)) { /* Is it a sub-dir? */ |
mkilivan | 5:c8918e47c566 | 3279 | if (dclst < 2) { |
mkilivan | 5:c8918e47c566 | 3280 | res = FR_INT_ERR; |
mkilivan | 5:c8918e47c566 | 3281 | } else { |
mkilivan | 5:c8918e47c566 | 3282 | mem_cpy(&sdj, &dj, sizeof (FATFS_DIR)); /* Check if the sub-dir is empty or not */ |
mkilivan | 5:c8918e47c566 | 3283 | sdj.sclust = dclst; |
mkilivan | 5:c8918e47c566 | 3284 | res = dir_sdi(&sdj, 2); /* Exclude dot entries */ |
mkilivan | 5:c8918e47c566 | 3285 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 3286 | res = dir_read(&sdj); |
mkilivan | 5:c8918e47c566 | 3287 | if (res == FR_OK /* Not empty dir */ |
mkilivan | 5:c8918e47c566 | 3288 | #if _FS_RPATH |
mkilivan | 5:c8918e47c566 | 3289 | || dclst == dj.fs->cdir /* Current dir */ |
mkilivan | 5:c8918e47c566 | 3290 | #endif |
mkilivan | 5:c8918e47c566 | 3291 | ) res = FR_DENIED; |
mkilivan | 5:c8918e47c566 | 3292 | if (res == FR_NO_FILE) res = FR_OK; /* Empty */ |
mkilivan | 5:c8918e47c566 | 3293 | } |
mkilivan | 5:c8918e47c566 | 3294 | } |
mkilivan | 5:c8918e47c566 | 3295 | } |
mkilivan | 5:c8918e47c566 | 3296 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 3297 | res = dir_remove(&dj); /* Remove the directory entry */ |
mkilivan | 5:c8918e47c566 | 3298 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 3299 | if (dclst) /* Remove the cluster chain if exist */ |
mkilivan | 5:c8918e47c566 | 3300 | res = remove_chain(dj.fs, dclst); |
mkilivan | 5:c8918e47c566 | 3301 | if (res == FR_OK) res = sync(dj.fs); |
mkilivan | 5:c8918e47c566 | 3302 | } |
mkilivan | 5:c8918e47c566 | 3303 | } |
mkilivan | 5:c8918e47c566 | 3304 | } |
mkilivan | 5:c8918e47c566 | 3305 | FREE_BUF(); |
mkilivan | 5:c8918e47c566 | 3306 | } |
mkilivan | 5:c8918e47c566 | 3307 | LEAVE_FF(dj.fs, res); |
mkilivan | 5:c8918e47c566 | 3308 | } |
mkilivan | 5:c8918e47c566 | 3309 | |
mkilivan | 5:c8918e47c566 | 3310 | |
mkilivan | 5:c8918e47c566 | 3311 | |
mkilivan | 5:c8918e47c566 | 3312 | |
mkilivan | 5:c8918e47c566 | 3313 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 3314 | /* Create a Directory */ |
mkilivan | 5:c8918e47c566 | 3315 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 3316 | |
mkilivan | 5:c8918e47c566 | 3317 | FRESULT f_mkdir ( |
mkilivan | 5:c8918e47c566 | 3318 | const TCHAR *path /* Pointer to the directory path */ |
mkilivan | 5:c8918e47c566 | 3319 | ) |
mkilivan | 5:c8918e47c566 | 3320 | { |
mkilivan | 5:c8918e47c566 | 3321 | FRESULT res; |
mkilivan | 5:c8918e47c566 | 3322 | FATFS_DIR dj; |
mkilivan | 5:c8918e47c566 | 3323 | BYTE *dir, n; |
mkilivan | 5:c8918e47c566 | 3324 | DWORD dsc, dcl, pcl, tim = get_fattime(); |
mkilivan | 5:c8918e47c566 | 3325 | DEF_NAMEBUF; |
mkilivan | 5:c8918e47c566 | 3326 | |
mkilivan | 5:c8918e47c566 | 3327 | |
mkilivan | 5:c8918e47c566 | 3328 | res = chk_mounted(&path, &dj.fs, 1); |
mkilivan | 5:c8918e47c566 | 3329 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 3330 | INIT_BUF(dj); |
mkilivan | 5:c8918e47c566 | 3331 | res = follow_path(&dj, path); /* Follow the file path */ |
mkilivan | 5:c8918e47c566 | 3332 | if (res == FR_OK) res = FR_EXIST; /* Any object with same name is already existing */ |
mkilivan | 5:c8918e47c566 | 3333 | if (_FS_RPATH && res == FR_NO_FILE && (dj.fn[NS] & NS_DOT)) |
mkilivan | 5:c8918e47c566 | 3334 | res = FR_INVALID_NAME; |
mkilivan | 5:c8918e47c566 | 3335 | if (res == FR_NO_FILE) { /* Can create a new directory */ |
mkilivan | 5:c8918e47c566 | 3336 | dcl = create_chain(dj.fs, 0); /* Allocate a cluster for the new directory table */ |
mkilivan | 5:c8918e47c566 | 3337 | res = FR_OK; |
mkilivan | 5:c8918e47c566 | 3338 | if (dcl == 0) res = FR_DENIED; /* No space to allocate a new cluster */ |
mkilivan | 5:c8918e47c566 | 3339 | if (dcl == 1) res = FR_INT_ERR; |
mkilivan | 5:c8918e47c566 | 3340 | if (dcl == 0xFFFFFFFF) res = FR_DISK_ERR; |
mkilivan | 5:c8918e47c566 | 3341 | if (res == FR_OK) /* Flush FAT */ |
mkilivan | 5:c8918e47c566 | 3342 | res = move_window(dj.fs, 0); |
mkilivan | 5:c8918e47c566 | 3343 | if (res == FR_OK) { /* Initialize the new directory table */ |
mkilivan | 5:c8918e47c566 | 3344 | dsc = clust2sect(dj.fs, dcl); |
mkilivan | 5:c8918e47c566 | 3345 | dir = dj.fs->win; |
mkilivan | 5:c8918e47c566 | 3346 | mem_set(dir, 0, SS(dj.fs)); |
mkilivan | 5:c8918e47c566 | 3347 | mem_set(dir+DIR_Name, ' ', 8+3); /* Create "." entry */ |
mkilivan | 5:c8918e47c566 | 3348 | dir[DIR_Name] = '.'; |
mkilivan | 5:c8918e47c566 | 3349 | dir[DIR_Attr] = AM_DIR; |
mkilivan | 5:c8918e47c566 | 3350 | ST_DWORD(dir+DIR_WrtTime, tim); |
mkilivan | 5:c8918e47c566 | 3351 | st_clust(dir, dcl); |
mkilivan | 5:c8918e47c566 | 3352 | mem_cpy(dir+SZ_DIR, dir, SZ_DIR); /* Create ".." entry */ |
mkilivan | 5:c8918e47c566 | 3353 | dir[33] = '.'; pcl = dj.sclust; |
mkilivan | 5:c8918e47c566 | 3354 | if (dj.fs->fs_type == FS_FAT32 && pcl == dj.fs->dirbase) |
mkilivan | 5:c8918e47c566 | 3355 | pcl = 0; |
mkilivan | 5:c8918e47c566 | 3356 | st_clust(dir+SZ_DIR, pcl); |
mkilivan | 5:c8918e47c566 | 3357 | for (n = dj.fs->csize; n; n--) { /* Write dot entries and clear following sectors */ |
mkilivan | 5:c8918e47c566 | 3358 | dj.fs->winsect = dsc++; |
mkilivan | 5:c8918e47c566 | 3359 | dj.fs->wflag = 1; |
mkilivan | 5:c8918e47c566 | 3360 | res = move_window(dj.fs, 0); |
mkilivan | 5:c8918e47c566 | 3361 | if (res != FR_OK) break; |
mkilivan | 5:c8918e47c566 | 3362 | mem_set(dir, 0, SS(dj.fs)); |
mkilivan | 5:c8918e47c566 | 3363 | } |
mkilivan | 5:c8918e47c566 | 3364 | } |
mkilivan | 5:c8918e47c566 | 3365 | if (res == FR_OK) res = dir_register(&dj); /* Register the object to the directoy */ |
mkilivan | 5:c8918e47c566 | 3366 | if (res != FR_OK) { |
mkilivan | 5:c8918e47c566 | 3367 | remove_chain(dj.fs, dcl); /* Could not register, remove cluster chain */ |
mkilivan | 5:c8918e47c566 | 3368 | } else { |
mkilivan | 5:c8918e47c566 | 3369 | dir = dj.dir; |
mkilivan | 5:c8918e47c566 | 3370 | dir[DIR_Attr] = AM_DIR; /* Attribute */ |
mkilivan | 5:c8918e47c566 | 3371 | ST_DWORD(dir+DIR_WrtTime, tim); /* Created time */ |
mkilivan | 5:c8918e47c566 | 3372 | st_clust(dir, dcl); /* Table start cluster */ |
mkilivan | 5:c8918e47c566 | 3373 | dj.fs->wflag = 1; |
mkilivan | 5:c8918e47c566 | 3374 | res = sync(dj.fs); |
mkilivan | 5:c8918e47c566 | 3375 | } |
mkilivan | 5:c8918e47c566 | 3376 | } |
mkilivan | 5:c8918e47c566 | 3377 | FREE_BUF(); |
mkilivan | 5:c8918e47c566 | 3378 | } |
mkilivan | 5:c8918e47c566 | 3379 | |
mkilivan | 5:c8918e47c566 | 3380 | LEAVE_FF(dj.fs, res); |
mkilivan | 5:c8918e47c566 | 3381 | } |
mkilivan | 5:c8918e47c566 | 3382 | |
mkilivan | 5:c8918e47c566 | 3383 | |
mkilivan | 5:c8918e47c566 | 3384 | |
mkilivan | 5:c8918e47c566 | 3385 | |
mkilivan | 5:c8918e47c566 | 3386 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 3387 | /* Change Attribute */ |
mkilivan | 5:c8918e47c566 | 3388 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 3389 | |
mkilivan | 5:c8918e47c566 | 3390 | FRESULT f_chmod ( |
mkilivan | 5:c8918e47c566 | 3391 | const TCHAR *path, /* Pointer to the file path */ |
mkilivan | 5:c8918e47c566 | 3392 | BYTE value, /* Attribute bits */ |
mkilivan | 5:c8918e47c566 | 3393 | BYTE mask /* Attribute mask to change */ |
mkilivan | 5:c8918e47c566 | 3394 | ) |
mkilivan | 5:c8918e47c566 | 3395 | { |
mkilivan | 5:c8918e47c566 | 3396 | FRESULT res; |
mkilivan | 5:c8918e47c566 | 3397 | FATFS_DIR dj; |
mkilivan | 5:c8918e47c566 | 3398 | BYTE *dir; |
mkilivan | 5:c8918e47c566 | 3399 | DEF_NAMEBUF; |
mkilivan | 5:c8918e47c566 | 3400 | |
mkilivan | 5:c8918e47c566 | 3401 | |
mkilivan | 5:c8918e47c566 | 3402 | res = chk_mounted(&path, &dj.fs, 1); |
mkilivan | 5:c8918e47c566 | 3403 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 3404 | INIT_BUF(dj); |
mkilivan | 5:c8918e47c566 | 3405 | res = follow_path(&dj, path); /* Follow the file path */ |
mkilivan | 5:c8918e47c566 | 3406 | FREE_BUF(); |
mkilivan | 5:c8918e47c566 | 3407 | if (_FS_RPATH && res == FR_OK && (dj.fn[NS] & NS_DOT)) |
mkilivan | 5:c8918e47c566 | 3408 | res = FR_INVALID_NAME; |
mkilivan | 5:c8918e47c566 | 3409 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 3410 | dir = dj.dir; |
mkilivan | 5:c8918e47c566 | 3411 | if (!dir) { /* Is it a root directory? */ |
mkilivan | 5:c8918e47c566 | 3412 | res = FR_INVALID_NAME; |
mkilivan | 5:c8918e47c566 | 3413 | } else { /* File or sub directory */ |
mkilivan | 5:c8918e47c566 | 3414 | mask &= AM_RDO|AM_HID|AM_SYS|AM_ARC; /* Valid attribute mask */ |
mkilivan | 5:c8918e47c566 | 3415 | dir[DIR_Attr] = (value & mask) | (dir[DIR_Attr] & (BYTE)~mask); /* Apply attribute change */ |
mkilivan | 5:c8918e47c566 | 3416 | dj.fs->wflag = 1; |
mkilivan | 5:c8918e47c566 | 3417 | res = sync(dj.fs); |
mkilivan | 5:c8918e47c566 | 3418 | } |
mkilivan | 5:c8918e47c566 | 3419 | } |
mkilivan | 5:c8918e47c566 | 3420 | } |
mkilivan | 5:c8918e47c566 | 3421 | |
mkilivan | 5:c8918e47c566 | 3422 | LEAVE_FF(dj.fs, res); |
mkilivan | 5:c8918e47c566 | 3423 | } |
mkilivan | 5:c8918e47c566 | 3424 | |
mkilivan | 5:c8918e47c566 | 3425 | |
mkilivan | 5:c8918e47c566 | 3426 | |
mkilivan | 5:c8918e47c566 | 3427 | |
mkilivan | 5:c8918e47c566 | 3428 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 3429 | /* Change Timestamp */ |
mkilivan | 5:c8918e47c566 | 3430 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 3431 | |
mkilivan | 5:c8918e47c566 | 3432 | FRESULT f_utime ( |
mkilivan | 5:c8918e47c566 | 3433 | const TCHAR *path, /* Pointer to the file/directory name */ |
mkilivan | 5:c8918e47c566 | 3434 | const FILINFO *fno /* Pointer to the time stamp to be set */ |
mkilivan | 5:c8918e47c566 | 3435 | ) |
mkilivan | 5:c8918e47c566 | 3436 | { |
mkilivan | 5:c8918e47c566 | 3437 | FRESULT res; |
mkilivan | 5:c8918e47c566 | 3438 | FATFS_DIR dj; |
mkilivan | 5:c8918e47c566 | 3439 | BYTE *dir; |
mkilivan | 5:c8918e47c566 | 3440 | DEF_NAMEBUF; |
mkilivan | 5:c8918e47c566 | 3441 | |
mkilivan | 5:c8918e47c566 | 3442 | |
mkilivan | 5:c8918e47c566 | 3443 | res = chk_mounted(&path, &dj.fs, 1); |
mkilivan | 5:c8918e47c566 | 3444 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 3445 | INIT_BUF(dj); |
mkilivan | 5:c8918e47c566 | 3446 | res = follow_path(&dj, path); /* Follow the file path */ |
mkilivan | 5:c8918e47c566 | 3447 | FREE_BUF(); |
mkilivan | 5:c8918e47c566 | 3448 | if (_FS_RPATH && res == FR_OK && (dj.fn[NS] & NS_DOT)) |
mkilivan | 5:c8918e47c566 | 3449 | res = FR_INVALID_NAME; |
mkilivan | 5:c8918e47c566 | 3450 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 3451 | dir = dj.dir; |
mkilivan | 5:c8918e47c566 | 3452 | if (!dir) { /* Root directory */ |
mkilivan | 5:c8918e47c566 | 3453 | res = FR_INVALID_NAME; |
mkilivan | 5:c8918e47c566 | 3454 | } else { /* File or sub-directory */ |
mkilivan | 5:c8918e47c566 | 3455 | ST_WORD(dir+DIR_WrtTime, fno->ftime); |
mkilivan | 5:c8918e47c566 | 3456 | ST_WORD(dir+DIR_WrtDate, fno->fdate); |
mkilivan | 5:c8918e47c566 | 3457 | dj.fs->wflag = 1; |
mkilivan | 5:c8918e47c566 | 3458 | res = sync(dj.fs); |
mkilivan | 5:c8918e47c566 | 3459 | } |
mkilivan | 5:c8918e47c566 | 3460 | } |
mkilivan | 5:c8918e47c566 | 3461 | } |
mkilivan | 5:c8918e47c566 | 3462 | |
mkilivan | 5:c8918e47c566 | 3463 | LEAVE_FF(dj.fs, res); |
mkilivan | 5:c8918e47c566 | 3464 | } |
mkilivan | 5:c8918e47c566 | 3465 | |
mkilivan | 5:c8918e47c566 | 3466 | |
mkilivan | 5:c8918e47c566 | 3467 | |
mkilivan | 5:c8918e47c566 | 3468 | |
mkilivan | 5:c8918e47c566 | 3469 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 3470 | /* Rename File/Directory */ |
mkilivan | 5:c8918e47c566 | 3471 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 3472 | |
mkilivan | 5:c8918e47c566 | 3473 | FRESULT f_rename ( |
mkilivan | 5:c8918e47c566 | 3474 | const TCHAR *path_old, /* Pointer to the old name */ |
mkilivan | 5:c8918e47c566 | 3475 | const TCHAR *path_new /* Pointer to the new name */ |
mkilivan | 5:c8918e47c566 | 3476 | ) |
mkilivan | 5:c8918e47c566 | 3477 | { |
mkilivan | 5:c8918e47c566 | 3478 | FRESULT res; |
mkilivan | 5:c8918e47c566 | 3479 | FATFS_DIR djo, djn; |
mkilivan | 5:c8918e47c566 | 3480 | BYTE buf[21], *dir; |
mkilivan | 5:c8918e47c566 | 3481 | DWORD dw; |
mkilivan | 5:c8918e47c566 | 3482 | DEF_NAMEBUF; |
mkilivan | 5:c8918e47c566 | 3483 | |
mkilivan | 5:c8918e47c566 | 3484 | |
mkilivan | 5:c8918e47c566 | 3485 | res = chk_mounted(&path_old, &djo.fs, 1); |
mkilivan | 5:c8918e47c566 | 3486 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 3487 | djn.fs = djo.fs; |
mkilivan | 5:c8918e47c566 | 3488 | INIT_BUF(djo); |
mkilivan | 5:c8918e47c566 | 3489 | res = follow_path(&djo, path_old); /* Check old object */ |
mkilivan | 5:c8918e47c566 | 3490 | if (_FS_RPATH && res == FR_OK && (djo.fn[NS] & NS_DOT)) |
mkilivan | 5:c8918e47c566 | 3491 | res = FR_INVALID_NAME; |
mkilivan | 5:c8918e47c566 | 3492 | #if _FS_LOCK |
mkilivan | 5:c8918e47c566 | 3493 | if (res == FR_OK) res = chk_lock(&djo, 2); |
mkilivan | 5:c8918e47c566 | 3494 | #endif |
mkilivan | 5:c8918e47c566 | 3495 | if (res == FR_OK) { /* Old object is found */ |
mkilivan | 5:c8918e47c566 | 3496 | if (!djo.dir) { /* Is root dir? */ |
mkilivan | 5:c8918e47c566 | 3497 | res = FR_NO_FILE; |
mkilivan | 5:c8918e47c566 | 3498 | } else { |
mkilivan | 5:c8918e47c566 | 3499 | mem_cpy(buf, djo.dir+DIR_Attr, 21); /* Save the object information except for name */ |
mkilivan | 5:c8918e47c566 | 3500 | mem_cpy(&djn, &djo, sizeof (FATFS_DIR)); /* Check new object */ |
mkilivan | 5:c8918e47c566 | 3501 | res = follow_path(&djn, path_new); |
mkilivan | 5:c8918e47c566 | 3502 | if (res == FR_OK) res = FR_EXIST; /* The new object name is already existing */ |
mkilivan | 5:c8918e47c566 | 3503 | if (res == FR_NO_FILE) { /* Is it a valid path and no name collision? */ |
mkilivan | 5:c8918e47c566 | 3504 | /* Start critical section that an interruption or error can cause cross-link */ |
mkilivan | 5:c8918e47c566 | 3505 | res = dir_register(&djn); /* Register the new entry */ |
mkilivan | 5:c8918e47c566 | 3506 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 3507 | dir = djn.dir; /* Copy object information except for name */ |
mkilivan | 5:c8918e47c566 | 3508 | mem_cpy(dir+13, buf+2, 19); |
mkilivan | 5:c8918e47c566 | 3509 | dir[DIR_Attr] = buf[0] | AM_ARC; |
mkilivan | 5:c8918e47c566 | 3510 | djo.fs->wflag = 1; |
mkilivan | 5:c8918e47c566 | 3511 | if (djo.sclust != djn.sclust && (dir[DIR_Attr] & AM_DIR)) { /* Update .. entry in the directory if needed */ |
mkilivan | 5:c8918e47c566 | 3512 | dw = clust2sect(djo.fs, ld_clust(djo.fs, dir)); |
mkilivan | 5:c8918e47c566 | 3513 | if (!dw) { |
mkilivan | 5:c8918e47c566 | 3514 | res = FR_INT_ERR; |
mkilivan | 5:c8918e47c566 | 3515 | } else { |
mkilivan | 5:c8918e47c566 | 3516 | res = move_window(djo.fs, dw); |
mkilivan | 5:c8918e47c566 | 3517 | dir = djo.fs->win+SZ_DIR; /* .. entry */ |
mkilivan | 5:c8918e47c566 | 3518 | if (res == FR_OK && dir[1] == '.') { |
mkilivan | 5:c8918e47c566 | 3519 | dw = (djo.fs->fs_type == FS_FAT32 && djn.sclust == djo.fs->dirbase) ? 0 : djn.sclust; |
mkilivan | 5:c8918e47c566 | 3520 | st_clust(dir, dw); |
mkilivan | 5:c8918e47c566 | 3521 | djo.fs->wflag = 1; |
mkilivan | 5:c8918e47c566 | 3522 | } |
mkilivan | 5:c8918e47c566 | 3523 | } |
mkilivan | 5:c8918e47c566 | 3524 | } |
mkilivan | 5:c8918e47c566 | 3525 | if (res == FR_OK) { |
mkilivan | 5:c8918e47c566 | 3526 | res = dir_remove(&djo); /* Remove old entry */ |
mkilivan | 5:c8918e47c566 | 3527 | if (res == FR_OK) |
mkilivan | 5:c8918e47c566 | 3528 | res = sync(djo.fs); |
mkilivan | 5:c8918e47c566 | 3529 | } |
mkilivan | 5:c8918e47c566 | 3530 | } |
mkilivan | 5:c8918e47c566 | 3531 | /* End critical section */ |
mkilivan | 5:c8918e47c566 | 3532 | } |
mkilivan | 5:c8918e47c566 | 3533 | } |
mkilivan | 5:c8918e47c566 | 3534 | } |
mkilivan | 5:c8918e47c566 | 3535 | FREE_BUF(); |
mkilivan | 5:c8918e47c566 | 3536 | } |
mkilivan | 5:c8918e47c566 | 3537 | LEAVE_FF(djo.fs, res); |
mkilivan | 5:c8918e47c566 | 3538 | } |
mkilivan | 5:c8918e47c566 | 3539 | |
mkilivan | 5:c8918e47c566 | 3540 | #endif /* !_FS_READONLY */ |
mkilivan | 5:c8918e47c566 | 3541 | #endif /* _FS_MINIMIZE == 0 */ |
mkilivan | 5:c8918e47c566 | 3542 | #endif /* _FS_MINIMIZE <= 1 */ |
mkilivan | 5:c8918e47c566 | 3543 | #endif /* _FS_MINIMIZE <= 2 */ |
mkilivan | 5:c8918e47c566 | 3544 | |
mkilivan | 5:c8918e47c566 | 3545 | |
mkilivan | 5:c8918e47c566 | 3546 | |
mkilivan | 5:c8918e47c566 | 3547 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 3548 | /* Forward data to the stream directly (available on only tiny cfg) */ |
mkilivan | 5:c8918e47c566 | 3549 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 3550 | #if _USE_FORWARD && _FS_TINY |
mkilivan | 5:c8918e47c566 | 3551 | |
mkilivan | 5:c8918e47c566 | 3552 | FRESULT f_forward ( |
mkilivan | 5:c8918e47c566 | 3553 | FIL *fp, /* Pointer to the file object */ |
mkilivan | 5:c8918e47c566 | 3554 | UINT (*func)(const BYTE*,UINT), /* Pointer to the streaming function */ |
mkilivan | 5:c8918e47c566 | 3555 | UINT btr, /* Number of bytes to forward */ |
mkilivan | 5:c8918e47c566 | 3556 | UINT *bf /* Pointer to number of bytes forwarded */ |
mkilivan | 5:c8918e47c566 | 3557 | ) |
mkilivan | 5:c8918e47c566 | 3558 | { |
mkilivan | 5:c8918e47c566 | 3559 | FRESULT res; |
mkilivan | 5:c8918e47c566 | 3560 | DWORD remain, clst, sect; |
mkilivan | 5:c8918e47c566 | 3561 | UINT rcnt; |
mkilivan | 5:c8918e47c566 | 3562 | BYTE csect; |
mkilivan | 5:c8918e47c566 | 3563 | |
mkilivan | 5:c8918e47c566 | 3564 | |
mkilivan | 5:c8918e47c566 | 3565 | *bf = 0; /* Clear transfer byte counter */ |
mkilivan | 5:c8918e47c566 | 3566 | |
mkilivan | 5:c8918e47c566 | 3567 | if (!fp) return FR_INVALID_OBJECT; |
mkilivan | 5:c8918e47c566 | 3568 | |
mkilivan | 5:c8918e47c566 | 3569 | res = validate(fp); /* Check validity of the object */ |
mkilivan | 5:c8918e47c566 | 3570 | if (res != FR_OK) LEAVE_FF(fp->fs, res); |
mkilivan | 5:c8918e47c566 | 3571 | if (fp->flag & FA__ERROR) /* Check error flag */ |
mkilivan | 5:c8918e47c566 | 3572 | LEAVE_FF(fp->fs, FR_INT_ERR); |
mkilivan | 5:c8918e47c566 | 3573 | if (!(fp->flag & FA_READ)) /* Check access mode */ |
mkilivan | 5:c8918e47c566 | 3574 | LEAVE_FF(fp->fs, FR_DENIED); |
mkilivan | 5:c8918e47c566 | 3575 | |
mkilivan | 5:c8918e47c566 | 3576 | remain = fp->fsize - fp->fptr; |
mkilivan | 5:c8918e47c566 | 3577 | if (btr > remain) btr = (UINT)remain; /* Truncate btr by remaining bytes */ |
mkilivan | 5:c8918e47c566 | 3578 | |
mkilivan | 5:c8918e47c566 | 3579 | for ( ; btr && (*func)(0, 0); /* Repeat until all data transferred or stream becomes busy */ |
mkilivan | 5:c8918e47c566 | 3580 | fp->fptr += rcnt, *bf += rcnt, btr -= rcnt) { |
mkilivan | 5:c8918e47c566 | 3581 | csect = (BYTE)(fp->fptr / SS(fp->fs) & (fp->fs->csize - 1)); /* Sector offset in the cluster */ |
mkilivan | 5:c8918e47c566 | 3582 | if ((fp->fptr % SS(fp->fs)) == 0) { /* On the sector boundary? */ |
mkilivan | 5:c8918e47c566 | 3583 | if (!csect) { /* On the cluster boundary? */ |
mkilivan | 5:c8918e47c566 | 3584 | clst = (fp->fptr == 0) ? /* On the top of the file? */ |
mkilivan | 5:c8918e47c566 | 3585 | fp->sclust : get_fat(fp->fs, fp->clust); |
mkilivan | 5:c8918e47c566 | 3586 | if (clst <= 1) ABORT(fp->fs, FR_INT_ERR); |
mkilivan | 5:c8918e47c566 | 3587 | if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR); |
mkilivan | 5:c8918e47c566 | 3588 | fp->clust = clst; /* Update current cluster */ |
mkilivan | 5:c8918e47c566 | 3589 | } |
mkilivan | 5:c8918e47c566 | 3590 | } |
mkilivan | 5:c8918e47c566 | 3591 | sect = clust2sect(fp->fs, fp->clust); /* Get current data sector */ |
mkilivan | 5:c8918e47c566 | 3592 | if (!sect) ABORT(fp->fs, FR_INT_ERR); |
mkilivan | 5:c8918e47c566 | 3593 | sect += csect; |
mkilivan | 5:c8918e47c566 | 3594 | if (move_window(fp->fs, sect)) /* Move sector window */ |
mkilivan | 5:c8918e47c566 | 3595 | ABORT(fp->fs, FR_DISK_ERR); |
mkilivan | 5:c8918e47c566 | 3596 | fp->dsect = sect; |
mkilivan | 5:c8918e47c566 | 3597 | rcnt = SS(fp->fs) - (WORD)(fp->fptr % SS(fp->fs)); /* Forward data from sector window */ |
mkilivan | 5:c8918e47c566 | 3598 | if (rcnt > btr) rcnt = btr; |
mkilivan | 5:c8918e47c566 | 3599 | rcnt = (*func)(&fp->fs->win[(WORD)fp->fptr % SS(fp->fs)], rcnt); |
mkilivan | 5:c8918e47c566 | 3600 | if (!rcnt) ABORT(fp->fs, FR_INT_ERR); |
mkilivan | 5:c8918e47c566 | 3601 | } |
mkilivan | 5:c8918e47c566 | 3602 | |
mkilivan | 5:c8918e47c566 | 3603 | LEAVE_FF(fp->fs, FR_OK); |
mkilivan | 5:c8918e47c566 | 3604 | } |
mkilivan | 5:c8918e47c566 | 3605 | #endif /* _USE_FORWARD */ |
mkilivan | 5:c8918e47c566 | 3606 | |
mkilivan | 5:c8918e47c566 | 3607 | |
mkilivan | 5:c8918e47c566 | 3608 | |
mkilivan | 5:c8918e47c566 | 3609 | #if _USE_MKFS && !_FS_READONLY |
mkilivan | 5:c8918e47c566 | 3610 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 3611 | /* Create File System on the Drive */ |
mkilivan | 5:c8918e47c566 | 3612 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 3613 | #define N_ROOTDIR 512 /* Number of root dir entries for FAT12/16 */ |
mkilivan | 5:c8918e47c566 | 3614 | #define N_FATS 1 /* Number of FAT copies (1 or 2) */ |
mkilivan | 5:c8918e47c566 | 3615 | |
mkilivan | 5:c8918e47c566 | 3616 | |
mkilivan | 5:c8918e47c566 | 3617 | FRESULT f_mkfs ( |
mkilivan | 5:c8918e47c566 | 3618 | BYTE drv, /* Logical drive number */ |
mkilivan | 5:c8918e47c566 | 3619 | BYTE sfd, /* Partitioning rule 0:FDISK, 1:SFD */ |
mkilivan | 5:c8918e47c566 | 3620 | UINT au /* Allocation unit size [bytes] */ |
mkilivan | 5:c8918e47c566 | 3621 | ) |
mkilivan | 5:c8918e47c566 | 3622 | { |
mkilivan | 5:c8918e47c566 | 3623 | static const WORD vst[] = { 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 0}; |
mkilivan | 5:c8918e47c566 | 3624 | static const WORD cst[] = {32768, 16384, 8192, 4096, 2048, 16384, 8192, 4096, 2048, 1024, 512}; |
mkilivan | 5:c8918e47c566 | 3625 | BYTE fmt, md, sys, *tbl, pdrv, part; |
mkilivan | 5:c8918e47c566 | 3626 | DWORD n_clst, vs, n, wsect; |
mkilivan | 5:c8918e47c566 | 3627 | UINT i; |
mkilivan | 5:c8918e47c566 | 3628 | DWORD b_vol, b_fat, b_dir, b_data; /* LBA */ |
mkilivan | 5:c8918e47c566 | 3629 | DWORD n_vol, n_rsv, n_fat, n_dir; /* Size */ |
mkilivan | 5:c8918e47c566 | 3630 | FATFS *fs; |
mkilivan | 5:c8918e47c566 | 3631 | DSTATUS stat; |
mkilivan | 5:c8918e47c566 | 3632 | |
mkilivan | 5:c8918e47c566 | 3633 | |
mkilivan | 5:c8918e47c566 | 3634 | /* Check mounted drive and clear work area */ |
mkilivan | 5:c8918e47c566 | 3635 | if (drv >= _VOLUMES) return FR_INVALID_DRIVE; |
mkilivan | 5:c8918e47c566 | 3636 | if (sfd > 1) return FR_INVALID_PARAMETER; |
mkilivan | 5:c8918e47c566 | 3637 | if (au & (au - 1)) return FR_INVALID_PARAMETER; |
mkilivan | 5:c8918e47c566 | 3638 | fs = FatFs[drv]; |
mkilivan | 5:c8918e47c566 | 3639 | if (!fs) return FR_NOT_ENABLED; |
mkilivan | 5:c8918e47c566 | 3640 | fs->fs_type = 0; |
mkilivan | 5:c8918e47c566 | 3641 | pdrv = LD2PD(drv); /* Physical drive */ |
mkilivan | 5:c8918e47c566 | 3642 | part = LD2PT(drv); /* Partition (0:auto detect, 1-4:get from partition table)*/ |
mkilivan | 5:c8918e47c566 | 3643 | |
mkilivan | 5:c8918e47c566 | 3644 | /* Get disk statics */ |
mkilivan | 5:c8918e47c566 | 3645 | stat = disk_initialize(pdrv); |
mkilivan | 5:c8918e47c566 | 3646 | if (stat & STA_NOINIT) return FR_NOT_READY; |
mkilivan | 5:c8918e47c566 | 3647 | if (stat & STA_PROTECT) return FR_WRITE_PROTECTED; |
mkilivan | 5:c8918e47c566 | 3648 | #if _MAX_SS != 512 /* Get disk sector size */ |
mkilivan | 5:c8918e47c566 | 3649 | if (disk_ioctl(pdrv, GET_SECTOR_SIZE, &SS(fs)) != RES_OK || SS(fs) > _MAX_SS) |
mkilivan | 5:c8918e47c566 | 3650 | return FR_DISK_ERR; |
mkilivan | 5:c8918e47c566 | 3651 | #endif |
mkilivan | 5:c8918e47c566 | 3652 | if (_MULTI_PARTITION && part) { |
mkilivan | 5:c8918e47c566 | 3653 | /* Get partition information from partition table in the MBR */ |
mkilivan | 5:c8918e47c566 | 3654 | if (disk_read(pdrv, fs->win, 0, 1) != RES_OK) return FR_DISK_ERR; |
mkilivan | 5:c8918e47c566 | 3655 | if (LD_WORD(fs->win+BS_55AA) != 0xAA55) return FR_MKFS_ABORTED; |
mkilivan | 5:c8918e47c566 | 3656 | tbl = &fs->win[MBR_Table + (part - 1) * SZ_PTE]; |
mkilivan | 5:c8918e47c566 | 3657 | if (!tbl[4]) return FR_MKFS_ABORTED; /* No partition? */ |
mkilivan | 5:c8918e47c566 | 3658 | b_vol = LD_DWORD(tbl+8); /* Volume start sector */ |
mkilivan | 5:c8918e47c566 | 3659 | n_vol = LD_DWORD(tbl+12); /* Volume size */ |
mkilivan | 5:c8918e47c566 | 3660 | } else { |
mkilivan | 5:c8918e47c566 | 3661 | /* Create a partition in this function */ |
mkilivan | 5:c8918e47c566 | 3662 | if (disk_ioctl(pdrv, GET_SECTOR_COUNT, &n_vol) != RES_OK || n_vol < 128) |
mkilivan | 5:c8918e47c566 | 3663 | return FR_DISK_ERR; |
mkilivan | 5:c8918e47c566 | 3664 | b_vol = (sfd) ? 0 : 63; /* Volume start sector */ |
mkilivan | 5:c8918e47c566 | 3665 | n_vol -= b_vol; /* Volume size */ |
mkilivan | 5:c8918e47c566 | 3666 | } |
mkilivan | 5:c8918e47c566 | 3667 | |
mkilivan | 5:c8918e47c566 | 3668 | if (!au) { /* AU auto selection */ |
mkilivan | 5:c8918e47c566 | 3669 | vs = n_vol / (2000 / (SS(fs) / 512)); |
mkilivan | 5:c8918e47c566 | 3670 | for (i = 0; vs < vst[i]; i++) ; |
mkilivan | 5:c8918e47c566 | 3671 | au = cst[i]; |
mkilivan | 5:c8918e47c566 | 3672 | } |
mkilivan | 5:c8918e47c566 | 3673 | au /= SS(fs); /* Number of sectors per cluster */ |
mkilivan | 5:c8918e47c566 | 3674 | if (au == 0) au = 1; |
mkilivan | 5:c8918e47c566 | 3675 | if (au > 128) au = 128; |
mkilivan | 5:c8918e47c566 | 3676 | |
mkilivan | 5:c8918e47c566 | 3677 | /* Pre-compute number of clusters and FAT sub-type */ |
mkilivan | 5:c8918e47c566 | 3678 | n_clst = n_vol / au; |
mkilivan | 5:c8918e47c566 | 3679 | fmt = FS_FAT12; |
mkilivan | 5:c8918e47c566 | 3680 | if (n_clst >= MIN_FAT16) fmt = FS_FAT16; |
mkilivan | 5:c8918e47c566 | 3681 | if (n_clst >= MIN_FAT32) fmt = FS_FAT32; |
mkilivan | 5:c8918e47c566 | 3682 | |
mkilivan | 5:c8918e47c566 | 3683 | /* Determine offset and size of FAT structure */ |
mkilivan | 5:c8918e47c566 | 3684 | if (fmt == FS_FAT32) { |
mkilivan | 5:c8918e47c566 | 3685 | n_fat = ((n_clst * 4) + 8 + SS(fs) - 1) / SS(fs); |
mkilivan | 5:c8918e47c566 | 3686 | n_rsv = 32; |
mkilivan | 5:c8918e47c566 | 3687 | n_dir = 0; |
mkilivan | 5:c8918e47c566 | 3688 | } else { |
mkilivan | 5:c8918e47c566 | 3689 | n_fat = (fmt == FS_FAT12) ? (n_clst * 3 + 1) / 2 + 3 : (n_clst * 2) + 4; |
mkilivan | 5:c8918e47c566 | 3690 | n_fat = (n_fat + SS(fs) - 1) / SS(fs); |
mkilivan | 5:c8918e47c566 | 3691 | n_rsv = 1; |
mkilivan | 5:c8918e47c566 | 3692 | n_dir = (DWORD)N_ROOTDIR * SZ_DIR / SS(fs); |
mkilivan | 5:c8918e47c566 | 3693 | } |
mkilivan | 5:c8918e47c566 | 3694 | b_fat = b_vol + n_rsv; /* FAT area start sector */ |
mkilivan | 5:c8918e47c566 | 3695 | b_dir = b_fat + n_fat * N_FATS; /* Directory area start sector */ |
mkilivan | 5:c8918e47c566 | 3696 | b_data = b_dir + n_dir; /* Data area start sector */ |
mkilivan | 5:c8918e47c566 | 3697 | if (n_vol < b_data + au - b_vol) return FR_MKFS_ABORTED; /* Too small volume */ |
mkilivan | 5:c8918e47c566 | 3698 | |
mkilivan | 5:c8918e47c566 | 3699 | /* Align data start sector to erase block boundary (for flash memory media) */ |
mkilivan | 5:c8918e47c566 | 3700 | if (disk_ioctl(pdrv, GET_BLOCK_SIZE, &n) != RES_OK || !n || n > 32768) n = 1; |
mkilivan | 5:c8918e47c566 | 3701 | n = (b_data + n - 1) & ~(n - 1); /* Next nearest erase block from current data start */ |
mkilivan | 5:c8918e47c566 | 3702 | n = (n - b_data) / N_FATS; |
mkilivan | 5:c8918e47c566 | 3703 | if (fmt == FS_FAT32) { /* FAT32: Move FAT offset */ |
mkilivan | 5:c8918e47c566 | 3704 | n_rsv += n; |
mkilivan | 5:c8918e47c566 | 3705 | b_fat += n; |
mkilivan | 5:c8918e47c566 | 3706 | } else { /* FAT12/16: Expand FAT size */ |
mkilivan | 5:c8918e47c566 | 3707 | n_fat += n; |
mkilivan | 5:c8918e47c566 | 3708 | } |
mkilivan | 5:c8918e47c566 | 3709 | |
mkilivan | 5:c8918e47c566 | 3710 | /* Determine number of clusters and final check of validity of the FAT sub-type */ |
mkilivan | 5:c8918e47c566 | 3711 | n_clst = (n_vol - n_rsv - n_fat * N_FATS - n_dir) / au; |
mkilivan | 5:c8918e47c566 | 3712 | if ( (fmt == FS_FAT16 && n_clst < MIN_FAT16) |
mkilivan | 5:c8918e47c566 | 3713 | || (fmt == FS_FAT32 && n_clst < MIN_FAT32)) |
mkilivan | 5:c8918e47c566 | 3714 | return FR_MKFS_ABORTED; |
mkilivan | 5:c8918e47c566 | 3715 | |
mkilivan | 5:c8918e47c566 | 3716 | switch (fmt) { /* Determine system ID for partition table */ |
mkilivan | 5:c8918e47c566 | 3717 | case FS_FAT12: sys = 0x01; break; |
mkilivan | 5:c8918e47c566 | 3718 | case FS_FAT16: sys = (n_vol < 0x10000) ? 0x04 : 0x06; break; |
mkilivan | 5:c8918e47c566 | 3719 | default: sys = 0x0C; |
mkilivan | 5:c8918e47c566 | 3720 | } |
mkilivan | 5:c8918e47c566 | 3721 | |
mkilivan | 5:c8918e47c566 | 3722 | if (_MULTI_PARTITION && part) { |
mkilivan | 5:c8918e47c566 | 3723 | /* Update system ID in the partition table */ |
mkilivan | 5:c8918e47c566 | 3724 | tbl = &fs->win[MBR_Table + (part - 1) * SZ_PTE]; |
mkilivan | 5:c8918e47c566 | 3725 | tbl[4] = sys; |
mkilivan | 5:c8918e47c566 | 3726 | if (disk_write(pdrv, fs->win, 0, 1) != RES_OK) return FR_DISK_ERR; |
mkilivan | 5:c8918e47c566 | 3727 | md = 0xF8; |
mkilivan | 5:c8918e47c566 | 3728 | } else { |
mkilivan | 5:c8918e47c566 | 3729 | if (sfd) { /* No partition table (SFD) */ |
mkilivan | 5:c8918e47c566 | 3730 | md = 0xF0; |
mkilivan | 5:c8918e47c566 | 3731 | } else { /* Create partition table (FDISK) */ |
mkilivan | 5:c8918e47c566 | 3732 | mem_set(fs->win, 0, SS(fs)); |
mkilivan | 5:c8918e47c566 | 3733 | tbl = fs->win+MBR_Table; /* Create partition table for single partition in the drive */ |
mkilivan | 5:c8918e47c566 | 3734 | tbl[1] = 1; /* Partition start head */ |
mkilivan | 5:c8918e47c566 | 3735 | tbl[2] = 1; /* Partition start sector */ |
mkilivan | 5:c8918e47c566 | 3736 | tbl[3] = 0; /* Partition start cylinder */ |
mkilivan | 5:c8918e47c566 | 3737 | tbl[4] = sys; /* System type */ |
mkilivan | 5:c8918e47c566 | 3738 | tbl[5] = 254; /* Partition end head */ |
mkilivan | 5:c8918e47c566 | 3739 | n = (b_vol + n_vol) / 63 / 255; |
mkilivan | 5:c8918e47c566 | 3740 | tbl[6] = (BYTE)((n >> 2) | 63); /* Partition end sector */ |
mkilivan | 5:c8918e47c566 | 3741 | tbl[7] = (BYTE)n; /* End cylinder */ |
mkilivan | 5:c8918e47c566 | 3742 | ST_DWORD(tbl+8, 63); /* Partition start in LBA */ |
mkilivan | 5:c8918e47c566 | 3743 | ST_DWORD(tbl+12, n_vol); /* Partition size in LBA */ |
mkilivan | 5:c8918e47c566 | 3744 | ST_WORD(fs->win+BS_55AA, 0xAA55); /* MBR signature */ |
mkilivan | 5:c8918e47c566 | 3745 | if (disk_write(pdrv, fs->win, 0, 1) != RES_OK) /* Write it to the MBR sector */ |
mkilivan | 5:c8918e47c566 | 3746 | return FR_DISK_ERR; |
mkilivan | 5:c8918e47c566 | 3747 | md = 0xF8; |
mkilivan | 5:c8918e47c566 | 3748 | } |
mkilivan | 5:c8918e47c566 | 3749 | } |
mkilivan | 5:c8918e47c566 | 3750 | |
mkilivan | 5:c8918e47c566 | 3751 | /* Create BPB in the VBR */ |
mkilivan | 5:c8918e47c566 | 3752 | tbl = fs->win; /* Clear sector */ |
mkilivan | 5:c8918e47c566 | 3753 | mem_set(tbl, 0, SS(fs)); |
mkilivan | 5:c8918e47c566 | 3754 | mem_cpy(tbl, "\xEB\xFE\x90" "MSDOS5.0", 11);/* Boot jump code, OEM name */ |
mkilivan | 5:c8918e47c566 | 3755 | i = SS(fs); /* Sector size */ |
mkilivan | 5:c8918e47c566 | 3756 | ST_WORD(tbl+BPB_BytsPerSec, i); |
mkilivan | 5:c8918e47c566 | 3757 | tbl[BPB_SecPerClus] = (BYTE)au; /* Sectors per cluster */ |
mkilivan | 5:c8918e47c566 | 3758 | ST_WORD(tbl+BPB_RsvdSecCnt, n_rsv); /* Reserved sectors */ |
mkilivan | 5:c8918e47c566 | 3759 | tbl[BPB_NumFATs] = N_FATS; /* Number of FATs */ |
mkilivan | 5:c8918e47c566 | 3760 | i = (fmt == FS_FAT32) ? 0 : N_ROOTDIR; /* Number of rootdir entries */ |
mkilivan | 5:c8918e47c566 | 3761 | ST_WORD(tbl+BPB_RootEntCnt, i); |
mkilivan | 5:c8918e47c566 | 3762 | if (n_vol < 0x10000) { /* Number of total sectors */ |
mkilivan | 5:c8918e47c566 | 3763 | ST_WORD(tbl+BPB_TotSec16, n_vol); |
mkilivan | 5:c8918e47c566 | 3764 | } else { |
mkilivan | 5:c8918e47c566 | 3765 | ST_DWORD(tbl+BPB_TotSec32, n_vol); |
mkilivan | 5:c8918e47c566 | 3766 | } |
mkilivan | 5:c8918e47c566 | 3767 | tbl[BPB_Media] = md; /* Media descriptor */ |
mkilivan | 5:c8918e47c566 | 3768 | ST_WORD(tbl+BPB_SecPerTrk, 63); /* Number of sectors per track */ |
mkilivan | 5:c8918e47c566 | 3769 | ST_WORD(tbl+BPB_NumHeads, 255); /* Number of heads */ |
mkilivan | 5:c8918e47c566 | 3770 | ST_DWORD(tbl+BPB_HiddSec, b_vol); /* Hidden sectors */ |
mkilivan | 5:c8918e47c566 | 3771 | n = get_fattime(); /* Use current time as VSN */ |
mkilivan | 5:c8918e47c566 | 3772 | if (fmt == FS_FAT32) { |
mkilivan | 5:c8918e47c566 | 3773 | ST_DWORD(tbl+BS_VolID32, n); /* VSN */ |
mkilivan | 5:c8918e47c566 | 3774 | ST_DWORD(tbl+BPB_FATSz32, n_fat); /* Number of sectors per FAT */ |
mkilivan | 5:c8918e47c566 | 3775 | ST_DWORD(tbl+BPB_RootClus, 2); /* Root directory start cluster (2) */ |
mkilivan | 5:c8918e47c566 | 3776 | ST_WORD(tbl+BPB_FSInfo, 1); /* FSInfo record offset (VBR+1) */ |
mkilivan | 5:c8918e47c566 | 3777 | ST_WORD(tbl+BPB_BkBootSec, 6); /* Backup boot record offset (VBR+6) */ |
mkilivan | 5:c8918e47c566 | 3778 | tbl[BS_DrvNum32] = 0x80; /* Drive number */ |
mkilivan | 5:c8918e47c566 | 3779 | tbl[BS_BootSig32] = 0x29; /* Extended boot signature */ |
mkilivan | 5:c8918e47c566 | 3780 | mem_cpy(tbl+BS_VolLab32, "NO NAME " "FAT32 ", 19); /* Volume label, FAT signature */ |
mkilivan | 5:c8918e47c566 | 3781 | } else { |
mkilivan | 5:c8918e47c566 | 3782 | ST_DWORD(tbl+BS_VolID, n); /* VSN */ |
mkilivan | 5:c8918e47c566 | 3783 | ST_WORD(tbl+BPB_FATSz16, n_fat); /* Number of sectors per FAT */ |
mkilivan | 5:c8918e47c566 | 3784 | tbl[BS_DrvNum] = 0x80; /* Drive number */ |
mkilivan | 5:c8918e47c566 | 3785 | tbl[BS_BootSig] = 0x29; /* Extended boot signature */ |
mkilivan | 5:c8918e47c566 | 3786 | mem_cpy(tbl+BS_VolLab, "NO NAME " "FAT ", 19); /* Volume label, FAT signature */ |
mkilivan | 5:c8918e47c566 | 3787 | } |
mkilivan | 5:c8918e47c566 | 3788 | ST_WORD(tbl+BS_55AA, 0xAA55); /* Signature (Offset is fixed here regardless of sector size) */ |
mkilivan | 5:c8918e47c566 | 3789 | if (disk_write(pdrv, tbl, b_vol, 1) != RES_OK) /* Write it to the VBR sector */ |
mkilivan | 5:c8918e47c566 | 3790 | return FR_DISK_ERR; |
mkilivan | 5:c8918e47c566 | 3791 | if (fmt == FS_FAT32) /* Write backup VBR if needed (VBR+6) */ |
mkilivan | 5:c8918e47c566 | 3792 | disk_write(pdrv, tbl, b_vol + 6, 1); |
mkilivan | 5:c8918e47c566 | 3793 | |
mkilivan | 5:c8918e47c566 | 3794 | /* Initialize FAT area */ |
mkilivan | 5:c8918e47c566 | 3795 | wsect = b_fat; |
mkilivan | 5:c8918e47c566 | 3796 | for (i = 0; i < N_FATS; i++) { /* Initialize each FAT copy */ |
mkilivan | 5:c8918e47c566 | 3797 | mem_set(tbl, 0, SS(fs)); /* 1st sector of the FAT */ |
mkilivan | 5:c8918e47c566 | 3798 | n = md; /* Media descriptor byte */ |
mkilivan | 5:c8918e47c566 | 3799 | if (fmt != FS_FAT32) { |
mkilivan | 5:c8918e47c566 | 3800 | n |= (fmt == FS_FAT12) ? 0x00FFFF00 : 0xFFFFFF00; |
mkilivan | 5:c8918e47c566 | 3801 | ST_DWORD(tbl+0, n); /* Reserve cluster #0-1 (FAT12/16) */ |
mkilivan | 5:c8918e47c566 | 3802 | } else { |
mkilivan | 5:c8918e47c566 | 3803 | n |= 0xFFFFFF00; |
mkilivan | 5:c8918e47c566 | 3804 | ST_DWORD(tbl+0, n); /* Reserve cluster #0-1 (FAT32) */ |
mkilivan | 5:c8918e47c566 | 3805 | ST_DWORD(tbl+4, 0xFFFFFFFF); |
mkilivan | 5:c8918e47c566 | 3806 | ST_DWORD(tbl+8, 0x0FFFFFFF); /* Reserve cluster #2 for root dir */ |
mkilivan | 5:c8918e47c566 | 3807 | } |
mkilivan | 5:c8918e47c566 | 3808 | if (disk_write(pdrv, tbl, wsect++, 1) != RES_OK) |
mkilivan | 5:c8918e47c566 | 3809 | return FR_DISK_ERR; |
mkilivan | 5:c8918e47c566 | 3810 | mem_set(tbl, 0, SS(fs)); /* Fill following FAT entries with zero */ |
mkilivan | 5:c8918e47c566 | 3811 | for (n = 1; n < n_fat; n++) { /* This loop may take a time on FAT32 volume due to many single sector writes */ |
mkilivan | 5:c8918e47c566 | 3812 | if (disk_write(pdrv, tbl, wsect++, 1) != RES_OK) |
mkilivan | 5:c8918e47c566 | 3813 | return FR_DISK_ERR; |
mkilivan | 5:c8918e47c566 | 3814 | } |
mkilivan | 5:c8918e47c566 | 3815 | } |
mkilivan | 5:c8918e47c566 | 3816 | |
mkilivan | 5:c8918e47c566 | 3817 | /* Initialize root directory */ |
mkilivan | 5:c8918e47c566 | 3818 | i = (fmt == FS_FAT32) ? au : n_dir; |
mkilivan | 5:c8918e47c566 | 3819 | do { |
mkilivan | 5:c8918e47c566 | 3820 | if (disk_write(pdrv, tbl, wsect++, 1) != RES_OK) |
mkilivan | 5:c8918e47c566 | 3821 | return FR_DISK_ERR; |
mkilivan | 5:c8918e47c566 | 3822 | } while (--i); |
mkilivan | 5:c8918e47c566 | 3823 | |
mkilivan | 5:c8918e47c566 | 3824 | #if _USE_ERASE /* Erase data area if needed */ |
mkilivan | 5:c8918e47c566 | 3825 | { |
mkilivan | 5:c8918e47c566 | 3826 | DWORD eb[2]; |
mkilivan | 5:c8918e47c566 | 3827 | |
mkilivan | 5:c8918e47c566 | 3828 | eb[0] = wsect; eb[1] = wsect + (n_clst - ((fmt == FS_FAT32) ? 1 : 0)) * au - 1; |
mkilivan | 5:c8918e47c566 | 3829 | disk_ioctl(pdrv, CTRL_ERASE_SECTOR, eb); |
mkilivan | 5:c8918e47c566 | 3830 | } |
mkilivan | 5:c8918e47c566 | 3831 | #endif |
mkilivan | 5:c8918e47c566 | 3832 | |
mkilivan | 5:c8918e47c566 | 3833 | /* Create FSInfo if needed */ |
mkilivan | 5:c8918e47c566 | 3834 | if (fmt == FS_FAT32) { |
mkilivan | 5:c8918e47c566 | 3835 | ST_DWORD(tbl+FSI_LeadSig, 0x41615252); |
mkilivan | 5:c8918e47c566 | 3836 | ST_DWORD(tbl+FSI_StrucSig, 0x61417272); |
mkilivan | 5:c8918e47c566 | 3837 | ST_DWORD(tbl+FSI_Free_Count, n_clst - 1); /* Number of free clusters */ |
mkilivan | 5:c8918e47c566 | 3838 | ST_DWORD(tbl+FSI_Nxt_Free, 2); /* Last allocated cluster# */ |
mkilivan | 5:c8918e47c566 | 3839 | ST_WORD(tbl+BS_55AA, 0xAA55); |
mkilivan | 5:c8918e47c566 | 3840 | disk_write(pdrv, tbl, b_vol + 1, 1); /* Write original (VBR+1) */ |
mkilivan | 5:c8918e47c566 | 3841 | disk_write(pdrv, tbl, b_vol + 7, 1); /* Write backup (VBR+7) */ |
mkilivan | 5:c8918e47c566 | 3842 | } |
mkilivan | 5:c8918e47c566 | 3843 | |
mkilivan | 5:c8918e47c566 | 3844 | return (disk_ioctl(pdrv, CTRL_SYNC, 0) == RES_OK) ? FR_OK : FR_DISK_ERR; |
mkilivan | 5:c8918e47c566 | 3845 | } |
mkilivan | 5:c8918e47c566 | 3846 | |
mkilivan | 5:c8918e47c566 | 3847 | |
mkilivan | 5:c8918e47c566 | 3848 | #if _MULTI_PARTITION == 2 |
mkilivan | 5:c8918e47c566 | 3849 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 3850 | /* Divide Physical Drive */ |
mkilivan | 5:c8918e47c566 | 3851 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 3852 | |
mkilivan | 5:c8918e47c566 | 3853 | FRESULT f_fdisk ( |
mkilivan | 5:c8918e47c566 | 3854 | BYTE pdrv, /* Physical drive number */ |
mkilivan | 5:c8918e47c566 | 3855 | const DWORD szt[], /* Pointer to the size table for each partitions */ |
mkilivan | 5:c8918e47c566 | 3856 | void* work /* Pointer to the working buffer */ |
mkilivan | 5:c8918e47c566 | 3857 | ) |
mkilivan | 5:c8918e47c566 | 3858 | { |
mkilivan | 5:c8918e47c566 | 3859 | UINT i, n, sz_cyl, tot_cyl, b_cyl, e_cyl, p_cyl; |
mkilivan | 5:c8918e47c566 | 3860 | BYTE s_hd, e_hd, *p, *buf = (BYTE*)work; |
mkilivan | 5:c8918e47c566 | 3861 | DSTATUS stat; |
mkilivan | 5:c8918e47c566 | 3862 | DWORD sz_disk, sz_part, s_part; |
mkilivan | 5:c8918e47c566 | 3863 | |
mkilivan | 5:c8918e47c566 | 3864 | |
mkilivan | 5:c8918e47c566 | 3865 | stat = disk_initialize(pdrv); |
mkilivan | 5:c8918e47c566 | 3866 | if (stat & STA_NOINIT) return FR_NOT_READY; |
mkilivan | 5:c8918e47c566 | 3867 | if (stat & STA_PROTECT) return FR_WRITE_PROTECTED; |
mkilivan | 5:c8918e47c566 | 3868 | if (disk_ioctl(pdrv, GET_SECTOR_COUNT, &sz_disk)) return FR_DISK_ERR; |
mkilivan | 5:c8918e47c566 | 3869 | |
mkilivan | 5:c8918e47c566 | 3870 | /* Determine CHS in the table regardless of the drive geometry */ |
mkilivan | 5:c8918e47c566 | 3871 | for (n = 16; n < 256 && sz_disk / n / 63 > 1024; n *= 2) ; |
mkilivan | 5:c8918e47c566 | 3872 | if (n == 256) n--; |
mkilivan | 5:c8918e47c566 | 3873 | e_hd = n - 1; |
mkilivan | 5:c8918e47c566 | 3874 | sz_cyl = 63 * n; |
mkilivan | 5:c8918e47c566 | 3875 | tot_cyl = sz_disk / sz_cyl; |
mkilivan | 5:c8918e47c566 | 3876 | |
mkilivan | 5:c8918e47c566 | 3877 | /* Create partition table */ |
mkilivan | 5:c8918e47c566 | 3878 | mem_set(buf, 0, _MAX_SS); |
mkilivan | 5:c8918e47c566 | 3879 | p = buf + MBR_Table; b_cyl = 0; |
mkilivan | 5:c8918e47c566 | 3880 | for (i = 0; i < 4; i++, p += SZ_PTE) { |
mkilivan | 5:c8918e47c566 | 3881 | p_cyl = (szt[i] <= 100) ? (DWORD)tot_cyl * szt[i] / 100 : szt[i] / sz_cyl; |
mkilivan | 5:c8918e47c566 | 3882 | if (!p_cyl) continue; |
mkilivan | 5:c8918e47c566 | 3883 | s_part = (DWORD)sz_cyl * b_cyl; |
mkilivan | 5:c8918e47c566 | 3884 | sz_part = (DWORD)sz_cyl * p_cyl; |
mkilivan | 5:c8918e47c566 | 3885 | if (i == 0) { /* Exclude first track of cylinder 0 */ |
mkilivan | 5:c8918e47c566 | 3886 | s_hd = 1; |
mkilivan | 5:c8918e47c566 | 3887 | s_part += 63; sz_part -= 63; |
mkilivan | 5:c8918e47c566 | 3888 | } else { |
mkilivan | 5:c8918e47c566 | 3889 | s_hd = 0; |
mkilivan | 5:c8918e47c566 | 3890 | } |
mkilivan | 5:c8918e47c566 | 3891 | e_cyl = b_cyl + p_cyl - 1; |
mkilivan | 5:c8918e47c566 | 3892 | if (e_cyl >= tot_cyl) return FR_INVALID_PARAMETER; |
mkilivan | 5:c8918e47c566 | 3893 | |
mkilivan | 5:c8918e47c566 | 3894 | /* Set partition table */ |
mkilivan | 5:c8918e47c566 | 3895 | p[1] = s_hd; /* Start head */ |
mkilivan | 5:c8918e47c566 | 3896 | p[2] = (BYTE)((b_cyl >> 2) + 1); /* Start sector */ |
mkilivan | 5:c8918e47c566 | 3897 | p[3] = (BYTE)b_cyl; /* Start cylinder */ |
mkilivan | 5:c8918e47c566 | 3898 | p[4] = 0x06; /* System type (temporary setting) */ |
mkilivan | 5:c8918e47c566 | 3899 | p[5] = e_hd; /* End head */ |
mkilivan | 5:c8918e47c566 | 3900 | p[6] = (BYTE)((e_cyl >> 2) + 63); /* End sector */ |
mkilivan | 5:c8918e47c566 | 3901 | p[7] = (BYTE)e_cyl; /* End cylinder */ |
mkilivan | 5:c8918e47c566 | 3902 | ST_DWORD(p + 8, s_part); /* Start sector in LBA */ |
mkilivan | 5:c8918e47c566 | 3903 | ST_DWORD(p + 12, sz_part); /* Partition size */ |
mkilivan | 5:c8918e47c566 | 3904 | |
mkilivan | 5:c8918e47c566 | 3905 | /* Next partition */ |
mkilivan | 5:c8918e47c566 | 3906 | b_cyl += p_cyl; |
mkilivan | 5:c8918e47c566 | 3907 | } |
mkilivan | 5:c8918e47c566 | 3908 | ST_WORD(p, 0xAA55); |
mkilivan | 5:c8918e47c566 | 3909 | |
mkilivan | 5:c8918e47c566 | 3910 | /* Write it to the MBR */ |
mkilivan | 5:c8918e47c566 | 3911 | return (disk_write(pdrv, buf, 0, 1) || disk_ioctl(pdrv, CTRL_SYNC, 0)) ? FR_DISK_ERR : FR_OK; |
mkilivan | 5:c8918e47c566 | 3912 | } |
mkilivan | 5:c8918e47c566 | 3913 | |
mkilivan | 5:c8918e47c566 | 3914 | |
mkilivan | 5:c8918e47c566 | 3915 | #endif /* _MULTI_PARTITION == 2 */ |
mkilivan | 5:c8918e47c566 | 3916 | #endif /* _USE_MKFS && !_FS_READONLY */ |
mkilivan | 5:c8918e47c566 | 3917 | |
mkilivan | 5:c8918e47c566 | 3918 | |
mkilivan | 5:c8918e47c566 | 3919 | |
mkilivan | 5:c8918e47c566 | 3920 | |
mkilivan | 5:c8918e47c566 | 3921 | #if _USE_STRFUNC |
mkilivan | 5:c8918e47c566 | 3922 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 3923 | /* Get a string from the file */ |
mkilivan | 5:c8918e47c566 | 3924 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 3925 | TCHAR* f_gets ( |
mkilivan | 5:c8918e47c566 | 3926 | TCHAR* buff, /* Pointer to the string buffer to read */ |
mkilivan | 5:c8918e47c566 | 3927 | int len, /* Size of string buffer (characters) */ |
mkilivan | 5:c8918e47c566 | 3928 | FIL* fil /* Pointer to the file object */ |
mkilivan | 5:c8918e47c566 | 3929 | ) |
mkilivan | 5:c8918e47c566 | 3930 | { |
mkilivan | 5:c8918e47c566 | 3931 | int n = 0; |
mkilivan | 5:c8918e47c566 | 3932 | TCHAR c, *p = buff; |
mkilivan | 5:c8918e47c566 | 3933 | BYTE s[2]; |
mkilivan | 5:c8918e47c566 | 3934 | UINT rc; |
mkilivan | 5:c8918e47c566 | 3935 | |
mkilivan | 5:c8918e47c566 | 3936 | |
mkilivan | 5:c8918e47c566 | 3937 | while (n < len - 1) { /* Read bytes until buffer gets filled */ |
mkilivan | 5:c8918e47c566 | 3938 | f_read(fil, s, 1, &rc); |
mkilivan | 5:c8918e47c566 | 3939 | if (rc != 1) break; /* Break on EOF or error */ |
mkilivan | 5:c8918e47c566 | 3940 | c = s[0]; |
mkilivan | 5:c8918e47c566 | 3941 | #if _LFN_UNICODE /* Read a character in UTF-8 encoding */ |
mkilivan | 5:c8918e47c566 | 3942 | if (c >= 0x80) { |
mkilivan | 5:c8918e47c566 | 3943 | if (c < 0xC0) continue; /* Skip stray trailer */ |
mkilivan | 5:c8918e47c566 | 3944 | if (c < 0xE0) { /* Two-byte sequence */ |
mkilivan | 5:c8918e47c566 | 3945 | f_read(fil, s, 1, &rc); |
mkilivan | 5:c8918e47c566 | 3946 | if (rc != 1) break; |
mkilivan | 5:c8918e47c566 | 3947 | c = ((c & 0x1F) << 6) | (s[0] & 0x3F); |
mkilivan | 5:c8918e47c566 | 3948 | if (c < 0x80) c = '?'; |
mkilivan | 5:c8918e47c566 | 3949 | } else { |
mkilivan | 5:c8918e47c566 | 3950 | if (c < 0xF0) { /* Three-byte sequence */ |
mkilivan | 5:c8918e47c566 | 3951 | f_read(fil, s, 2, &rc); |
mkilivan | 5:c8918e47c566 | 3952 | if (rc != 2) break; |
mkilivan | 5:c8918e47c566 | 3953 | c = (c << 12) | ((s[0] & 0x3F) << 6) | (s[1] & 0x3F); |
mkilivan | 5:c8918e47c566 | 3954 | if (c < 0x800) c = '?'; |
mkilivan | 5:c8918e47c566 | 3955 | } else { /* Reject four-byte sequence */ |
mkilivan | 5:c8918e47c566 | 3956 | c = '?'; |
mkilivan | 5:c8918e47c566 | 3957 | } |
mkilivan | 5:c8918e47c566 | 3958 | } |
mkilivan | 5:c8918e47c566 | 3959 | } |
mkilivan | 5:c8918e47c566 | 3960 | #endif |
mkilivan | 5:c8918e47c566 | 3961 | #if _USE_STRFUNC >= 2 |
mkilivan | 5:c8918e47c566 | 3962 | if (c == '\r') continue; /* Strip '\r' */ |
mkilivan | 5:c8918e47c566 | 3963 | #endif |
mkilivan | 5:c8918e47c566 | 3964 | *p++ = c; |
mkilivan | 5:c8918e47c566 | 3965 | n++; |
mkilivan | 5:c8918e47c566 | 3966 | if (c == '\n') break; /* Break on EOL */ |
mkilivan | 5:c8918e47c566 | 3967 | } |
mkilivan | 5:c8918e47c566 | 3968 | *p = 0; |
mkilivan | 5:c8918e47c566 | 3969 | return n ? buff : 0; /* When no data read (eof or error), return with error. */ |
mkilivan | 5:c8918e47c566 | 3970 | } |
mkilivan | 5:c8918e47c566 | 3971 | |
mkilivan | 5:c8918e47c566 | 3972 | |
mkilivan | 5:c8918e47c566 | 3973 | |
mkilivan | 5:c8918e47c566 | 3974 | #if !_FS_READONLY |
mkilivan | 5:c8918e47c566 | 3975 | #include <stdarg.h> |
mkilivan | 5:c8918e47c566 | 3976 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 3977 | /* Put a character to the file */ |
mkilivan | 5:c8918e47c566 | 3978 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 3979 | int f_putc ( |
mkilivan | 5:c8918e47c566 | 3980 | TCHAR c, /* A character to be output */ |
mkilivan | 5:c8918e47c566 | 3981 | FIL* fil /* Pointer to the file object */ |
mkilivan | 5:c8918e47c566 | 3982 | ) |
mkilivan | 5:c8918e47c566 | 3983 | { |
mkilivan | 5:c8918e47c566 | 3984 | UINT bw, btw; |
mkilivan | 5:c8918e47c566 | 3985 | BYTE s[3]; |
mkilivan | 5:c8918e47c566 | 3986 | |
mkilivan | 5:c8918e47c566 | 3987 | |
mkilivan | 5:c8918e47c566 | 3988 | #if _USE_STRFUNC >= 2 |
mkilivan | 5:c8918e47c566 | 3989 | if (c == '\n') f_putc ('\r', fil); /* LF -> CRLF conversion */ |
mkilivan | 5:c8918e47c566 | 3990 | #endif |
mkilivan | 5:c8918e47c566 | 3991 | |
mkilivan | 5:c8918e47c566 | 3992 | #if _LFN_UNICODE /* Write the character in UTF-8 encoding */ |
mkilivan | 5:c8918e47c566 | 3993 | if (c < 0x80) { /* 7-bit */ |
mkilivan | 5:c8918e47c566 | 3994 | s[0] = (BYTE)c; |
mkilivan | 5:c8918e47c566 | 3995 | btw = 1; |
mkilivan | 5:c8918e47c566 | 3996 | } else { |
mkilivan | 5:c8918e47c566 | 3997 | if (c < 0x800) { /* 11-bit */ |
mkilivan | 5:c8918e47c566 | 3998 | s[0] = (BYTE)(0xC0 | (c >> 6)); |
mkilivan | 5:c8918e47c566 | 3999 | s[1] = (BYTE)(0x80 | (c & 0x3F)); |
mkilivan | 5:c8918e47c566 | 4000 | btw = 2; |
mkilivan | 5:c8918e47c566 | 4001 | } else { /* 16-bit */ |
mkilivan | 5:c8918e47c566 | 4002 | s[0] = (BYTE)(0xE0 | (c >> 12)); |
mkilivan | 5:c8918e47c566 | 4003 | s[1] = (BYTE)(0x80 | ((c >> 6) & 0x3F)); |
mkilivan | 5:c8918e47c566 | 4004 | s[2] = (BYTE)(0x80 | (c & 0x3F)); |
mkilivan | 5:c8918e47c566 | 4005 | btw = 3; |
mkilivan | 5:c8918e47c566 | 4006 | } |
mkilivan | 5:c8918e47c566 | 4007 | } |
mkilivan | 5:c8918e47c566 | 4008 | #else /* Write the character without conversion */ |
mkilivan | 5:c8918e47c566 | 4009 | s[0] = (BYTE)c; |
mkilivan | 5:c8918e47c566 | 4010 | btw = 1; |
mkilivan | 5:c8918e47c566 | 4011 | #endif |
mkilivan | 5:c8918e47c566 | 4012 | f_write(fil, s, btw, &bw); /* Write the char to the file */ |
mkilivan | 5:c8918e47c566 | 4013 | return (bw == btw) ? 1 : EOF; /* Return the result */ |
mkilivan | 5:c8918e47c566 | 4014 | } |
mkilivan | 5:c8918e47c566 | 4015 | |
mkilivan | 5:c8918e47c566 | 4016 | |
mkilivan | 5:c8918e47c566 | 4017 | |
mkilivan | 5:c8918e47c566 | 4018 | |
mkilivan | 5:c8918e47c566 | 4019 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 4020 | /* Put a string to the file */ |
mkilivan | 5:c8918e47c566 | 4021 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 4022 | int f_puts ( |
mkilivan | 5:c8918e47c566 | 4023 | const TCHAR* str, /* Pointer to the string to be output */ |
mkilivan | 5:c8918e47c566 | 4024 | FIL* fil /* Pointer to the file object */ |
mkilivan | 5:c8918e47c566 | 4025 | ) |
mkilivan | 5:c8918e47c566 | 4026 | { |
mkilivan | 5:c8918e47c566 | 4027 | int n; |
mkilivan | 5:c8918e47c566 | 4028 | |
mkilivan | 5:c8918e47c566 | 4029 | |
mkilivan | 5:c8918e47c566 | 4030 | for (n = 0; *str; str++, n++) { |
mkilivan | 5:c8918e47c566 | 4031 | if (f_putc(*str, fil) == EOF) return EOF; |
mkilivan | 5:c8918e47c566 | 4032 | } |
mkilivan | 5:c8918e47c566 | 4033 | return n; |
mkilivan | 5:c8918e47c566 | 4034 | } |
mkilivan | 5:c8918e47c566 | 4035 | |
mkilivan | 5:c8918e47c566 | 4036 | |
mkilivan | 5:c8918e47c566 | 4037 | |
mkilivan | 5:c8918e47c566 | 4038 | |
mkilivan | 5:c8918e47c566 | 4039 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 4040 | /* Put a formatted string to the file */ |
mkilivan | 5:c8918e47c566 | 4041 | /*-----------------------------------------------------------------------*/ |
mkilivan | 5:c8918e47c566 | 4042 | int f_printf ( |
mkilivan | 5:c8918e47c566 | 4043 | FIL* fil, /* Pointer to the file object */ |
mkilivan | 5:c8918e47c566 | 4044 | const TCHAR* str, /* Pointer to the format string */ |
mkilivan | 5:c8918e47c566 | 4045 | ... /* Optional arguments... */ |
mkilivan | 5:c8918e47c566 | 4046 | ) |
mkilivan | 5:c8918e47c566 | 4047 | { |
mkilivan | 5:c8918e47c566 | 4048 | va_list arp; |
mkilivan | 5:c8918e47c566 | 4049 | BYTE f, r; |
mkilivan | 5:c8918e47c566 | 4050 | UINT i, j, w; |
mkilivan | 5:c8918e47c566 | 4051 | ULONG v; |
mkilivan | 5:c8918e47c566 | 4052 | TCHAR c, d, s[16], *p; |
mkilivan | 5:c8918e47c566 | 4053 | int res, chc, cc; |
mkilivan | 5:c8918e47c566 | 4054 | |
mkilivan | 5:c8918e47c566 | 4055 | |
mkilivan | 5:c8918e47c566 | 4056 | va_start(arp, str); |
mkilivan | 5:c8918e47c566 | 4057 | |
mkilivan | 5:c8918e47c566 | 4058 | for (cc = res = 0; cc != EOF; res += cc) { |
mkilivan | 5:c8918e47c566 | 4059 | c = *str++; |
mkilivan | 5:c8918e47c566 | 4060 | if (c == 0) break; /* End of string */ |
mkilivan | 5:c8918e47c566 | 4061 | if (c != '%') { /* Non escape character */ |
mkilivan | 5:c8918e47c566 | 4062 | cc = f_putc(c, fil); |
mkilivan | 5:c8918e47c566 | 4063 | if (cc != EOF) cc = 1; |
mkilivan | 5:c8918e47c566 | 4064 | continue; |
mkilivan | 5:c8918e47c566 | 4065 | } |
mkilivan | 5:c8918e47c566 | 4066 | w = f = 0; |
mkilivan | 5:c8918e47c566 | 4067 | c = *str++; |
mkilivan | 5:c8918e47c566 | 4068 | if (c == '0') { /* Flag: '0' padding */ |
mkilivan | 5:c8918e47c566 | 4069 | f = 1; c = *str++; |
mkilivan | 5:c8918e47c566 | 4070 | } else { |
mkilivan | 5:c8918e47c566 | 4071 | if (c == '-') { /* Flag: left justified */ |
mkilivan | 5:c8918e47c566 | 4072 | f = 2; c = *str++; |
mkilivan | 5:c8918e47c566 | 4073 | } |
mkilivan | 5:c8918e47c566 | 4074 | } |
mkilivan | 5:c8918e47c566 | 4075 | while (IsDigit(c)) { /* Precision */ |
mkilivan | 5:c8918e47c566 | 4076 | w = w * 10 + c - '0'; |
mkilivan | 5:c8918e47c566 | 4077 | c = *str++; |
mkilivan | 5:c8918e47c566 | 4078 | } |
mkilivan | 5:c8918e47c566 | 4079 | if (c == 'l' || c == 'L') { /* Prefix: Size is long int */ |
mkilivan | 5:c8918e47c566 | 4080 | f |= 4; c = *str++; |
mkilivan | 5:c8918e47c566 | 4081 | } |
mkilivan | 5:c8918e47c566 | 4082 | if (!c) break; |
mkilivan | 5:c8918e47c566 | 4083 | d = c; |
mkilivan | 5:c8918e47c566 | 4084 | if (IsLower(d)) d -= 0x20; |
mkilivan | 5:c8918e47c566 | 4085 | switch (d) { /* Type is... */ |
mkilivan | 5:c8918e47c566 | 4086 | case 'S' : /* String */ |
mkilivan | 5:c8918e47c566 | 4087 | p = va_arg(arp, TCHAR*); |
mkilivan | 5:c8918e47c566 | 4088 | for (j = 0; p[j]; j++) ; |
mkilivan | 5:c8918e47c566 | 4089 | chc = 0; |
mkilivan | 5:c8918e47c566 | 4090 | if (!(f & 2)) { |
mkilivan | 5:c8918e47c566 | 4091 | while (j++ < w) chc += (cc = f_putc(' ', fil)); |
mkilivan | 5:c8918e47c566 | 4092 | } |
mkilivan | 5:c8918e47c566 | 4093 | chc += (cc = f_puts(p, fil)); |
mkilivan | 5:c8918e47c566 | 4094 | while (j++ < w) chc += (cc = f_putc(' ', fil)); |
mkilivan | 5:c8918e47c566 | 4095 | if (cc != EOF) cc = chc; |
mkilivan | 5:c8918e47c566 | 4096 | continue; |
mkilivan | 5:c8918e47c566 | 4097 | case 'C' : /* Character */ |
mkilivan | 5:c8918e47c566 | 4098 | cc = f_putc((TCHAR)va_arg(arp, int), fil); continue; |
mkilivan | 5:c8918e47c566 | 4099 | case 'B' : /* Binary */ |
mkilivan | 5:c8918e47c566 | 4100 | r = 2; break; |
mkilivan | 5:c8918e47c566 | 4101 | case 'O' : /* Octal */ |
mkilivan | 5:c8918e47c566 | 4102 | r = 8; break; |
mkilivan | 5:c8918e47c566 | 4103 | case 'D' : /* Signed decimal */ |
mkilivan | 5:c8918e47c566 | 4104 | case 'U' : /* Unsigned decimal */ |
mkilivan | 5:c8918e47c566 | 4105 | r = 10; break; |
mkilivan | 5:c8918e47c566 | 4106 | case 'X' : /* Hexdecimal */ |
mkilivan | 5:c8918e47c566 | 4107 | r = 16; break; |
mkilivan | 5:c8918e47c566 | 4108 | default: /* Unknown type (pass-through) */ |
mkilivan | 5:c8918e47c566 | 4109 | cc = f_putc(c, fil); continue; |
mkilivan | 5:c8918e47c566 | 4110 | } |
mkilivan | 5:c8918e47c566 | 4111 | |
mkilivan | 5:c8918e47c566 | 4112 | /* Get an argument and put it in numeral */ |
mkilivan | 5:c8918e47c566 | 4113 | v = (f & 4) ? (ULONG)va_arg(arp, long) : ((d == 'D') ? (ULONG)(long)va_arg(arp, int) : (ULONG)va_arg(arp, unsigned int)); |
mkilivan | 5:c8918e47c566 | 4114 | if (d == 'D' && (v & 0x80000000)) { |
mkilivan | 5:c8918e47c566 | 4115 | v = 0 - v; |
mkilivan | 5:c8918e47c566 | 4116 | f |= 8; |
mkilivan | 5:c8918e47c566 | 4117 | } |
mkilivan | 5:c8918e47c566 | 4118 | i = 0; |
mkilivan | 5:c8918e47c566 | 4119 | do { |
mkilivan | 5:c8918e47c566 | 4120 | d = (TCHAR)(v % r); v /= r; |
mkilivan | 5:c8918e47c566 | 4121 | if (d > 9) d += (c == 'x') ? 0x27 : 0x07; |
mkilivan | 5:c8918e47c566 | 4122 | s[i++] = d + '0'; |
mkilivan | 5:c8918e47c566 | 4123 | } while (v && i < sizeof s / sizeof s[0]); |
mkilivan | 5:c8918e47c566 | 4124 | if (f & 8) s[i++] = '-'; |
mkilivan | 5:c8918e47c566 | 4125 | j = i; d = (f & 1) ? '0' : ' '; |
mkilivan | 5:c8918e47c566 | 4126 | res = 0; |
mkilivan | 5:c8918e47c566 | 4127 | while (!(f & 2) && j++ < w) res += (cc = f_putc(d, fil)); |
mkilivan | 5:c8918e47c566 | 4128 | do res += (cc = f_putc(s[--i], fil)); while(i); |
mkilivan | 5:c8918e47c566 | 4129 | while (j++ < w) res += (cc = f_putc(' ', fil)); |
mkilivan | 5:c8918e47c566 | 4130 | if (cc != EOF) cc = res; |
mkilivan | 5:c8918e47c566 | 4131 | } |
mkilivan | 5:c8918e47c566 | 4132 | |
mkilivan | 5:c8918e47c566 | 4133 | va_end(arp); |
mkilivan | 5:c8918e47c566 | 4134 | return (cc == EOF) ? cc : res; |
mkilivan | 5:c8918e47c566 | 4135 | } |
mkilivan | 5:c8918e47c566 | 4136 | |
mkilivan | 5:c8918e47c566 | 4137 | #endif /* !_FS_READONLY */ |
mkilivan | 5:c8918e47c566 | 4138 | #endif /* _USE_STRFUNC */ |