annotate ff.c @ 337:f575ef538f5d

- Various fixes for web server, kind of works
author Matt Johnston <matt@ucc.asn.au>
date Wed, 13 Jun 2012 23:41:05 +0800
parents 49e83333e546
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
309
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 /*----------------------------------------------------------------------------/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 / FatFs - FAT file system module R0.09 (C)ChaN, 2011
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 /-----------------------------------------------------------------------------/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 / FatFs module is a generic FAT file system module for small embedded systems.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 / This is a free software that opened for education, research and commercial
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 / developments under license policy of following terms.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 /
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 / Copyright (C) 2011, ChaN, all right reserved.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 /
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 / * The FatFs module is a free software and there is NO WARRANTY.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 / * No restriction on use. You can use, modify and redistribute it for
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 / personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 / * Redistributions of source code must retain the above copyright notice.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 /
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 /-----------------------------------------------------------------------------/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 / Feb 26,'06 R0.00 Prototype.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 /
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 / Apr 29,'06 R0.01 First stable version.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 /
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 / Jun 01,'06 R0.02 Added FAT12 support.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 / Removed unbuffered mode.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 / Fixed a problem on small (<32M) partition.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 / Jun 10,'06 R0.02a Added a configuration option (_FS_MINIMUM).
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 /
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 / Sep 22,'06 R0.03 Added f_rename().
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 / Changed option _FS_MINIMUM to _FS_MINIMIZE.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 / Dec 11,'06 R0.03a Improved cluster scan algorithm to write files fast.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 / Fixed f_mkdir() creates incorrect directory on FAT32.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 /
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 / Feb 04,'07 R0.04 Supported multiple drive system.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 / Changed some interfaces for multiple drive system.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 / Changed f_mountdrv() to f_mount().
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 / Added f_mkfs().
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 / Apr 01,'07 R0.04a Supported multiple partitions on a physical drive.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 / Added a capability of extending file size to f_lseek().
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 / Added minimization level 3.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 / Fixed an endian sensitive code in f_mkfs().
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 / May 05,'07 R0.04b Added a configuration option _USE_NTFLAG.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 / Added FSInfo support.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 / Fixed DBCS name can result FR_INVALID_NAME.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 / Fixed short seek (<= csize) collapses the file object.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 /
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 / Aug 25,'07 R0.05 Changed arguments of f_read(), f_write() and f_mkfs().
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 / Fixed f_mkfs() on FAT32 creates incorrect FSInfo.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 / Fixed f_mkdir() on FAT32 creates incorrect directory.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 / Feb 03,'08 R0.05a Added f_truncate() and f_utime().
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 / Fixed off by one error at FAT sub-type determination.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 / Fixed btr in f_read() can be mistruncated.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 / Fixed cached sector is not flushed when create and close without write.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 /
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 / Apr 01,'08 R0.06 Added fputc(), fputs(), fprintf() and fgets().
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 / Improved performance of f_lseek() on moving to the same or following cluster.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 /
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 / Apr 01,'09 R0.07 Merged Tiny-FatFs as a configuration option. (_FS_TINY)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 / Added long file name feature.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 / Added multiple code page feature.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 / Added re-entrancy for multitask operation.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 / Added auto cluster size selection to f_mkfs().
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 / Added rewind option to f_readdir().
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 / Changed result code of critical errors.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 / Renamed string functions to avoid name collision.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 / Apr 14,'09 R0.07a Separated out OS dependent code on reentrant cfg.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 / Added multiple sector size feature.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 / Jun 21,'09 R0.07c Fixed f_unlink() can return FR_OK on error.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 / Fixed wrong cache control in f_lseek().
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 / Added relative path feature.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 / Added f_chdir() and f_chdrive().
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 / Added proper case conversion to extended char.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 / Nov 03,'09 R0.07e Separated out configuration options from ff.h to ffconf.h.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 / Fixed f_unlink() fails to remove a sub-dir on _FS_RPATH.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 / Fixed name matching error on the 13 char boundary.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 / Added a configuration option, _LFN_UNICODE.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 / Changed f_readdir() to return the SFN with always upper case on non-LFN cfg.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 /
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 / May 15,'10 R0.08 Added a memory configuration option. (_USE_LFN = 3)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 / Added file lock feature. (_FS_SHARE)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 / Added fast seek feature. (_USE_FASTSEEK)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78 / Changed some types on the API, XCHAR->TCHAR.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 / Changed fname member in the FILINFO structure on Unicode cfg.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 / String functions support UTF-8 encoding files on Unicode cfg.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 / Aug 16,'10 R0.08a Added f_getcwd(). (_FS_RPATH = 2)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 / Added sector erase feature. (_USE_ERASE)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 / Moved file lock semaphore table from fs object to the bss.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 / Fixed a wrong directory entry is created on non-LFN cfg when the given name contains ';'.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 / Fixed f_mkfs() creates wrong FAT32 volume.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86 / Jan 15,'11 R0.08b Fast seek feature is also applied to f_read() and f_write().
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 / f_lseek() reports required table size on creating CLMP.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88 / Extended format syntax of f_printf function.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89 / Ignores duplicated directory separators in given path names.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90 /
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 / Sep 06,'11 R0.09 f_mkfs() supports multiple partition to finish the multiple partition feature.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 / Added f_fdisk(). (_MULTI_PARTITION = 2)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 /---------------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 #include "ff.h" /* FatFs configurations and declarations */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 #include "diskio.h" /* Declarations of low level disk I/O functions */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99 /*--------------------------------------------------------------------------
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101 Module Private Definitions
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103 ---------------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
104
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105 #if _FATFS != 6502 /* Revision ID */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
106 #error Wrong include file (ff.h).
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
109
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110 /* Definitions on sector size */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
111 #if _MAX_SS != 512 && _MAX_SS != 1024 && _MAX_SS != 2048 && _MAX_SS != 4096
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112 #error Wrong sector size.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
113 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
114 #if _MAX_SS != 512
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
115 #define SS(fs) ((fs)->ssize) /* Variable sector size */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
116 #else
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
117 #define SS(fs) 512U /* Fixed sector size */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
118 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
119
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
120
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
121 /* Reentrancy related */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
122 #if _FS_REENTRANT
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
123 #if _USE_LFN == 1
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
124 #error Static LFN work area must not be used in re-entrant configuration.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
125 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
126 #define ENTER_FF(fs) { if (!lock_fs(fs)) return FR_TIMEOUT; }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
127 #define LEAVE_FF(fs, res) { unlock_fs(fs, res); return res; }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
128 #else
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
129 #define ENTER_FF(fs)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
130 #define LEAVE_FF(fs, res) return res
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
131 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
132
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
133 #define ABORT(fs, res) { fp->flag |= FA__ERROR; LEAVE_FF(fs, res); }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
134
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
135
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
136 /* File shareing feature */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
137 #if _FS_SHARE
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
138 #if _FS_READONLY
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
139 #error _FS_SHARE must be 0 on read-only cfg.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
140 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
141 typedef struct {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
142 FATFS *fs; /* File ID 1, volume (NULL:blank entry) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
143 DWORD clu; /* File ID 2, directory */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
144 WORD idx; /* File ID 3, directory index */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
145 WORD ctr; /* File open counter, 0:none, 0x01..0xFF:read open count, 0x100:write mode */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
146 } FILESEM;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
147 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
148
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
149
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
150 /* Misc definitions */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
151 #define LD_CLUST(dir) (((DWORD)LD_WORD(dir+DIR_FstClusHI)<<16) | LD_WORD(dir+DIR_FstClusLO))
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
152 #define ST_CLUST(dir,cl) {ST_WORD(dir+DIR_FstClusLO, cl); ST_WORD(dir+DIR_FstClusHI, (DWORD)cl>>16);}
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
153
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
154
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
155 /* DBCS code ranges and SBCS extend char conversion table */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
156
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
157 #if _CODE_PAGE == 932 /* Japanese Shift-JIS */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
158 #define _DF1S 0x81 /* DBC 1st byte range 1 start */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
159 #define _DF1E 0x9F /* DBC 1st byte range 1 end */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
160 #define _DF2S 0xE0 /* DBC 1st byte range 2 start */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
161 #define _DF2E 0xFC /* DBC 1st byte range 2 end */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
162 #define _DS1S 0x40 /* DBC 2nd byte range 1 start */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
163 #define _DS1E 0x7E /* DBC 2nd byte range 1 end */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
164 #define _DS2S 0x80 /* DBC 2nd byte range 2 start */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
165 #define _DS2E 0xFC /* DBC 2nd byte range 2 end */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
166
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
167 #elif _CODE_PAGE == 936 /* Simplified Chinese GBK */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
168 #define _DF1S 0x81
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
169 #define _DF1E 0xFE
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
170 #define _DS1S 0x40
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
171 #define _DS1E 0x7E
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
172 #define _DS2S 0x80
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
173 #define _DS2E 0xFE
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
174
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
175 #elif _CODE_PAGE == 949 /* Korean */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
176 #define _DF1S 0x81
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
177 #define _DF1E 0xFE
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
178 #define _DS1S 0x41
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
179 #define _DS1E 0x5A
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
180 #define _DS2S 0x61
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
181 #define _DS2E 0x7A
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
182 #define _DS3S 0x81
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
183 #define _DS3E 0xFE
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
184
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
185 #elif _CODE_PAGE == 950 /* Traditional Chinese Big5 */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
186 #define _DF1S 0x81
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
187 #define _DF1E 0xFE
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
188 #define _DS1S 0x40
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
189 #define _DS1E 0x7E
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
190 #define _DS2S 0xA1
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
191 #define _DS2E 0xFE
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
192
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
193 #elif _CODE_PAGE == 437 /* U.S. (OEM) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
194 #define _DF1S 0
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
195 #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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
196 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
197 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
198 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}
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
199
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
200 #elif _CODE_PAGE == 720 /* Arabic (OEM) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
201 #define _DF1S 0
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
202 #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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
203 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
204 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
205 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}
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
206
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
207 #elif _CODE_PAGE == 737 /* Greek (OEM) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
208 #define _DF1S 0
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
209 #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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
210 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
211 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
212 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}
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
213
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
214 #elif _CODE_PAGE == 775 /* Baltic (OEM) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
215 #define _DF1S 0
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
216 #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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
217 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
218 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
219 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}
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
220
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
221 #elif _CODE_PAGE == 850 /* Multilingual Latin 1 (OEM) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
222 #define _DF1S 0
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
223 #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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
224 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
225 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
226 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}
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
227
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
228 #elif _CODE_PAGE == 852 /* Latin 2 (OEM) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
229 #define _DF1S 0
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
230 #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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
231 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
232 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
233 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}
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
234
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
235 #elif _CODE_PAGE == 855 /* Cyrillic (OEM) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
236 #define _DF1S 0
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
237 #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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
238 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
239 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
240 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}
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
241
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
242 #elif _CODE_PAGE == 857 /* Turkish (OEM) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
243 #define _DF1S 0
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
244 #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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
245 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
246 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
247 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}
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
248
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
249 #elif _CODE_PAGE == 858 /* Multilingual Latin 1 + Euro (OEM) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
250 #define _DF1S 0
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
251 #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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
252 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
253 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
254 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}
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
255
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
256 #elif _CODE_PAGE == 862 /* Hebrew (OEM) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
257 #define _DF1S 0
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
258 #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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
259 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
260 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
261 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}
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
262
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
263 #elif _CODE_PAGE == 866 /* Russian (OEM) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
264 #define _DF1S 0
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
265 #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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
266 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
267 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
268 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}
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
269
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
270 #elif _CODE_PAGE == 874 /* Thai (OEM, Windows) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
271 #define _DF1S 0
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
272 #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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
273 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
274 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
275 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}
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
276
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
277 #elif _CODE_PAGE == 1250 /* Central Europe (Windows) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
278 #define _DF1S 0
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
279 #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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
280 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
281 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
282 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}
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
283
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
284 #elif _CODE_PAGE == 1251 /* Cyrillic (Windows) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
285 #define _DF1S 0
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
286 #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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
287 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
289 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}
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
290
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
291 #elif _CODE_PAGE == 1252 /* Latin 1 (Windows) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
292 #define _DF1S 0
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
293 #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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
294 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
295 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
296 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}
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
297
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
298 #elif _CODE_PAGE == 1253 /* Greek (Windows) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
299 #define _DF1S 0
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
300 #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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
301 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
302 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
303 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}
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
304
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
305 #elif _CODE_PAGE == 1254 /* Turkish (Windows) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
306 #define _DF1S 0
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
307 #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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
308 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
309 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
310 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}
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
311
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
312 #elif _CODE_PAGE == 1255 /* Hebrew (Windows) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
313 #define _DF1S 0
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
314 #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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
315 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
316 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
317 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}
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
318
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
319 #elif _CODE_PAGE == 1256 /* Arabic (Windows) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
320 #define _DF1S 0
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
321 #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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
322 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
323 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
324 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}
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
325
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
326 #elif _CODE_PAGE == 1257 /* Baltic (Windows) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
327 #define _DF1S 0
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
328 #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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
329 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
330 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
331 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}
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
332
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
333 #elif _CODE_PAGE == 1258 /* Vietnam (OEM, Windows) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
334 #define _DF1S 0
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
335 #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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
336 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
337 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, \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
338 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}
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
339
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
340 #elif _CODE_PAGE == 1 /* ASCII (for only non-LFN cfg) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
341 #if _USE_LFN
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
342 #error Cannot use LFN feature without valid code page.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
343 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
344 #define _DF1S 0
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
345
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
346 #else
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
347 #error Unknown code page
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
348
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
349 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
350
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
351
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
352 /* Character code support macros */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
353 #define IsUpper(c) (((c)>='A')&&((c)<='Z'))
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
354 #define IsLower(c) (((c)>='a')&&((c)<='z'))
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
355 #define IsDigit(c) (((c)>='0')&&((c)<='9'))
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
356
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
357 #if _DF1S /* Code page is DBCS */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
358
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
359 #ifdef _DF2S /* Two 1st byte areas */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
360 #define IsDBCS1(c) (((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E) || ((BYTE)(c) >= _DF2S && (BYTE)(c) <= _DF2E))
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
361 #else /* One 1st byte area */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
362 #define IsDBCS1(c) ((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
363 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
364
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
365 #ifdef _DS3S /* Three 2nd byte areas */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
366 #define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E) || ((BYTE)(c) >= _DS3S && (BYTE)(c) <= _DS3E))
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
367 #else /* Two 2nd byte areas */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
368 #define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E))
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
369 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
370
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
371 #else /* Code page is SBCS */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
372
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
373 #define IsDBCS1(c) 0
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
374 #define IsDBCS2(c) 0
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
375
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
376 #endif /* _DF1S */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
377
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
378
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
379 /* Name status flags */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
380 #define NS 11 /* Index of name status byte in fn[] */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
381 #define NS_LOSS 0x01 /* Out of 8.3 format */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
382 #define NS_LFN 0x02 /* Force to create LFN entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
383 #define NS_LAST 0x04 /* Last segment */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
384 #define NS_BODY 0x08 /* Lower case flag (body) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
385 #define NS_EXT 0x10 /* Lower case flag (ext) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
386 #define NS_DOT 0x20 /* Dot entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
387
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
388
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
389 /* FAT sub-type boundaries */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
390 /* Note that the FAT spec by Microsoft says 4085 but Windows works with 4087! */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
391 #define MIN_FAT16 4086 /* Minimum number of clusters for FAT16 */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
392 #define MIN_FAT32 65526 /* Minimum number of clusters for FAT32 */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
393
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
394
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
395 /* FatFs refers the members in the FAT structures as byte array instead of
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
396 / structure member because the structure is not binary compatible between
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
397 / different platforms */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
398
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
399 #define BS_jmpBoot 0 /* Jump instruction (3) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
400 #define BS_OEMName 3 /* OEM name (8) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
401 #define BPB_BytsPerSec 11 /* Sector size [byte] (2) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
402 #define BPB_SecPerClus 13 /* Cluster size [sector] (1) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
403 #define BPB_RsvdSecCnt 14 /* Size of reserved area [sector] (2) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
404 #define BPB_NumFATs 16 /* Number of FAT copies (1) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
405 #define BPB_RootEntCnt 17 /* Number of root dir entries for FAT12/16 (2) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
406 #define BPB_TotSec16 19 /* Volume size [sector] (2) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
407 #define BPB_Media 21 /* Media descriptor (1) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
408 #define BPB_FATSz16 22 /* FAT size [sector] (2) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
409 #define BPB_SecPerTrk 24 /* Track size [sector] (2) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
410 #define BPB_NumHeads 26 /* Number of heads (2) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
411 #define BPB_HiddSec 28 /* Number of special hidden sectors (4) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
412 #define BPB_TotSec32 32 /* Volume size [sector] (4) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
413 #define BS_DrvNum 36 /* Physical drive number (2) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
414 #define BS_BootSig 38 /* Extended boot signature (1) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
415 #define BS_VolID 39 /* Volume serial number (4) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
416 #define BS_VolLab 43 /* Volume label (8) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
417 #define BS_FilSysType 54 /* File system type (1) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
418 #define BPB_FATSz32 36 /* FAT size [sector] (4) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
419 #define BPB_ExtFlags 40 /* Extended flags (2) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
420 #define BPB_FSVer 42 /* File system version (2) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
421 #define BPB_RootClus 44 /* Root dir first cluster (4) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
422 #define BPB_FSInfo 48 /* Offset of FSInfo sector (2) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
423 #define BPB_BkBootSec 50 /* Offset of backup boot sectot (2) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
424 #define BS_DrvNum32 64 /* Physical drive number (2) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
425 #define BS_BootSig32 66 /* Extended boot signature (1) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
426 #define BS_VolID32 67 /* Volume serial number (4) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
427 #define BS_VolLab32 71 /* Volume label (8) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
428 #define BS_FilSysType32 82 /* File system type (1) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
429 #define FSI_LeadSig 0 /* FSI: Leading signature (4) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
430 #define FSI_StrucSig 484 /* FSI: Structure signature (4) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
431 #define FSI_Free_Count 488 /* FSI: Number of free clusters (4) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
432 #define FSI_Nxt_Free 492 /* FSI: Last allocated cluster (4) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
433 #define MBR_Table 446 /* MBR: Partition table offset (2) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
434 #define SZ_PTE 16 /* MBR: Size of a partition table entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
435 #define BS_55AA 510 /* Boot sector signature (2) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
436
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
437 #define DIR_Name 0 /* Short file name (11) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
438 #define DIR_Attr 11 /* Attribute (1) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
439 #define DIR_NTres 12 /* NT flag (1) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
440 #define DIR_CrtTime 14 /* Created time (2) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
441 #define DIR_CrtDate 16 /* Created date (2) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
442 #define DIR_FstClusHI 20 /* Higher 16-bit of first cluster (2) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
443 #define DIR_WrtTime 22 /* Modified time (2) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
444 #define DIR_WrtDate 24 /* Modified date (2) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
445 #define DIR_FstClusLO 26 /* Lower 16-bit of first cluster (2) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
446 #define DIR_FileSize 28 /* File size (4) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
447 #define LDIR_Ord 0 /* LFN entry order and LLE flag (1) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
448 #define LDIR_Attr 11 /* LFN attribute (1) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
449 #define LDIR_Type 12 /* LFN type (1) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
450 #define LDIR_Chksum 13 /* Sum of corresponding SFN entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
451 #define LDIR_FstClusLO 26 /* Filled by zero (0) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
452 #define SZ_DIR 32 /* Size of a directory entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
453 #define LLE 0x40 /* Last long entry flag in LDIR_Ord */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
454 #define DDE 0xE5 /* Deleted directory enrty mark in DIR_Name[0] */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
455 #define NDDE 0x05 /* Replacement of a character collides with DDE */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
456
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
457
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
458 /*------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
459 /* Module private work area */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
460 /*------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
461 /* Note that uninitialized variables with static duration are
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
462 / zeroed/nulled at start-up. If not, the compiler or start-up
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
463 / routine is out of ANSI-C standard.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
464 */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
465
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
466 #if _VOLUMES
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
467 static
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
468 FATFS *FatFs[_VOLUMES]; /* Pointer to the file system objects (logical drives) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
469 #else
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
470 #error Number of volumes must not be 0.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
471 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
472
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
473 static
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
474 WORD Fsid; /* File system mount ID */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
475
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
476 #if _FS_RPATH
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
477 static
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
478 BYTE CurrVol; /* Current drive */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
479 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
480
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
481 #if _FS_SHARE
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
482 static
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
483 FILESEM Files[_FS_SHARE]; /* File lock semaphores */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
484 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
485
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
486 #if _USE_LFN == 0 /* No LFN feature */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
487 #define DEF_NAMEBUF BYTE sfn[12]
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
488 #define INIT_BUF(dobj) (dobj).fn = sfn
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
489 #define FREE_BUF()
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
490
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
491 #elif _USE_LFN == 1 /* LFN feature with static working buffer */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
492 static WCHAR LfnBuf[_MAX_LFN+1];
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
493 #define DEF_NAMEBUF BYTE sfn[12]
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
494 #define INIT_BUF(dobj) { (dobj).fn = sfn; (dobj).lfn = LfnBuf; }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
495 #define FREE_BUF()
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
496
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
497 #elif _USE_LFN == 2 /* LFN feature with dynamic working buffer on the stack */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
498 #define DEF_NAMEBUF BYTE sfn[12]; WCHAR lbuf[_MAX_LFN+1]
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
499 #define INIT_BUF(dobj) { (dobj).fn = sfn; (dobj).lfn = lbuf; }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
500 #define FREE_BUF()
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
501
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
502 #elif _USE_LFN == 3 /* LFN feature with dynamic working buffer on the heap */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
503 #define DEF_NAMEBUF BYTE sfn[12]; WCHAR *lfn
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
504 #define INIT_BUF(dobj) { lfn = ff_memalloc((_MAX_LFN + 1) * 2); \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
505 if (!lfn) LEAVE_FF((dobj).fs, FR_NOT_ENOUGH_CORE); \
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
506 (dobj).lfn = lfn; (dobj).fn = sfn; }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
507 #define FREE_BUF() ff_memfree(lfn)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
508
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
509 #else
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
510 #error Wrong LFN configuration.
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
511 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
512
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
513
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
514
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
515
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
516 /*--------------------------------------------------------------------------
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
517
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
518 Module Private Functions
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
519
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
520 ---------------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
521
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
522
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
523 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
524 /* String functions */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
525 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
526
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
527 /* Copy memory to memory */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
528 static
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
529 void mem_cpy (void* dst, const void* src, UINT cnt) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
530 BYTE *d = (BYTE*)dst;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
531 const BYTE *s = (const BYTE*)src;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
532
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
533 #if _WORD_ACCESS == 1
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
534 while (cnt >= sizeof(int)) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
535 *(int*)d = *(int*)s;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
536 d += sizeof(int); s += sizeof(int);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
537 cnt -= sizeof(int);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
538 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
539 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
540 while (cnt--)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
541 *d++ = *s++;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
542 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
543
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
544 /* Fill memory */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
545 static
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
546 void mem_set (void* dst, int val, UINT cnt) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
547 BYTE *d = (BYTE*)dst;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
548
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
549 while (cnt--)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
550 *d++ = (BYTE)val;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
551 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
552
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
553 /* Compare memory to memory */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
554 static
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
555 int mem_cmp (const void* dst, const void* src, UINT cnt) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
556 const BYTE *d = (const BYTE *)dst, *s = (const BYTE *)src;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
557 int r = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
558
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
559 while (cnt-- && (r = *d++ - *s++) == 0) ;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
560 return r;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
561 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
562
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
563 /* Check if chr is contained in the string */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
564 static
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
565 int chk_chr (const char* str, int chr) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
566 while (*str && *str != chr) str++;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
567 return *str;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
568 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
569
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
570
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
571
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
572 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
573 /* Request/Release grant to access the volume */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
574 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
575 #if _FS_REENTRANT
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
576
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
577 static
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
578 int lock_fs (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
579 FATFS *fs /* File system object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
580 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
581 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
582 return ff_req_grant(fs->sobj);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
583 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
584
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
585
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
586 static
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
587 void unlock_fs (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
588 FATFS *fs, /* File system object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
589 FRESULT res /* Result code to be returned */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
590 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
591 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
592 if (res != FR_NOT_ENABLED &&
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
593 res != FR_INVALID_DRIVE &&
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
594 res != FR_INVALID_OBJECT &&
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
595 res != FR_TIMEOUT) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
596 ff_rel_grant(fs->sobj);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
597 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
598 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
599 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
600
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
601
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
602
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
603 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
604 /* File shareing control functions */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
605 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
606 #if _FS_SHARE
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
607
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
608 static
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
609 FRESULT chk_lock ( /* Check if the file can be accessed */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
610 DIR* dj, /* Directory object pointing the file to be checked */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
611 int acc /* Desired access (0:Read, 1:Write, 2:Delete/Rename) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
612 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
613 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
614 UINT i, be;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
615
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
616 /* Search file semaphore table */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
617 for (i = be = 0; i < _FS_SHARE; i++) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
618 if (Files[i].fs) { /* Existing entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
619 if (Files[i].fs == dj->fs && /* Check if the file matched with an open file */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
620 Files[i].clu == dj->sclust &&
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
621 Files[i].idx == dj->index) break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
622 } else { /* Blank entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
623 be++;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
624 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
625 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
626 if (i == _FS_SHARE) /* The file is not opened */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
627 return (be || acc == 2) ? FR_OK : FR_TOO_MANY_OPEN_FILES; /* Is there a blank entry for new file? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
628
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
629 /* The file has been opened. Reject any open against writing file and all write mode open */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
630 return (acc || Files[i].ctr == 0x100) ? FR_LOCKED : FR_OK;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
631 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
632
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
633
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
634 static
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
635 int enq_lock (void) /* Check if an entry is available for a new file */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
636 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
637 UINT i;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
638
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
639 for (i = 0; i < _FS_SHARE && Files[i].fs; i++) ;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
640 return (i == _FS_SHARE) ? 0 : 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
641 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
642
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
643
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
644 static
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
645 UINT inc_lock ( /* Increment file open counter and returns its index (0:int error) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
646 DIR* dj, /* Directory object pointing the file to register or increment */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
647 int acc /* Desired access mode (0:Read, !0:Write) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
648 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
649 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
650 UINT i;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
651
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
652
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
653 for (i = 0; i < _FS_SHARE; i++) { /* Find the file */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
654 if (Files[i].fs == dj->fs &&
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
655 Files[i].clu == dj->sclust &&
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
656 Files[i].idx == dj->index) break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
657 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
658
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
659 if (i == _FS_SHARE) { /* Not opened. Register it as new. */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
660 for (i = 0; i < _FS_SHARE && Files[i].fs; i++) ;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
661 if (i == _FS_SHARE) return 0; /* No space to register (int err) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
662 Files[i].fs = dj->fs;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
663 Files[i].clu = dj->sclust;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
664 Files[i].idx = dj->index;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
665 Files[i].ctr = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
666 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
667
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
668 if (acc && Files[i].ctr) return 0; /* Access violation (int err) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
669
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
670 Files[i].ctr = acc ? 0x100 : Files[i].ctr + 1; /* Set semaphore value */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
671
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
672 return i + 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
673 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
674
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
675
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
676 static
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
677 FRESULT dec_lock ( /* Decrement file open counter */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
678 UINT i /* Semaphore index */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
679 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
680 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
681 WORD n;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
682 FRESULT res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
683
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
684
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
685 if (--i < _FS_SHARE) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
686 n = Files[i].ctr;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
687 if (n == 0x100) n = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
688 if (n) n--;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
689 Files[i].ctr = n;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
690 if (!n) Files[i].fs = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
691 res = FR_OK;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
692 } else {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
693 res = FR_INT_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
694 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
695 return res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
696 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
697
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
698
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
699 static
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
700 void clear_lock ( /* Clear lock entries of the volume */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
701 FATFS *fs
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
702 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
703 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
704 UINT i;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
705
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
706 for (i = 0; i < _FS_SHARE; i++) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
707 if (Files[i].fs == fs) Files[i].fs = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
708 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
709 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
710 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
711
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
712
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
713
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
714 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
715 /* Change window offset */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
716 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
717
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
718 static
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
719 FRESULT move_window (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
720 FATFS *fs, /* File system object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
721 DWORD sector /* Sector number to make appearance in the fs->win[] */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
722 ) /* Move to zero only writes back dirty window */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
723 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
724 DWORD wsect;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
725
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
726
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
727 wsect = fs->winsect;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
728 if (wsect != sector) { /* Changed current window */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
729 #if !_FS_READONLY
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
730 if (fs->wflag) { /* Write back dirty window if needed */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
731 if (disk_write(fs->drv, fs->win, wsect, 1) != RES_OK)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
732 return FR_DISK_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
733 fs->wflag = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
734 if (wsect < (fs->fatbase + fs->fsize)) { /* In FAT area */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
735 BYTE nf;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
736 for (nf = fs->n_fats; nf > 1; nf--) { /* Reflect the change to all FAT copies */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
737 wsect += fs->fsize;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
738 disk_write(fs->drv, fs->win, wsect, 1);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
739 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
740 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
741 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
742 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
743 if (sector) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
744 if (disk_read(fs->drv, fs->win, sector, 1) != RES_OK)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
745 return FR_DISK_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
746 fs->winsect = sector;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
747 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
748 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
749
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
750 return FR_OK;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
751 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
752
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
753
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
754
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
755
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
756 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
757 /* Clean-up cached data */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
758 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
759 #if !_FS_READONLY
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
760 static
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
761 FRESULT sync ( /* FR_OK: successful, FR_DISK_ERR: failed */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
762 FATFS *fs /* File system object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
763 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
764 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
765 FRESULT res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
766
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
767
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
768 res = move_window(fs, 0);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
769 if (res == FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
770 /* Update FSInfo sector if needed */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
771 if (fs->fs_type == FS_FAT32 && fs->fsi_flag) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
772 fs->winsect = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
773 /* Create FSInfo structure */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
774 mem_set(fs->win, 0, 512);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
775 ST_WORD(fs->win+BS_55AA, 0xAA55);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
776 ST_DWORD(fs->win+FSI_LeadSig, 0x41615252);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
777 ST_DWORD(fs->win+FSI_StrucSig, 0x61417272);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
778 ST_DWORD(fs->win+FSI_Free_Count, fs->free_clust);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
779 ST_DWORD(fs->win+FSI_Nxt_Free, fs->last_clust);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
780 /* Write it into the FSInfo sector */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
781 disk_write(fs->drv, fs->win, fs->fsi_sector, 1);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
782 fs->fsi_flag = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
783 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
784 /* Make sure that no pending write process in the physical drive */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
785 if (disk_ioctl(fs->drv, CTRL_SYNC, 0) != RES_OK)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
786 res = FR_DISK_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
787 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
788
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
789 return res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
790 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
791 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
792
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
793
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
794
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
795
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
796 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
797 /* Get sector# from cluster# */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
798 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
799
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
800
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
801 DWORD clust2sect ( /* !=0: Sector number, 0: Failed - invalid cluster# */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
802 FATFS *fs, /* File system object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
803 DWORD clst /* Cluster# to be converted */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
804 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
805 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
806 clst -= 2;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
807 if (clst >= (fs->n_fatent - 2)) return 0; /* Invalid cluster# */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
808 return clst * fs->csize + fs->database;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
809 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
810
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
811
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
812
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
813
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
814 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
815 /* FAT access - Read value of a FAT entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
816 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
817
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
818
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
819 DWORD get_fat ( /* 0xFFFFFFFF:Disk error, 1:Internal error, Else:Cluster status */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
820 FATFS *fs, /* File system object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
821 DWORD clst /* Cluster# to get the link information */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
822 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
823 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
824 UINT wc, bc;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
825 BYTE *p;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
826
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
827
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
828 if (clst < 2 || clst >= fs->n_fatent) /* Chack range */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
829 return 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
830
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
831 switch (fs->fs_type) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
832 case FS_FAT12 :
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
833 bc = (UINT)clst; bc += bc / 2;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
834 if (move_window(fs, fs->fatbase + (bc / SS(fs)))) break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
835 wc = fs->win[bc % SS(fs)]; bc++;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
836 if (move_window(fs, fs->fatbase + (bc / SS(fs)))) break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
837 wc |= fs->win[bc % SS(fs)] << 8;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
838 return (clst & 1) ? (wc >> 4) : (wc & 0xFFF);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
839
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
840 case FS_FAT16 :
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
841 if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 2)))) break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
842 p = &fs->win[clst * 2 % SS(fs)];
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
843 return LD_WORD(p);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
844
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
845 case FS_FAT32 :
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
846 if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 4)))) break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
847 p = &fs->win[clst * 4 % SS(fs)];
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
848 return LD_DWORD(p) & 0x0FFFFFFF;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
849 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
850
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
851 return 0xFFFFFFFF; /* An error occurred at the disk I/O layer */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
852 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
853
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
854
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
855
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
856
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
857 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
858 /* FAT access - Change value of a FAT entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
859 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
860 #if !_FS_READONLY
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
861
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
862 FRESULT put_fat (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
863 FATFS *fs, /* File system object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
864 DWORD clst, /* Cluster# to be changed in range of 2 to fs->n_fatent - 1 */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
865 DWORD val /* New value to mark the cluster */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
866 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
867 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
868 UINT bc;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
869 BYTE *p;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
870 FRESULT res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
871
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
872
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
873 if (clst < 2 || clst >= fs->n_fatent) { /* Check range */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
874 res = FR_INT_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
875
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
876 } else {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
877 switch (fs->fs_type) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
878 case FS_FAT12 :
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
879 bc = clst; bc += bc / 2;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
880 res = move_window(fs, fs->fatbase + (bc / SS(fs)));
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
881 if (res != FR_OK) break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
882 p = &fs->win[bc % SS(fs)];
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
883 *p = (clst & 1) ? ((*p & 0x0F) | ((BYTE)val << 4)) : (BYTE)val;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
884 bc++;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
885 fs->wflag = 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
886 res = move_window(fs, fs->fatbase + (bc / SS(fs)));
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
887 if (res != FR_OK) break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
888 p = &fs->win[bc % SS(fs)];
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
889 *p = (clst & 1) ? (BYTE)(val >> 4) : ((*p & 0xF0) | ((BYTE)(val >> 8) & 0x0F));
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
890 break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
891
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
892 case FS_FAT16 :
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
893 res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 2)));
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
894 if (res != FR_OK) break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
895 p = &fs->win[clst * 2 % SS(fs)];
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
896 ST_WORD(p, (WORD)val);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
897 break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
898
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
899 case FS_FAT32 :
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
900 res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 4)));
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
901 if (res != FR_OK) break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
902 p = &fs->win[clst * 4 % SS(fs)];
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
903 val |= LD_DWORD(p) & 0xF0000000;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
904 ST_DWORD(p, val);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
905 break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
906
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
907 default :
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
908 res = FR_INT_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
909 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
910 fs->wflag = 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
911 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
912
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
913 return res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
914 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
915 #endif /* !_FS_READONLY */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
916
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
917
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
918
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
919
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
920 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
921 /* FAT handling - Remove a cluster chain */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
922 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
923 #if !_FS_READONLY
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
924 static
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
925 FRESULT remove_chain (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
926 FATFS *fs, /* File system object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
927 DWORD clst /* Cluster# to remove a chain from */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
928 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
929 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
930 FRESULT res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
931 DWORD nxt;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
932 #if _USE_ERASE
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
933 DWORD scl = clst, ecl = clst, resion[2];
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
934 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
935
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
936 if (clst < 2 || clst >= fs->n_fatent) { /* Check range */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
937 res = FR_INT_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
938
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
939 } else {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
940 res = FR_OK;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
941 while (clst < fs->n_fatent) { /* Not a last link? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
942 nxt = get_fat(fs, clst); /* Get cluster status */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
943 if (nxt == 0) break; /* Empty cluster? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
944 if (nxt == 1) { res = FR_INT_ERR; break; } /* Internal error? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
945 if (nxt == 0xFFFFFFFF) { res = FR_DISK_ERR; break; } /* Disk error? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
946 res = put_fat(fs, clst, 0); /* Mark the cluster "empty" */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
947 if (res != FR_OK) break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
948 if (fs->free_clust != 0xFFFFFFFF) { /* Update FSInfo */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
949 fs->free_clust++;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
950 fs->fsi_flag = 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
951 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
952 #if _USE_ERASE
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
953 if (ecl + 1 == nxt) { /* Next cluster is contiguous */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
954 ecl = nxt;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
955 } else { /* End of contiguous clusters */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
956 resion[0] = clust2sect(fs, scl); /* Start sector */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
957 resion[1] = clust2sect(fs, ecl) + fs->csize - 1; /* End sector */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
958 disk_ioctl(fs->drv, CTRL_ERASE_SECTOR, resion); /* Erase the block */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
959 scl = ecl = nxt;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
960 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
961 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
962 clst = nxt; /* Next cluster */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
963 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
964 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
965
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
966 return res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
967 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
968 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
969
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
970
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
971
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
972
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
973 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
974 /* FAT handling - Stretch or Create a cluster chain */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
975 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
976 #if !_FS_READONLY
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
977 static
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
978 DWORD create_chain ( /* 0:No free cluster, 1:Internal error, 0xFFFFFFFF:Disk error, >=2:New cluster# */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
979 FATFS *fs, /* File system object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
980 DWORD clst /* Cluster# to stretch. 0 means create a new chain. */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
981 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
982 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
983 DWORD cs, ncl, scl;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
984 FRESULT res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
985
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
986
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
987 if (clst == 0) { /* Create a new chain */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
988 scl = fs->last_clust; /* Get suggested start point */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
989 if (!scl || scl >= fs->n_fatent) scl = 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
990 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
991 else { /* Stretch the current chain */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
992 cs = get_fat(fs, clst); /* Check the cluster status */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
993 if (cs < 2) return 1; /* It is an invalid cluster */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
994 if (cs < fs->n_fatent) return cs; /* It is already followed by next cluster */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
995 scl = clst;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
996 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
997
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
998 ncl = scl; /* Start cluster */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
999 for (;;) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1000 ncl++; /* Next cluster */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1001 if (ncl >= fs->n_fatent) { /* Wrap around */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1002 ncl = 2;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1003 if (ncl > scl) return 0; /* No free cluster */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1004 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1005 cs = get_fat(fs, ncl); /* Get the cluster status */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1006 if (cs == 0) break; /* Found a free cluster */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1007 if (cs == 0xFFFFFFFF || cs == 1)/* An error occurred */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1008 return cs;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1009 if (ncl == scl) return 0; /* No free cluster */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1010 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1011
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1012 res = put_fat(fs, ncl, 0x0FFFFFFF); /* Mark the new cluster "last link" */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1013 if (res == FR_OK && clst != 0) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1014 res = put_fat(fs, clst, ncl); /* Link it to the previous one if needed */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1015 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1016 if (res == FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1017 fs->last_clust = ncl; /* Update FSINFO */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1018 if (fs->free_clust != 0xFFFFFFFF) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1019 fs->free_clust--;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1020 fs->fsi_flag = 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1021 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1022 } else {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1023 ncl = (res == FR_DISK_ERR) ? 0xFFFFFFFF : 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1024 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1025
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1026 return ncl; /* Return new cluster number or error code */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1027 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1028 #endif /* !_FS_READONLY */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1029
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1030
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1031
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1032 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1033 /* FAT handling - Convert offset into cluster with link map table */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1034 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1035
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1036 #if _USE_FASTSEEK
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1037 static
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1038 DWORD clmt_clust ( /* <2:Error, >=2:Cluster number */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1039 FIL* fp, /* Pointer to the file object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1040 DWORD ofs /* File offset to be converted to cluster# */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1041 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1042 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1043 DWORD cl, ncl, *tbl;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1044
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1045
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1046 tbl = fp->cltbl + 1; /* Top of CLMT */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1047 cl = ofs / SS(fp->fs) / fp->fs->csize; /* Cluster order from top of the file */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1048 for (;;) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1049 ncl = *tbl++; /* Number of cluters in the fragment */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1050 if (!ncl) return 0; /* End of table? (error) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1051 if (cl < ncl) break; /* In this fragment? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1052 cl -= ncl; tbl++; /* Next fragment */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1053 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1054 return cl + *tbl; /* Return the cluster number */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1055 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1056 #endif /* _USE_FASTSEEK */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1057
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1058
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1059
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1060 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1061 /* Directory handling - Set directory index */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1062 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1063
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1064 static
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1065 FRESULT dir_sdi (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1066 DIR *dj, /* Pointer to directory object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1067 WORD idx /* Directory index number */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1068 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1069 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1070 DWORD clst;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1071 WORD ic;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1072
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1073
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1074 dj->index = idx;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1075 clst = dj->sclust;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1076 if (clst == 1 || clst >= dj->fs->n_fatent) /* Check start cluster range */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1077 return FR_INT_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1078 if (!clst && dj->fs->fs_type == FS_FAT32) /* Replace cluster# 0 with root cluster# if in FAT32 */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1079 clst = dj->fs->dirbase;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1080
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1081 if (clst == 0) { /* Static table (root-dir in FAT12/16) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1082 dj->clust = clst;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1083 if (idx >= dj->fs->n_rootdir) /* Index is out of range */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1084 return FR_INT_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1085 dj->sect = dj->fs->dirbase + idx / (SS(dj->fs) / SZ_DIR); /* Sector# */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1086 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1087 else { /* Dynamic table (sub-dirs or root-dir in FAT32) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1088 ic = SS(dj->fs) / SZ_DIR * dj->fs->csize; /* Entries per cluster */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1089 while (idx >= ic) { /* Follow cluster chain */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1090 clst = get_fat(dj->fs, clst); /* Get next cluster */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1091 if (clst == 0xFFFFFFFF) return FR_DISK_ERR; /* Disk error */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1092 if (clst < 2 || clst >= dj->fs->n_fatent) /* Reached to end of table or int error */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1093 return FR_INT_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1094 idx -= ic;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1095 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1096 dj->clust = clst;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1097 dj->sect = clust2sect(dj->fs, clst) + idx / (SS(dj->fs) / SZ_DIR); /* Sector# */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1098 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1099
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1100 dj->dir = dj->fs->win + (idx % (SS(dj->fs) / SZ_DIR)) * SZ_DIR; /* Ptr to the entry in the sector */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1101
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1102 return FR_OK; /* Seek succeeded */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1103 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1104
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1105
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1106
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1107
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1108 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1109 /* Directory handling - Move directory index next */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1110 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1111
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1112 static
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1113 FRESULT dir_next ( /* FR_OK:Succeeded, FR_NO_FILE:End of table, FR_DENIED:EOT and could not stretch */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1114 DIR *dj, /* Pointer to directory object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1115 int stretch /* 0: Do not stretch table, 1: Stretch table if needed */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1116 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1117 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1118 DWORD clst;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1119 WORD i;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1120
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1121
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1122 stretch = stretch; /* To suppress warning on read-only cfg. */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1123 i = dj->index + 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1124 if (!i || !dj->sect) /* Report EOT when index has reached 65535 */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1125 return FR_NO_FILE;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1126
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1127 if (!(i % (SS(dj->fs) / SZ_DIR))) { /* Sector changed? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1128 dj->sect++; /* Next sector */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1129
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1130 if (dj->clust == 0) { /* Static table */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1131 if (i >= dj->fs->n_rootdir) /* Report EOT when end of table */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1132 return FR_NO_FILE;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1133 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1134 else { /* Dynamic table */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1135 if (((i / (SS(dj->fs) / SZ_DIR)) & (dj->fs->csize - 1)) == 0) { /* Cluster changed? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1136 clst = get_fat(dj->fs, dj->clust); /* Get next cluster */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1137 if (clst <= 1) return FR_INT_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1138 if (clst == 0xFFFFFFFF) return FR_DISK_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1139 if (clst >= dj->fs->n_fatent) { /* When it reached end of dynamic table */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1140 #if !_FS_READONLY
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1141 BYTE c;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1142 if (!stretch) return FR_NO_FILE; /* When do not stretch, report EOT */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1143 clst = create_chain(dj->fs, dj->clust); /* Stretch cluster chain */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1144 if (clst == 0) return FR_DENIED; /* No free cluster */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1145 if (clst == 1) return FR_INT_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1146 if (clst == 0xFFFFFFFF) return FR_DISK_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1147 /* Clean-up stretched table */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1148 if (move_window(dj->fs, 0)) return FR_DISK_ERR; /* Flush active window */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1149 mem_set(dj->fs->win, 0, SS(dj->fs)); /* Clear window buffer */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1150 dj->fs->winsect = clust2sect(dj->fs, clst); /* Cluster start sector */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1151 for (c = 0; c < dj->fs->csize; c++) { /* Fill the new cluster with 0 */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1152 dj->fs->wflag = 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1153 if (move_window(dj->fs, 0)) return FR_DISK_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1154 dj->fs->winsect++;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1155 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1156 dj->fs->winsect -= c; /* Rewind window address */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1157 #else
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1158 return FR_NO_FILE; /* Report EOT */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1159 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1160 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1161 dj->clust = clst; /* Initialize data for new cluster */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1162 dj->sect = clust2sect(dj->fs, clst);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1163 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1164 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1165 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1166
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1167 dj->index = i;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1168 dj->dir = dj->fs->win + (i % (SS(dj->fs) / SZ_DIR)) * SZ_DIR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1169
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1170 return FR_OK;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1171 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1172
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1173
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1174
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1175
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1176 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1177 /* LFN handling - Test/Pick/Fit an LFN segment from/to directory entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1178 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1179 #if _USE_LFN
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1180 static
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1181 const BYTE LfnOfs[] = {1,3,5,7,9,14,16,18,20,22,24,28,30}; /* Offset of LFN chars in the directory entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1182
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1183
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1184 static
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1185 int cmp_lfn ( /* 1:Matched, 0:Not matched */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1186 WCHAR *lfnbuf, /* Pointer to the LFN to be compared */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1187 BYTE *dir /* Pointer to the directory entry containing a part of LFN */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1188 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1189 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1190 UINT i, s;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1191 WCHAR wc, uc;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1192
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1193
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1194 i = ((dir[LDIR_Ord] & ~LLE) - 1) * 13; /* Get offset in the LFN buffer */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1195 s = 0; wc = 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1196 do {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1197 uc = LD_WORD(dir+LfnOfs[s]); /* Pick an LFN character from the entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1198 if (wc) { /* Last char has not been processed */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1199 wc = ff_wtoupper(uc); /* Convert it to upper case */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1200 if (i >= _MAX_LFN || wc != ff_wtoupper(lfnbuf[i++])) /* Compare it */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1201 return 0; /* Not matched */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1202 } else {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1203 if (uc != 0xFFFF) return 0; /* Check filler */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1204 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1205 } while (++s < 13); /* Repeat until all chars in the entry are checked */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1206
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1207 if ((dir[LDIR_Ord] & LLE) && wc && lfnbuf[i]) /* Last segment matched but different length */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1208 return 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1209
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1210 return 1; /* The part of LFN matched */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1211 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1212
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1213
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1214
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1215 static
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1216 int pick_lfn ( /* 1:Succeeded, 0:Buffer overflow */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1217 WCHAR *lfnbuf, /* Pointer to the Unicode-LFN buffer */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1218 BYTE *dir /* Pointer to the directory entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1219 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1220 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1221 UINT i, s;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1222 WCHAR wc, uc;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1223
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1224
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1225 i = ((dir[LDIR_Ord] & 0x3F) - 1) * 13; /* Offset in the LFN buffer */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1226
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1227 s = 0; wc = 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1228 do {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1229 uc = LD_WORD(dir+LfnOfs[s]); /* Pick an LFN character from the entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1230 if (wc) { /* Last char has not been processed */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1231 if (i >= _MAX_LFN) return 0; /* Buffer overflow? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1232 lfnbuf[i++] = wc = uc; /* Store it */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1233 } else {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1234 if (uc != 0xFFFF) return 0; /* Check filler */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1235 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1236 } while (++s < 13); /* Read all character in the entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1237
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1238 if (dir[LDIR_Ord] & LLE) { /* Put terminator if it is the last LFN part */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1239 if (i >= _MAX_LFN) return 0; /* Buffer overflow? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1240 lfnbuf[i] = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1241 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1242
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1243 return 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1244 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1245
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1246
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1247 #if !_FS_READONLY
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1248 static
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1249 void fit_lfn (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1250 const WCHAR *lfnbuf, /* Pointer to the LFN buffer */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1251 BYTE *dir, /* Pointer to the directory entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1252 BYTE ord, /* LFN order (1-20) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1253 BYTE sum /* SFN sum */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1254 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1255 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1256 UINT i, s;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1257 WCHAR wc;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1258
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1259
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1260 dir[LDIR_Chksum] = sum; /* Set check sum */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1261 dir[LDIR_Attr] = AM_LFN; /* Set attribute. LFN entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1262 dir[LDIR_Type] = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1263 ST_WORD(dir+LDIR_FstClusLO, 0);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1264
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1265 i = (ord - 1) * 13; /* Get offset in the LFN buffer */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1266 s = wc = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1267 do {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1268 if (wc != 0xFFFF) wc = lfnbuf[i++]; /* Get an effective char */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1269 ST_WORD(dir+LfnOfs[s], wc); /* Put it */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1270 if (!wc) wc = 0xFFFF; /* Padding chars following last char */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1271 } while (++s < 13);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1272 if (wc == 0xFFFF || !lfnbuf[i]) ord |= LLE; /* Bottom LFN part is the start of LFN sequence */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1273 dir[LDIR_Ord] = ord; /* Set the LFN order */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1274 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1275
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1276 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1277 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1278
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1279
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1280
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1281 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1282 /* Create numbered name */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1283 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1284 #if _USE_LFN
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1285 void gen_numname (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1286 BYTE *dst, /* Pointer to generated SFN */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1287 const BYTE *src, /* Pointer to source SFN to be modified */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1288 const WCHAR *lfn, /* Pointer to LFN */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1289 WORD seq /* Sequence number */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1290 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1291 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1292 BYTE ns[8], c;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1293 UINT i, j;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1294
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1295
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1296 mem_cpy(dst, src, 11);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1297
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1298 if (seq > 5) { /* On many collisions, generate a hash number instead of sequential number */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1299 do seq = (seq >> 1) + (seq << 15) + (WORD)*lfn++; while (*lfn);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1300 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1301
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1302 /* itoa (hexdecimal) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1303 i = 7;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1304 do {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1305 c = (seq % 16) + '0';
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1306 if (c > '9') c += 7;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1307 ns[i--] = c;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1308 seq /= 16;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1309 } while (seq);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1310 ns[i] = '~';
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1311
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1312 /* Append the number */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1313 for (j = 0; j < i && dst[j] != ' '; j++) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1314 if (IsDBCS1(dst[j])) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1315 if (j == i - 1) break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1316 j++;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1317 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1318 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1319 do {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1320 dst[j++] = (i < 8) ? ns[i++] : ' ';
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1321 } while (j < 8);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1322 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1323 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1324
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1325
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1326
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1327
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1328 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1329 /* Calculate sum of an SFN */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1330 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1331 #if _USE_LFN
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1332 static
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1333 BYTE sum_sfn (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1334 const BYTE *dir /* Ptr to directory entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1335 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1336 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1337 BYTE sum = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1338 UINT n = 11;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1339
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1340 do sum = (sum >> 1) + (sum << 7) + *dir++; while (--n);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1341 return sum;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1342 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1343 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1344
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1345
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1346
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1347
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1348 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1349 /* Directory handling - Find an object in the directory */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1350 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1351
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1352 static
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1353 FRESULT dir_find (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1354 DIR *dj /* Pointer to the directory object linked to the file name */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1355 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1356 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1357 FRESULT res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1358 BYTE c, *dir;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1359 #if _USE_LFN
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1360 BYTE a, ord, sum;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1361 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1362
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1363 res = dir_sdi(dj, 0); /* Rewind directory object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1364 if (res != FR_OK) return res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1365
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1366 #if _USE_LFN
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1367 ord = sum = 0xFF;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1368 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1369 do {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1370 res = move_window(dj->fs, dj->sect);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1371 if (res != FR_OK) break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1372 dir = dj->dir; /* Ptr to the directory entry of current index */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1373 c = dir[DIR_Name];
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1374 if (c == 0) { res = FR_NO_FILE; break; } /* Reached to end of table */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1375 #if _USE_LFN /* LFN configuration */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1376 a = dir[DIR_Attr] & AM_MASK;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1377 if (c == DDE || ((a & AM_VOL) && a != AM_LFN)) { /* An entry without valid data */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1378 ord = 0xFF;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1379 } else {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1380 if (a == AM_LFN) { /* An LFN entry is found */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1381 if (dj->lfn) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1382 if (c & LLE) { /* Is it start of LFN sequence? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1383 sum = dir[LDIR_Chksum];
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1384 c &= ~LLE; ord = c; /* LFN start order */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1385 dj->lfn_idx = dj->index;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1386 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1387 /* Check validity of the LFN entry and compare it with given name */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1388 ord = (c == ord && sum == dir[LDIR_Chksum] && cmp_lfn(dj->lfn, dir)) ? ord - 1 : 0xFF;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1389 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1390 } else { /* An SFN entry is found */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1391 if (!ord && sum == sum_sfn(dir)) break; /* LFN matched? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1392 ord = 0xFF; dj->lfn_idx = 0xFFFF; /* Reset LFN sequence */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1393 if (!(dj->fn[NS] & NS_LOSS) && !mem_cmp(dir, dj->fn, 11)) break; /* SFN matched? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1394 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1395 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1396 #else /* Non LFN configuration */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1397 if (!(dir[DIR_Attr] & AM_VOL) && !mem_cmp(dir, dj->fn, 11)) /* Is it a valid entry? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1398 break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1399 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1400 res = dir_next(dj, 0); /* Next entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1401 } while (res == FR_OK);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1402
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1403 return res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1404 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1405
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1406
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1407
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1408
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1409 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1410 /* Read an object from the directory */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1411 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1412 #if _FS_MINIMIZE <= 1
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1413 static
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1414 FRESULT dir_read (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1415 DIR *dj /* Pointer to the directory object that pointing the entry to be read */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1416 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1417 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1418 FRESULT res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1419 BYTE c, *dir;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1420 #if _USE_LFN
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1421 BYTE a, ord = 0xFF, sum = 0xFF;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1422 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1423
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1424 res = FR_NO_FILE;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1425 while (dj->sect) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1426 res = move_window(dj->fs, dj->sect);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1427 if (res != FR_OK) break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1428 dir = dj->dir; /* Ptr to the directory entry of current index */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1429 c = dir[DIR_Name];
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1430 if (c == 0) { res = FR_NO_FILE; break; } /* Reached to end of table */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1431 #if _USE_LFN /* LFN configuration */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1432 a = dir[DIR_Attr] & AM_MASK;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1433 if (c == DDE || (!_FS_RPATH && c == '.') || ((a & AM_VOL) && a != AM_LFN)) { /* An entry without valid data */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1434 ord = 0xFF;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1435 } else {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1436 if (a == AM_LFN) { /* An LFN entry is found */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1437 if (c & LLE) { /* Is it start of LFN sequence? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1438 sum = dir[LDIR_Chksum];
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1439 c &= ~LLE; ord = c;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1440 dj->lfn_idx = dj->index;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1441 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1442 /* Check LFN validity and capture it */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1443 ord = (c == ord && sum == dir[LDIR_Chksum] && pick_lfn(dj->lfn, dir)) ? ord - 1 : 0xFF;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1444 } else { /* An SFN entry is found */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1445 if (ord || sum != sum_sfn(dir)) /* Is there a valid LFN? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1446 dj->lfn_idx = 0xFFFF; /* It has no LFN. */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1447 break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1448 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1449 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1450 #else /* Non LFN configuration */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1451 if (c != DDE && (_FS_RPATH || c != '.') && !(dir[DIR_Attr] & AM_VOL)) /* Is it a valid entry? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1452 break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1453 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1454 res = dir_next(dj, 0); /* Next entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1455 if (res != FR_OK) break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1456 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1457
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1458 if (res != FR_OK) dj->sect = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1459
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1460 return res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1461 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1462 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1463
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1464
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1465
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1466 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1467 /* Register an object to the directory */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1468 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1469 #if !_FS_READONLY
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1470 static
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1471 FRESULT dir_register ( /* FR_OK:Successful, FR_DENIED:No free entry or too many SFN collision, FR_DISK_ERR:Disk error */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1472 DIR *dj /* Target directory with object name to be created */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1473 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1474 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1475 FRESULT res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1476 BYTE c, *dir;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1477 #if _USE_LFN /* LFN configuration */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1478 WORD n, ne, is;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1479 BYTE sn[12], *fn, sum;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1480 WCHAR *lfn;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1481
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1482
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1483 fn = dj->fn; lfn = dj->lfn;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1484 mem_cpy(sn, fn, 12);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1485
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1486 if (_FS_RPATH && (sn[NS] & NS_DOT)) /* Cannot create dot entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1487 return FR_INVALID_NAME;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1488
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1489 if (sn[NS] & NS_LOSS) { /* When LFN is out of 8.3 format, generate a numbered name */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1490 fn[NS] = 0; dj->lfn = 0; /* Find only SFN */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1491 for (n = 1; n < 100; n++) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1492 gen_numname(fn, sn, lfn, n); /* Generate a numbered name */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1493 res = dir_find(dj); /* Check if the name collides with existing SFN */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1494 if (res != FR_OK) break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1495 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1496 if (n == 100) return FR_DENIED; /* Abort if too many collisions */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1497 if (res != FR_NO_FILE) return res; /* Abort if the result is other than 'not collided' */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1498 fn[NS] = sn[NS]; dj->lfn = lfn;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1499 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1500
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1501 if (sn[NS] & NS_LFN) { /* When LFN is to be created, reserve an SFN + LFN entries. */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1502 for (ne = 0; lfn[ne]; ne++) ;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1503 ne = (ne + 25) / 13;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1504 } else { /* Otherwise reserve only an SFN entry. */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1505 ne = 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1506 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1507
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1508 /* Reserve contiguous entries */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1509 res = dir_sdi(dj, 0);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1510 if (res != FR_OK) return res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1511 n = is = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1512 do {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1513 res = move_window(dj->fs, dj->sect);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1514 if (res != FR_OK) break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1515 c = *dj->dir; /* Check the entry status */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1516 if (c == DDE || c == 0) { /* Is it a blank entry? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1517 if (n == 0) is = dj->index; /* First index of the contiguous entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1518 if (++n == ne) break; /* A contiguous entry that required count is found */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1519 } else {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1520 n = 0; /* Not a blank entry. Restart to search */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1521 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1522 res = dir_next(dj, 1); /* Next entry with table stretch */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1523 } while (res == FR_OK);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1524
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1525 if (res == FR_OK && ne > 1) { /* Initialize LFN entry if needed */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1526 res = dir_sdi(dj, is);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1527 if (res == FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1528 sum = sum_sfn(dj->fn); /* Sum of the SFN tied to the LFN */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1529 ne--;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1530 do { /* Store LFN entries in bottom first */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1531 res = move_window(dj->fs, dj->sect);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1532 if (res != FR_OK) break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1533 fit_lfn(dj->lfn, dj->dir, (BYTE)ne, sum);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1534 dj->fs->wflag = 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1535 res = dir_next(dj, 0); /* Next entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1536 } while (res == FR_OK && --ne);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1537 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1538 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1539
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1540 #else /* Non LFN configuration */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1541 res = dir_sdi(dj, 0);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1542 if (res == FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1543 do { /* Find a blank entry for the SFN */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1544 res = move_window(dj->fs, dj->sect);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1545 if (res != FR_OK) break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1546 c = *dj->dir;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1547 if (c == DDE || c == 0) break; /* Is it a blank entry? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1548 res = dir_next(dj, 1); /* Next entry with table stretch */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1549 } while (res == FR_OK);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1550 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1551 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1552
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1553 if (res == FR_OK) { /* Initialize the SFN entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1554 res = move_window(dj->fs, dj->sect);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1555 if (res == FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1556 dir = dj->dir;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1557 mem_set(dir, 0, SZ_DIR); /* Clean the entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1558 mem_cpy(dir, dj->fn, 11); /* Put SFN */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1559 #if _USE_LFN
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1560 dir[DIR_NTres] = *(dj->fn+NS) & (NS_BODY | NS_EXT); /* Put NT flag */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1561 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1562 dj->fs->wflag = 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1563 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1564 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1565
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1566 return res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1567 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1568 #endif /* !_FS_READONLY */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1569
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1570
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1571
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1572
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1573 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1574 /* Remove an object from the directory */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1575 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1576 #if !_FS_READONLY && !_FS_MINIMIZE
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1577 static
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1578 FRESULT dir_remove ( /* FR_OK: Successful, FR_DISK_ERR: A disk error */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1579 DIR *dj /* Directory object pointing the entry to be removed */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1580 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1581 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1582 FRESULT res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1583 #if _USE_LFN /* LFN configuration */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1584 WORD i;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1585
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1586 i = dj->index; /* SFN index */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1587 res = dir_sdi(dj, (WORD)((dj->lfn_idx == 0xFFFF) ? i : dj->lfn_idx)); /* Goto the SFN or top of the LFN entries */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1588 if (res == FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1589 do {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1590 res = move_window(dj->fs, dj->sect);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1591 if (res != FR_OK) break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1592 *dj->dir = DDE; /* Mark the entry "deleted" */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1593 dj->fs->wflag = 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1594 if (dj->index >= i) break; /* When reached SFN, all entries of the object has been deleted. */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1595 res = dir_next(dj, 0); /* Next entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1596 } while (res == FR_OK);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1597 if (res == FR_NO_FILE) res = FR_INT_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1598 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1599
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1600 #else /* Non LFN configuration */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1601 res = dir_sdi(dj, dj->index);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1602 if (res == FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1603 res = move_window(dj->fs, dj->sect);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1604 if (res == FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1605 *dj->dir = DDE; /* Mark the entry "deleted" */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1606 dj->fs->wflag = 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1607 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1608 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1609 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1610
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1611 return res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1612 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1613 #endif /* !_FS_READONLY */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1614
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1615
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1616
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1617
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1618 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1619 /* Pick a segment and create the object name in directory form */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1620 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1621
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1622 static
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1623 FRESULT create_name (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1624 DIR *dj, /* Pointer to the directory object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1625 const TCHAR **path /* Pointer to pointer to the segment in the path string */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1626 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1627 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1628 #ifdef _EXCVT
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1629 static const BYTE excvt[] = _EXCVT; /* Upper conversion table for extended chars */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1630 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1631
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1632 #if _USE_LFN /* LFN configuration */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1633 BYTE b, cf;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1634 WCHAR w, *lfn;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1635 UINT i, ni, si, di;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1636 const TCHAR *p;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1637
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1638 /* Create LFN in Unicode */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1639 for (p = *path; *p == '/' || *p == '\\'; p++) ; /* Strip duplicated separator */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1640 lfn = dj->lfn;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1641 si = di = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1642 for (;;) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1643 w = p[si++]; /* Get a character */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1644 if (w < ' ' || w == '/' || w == '\\') break; /* Break on end of segment */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1645 if (di >= _MAX_LFN) /* Reject too long name */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1646 return FR_INVALID_NAME;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1647 #if !_LFN_UNICODE
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1648 w &= 0xFF;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1649 if (IsDBCS1(w)) { /* Check if it is a DBC 1st byte (always false on SBCS cfg) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1650 b = (BYTE)p[si++]; /* Get 2nd byte */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1651 if (!IsDBCS2(b))
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1652 return FR_INVALID_NAME; /* Reject invalid sequence */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1653 w = (w << 8) + b; /* Create a DBC */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1654 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1655 w = ff_convert(w, 1); /* Convert ANSI/OEM to Unicode */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1656 if (!w) return FR_INVALID_NAME; /* Reject invalid code */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1657 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1658 if (w < 0x80 && chk_chr("\"*:<>\?|\x7F", w)) /* Reject illegal chars for LFN */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1659 return FR_INVALID_NAME;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1660 lfn[di++] = w; /* Store the Unicode char */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1661 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1662 *path = &p[si]; /* Return pointer to the next segment */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1663 cf = (w < ' ') ? NS_LAST : 0; /* Set last segment flag if end of path */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1664 #if _FS_RPATH
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1665 if ((di == 1 && lfn[di-1] == '.') || /* Is this a dot entry? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1666 (di == 2 && lfn[di-1] == '.' && lfn[di-2] == '.')) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1667 lfn[di] = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1668 for (i = 0; i < 11; i++)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1669 dj->fn[i] = (i < di) ? '.' : ' ';
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1670 dj->fn[i] = cf | NS_DOT; /* This is a dot entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1671 return FR_OK;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1672 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1673 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1674 while (di) { /* Strip trailing spaces and dots */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1675 w = lfn[di-1];
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1676 if (w != ' ' && w != '.') break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1677 di--;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1678 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1679 if (!di) return FR_INVALID_NAME; /* Reject nul string */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1680
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1681 lfn[di] = 0; /* LFN is created */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1682
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1683 /* Create SFN in directory form */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1684 mem_set(dj->fn, ' ', 11);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1685 for (si = 0; lfn[si] == ' ' || lfn[si] == '.'; si++) ; /* Strip leading spaces and dots */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1686 if (si) cf |= NS_LOSS | NS_LFN;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1687 while (di && lfn[di - 1] != '.') di--; /* Find extension (di<=si: no extension) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1688
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1689 b = i = 0; ni = 8;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1690 for (;;) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1691 w = lfn[si++]; /* Get an LFN char */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1692 if (!w) break; /* Break on end of the LFN */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1693 if (w == ' ' || (w == '.' && si != di)) { /* Remove spaces and dots */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1694 cf |= NS_LOSS | NS_LFN; continue;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1695 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1696
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1697 if (i >= ni || si == di) { /* Extension or end of SFN */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1698 if (ni == 11) { /* Long extension */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1699 cf |= NS_LOSS | NS_LFN; break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1700 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1701 if (si != di) cf |= NS_LOSS | NS_LFN; /* Out of 8.3 format */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1702 if (si > di) break; /* No extension */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1703 si = di; i = 8; ni = 11; /* Enter extension section */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1704 b <<= 2; continue;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1705 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1706
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1707 if (w >= 0x80) { /* Non ASCII char */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1708 #ifdef _EXCVT
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1709 w = ff_convert(w, 0); /* Unicode -> OEM code */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1710 if (w) w = excvt[w - 0x80]; /* Convert extended char to upper (SBCS) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1711 #else
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1712 w = ff_convert(ff_wtoupper(w), 0); /* Upper converted Unicode -> OEM code */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1713 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1714 cf |= NS_LFN; /* Force create LFN entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1715 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1716
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1717 if (_DF1S && w >= 0x100) { /* Double byte char (always false on SBCS cfg) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1718 if (i >= ni - 1) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1719 cf |= NS_LOSS | NS_LFN; i = ni; continue;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1720 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1721 dj->fn[i++] = (BYTE)(w >> 8);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1722 } else { /* Single byte char */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1723 if (!w || chk_chr("+,;=[]", w)) { /* Replace illegal chars for SFN */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1724 w = '_'; cf |= NS_LOSS | NS_LFN;/* Lossy conversion */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1725 } else {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1726 if (IsUpper(w)) { /* ASCII large capital */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1727 b |= 2;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1728 } else {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1729 if (IsLower(w)) { /* ASCII small capital */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1730 b |= 1; w -= 0x20;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1731 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1732 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1733 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1734 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1735 dj->fn[i++] = (BYTE)w;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1736 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1737
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1738 if (dj->fn[0] == DDE) dj->fn[0] = NDDE; /* If the first char collides with deleted mark, replace it with 0x05 */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1739
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1740 if (ni == 8) b <<= 2;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1741 if ((b & 0x0C) == 0x0C || (b & 0x03) == 0x03) /* Create LFN entry when there are composite capitals */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1742 cf |= NS_LFN;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1743 if (!(cf & NS_LFN)) { /* When LFN is in 8.3 format without extended char, NT flags are created */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1744 if ((b & 0x03) == 0x01) cf |= NS_EXT; /* NT flag (Extension has only small capital) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1745 if ((b & 0x0C) == 0x04) cf |= NS_BODY; /* NT flag (Filename has only small capital) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1746 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1747
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1748 dj->fn[NS] = cf; /* SFN is created */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1749
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1750 return FR_OK;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1751
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1752
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1753 #else /* Non-LFN configuration */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1754 BYTE b, c, d, *sfn;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1755 UINT ni, si, i;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1756 const char *p;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1757
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1758 /* Create file name in directory form */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1759 for (p = *path; *p == '/' || *p == '\\'; p++) ; /* Strip duplicated separator */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1760 sfn = dj->fn;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1761 mem_set(sfn, ' ', 11);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1762 si = i = b = 0; ni = 8;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1763 #if _FS_RPATH
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1764 if (p[si] == '.') { /* Is this a dot entry? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1765 for (;;) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1766 c = (BYTE)p[si++];
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1767 if (c != '.' || si >= 3) break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1768 sfn[i++] = c;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1769 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1770 if (c != '/' && c != '\\' && c > ' ') return FR_INVALID_NAME;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1771 *path = &p[si]; /* Return pointer to the next segment */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1772 sfn[NS] = (c <= ' ') ? NS_LAST | NS_DOT : NS_DOT; /* Set last segment flag if end of path */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1773 return FR_OK;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1774 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1775 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1776 for (;;) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1777 c = (BYTE)p[si++];
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1778 if (c <= ' ' || c == '/' || c == '\\') break; /* Break on end of segment */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1779 if (c == '.' || i >= ni) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1780 if (ni != 8 || c != '.') return FR_INVALID_NAME;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1781 i = 8; ni = 11;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1782 b <<= 2; continue;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1783 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1784 if (c >= 0x80) { /* Extended char? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1785 b |= 3; /* Eliminate NT flag */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1786 #ifdef _EXCVT
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1787 c = excvt[c-0x80]; /* Upper conversion (SBCS) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1788 #else
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1789 #if !_DF1S /* ASCII only cfg */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1790 return FR_INVALID_NAME;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1791 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1792 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1793 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1794 if (IsDBCS1(c)) { /* Check if it is a DBC 1st byte (always false on SBCS cfg) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1795 d = (BYTE)p[si++]; /* Get 2nd byte */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1796 if (!IsDBCS2(d) || i >= ni - 1) /* Reject invalid DBC */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1797 return FR_INVALID_NAME;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1798 sfn[i++] = c;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1799 sfn[i++] = d;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1800 } else { /* Single byte code */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1801 if (chk_chr("\"*+,:;<=>\?[]|\x7F", c)) /* Reject illegal chrs for SFN */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1802 return FR_INVALID_NAME;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1803 if (IsUpper(c)) { /* ASCII large capital? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1804 b |= 2;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1805 } else {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1806 if (IsLower(c)) { /* ASCII small capital? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1807 b |= 1; c -= 0x20;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1808 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1809 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1810 sfn[i++] = c;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1811 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1812 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1813 *path = &p[si]; /* Return pointer to the next segment */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1814 c = (c <= ' ') ? NS_LAST : 0; /* Set last segment flag if end of path */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1815
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1816 if (!i) return FR_INVALID_NAME; /* Reject nul string */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1817 if (sfn[0] == DDE) sfn[0] = NDDE; /* When first char collides with DDE, replace it with 0x05 */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1818
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1819 if (ni == 8) b <<= 2;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1820 if ((b & 0x03) == 0x01) c |= NS_EXT; /* NT flag (Name extension has only small capital) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1821 if ((b & 0x0C) == 0x04) c |= NS_BODY; /* NT flag (Name body has only small capital) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1822
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1823 sfn[NS] = c; /* Store NT flag, File name is created */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1824
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1825 return FR_OK;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1826 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1827 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1828
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1829
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1830
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1831
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1832 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1833 /* Get file information from directory entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1834 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1835 #if _FS_MINIMIZE <= 1
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1836 static
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1837 void get_fileinfo ( /* No return code */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1838 DIR *dj, /* Pointer to the directory object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1839 FILINFO *fno /* Pointer to the file information to be filled */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1840 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1841 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1842 UINT i;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1843 BYTE nt, *dir;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1844 TCHAR *p, c;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1845
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1846
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1847 p = fno->fname;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1848 if (dj->sect) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1849 dir = dj->dir;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1850 nt = dir[DIR_NTres]; /* NT flag */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1851 for (i = 0; i < 8; i++) { /* Copy name body */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1852 c = dir[i];
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1853 if (c == ' ') break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1854 if (c == NDDE) c = (TCHAR)DDE;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1855 if (_USE_LFN && (nt & NS_BODY) && IsUpper(c)) c += 0x20;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1856 #if _LFN_UNICODE
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1857 if (IsDBCS1(c) && i < 7 && IsDBCS2(dir[i+1]))
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1858 c = (c << 8) | dir[++i];
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1859 c = ff_convert(c, 1);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1860 if (!c) c = '?';
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1861 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1862 *p++ = c;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1863 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1864 if (dir[8] != ' ') { /* Copy name extension */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1865 *p++ = '.';
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1866 for (i = 8; i < 11; i++) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1867 c = dir[i];
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1868 if (c == ' ') break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1869 if (_USE_LFN && (nt & NS_EXT) && IsUpper(c)) c += 0x20;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1870 #if _LFN_UNICODE
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1871 if (IsDBCS1(c) && i < 10 && IsDBCS2(dir[i+1]))
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1872 c = (c << 8) | dir[++i];
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1873 c = ff_convert(c, 1);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1874 if (!c) c = '?';
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1875 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1876 *p++ = c;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1877 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1878 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1879 fno->fattrib = dir[DIR_Attr]; /* Attribute */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1880 fno->fsize = LD_DWORD(dir+DIR_FileSize); /* Size */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1881 fno->fdate = LD_WORD(dir+DIR_WrtDate); /* Date */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1882 fno->ftime = LD_WORD(dir+DIR_WrtTime); /* Time */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1883 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1884 *p = 0; /* Terminate SFN str by a \0 */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1885
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1886 #if _USE_LFN
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1887 if (fno->lfname && fno->lfsize) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1888 TCHAR *tp = fno->lfname;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1889 WCHAR w, *lfn;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1890
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1891 i = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1892 if (dj->sect && dj->lfn_idx != 0xFFFF) {/* Get LFN if available */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1893 lfn = dj->lfn;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1894 while ((w = *lfn++) != 0) { /* Get an LFN char */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1895 #if !_LFN_UNICODE
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1896 w = ff_convert(w, 0); /* Unicode -> OEM conversion */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1897 if (!w) { i = 0; break; } /* Could not convert, no LFN */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1898 if (_DF1S && w >= 0x100) /* Put 1st byte if it is a DBC (always false on SBCS cfg) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1899 tp[i++] = (TCHAR)(w >> 8);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1900 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1901 if (i >= fno->lfsize - 1) { i = 0; break; } /* Buffer overflow, no LFN */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1902 tp[i++] = (TCHAR)w;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1903 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1904 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1905 tp[i] = 0; /* Terminate the LFN str by a \0 */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1906 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1907 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1908 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1909 #endif /* _FS_MINIMIZE <= 1 */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1910
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1911
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1912
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1913
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1914 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1915 /* Follow a file path */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1916 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1917
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1918 static
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1919 FRESULT follow_path ( /* FR_OK(0): successful, !=0: error code */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1920 DIR *dj, /* Directory object to return last directory and found object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1921 const TCHAR *path /* Full-path string to find a file or directory */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1922 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1923 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1924 FRESULT res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1925 BYTE *dir, ns;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1926
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1927
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1928 #if _FS_RPATH
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1929 if (*path == '/' || *path == '\\') { /* There is a heading separator */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1930 path++; dj->sclust = 0; /* Strip it and start from the root dir */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1931 } else { /* No heading separator */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1932 dj->sclust = dj->fs->cdir; /* Start from the current dir */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1933 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1934 #else
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1935 if (*path == '/' || *path == '\\') /* Strip heading separator if exist */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1936 path++;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1937 dj->sclust = 0; /* Start from the root dir */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1938 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1939
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1940 if ((UINT)*path < ' ') { /* Nul path means the start directory itself */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1941 res = dir_sdi(dj, 0);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1942 dj->dir = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1943
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1944 } else { /* Follow path */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1945 for (;;) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1946 res = create_name(dj, &path); /* Get a segment */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1947 if (res != FR_OK) break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1948 res = dir_find(dj); /* Find it */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1949 ns = *(dj->fn+NS);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1950 if (res != FR_OK) { /* Failed to find the object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1951 if (res != FR_NO_FILE) break; /* Abort if any hard error occured */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1952 /* Object not found */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1953 if (_FS_RPATH && (ns & NS_DOT)) { /* If dot entry is not exit */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1954 dj->sclust = 0; dj->dir = 0; /* It is the root dir */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1955 res = FR_OK;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1956 if (!(ns & NS_LAST)) continue;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1957 } else { /* Could not find the object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1958 if (!(ns & NS_LAST)) res = FR_NO_PATH;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1959 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1960 break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1961 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1962 if (ns & NS_LAST) break; /* Last segment match. Function completed. */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1963 dir = dj->dir; /* There is next segment. Follow the sub directory */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1964 if (!(dir[DIR_Attr] & AM_DIR)) { /* Cannot follow because it is a file */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1965 res = FR_NO_PATH; break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1966 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1967 dj->sclust = LD_CLUST(dir);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1968 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1969 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1970
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1971 return res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1972 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1973
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1974
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1975
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1976
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1977 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1978 /* Load a sector and check if it is an FAT Volume Boot Record */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1979 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1980
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1981 static
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1982 BYTE check_fs ( /* 0:FAT-VBR, 1:Valid BR but not FAT, 2:Not a BR, 3:Disk error */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1983 FATFS *fs, /* File system object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1984 DWORD sect /* Sector# (lba) to check if it is an FAT boot record or not */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1985 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1986 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1987 if (disk_read(fs->drv, fs->win, sect, 1) != RES_OK) /* Load boot record */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1988 return 3;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1989 if (LD_WORD(&fs->win[BS_55AA]) != 0xAA55) /* Check record signature (always placed at offset 510 even if the sector size is >512) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1990 return 2;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1991
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1992 if ((LD_DWORD(&fs->win[BS_FilSysType]) & 0xFFFFFF) == 0x544146) /* Check "FAT" string */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1993 return 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1994 if ((LD_DWORD(&fs->win[BS_FilSysType32]) & 0xFFFFFF) == 0x544146)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1995 return 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1996
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1997 return 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1998 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1999
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2000
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2001
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2002
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2003 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2004 /* Check if the file system object is valid or not */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2005 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2006
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2007 static
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2008 FRESULT chk_mounted ( /* FR_OK(0): successful, !=0: any error occurred */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2009 const TCHAR **path, /* Pointer to pointer to the path name (drive number) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2010 FATFS **rfs, /* Pointer to pointer to the found file system object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2011 BYTE chk_wp /* !=0: Check media write protection for write access */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2012 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2013 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2014 BYTE fmt, b, pi, *tbl;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2015 UINT vol;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2016 DSTATUS stat;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2017 DWORD bsect, fasize, tsect, sysect, nclst, szbfat;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2018 WORD nrsv;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2019 const TCHAR *p = *path;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2020 FATFS *fs;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2021
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2022 /* Get logical drive number from the path name */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2023 vol = p[0] - '0'; /* Is there a drive number? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2024 if (vol <= 9 && p[1] == ':') { /* Found a drive number, get and strip it */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2025 p += 2; *path = p; /* Return pointer to the path name */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2026 } else { /* No drive number is given */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2027 #if _FS_RPATH
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2028 vol = CurrVol; /* Use current drive */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2029 #else
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2030 vol = 0; /* Use drive 0 */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2031 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2032 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2033
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2034 /* Check if the file system object is valid or not */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2035 if (vol >= _VOLUMES) /* Is the drive number valid? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2036 return FR_INVALID_DRIVE;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2037 *rfs = fs = FatFs[vol]; /* Return pointer to the corresponding file system object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2038 if (!fs) return FR_NOT_ENABLED; /* Is the file system object available? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2039
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2040 ENTER_FF(fs); /* Lock file system */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2041
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2042 if (fs->fs_type) { /* If the logical drive has been mounted */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2043 stat = disk_status(fs->drv);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2044 if (!(stat & STA_NOINIT)) { /* and the physical drive is kept initialized (has not been changed), */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2045 if (!_FS_READONLY && chk_wp && (stat & STA_PROTECT)) /* Check write protection if needed */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2046 return FR_WRITE_PROTECTED;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2047 return FR_OK; /* The file system object is valid */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2048 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2049 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2050
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2051 /* The file system object is not valid. */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2052 /* Following code attempts to mount the volume. (analyze BPB and initialize the fs object) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2053
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2054 fs->fs_type = 0; /* Clear the file system object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2055 fs->drv = LD2PD(vol); /* Bind the logical drive and a physical drive */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2056 stat = disk_initialize(fs->drv); /* Initialize low level disk I/O layer */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2057 if (stat & STA_NOINIT) /* Check if the initialization succeeded */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2058 return FR_NOT_READY; /* Failed to initialize due to no media or hard error */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2059 if (!_FS_READONLY && chk_wp && (stat & STA_PROTECT)) /* Check disk write protection if needed */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2060 return FR_WRITE_PROTECTED;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2061 #if _MAX_SS != 512 /* Get disk sector size (variable sector size cfg only) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2062 if (disk_ioctl(fs->drv, GET_SECTOR_SIZE, &fs->ssize) != RES_OK)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2063 return FR_DISK_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2064 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2065 /* Search FAT partition on the drive. Supports only generic partitionings, FDISK and SFD. */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2066 fmt = check_fs(fs, bsect = 0); /* Load sector 0 and check if it is an FAT-VBR (in SFD) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2067 if (LD2PT(vol) && !fmt) fmt = 1; /* Force non-SFD if the volume is forced partition */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2068 if (fmt == 1) { /* Not an FAT-VBR, the physical drive can be partitioned */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2069 /* Check the partition listed in the partition table */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2070 pi = LD2PT(vol);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2071 if (pi) pi--;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2072 tbl = &fs->win[MBR_Table + pi * SZ_PTE];/* Partition table */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2073 if (tbl[4]) { /* Is the partition existing? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2074 bsect = LD_DWORD(&tbl[8]); /* Partition offset in LBA */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2075 fmt = check_fs(fs, bsect); /* Check the partition */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2076 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2077 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2078 if (fmt == 3) return FR_DISK_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2079 if (fmt) return FR_NO_FILESYSTEM; /* No FAT volume is found */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2080
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2081 /* An FAT volume is found. Following code initializes the file system object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2082
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2083 if (LD_WORD(fs->win+BPB_BytsPerSec) != SS(fs)) /* (BPB_BytsPerSec must be equal to the physical sector size) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2084 return FR_NO_FILESYSTEM;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2085
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2086 fasize = LD_WORD(fs->win+BPB_FATSz16); /* Number of sectors per FAT */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2087 if (!fasize) fasize = LD_DWORD(fs->win+BPB_FATSz32);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2088 fs->fsize = fasize;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2089
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2090 fs->n_fats = b = fs->win[BPB_NumFATs]; /* Number of FAT copies */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2091 if (b != 1 && b != 2) return FR_NO_FILESYSTEM; /* (Must be 1 or 2) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2092 fasize *= b; /* Number of sectors for FAT area */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2093
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2094 fs->csize = b = fs->win[BPB_SecPerClus]; /* Number of sectors per cluster */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2095 if (!b || (b & (b - 1))) return FR_NO_FILESYSTEM; /* (Must be power of 2) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2096
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2097 fs->n_rootdir = LD_WORD(fs->win+BPB_RootEntCnt); /* Number of root directory entries */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2098 if (fs->n_rootdir % (SS(fs) / SZ_DIR)) return FR_NO_FILESYSTEM; /* (BPB_RootEntCnt must be sector aligned) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2099
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2100 tsect = LD_WORD(fs->win+BPB_TotSec16); /* Number of sectors on the volume */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2101 if (!tsect) tsect = LD_DWORD(fs->win+BPB_TotSec32);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2102
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2103 nrsv = LD_WORD(fs->win+BPB_RsvdSecCnt); /* Number of reserved sectors */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2104 if (!nrsv) return FR_NO_FILESYSTEM; /* (BPB_RsvdSecCnt must not be 0) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2105
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2106 /* Determine the FAT sub type */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2107 sysect = nrsv + fasize + fs->n_rootdir / (SS(fs) / SZ_DIR); /* RSV+FAT+DIR */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2108 if (tsect < sysect) return FR_NO_FILESYSTEM; /* (Invalid volume size) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2109 nclst = (tsect - sysect) / fs->csize; /* Number of clusters */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2110 if (!nclst) return FR_NO_FILESYSTEM; /* (Invalid volume size) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2111 fmt = FS_FAT12;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2112 if (nclst >= MIN_FAT16) fmt = FS_FAT16;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2113 if (nclst >= MIN_FAT32) fmt = FS_FAT32;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2114
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2115 /* Boundaries and Limits */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2116 fs->n_fatent = nclst + 2; /* Number of FAT entries */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2117 fs->database = bsect + sysect; /* Data start sector */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2118 fs->fatbase = bsect + nrsv; /* FAT start sector */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2119 if (fmt == FS_FAT32) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2120 if (fs->n_rootdir) return FR_NO_FILESYSTEM; /* (BPB_RootEntCnt must be 0) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2121 fs->dirbase = LD_DWORD(fs->win+BPB_RootClus); /* Root directory start cluster */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2122 szbfat = fs->n_fatent * 4; /* (Required FAT size) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2123 } else {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2124 if (!fs->n_rootdir) return FR_NO_FILESYSTEM; /* (BPB_RootEntCnt must not be 0) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2125 fs->dirbase = fs->fatbase + fasize; /* Root directory start sector */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2126 szbfat = (fmt == FS_FAT16) ? /* (Required FAT size) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2127 fs->n_fatent * 2 : fs->n_fatent * 3 / 2 + (fs->n_fatent & 1);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2128 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2129 if (fs->fsize < (szbfat + (SS(fs) - 1)) / SS(fs)) /* (BPB_FATSz must not be less than required) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2130 return FR_NO_FILESYSTEM;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2131
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2132 #if !_FS_READONLY
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2133 /* Initialize cluster allocation information */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2134 fs->free_clust = 0xFFFFFFFF;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2135 fs->last_clust = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2136
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2137 /* Get fsinfo if available */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2138 if (fmt == FS_FAT32) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2139 fs->fsi_flag = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2140 fs->fsi_sector = bsect + LD_WORD(fs->win+BPB_FSInfo);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2141 if (disk_read(fs->drv, fs->win, fs->fsi_sector, 1) == RES_OK &&
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2142 LD_WORD(fs->win+BS_55AA) == 0xAA55 &&
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2143 LD_DWORD(fs->win+FSI_LeadSig) == 0x41615252 &&
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2144 LD_DWORD(fs->win+FSI_StrucSig) == 0x61417272) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2145 fs->last_clust = LD_DWORD(fs->win+FSI_Nxt_Free);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2146 fs->free_clust = LD_DWORD(fs->win+FSI_Free_Count);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2147 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2148 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2149 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2150 fs->fs_type = fmt; /* FAT sub-type */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2151 fs->id = ++Fsid; /* File system mount ID */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2152 fs->winsect = 0; /* Invalidate sector cache */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2153 fs->wflag = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2154 #if _FS_RPATH
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2155 fs->cdir = 0; /* Current directory (root dir) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2156 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2157 #if _FS_SHARE /* Clear file lock semaphores */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2158 clear_lock(fs);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2159 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2160
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2161 return FR_OK;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2162 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2163
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2164
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2165
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2166
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2167 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2168 /* Check if the file/dir object is valid or not */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2169 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2170
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2171 static
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2172 FRESULT validate ( /* FR_OK(0): The object is valid, !=0: Invalid */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2173 FATFS *fs, /* Pointer to the file system object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2174 WORD id /* Member id of the target object to be checked */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2175 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2176 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2177 if (!fs || !fs->fs_type || fs->id != id)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2178 return FR_INVALID_OBJECT;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2179
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2180 ENTER_FF(fs); /* Lock file system */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2181
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2182 if (disk_status(fs->drv) & STA_NOINIT)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2183 return FR_NOT_READY;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2184
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2185 return FR_OK;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2186 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2187
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2188
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2189
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2190
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2191 /*--------------------------------------------------------------------------
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2192
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2193 Public Functions
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2194
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2195 --------------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2196
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2197
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2198
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2199 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2200 /* Mount/Unmount a Logical Drive */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2201 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2202
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2203 FRESULT f_mount (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2204 BYTE vol, /* Logical drive number to be mounted/unmounted */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2205 FATFS *fs /* Pointer to new file system object (NULL for unmount)*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2206 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2207 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2208 FATFS *rfs;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2209
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2210
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2211 if (vol >= _VOLUMES) /* Check if the drive number is valid */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2212 return FR_INVALID_DRIVE;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2213 rfs = FatFs[vol]; /* Get current fs object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2214
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2215 if (rfs) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2216 #if _FS_SHARE
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2217 clear_lock(rfs);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2218 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2219 #if _FS_REENTRANT /* Discard sync object of the current volume */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2220 if (!ff_del_syncobj(rfs->sobj)) return FR_INT_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2221 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2222 rfs->fs_type = 0; /* Clear old fs object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2223 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2224
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2225 if (fs) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2226 fs->fs_type = 0; /* Clear new fs object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2227 #if _FS_REENTRANT /* Create sync object for the new volume */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2228 if (!ff_cre_syncobj(vol, &fs->sobj)) return FR_INT_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2229 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2230 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2231 FatFs[vol] = fs; /* Register new fs object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2232
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2233 return FR_OK;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2234 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2235
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2236
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2237
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2238
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2239 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2240 /* Open or Create a File */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2241 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2242
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2243 FRESULT f_open (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2244 FIL *fp, /* Pointer to the blank file object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2245 const TCHAR *path, /* Pointer to the file name */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2246 BYTE mode /* Access mode and file open mode flags */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2247 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2248 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2249 FRESULT res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2250 DIR dj;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2251 BYTE *dir;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2252 DEF_NAMEBUF;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2253
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2254
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2255 fp->fs = 0; /* Clear file object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2256
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2257 #if !_FS_READONLY
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2258 mode &= FA_READ | FA_WRITE | FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2259 res = chk_mounted(&path, &dj.fs, (BYTE)(mode & ~FA_READ));
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2260 #else
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2261 mode &= FA_READ;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2262 res = chk_mounted(&path, &dj.fs, 0);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2263 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2264 INIT_BUF(dj);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2265 if (res == FR_OK)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2266 res = follow_path(&dj, path); /* Follow the file path */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2267 dir = dj.dir;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2268
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2269 #if !_FS_READONLY /* R/W configuration */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2270 if (res == FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2271 if (!dir) /* Current dir itself */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2272 res = FR_INVALID_NAME;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2273 #if _FS_SHARE
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2274 else
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2275 res = chk_lock(&dj, (mode & ~FA_READ) ? 1 : 0);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2276 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2277 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2278 /* Create or Open a file */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2279 if (mode & (FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW)) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2280 DWORD dw, cl;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2281
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2282 if (res != FR_OK) { /* No file, create new */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2283 if (res == FR_NO_FILE) /* There is no file to open, create a new entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2284 #if _FS_SHARE
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2285 res = enq_lock() ? dir_register(&dj) : FR_TOO_MANY_OPEN_FILES;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2286 #else
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2287 res = dir_register(&dj);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2288 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2289 mode |= FA_CREATE_ALWAYS; /* File is created */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2290 dir = dj.dir; /* New entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2291 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2292 else { /* Any object is already existing */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2293 if (dir[DIR_Attr] & (AM_RDO | AM_DIR)) { /* Cannot overwrite it (R/O or DIR) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2294 res = FR_DENIED;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2295 } else {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2296 if (mode & FA_CREATE_NEW) /* Cannot create as new file */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2297 res = FR_EXIST;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2298 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2299 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2300 if (res == FR_OK && (mode & FA_CREATE_ALWAYS)) { /* Truncate it if overwrite mode */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2301 dw = get_fattime(); /* Created time */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2302 ST_DWORD(dir+DIR_CrtTime, dw);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2303 dir[DIR_Attr] = 0; /* Reset attribute */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2304 ST_DWORD(dir+DIR_FileSize, 0); /* size = 0 */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2305 cl = LD_CLUST(dir); /* Get start cluster */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2306 ST_CLUST(dir, 0); /* cluster = 0 */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2307 dj.fs->wflag = 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2308 if (cl) { /* Remove the cluster chain if exist */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2309 dw = dj.fs->winsect;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2310 res = remove_chain(dj.fs, cl);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2311 if (res == FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2312 dj.fs->last_clust = cl - 1; /* Reuse the cluster hole */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2313 res = move_window(dj.fs, dw);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2314 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2315 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2316 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2317 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2318 else { /* Open an existing file */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2319 if (res == FR_OK) { /* Follow succeeded */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2320 if (dir[DIR_Attr] & AM_DIR) { /* It is a directory */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2321 res = FR_NO_FILE;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2322 } else {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2323 if ((mode & FA_WRITE) && (dir[DIR_Attr] & AM_RDO)) /* R/O violation */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2324 res = FR_DENIED;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2325 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2326 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2327 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2328 if (res == FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2329 if (mode & FA_CREATE_ALWAYS) /* Set file change flag if created or overwritten */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2330 mode |= FA__WRITTEN;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2331 fp->dir_sect = dj.fs->winsect; /* Pointer to the directory entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2332 fp->dir_ptr = dir;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2333 #if _FS_SHARE
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2334 fp->lockid = inc_lock(&dj, (mode & ~FA_READ) ? 1 : 0);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2335 if (!fp->lockid) res = FR_INT_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2336 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2337 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2338
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2339 #else /* R/O configuration */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2340 if (res == FR_OK) { /* Follow succeeded */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2341 if (!dir) { /* Current dir itself */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2342 res = FR_INVALID_NAME;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2343 } else {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2344 if (dir[DIR_Attr] & AM_DIR) /* It is a directory */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2345 res = FR_NO_FILE;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2346 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2347 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2348 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2349 FREE_BUF();
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2350
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2351 if (res == FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2352 fp->flag = mode; /* File access mode */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2353 fp->sclust = LD_CLUST(dir); /* File start cluster */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2354 fp->fsize = LD_DWORD(dir+DIR_FileSize); /* File size */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2355 fp->fptr = 0; /* File pointer */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2356 fp->dsect = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2357 #if _USE_FASTSEEK
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2358 fp->cltbl = 0; /* Normal seek mode */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2359 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2360 fp->fs = dj.fs; fp->id = dj.fs->id; /* Validate file object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2361 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2362
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2363 LEAVE_FF(dj.fs, res);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2364 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2365
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2366
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2367
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2368
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2369 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2370 /* Read File */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2371 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2372
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2373 FRESULT f_read (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2374 FIL *fp, /* Pointer to the file object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2375 void *buff, /* Pointer to data buffer */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2376 UINT btr, /* Number of bytes to read */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2377 UINT *br /* Pointer to number of bytes read */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2378 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2379 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2380 FRESULT res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2381 DWORD clst, sect, remain;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2382 UINT rcnt, cc;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2383 BYTE csect, *rbuff = buff;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2384
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2385
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2386 *br = 0; /* Initialize byte counter */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2387
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2388 res = validate(fp->fs, fp->id); /* Check validity */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2389 if (res != FR_OK) LEAVE_FF(fp->fs, res);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2390 if (fp->flag & FA__ERROR) /* Aborted file? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2391 LEAVE_FF(fp->fs, FR_INT_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2392 if (!(fp->flag & FA_READ)) /* Check access mode */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2393 LEAVE_FF(fp->fs, FR_DENIED);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2394 remain = fp->fsize - fp->fptr;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2395 if (btr > remain) btr = (UINT)remain; /* Truncate btr by remaining bytes */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2396
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2397 for ( ; btr; /* Repeat until all data read */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2398 rbuff += rcnt, fp->fptr += rcnt, *br += rcnt, btr -= rcnt) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2399 if ((fp->fptr % SS(fp->fs)) == 0) { /* On the sector boundary? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2400 csect = (BYTE)(fp->fptr / SS(fp->fs) & (fp->fs->csize - 1)); /* Sector offset in the cluster */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2401 if (!csect) { /* On the cluster boundary? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2402 if (fp->fptr == 0) { /* On the top of the file? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2403 clst = fp->sclust; /* Follow from the origin */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2404 } else { /* Middle or end of the file */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2405 #if _USE_FASTSEEK
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2406 if (fp->cltbl)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2407 clst = clmt_clust(fp, fp->fptr); /* Get cluster# from the CLMT */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2408 else
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2409 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2410 clst = get_fat(fp->fs, fp->clust); /* Follow cluster chain on the FAT */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2411 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2412 if (clst < 2) ABORT(fp->fs, FR_INT_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2413 if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2414 fp->clust = clst; /* Update current cluster */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2415 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2416 sect = clust2sect(fp->fs, fp->clust); /* Get current sector */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2417 if (!sect) ABORT(fp->fs, FR_INT_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2418 sect += csect;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2419 cc = btr / SS(fp->fs); /* When remaining bytes >= sector size, */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2420 if (cc) { /* Read maximum contiguous sectors directly */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2421 if (csect + cc > fp->fs->csize) /* Clip at cluster boundary */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2422 cc = fp->fs->csize - csect;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2423 if (disk_read(fp->fs->drv, rbuff, sect, (BYTE)cc) != RES_OK)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2424 ABORT(fp->fs, FR_DISK_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2425 #if !_FS_READONLY && _FS_MINIMIZE <= 2 /* Replace one of the read sectors with cached data if it contains a dirty sector */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2426 #if _FS_TINY
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2427 if (fp->fs->wflag && fp->fs->winsect - sect < cc)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2428 mem_cpy(rbuff + ((fp->fs->winsect - sect) * SS(fp->fs)), fp->fs->win, SS(fp->fs));
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2429 #else
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2430 if ((fp->flag & FA__DIRTY) && fp->dsect - sect < cc)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2431 mem_cpy(rbuff + ((fp->dsect - sect) * SS(fp->fs)), fp->buf, SS(fp->fs));
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2432 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2433 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2434 rcnt = SS(fp->fs) * cc; /* Number of bytes transferred */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2435 continue;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2436 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2437 #if !_FS_TINY
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2438 if (fp->dsect != sect) { /* Load data sector if not in cache */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2439 #if !_FS_READONLY
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2440 if (fp->flag & FA__DIRTY) { /* Write-back dirty sector cache */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2441 if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2442 ABORT(fp->fs, FR_DISK_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2443 fp->flag &= ~FA__DIRTY;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2444 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2445 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2446 if (disk_read(fp->fs->drv, fp->buf, sect, 1) != RES_OK) /* Fill sector cache */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2447 ABORT(fp->fs, FR_DISK_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2448 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2449 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2450 fp->dsect = sect;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2451 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2452 rcnt = SS(fp->fs) - (fp->fptr % SS(fp->fs)); /* Get partial sector data from sector buffer */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2453 if (rcnt > btr) rcnt = btr;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2454 #if _FS_TINY
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2455 if (move_window(fp->fs, fp->dsect)) /* Move sector window */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2456 ABORT(fp->fs, FR_DISK_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2457 mem_cpy(rbuff, &fp->fs->win[fp->fptr % SS(fp->fs)], rcnt); /* Pick partial sector */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2458 #else
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2459 mem_cpy(rbuff, &fp->buf[fp->fptr % SS(fp->fs)], rcnt); /* Pick partial sector */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2460 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2461 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2462
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2463 LEAVE_FF(fp->fs, FR_OK);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2464 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2465
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2466
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2467
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2468
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2469 #if !_FS_READONLY
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2470 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2471 /* Write File */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2472 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2473
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2474 FRESULT f_write (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2475 FIL *fp, /* Pointer to the file object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2476 const void *buff, /* Pointer to the data to be written */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2477 UINT btw, /* Number of bytes to write */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2478 UINT *bw /* Pointer to number of bytes written */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2479 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2480 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2481 FRESULT res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2482 DWORD clst, sect;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2483 UINT wcnt, cc;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2484 const BYTE *wbuff = buff;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2485 BYTE csect;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2486
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2487
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2488 *bw = 0; /* Initialize byte counter */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2489
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2490 res = validate(fp->fs, fp->id); /* Check validity */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2491 if (res != FR_OK) LEAVE_FF(fp->fs, res);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2492 if (fp->flag & FA__ERROR) /* Aborted file? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2493 LEAVE_FF(fp->fs, FR_INT_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2494 if (!(fp->flag & FA_WRITE)) /* Check access mode */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2495 LEAVE_FF(fp->fs, FR_DENIED);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2496 if ((DWORD)(fp->fsize + btw) < fp->fsize) btw = 0; /* File size cannot reach 4GB */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2497
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2498 for ( ; btw; /* Repeat until all data written */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2499 wbuff += wcnt, fp->fptr += wcnt, *bw += wcnt, btw -= wcnt) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2500 if ((fp->fptr % SS(fp->fs)) == 0) { /* On the sector boundary? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2501 csect = (BYTE)(fp->fptr / SS(fp->fs) & (fp->fs->csize - 1)); /* Sector offset in the cluster */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2502 if (!csect) { /* On the cluster boundary? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2503 if (fp->fptr == 0) { /* On the top of the file? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2504 clst = fp->sclust; /* Follow from the origin */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2505 if (clst == 0) /* When no cluster is allocated, */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2506 fp->sclust = clst = create_chain(fp->fs, 0); /* Create a new cluster chain */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2507 } else { /* Middle or end of the file */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2508 #if _USE_FASTSEEK
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2509 if (fp->cltbl)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2510 clst = clmt_clust(fp, fp->fptr); /* Get cluster# from the CLMT */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2511 else
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2512 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2513 clst = create_chain(fp->fs, fp->clust); /* Follow or stretch cluster chain on the FAT */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2514 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2515 if (clst == 0) break; /* Could not allocate a new cluster (disk full) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2516 if (clst == 1) ABORT(fp->fs, FR_INT_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2517 if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2518 fp->clust = clst; /* Update current cluster */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2519 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2520 #if _FS_TINY
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2521 if (fp->fs->winsect == fp->dsect && move_window(fp->fs, 0)) /* Write-back sector cache */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2522 ABORT(fp->fs, FR_DISK_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2523 #else
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2524 if (fp->flag & FA__DIRTY) { /* Write-back sector cache */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2525 if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2526 ABORT(fp->fs, FR_DISK_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2527 fp->flag &= ~FA__DIRTY;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2528 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2529 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2530 sect = clust2sect(fp->fs, fp->clust); /* Get current sector */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2531 if (!sect) ABORT(fp->fs, FR_INT_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2532 sect += csect;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2533 cc = btw / SS(fp->fs); /* When remaining bytes >= sector size, */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2534 if (cc) { /* Write maximum contiguous sectors directly */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2535 if (csect + cc > fp->fs->csize) /* Clip at cluster boundary */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2536 cc = fp->fs->csize - csect;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2537 if (disk_write(fp->fs->drv, wbuff, sect, (BYTE)cc) != RES_OK)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2538 ABORT(fp->fs, FR_DISK_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2539 #if _FS_TINY
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2540 if (fp->fs->winsect - sect < cc) { /* Refill sector cache if it gets invalidated by the direct write */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2541 mem_cpy(fp->fs->win, wbuff + ((fp->fs->winsect - sect) * SS(fp->fs)), SS(fp->fs));
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2542 fp->fs->wflag = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2543 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2544 #else
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2545 if (fp->dsect - sect < cc) { /* Refill sector cache if it gets invalidated by the direct write */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2546 mem_cpy(fp->buf, wbuff + ((fp->dsect - sect) * SS(fp->fs)), SS(fp->fs));
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2547 fp->flag &= ~FA__DIRTY;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2548 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2549 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2550 wcnt = SS(fp->fs) * cc; /* Number of bytes transferred */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2551 continue;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2552 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2553 #if _FS_TINY
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2554 if (fp->fptr >= fp->fsize) { /* Avoid silly cache filling at growing edge */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2555 if (move_window(fp->fs, 0)) ABORT(fp->fs, FR_DISK_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2556 fp->fs->winsect = sect;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2557 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2558 #else
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2559 if (fp->dsect != sect) { /* Fill sector cache with file data */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2560 if (fp->fptr < fp->fsize &&
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2561 disk_read(fp->fs->drv, fp->buf, sect, 1) != RES_OK)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2562 ABORT(fp->fs, FR_DISK_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2563 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2564 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2565 fp->dsect = sect;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2566 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2567 wcnt = SS(fp->fs) - (fp->fptr % SS(fp->fs));/* Put partial sector into file I/O buffer */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2568 if (wcnt > btw) wcnt = btw;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2569 #if _FS_TINY
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2570 if (move_window(fp->fs, fp->dsect)) /* Move sector window */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2571 ABORT(fp->fs, FR_DISK_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2572 mem_cpy(&fp->fs->win[fp->fptr % SS(fp->fs)], wbuff, wcnt); /* Fit partial sector */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2573 fp->fs->wflag = 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2574 #else
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2575 mem_cpy(&fp->buf[fp->fptr % SS(fp->fs)], wbuff, wcnt); /* Fit partial sector */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2576 fp->flag |= FA__DIRTY;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2577 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2578 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2579
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2580 if (fp->fptr > fp->fsize) fp->fsize = fp->fptr; /* Update file size if needed */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2581 fp->flag |= FA__WRITTEN; /* Set file change flag */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2582
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2583 LEAVE_FF(fp->fs, FR_OK);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2584 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2585
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2586
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2587
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2588
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2589 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2590 /* Synchronize the File Object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2591 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2592
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2593 FRESULT f_sync (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2594 FIL *fp /* Pointer to the file object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2595 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2596 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2597 FRESULT res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2598 DWORD tim;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2599 BYTE *dir;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2600
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2601
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2602 res = validate(fp->fs, fp->id); /* Check validity of the object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2603 if (res == FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2604 if (fp->flag & FA__WRITTEN) { /* Has the file been written? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2605 #if !_FS_TINY /* Write-back dirty buffer */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2606 if (fp->flag & FA__DIRTY) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2607 if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2608 LEAVE_FF(fp->fs, FR_DISK_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2609 fp->flag &= ~FA__DIRTY;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2610 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2611 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2612 /* Update the directory entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2613 res = move_window(fp->fs, fp->dir_sect);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2614 if (res == FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2615 dir = fp->dir_ptr;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2616 dir[DIR_Attr] |= AM_ARC; /* Set archive bit */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2617 ST_DWORD(dir+DIR_FileSize, fp->fsize); /* Update file size */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2618 ST_CLUST(dir, fp->sclust); /* Update start cluster */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2619 tim = get_fattime(); /* Update updated time */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2620 ST_DWORD(dir+DIR_WrtTime, tim);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2621 fp->flag &= ~FA__WRITTEN;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2622 fp->fs->wflag = 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2623 res = sync(fp->fs);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2624 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2625 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2626 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2627
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2628 LEAVE_FF(fp->fs, res);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2629 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2630
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2631 #endif /* !_FS_READONLY */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2632
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2633
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2634
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2635
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2636 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2637 /* Close File */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2638 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2639
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2640 FRESULT f_close (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2641 FIL *fp /* Pointer to the file object to be closed */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2642 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2643 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2644 FRESULT res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2645
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2646 #if _FS_READONLY
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2647 FATFS *fs = fp->fs;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2648 res = validate(fs, fp->id);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2649 if (res == FR_OK) fp->fs = 0; /* Discard file object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2650 LEAVE_FF(fs, res);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2651
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2652 #else
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2653 res = f_sync(fp); /* Flush cached data */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2654 #if _FS_SHARE
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2655 if (res == FR_OK) { /* Decrement open counter */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2656 #if _FS_REENTRANT
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2657 res = validate(fp->fs, fp->id);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2658 if (res == FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2659 res = dec_lock(fp->lockid);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2660 unlock_fs(fp->fs, FR_OK);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2661 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2662 #else
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2663 res = dec_lock(fp->lockid);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2664 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2665 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2666 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2667 if (res == FR_OK) fp->fs = 0; /* Discard file object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2668 return res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2669 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2670 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2671
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2672
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2673
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2674
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2675 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2676 /* Current Drive/Directory Handlings */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2677 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2678
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2679 #if _FS_RPATH >= 1
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2680
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2681 FRESULT f_chdrive (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2682 BYTE drv /* Drive number */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2683 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2684 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2685 if (drv >= _VOLUMES) return FR_INVALID_DRIVE;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2686
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2687 CurrVol = drv;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2688
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2689 return FR_OK;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2690 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2691
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2692
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2693
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2694 FRESULT f_chdir (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2695 const TCHAR *path /* Pointer to the directory path */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2696 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2697 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2698 FRESULT res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2699 DIR dj;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2700 DEF_NAMEBUF;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2701
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2702
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2703 res = chk_mounted(&path, &dj.fs, 0);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2704 if (res == FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2705 INIT_BUF(dj);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2706 res = follow_path(&dj, path); /* Follow the path */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2707 FREE_BUF();
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2708 if (res == FR_OK) { /* Follow completed */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2709 if (!dj.dir) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2710 dj.fs->cdir = dj.sclust; /* Start directory itself */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2711 } else {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2712 if (dj.dir[DIR_Attr] & AM_DIR) /* Reached to the directory */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2713 dj.fs->cdir = LD_CLUST(dj.dir);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2714 else
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2715 res = FR_NO_PATH; /* Reached but a file */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2716 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2717 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2718 if (res == FR_NO_FILE) res = FR_NO_PATH;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2719 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2720
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2721 LEAVE_FF(dj.fs, res);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2722 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2723
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2724
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2725 #if _FS_RPATH >= 2
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2726 FRESULT f_getcwd (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2727 TCHAR *path, /* Pointer to the directory path */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2728 UINT sz_path /* Size of path */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2729 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2730 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2731 FRESULT res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2732 DIR dj;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2733 UINT i, n;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2734 DWORD ccl;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2735 TCHAR *tp;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2736 FILINFO fno;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2737 DEF_NAMEBUF;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2738
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2739
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2740 *path = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2741 res = chk_mounted((const TCHAR**)&path, &dj.fs, 0); /* Get current volume */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2742 if (res == FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2743 INIT_BUF(dj);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2744 i = sz_path; /* Bottom of buffer (dir stack base) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2745 dj.sclust = dj.fs->cdir; /* Start to follow upper dir from current dir */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2746 while ((ccl = dj.sclust) != 0) { /* Repeat while current dir is a sub-dir */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2747 res = dir_sdi(&dj, 1); /* Get parent dir */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2748 if (res != FR_OK) break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2749 res = dir_read(&dj);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2750 if (res != FR_OK) break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2751 dj.sclust = LD_CLUST(dj.dir); /* Goto parent dir */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2752 res = dir_sdi(&dj, 0);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2753 if (res != FR_OK) break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2754 do { /* Find the entry links to the child dir */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2755 res = dir_read(&dj);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2756 if (res != FR_OK) break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2757 if (ccl == LD_CLUST(dj.dir)) break; /* Found the entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2758 res = dir_next(&dj, 0);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2759 } while (res == FR_OK);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2760 if (res == FR_NO_FILE) res = FR_INT_ERR;/* It cannot be 'not found'. */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2761 if (res != FR_OK) break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2762 #if _USE_LFN
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2763 fno.lfname = path;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2764 fno.lfsize = i;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2765 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2766 get_fileinfo(&dj, &fno); /* Get the dir name and push it to the buffer */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2767 tp = fno.fname;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2768 if (_USE_LFN && *path) tp = path;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2769 for (n = 0; tp[n]; n++) ;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2770 if (i < n + 3) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2771 res = FR_NOT_ENOUGH_CORE; break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2772 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2773 while (n) path[--i] = tp[--n];
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2774 path[--i] = '/';
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2775 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2776 tp = path;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2777 if (res == FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2778 *tp++ = '0' + CurrVol; /* Put drive number */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2779 *tp++ = ':';
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2780 if (i == sz_path) { /* Root-dir */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2781 *tp++ = '/';
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2782 } else { /* Sub-dir */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2783 do /* Add stacked path str */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2784 *tp++ = path[i++];
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2785 while (i < sz_path);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2786 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2787 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2788 *tp = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2789 FREE_BUF();
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2790 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2791
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2792 LEAVE_FF(dj.fs, res);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2793 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2794 #endif /* _FS_RPATH >= 2 */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2795 #endif /* _FS_RPATH >= 1 */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2796
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2797
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2798
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2799 #if _FS_MINIMIZE <= 2
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2800 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2801 /* Seek File R/W Pointer */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2802 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2803
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2804 FRESULT f_lseek (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2805 FIL *fp, /* Pointer to the file object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2806 DWORD ofs /* File pointer from top of file */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2807 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2808 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2809 FRESULT res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2810
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2811
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2812 res = validate(fp->fs, fp->id); /* Check validity of the object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2813 if (res != FR_OK) LEAVE_FF(fp->fs, res);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2814 if (fp->flag & FA__ERROR) /* Check abort flag */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2815 LEAVE_FF(fp->fs, FR_INT_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2816
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2817 #if _USE_FASTSEEK
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2818 if (fp->cltbl) { /* Fast seek */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2819 DWORD cl, pcl, ncl, tcl, dsc, tlen, ulen, *tbl;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2820
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2821 if (ofs == CREATE_LINKMAP) { /* Create CLMT */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2822 tbl = fp->cltbl;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2823 tlen = *tbl++; ulen = 2; /* Given table size and required table size */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2824 cl = fp->sclust; /* Top of the chain */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2825 if (cl) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2826 do {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2827 /* Get a fragment */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2828 tcl = cl; ncl = 0; ulen += 2; /* Top, length and used items */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2829 do {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2830 pcl = cl; ncl++;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2831 cl = get_fat(fp->fs, cl);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2832 if (cl <= 1) ABORT(fp->fs, FR_INT_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2833 if (cl == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2834 } while (cl == pcl + 1);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2835 if (ulen <= tlen) { /* Store the length and top of the fragment */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2836 *tbl++ = ncl; *tbl++ = tcl;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2837 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2838 } while (cl < fp->fs->n_fatent); /* Repeat until end of chain */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2839 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2840 *fp->cltbl = ulen; /* Number of items used */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2841 if (ulen <= tlen)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2842 *tbl = 0; /* Terminate table */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2843 else
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2844 res = FR_NOT_ENOUGH_CORE; /* Given table size is smaller than required */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2845
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2846 } else { /* Fast seek */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2847 if (ofs > fp->fsize) /* Clip offset at the file size */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2848 ofs = fp->fsize;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2849 fp->fptr = ofs; /* Set file pointer */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2850 if (ofs) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2851 fp->clust = clmt_clust(fp, ofs - 1);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2852 dsc = clust2sect(fp->fs, fp->clust);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2853 if (!dsc) ABORT(fp->fs, FR_INT_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2854 dsc += (ofs - 1) / SS(fp->fs) & (fp->fs->csize - 1);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2855 if (fp->fptr % SS(fp->fs) && dsc != fp->dsect) { /* Refill sector cache if needed */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2856 #if !_FS_TINY
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2857 #if !_FS_READONLY
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2858 if (fp->flag & FA__DIRTY) { /* Write-back dirty sector cache */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2859 if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2860 ABORT(fp->fs, FR_DISK_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2861 fp->flag &= ~FA__DIRTY;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2862 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2863 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2864 if (disk_read(fp->fs->drv, fp->buf, dsc, 1) != RES_OK) /* Load current sector */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2865 ABORT(fp->fs, FR_DISK_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2866 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2867 fp->dsect = dsc;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2868 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2869 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2870 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2871 } else
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2872 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2873
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2874 /* Normal Seek */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2875 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2876 DWORD clst, bcs, nsect, ifptr;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2877
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2878 if (ofs > fp->fsize /* In read-only mode, clip offset with the file size */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2879 #if !_FS_READONLY
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2880 && !(fp->flag & FA_WRITE)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2881 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2882 ) ofs = fp->fsize;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2883
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2884 ifptr = fp->fptr;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2885 fp->fptr = nsect = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2886 if (ofs) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2887 bcs = (DWORD)fp->fs->csize * SS(fp->fs); /* Cluster size (byte) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2888 if (ifptr > 0 &&
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2889 (ofs - 1) / bcs >= (ifptr - 1) / bcs) { /* When seek to same or following cluster, */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2890 fp->fptr = (ifptr - 1) & ~(bcs - 1); /* start from the current cluster */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2891 ofs -= fp->fptr;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2892 clst = fp->clust;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2893 } else { /* When seek to back cluster, */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2894 clst = fp->sclust; /* start from the first cluster */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2895 #if !_FS_READONLY
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2896 if (clst == 0) { /* If no cluster chain, create a new chain */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2897 clst = create_chain(fp->fs, 0);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2898 if (clst == 1) ABORT(fp->fs, FR_INT_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2899 if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2900 fp->sclust = clst;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2901 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2902 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2903 fp->clust = clst;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2904 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2905 if (clst != 0) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2906 while (ofs > bcs) { /* Cluster following loop */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2907 #if !_FS_READONLY
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2908 if (fp->flag & FA_WRITE) { /* Check if in write mode or not */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2909 clst = create_chain(fp->fs, clst); /* Force stretch if in write mode */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2910 if (clst == 0) { /* When disk gets full, clip file size */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2911 ofs = bcs; break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2912 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2913 } else
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2914 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2915 clst = get_fat(fp->fs, clst); /* Follow cluster chain if not in write mode */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2916 if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2917 if (clst <= 1 || clst >= fp->fs->n_fatent) ABORT(fp->fs, FR_INT_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2918 fp->clust = clst;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2919 fp->fptr += bcs;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2920 ofs -= bcs;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2921 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2922 fp->fptr += ofs;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2923 if (ofs % SS(fp->fs)) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2924 nsect = clust2sect(fp->fs, clst); /* Current sector */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2925 if (!nsect) ABORT(fp->fs, FR_INT_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2926 nsect += ofs / SS(fp->fs);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2927 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2928 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2929 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2930 if (fp->fptr % SS(fp->fs) && nsect != fp->dsect) { /* Fill sector cache if needed */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2931 #if !_FS_TINY
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2932 #if !_FS_READONLY
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2933 if (fp->flag & FA__DIRTY) { /* Write-back dirty sector cache */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2934 if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2935 ABORT(fp->fs, FR_DISK_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2936 fp->flag &= ~FA__DIRTY;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2937 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2938 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2939 if (disk_read(fp->fs->drv, fp->buf, nsect, 1) != RES_OK) /* Fill sector cache */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2940 ABORT(fp->fs, FR_DISK_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2941 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2942 fp->dsect = nsect;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2943 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2944 #if !_FS_READONLY
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2945 if (fp->fptr > fp->fsize) { /* Set file change flag if the file size is extended */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2946 fp->fsize = fp->fptr;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2947 fp->flag |= FA__WRITTEN;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2948 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2949 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2950 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2951
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2952 LEAVE_FF(fp->fs, res);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2953 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2954
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2955
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2956
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2957 #if _FS_MINIMIZE <= 1
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2958 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2959 /* Create a Directroy Object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2960 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2961
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2962 FRESULT f_opendir (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2963 DIR *dj, /* Pointer to directory object to create */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2964 const TCHAR *path /* Pointer to the directory path */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2965 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2966 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2967 FRESULT res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2968 DEF_NAMEBUF;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2969
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2970
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2971 res = chk_mounted(&path, &dj->fs, 0);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2972 if (res == FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2973 INIT_BUF(*dj);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2974 res = follow_path(dj, path); /* Follow the path to the directory */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2975 FREE_BUF();
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2976 if (res == FR_OK) { /* Follow completed */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2977 if (dj->dir) { /* It is not the root dir */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2978 if (dj->dir[DIR_Attr] & AM_DIR) { /* The object is a directory */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2979 dj->sclust = LD_CLUST(dj->dir);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2980 } else { /* The object is not a directory */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2981 res = FR_NO_PATH;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2982 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2983 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2984 if (res == FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2985 dj->id = dj->fs->id;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2986 res = dir_sdi(dj, 0); /* Rewind dir */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2987 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2988 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2989 if (res == FR_NO_FILE) res = FR_NO_PATH;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2990 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2991
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2992 LEAVE_FF(dj->fs, res);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2993 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2994
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2995
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2996
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2997
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2998 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2999 /* Read Directory Entry in Sequense */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3000 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3001
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3002 FRESULT f_readdir (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3003 DIR *dj, /* Pointer to the open directory object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3004 FILINFO *fno /* Pointer to file information to return */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3005 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3006 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3007 FRESULT res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3008 DEF_NAMEBUF;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3009
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3010
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3011 res = validate(dj->fs, dj->id); /* Check validity of the object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3012 if (res == FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3013 if (!fno) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3014 res = dir_sdi(dj, 0); /* Rewind the directory object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3015 } else {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3016 INIT_BUF(*dj);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3017 res = dir_read(dj); /* Read an directory item */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3018 if (res == FR_NO_FILE) { /* Reached end of dir */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3019 dj->sect = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3020 res = FR_OK;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3021 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3022 if (res == FR_OK) { /* A valid entry is found */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3023 get_fileinfo(dj, fno); /* Get the object information */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3024 res = dir_next(dj, 0); /* Increment index for next */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3025 if (res == FR_NO_FILE) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3026 dj->sect = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3027 res = FR_OK;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3028 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3029 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3030 FREE_BUF();
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3031 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3032 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3033
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3034 LEAVE_FF(dj->fs, res);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3035 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3036
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3037
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3038
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3039 #if _FS_MINIMIZE == 0
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3040 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3041 /* Get File Status */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3042 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3043
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3044 FRESULT f_stat (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3045 const TCHAR *path, /* Pointer to the file path */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3046 FILINFO *fno /* Pointer to file information to return */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3047 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3048 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3049 FRESULT res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3050 DIR dj;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3051 DEF_NAMEBUF;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3052
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3053
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3054 res = chk_mounted(&path, &dj.fs, 0);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3055 if (res == FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3056 INIT_BUF(dj);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3057 res = follow_path(&dj, path); /* Follow the file path */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3058 if (res == FR_OK) { /* Follow completed */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3059 if (dj.dir) /* Found an object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3060 get_fileinfo(&dj, fno);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3061 else /* It is root dir */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3062 res = FR_INVALID_NAME;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3063 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3064 FREE_BUF();
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3065 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3066
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3067 LEAVE_FF(dj.fs, res);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3068 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3069
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3070
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3071
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3072 #if !_FS_READONLY
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3073 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3074 /* Get Number of Free Clusters */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3075 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3076
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3077 FRESULT f_getfree (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3078 const TCHAR *path, /* Pointer to the logical drive number (root dir) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3079 DWORD *nclst, /* Pointer to the variable to return number of free clusters */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3080 FATFS **fatfs /* Pointer to pointer to corresponding file system object to return */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3081 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3082 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3083 FRESULT res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3084 DWORD n, clst, sect, stat;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3085 UINT i;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3086 BYTE fat, *p;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3087
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3088
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3089 /* Get drive number */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3090 res = chk_mounted(&path, fatfs, 0);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3091 if (res == FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3092 /* If free_clust is valid, return it without full cluster scan */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3093 if ((*fatfs)->free_clust <= (*fatfs)->n_fatent - 2) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3094 *nclst = (*fatfs)->free_clust;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3095 } else {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3096 /* Get number of free clusters */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3097 fat = (*fatfs)->fs_type;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3098 n = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3099 if (fat == FS_FAT12) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3100 clst = 2;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3101 do {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3102 stat = get_fat(*fatfs, clst);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3103 if (stat == 0xFFFFFFFF) { res = FR_DISK_ERR; break; }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3104 if (stat == 1) { res = FR_INT_ERR; break; }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3105 if (stat == 0) n++;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3106 } while (++clst < (*fatfs)->n_fatent);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3107 } else {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3108 clst = (*fatfs)->n_fatent;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3109 sect = (*fatfs)->fatbase;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3110 i = 0; p = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3111 do {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3112 if (!i) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3113 res = move_window(*fatfs, sect++);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3114 if (res != FR_OK) break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3115 p = (*fatfs)->win;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3116 i = SS(*fatfs);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3117 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3118 if (fat == FS_FAT16) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3119 if (LD_WORD(p) == 0) n++;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3120 p += 2; i -= 2;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3121 } else {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3122 if ((LD_DWORD(p) & 0x0FFFFFFF) == 0) n++;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3123 p += 4; i -= 4;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3124 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3125 } while (--clst);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3126 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3127 (*fatfs)->free_clust = n;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3128 if (fat == FS_FAT32) (*fatfs)->fsi_flag = 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3129 *nclst = n;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3130 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3131 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3132 LEAVE_FF(*fatfs, res);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3133 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3134
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3135
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3136
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3137
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3138 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3139 /* Truncate File */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3140 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3141
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3142 FRESULT f_truncate (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3143 FIL *fp /* Pointer to the file object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3144 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3145 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3146 FRESULT res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3147 DWORD ncl;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3148
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3149
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3150 res = validate(fp->fs, fp->id); /* Check validity of the object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3151 if (res == FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3152 if (fp->flag & FA__ERROR) { /* Check abort flag */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3153 res = FR_INT_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3154 } else {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3155 if (!(fp->flag & FA_WRITE)) /* Check access mode */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3156 res = FR_DENIED;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3157 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3158 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3159 if (res == FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3160 if (fp->fsize > fp->fptr) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3161 fp->fsize = fp->fptr; /* Set file size to current R/W point */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3162 fp->flag |= FA__WRITTEN;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3163 if (fp->fptr == 0) { /* When set file size to zero, remove entire cluster chain */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3164 res = remove_chain(fp->fs, fp->sclust);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3165 fp->sclust = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3166 } else { /* When truncate a part of the file, remove remaining clusters */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3167 ncl = get_fat(fp->fs, fp->clust);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3168 res = FR_OK;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3169 if (ncl == 0xFFFFFFFF) res = FR_DISK_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3170 if (ncl == 1) res = FR_INT_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3171 if (res == FR_OK && ncl < fp->fs->n_fatent) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3172 res = put_fat(fp->fs, fp->clust, 0x0FFFFFFF);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3173 if (res == FR_OK) res = remove_chain(fp->fs, ncl);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3174 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3175 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3176 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3177 if (res != FR_OK) fp->flag |= FA__ERROR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3178 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3179
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3180 LEAVE_FF(fp->fs, res);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3181 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3182
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3183
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3184
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3185
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3186 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3187 /* Delete a File or Directory */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3188 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3189
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3190 FRESULT f_unlink (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3191 const TCHAR *path /* Pointer to the file or directory path */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3192 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3193 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3194 FRESULT res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3195 DIR dj, sdj;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3196 BYTE *dir;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3197 DWORD dclst;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3198 DEF_NAMEBUF;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3199
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3200
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3201 res = chk_mounted(&path, &dj.fs, 1);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3202 if (res == FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3203 INIT_BUF(dj);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3204 res = follow_path(&dj, path); /* Follow the file path */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3205 if (_FS_RPATH && res == FR_OK && (dj.fn[NS] & NS_DOT))
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3206 res = FR_INVALID_NAME; /* Cannot remove dot entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3207 #if _FS_SHARE
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3208 if (res == FR_OK) res = chk_lock(&dj, 2); /* Cannot remove open file */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3209 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3210 if (res == FR_OK) { /* The object is accessible */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3211 dir = dj.dir;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3212 if (!dir) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3213 res = FR_INVALID_NAME; /* Cannot remove the start directory */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3214 } else {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3215 if (dir[DIR_Attr] & AM_RDO)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3216 res = FR_DENIED; /* Cannot remove R/O object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3217 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3218 dclst = LD_CLUST(dir);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3219 if (res == FR_OK && (dir[DIR_Attr] & AM_DIR)) { /* Is it a sub-dir? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3220 if (dclst < 2) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3221 res = FR_INT_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3222 } else {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3223 mem_cpy(&sdj, &dj, sizeof(DIR)); /* Check if the sub-dir is empty or not */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3224 sdj.sclust = dclst;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3225 res = dir_sdi(&sdj, 2); /* Exclude dot entries */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3226 if (res == FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3227 res = dir_read(&sdj);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3228 if (res == FR_OK /* Not empty dir */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3229 #if _FS_RPATH
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3230 || dclst == sdj.fs->cdir /* Current dir */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3231 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3232 ) res = FR_DENIED;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3233 if (res == FR_NO_FILE) res = FR_OK; /* Empty */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3234 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3235 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3236 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3237 if (res == FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3238 res = dir_remove(&dj); /* Remove the directory entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3239 if (res == FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3240 if (dclst) /* Remove the cluster chain if exist */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3241 res = remove_chain(dj.fs, dclst);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3242 if (res == FR_OK) res = sync(dj.fs);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3243 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3244 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3245 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3246 FREE_BUF();
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3247 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3248 LEAVE_FF(dj.fs, res);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3249 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3250
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3251
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3252
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3253
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3254 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3255 /* Create a Directory */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3256 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3257
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3258 FRESULT f_mkdir (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3259 const TCHAR *path /* Pointer to the directory path */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3260 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3261 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3262 FRESULT res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3263 DIR dj;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3264 BYTE *dir, n;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3265 DWORD dsc, dcl, pcl, tim = get_fattime();
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3266 DEF_NAMEBUF;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3267
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3268
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3269 res = chk_mounted(&path, &dj.fs, 1);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3270 if (res == FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3271 INIT_BUF(dj);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3272 res = follow_path(&dj, path); /* Follow the file path */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3273 if (res == FR_OK) res = FR_EXIST; /* Any object with same name is already existing */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3274 if (_FS_RPATH && res == FR_NO_FILE && (dj.fn[NS] & NS_DOT))
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3275 res = FR_INVALID_NAME;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3276 if (res == FR_NO_FILE) { /* Can create a new directory */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3277 dcl = create_chain(dj.fs, 0); /* Allocate a cluster for the new directory table */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3278 res = FR_OK;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3279 if (dcl == 0) res = FR_DENIED; /* No space to allocate a new cluster */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3280 if (dcl == 1) res = FR_INT_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3281 if (dcl == 0xFFFFFFFF) res = FR_DISK_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3282 if (res == FR_OK) /* Flush FAT */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3283 res = move_window(dj.fs, 0);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3284 if (res == FR_OK) { /* Initialize the new directory table */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3285 dsc = clust2sect(dj.fs, dcl);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3286 dir = dj.fs->win;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3287 mem_set(dir, 0, SS(dj.fs));
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3288 mem_set(dir+DIR_Name, ' ', 8+3); /* Create "." entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3289 dir[DIR_Name] = '.';
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3290 dir[DIR_Attr] = AM_DIR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3291 ST_DWORD(dir+DIR_WrtTime, tim);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3292 ST_CLUST(dir, dcl);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3293 mem_cpy(dir+SZ_DIR, dir, SZ_DIR); /* Create ".." entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3294 dir[33] = '.'; pcl = dj.sclust;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3295 if (dj.fs->fs_type == FS_FAT32 && pcl == dj.fs->dirbase)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3296 pcl = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3297 ST_CLUST(dir+SZ_DIR, pcl);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3298 for (n = dj.fs->csize; n; n--) { /* Write dot entries and clear following sectors */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3299 dj.fs->winsect = dsc++;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3300 dj.fs->wflag = 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3301 res = move_window(dj.fs, 0);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3302 if (res != FR_OK) break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3303 mem_set(dir, 0, SS(dj.fs));
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3304 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3305 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3306 if (res == FR_OK) res = dir_register(&dj); /* Register the object to the directoy */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3307 if (res != FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3308 remove_chain(dj.fs, dcl); /* Could not register, remove cluster chain */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3309 } else {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3310 dir = dj.dir;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3311 dir[DIR_Attr] = AM_DIR; /* Attribute */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3312 ST_DWORD(dir+DIR_WrtTime, tim); /* Created time */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3313 ST_CLUST(dir, dcl); /* Table start cluster */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3314 dj.fs->wflag = 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3315 res = sync(dj.fs);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3316 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3317 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3318 FREE_BUF();
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3319 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3320
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3321 LEAVE_FF(dj.fs, res);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3322 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3323
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3324
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3325
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3326
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3327 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3328 /* Change Attribute */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3329 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3330
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3331 FRESULT f_chmod (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3332 const TCHAR *path, /* Pointer to the file path */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3333 BYTE value, /* Attribute bits */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3334 BYTE mask /* Attribute mask to change */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3335 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3336 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3337 FRESULT res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3338 DIR dj;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3339 BYTE *dir;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3340 DEF_NAMEBUF;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3341
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3342
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3343 res = chk_mounted(&path, &dj.fs, 1);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3344 if (res == FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3345 INIT_BUF(dj);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3346 res = follow_path(&dj, path); /* Follow the file path */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3347 FREE_BUF();
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3348 if (_FS_RPATH && res == FR_OK && (dj.fn[NS] & NS_DOT))
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3349 res = FR_INVALID_NAME;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3350 if (res == FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3351 dir = dj.dir;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3352 if (!dir) { /* Is it a root directory? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3353 res = FR_INVALID_NAME;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3354 } else { /* File or sub directory */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3355 mask &= AM_RDO|AM_HID|AM_SYS|AM_ARC; /* Valid attribute mask */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3356 dir[DIR_Attr] = (value & mask) | (dir[DIR_Attr] & (BYTE)~mask); /* Apply attribute change */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3357 dj.fs->wflag = 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3358 res = sync(dj.fs);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3359 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3360 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3361 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3362
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3363 LEAVE_FF(dj.fs, res);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3364 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3365
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3366
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3367
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3368
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3369 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3370 /* Change Timestamp */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3371 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3372
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3373 FRESULT f_utime (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3374 const TCHAR *path, /* Pointer to the file/directory name */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3375 const FILINFO *fno /* Pointer to the time stamp to be set */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3376 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3377 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3378 FRESULT res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3379 DIR dj;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3380 BYTE *dir;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3381 DEF_NAMEBUF;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3382
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3383
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3384 res = chk_mounted(&path, &dj.fs, 1);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3385 if (res == FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3386 INIT_BUF(dj);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3387 res = follow_path(&dj, path); /* Follow the file path */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3388 FREE_BUF();
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3389 if (_FS_RPATH && res == FR_OK && (dj.fn[NS] & NS_DOT))
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3390 res = FR_INVALID_NAME;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3391 if (res == FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3392 dir = dj.dir;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3393 if (!dir) { /* Root directory */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3394 res = FR_INVALID_NAME;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3395 } else { /* File or sub-directory */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3396 ST_WORD(dir+DIR_WrtTime, fno->ftime);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3397 ST_WORD(dir+DIR_WrtDate, fno->fdate);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3398 dj.fs->wflag = 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3399 res = sync(dj.fs);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3400 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3401 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3402 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3403
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3404 LEAVE_FF(dj.fs, res);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3405 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3406
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3407
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3408
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3409
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3410 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3411 /* Rename File/Directory */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3412 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3413
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3414 FRESULT f_rename (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3415 const TCHAR *path_old, /* Pointer to the old name */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3416 const TCHAR *path_new /* Pointer to the new name */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3417 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3418 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3419 FRESULT res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3420 DIR djo, djn;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3421 BYTE buf[21], *dir;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3422 DWORD dw;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3423 DEF_NAMEBUF;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3424
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3425
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3426 res = chk_mounted(&path_old, &djo.fs, 1);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3427 if (res == FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3428 djn.fs = djo.fs;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3429 INIT_BUF(djo);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3430 res = follow_path(&djo, path_old); /* Check old object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3431 if (_FS_RPATH && res == FR_OK && (djo.fn[NS] & NS_DOT))
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3432 res = FR_INVALID_NAME;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3433 #if _FS_SHARE
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3434 if (res == FR_OK) res = chk_lock(&djo, 2);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3435 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3436 if (res == FR_OK) { /* Old object is found */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3437 if (!djo.dir) { /* Is root dir? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3438 res = FR_NO_FILE;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3439 } else {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3440 mem_cpy(buf, djo.dir+DIR_Attr, 21); /* Save the object information except for name */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3441 mem_cpy(&djn, &djo, sizeof(DIR)); /* Check new object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3442 res = follow_path(&djn, path_new);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3443 if (res == FR_OK) res = FR_EXIST; /* The new object name is already existing */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3444 if (res == FR_NO_FILE) { /* Is it a valid path and no name collision? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3445 /* Start critical section that any interruption or error can cause cross-link */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3446 res = dir_register(&djn); /* Register the new entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3447 if (res == FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3448 dir = djn.dir; /* Copy object information except for name */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3449 mem_cpy(dir+13, buf+2, 19);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3450 dir[DIR_Attr] = buf[0] | AM_ARC;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3451 djo.fs->wflag = 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3452 if (djo.sclust != djn.sclust && (dir[DIR_Attr] & AM_DIR)) { /* Update .. entry in the directory if needed */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3453 dw = clust2sect(djn.fs, LD_CLUST(dir));
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3454 if (!dw) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3455 res = FR_INT_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3456 } else {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3457 res = move_window(djn.fs, dw);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3458 dir = djn.fs->win+SZ_DIR; /* .. entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3459 if (res == FR_OK && dir[1] == '.') {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3460 dw = (djn.fs->fs_type == FS_FAT32 && djn.sclust == djn.fs->dirbase) ? 0 : djn.sclust;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3461 ST_CLUST(dir, dw);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3462 djn.fs->wflag = 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3463 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3464 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3465 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3466 if (res == FR_OK) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3467 res = dir_remove(&djo); /* Remove old entry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3468 if (res == FR_OK)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3469 res = sync(djo.fs);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3470 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3471 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3472 /* End critical section */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3473 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3474 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3475 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3476 FREE_BUF();
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3477 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3478 LEAVE_FF(djo.fs, res);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3479 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3480
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3481 #endif /* !_FS_READONLY */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3482 #endif /* _FS_MINIMIZE == 0 */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3483 #endif /* _FS_MINIMIZE <= 1 */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3484 #endif /* _FS_MINIMIZE <= 2 */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3485
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3486
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3487
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3488 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3489 /* Forward data to the stream directly (available on only tiny cfg) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3490 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3491 #if _USE_FORWARD && _FS_TINY
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3492
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3493 FRESULT f_forward (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3494 FIL *fp, /* Pointer to the file object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3495 UINT (*func)(const BYTE*,UINT), /* Pointer to the streaming function */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3496 UINT btr, /* Number of bytes to forward */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3497 UINT *bf /* Pointer to number of bytes forwarded */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3498 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3499 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3500 FRESULT res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3501 DWORD remain, clst, sect;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3502 UINT rcnt;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3503 BYTE csect;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3504
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3505
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3506 *bf = 0; /* Initialize byte counter */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3507
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3508 res = validate(fp->fs, fp->id); /* Check validity of the object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3509 if (res != FR_OK) LEAVE_FF(fp->fs, res);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3510 if (fp->flag & FA__ERROR) /* Check error flag */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3511 LEAVE_FF(fp->fs, FR_INT_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3512 if (!(fp->flag & FA_READ)) /* Check access mode */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3513 LEAVE_FF(fp->fs, FR_DENIED);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3514
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3515 remain = fp->fsize - fp->fptr;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3516 if (btr > remain) btr = (UINT)remain; /* Truncate btr by remaining bytes */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3517
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3518 for ( ; btr && (*func)(0, 0); /* Repeat until all data transferred or stream becomes busy */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3519 fp->fptr += rcnt, *bf += rcnt, btr -= rcnt) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3520 csect = (BYTE)(fp->fptr / SS(fp->fs) & (fp->fs->csize - 1)); /* Sector offset in the cluster */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3521 if ((fp->fptr % SS(fp->fs)) == 0) { /* On the sector boundary? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3522 if (!csect) { /* On the cluster boundary? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3523 clst = (fp->fptr == 0) ? /* On the top of the file? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3524 fp->sclust : get_fat(fp->fs, fp->clust);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3525 if (clst <= 1) ABORT(fp->fs, FR_INT_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3526 if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3527 fp->clust = clst; /* Update current cluster */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3528 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3529 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3530 sect = clust2sect(fp->fs, fp->clust); /* Get current data sector */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3531 if (!sect) ABORT(fp->fs, FR_INT_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3532 sect += csect;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3533 if (move_window(fp->fs, sect)) /* Move sector window */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3534 ABORT(fp->fs, FR_DISK_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3535 fp->dsect = sect;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3536 rcnt = SS(fp->fs) - (WORD)(fp->fptr % SS(fp->fs)); /* Forward data from sector window */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3537 if (rcnt > btr) rcnt = btr;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3538 rcnt = (*func)(&fp->fs->win[(WORD)fp->fptr % SS(fp->fs)], rcnt);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3539 if (!rcnt) ABORT(fp->fs, FR_INT_ERR);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3540 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3541
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3542 LEAVE_FF(fp->fs, FR_OK);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3543 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3544 #endif /* _USE_FORWARD */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3545
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3546
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3547
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3548 #if _USE_MKFS && !_FS_READONLY
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3549 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3550 /* Create File System on the Drive */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3551 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3552 #define N_ROOTDIR 512 /* Number of root dir entries for FAT12/16 */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3553 #define N_FATS 1 /* Number of FAT copies (1 or 2) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3554
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3555
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3556 FRESULT f_mkfs (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3557 BYTE drv, /* Logical drive number */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3558 BYTE sfd, /* Partitioning rule 0:FDISK, 1:SFD */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3559 UINT au /* Allocation unit size [bytes] */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3560 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3561 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3562 static const WORD vst[] = { 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 0};
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3563 static const WORD cst[] = {32768, 16384, 8192, 4096, 2048, 16384, 8192, 4096, 2048, 1024, 512};
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3564 BYTE fmt, md, sys, *tbl, pdrv, part;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3565 DWORD n_clst, vs, n, wsect;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3566 UINT i;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3567 DWORD b_vol, b_fat, b_dir, b_data; /* LBA */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3568 DWORD n_vol, n_rsv, n_fat, n_dir; /* Size */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3569 FATFS *fs;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3570 DSTATUS stat;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3571
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3572
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3573 /* Check mounted drive and clear work area */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3574 if (drv >= _VOLUMES) return FR_INVALID_DRIVE;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3575 if (sfd > 1) return FR_INVALID_PARAMETER;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3576 if (au & (au - 1)) return FR_INVALID_PARAMETER;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3577 fs = FatFs[drv];
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3578 if (!fs) return FR_NOT_ENABLED;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3579 fs->fs_type = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3580 pdrv = LD2PD(drv); /* Physical drive */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3581 part = LD2PT(drv); /* Partition (0:auto detect, 1-4:get from partition table)*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3582
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3583 /* Get disk statics */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3584 stat = disk_initialize(pdrv);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3585 if (stat & STA_NOINIT) return FR_NOT_READY;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3586 if (stat & STA_PROTECT) return FR_WRITE_PROTECTED;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3587 #if _MAX_SS != 512 /* Get disk sector size */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3588 if (disk_ioctl(pdrv, GET_SECTOR_SIZE, &SS(fs)) != RES_OK || SS(fs) > _MAX_SS)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3589 return FR_DISK_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3590 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3591 if (_MULTI_PARTITION && part) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3592 /* Get partition information from partition table in the MBR */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3593 if (disk_read(pdrv, fs->win, 0, 1) != RES_OK) return FR_DISK_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3594 if (LD_WORD(fs->win+BS_55AA) != 0xAA55) return FR_MKFS_ABORTED;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3595 tbl = &fs->win[MBR_Table + (part - 1) * SZ_PTE];
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3596 if (!tbl[4]) return FR_MKFS_ABORTED; /* No partition? */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3597 b_vol = LD_DWORD(tbl+8); /* Volume start sector */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3598 n_vol = LD_DWORD(tbl+12); /* Volume size */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3599 } else {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3600 /* Create a partition in this function */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3601 if (disk_ioctl(pdrv, GET_SECTOR_COUNT, &n_vol) != RES_OK || n_vol < 128)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3602 return FR_DISK_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3603 b_vol = (sfd) ? 0 : 63; /* Volume start sector */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3604 n_vol -= b_vol; /* Volume size */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3605 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3606
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3607 if (!au) { /* AU auto selection */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3608 vs = n_vol / (2000 / (SS(fs) / 512));
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3609 for (i = 0; vs < vst[i]; i++) ;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3610 au = cst[i];
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3611 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3612 au /= SS(fs); /* Number of sectors per cluster */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3613 if (au == 0) au = 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3614 if (au > 128) au = 128;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3615
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3616 /* Pre-compute number of clusters and FAT syb-type */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3617 n_clst = n_vol / au;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3618 fmt = FS_FAT12;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3619 if (n_clst >= MIN_FAT16) fmt = FS_FAT16;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3620 if (n_clst >= MIN_FAT32) fmt = FS_FAT32;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3621
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3622 /* Determine offset and size of FAT structure */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3623 if (fmt == FS_FAT32) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3624 n_fat = ((n_clst * 4) + 8 + SS(fs) - 1) / SS(fs);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3625 n_rsv = 32;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3626 n_dir = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3627 } else {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3628 n_fat = (fmt == FS_FAT12) ? (n_clst * 3 + 1) / 2 + 3 : (n_clst * 2) + 4;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3629 n_fat = (n_fat + SS(fs) - 1) / SS(fs);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3630 n_rsv = 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3631 n_dir = (DWORD)N_ROOTDIR * SZ_DIR / SS(fs);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3632 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3633 b_fat = b_vol + n_rsv; /* FAT area start sector */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3634 b_dir = b_fat + n_fat * N_FATS; /* Directory area start sector */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3635 b_data = b_dir + n_dir; /* Data area start sector */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3636 if (n_vol < b_data + au - b_vol) return FR_MKFS_ABORTED; /* Too small volume */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3637
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3638 /* Align data start sector to erase block boundary (for flash memory media) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3639 if (disk_ioctl(pdrv, GET_BLOCK_SIZE, &n) != RES_OK || !n || n > 32768) n = 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3640 n = (b_data + n - 1) & ~(n - 1); /* Next nearest erase block from current data start */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3641 n = (n - b_data) / N_FATS;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3642 if (fmt == FS_FAT32) { /* FAT32: Move FAT offset */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3643 n_rsv += n;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3644 b_fat += n;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3645 } else { /* FAT12/16: Expand FAT size */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3646 n_fat += n;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3647 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3648
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3649 /* Determine number of clusters and final check of validity of the FAT sub-type */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3650 n_clst = (n_vol - n_rsv - n_fat * N_FATS - n_dir) / au;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3651 if ( (fmt == FS_FAT16 && n_clst < MIN_FAT16)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3652 || (fmt == FS_FAT32 && n_clst < MIN_FAT32))
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3653 return FR_MKFS_ABORTED;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3654
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3655 switch (fmt) { /* Determine system ID for partition table */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3656 case FS_FAT12: sys = 0x01; break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3657 case FS_FAT16: sys = (n_vol < 0x10000) ? 0x04 : 0x06; break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3658 default: sys = 0x0C;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3659 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3660
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3661 if (_MULTI_PARTITION && part) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3662 /* Update system ID in the partition table */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3663 tbl = &fs->win[MBR_Table + (part - 1) * SZ_PTE];
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3664 tbl[4] = sys;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3665 if (disk_write(pdrv, fs->win, 0, 1) != RES_OK) return FR_DISK_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3666 md = 0xF8;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3667 } else {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3668 if (sfd) { /* No patition table (SFD) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3669 md = 0xF0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3670 } else { /* Create partition table (FDISK) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3671 mem_set(fs->win, 0, SS(fs));
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3672 tbl = fs->win+MBR_Table; /* Create partiton table for single partition in the drive */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3673 tbl[1] = 1; /* Partition start head */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3674 tbl[2] = 1; /* Partition start sector */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3675 tbl[3] = 0; /* Partition start cylinder */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3676 tbl[4] = sys; /* System type */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3677 tbl[5] = 254; /* Partition end head */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3678 n = (b_vol + n_vol) / 63 / 255;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3679 tbl[6] = (BYTE)((n >> 2) | 63); /* Partiiton end sector */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3680 tbl[7] = (BYTE)n; /* End cylinder */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3681 ST_DWORD(tbl+8, 63); /* Partition start in LBA */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3682 ST_DWORD(tbl+12, n_vol); /* Partition size in LBA */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3683 ST_WORD(fs->win+BS_55AA, 0xAA55); /* MBR signature */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3684 if (disk_write(pdrv, fs->win, 0, 1) != RES_OK) /* Write it to the MBR sector */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3685 return FR_DISK_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3686 md = 0xF8;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3687 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3688 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3689
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3690 /* Create BPB in the VBR */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3691 tbl = fs->win; /* Clear sector */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3692 mem_set(tbl, 0, SS(fs));
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3693 mem_cpy(tbl, "\xEB\xFE\x90" "MSDOS5.0", 11);/* Boot jump code, OEM name */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3694 i = SS(fs); /* Sector size */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3695 ST_WORD(tbl+BPB_BytsPerSec, i);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3696 tbl[BPB_SecPerClus] = (BYTE)au; /* Sectors per cluster */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3697 ST_WORD(tbl+BPB_RsvdSecCnt, n_rsv); /* Reserved sectors */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3698 tbl[BPB_NumFATs] = N_FATS; /* Number of FATs */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3699 i = (fmt == FS_FAT32) ? 0 : N_ROOTDIR; /* Number of rootdir entries */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3700 ST_WORD(tbl+BPB_RootEntCnt, i);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3701 if (n_vol < 0x10000) { /* Number of total sectors */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3702 ST_WORD(tbl+BPB_TotSec16, n_vol);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3703 } else {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3704 ST_DWORD(tbl+BPB_TotSec32, n_vol);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3705 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3706 tbl[BPB_Media] = md; /* Media descriptor */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3707 ST_WORD(tbl+BPB_SecPerTrk, 63); /* Number of sectors per track */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3708 ST_WORD(tbl+BPB_NumHeads, 255); /* Number of heads */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3709 ST_DWORD(tbl+BPB_HiddSec, b_vol); /* Hidden sectors */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3710 n = get_fattime(); /* Use current time as VSN */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3711 if (fmt == FS_FAT32) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3712 ST_DWORD(tbl+BS_VolID32, n); /* VSN */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3713 ST_DWORD(tbl+BPB_FATSz32, n_fat); /* Number of sectors per FAT */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3714 ST_DWORD(tbl+BPB_RootClus, 2); /* Root directory start cluster (2) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3715 ST_WORD(tbl+BPB_FSInfo, 1); /* FSInfo record offset (VBR+1) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3716 ST_WORD(tbl+BPB_BkBootSec, 6); /* Backup boot record offset (VBR+6) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3717 tbl[BS_DrvNum32] = 0x80; /* Drive number */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3718 tbl[BS_BootSig32] = 0x29; /* Extended boot signature */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3719 mem_cpy(tbl+BS_VolLab32, "NO NAME " "FAT32 ", 19); /* Volume label, FAT signature */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3720 } else {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3721 ST_DWORD(tbl+BS_VolID, n); /* VSN */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3722 ST_WORD(tbl+BPB_FATSz16, n_fat); /* Number of sectors per FAT */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3723 tbl[BS_DrvNum] = 0x80; /* Drive number */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3724 tbl[BS_BootSig] = 0x29; /* Extended boot signature */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3725 mem_cpy(tbl+BS_VolLab, "NO NAME " "FAT ", 19); /* Volume label, FAT signature */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3726 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3727 ST_WORD(tbl+BS_55AA, 0xAA55); /* Signature (Offset is fixed here regardless of sector size) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3728 if (disk_write(pdrv, tbl, b_vol, 1) != RES_OK) /* Write it to the VBR sector */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3729 return FR_DISK_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3730 if (fmt == FS_FAT32) /* Write backup VBR if needed (VBR+6) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3731 disk_write(pdrv, tbl, b_vol + 6, 1);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3732
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3733 /* Initialize FAT area */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3734 wsect = b_fat;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3735 for (i = 0; i < N_FATS; i++) { /* Initialize each FAT copy */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3736 mem_set(tbl, 0, SS(fs)); /* 1st sector of the FAT */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3737 n = md; /* Media descriptor byte */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3738 if (fmt != FS_FAT32) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3739 n |= (fmt == FS_FAT12) ? 0x00FFFF00 : 0xFFFFFF00;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3740 ST_DWORD(tbl+0, n); /* Reserve cluster #0-1 (FAT12/16) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3741 } else {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3742 n |= 0xFFFFFF00;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3743 ST_DWORD(tbl+0, n); /* Reserve cluster #0-1 (FAT32) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3744 ST_DWORD(tbl+4, 0xFFFFFFFF);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3745 ST_DWORD(tbl+8, 0x0FFFFFFF); /* Reserve cluster #2 for root dir */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3746 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3747 if (disk_write(pdrv, tbl, wsect++, 1) != RES_OK)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3748 return FR_DISK_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3749 mem_set(tbl, 0, SS(fs)); /* Fill following FAT entries with zero */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3750 for (n = 1; n < n_fat; n++) { /* This loop may take a time on FAT32 volume due to many single sector writes */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3751 if (disk_write(pdrv, tbl, wsect++, 1) != RES_OK)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3752 return FR_DISK_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3753 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3754 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3755
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3756 /* Initialize root directory */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3757 i = (fmt == FS_FAT32) ? au : n_dir;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3758 do {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3759 if (disk_write(pdrv, tbl, wsect++, 1) != RES_OK)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3760 return FR_DISK_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3761 } while (--i);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3762
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3763 #if _USE_ERASE /* Erase data area if needed */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3764 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3765 DWORD eb[2];
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3766
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3767 eb[0] = wsect; eb[1] = wsect + (n_clst - ((fmt == FS_FAT32) ? 1 : 0)) * au - 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3768 disk_ioctl(pdrv, CTRL_ERASE_SECTOR, eb);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3769 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3770 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3771
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3772 /* Create FSInfo if needed */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3773 if (fmt == FS_FAT32) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3774 ST_DWORD(tbl+FSI_LeadSig, 0x41615252);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3775 ST_DWORD(tbl+FSI_StrucSig, 0x61417272);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3776 ST_DWORD(tbl+FSI_Free_Count, n_clst - 1); /* Number of free clusters */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3777 ST_DWORD(tbl+FSI_Nxt_Free, 2); /* Last allocated cluster# */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3778 ST_WORD(tbl+BS_55AA, 0xAA55);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3779 disk_write(pdrv, tbl, b_vol + 1, 1); /* Write original (VBR+1) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3780 disk_write(pdrv, tbl, b_vol + 7, 1); /* Write backup (VBR+7) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3781 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3782
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3783 return (disk_ioctl(pdrv, CTRL_SYNC, 0) == RES_OK) ? FR_OK : FR_DISK_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3784 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3785
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3786
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3787 #if _MULTI_PARTITION == 2
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3788 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3789 /* Divide Physical Drive */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3790 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3791
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3792 FRESULT f_fdisk (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3793 BYTE pdrv, /* Physical drive number */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3794 const DWORD szt[], /* Pointer to the size table for each partitions */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3795 void* work /* Pointer to the working buffer */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3796 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3797 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3798 UINT i, n, sz_cyl, tot_cyl, b_cyl, e_cyl, p_cyl;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3799 BYTE s_hd, e_hd, *p, *buf = (BYTE*)work;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3800 DSTATUS stat;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3801 DWORD sz_disk, sz_part, s_part;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3802
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3803
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3804 stat = disk_initialize(pdrv);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3805 if (stat & STA_NOINIT) return FR_NOT_READY;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3806 if (stat & STA_PROTECT) return FR_WRITE_PROTECTED;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3807 if (disk_ioctl(pdrv, GET_SECTOR_COUNT, &sz_disk)) return FR_DISK_ERR;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3808
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3809 /* Determine CHS in the table regardless of the drive geometry */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3810 for (n = 16; n < 256 && sz_disk / n / 63 > 1024; n *= 2) ;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3811 if (n == 256) n--;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3812 e_hd = n - 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3813 sz_cyl = 63 * n;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3814 tot_cyl = sz_disk / sz_cyl;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3815
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3816 /* Create partition table */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3817 mem_set(buf, 0, _MAX_SS);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3818 p = buf + MBR_Table; b_cyl = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3819 for (i = 0; i < 4; i++, p += SZ_PTE) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3820 p_cyl = (szt[i] <= 100) ? (DWORD)tot_cyl * szt[i] / 100 : szt[i] / sz_cyl;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3821 if (!p_cyl) continue;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3822 s_part = (DWORD)sz_cyl * b_cyl;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3823 sz_part = (DWORD)sz_cyl * p_cyl;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3824 if (i == 0) { /* Exclude first track of cylinder 0 */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3825 s_hd = 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3826 s_part += 63; sz_part -= 63;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3827 } else {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3828 s_hd = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3829 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3830 e_cyl = b_cyl + p_cyl - 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3831 if (e_cyl >= tot_cyl) return FR_INVALID_PARAMETER;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3832
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3833 /* Set partition table */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3834 p[1] = s_hd; /* Start head */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3835 p[2] = (BYTE)((b_cyl >> 2) + 1); /* Start sector */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3836 p[3] = (BYTE)b_cyl; /* Start cylinder */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3837 p[4] = 0x06; /* System type (temporary setting) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3838 p[5] = e_hd; /* End head */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3839 p[6] = (BYTE)((e_cyl >> 2) + 63); /* End sector */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3840 p[7] = (BYTE)e_cyl; /* End cylinder */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3841 ST_DWORD(p + 8, s_part); /* Start sector in LBA */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3842 ST_DWORD(p + 12, sz_part); /* Partition size */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3843
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3844 /* Next partition */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3845 b_cyl += p_cyl;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3846 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3847 ST_WORD(p, 0xAA55);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3848
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3849 /* Write it to the MBR */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3850 return (disk_write(pdrv, buf, 0, 1) || disk_ioctl(pdrv, CTRL_SYNC, 0)) ? FR_DISK_ERR : FR_OK;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3851 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3852
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3853
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3854 #endif /* _MULTI_PARTITION == 2 */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3855 #endif /* _USE_MKFS && !_FS_READONLY */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3856
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3857
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3858
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3859
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3860 #if _USE_STRFUNC
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3861 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3862 /* Get a string from the file */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3863 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3864 TCHAR* f_gets (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3865 TCHAR* buff, /* Pointer to the string buffer to read */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3866 int len, /* Size of string buffer (characters) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3867 FIL* fil /* Pointer to the file object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3868 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3869 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3870 int n = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3871 TCHAR c, *p = buff;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3872 BYTE s[2];
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3873 UINT rc;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3874
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3875
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3876 while (n < len - 1) { /* Read bytes until buffer gets filled */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3877 f_read(fil, s, 1, &rc);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3878 if (rc != 1) break; /* Break on EOF or error */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3879 c = s[0];
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3880 #if _LFN_UNICODE /* Read a character in UTF-8 encoding */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3881 if (c >= 0x80) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3882 if (c < 0xC0) continue; /* Skip stray trailer */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3883 if (c < 0xE0) { /* Two-byte sequense */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3884 f_read(fil, s, 1, &rc);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3885 if (rc != 1) break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3886 c = ((c & 0x1F) << 6) | (s[0] & 0x3F);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3887 if (c < 0x80) c = '?';
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3888 } else {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3889 if (c < 0xF0) { /* Three-byte sequense */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3890 f_read(fil, s, 2, &rc);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3891 if (rc != 2) break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3892 c = (c << 12) | ((s[0] & 0x3F) << 6) | (s[1] & 0x3F);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3893 if (c < 0x800) c = '?';
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3894 } else { /* Reject four-byte sequense */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3895 c = '?';
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3896 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3897 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3898 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3899 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3900 #if _USE_STRFUNC >= 2
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3901 if (c == '\r') continue; /* Strip '\r' */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3902 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3903 *p++ = c;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3904 n++;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3905 if (c == '\n') break; /* Break on EOL */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3906 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3907 *p = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3908 return n ? buff : 0; /* When no data read (eof or error), return with error. */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3909 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3910
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3911
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3912
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3913 #if !_FS_READONLY
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3914 #include <stdarg.h>
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3915 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3916 /* Put a character to the file */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3917 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3918 int f_putc (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3919 TCHAR c, /* A character to be output */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3920 FIL* fil /* Pointer to the file object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3921 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3922 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3923 UINT bw, btw;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3924 BYTE s[3];
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3925
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3926
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3927 #if _USE_STRFUNC >= 2
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3928 if (c == '\n') f_putc ('\r', fil); /* LF -> CRLF conversion */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3929 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3930
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3931 #if _LFN_UNICODE /* Write the character in UTF-8 encoding */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3932 if (c < 0x80) { /* 7-bit */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3933 s[0] = (BYTE)c;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3934 btw = 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3935 } else {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3936 if (c < 0x800) { /* 11-bit */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3937 s[0] = (BYTE)(0xC0 | (c >> 6));
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3938 s[1] = (BYTE)(0x80 | (c & 0x3F));
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3939 btw = 2;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3940 } else { /* 16-bit */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3941 s[0] = (BYTE)(0xE0 | (c >> 12));
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3942 s[1] = (BYTE)(0x80 | ((c >> 6) & 0x3F));
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3943 s[2] = (BYTE)(0x80 | (c & 0x3F));
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3944 btw = 3;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3945 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3946 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3947 #else /* Write the character without conversion */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3948 s[0] = (BYTE)c;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3949 btw = 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3950 #endif
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3951 f_write(fil, s, btw, &bw); /* Write the char to the file */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3952 return (bw == btw) ? 1 : EOF; /* Return the result */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3953 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3954
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3955
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3956
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3957
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3958 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3959 /* Put a string to the file */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3960 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3961 int f_puts (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3962 const TCHAR* str, /* Pointer to the string to be output */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3963 FIL* fil /* Pointer to the file object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3964 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3965 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3966 int n;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3967
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3968
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3969 for (n = 0; *str; str++, n++) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3970 if (f_putc(*str, fil) == EOF) return EOF;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3971 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3972 return n;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3973 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3974
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3975
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3976
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3977
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3978 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3979 /* Put a formatted string to the file */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3980 /*-----------------------------------------------------------------------*/
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3981 int f_printf (
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3982 FIL* fil, /* Pointer to the file object */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3983 const TCHAR* str, /* Pointer to the format string */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3984 ... /* Optional arguments... */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3985 )
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3986 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3987 va_list arp;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3988 BYTE f, r;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3989 UINT i, j, w;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3990 ULONG v;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3991 TCHAR c, d, s[16], *p;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3992 int res, chc, cc;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3993
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3994
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3995 va_start(arp, str);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3996
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3997 for (cc = res = 0; cc != EOF; res += cc) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3998 c = *str++;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3999 if (c == 0) break; /* End of string */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4000 if (c != '%') { /* Non escape character */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4001 cc = f_putc(c, fil);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4002 if (cc != EOF) cc = 1;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4003 continue;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4004 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4005 w = f = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4006 c = *str++;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4007 if (c == '0') { /* Flag: '0' padding */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4008 f = 1; c = *str++;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4009 } else {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4010 if (c == '-') { /* Flag: left justified */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4011 f = 2; c = *str++;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4012 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4013 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4014 while (IsDigit(c)) { /* Precision */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4015 w = w * 10 + c - '0';
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4016 c = *str++;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4017 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4018 if (c == 'l' || c == 'L') { /* Prefix: Size is long int */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4019 f |= 4; c = *str++;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4020 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4021 if (!c) break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4022 d = c;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4023 if (IsLower(d)) d -= 0x20;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4024 switch (d) { /* Type is... */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4025 case 'S' : /* String */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4026 p = va_arg(arp, TCHAR*);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4027 for (j = 0; p[j]; j++) ;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4028 chc = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4029 if (!(f & 2)) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4030 while (j++ < w) chc += (cc = f_putc(' ', fil));
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4031 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4032 chc += (cc = f_puts(p, fil));
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4033 while (j++ < w) chc += (cc = f_putc(' ', fil));
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4034 if (cc != EOF) cc = chc;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4035 continue;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4036 case 'C' : /* Character */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4037 cc = f_putc((TCHAR)va_arg(arp, int), fil); continue;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4038 case 'B' : /* Binary */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4039 r = 2; break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4040 case 'O' : /* Octal */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4041 r = 8; break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4042 case 'D' : /* Signed decimal */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4043 case 'U' : /* Unsigned decimal */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4044 r = 10; break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4045 case 'X' : /* Hexdecimal */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4046 r = 16; break;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4047 default: /* Unknown type (passthrough) */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4048 cc = f_putc(c, fil); continue;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4049 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4050
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4051 /* Get an argument and put it in numeral */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4052 v = (f & 4) ? (ULONG)va_arg(arp, long) : ((d == 'D') ? (ULONG)(long)va_arg(arp, int) : (ULONG)va_arg(arp, unsigned int));
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4053 if (d == 'D' && (v & 0x80000000)) {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4054 v = 0 - v;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4055 f |= 8;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4056 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4057 i = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4058 do {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4059 d = (TCHAR)(v % r); v /= r;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4060 if (d > 9) d += (c == 'x') ? 0x27 : 0x07;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4061 s[i++] = d + '0';
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4062 } while (v && i < sizeof(s) / sizeof(s[0]));
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4063 if (f & 8) s[i++] = '-';
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4064 j = i; d = (f & 1) ? '0' : ' ';
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4065 res = 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4066 while (!(f & 2) && j++ < w) res += (cc = f_putc(d, fil));
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4067 do res += (cc = f_putc(s[--i], fil)); while(i);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4068 while (j++ < w) res += (cc = f_putc(' ', fil));
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4069 if (cc != EOF) cc = res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4070 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4071
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4072 va_end(arp);
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4073 return (cc == EOF) ? cc : res;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4074 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4075
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4076 #endif /* !_FS_READONLY */
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4077 #endif /* _USE_STRFUNC */