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