Dependencies:   mbed

Committer:
simon
Date:
Thu Jan 12 12:59:33 2012 +0000
Revision:
1:208803a150b2
Parent:
0:560a6744936c
fix on line 61 of diskio, sector -> s

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon 0:560a6744936c 1 /*----------------------------------------------------------------------------/
simon 0:560a6744936c 2 / FatFs - FAT file system module R0.06 (C)ChaN, 2008
simon 0:560a6744936c 3 /-----------------------------------------------------------------------------/
simon 0:560a6744936c 4 / The FatFs module is an experimenal project to implement FAT file system to
simon 0:560a6744936c 5 / cheap microcontrollers. This is a free software and is opened for education,
simon 0:560a6744936c 6 / research and development under license policy of following trems.
simon 0:560a6744936c 7 /
simon 0:560a6744936c 8 / Copyright (C) 2008, ChaN, all right reserved.
simon 0:560a6744936c 9 /
simon 0:560a6744936c 10 / * The FatFs module is a free software and there is no warranty.
simon 0:560a6744936c 11 / * You can use, modify and/or redistribute it for personal, non-profit or
simon 0:560a6744936c 12 / commercial use without restriction under your responsibility.
simon 0:560a6744936c 13 / * Redistributions of source code must retain the above copyright notice.
simon 0:560a6744936c 14 /
simon 0:560a6744936c 15 /-----------------------------------------------------------------------------/
simon 0:560a6744936c 16 / Feb 26,'06 R0.00 Prototype.
simon 0:560a6744936c 17 /
simon 0:560a6744936c 18 / Apr 29,'06 R0.01 First stable version.
simon 0:560a6744936c 19 /
simon 0:560a6744936c 20 / Jun 01,'06 R0.02 Added FAT12 support.
simon 0:560a6744936c 21 / Removed unbuffered mode.
simon 0:560a6744936c 22 / Fixed a problem on small (<32M) patition.
simon 0:560a6744936c 23 / Jun 10,'06 R0.02a Added a configuration option (_FS_MINIMUM).
simon 0:560a6744936c 24 /
simon 0:560a6744936c 25 / Sep 22,'06 R0.03 Added f_rename().
simon 0:560a6744936c 26 / Changed option _FS_MINIMUM to _FS_MINIMIZE.
simon 0:560a6744936c 27 / Dec 11,'06 R0.03a Improved cluster scan algolithm to write files fast.
simon 0:560a6744936c 28 / Fixed f_mkdir() creates incorrect directory on FAT32.
simon 0:560a6744936c 29 /
simon 0:560a6744936c 30 / Feb 04,'07 R0.04 Supported multiple drive system.
simon 0:560a6744936c 31 / Changed some interfaces for multiple drive system.
simon 0:560a6744936c 32 / Changed f_mountdrv() to f_mount().
simon 0:560a6744936c 33 / Added f_mkfs().
simon 0:560a6744936c 34 / Apr 01,'07 R0.04a Supported multiple partitions on a plysical drive.
simon 0:560a6744936c 35 / Added a capability of extending file size to f_lseek().
simon 0:560a6744936c 36 / Added minimization level 3.
simon 0:560a6744936c 37 / Fixed an endian sensitive code in f_mkfs().
simon 0:560a6744936c 38 / May 05,'07 R0.04b Added a configuration option _USE_NTFLAG.
simon 0:560a6744936c 39 / Added FSInfo support.
simon 0:560a6744936c 40 / Fixed DBCS name can result FR_INVALID_NAME.
simon 0:560a6744936c 41 / Fixed short seek (<= csize) collapses the file object.
simon 0:560a6744936c 42 /
simon 0:560a6744936c 43 / Aug 25,'07 R0.05 Changed arguments of f_read(), f_write() and f_mkfs().
simon 0:560a6744936c 44 / Fixed f_mkfs() on FAT32 creates incorrect FSInfo.
simon 0:560a6744936c 45 / Fixed f_mkdir() on FAT32 creates incorrect directory.
simon 0:560a6744936c 46 / Feb 03,'08 R0.05a Added f_truncate() and f_utime().
simon 0:560a6744936c 47 / Fixed off by one error at FAT sub-type determination.
simon 0:560a6744936c 48 / Fixed btr in f_read() can be mistruncated.
simon 0:560a6744936c 49 / Fixed cached sector is not flushed when create and close
simon 0:560a6744936c 50 / without write.
simon 0:560a6744936c 51 /
simon 0:560a6744936c 52 / Apr 01,'08 R0.06 Added fputc(), fputs(), fprintf() and fgets().
simon 0:560a6744936c 53 / Improved performance of f_lseek() on moving to the same
simon 0:560a6744936c 54 / or following cluster.
simon 0:560a6744936c 55 /---------------------------------------------------------------------------*/
simon 0:560a6744936c 56
simon 0:560a6744936c 57 #include <string.h>
simon 0:560a6744936c 58 #include "ff.h" /* FatFs declarations */
simon 0:560a6744936c 59 #include "diskio.h" /* Include file for user provided disk functions */
simon 0:560a6744936c 60
simon 0:560a6744936c 61
simon 0:560a6744936c 62 /*--------------------------------------------------------------------------
simon 0:560a6744936c 63
simon 0:560a6744936c 64 Module Private Functions
simon 0:560a6744936c 65
simon 0:560a6744936c 66 ---------------------------------------------------------------------------*/
simon 0:560a6744936c 67
simon 0:560a6744936c 68 static
simon 0:560a6744936c 69 FATFS *FatFs[_DRIVES]; /* Pointer to the file system objects (logical drives) */
simon 0:560a6744936c 70 static
simon 0:560a6744936c 71 WORD fsid; /* File system mount ID */
simon 0:560a6744936c 72
simon 0:560a6744936c 73
simon 0:560a6744936c 74
simon 0:560a6744936c 75 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 76 /* Change window offset */
simon 0:560a6744936c 77 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 78
simon 0:560a6744936c 79 static
simon 0:560a6744936c 80 BOOL move_window ( /* TRUE: successful, FALSE: failed */
simon 0:560a6744936c 81 FATFS *fs, /* File system object */
simon 0:560a6744936c 82 DWORD sector /* Sector number to make apperance in the fs->win[] */
simon 0:560a6744936c 83 ) /* Move to zero only writes back dirty window */
simon 0:560a6744936c 84 {
simon 0:560a6744936c 85 DWORD wsect;
simon 0:560a6744936c 86
simon 0:560a6744936c 87
simon 0:560a6744936c 88 wsect = fs->winsect;
simon 0:560a6744936c 89 if (wsect != sector) { /* Changed current window */
simon 0:560a6744936c 90 #if !_FS_READONLY
simon 0:560a6744936c 91 BYTE n;
simon 0:560a6744936c 92 if (fs->winflag) { /* Write back dirty window if needed */
simon 0:560a6744936c 93 if (disk_write(fs->drive, fs->win, wsect, 1) != RES_OK)
simon 0:560a6744936c 94 return FALSE;
simon 0:560a6744936c 95 fs->winflag = 0;
simon 0:560a6744936c 96 if (wsect < (fs->fatbase + fs->sects_fat)) { /* In FAT area */
simon 0:560a6744936c 97 for (n = fs->n_fats; n >= 2; n--) { /* Refrect the change to FAT copy */
simon 0:560a6744936c 98 wsect += fs->sects_fat;
simon 0:560a6744936c 99 disk_write(fs->drive, fs->win, wsect, 1);
simon 0:560a6744936c 100 }
simon 0:560a6744936c 101 }
simon 0:560a6744936c 102 }
simon 0:560a6744936c 103 #endif
simon 0:560a6744936c 104 if (sector) {
simon 0:560a6744936c 105 if (disk_read(fs->drive, fs->win, sector, 1) != RES_OK)
simon 0:560a6744936c 106 return FALSE;
simon 0:560a6744936c 107 fs->winsect = sector;
simon 0:560a6744936c 108 }
simon 0:560a6744936c 109 }
simon 0:560a6744936c 110 return TRUE;
simon 0:560a6744936c 111 }
simon 0:560a6744936c 112
simon 0:560a6744936c 113
simon 0:560a6744936c 114
simon 0:560a6744936c 115
simon 0:560a6744936c 116 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 117 /* Clean-up cached data */
simon 0:560a6744936c 118 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 119
simon 0:560a6744936c 120 #if !_FS_READONLY
simon 0:560a6744936c 121 static
simon 0:560a6744936c 122 FRESULT sync ( /* FR_OK: successful, FR_RW_ERROR: failed */
simon 0:560a6744936c 123 FATFS *fs /* File system object */
simon 0:560a6744936c 124 )
simon 0:560a6744936c 125 {
simon 0:560a6744936c 126 fs->winflag = 1;
simon 0:560a6744936c 127 if (!move_window(fs, 0)) return FR_RW_ERROR;
simon 0:560a6744936c 128 #if _USE_FSINFO
simon 0:560a6744936c 129 /* Update FSInfo sector if needed */
simon 0:560a6744936c 130 if (fs->fs_type == FS_FAT32 && fs->fsi_flag) {
simon 0:560a6744936c 131 fs->winsect = 0;
simon 0:560a6744936c 132 memset(fs->win, 0, 512);
simon 0:560a6744936c 133 ST_WORD(&fs->win[BS_55AA], 0xAA55);
simon 0:560a6744936c 134 ST_DWORD(&fs->win[FSI_LeadSig], 0x41615252);
simon 0:560a6744936c 135 ST_DWORD(&fs->win[FSI_StrucSig], 0x61417272);
simon 0:560a6744936c 136 ST_DWORD(&fs->win[FSI_Free_Count], fs->free_clust);
simon 0:560a6744936c 137 ST_DWORD(&fs->win[FSI_Nxt_Free], fs->last_clust);
simon 0:560a6744936c 138 disk_write(fs->drive, fs->win, fs->fsi_sector, 1);
simon 0:560a6744936c 139 fs->fsi_flag = 0;
simon 0:560a6744936c 140 }
simon 0:560a6744936c 141 #endif
simon 0:560a6744936c 142 /* Make sure that no pending write process in the physical drive */
simon 0:560a6744936c 143 if (disk_ioctl(fs->drive, CTRL_SYNC, NULL) != RES_OK)
simon 0:560a6744936c 144 return FR_RW_ERROR;
simon 0:560a6744936c 145 return FR_OK;
simon 0:560a6744936c 146 }
simon 0:560a6744936c 147 #endif
simon 0:560a6744936c 148
simon 0:560a6744936c 149
simon 0:560a6744936c 150
simon 0:560a6744936c 151
simon 0:560a6744936c 152 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 153 /* Get a cluster status */
simon 0:560a6744936c 154 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 155
simon 0:560a6744936c 156 static
simon 0:560a6744936c 157 DWORD get_cluster ( /* 0,>=2: successful, 1: failed */
simon 0:560a6744936c 158 FATFS *fs, /* File system object */
simon 0:560a6744936c 159 DWORD clust /* Cluster# to get the link information */
simon 0:560a6744936c 160 )
simon 0:560a6744936c 161 {
simon 0:560a6744936c 162 WORD wc, bc;
simon 0:560a6744936c 163 DWORD fatsect;
simon 0:560a6744936c 164
simon 0:560a6744936c 165
simon 0:560a6744936c 166 if (clust >= 2 && clust < fs->max_clust) { /* Is it a valid cluster#? */
simon 0:560a6744936c 167 fatsect = fs->fatbase;
simon 0:560a6744936c 168 switch (fs->fs_type) {
simon 0:560a6744936c 169 case FS_FAT12 :
simon 0:560a6744936c 170 bc = (WORD)clust * 3 / 2;
simon 0:560a6744936c 171 if (!move_window(fs, fatsect + (bc / SS(fs)))) break;
simon 0:560a6744936c 172 wc = fs->win[bc & (SS(fs) - 1)]; bc++;
simon 0:560a6744936c 173 if (!move_window(fs, fatsect + (bc / SS(fs)))) break;
simon 0:560a6744936c 174 wc |= (WORD)fs->win[bc & (SS(fs) - 1)] << 8;
simon 0:560a6744936c 175 return (clust & 1) ? (wc >> 4) : (wc & 0xFFF);
simon 0:560a6744936c 176
simon 0:560a6744936c 177 case FS_FAT16 :
simon 0:560a6744936c 178 if (!move_window(fs, fatsect + (clust / (SS(fs) / 2)))) break;
simon 0:560a6744936c 179 return LD_WORD(&fs->win[((WORD)clust * 2) & (SS(fs) - 1)]);
simon 0:560a6744936c 180
simon 0:560a6744936c 181 case FS_FAT32 :
simon 0:560a6744936c 182 if (!move_window(fs, fatsect + (clust / (SS(fs) / 4)))) break;
simon 0:560a6744936c 183 return LD_DWORD(&fs->win[((WORD)clust * 4) & (SS(fs) - 1)]) & 0x0FFFFFFF;
simon 0:560a6744936c 184 }
simon 0:560a6744936c 185 }
simon 0:560a6744936c 186
simon 0:560a6744936c 187 return 1; /* Out of cluster range, or an error occured */
simon 0:560a6744936c 188 }
simon 0:560a6744936c 189
simon 0:560a6744936c 190
simon 0:560a6744936c 191
simon 0:560a6744936c 192
simon 0:560a6744936c 193 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 194 /* Change a cluster status */
simon 0:560a6744936c 195 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 196
simon 0:560a6744936c 197 #if !_FS_READONLY
simon 0:560a6744936c 198 static
simon 0:560a6744936c 199 BOOL put_cluster ( /* TRUE: successful, FALSE: failed */
simon 0:560a6744936c 200 FATFS *fs, /* File system object */
simon 0:560a6744936c 201 DWORD clust, /* Cluster# to change (must be 2 to fs->max_clust-1) */
simon 0:560a6744936c 202 DWORD val /* New value to mark the cluster */
simon 0:560a6744936c 203 )
simon 0:560a6744936c 204 {
simon 0:560a6744936c 205 WORD bc;
simon 0:560a6744936c 206 BYTE *p;
simon 0:560a6744936c 207 DWORD fatsect;
simon 0:560a6744936c 208
simon 0:560a6744936c 209
simon 0:560a6744936c 210 fatsect = fs->fatbase;
simon 0:560a6744936c 211 switch (fs->fs_type) {
simon 0:560a6744936c 212 case FS_FAT12 :
simon 0:560a6744936c 213 bc = (WORD)clust * 3 / 2;
simon 0:560a6744936c 214 if (!move_window(fs, fatsect + (bc / SS(fs)))) return FALSE;
simon 0:560a6744936c 215 p = &fs->win[bc & (SS(fs) - 1)];
simon 0:560a6744936c 216 *p = (clust & 1) ? ((*p & 0x0F) | ((BYTE)val << 4)) : (BYTE)val;
simon 0:560a6744936c 217 bc++;
simon 0:560a6744936c 218 fs->winflag = 1;
simon 0:560a6744936c 219 if (!move_window(fs, fatsect + (bc / SS(fs)))) return FALSE;
simon 0:560a6744936c 220 p = &fs->win[bc & (SS(fs) - 1)];
simon 0:560a6744936c 221 *p = (clust & 1) ? (BYTE)(val >> 4) : ((*p & 0xF0) | ((BYTE)(val >> 8) & 0x0F));
simon 0:560a6744936c 222 break;
simon 0:560a6744936c 223
simon 0:560a6744936c 224 case FS_FAT16 :
simon 0:560a6744936c 225 if (!move_window(fs, fatsect + (clust / (SS(fs) / 2)))) return FALSE;
simon 0:560a6744936c 226 ST_WORD(&fs->win[((WORD)clust * 2) & (SS(fs) - 1)], (WORD)val);
simon 0:560a6744936c 227 break;
simon 0:560a6744936c 228
simon 0:560a6744936c 229 case FS_FAT32 :
simon 0:560a6744936c 230 if (!move_window(fs, fatsect + (clust / (SS(fs) / 4)))) return FALSE;
simon 0:560a6744936c 231 ST_DWORD(&fs->win[((WORD)clust * 4) & (SS(fs) - 1)], val);
simon 0:560a6744936c 232 break;
simon 0:560a6744936c 233
simon 0:560a6744936c 234 default :
simon 0:560a6744936c 235 return FALSE;
simon 0:560a6744936c 236 }
simon 0:560a6744936c 237 fs->winflag = 1;
simon 0:560a6744936c 238 return TRUE;
simon 0:560a6744936c 239 }
simon 0:560a6744936c 240 #endif /* !_FS_READONLY */
simon 0:560a6744936c 241
simon 0:560a6744936c 242
simon 0:560a6744936c 243
simon 0:560a6744936c 244
simon 0:560a6744936c 245 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 246 /* Remove a cluster chain */
simon 0:560a6744936c 247 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 248
simon 0:560a6744936c 249 #if !_FS_READONLY
simon 0:560a6744936c 250 static
simon 0:560a6744936c 251 BOOL remove_chain ( /* TRUE: successful, FALSE: failed */
simon 0:560a6744936c 252 FATFS *fs, /* File system object */
simon 0:560a6744936c 253 DWORD clust /* Cluster# to remove chain from */
simon 0:560a6744936c 254 )
simon 0:560a6744936c 255 {
simon 0:560a6744936c 256 DWORD nxt;
simon 0:560a6744936c 257
simon 0:560a6744936c 258
simon 0:560a6744936c 259 while (clust >= 2 && clust < fs->max_clust) {
simon 0:560a6744936c 260 nxt = get_cluster(fs, clust);
simon 0:560a6744936c 261 if (nxt == 1) return FALSE;
simon 0:560a6744936c 262 if (!put_cluster(fs, clust, 0)) return FALSE;
simon 0:560a6744936c 263 if (fs->free_clust != 0xFFFFFFFF) {
simon 0:560a6744936c 264 fs->free_clust++;
simon 0:560a6744936c 265 #if _USE_FSINFO
simon 0:560a6744936c 266 fs->fsi_flag = 1;
simon 0:560a6744936c 267 #endif
simon 0:560a6744936c 268 }
simon 0:560a6744936c 269 clust = nxt;
simon 0:560a6744936c 270 }
simon 0:560a6744936c 271 return TRUE;
simon 0:560a6744936c 272 }
simon 0:560a6744936c 273 #endif
simon 0:560a6744936c 274
simon 0:560a6744936c 275
simon 0:560a6744936c 276
simon 0:560a6744936c 277
simon 0:560a6744936c 278 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 279 /* Stretch or create a cluster chain */
simon 0:560a6744936c 280 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 281
simon 0:560a6744936c 282 #if !_FS_READONLY
simon 0:560a6744936c 283 static
simon 0:560a6744936c 284 DWORD create_chain ( /* 0: No free cluster, 1: Error, >=2: New cluster number */
simon 0:560a6744936c 285 FATFS *fs, /* File system object */
simon 0:560a6744936c 286 DWORD clust /* Cluster# to stretch, 0 means create new */
simon 0:560a6744936c 287 )
simon 0:560a6744936c 288 {
simon 0:560a6744936c 289 DWORD cstat, ncl, scl, mcl = fs->max_clust;
simon 0:560a6744936c 290
simon 0:560a6744936c 291
simon 0:560a6744936c 292 if (clust == 0) { /* Create new chain */
simon 0:560a6744936c 293 scl = fs->last_clust; /* Get suggested start point */
simon 0:560a6744936c 294 if (scl == 0 || scl >= mcl) scl = 1;
simon 0:560a6744936c 295 }
simon 0:560a6744936c 296 else { /* Stretch existing chain */
simon 0:560a6744936c 297 cstat = get_cluster(fs, clust); /* Check the cluster status */
simon 0:560a6744936c 298 if (cstat < 2) return 1; /* It is an invalid cluster */
simon 0:560a6744936c 299 if (cstat < mcl) return cstat; /* It is already followed by next cluster */
simon 0:560a6744936c 300 scl = clust;
simon 0:560a6744936c 301 }
simon 0:560a6744936c 302
simon 0:560a6744936c 303 ncl = scl; /* Start cluster */
simon 0:560a6744936c 304 for (;;) {
simon 0:560a6744936c 305 ncl++; /* Next cluster */
simon 0:560a6744936c 306 if (ncl >= mcl) { /* Wrap around */
simon 0:560a6744936c 307 ncl = 2;
simon 0:560a6744936c 308 if (ncl > scl) return 0; /* No free custer */
simon 0:560a6744936c 309 }
simon 0:560a6744936c 310 cstat = get_cluster(fs, ncl); /* Get the cluster status */
simon 0:560a6744936c 311 if (cstat == 0) break; /* Found a free cluster */
simon 0:560a6744936c 312 if (cstat == 1) return 1; /* Any error occured */
simon 0:560a6744936c 313 if (ncl == scl) return 0; /* No free custer */
simon 0:560a6744936c 314 }
simon 0:560a6744936c 315
simon 0:560a6744936c 316 if (!put_cluster(fs, ncl, 0x0FFFFFFF)) return 1; /* Mark the new cluster "in use" */
simon 0:560a6744936c 317 if (clust != 0 && !put_cluster(fs, clust, ncl)) return 1; /* Link it to previous one if needed */
simon 0:560a6744936c 318
simon 0:560a6744936c 319 fs->last_clust = ncl; /* Update fsinfo */
simon 0:560a6744936c 320 if (fs->free_clust != 0xFFFFFFFF) {
simon 0:560a6744936c 321 fs->free_clust--;
simon 0:560a6744936c 322 #if _USE_FSINFO
simon 0:560a6744936c 323 fs->fsi_flag = 1;
simon 0:560a6744936c 324 #endif
simon 0:560a6744936c 325 }
simon 0:560a6744936c 326
simon 0:560a6744936c 327 return ncl; /* Return new cluster number */
simon 0:560a6744936c 328 }
simon 0:560a6744936c 329 #endif /* !_FS_READONLY */
simon 0:560a6744936c 330
simon 0:560a6744936c 331
simon 0:560a6744936c 332
simon 0:560a6744936c 333
simon 0:560a6744936c 334 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 335 /* Get sector# from cluster# */
simon 0:560a6744936c 336 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 337
simon 0:560a6744936c 338 static
simon 0:560a6744936c 339 DWORD clust2sect ( /* !=0: sector number, 0: failed - invalid cluster# */
simon 0:560a6744936c 340 FATFS *fs, /* File system object */
simon 0:560a6744936c 341 DWORD clust /* Cluster# to be converted */
simon 0:560a6744936c 342 )
simon 0:560a6744936c 343 {
simon 0:560a6744936c 344 clust -= 2;
simon 0:560a6744936c 345 if (clust >= (fs->max_clust - 2)) return 0; /* Invalid cluster# */
simon 0:560a6744936c 346 return clust * fs->csize + fs->database;
simon 0:560a6744936c 347 }
simon 0:560a6744936c 348
simon 0:560a6744936c 349
simon 0:560a6744936c 350
simon 0:560a6744936c 351
simon 0:560a6744936c 352 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 353 /* Move directory pointer to next */
simon 0:560a6744936c 354 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 355
simon 0:560a6744936c 356 static
simon 0:560a6744936c 357 BOOL next_dir_entry ( /* TRUE: successful, FALSE: could not move next */
simon 0:560a6744936c 358 FATFS_DIR *dj /* Pointer to directory object */
simon 0:560a6744936c 359 )
simon 0:560a6744936c 360 {
simon 0:560a6744936c 361 DWORD clust;
simon 0:560a6744936c 362 WORD idx;
simon 0:560a6744936c 363
simon 0:560a6744936c 364
simon 0:560a6744936c 365 idx = dj->index + 1;
simon 0:560a6744936c 366 if ((idx & ((SS(dj->fs) - 1) / 32)) == 0) { /* Table sector changed? */
simon 0:560a6744936c 367 dj->sect++; /* Next sector */
simon 0:560a6744936c 368 if (dj->clust == 0) { /* In static table */
simon 0:560a6744936c 369 if (idx >= dj->fs->n_rootdir) return FALSE; /* Reached to end of table */
simon 0:560a6744936c 370 } else { /* In dynamic table */
simon 0:560a6744936c 371 if (((idx / (SS(dj->fs) / 32)) & (dj->fs->csize - 1)) == 0) { /* Cluster changed? */
simon 0:560a6744936c 372 clust = get_cluster(dj->fs, dj->clust); /* Get next cluster */
simon 0:560a6744936c 373 if (clust < 2 || clust >= dj->fs->max_clust) /* Reached to end of table */
simon 0:560a6744936c 374 return FALSE;
simon 0:560a6744936c 375 dj->clust = clust; /* Initialize for new cluster */
simon 0:560a6744936c 376 dj->sect = clust2sect(dj->fs, clust);
simon 0:560a6744936c 377 }
simon 0:560a6744936c 378 }
simon 0:560a6744936c 379 }
simon 0:560a6744936c 380 dj->index = idx; /* Lower several bits of dj->index indicates offset in dj->sect */
simon 0:560a6744936c 381 return TRUE;
simon 0:560a6744936c 382 }
simon 0:560a6744936c 383
simon 0:560a6744936c 384
simon 0:560a6744936c 385
simon 0:560a6744936c 386
simon 0:560a6744936c 387 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 388 /* Get file status from directory entry */
simon 0:560a6744936c 389 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 390
simon 0:560a6744936c 391 #if _FS_MINIMIZE <= 1
simon 0:560a6744936c 392 static
simon 0:560a6744936c 393 void get_fileinfo ( /* No return code */
simon 0:560a6744936c 394 FILINFO *finfo, /* Ptr to store the file information */
simon 0:560a6744936c 395 const BYTE *dir /* Ptr to the directory entry */
simon 0:560a6744936c 396 )
simon 0:560a6744936c 397 {
simon 0:560a6744936c 398 BYTE n, c, a;
simon 0:560a6744936c 399 char *p;
simon 0:560a6744936c 400
simon 0:560a6744936c 401
simon 0:560a6744936c 402 p = &finfo->fname[0];
simon 0:560a6744936c 403 a = _USE_NTFLAG ? dir[DIR_NTres] : 0; /* NT flag */
simon 0:560a6744936c 404 for (n = 0; n < 8; n++) { /* Convert file name (body) */
simon 0:560a6744936c 405 c = dir[n];
simon 0:560a6744936c 406 if (c == ' ') break;
simon 0:560a6744936c 407 if (c == 0x05) c = 0xE5;
simon 0:560a6744936c 408 if (a & 0x08 && c >= 'A' && c <= 'Z') c += 0x20;
simon 0:560a6744936c 409 *p++ = c;
simon 0:560a6744936c 410 }
simon 0:560a6744936c 411 if (dir[8] != ' ') { /* Convert file name (extension) */
simon 0:560a6744936c 412 *p++ = '.';
simon 0:560a6744936c 413 for (n = 8; n < 11; n++) {
simon 0:560a6744936c 414 c = dir[n];
simon 0:560a6744936c 415 if (c == ' ') break;
simon 0:560a6744936c 416 if (a & 0x10 && c >= 'A' && c <= 'Z') c += 0x20;
simon 0:560a6744936c 417 *p++ = c;
simon 0:560a6744936c 418 }
simon 0:560a6744936c 419 }
simon 0:560a6744936c 420 *p = '\0';
simon 0:560a6744936c 421
simon 0:560a6744936c 422 finfo->fattrib = dir[DIR_Attr]; /* Attribute */
simon 0:560a6744936c 423 finfo->fsize = LD_DWORD(&dir[DIR_FileSize]); /* Size */
simon 0:560a6744936c 424 finfo->fdate = LD_WORD(&dir[DIR_WrtDate]); /* Date */
simon 0:560a6744936c 425 finfo->ftime = LD_WORD(&dir[DIR_WrtTime]); /* Time */
simon 0:560a6744936c 426 }
simon 0:560a6744936c 427 #endif /* _FS_MINIMIZE <= 1 */
simon 0:560a6744936c 428
simon 0:560a6744936c 429
simon 0:560a6744936c 430
simon 0:560a6744936c 431
simon 0:560a6744936c 432 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 433 /* Pick a paragraph and create the name in format of directory entry */
simon 0:560a6744936c 434 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 435
simon 0:560a6744936c 436 static
simon 0:560a6744936c 437 char make_dirfile ( /* 1: error - detected an invalid format, '\0'or'/': next character */
simon 0:560a6744936c 438 const char **path, /* Pointer to the file path pointer */
simon 0:560a6744936c 439 char *dirname /* Pointer to directory name buffer {Name(8), Ext(3), NT flag(1)} */
simon 0:560a6744936c 440 )
simon 0:560a6744936c 441 {
simon 0:560a6744936c 442 BYTE n, t, c, a, b;
simon 0:560a6744936c 443
simon 0:560a6744936c 444
simon 0:560a6744936c 445 memset(dirname, ' ', 8+3); /* Fill buffer with spaces */
simon 0:560a6744936c 446 a = 0; b = 0x18; /* NT flag */
simon 0:560a6744936c 447 n = 0; t = 8;
simon 0:560a6744936c 448 for (;;) {
simon 0:560a6744936c 449 c = *(*path)++;
simon 0:560a6744936c 450 if (c == '\0' || c == '/') { /* Reached to end of str or directory separator */
simon 0:560a6744936c 451 if (n == 0) break;
simon 0:560a6744936c 452 dirname[11] = _USE_NTFLAG ? (a & b) : 0;
simon 0:560a6744936c 453 return c;
simon 0:560a6744936c 454 }
simon 0:560a6744936c 455 if (c <= ' ' || c == 0x7F) break; /* Reject invisible chars */
simon 0:560a6744936c 456 if (c == '.') {
simon 0:560a6744936c 457 if (!(a & 1) && n >= 1 && n <= 8) { /* Enter extension part */
simon 0:560a6744936c 458 n = 8; t = 11; continue;
simon 0:560a6744936c 459 }
simon 0:560a6744936c 460 break;
simon 0:560a6744936c 461 }
simon 0:560a6744936c 462 if (_USE_SJIS &&
simon 0:560a6744936c 463 ((c >= 0x81 && c <= 0x9F) || /* Accept S-JIS code */
simon 0:560a6744936c 464 (c >= 0xE0 && c <= 0xFC))) {
simon 0:560a6744936c 465 if (n == 0 && c == 0xE5) /* Change heading \xE5 to \x05 */
simon 0:560a6744936c 466 c = 0x05;
simon 0:560a6744936c 467 a ^= 0x01; goto md_l2;
simon 0:560a6744936c 468 }
simon 0:560a6744936c 469 if (c == '"') break; /* Reject " */
simon 0:560a6744936c 470 if (c <= ')') goto md_l1; /* Accept ! # $ % & ' ( ) */
simon 0:560a6744936c 471 if (c <= ',') break; /* Reject * + , */
simon 0:560a6744936c 472 if (c <= '9') goto md_l1; /* Accept - 0-9 */
simon 0:560a6744936c 473 if (c <= '?') break; /* Reject : ; < = > ? */
simon 0:560a6744936c 474 if (!(a & 1)) { /* These checks are not applied to S-JIS 2nd byte */
simon 0:560a6744936c 475 if (c == '|') break; /* Reject | */
simon 0:560a6744936c 476 if (c >= '[' && c <= ']') break;/* Reject [ \ ] */
simon 0:560a6744936c 477 if (_USE_NTFLAG && c >= 'A' && c <= 'Z')
simon 0:560a6744936c 478 (t == 8) ? (b &= 0xF7) : (b &= 0xEF);
simon 0:560a6744936c 479 if (c >= 'a' && c <= 'z') { /* Convert to upper case */
simon 0:560a6744936c 480 c -= 0x20;
simon 0:560a6744936c 481 if (_USE_NTFLAG) (t == 8) ? (a |= 0x08) : (a |= 0x10);
simon 0:560a6744936c 482 }
simon 0:560a6744936c 483 }
simon 0:560a6744936c 484 md_l1:
simon 0:560a6744936c 485 a &= 0xFE;
simon 0:560a6744936c 486 md_l2:
simon 0:560a6744936c 487 if (n >= t) break;
simon 0:560a6744936c 488 dirname[n++] = c;
simon 0:560a6744936c 489 }
simon 0:560a6744936c 490 return 1;
simon 0:560a6744936c 491 }
simon 0:560a6744936c 492
simon 0:560a6744936c 493
simon 0:560a6744936c 494
simon 0:560a6744936c 495
simon 0:560a6744936c 496 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 497 /* Trace a file path */
simon 0:560a6744936c 498 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 499
simon 0:560a6744936c 500 static
simon 0:560a6744936c 501 FRESULT trace_path ( /* FR_OK(0): successful, !=0: error code */
simon 0:560a6744936c 502 FATFS_DIR *dj, /* Pointer to directory object to return last directory */
simon 0:560a6744936c 503 char *fn, /* Pointer to last segment name to return {file(8),ext(3),attr(1)} */
simon 0:560a6744936c 504 const char *path, /* Full-path string to trace a file or directory */
simon 0:560a6744936c 505 BYTE **dir /* Pointer to pointer to found entry to retutn */
simon 0:560a6744936c 506 )
simon 0:560a6744936c 507 {
simon 0:560a6744936c 508 DWORD clust;
simon 0:560a6744936c 509 char ds;
simon 0:560a6744936c 510 BYTE *dptr = NULL;
simon 0:560a6744936c 511 FATFS *fs = dj->fs;
simon 0:560a6744936c 512
simon 0:560a6744936c 513
simon 0:560a6744936c 514 /* Initialize directory object */
simon 0:560a6744936c 515 clust = fs->dirbase;
simon 0:560a6744936c 516 if (fs->fs_type == FS_FAT32) {
simon 0:560a6744936c 517 dj->clust = dj->sclust = clust;
simon 0:560a6744936c 518 dj->sect = clust2sect(fs, clust);
simon 0:560a6744936c 519 } else {
simon 0:560a6744936c 520 dj->clust = dj->sclust = 0;
simon 0:560a6744936c 521 dj->sect = clust;
simon 0:560a6744936c 522 }
simon 0:560a6744936c 523 dj->index = 0;
simon 0:560a6744936c 524
simon 0:560a6744936c 525 if (*path == '\0') { /* Null path means the root directory */
simon 0:560a6744936c 526 *dir = NULL; return FR_OK;
simon 0:560a6744936c 527 }
simon 0:560a6744936c 528
simon 0:560a6744936c 529 for (;;) {
simon 0:560a6744936c 530 ds = make_dirfile(&path, fn); /* Get a paragraph into fn[] */
simon 0:560a6744936c 531 if (ds == 1) return FR_INVALID_NAME;
simon 0:560a6744936c 532 for (;;) {
simon 0:560a6744936c 533 if (!move_window(fs, dj->sect)) return FR_RW_ERROR;
simon 0:560a6744936c 534 dptr = &fs->win[(dj->index & ((SS(fs) - 1) / 32)) * 32]; /* Pointer to the directory entry */
simon 0:560a6744936c 535 if (dptr[DIR_Name] == 0) /* Has it reached to end of dir? */
simon 0:560a6744936c 536 return !ds ? FR_NO_FILE : FR_NO_PATH;
simon 0:560a6744936c 537 if (dptr[DIR_Name] != 0xE5 /* Matched? */
simon 0:560a6744936c 538 && !(dptr[DIR_Attr] & AM_VOL)
simon 0:560a6744936c 539 && !memcmp(&dptr[DIR_Name], fn, 8+3) ) break;
simon 0:560a6744936c 540 if (!next_dir_entry(dj)) /* Next directory pointer */
simon 0:560a6744936c 541 return !ds ? FR_NO_FILE : FR_NO_PATH;
simon 0:560a6744936c 542 }
simon 0:560a6744936c 543 if (!ds) { *dir = dptr; return FR_OK; } /* Matched with end of path */
simon 0:560a6744936c 544 if (!(dptr[DIR_Attr] & AM_DIR)) return FR_NO_PATH; /* Cannot trace because it is a file */
simon 0:560a6744936c 545 clust = ((DWORD)LD_WORD(&dptr[DIR_FstClusHI]) << 16) | LD_WORD(&dptr[DIR_FstClusLO]); /* Get cluster# of the directory */
simon 0:560a6744936c 546 dj->clust = dj->sclust = clust; /* Restart scanning at the new directory */
simon 0:560a6744936c 547 dj->sect = clust2sect(fs, clust);
simon 0:560a6744936c 548 dj->index = 2;
simon 0:560a6744936c 549 }
simon 0:560a6744936c 550 }
simon 0:560a6744936c 551
simon 0:560a6744936c 552
simon 0:560a6744936c 553
simon 0:560a6744936c 554
simon 0:560a6744936c 555 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 556 /* Reserve a directory entry */
simon 0:560a6744936c 557 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 558
simon 0:560a6744936c 559 #if !_FS_READONLY
simon 0:560a6744936c 560 static
simon 0:560a6744936c 561 FRESULT reserve_direntry ( /* FR_OK: successful, FR_DENIED: no free entry, FR_RW_ERROR: a disk error occured */
simon 0:560a6744936c 562 FATFS_DIR *dj, /* Target directory to create new entry */
simon 0:560a6744936c 563 BYTE **dir /* Pointer to pointer to created entry to retutn */
simon 0:560a6744936c 564 )
simon 0:560a6744936c 565 {
simon 0:560a6744936c 566 DWORD clust, sector;
simon 0:560a6744936c 567 BYTE c, n, *dptr;
simon 0:560a6744936c 568 FATFS *fs = dj->fs;
simon 0:560a6744936c 569
simon 0:560a6744936c 570
simon 0:560a6744936c 571 /* Re-initialize directory object */
simon 0:560a6744936c 572 clust = dj->sclust;
simon 0:560a6744936c 573 if (clust != 0) { /* Dyanmic directory table */
simon 0:560a6744936c 574 dj->clust = clust;
simon 0:560a6744936c 575 dj->sect = clust2sect(fs, clust);
simon 0:560a6744936c 576 } else { /* Static directory table */
simon 0:560a6744936c 577 dj->sect = fs->dirbase;
simon 0:560a6744936c 578 }
simon 0:560a6744936c 579 dj->index = 0;
simon 0:560a6744936c 580
simon 0:560a6744936c 581 do {
simon 0:560a6744936c 582 if (!move_window(fs, dj->sect)) return FR_RW_ERROR;
simon 0:560a6744936c 583 dptr = &fs->win[(dj->index & ((SS(dj->fs) - 1) / 32)) * 32]; /* Pointer to the directory entry */
simon 0:560a6744936c 584 c = dptr[DIR_Name];
simon 0:560a6744936c 585 if (c == 0 || c == 0xE5) { /* Found an empty entry */
simon 0:560a6744936c 586 *dir = dptr; return FR_OK;
simon 0:560a6744936c 587 }
simon 0:560a6744936c 588 } while (next_dir_entry(dj)); /* Next directory pointer */
simon 0:560a6744936c 589 /* Reached to end of the directory table */
simon 0:560a6744936c 590
simon 0:560a6744936c 591 /* Abort when it is a static table or could not stretch dynamic table */
simon 0:560a6744936c 592 if (clust == 0 || ((clust = create_chain(fs, dj->clust)) == 0)) return FR_DENIED;
simon 0:560a6744936c 593 if (clust == 1 || !move_window(fs, 0)) return FR_RW_ERROR;
simon 0:560a6744936c 594
simon 0:560a6744936c 595 /* Cleanup the expanded table */
simon 0:560a6744936c 596 fs->winsect = sector = clust2sect(fs, clust);
simon 0:560a6744936c 597 memset(fs->win, 0, SS(fs));
simon 0:560a6744936c 598 for (n = fs->csize; n; n--) {
simon 0:560a6744936c 599 if (disk_write(fs->drive, fs->win, sector, 1) != RES_OK)
simon 0:560a6744936c 600 return FR_RW_ERROR;
simon 0:560a6744936c 601 sector++;
simon 0:560a6744936c 602 }
simon 0:560a6744936c 603 fs->winflag = 1;
simon 0:560a6744936c 604 *dir = fs->win;
simon 0:560a6744936c 605
simon 0:560a6744936c 606 return FR_OK;
simon 0:560a6744936c 607 }
simon 0:560a6744936c 608 #endif /* !_FS_READONLY */
simon 0:560a6744936c 609
simon 0:560a6744936c 610
simon 0:560a6744936c 611
simon 0:560a6744936c 612
simon 0:560a6744936c 613 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 614 /* Load boot record and check if it is an FAT boot record */
simon 0:560a6744936c 615 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 616
simon 0:560a6744936c 617 static
simon 0:560a6744936c 618 BYTE check_fs ( /* 0:The FAT boot record, 1:Valid boot record but not an FAT, 2:Not a boot record or error */
simon 0:560a6744936c 619 FATFS *fs, /* File system object */
simon 0:560a6744936c 620 DWORD sect /* Sector# (lba) to check if it is an FAT boot record or not */
simon 0:560a6744936c 621 )
simon 0:560a6744936c 622 {
simon 0:560a6744936c 623 if (disk_read(fs->drive, fs->win, sect, 1) != RES_OK) /* Load boot record */
simon 0:560a6744936c 624 return 2;
simon 0:560a6744936c 625 if (LD_WORD(&fs->win[BS_55AA]) != 0xAA55) /* Check record signature (always placed at offset 510 even if the sector size is >512) */
simon 0:560a6744936c 626 return 2;
simon 0:560a6744936c 627
simon 0:560a6744936c 628 if (!memcmp(&fs->win[BS_FilSysType], "FAT", 3)) /* Check FAT signature */
simon 0:560a6744936c 629 return 0;
simon 0:560a6744936c 630 if (!memcmp(&fs->win[BS_FilSysType32], "FAT32", 5) && !(fs->win[BPB_ExtFlags] & 0x80))
simon 0:560a6744936c 631 return 0;
simon 0:560a6744936c 632
simon 0:560a6744936c 633 return 1;
simon 0:560a6744936c 634 }
simon 0:560a6744936c 635
simon 0:560a6744936c 636
simon 0:560a6744936c 637
simon 0:560a6744936c 638
simon 0:560a6744936c 639 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 640 /* Make sure that the file system is valid */
simon 0:560a6744936c 641 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 642
simon 0:560a6744936c 643 static
simon 0:560a6744936c 644 FRESULT auto_mount ( /* FR_OK(0): successful, !=0: any error occured */
simon 0:560a6744936c 645 const char **path, /* Pointer to pointer to the path name (drive number) */
simon 0:560a6744936c 646 FATFS **rfs, /* Pointer to pointer to the found file system object */
simon 0:560a6744936c 647 BYTE chk_wp /* !=0: Check media write protection for write access */
simon 0:560a6744936c 648 )
simon 0:560a6744936c 649 {
simon 0:560a6744936c 650 BYTE drv, fmt, *tbl;
simon 0:560a6744936c 651 DSTATUS stat;
simon 0:560a6744936c 652 DWORD bootsect, fatsize, totalsect, maxclust;
simon 0:560a6744936c 653 const char *p = *path;
simon 0:560a6744936c 654 FATFS *fs;
simon 0:560a6744936c 655
simon 0:560a6744936c 656
simon 0:560a6744936c 657 /* Get drive number from the path name */
simon 0:560a6744936c 658 while (*p == ' ') p++; /* Strip leading spaces */
simon 0:560a6744936c 659 drv = p[0] - '0'; /* Is there a drive number? */
simon 0:560a6744936c 660 if (drv <= 9 && p[1] == ':')
simon 0:560a6744936c 661 p += 2; /* Found a drive number, get and strip it */
simon 0:560a6744936c 662 else
simon 0:560a6744936c 663 drv = 0; /* No drive number is given, use drive number 0 as default */
simon 0:560a6744936c 664 if (*p == '/') p++; /* Strip heading slash */
simon 0:560a6744936c 665 *path = p; /* Return pointer to the path name */
simon 0:560a6744936c 666
simon 0:560a6744936c 667 /* Check if the drive number is valid or not */
simon 0:560a6744936c 668 if (drv >= _DRIVES) return FR_INVALID_DRIVE; /* Is the drive number valid? */
simon 0:560a6744936c 669 *rfs = fs = FatFs[drv]; /* Returen pointer to the corresponding file system object */
simon 0:560a6744936c 670 if (!fs) return FR_NOT_ENABLED; /* Is the file system object registered? */
simon 0:560a6744936c 671
simon 0:560a6744936c 672 if (fs->fs_type) { /* If the logical drive has been mounted */
simon 0:560a6744936c 673 stat = disk_status(fs->drive);
simon 0:560a6744936c 674 if (!(stat & STA_NOINIT)) { /* and physical drive is kept initialized (has not been changed), */
simon 0:560a6744936c 675 #if !_FS_READONLY
simon 0:560a6744936c 676 if (chk_wp && (stat & STA_PROTECT)) /* Check write protection if needed */
simon 0:560a6744936c 677 return FR_WRITE_PROTECTED;
simon 0:560a6744936c 678 #endif
simon 0:560a6744936c 679 return FR_OK; /* The file system object is valid */
simon 0:560a6744936c 680 }
simon 0:560a6744936c 681 }
simon 0:560a6744936c 682
simon 0:560a6744936c 683 /* The logical drive must be re-mounted. Following code attempts to mount the logical drive */
simon 0:560a6744936c 684
simon 0:560a6744936c 685 memset(fs, 0, sizeof(FATFS)); /* Clean-up the file system object */
simon 0:560a6744936c 686 fs->drive = LD2PD(drv); /* Bind the logical drive and a physical drive */
simon 0:560a6744936c 687 stat = disk_initialize(fs->drive); /* Initialize low level disk I/O layer */
simon 0:560a6744936c 688 if (stat & STA_NOINIT) /* Check if the drive is ready */
simon 0:560a6744936c 689 return FR_NOT_READY;
simon 0:560a6744936c 690 #if S_MAX_SIZ > 512 /* Get disk sector size if needed */
simon 0:560a6744936c 691 if (disk_ioctl(drv, GET_SECTOR_SIZE, &SS(fs)) != RES_OK || SS(fs) > S_MAX_SIZ)
simon 0:560a6744936c 692 return FR_NO_FILESYSTEM;
simon 0:560a6744936c 693 #endif
simon 0:560a6744936c 694 #if !_FS_READONLY
simon 0:560a6744936c 695 if (chk_wp && (stat & STA_PROTECT)) /* Check write protection if needed */
simon 0:560a6744936c 696 return FR_WRITE_PROTECTED;
simon 0:560a6744936c 697 #endif
simon 0:560a6744936c 698 /* Search FAT partition on the drive */
simon 0:560a6744936c 699 fmt = check_fs(fs, bootsect = 0); /* Check sector 0 as an SFD format */
simon 0:560a6744936c 700 if (fmt == 1) { /* Not an FAT boot record, it may be patitioned */
simon 0:560a6744936c 701 /* Check a partition listed in top of the partition table */
simon 0:560a6744936c 702 tbl = &fs->win[MBR_Table + LD2PT(drv) * 16]; /* Partition table */
simon 0:560a6744936c 703 if (tbl[4]) { /* Is the partition existing? */
simon 0:560a6744936c 704 bootsect = LD_DWORD(&tbl[8]); /* Partition offset in LBA */
simon 0:560a6744936c 705 fmt = check_fs(fs, bootsect); /* Check the partition */
simon 0:560a6744936c 706 }
simon 0:560a6744936c 707 }
simon 0:560a6744936c 708 if (fmt || LD_WORD(&fs->win[BPB_BytsPerSec]) != SS(fs)) /* No valid FAT patition is found */
simon 0:560a6744936c 709 return FR_NO_FILESYSTEM;
simon 0:560a6744936c 710
simon 0:560a6744936c 711 /* Initialize the file system object */
simon 0:560a6744936c 712 fatsize = LD_WORD(&fs->win[BPB_FATSz16]); /* Number of sectors per FAT */
simon 0:560a6744936c 713 if (!fatsize) fatsize = LD_DWORD(&fs->win[BPB_FATSz32]);
simon 0:560a6744936c 714 fs->sects_fat = fatsize;
simon 0:560a6744936c 715 fs->n_fats = fs->win[BPB_NumFATs]; /* Number of FAT copies */
simon 0:560a6744936c 716 fatsize *= fs->n_fats; /* (Number of sectors in FAT area) */
simon 0:560a6744936c 717 fs->fatbase = bootsect + LD_WORD(&fs->win[BPB_RsvdSecCnt]); /* FAT start sector (lba) */
simon 0:560a6744936c 718 fs->csize = fs->win[BPB_SecPerClus]; /* Number of sectors per cluster */
simon 0:560a6744936c 719 fs->n_rootdir = LD_WORD(&fs->win[BPB_RootEntCnt]); /* Nmuber of root directory entries */
simon 0:560a6744936c 720 totalsect = LD_WORD(&fs->win[BPB_TotSec16]); /* Number of sectors on the file system */
simon 0:560a6744936c 721 if (!totalsect) totalsect = LD_DWORD(&fs->win[BPB_TotSec32]);
simon 0:560a6744936c 722 fs->max_clust = maxclust = (totalsect /* max_clust = Last cluster# + 1 */
simon 0:560a6744936c 723 - LD_WORD(&fs->win[BPB_RsvdSecCnt]) - fatsize - fs->n_rootdir / (SS(fs)/32)
simon 0:560a6744936c 724 ) / fs->csize + 2;
simon 0:560a6744936c 725
simon 0:560a6744936c 726 fmt = FS_FAT12; /* Determine the FAT sub type */
simon 0:560a6744936c 727 if (maxclust >= 0xFF7) fmt = FS_FAT16;
simon 0:560a6744936c 728 if (maxclust >= 0xFFF7) fmt = FS_FAT32;
simon 0:560a6744936c 729
simon 0:560a6744936c 730 if (fmt == FS_FAT32)
simon 0:560a6744936c 731 fs->dirbase = LD_DWORD(&fs->win[BPB_RootClus]); /* Root directory start cluster */
simon 0:560a6744936c 732 else
simon 0:560a6744936c 733 fs->dirbase = fs->fatbase + fatsize; /* Root directory start sector (lba) */
simon 0:560a6744936c 734 fs->database = fs->fatbase + fatsize + fs->n_rootdir / (SS(fs)/32); /* Data start sector (lba) */
simon 0:560a6744936c 735
simon 0:560a6744936c 736 #if !_FS_READONLY
simon 0:560a6744936c 737 /* Initialize allocation information */
simon 0:560a6744936c 738 fs->free_clust = 0xFFFFFFFF;
simon 0:560a6744936c 739 #if _USE_FSINFO
simon 0:560a6744936c 740 /* Get fsinfo if needed */
simon 0:560a6744936c 741 if (fmt == FS_FAT32) {
simon 0:560a6744936c 742 fs->fsi_sector = bootsect + LD_WORD(&fs->win[BPB_FSInfo]);
simon 0:560a6744936c 743 if (disk_read(fs->drive, fs->win, fs->fsi_sector, 1) == RES_OK &&
simon 0:560a6744936c 744 LD_WORD(&fs->win[BS_55AA]) == 0xAA55 &&
simon 0:560a6744936c 745 LD_DWORD(&fs->win[FSI_LeadSig]) == 0x41615252 &&
simon 0:560a6744936c 746 LD_DWORD(&fs->win[FSI_StrucSig]) == 0x61417272) {
simon 0:560a6744936c 747 fs->last_clust = LD_DWORD(&fs->win[FSI_Nxt_Free]);
simon 0:560a6744936c 748 fs->free_clust = LD_DWORD(&fs->win[FSI_Free_Count]);
simon 0:560a6744936c 749 }
simon 0:560a6744936c 750 }
simon 0:560a6744936c 751 #endif
simon 0:560a6744936c 752 #endif
simon 0:560a6744936c 753
simon 0:560a6744936c 754 fs->fs_type = fmt; /* FAT syb-type */
simon 0:560a6744936c 755 fs->id = ++fsid; /* File system mount ID */
simon 0:560a6744936c 756 return FR_OK;
simon 0:560a6744936c 757 }
simon 0:560a6744936c 758
simon 0:560a6744936c 759
simon 0:560a6744936c 760
simon 0:560a6744936c 761
simon 0:560a6744936c 762 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 763 /* Check if the file/dir object is valid or not */
simon 0:560a6744936c 764 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 765
simon 0:560a6744936c 766 static
simon 0:560a6744936c 767 FRESULT validate ( /* FR_OK(0): The object is valid, !=0: Invalid */
simon 0:560a6744936c 768 const FATFS *fs, /* Pointer to the file system object */
simon 0:560a6744936c 769 WORD id /* Member id of the target object to be checked */
simon 0:560a6744936c 770 )
simon 0:560a6744936c 771 {
simon 0:560a6744936c 772 if (!fs || !fs->fs_type || fs->id != id)
simon 0:560a6744936c 773 return FR_INVALID_OBJECT;
simon 0:560a6744936c 774 if (disk_status(fs->drive) & STA_NOINIT)
simon 0:560a6744936c 775 return FR_NOT_READY;
simon 0:560a6744936c 776
simon 0:560a6744936c 777 return FR_OK;
simon 0:560a6744936c 778 }
simon 0:560a6744936c 779
simon 0:560a6744936c 780
simon 0:560a6744936c 781
simon 0:560a6744936c 782
simon 0:560a6744936c 783 /*--------------------------------------------------------------------------
simon 0:560a6744936c 784
simon 0:560a6744936c 785 Public Functions
simon 0:560a6744936c 786
simon 0:560a6744936c 787 --------------------------------------------------------------------------*/
simon 0:560a6744936c 788
simon 0:560a6744936c 789
simon 0:560a6744936c 790
simon 0:560a6744936c 791 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 792 /* Mount/Unmount a Locical Drive */
simon 0:560a6744936c 793 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 794
simon 0:560a6744936c 795 FRESULT f_mount (
simon 0:560a6744936c 796 BYTE drv, /* Logical drive number to be mounted/unmounted */
simon 0:560a6744936c 797 FATFS *fs /* Pointer to new file system object (NULL for unmount)*/
simon 0:560a6744936c 798 )
simon 0:560a6744936c 799 {
simon 0:560a6744936c 800 if (drv >= _DRIVES) return FR_INVALID_DRIVE;
simon 0:560a6744936c 801
simon 0:560a6744936c 802 if (FatFs[drv]) FatFs[drv]->fs_type = 0; /* Clear old object */
simon 0:560a6744936c 803
simon 0:560a6744936c 804 FatFs[drv] = fs; /* Register and clear new object */
simon 0:560a6744936c 805 if (fs) fs->fs_type = 0;
simon 0:560a6744936c 806
simon 0:560a6744936c 807 return FR_OK;
simon 0:560a6744936c 808 }
simon 0:560a6744936c 809
simon 0:560a6744936c 810
simon 0:560a6744936c 811
simon 0:560a6744936c 812
simon 0:560a6744936c 813 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 814 /* Open or Create a File */
simon 0:560a6744936c 815 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 816
simon 0:560a6744936c 817 FRESULT f_open (
simon 0:560a6744936c 818 FIL *fp, /* Pointer to the blank file object */
simon 0:560a6744936c 819 const char *path, /* Pointer to the file name */
simon 0:560a6744936c 820 BYTE mode /* Access mode and file open mode flags */
simon 0:560a6744936c 821 )
simon 0:560a6744936c 822 {
simon 0:560a6744936c 823 FRESULT res;
simon 0:560a6744936c 824 FATFS_DIR dj;
simon 0:560a6744936c 825 BYTE *dir;
simon 0:560a6744936c 826 char fn[8+3+1];
simon 0:560a6744936c 827
simon 0:560a6744936c 828
simon 0:560a6744936c 829 fp->fs = NULL; /* Clear file object */
simon 0:560a6744936c 830 #if !_FS_READONLY
simon 0:560a6744936c 831 mode &= (FA_READ|FA_WRITE|FA_CREATE_ALWAYS|FA_OPEN_ALWAYS|FA_CREATE_NEW);
simon 0:560a6744936c 832 res = auto_mount(&path, &dj.fs, (BYTE)(mode & (FA_WRITE|FA_CREATE_ALWAYS|FA_OPEN_ALWAYS|FA_CREATE_NEW)));
simon 0:560a6744936c 833 #else
simon 0:560a6744936c 834 mode &= FA_READ;
simon 0:560a6744936c 835 res = auto_mount(&path, &dj.fs, 0);
simon 0:560a6744936c 836 #endif
simon 0:560a6744936c 837 if (res != FR_OK) return res;
simon 0:560a6744936c 838 res = trace_path(&dj, fn, path, &dir); /* Trace the file path */
simon 0:560a6744936c 839
simon 0:560a6744936c 840 #if !_FS_READONLY
simon 0:560a6744936c 841 /* Create or Open a file */
simon 0:560a6744936c 842 if (mode & (FA_CREATE_ALWAYS|FA_OPEN_ALWAYS|FA_CREATE_NEW)) {
simon 0:560a6744936c 843 DWORD ps, rs;
simon 0:560a6744936c 844 if (res != FR_OK) { /* No file, create new */
simon 0:560a6744936c 845 if (res != FR_NO_FILE) return res;
simon 0:560a6744936c 846 res = reserve_direntry(&dj, &dir);
simon 0:560a6744936c 847 if (res != FR_OK) return res;
simon 0:560a6744936c 848 memset(dir, 0, 32); /* Initialize the new entry with open name */
simon 0:560a6744936c 849 memcpy(&dir[DIR_Name], fn, 8+3);
simon 0:560a6744936c 850 dir[DIR_NTres] = fn[11];
simon 0:560a6744936c 851 mode |= FA_CREATE_ALWAYS;
simon 0:560a6744936c 852 }
simon 0:560a6744936c 853 else { /* Any object is already existing */
simon 0:560a6744936c 854 if (mode & FA_CREATE_NEW) /* Cannot create new */
simon 0:560a6744936c 855 return FR_EXIST;
simon 0:560a6744936c 856 if (!dir || (dir[DIR_Attr] & (AM_RDO|AM_DIR))) /* Cannot overwrite it (R/O or DIR) */
simon 0:560a6744936c 857 return FR_DENIED;
simon 0:560a6744936c 858 if (mode & FA_CREATE_ALWAYS) { /* Resize it to zero if needed */
simon 0:560a6744936c 859 rs = ((DWORD)LD_WORD(&dir[DIR_FstClusHI]) << 16) | LD_WORD(&dir[DIR_FstClusLO]); /* Get start cluster */
simon 0:560a6744936c 860 ST_WORD(&dir[DIR_FstClusHI], 0); /* cluster = 0 */
simon 0:560a6744936c 861 ST_WORD(&dir[DIR_FstClusLO], 0);
simon 0:560a6744936c 862 ST_DWORD(&dir[DIR_FileSize], 0); /* size = 0 */
simon 0:560a6744936c 863 dj.fs->winflag = 1;
simon 0:560a6744936c 864 ps = dj.fs->winsect; /* Remove the cluster chain */
simon 0:560a6744936c 865 if (!remove_chain(dj.fs, rs) || !move_window(dj.fs, ps))
simon 0:560a6744936c 866 return FR_RW_ERROR;
simon 0:560a6744936c 867 dj.fs->last_clust = rs - 1; /* Reuse the cluster hole */
simon 0:560a6744936c 868 }
simon 0:560a6744936c 869 }
simon 0:560a6744936c 870 if (mode & FA_CREATE_ALWAYS) {
simon 0:560a6744936c 871 dir[DIR_Attr] = 0; /* Reset attribute */
simon 0:560a6744936c 872 ps = get_fattime();
simon 0:560a6744936c 873 ST_DWORD(&dir[DIR_CrtTime], ps); /* Created time */
simon 0:560a6744936c 874 dj.fs->winflag = 1;
simon 0:560a6744936c 875 mode |= FA__WRITTEN; /* Set file changed flag */
simon 0:560a6744936c 876 }
simon 0:560a6744936c 877 }
simon 0:560a6744936c 878 /* Open an existing file */
simon 0:560a6744936c 879 else {
simon 0:560a6744936c 880 #endif /* !_FS_READONLY */
simon 0:560a6744936c 881 if (res != FR_OK) return res; /* Trace failed */
simon 0:560a6744936c 882 if (!dir || (dir[DIR_Attr] & AM_DIR)) /* It is a directory */
simon 0:560a6744936c 883 return FR_NO_FILE;
simon 0:560a6744936c 884 #if !_FS_READONLY
simon 0:560a6744936c 885 if ((mode & FA_WRITE) && (dir[DIR_Attr] & AM_RDO)) /* R/O violation */
simon 0:560a6744936c 886 return FR_DENIED;
simon 0:560a6744936c 887 }
simon 0:560a6744936c 888 fp->dir_sect = dj.fs->winsect; /* Pointer to the directory entry */
simon 0:560a6744936c 889 fp->dir_ptr = dir;
simon 0:560a6744936c 890 #endif
simon 0:560a6744936c 891 fp->flag = mode; /* File access mode */
simon 0:560a6744936c 892 fp->org_clust = /* File start cluster */
simon 0:560a6744936c 893 ((DWORD)LD_WORD(&dir[DIR_FstClusHI]) << 16) | LD_WORD(&dir[DIR_FstClusLO]);
simon 0:560a6744936c 894 fp->fsize = LD_DWORD(&dir[DIR_FileSize]); /* File size */
simon 0:560a6744936c 895 fp->fptr = 0; fp->csect = 255; /* File pointer */
simon 0:560a6744936c 896 fp->curr_sect = 0;
simon 0:560a6744936c 897 fp->fs = dj.fs; fp->id = dj.fs->id; /* Owner file system object of the file */
simon 0:560a6744936c 898
simon 0:560a6744936c 899 return FR_OK;
simon 0:560a6744936c 900 }
simon 0:560a6744936c 901
simon 0:560a6744936c 902
simon 0:560a6744936c 903
simon 0:560a6744936c 904
simon 0:560a6744936c 905 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 906 /* Read File */
simon 0:560a6744936c 907 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 908
simon 0:560a6744936c 909 FRESULT f_read (
simon 0:560a6744936c 910 FIL *fp, /* Pointer to the file object */
simon 0:560a6744936c 911 void *buff, /* Pointer to data buffer */
simon 0:560a6744936c 912 UINT btr, /* Number of bytes to read */
simon 0:560a6744936c 913 UINT *br /* Pointer to number of bytes read */
simon 0:560a6744936c 914 )
simon 0:560a6744936c 915 {
simon 0:560a6744936c 916 FRESULT res;
simon 0:560a6744936c 917 DWORD clust, sect, remain;
simon 0:560a6744936c 918 UINT rcnt, cc;
simon 0:560a6744936c 919 BYTE *rbuff = (BYTE*)buff;
simon 0:560a6744936c 920
simon 0:560a6744936c 921
simon 0:560a6744936c 922 *br = 0;
simon 0:560a6744936c 923 res = validate(fp->fs, fp->id); /* Check validity of the object */
simon 0:560a6744936c 924 if (res != FR_OK) return res;
simon 0:560a6744936c 925 if (fp->flag & FA__ERROR) return FR_RW_ERROR; /* Check error flag */
simon 0:560a6744936c 926 if (!(fp->flag & FA_READ)) return FR_DENIED; /* Check access mode */
simon 0:560a6744936c 927 remain = fp->fsize - fp->fptr;
simon 0:560a6744936c 928 if (btr > remain) btr = (UINT)remain; /* Truncate btr by remaining bytes */
simon 0:560a6744936c 929
simon 0:560a6744936c 930 for ( ; btr; /* Repeat until all data transferred */
simon 0:560a6744936c 931 rbuff += rcnt, fp->fptr += rcnt, *br += rcnt, btr -= rcnt) {
simon 0:560a6744936c 932 if ((fp->fptr % SS(fp->fs)) == 0) { /* On the sector boundary? */
simon 0:560a6744936c 933 if (fp->csect >= fp->fs->csize) { /* On the cluster boundary? */
simon 0:560a6744936c 934 clust = (fp->fptr == 0) ? /* On the top of the file? */
simon 0:560a6744936c 935 fp->org_clust : get_cluster(fp->fs, fp->curr_clust);
simon 0:560a6744936c 936 if (clust < 2 || clust >= fp->fs->max_clust) goto fr_error;
simon 0:560a6744936c 937 fp->curr_clust = clust; /* Update current cluster */
simon 0:560a6744936c 938 fp->csect = 0; /* Reset sector address in the cluster */
simon 0:560a6744936c 939 }
simon 0:560a6744936c 940 sect = clust2sect(fp->fs, fp->curr_clust) + fp->csect; /* Get current sector */
simon 0:560a6744936c 941 cc = btr / SS(fp->fs); /* When remaining bytes >= sector size, */
simon 0:560a6744936c 942 if (cc) { /* Read maximum contiguous sectors directly */
simon 0:560a6744936c 943 if (fp->csect + cc > fp->fs->csize) /* Clip at cluster boundary */
simon 0:560a6744936c 944 cc = fp->fs->csize - fp->csect;
simon 0:560a6744936c 945 if (disk_read(fp->fs->drive, rbuff, sect, (BYTE)cc) != RES_OK)
simon 0:560a6744936c 946 goto fr_error;
simon 0:560a6744936c 947 fp->csect += (BYTE)cc; /* Next sector address in the cluster */
simon 0:560a6744936c 948 rcnt = SS(fp->fs) * cc; /* Number of bytes transferred */
simon 0:560a6744936c 949 continue;
simon 0:560a6744936c 950 }
simon 0:560a6744936c 951 if (sect != fp->curr_sect) { /* Is window offset changed? */
simon 0:560a6744936c 952 #if !_FS_READONLY
simon 0:560a6744936c 953 if (fp->flag & FA__DIRTY) { /* Write back file I/O buffer if needed */
simon 0:560a6744936c 954 if (disk_write(fp->fs->drive, fp->buffer, fp->curr_sect, 1) != RES_OK)
simon 0:560a6744936c 955 goto fr_error;
simon 0:560a6744936c 956 fp->flag &= (BYTE)~FA__DIRTY;
simon 0:560a6744936c 957 }
simon 0:560a6744936c 958 #endif
simon 0:560a6744936c 959 if (disk_read(fp->fs->drive, fp->buffer, sect, 1) != RES_OK) /* Fill file I/O buffer with file data */
simon 0:560a6744936c 960 goto fr_error;
simon 0:560a6744936c 961 fp->curr_sect = sect;
simon 0:560a6744936c 962 }
simon 0:560a6744936c 963 fp->csect++; /* Next sector address in the cluster */
simon 0:560a6744936c 964 }
simon 0:560a6744936c 965 rcnt = SS(fp->fs) - (fp->fptr % SS(fp->fs)); /* Get partial sector from file I/O buffer */
simon 0:560a6744936c 966 if (rcnt > btr) rcnt = btr;
simon 0:560a6744936c 967 memcpy(rbuff, &fp->buffer[fp->fptr % SS(fp->fs)], rcnt);
simon 0:560a6744936c 968 }
simon 0:560a6744936c 969
simon 0:560a6744936c 970 return FR_OK;
simon 0:560a6744936c 971
simon 0:560a6744936c 972 fr_error: /* Abort this file due to an unrecoverable error */
simon 0:560a6744936c 973 fp->flag |= FA__ERROR;
simon 0:560a6744936c 974 return FR_RW_ERROR;
simon 0:560a6744936c 975 }
simon 0:560a6744936c 976
simon 0:560a6744936c 977
simon 0:560a6744936c 978
simon 0:560a6744936c 979
simon 0:560a6744936c 980 #if !_FS_READONLY
simon 0:560a6744936c 981 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 982 /* Write File */
simon 0:560a6744936c 983 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 984
simon 0:560a6744936c 985 FRESULT f_write (
simon 0:560a6744936c 986 FIL *fp, /* Pointer to the file object */
simon 0:560a6744936c 987 const void *buff, /* Pointer to the data to be written */
simon 0:560a6744936c 988 UINT btw, /* Number of bytes to write */
simon 0:560a6744936c 989 UINT *bw /* Pointer to number of bytes written */
simon 0:560a6744936c 990 )
simon 0:560a6744936c 991 {
simon 0:560a6744936c 992 FRESULT res;
simon 0:560a6744936c 993 DWORD clust, sect;
simon 0:560a6744936c 994 UINT wcnt, cc;
simon 0:560a6744936c 995 const BYTE *wbuff = (const BYTE*)buff;
simon 0:560a6744936c 996
simon 0:560a6744936c 997
simon 0:560a6744936c 998 *bw = 0;
simon 0:560a6744936c 999 res = validate(fp->fs, fp->id); /* Check validity of the object */
simon 0:560a6744936c 1000 if (res != FR_OK) return res;
simon 0:560a6744936c 1001 if (fp->flag & FA__ERROR) return FR_RW_ERROR; /* Check error flag */
simon 0:560a6744936c 1002 if (!(fp->flag & FA_WRITE)) return FR_DENIED; /* Check access mode */
simon 0:560a6744936c 1003 if (fp->fsize + btw < fp->fsize) return FR_OK; /* File size cannot reach 4GB */
simon 0:560a6744936c 1004
simon 0:560a6744936c 1005 for ( ; btw; /* Repeat until all data transferred */
simon 0:560a6744936c 1006 wbuff += wcnt, fp->fptr += wcnt, *bw += wcnt, btw -= wcnt) {
simon 0:560a6744936c 1007 if ((fp->fptr % SS(fp->fs)) == 0) { /* On the sector boundary? */
simon 0:560a6744936c 1008 if (fp->csect >= fp->fs->csize) { /* On the cluster boundary? */
simon 0:560a6744936c 1009 if (fp->fptr == 0) { /* On the top of the file? */
simon 0:560a6744936c 1010 clust = fp->org_clust; /* Follow from the origin */
simon 0:560a6744936c 1011 if (clust == 0) /* When there is no cluster chain, */
simon 0:560a6744936c 1012 fp->org_clust = clust = create_chain(fp->fs, 0); /* Create a new cluster chain */
simon 0:560a6744936c 1013 } else { /* Middle or end of the file */
simon 0:560a6744936c 1014 clust = create_chain(fp->fs, fp->curr_clust); /* Trace or streach cluster chain */
simon 0:560a6744936c 1015 }
simon 0:560a6744936c 1016 if (clust == 0) break; /* Could not allocate a new cluster (disk full) */
simon 0:560a6744936c 1017 if (clust == 1 || clust >= fp->fs->max_clust) goto fw_error;
simon 0:560a6744936c 1018 fp->curr_clust = clust; /* Update current cluster */
simon 0:560a6744936c 1019 fp->csect = 0; /* Reset sector address in the cluster */
simon 0:560a6744936c 1020 }
simon 0:560a6744936c 1021 sect = clust2sect(fp->fs, fp->curr_clust) + fp->csect; /* Get current sector */
simon 0:560a6744936c 1022 cc = btw / SS(fp->fs); /* When remaining bytes >= sector size, */
simon 0:560a6744936c 1023 if (cc) { /* Write maximum contiguous sectors directly */
simon 0:560a6744936c 1024 if (fp->csect + cc > fp->fs->csize) /* Clip at cluster boundary */
simon 0:560a6744936c 1025 cc = fp->fs->csize - fp->csect;
simon 0:560a6744936c 1026 if (disk_write(fp->fs->drive, wbuff, sect, (BYTE)cc) != RES_OK)
simon 0:560a6744936c 1027 goto fw_error;
simon 0:560a6744936c 1028 fp->csect += (BYTE)cc; /* Next sector address in the cluster */
simon 0:560a6744936c 1029 wcnt = SS(fp->fs) * cc; /* Number of bytes transferred */
simon 0:560a6744936c 1030 continue;
simon 0:560a6744936c 1031 }
simon 0:560a6744936c 1032 if (sect != fp->curr_sect) { /* Is window offset changed? */
simon 0:560a6744936c 1033 if (fp->flag & FA__DIRTY) { /* Write back file I/O buffer if needed */
simon 0:560a6744936c 1034 if (disk_write(fp->fs->drive, fp->buffer, fp->curr_sect, 1) != RES_OK)
simon 0:560a6744936c 1035 goto fw_error;
simon 0:560a6744936c 1036 fp->flag &= (BYTE)~FA__DIRTY;
simon 0:560a6744936c 1037 }
simon 0:560a6744936c 1038 if (fp->fptr < fp->fsize && /* Fill file I/O buffer with file data */
simon 0:560a6744936c 1039 disk_read(fp->fs->drive, fp->buffer, sect, 1) != RES_OK)
simon 0:560a6744936c 1040 goto fw_error;
simon 0:560a6744936c 1041 fp->curr_sect = sect;
simon 0:560a6744936c 1042 }
simon 0:560a6744936c 1043 fp->csect++; /* Next sector address in the cluster */
simon 0:560a6744936c 1044 }
simon 0:560a6744936c 1045 wcnt = SS(fp->fs) - (fp->fptr % SS(fp->fs)); /* Put partial sector into file I/O buffer */
simon 0:560a6744936c 1046 if (wcnt > btw) wcnt = btw;
simon 0:560a6744936c 1047 memcpy(&fp->buffer[fp->fptr % SS(fp->fs)], wbuff, wcnt);
simon 0:560a6744936c 1048 fp->flag |= FA__DIRTY;
simon 0:560a6744936c 1049 }
simon 0:560a6744936c 1050
simon 0:560a6744936c 1051 if (fp->fptr > fp->fsize) fp->fsize = fp->fptr; /* Update file size if needed */
simon 0:560a6744936c 1052 fp->flag |= FA__WRITTEN; /* Set file changed flag */
simon 0:560a6744936c 1053 return FR_OK;
simon 0:560a6744936c 1054
simon 0:560a6744936c 1055 fw_error: /* Abort this file due to an unrecoverable error */
simon 0:560a6744936c 1056 fp->flag |= FA__ERROR;
simon 0:560a6744936c 1057 return FR_RW_ERROR;
simon 0:560a6744936c 1058 }
simon 0:560a6744936c 1059
simon 0:560a6744936c 1060
simon 0:560a6744936c 1061
simon 0:560a6744936c 1062
simon 0:560a6744936c 1063 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 1064 /* Synchronize the file object */
simon 0:560a6744936c 1065 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 1066
simon 0:560a6744936c 1067 FRESULT f_sync (
simon 0:560a6744936c 1068 FIL *fp /* Pointer to the file object */
simon 0:560a6744936c 1069 )
simon 0:560a6744936c 1070 {
simon 0:560a6744936c 1071 FRESULT res;
simon 0:560a6744936c 1072 DWORD tim;
simon 0:560a6744936c 1073 BYTE *dir;
simon 0:560a6744936c 1074
simon 0:560a6744936c 1075
simon 0:560a6744936c 1076 res = validate(fp->fs, fp->id); /* Check validity of the object */
simon 0:560a6744936c 1077 if (res == FR_OK) {
simon 0:560a6744936c 1078 if (fp->flag & FA__WRITTEN) { /* Has the file been written? */
simon 0:560a6744936c 1079 /* Write back data buffer if needed */
simon 0:560a6744936c 1080 if (fp->flag & FA__DIRTY) {
simon 0:560a6744936c 1081 if (disk_write(fp->fs->drive, fp->buffer, fp->curr_sect, 1) != RES_OK)
simon 0:560a6744936c 1082 return FR_RW_ERROR;
simon 0:560a6744936c 1083 fp->flag &= (BYTE)~FA__DIRTY;
simon 0:560a6744936c 1084 }
simon 0:560a6744936c 1085 /* Update the directory entry */
simon 0:560a6744936c 1086 if (!move_window(fp->fs, fp->dir_sect))
simon 0:560a6744936c 1087 return FR_RW_ERROR;
simon 0:560a6744936c 1088 dir = fp->dir_ptr;
simon 0:560a6744936c 1089 dir[DIR_Attr] |= AM_ARC; /* Set archive bit */
simon 0:560a6744936c 1090 ST_DWORD(&dir[DIR_FileSize], fp->fsize); /* Update file size */
simon 0:560a6744936c 1091 ST_WORD(&dir[DIR_FstClusLO], fp->org_clust); /* Update start cluster */
simon 0:560a6744936c 1092 ST_WORD(&dir[DIR_FstClusHI], fp->org_clust >> 16);
simon 0:560a6744936c 1093 tim = get_fattime(); /* Updated time */
simon 0:560a6744936c 1094 ST_DWORD(&dir[DIR_WrtTime], tim);
simon 0:560a6744936c 1095 fp->flag &= (BYTE)~FA__WRITTEN;
simon 0:560a6744936c 1096 res = sync(fp->fs);
simon 0:560a6744936c 1097 }
simon 0:560a6744936c 1098 }
simon 0:560a6744936c 1099 return res;
simon 0:560a6744936c 1100 }
simon 0:560a6744936c 1101
simon 0:560a6744936c 1102 #endif /* !_FS_READONLY */
simon 0:560a6744936c 1103
simon 0:560a6744936c 1104
simon 0:560a6744936c 1105
simon 0:560a6744936c 1106
simon 0:560a6744936c 1107 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 1108 /* Close File */
simon 0:560a6744936c 1109 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 1110
simon 0:560a6744936c 1111 FRESULT f_close (
simon 0:560a6744936c 1112 FIL *fp /* Pointer to the file object to be closed */
simon 0:560a6744936c 1113 )
simon 0:560a6744936c 1114 {
simon 0:560a6744936c 1115 FRESULT res;
simon 0:560a6744936c 1116
simon 0:560a6744936c 1117
simon 0:560a6744936c 1118 #if !_FS_READONLY
simon 0:560a6744936c 1119 res = f_sync(fp);
simon 0:560a6744936c 1120 #else
simon 0:560a6744936c 1121 res = validate(fp->fs, fp->id);
simon 0:560a6744936c 1122 #endif
simon 0:560a6744936c 1123 if (res == FR_OK) fp->fs = NULL;
simon 0:560a6744936c 1124 return res;
simon 0:560a6744936c 1125 }
simon 0:560a6744936c 1126
simon 0:560a6744936c 1127
simon 0:560a6744936c 1128
simon 0:560a6744936c 1129
simon 0:560a6744936c 1130 #if _FS_MINIMIZE <= 2
simon 0:560a6744936c 1131 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 1132 /* Seek File R/W Pointer */
simon 0:560a6744936c 1133 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 1134
simon 0:560a6744936c 1135 FRESULT f_lseek (
simon 0:560a6744936c 1136 FIL *fp, /* Pointer to the file object */
simon 0:560a6744936c 1137 DWORD ofs /* File pointer from top of file */
simon 0:560a6744936c 1138 )
simon 0:560a6744936c 1139 {
simon 0:560a6744936c 1140 FRESULT res;
simon 0:560a6744936c 1141 DWORD clust, csize, nsect, ifptr;
simon 0:560a6744936c 1142
simon 0:560a6744936c 1143
simon 0:560a6744936c 1144 res = validate(fp->fs, fp->id); /* Check validity of the object */
simon 0:560a6744936c 1145 if (res != FR_OK) return res;
simon 0:560a6744936c 1146 if (fp->flag & FA__ERROR) return FR_RW_ERROR;
simon 0:560a6744936c 1147 if (ofs > fp->fsize /* In read-only mode, clip offset with the file size */
simon 0:560a6744936c 1148 #if !_FS_READONLY
simon 0:560a6744936c 1149 && !(fp->flag & FA_WRITE)
simon 0:560a6744936c 1150 #endif
simon 0:560a6744936c 1151 ) ofs = fp->fsize;
simon 0:560a6744936c 1152
simon 0:560a6744936c 1153 ifptr = fp->fptr;
simon 0:560a6744936c 1154 fp->fptr = 0; fp->csect = 255;
simon 0:560a6744936c 1155 nsect = 0;
simon 0:560a6744936c 1156 if (ofs > 0) {
simon 0:560a6744936c 1157 csize = (DWORD)fp->fs->csize * SS(fp->fs); /* Cluster size (byte) */
simon 0:560a6744936c 1158 if (ifptr > 0 &&
simon 0:560a6744936c 1159 (ofs - 1) / csize >= (ifptr - 1) / csize) {/* When seek to same or following cluster, */
simon 0:560a6744936c 1160 fp->fptr = (ifptr - 1) & ~(csize - 1); /* start from the current cluster */
simon 0:560a6744936c 1161 ofs -= fp->fptr;
simon 0:560a6744936c 1162 clust = fp->curr_clust;
simon 0:560a6744936c 1163 } else { /* When seek to back cluster, */
simon 0:560a6744936c 1164 clust = fp->org_clust; /* start from the first cluster */
simon 0:560a6744936c 1165 #if !_FS_READONLY
simon 0:560a6744936c 1166 if (clust == 0) { /* If no cluster chain, create a new chain */
simon 0:560a6744936c 1167 clust = create_chain(fp->fs, 0);
simon 0:560a6744936c 1168 if (clust == 1) goto fk_error;
simon 0:560a6744936c 1169 fp->org_clust = clust;
simon 0:560a6744936c 1170 }
simon 0:560a6744936c 1171 #endif
simon 0:560a6744936c 1172 fp->curr_clust = clust;
simon 0:560a6744936c 1173 }
simon 0:560a6744936c 1174 if (clust != 0) {
simon 0:560a6744936c 1175 while (ofs > csize) { /* Cluster following loop */
simon 0:560a6744936c 1176 #if !_FS_READONLY
simon 0:560a6744936c 1177 if (fp->flag & FA_WRITE) { /* Check if in write mode or not */
simon 0:560a6744936c 1178 clust = create_chain(fp->fs, clust); /* Force streached if in write mode */
simon 0:560a6744936c 1179 if (clust == 0) { /* When disk gets full, clip file size */
simon 0:560a6744936c 1180 ofs = csize; break;
simon 0:560a6744936c 1181 }
simon 0:560a6744936c 1182 } else
simon 0:560a6744936c 1183 #endif
simon 0:560a6744936c 1184 clust = get_cluster(fp->fs, clust); /* Follow cluster chain if not in write mode */
simon 0:560a6744936c 1185 if (clust < 2 || clust >= fp->fs->max_clust) goto fk_error;
simon 0:560a6744936c 1186 fp->curr_clust = clust;
simon 0:560a6744936c 1187 fp->fptr += csize;
simon 0:560a6744936c 1188 ofs -= csize;
simon 0:560a6744936c 1189 }
simon 0:560a6744936c 1190 fp->fptr += ofs;
simon 0:560a6744936c 1191 fp->csect = (BYTE)(ofs / SS(fp->fs)); /* Sector offset in the cluster */
simon 0:560a6744936c 1192 if (ofs & (SS(fp->fs) - 1)) {
simon 0:560a6744936c 1193 nsect = clust2sect(fp->fs, clust) + fp->csect; /* Current sector */
simon 0:560a6744936c 1194 fp->csect++;
simon 0:560a6744936c 1195 }
simon 0:560a6744936c 1196 }
simon 0:560a6744936c 1197 }
simon 0:560a6744936c 1198 if (nsect && nsect != fp->curr_sect) {
simon 0:560a6744936c 1199 #if !_FS_READONLY
simon 0:560a6744936c 1200 if (fp->flag & FA__DIRTY) { /* Write-back dirty buffer if needed */
simon 0:560a6744936c 1201 if (disk_write(fp->fs->drive, fp->buffer, fp->curr_sect, 1) != RES_OK)
simon 0:560a6744936c 1202 goto fk_error;
simon 0:560a6744936c 1203 fp->flag &= (BYTE)~FA__DIRTY;
simon 0:560a6744936c 1204 }
simon 0:560a6744936c 1205 #endif
simon 0:560a6744936c 1206 if (disk_read(fp->fs->drive, fp->buffer, nsect, 1) != RES_OK)
simon 0:560a6744936c 1207 goto fk_error;
simon 0:560a6744936c 1208 fp->curr_sect = nsect;
simon 0:560a6744936c 1209 }
simon 0:560a6744936c 1210
simon 0:560a6744936c 1211 #if !_FS_READONLY
simon 0:560a6744936c 1212 if (fp->fptr > fp->fsize) { /* Set changed flag if the file was extended */
simon 0:560a6744936c 1213 fp->fsize = fp->fptr;
simon 0:560a6744936c 1214 fp->flag |= FA__WRITTEN;
simon 0:560a6744936c 1215 }
simon 0:560a6744936c 1216 #endif
simon 0:560a6744936c 1217
simon 0:560a6744936c 1218 return FR_OK;
simon 0:560a6744936c 1219
simon 0:560a6744936c 1220 fk_error: /* Abort this file due to an unrecoverable error */
simon 0:560a6744936c 1221 fp->flag |= FA__ERROR;
simon 0:560a6744936c 1222 return FR_RW_ERROR;
simon 0:560a6744936c 1223 }
simon 0:560a6744936c 1224
simon 0:560a6744936c 1225
simon 0:560a6744936c 1226
simon 0:560a6744936c 1227
simon 0:560a6744936c 1228 #if _FS_MINIMIZE <= 1
simon 0:560a6744936c 1229 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 1230 /* Create a directroy object */
simon 0:560a6744936c 1231 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 1232
simon 0:560a6744936c 1233 FRESULT f_opendir (
simon 0:560a6744936c 1234 FATFS_DIR *dj, /* Pointer to directory object to create */
simon 0:560a6744936c 1235 const char *path /* Pointer to the directory path */
simon 0:560a6744936c 1236 )
simon 0:560a6744936c 1237 {
simon 0:560a6744936c 1238 FRESULT res;
simon 0:560a6744936c 1239 BYTE *dir;
simon 0:560a6744936c 1240 char fn[8+3+1];
simon 0:560a6744936c 1241
simon 0:560a6744936c 1242
simon 0:560a6744936c 1243 res = auto_mount(&path, &dj->fs, 0);
simon 0:560a6744936c 1244 if (res == FR_OK) {
simon 0:560a6744936c 1245 res = trace_path(dj, fn, path, &dir); /* Trace the directory path */
simon 0:560a6744936c 1246 if (res == FR_OK) { /* Trace completed */
simon 0:560a6744936c 1247 if (dir) { /* It is not the root dir */
simon 0:560a6744936c 1248 if (dir[DIR_Attr] & AM_DIR) { /* The entry is a directory */
simon 0:560a6744936c 1249 dj->clust = ((DWORD)LD_WORD(&dir[DIR_FstClusHI]) << 16) | LD_WORD(&dir[DIR_FstClusLO]);
simon 0:560a6744936c 1250 dj->sect = clust2sect(dj->fs, dj->clust);
simon 0:560a6744936c 1251 dj->index = 2;
simon 0:560a6744936c 1252 } else { /* The entry is not a directory */
simon 0:560a6744936c 1253 res = FR_NO_FILE;
simon 0:560a6744936c 1254 }
simon 0:560a6744936c 1255 }
simon 0:560a6744936c 1256 dj->id = dj->fs->id;
simon 0:560a6744936c 1257 }
simon 0:560a6744936c 1258 }
simon 0:560a6744936c 1259
simon 0:560a6744936c 1260 return res;
simon 0:560a6744936c 1261 }
simon 0:560a6744936c 1262
simon 0:560a6744936c 1263
simon 0:560a6744936c 1264
simon 0:560a6744936c 1265
simon 0:560a6744936c 1266 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 1267 /* Read Directory Entry in Sequense */
simon 0:560a6744936c 1268 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 1269
simon 0:560a6744936c 1270 FRESULT f_readdir (
simon 0:560a6744936c 1271 FATFS_DIR *dj, /* Pointer to the directory object */
simon 0:560a6744936c 1272 FILINFO *finfo /* Pointer to file information to return */
simon 0:560a6744936c 1273 )
simon 0:560a6744936c 1274 {
simon 0:560a6744936c 1275 BYTE *dir, c;
simon 0:560a6744936c 1276 FRESULT res;
simon 0:560a6744936c 1277
simon 0:560a6744936c 1278 res = validate(dj->fs, dj->id); /* Check validity of the object */
simon 0:560a6744936c 1279 if (res != FR_OK) return res;
simon 0:560a6744936c 1280
simon 0:560a6744936c 1281 finfo->fname[0] = 0;
simon 0:560a6744936c 1282 while (dj->sect) {
simon 0:560a6744936c 1283 if (!move_window(dj->fs, dj->sect))
simon 0:560a6744936c 1284 return FR_RW_ERROR;
simon 0:560a6744936c 1285 dir = &dj->fs->win[(dj->index & ((SS(dj->fs) - 1) >> 5)) * 32]; /* pointer to the directory entry */
simon 0:560a6744936c 1286 c = dir[DIR_Name];
simon 0:560a6744936c 1287 if (c == 0) break; /* Has it reached to end of dir? */
simon 0:560a6744936c 1288 if (c != 0xE5 && !(dir[DIR_Attr] & AM_VOL)) /* Is it a valid entry? */
simon 0:560a6744936c 1289 get_fileinfo(finfo, dir);
simon 0:560a6744936c 1290 if (!next_dir_entry(dj)) dj->sect = 0; /* Next entry */
simon 0:560a6744936c 1291 if (finfo->fname[0]) break; /* Found valid entry */
simon 0:560a6744936c 1292 }
simon 0:560a6744936c 1293
simon 0:560a6744936c 1294 return FR_OK;
simon 0:560a6744936c 1295 }
simon 0:560a6744936c 1296
simon 0:560a6744936c 1297
simon 0:560a6744936c 1298
simon 0:560a6744936c 1299
simon 0:560a6744936c 1300 #if _FS_MINIMIZE == 0
simon 0:560a6744936c 1301 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 1302 /* Get File Status */
simon 0:560a6744936c 1303 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 1304
simon 0:560a6744936c 1305 FRESULT f_stat (
simon 0:560a6744936c 1306 const char *path, /* Pointer to the file path */
simon 0:560a6744936c 1307 FILINFO *finfo /* Pointer to file information to return */
simon 0:560a6744936c 1308 )
simon 0:560a6744936c 1309 {
simon 0:560a6744936c 1310 FRESULT res;
simon 0:560a6744936c 1311 FATFS_DIR dj;
simon 0:560a6744936c 1312 BYTE *dir;
simon 0:560a6744936c 1313 char fn[8+3+1];
simon 0:560a6744936c 1314
simon 0:560a6744936c 1315
simon 0:560a6744936c 1316 res = auto_mount(&path, &dj.fs, 0);
simon 0:560a6744936c 1317 if (res == FR_OK) {
simon 0:560a6744936c 1318 res = trace_path(&dj, fn, path, &dir); /* Trace the file path */
simon 0:560a6744936c 1319 if (res == FR_OK) { /* Trace completed */
simon 0:560a6744936c 1320 if (dir) /* Found an object */
simon 0:560a6744936c 1321 get_fileinfo(finfo, dir);
simon 0:560a6744936c 1322 else /* It is root dir */
simon 0:560a6744936c 1323 res = FR_INVALID_NAME;
simon 0:560a6744936c 1324 }
simon 0:560a6744936c 1325 }
simon 0:560a6744936c 1326
simon 0:560a6744936c 1327 return res;
simon 0:560a6744936c 1328 }
simon 0:560a6744936c 1329
simon 0:560a6744936c 1330
simon 0:560a6744936c 1331
simon 0:560a6744936c 1332 #if !_FS_READONLY
simon 0:560a6744936c 1333 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 1334 /* Truncate File */
simon 0:560a6744936c 1335 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 1336
simon 0:560a6744936c 1337 FRESULT f_truncate (
simon 0:560a6744936c 1338 FIL *fp /* Pointer to the file object */
simon 0:560a6744936c 1339 )
simon 0:560a6744936c 1340 {
simon 0:560a6744936c 1341 FRESULT res;
simon 0:560a6744936c 1342 DWORD ncl;
simon 0:560a6744936c 1343
simon 0:560a6744936c 1344
simon 0:560a6744936c 1345 res = validate(fp->fs, fp->id); /* Check validity of the object */
simon 0:560a6744936c 1346 if (res != FR_OK) return res;
simon 0:560a6744936c 1347 if (fp->flag & FA__ERROR) return FR_RW_ERROR; /* Check error flag */
simon 0:560a6744936c 1348 if (!(fp->flag & FA_WRITE)) return FR_DENIED; /* Check access mode */
simon 0:560a6744936c 1349
simon 0:560a6744936c 1350 if (fp->fsize > fp->fptr) {
simon 0:560a6744936c 1351 fp->fsize = fp->fptr; /* Set file size to current R/W point */
simon 0:560a6744936c 1352 fp->flag |= FA__WRITTEN;
simon 0:560a6744936c 1353 if (fp->fptr == 0) { /* When set file size to zero, remove entire cluster chain */
simon 0:560a6744936c 1354 if (!remove_chain(fp->fs, fp->org_clust)) goto ft_error;
simon 0:560a6744936c 1355 fp->org_clust = 0;
simon 0:560a6744936c 1356 } else { /* When truncate a part of the file, remove remaining clusters */
simon 0:560a6744936c 1357 ncl = get_cluster(fp->fs, fp->curr_clust);
simon 0:560a6744936c 1358 if (ncl < 2) goto ft_error;
simon 0:560a6744936c 1359 if (ncl < fp->fs->max_clust) {
simon 0:560a6744936c 1360 if (!put_cluster(fp->fs, fp->curr_clust, 0x0FFFFFFF)) goto ft_error;
simon 0:560a6744936c 1361 if (!remove_chain(fp->fs, ncl)) goto ft_error;
simon 0:560a6744936c 1362 }
simon 0:560a6744936c 1363 }
simon 0:560a6744936c 1364 }
simon 0:560a6744936c 1365
simon 0:560a6744936c 1366 return FR_OK;
simon 0:560a6744936c 1367
simon 0:560a6744936c 1368 ft_error: /* Abort this file due to an unrecoverable error */
simon 0:560a6744936c 1369 fp->flag |= FA__ERROR;
simon 0:560a6744936c 1370 return FR_RW_ERROR;
simon 0:560a6744936c 1371 }
simon 0:560a6744936c 1372
simon 0:560a6744936c 1373
simon 0:560a6744936c 1374
simon 0:560a6744936c 1375
simon 0:560a6744936c 1376 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 1377 /* Get Number of Free Clusters */
simon 0:560a6744936c 1378 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 1379
simon 0:560a6744936c 1380 FRESULT f_getfree (
simon 0:560a6744936c 1381 const char *drv, /* Pointer to the logical drive number (root dir) */
simon 0:560a6744936c 1382 DWORD *nclust, /* Pointer to the variable to return number of free clusters */
simon 0:560a6744936c 1383 FATFS **fatfs /* Pointer to pointer to corresponding file system object to return */
simon 0:560a6744936c 1384 )
simon 0:560a6744936c 1385 {
simon 0:560a6744936c 1386 FRESULT res;
simon 0:560a6744936c 1387 DWORD n, clust, sect;
simon 0:560a6744936c 1388 BYTE fat, f, *p;
simon 0:560a6744936c 1389
simon 0:560a6744936c 1390
simon 0:560a6744936c 1391 /* Get drive number */
simon 0:560a6744936c 1392 res = auto_mount(&drv, fatfs, 0);
simon 0:560a6744936c 1393 if (res != FR_OK) return res;
simon 0:560a6744936c 1394
simon 0:560a6744936c 1395 /* If number of free cluster is valid, return it without cluster scan. */
simon 0:560a6744936c 1396 if ((*fatfs)->free_clust <= (*fatfs)->max_clust - 2) {
simon 0:560a6744936c 1397 *nclust = (*fatfs)->free_clust;
simon 0:560a6744936c 1398 return FR_OK;
simon 0:560a6744936c 1399 }
simon 0:560a6744936c 1400
simon 0:560a6744936c 1401 /* Get number of free clusters */
simon 0:560a6744936c 1402 fat = (*fatfs)->fs_type;
simon 0:560a6744936c 1403 n = 0;
simon 0:560a6744936c 1404 if (fat == FS_FAT12) {
simon 0:560a6744936c 1405 clust = 2;
simon 0:560a6744936c 1406 do {
simon 0:560a6744936c 1407 if ((WORD)get_cluster(*fatfs, clust) == 0) n++;
simon 0:560a6744936c 1408 } while (++clust < (*fatfs)->max_clust);
simon 0:560a6744936c 1409 } else {
simon 0:560a6744936c 1410 clust = (*fatfs)->max_clust;
simon 0:560a6744936c 1411 sect = (*fatfs)->fatbase;
simon 0:560a6744936c 1412 f = 0; p = 0;
simon 0:560a6744936c 1413 do {
simon 0:560a6744936c 1414 if (!f) {
simon 0:560a6744936c 1415 if (!move_window(*fatfs, sect++)) return FR_RW_ERROR;
simon 0:560a6744936c 1416 p = (*fatfs)->win;
simon 0:560a6744936c 1417 }
simon 0:560a6744936c 1418 if (fat == FS_FAT16) {
simon 0:560a6744936c 1419 if (LD_WORD(p) == 0) n++;
simon 0:560a6744936c 1420 p += 2; f += 1;
simon 0:560a6744936c 1421 } else {
simon 0:560a6744936c 1422 if (LD_DWORD(p) == 0) n++;
simon 0:560a6744936c 1423 p += 4; f += 2;
simon 0:560a6744936c 1424 }
simon 0:560a6744936c 1425 } while (--clust);
simon 0:560a6744936c 1426 }
simon 0:560a6744936c 1427 (*fatfs)->free_clust = n;
simon 0:560a6744936c 1428 #if _USE_FSINFO
simon 0:560a6744936c 1429 if (fat == FS_FAT32) (*fatfs)->fsi_flag = 1;
simon 0:560a6744936c 1430 #endif
simon 0:560a6744936c 1431
simon 0:560a6744936c 1432 *nclust = n;
simon 0:560a6744936c 1433 return FR_OK;
simon 0:560a6744936c 1434 }
simon 0:560a6744936c 1435
simon 0:560a6744936c 1436
simon 0:560a6744936c 1437
simon 0:560a6744936c 1438
simon 0:560a6744936c 1439 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 1440 /* Delete a File or Directory */
simon 0:560a6744936c 1441 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 1442
simon 0:560a6744936c 1443 FRESULT f_unlink (
simon 0:560a6744936c 1444 const char *path /* Pointer to the file or directory path */
simon 0:560a6744936c 1445 )
simon 0:560a6744936c 1446 {
simon 0:560a6744936c 1447 FRESULT res;
simon 0:560a6744936c 1448 FATFS_DIR dj;
simon 0:560a6744936c 1449 BYTE *dir, *sdir;
simon 0:560a6744936c 1450 DWORD dclust, dsect;
simon 0:560a6744936c 1451 char fn[8+3+1];
simon 0:560a6744936c 1452
simon 0:560a6744936c 1453
simon 0:560a6744936c 1454 res = auto_mount(&path, &dj.fs, 1);
simon 0:560a6744936c 1455 if (res != FR_OK) return res;
simon 0:560a6744936c 1456 res = trace_path(&dj, fn, path, &dir); /* Trace the file path */
simon 0:560a6744936c 1457 if (res != FR_OK) return res; /* Trace failed */
simon 0:560a6744936c 1458 if (!dir) return FR_INVALID_NAME; /* It is the root directory */
simon 0:560a6744936c 1459 if (dir[DIR_Attr] & AM_RDO) return FR_DENIED; /* It is a R/O object */
simon 0:560a6744936c 1460 dsect = dj.fs->winsect;
simon 0:560a6744936c 1461 dclust = ((DWORD)LD_WORD(&dir[DIR_FstClusHI]) << 16) | LD_WORD(&dir[DIR_FstClusLO]);
simon 0:560a6744936c 1462
simon 0:560a6744936c 1463 if (dir[DIR_Attr] & AM_DIR) { /* It is a sub-directory */
simon 0:560a6744936c 1464 dj.clust = dclust; /* Check if the sub-dir is empty or not */
simon 0:560a6744936c 1465 dj.sect = clust2sect(dj.fs, dclust);
simon 0:560a6744936c 1466 dj.index = 2;
simon 0:560a6744936c 1467 do {
simon 0:560a6744936c 1468 if (!move_window(dj.fs, dj.sect)) return FR_RW_ERROR;
simon 0:560a6744936c 1469 sdir = &dj.fs->win[(dj.index & ((SS(dj.fs) - 1) >> 5)) * 32];
simon 0:560a6744936c 1470 if (sdir[DIR_Name] == 0) break;
simon 0:560a6744936c 1471 if (sdir[DIR_Name] != 0xE5 && !(sdir[DIR_Attr] & AM_VOL))
simon 0:560a6744936c 1472 return FR_DENIED; /* The directory is not empty */
simon 0:560a6744936c 1473 } while (next_dir_entry(&dj));
simon 0:560a6744936c 1474 }
simon 0:560a6744936c 1475
simon 0:560a6744936c 1476 if (!move_window(dj.fs, dsect)) return FR_RW_ERROR; /* Mark the directory entry 'deleted' */
simon 0:560a6744936c 1477 dir[DIR_Name] = 0xE5;
simon 0:560a6744936c 1478 dj.fs->winflag = 1;
simon 0:560a6744936c 1479 if (!remove_chain(dj.fs, dclust)) return FR_RW_ERROR; /* Remove the cluster chain */
simon 0:560a6744936c 1480
simon 0:560a6744936c 1481 return sync(dj.fs);
simon 0:560a6744936c 1482 }
simon 0:560a6744936c 1483
simon 0:560a6744936c 1484
simon 0:560a6744936c 1485
simon 0:560a6744936c 1486
simon 0:560a6744936c 1487 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 1488 /* Create a Directory */
simon 0:560a6744936c 1489 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 1490
simon 0:560a6744936c 1491 FRESULT f_mkdir (
simon 0:560a6744936c 1492 const char *path /* Pointer to the directory path */
simon 0:560a6744936c 1493 )
simon 0:560a6744936c 1494 {
simon 0:560a6744936c 1495 FRESULT res;
simon 0:560a6744936c 1496 FATFS_DIR dj;
simon 0:560a6744936c 1497 BYTE *dir, *fw, n;
simon 0:560a6744936c 1498 char fn[8+3+1];
simon 0:560a6744936c 1499 DWORD sect, dsect, dclust, pclust, tim;
simon 0:560a6744936c 1500
simon 0:560a6744936c 1501
simon 0:560a6744936c 1502 res = auto_mount(&path, &dj.fs, 1);
simon 0:560a6744936c 1503 if (res != FR_OK) return res;
simon 0:560a6744936c 1504 res = trace_path(&dj, fn, path, &dir); /* Trace the file path */
simon 0:560a6744936c 1505 if (res == FR_OK) return FR_EXIST; /* Any file or directory is already existing */
simon 0:560a6744936c 1506 if (res != FR_NO_FILE) return res;
simon 0:560a6744936c 1507
simon 0:560a6744936c 1508 res = reserve_direntry(&dj, &dir); /* Reserve a directory entry */
simon 0:560a6744936c 1509 if (res != FR_OK) return res;
simon 0:560a6744936c 1510 sect = dj.fs->winsect;
simon 0:560a6744936c 1511 dclust = create_chain(dj.fs, 0); /* Allocate a cluster for new directory table */
simon 0:560a6744936c 1512 if (dclust == 1) return FR_RW_ERROR;
simon 0:560a6744936c 1513 dsect = clust2sect(dj.fs, dclust);
simon 0:560a6744936c 1514 if (!dsect) return FR_DENIED;
simon 0:560a6744936c 1515 if (!move_window(dj.fs, dsect)) return FR_RW_ERROR;
simon 0:560a6744936c 1516
simon 0:560a6744936c 1517 fw = dj.fs->win;
simon 0:560a6744936c 1518 memset(fw, 0, SS(dj.fs)); /* Clear the new directory table */
simon 0:560a6744936c 1519 for (n = 1; n < dj.fs->csize; n++) {
simon 0:560a6744936c 1520 if (disk_write(dj.fs->drive, fw, ++dsect, 1) != RES_OK)
simon 0:560a6744936c 1521 return FR_RW_ERROR;
simon 0:560a6744936c 1522 }
simon 0:560a6744936c 1523 memset(&fw[DIR_Name], ' ', 8+3); /* Create "." entry */
simon 0:560a6744936c 1524 fw[DIR_Name] = '.';
simon 0:560a6744936c 1525 fw[DIR_Attr] = AM_DIR;
simon 0:560a6744936c 1526 tim = get_fattime();
simon 0:560a6744936c 1527 ST_DWORD(&fw[DIR_WrtTime], tim);
simon 0:560a6744936c 1528 memcpy(&fw[32], &fw[0], 32); fw[33] = '.'; /* Create ".." entry */
simon 0:560a6744936c 1529 ST_WORD(&fw[ DIR_FstClusLO], dclust);
simon 0:560a6744936c 1530 ST_WORD(&fw[ DIR_FstClusHI], dclust >> 16);
simon 0:560a6744936c 1531 pclust = dj.sclust;
simon 0:560a6744936c 1532 if (dj.fs->fs_type == FS_FAT32 && pclust == dj.fs->dirbase) pclust = 0;
simon 0:560a6744936c 1533 ST_WORD(&fw[32+DIR_FstClusLO], pclust);
simon 0:560a6744936c 1534 ST_WORD(&fw[32+DIR_FstClusHI], pclust >> 16);
simon 0:560a6744936c 1535 dj.fs->winflag = 1;
simon 0:560a6744936c 1536
simon 0:560a6744936c 1537 if (!move_window(dj.fs, sect)) return FR_RW_ERROR;
simon 0:560a6744936c 1538 memset(&dir[0], 0, 32); /* Initialize the new entry */
simon 0:560a6744936c 1539 memcpy(&dir[DIR_Name], fn, 8+3); /* Name */
simon 0:560a6744936c 1540 dir[DIR_NTres] = fn[11];
simon 0:560a6744936c 1541 dir[DIR_Attr] = AM_DIR; /* Attribute */
simon 0:560a6744936c 1542 ST_DWORD(&dir[DIR_WrtTime], tim); /* Crated time */
simon 0:560a6744936c 1543 ST_WORD(&dir[DIR_FstClusLO], dclust); /* Table start cluster */
simon 0:560a6744936c 1544 ST_WORD(&dir[DIR_FstClusHI], dclust >> 16);
simon 0:560a6744936c 1545
simon 0:560a6744936c 1546 return sync(dj.fs);
simon 0:560a6744936c 1547 }
simon 0:560a6744936c 1548
simon 0:560a6744936c 1549
simon 0:560a6744936c 1550
simon 0:560a6744936c 1551
simon 0:560a6744936c 1552 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 1553 /* Change File Attribute */
simon 0:560a6744936c 1554 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 1555
simon 0:560a6744936c 1556 FRESULT f_chmod (
simon 0:560a6744936c 1557 const char *path, /* Pointer to the file path */
simon 0:560a6744936c 1558 BYTE value, /* Attribute bits */
simon 0:560a6744936c 1559 BYTE mask /* Attribute mask to change */
simon 0:560a6744936c 1560 )
simon 0:560a6744936c 1561 {
simon 0:560a6744936c 1562 FRESULT res;
simon 0:560a6744936c 1563 FATFS_DIR dj;
simon 0:560a6744936c 1564 BYTE *dir;
simon 0:560a6744936c 1565 char fn[8+3+1];
simon 0:560a6744936c 1566
simon 0:560a6744936c 1567
simon 0:560a6744936c 1568 res = auto_mount(&path, &dj.fs, 1);
simon 0:560a6744936c 1569 if (res == FR_OK) {
simon 0:560a6744936c 1570 res = trace_path(&dj, fn, path, &dir); /* Trace the file path */
simon 0:560a6744936c 1571 if (res == FR_OK) { /* Trace completed */
simon 0:560a6744936c 1572 if (!dir) {
simon 0:560a6744936c 1573 res = FR_INVALID_NAME; /* Root directory */
simon 0:560a6744936c 1574 } else {
simon 0:560a6744936c 1575 mask &= AM_RDO|AM_HID|AM_SYS|AM_ARC; /* Valid attribute mask */
simon 0:560a6744936c 1576 dir[DIR_Attr] = (value & mask) | (dir[DIR_Attr] & (BYTE)~mask); /* Apply attribute change */
simon 0:560a6744936c 1577 res = sync(dj.fs);
simon 0:560a6744936c 1578 }
simon 0:560a6744936c 1579 }
simon 0:560a6744936c 1580 }
simon 0:560a6744936c 1581 return res;
simon 0:560a6744936c 1582 }
simon 0:560a6744936c 1583
simon 0:560a6744936c 1584
simon 0:560a6744936c 1585
simon 0:560a6744936c 1586
simon 0:560a6744936c 1587 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 1588 /* Change Timestamp */
simon 0:560a6744936c 1589 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 1590
simon 0:560a6744936c 1591 FRESULT f_utime (
simon 0:560a6744936c 1592 const char *path, /* Pointer to the file/directory name */
simon 0:560a6744936c 1593 const FILINFO *finfo /* Pointer to the timestamp to be set */
simon 0:560a6744936c 1594 )
simon 0:560a6744936c 1595 {
simon 0:560a6744936c 1596 FRESULT res;
simon 0:560a6744936c 1597 FATFS_DIR dj;
simon 0:560a6744936c 1598 BYTE *dir;
simon 0:560a6744936c 1599 char fn[8+3+1];
simon 0:560a6744936c 1600
simon 0:560a6744936c 1601
simon 0:560a6744936c 1602 res = auto_mount(&path, &dj.fs, 1);
simon 0:560a6744936c 1603 if (res == FR_OK) {
simon 0:560a6744936c 1604 res = trace_path(&dj, fn, path, &dir); /* Trace the file path */
simon 0:560a6744936c 1605 if (res == FR_OK) { /* Trace completed */
simon 0:560a6744936c 1606 if (!dir) {
simon 0:560a6744936c 1607 res = FR_INVALID_NAME; /* Root directory */
simon 0:560a6744936c 1608 } else {
simon 0:560a6744936c 1609 ST_WORD(&dir[DIR_WrtTime], finfo->ftime);
simon 0:560a6744936c 1610 ST_WORD(&dir[DIR_WrtDate], finfo->fdate);
simon 0:560a6744936c 1611 res = sync(dj.fs);
simon 0:560a6744936c 1612 }
simon 0:560a6744936c 1613 }
simon 0:560a6744936c 1614 }
simon 0:560a6744936c 1615 return res;
simon 0:560a6744936c 1616 }
simon 0:560a6744936c 1617
simon 0:560a6744936c 1618
simon 0:560a6744936c 1619
simon 0:560a6744936c 1620
simon 0:560a6744936c 1621 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 1622 /* Rename File/Directory */
simon 0:560a6744936c 1623 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 1624
simon 0:560a6744936c 1625 FRESULT f_rename (
simon 0:560a6744936c 1626 const char *path_old, /* Pointer to the old name */
simon 0:560a6744936c 1627 const char *path_new /* Pointer to the new name */
simon 0:560a6744936c 1628 )
simon 0:560a6744936c 1629 {
simon 0:560a6744936c 1630 FRESULT res;
simon 0:560a6744936c 1631 FATFS_DIR dj;
simon 0:560a6744936c 1632 DWORD sect_old;
simon 0:560a6744936c 1633 BYTE *dir_old, *dir_new, direntry[32-11];
simon 0:560a6744936c 1634 char fn[8+3+1];
simon 0:560a6744936c 1635
simon 0:560a6744936c 1636
simon 0:560a6744936c 1637 res = auto_mount(&path_old, &dj.fs, 1);
simon 0:560a6744936c 1638 if (res != FR_OK) return res;
simon 0:560a6744936c 1639
simon 0:560a6744936c 1640 res = trace_path(&dj, fn, path_old, &dir_old); /* Check old object */
simon 0:560a6744936c 1641 if (res != FR_OK) return res; /* The old object is not found */
simon 0:560a6744936c 1642 if (!dir_old) return FR_NO_FILE;
simon 0:560a6744936c 1643 sect_old = dj.fs->winsect; /* Save the object information */
simon 0:560a6744936c 1644 memcpy(direntry, &dir_old[DIR_Attr], 32-11);
simon 0:560a6744936c 1645
simon 0:560a6744936c 1646 res = trace_path(&dj, fn, path_new, &dir_new); /* Check new object */
simon 0:560a6744936c 1647 if (res == FR_OK) return FR_EXIST; /* The new object name is already existing */
simon 0:560a6744936c 1648 if (res != FR_NO_FILE) return res; /* Is there no old name? */
simon 0:560a6744936c 1649 res = reserve_direntry(&dj, &dir_new); /* Reserve a directory entry */
simon 0:560a6744936c 1650 if (res != FR_OK) return res;
simon 0:560a6744936c 1651
simon 0:560a6744936c 1652 memcpy(&dir_new[DIR_Attr], direntry, 32-11); /* Create new entry */
simon 0:560a6744936c 1653 memcpy(&dir_new[DIR_Name], fn, 8+3);
simon 0:560a6744936c 1654 dir_new[DIR_NTres] = fn[11];
simon 0:560a6744936c 1655 dj.fs->winflag = 1;
simon 0:560a6744936c 1656
simon 0:560a6744936c 1657 if (!move_window(dj.fs, sect_old)) return FR_RW_ERROR; /* Delete old entry */
simon 0:560a6744936c 1658 dir_old[DIR_Name] = 0xE5;
simon 0:560a6744936c 1659
simon 0:560a6744936c 1660 return sync(dj.fs);
simon 0:560a6744936c 1661 }
simon 0:560a6744936c 1662
simon 0:560a6744936c 1663 #endif /* !_FS_READONLY */
simon 0:560a6744936c 1664 #endif /* _FS_MINIMIZE == 0 */
simon 0:560a6744936c 1665 #endif /* _FS_MINIMIZE <= 1 */
simon 0:560a6744936c 1666 #endif /* _FS_MINIMIZE <= 2 */
simon 0:560a6744936c 1667
simon 0:560a6744936c 1668
simon 0:560a6744936c 1669
simon 0:560a6744936c 1670 #if _USE_MKFS && !_FS_READONLY
simon 0:560a6744936c 1671 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 1672 /* Create File System on the Drive */
simon 0:560a6744936c 1673 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 1674 #define N_ROOTDIR 512 /* Multiple of 32 and <= 2048 */
simon 0:560a6744936c 1675 #define N_FATS 1 /* 1 or 2 */
simon 0:560a6744936c 1676 #define MAX_SECTOR 64000000UL /* Maximum partition size */
simon 0:560a6744936c 1677 #define MIN_SECTOR 2000UL /* Minimum partition size */
simon 0:560a6744936c 1678
simon 0:560a6744936c 1679
simon 0:560a6744936c 1680
simon 0:560a6744936c 1681 FRESULT f_mkfs (
simon 0:560a6744936c 1682 BYTE drv, /* Logical drive number */
simon 0:560a6744936c 1683 BYTE partition, /* Partitioning rule 0:FDISK, 1:SFD */
simon 0:560a6744936c 1684 WORD allocsize /* Allocation unit size [bytes] */
simon 0:560a6744936c 1685 )
simon 0:560a6744936c 1686 {
simon 0:560a6744936c 1687 BYTE fmt, m, *tbl;
simon 0:560a6744936c 1688 DWORD b_part, b_fat, b_dir, b_data; /* Area offset (LBA) */
simon 0:560a6744936c 1689 DWORD n_part, n_rsv, n_fat, n_dir; /* Area size */
simon 0:560a6744936c 1690 DWORD n_clust, n;
simon 0:560a6744936c 1691 FATFS *fs;
simon 0:560a6744936c 1692 DSTATUS stat;
simon 0:560a6744936c 1693
simon 0:560a6744936c 1694
simon 0:560a6744936c 1695 /* Check validity of the parameters */
simon 0:560a6744936c 1696 if (drv >= _DRIVES) return FR_INVALID_DRIVE;
simon 0:560a6744936c 1697 if (partition >= 2) return FR_MKFS_ABORTED;
simon 0:560a6744936c 1698 for (n = 512; n <= 32768U && n != allocsize; n <<= 1);
simon 0:560a6744936c 1699 if (n != allocsize) return FR_MKFS_ABORTED;
simon 0:560a6744936c 1700
simon 0:560a6744936c 1701 /* Check mounted drive and clear work area */
simon 0:560a6744936c 1702 fs = FatFs[drv];
simon 0:560a6744936c 1703 if (!fs) return FR_NOT_ENABLED;
simon 0:560a6744936c 1704 fs->fs_type = 0;
simon 0:560a6744936c 1705 drv = LD2PD(drv);
simon 0:560a6744936c 1706
simon 0:560a6744936c 1707 /* Get disk statics */
simon 0:560a6744936c 1708 stat = disk_initialize(drv);
simon 0:560a6744936c 1709 if (stat & STA_NOINIT) return FR_NOT_READY;
simon 0:560a6744936c 1710 if (stat & STA_PROTECT) return FR_WRITE_PROTECTED;
simon 0:560a6744936c 1711 if (disk_ioctl(drv, GET_SECTOR_COUNT, &n_part) != RES_OK || n_part < MIN_SECTOR)
simon 0:560a6744936c 1712 return FR_MKFS_ABORTED;
simon 0:560a6744936c 1713 if (n_part > MAX_SECTOR) n_part = MAX_SECTOR;
simon 0:560a6744936c 1714 b_part = (!partition) ? 63 : 0; /* Boot sector */
simon 0:560a6744936c 1715 n_part -= b_part;
simon 0:560a6744936c 1716 #if S_MAX_SIZ > 512 /* Check disk sector size */
simon 0:560a6744936c 1717 if (disk_ioctl(drv, GET_SECTOR_SIZE, &SS(fs)) != RES_OK
simon 0:560a6744936c 1718 || SS(fs) > S_MAX_SIZ
simon 0:560a6744936c 1719 || SS(fs) > allocsize)
simon 0:560a6744936c 1720 return FR_MKFS_ABORTED;
simon 0:560a6744936c 1721 #endif
simon 0:560a6744936c 1722 allocsize /= SS(fs); /* Number of sectors per cluster */
simon 0:560a6744936c 1723
simon 0:560a6744936c 1724 /* Pre-compute number of clusters and FAT type */
simon 0:560a6744936c 1725 n_clust = n_part / allocsize;
simon 0:560a6744936c 1726 fmt = FS_FAT12;
simon 0:560a6744936c 1727 if (n_clust >= 0xFF5) fmt = FS_FAT16;
simon 0:560a6744936c 1728 if (n_clust >= 0xFFF5) fmt = FS_FAT32;
simon 0:560a6744936c 1729
simon 0:560a6744936c 1730 /* Determine offset and size of FAT structure */
simon 0:560a6744936c 1731 switch (fmt) {
simon 0:560a6744936c 1732 case FS_FAT12:
simon 0:560a6744936c 1733 n_fat = ((n_clust * 3 + 1) / 2 + 3 + SS(fs) - 1) / SS(fs);
simon 0:560a6744936c 1734 n_rsv = 1 + partition;
simon 0:560a6744936c 1735 n_dir = N_ROOTDIR * 32 / SS(fs);
simon 0:560a6744936c 1736 break;
simon 0:560a6744936c 1737 case FS_FAT16:
simon 0:560a6744936c 1738 n_fat = ((n_clust * 2) + 4 + SS(fs) - 1) / SS(fs);
simon 0:560a6744936c 1739 n_rsv = 1 + partition;
simon 0:560a6744936c 1740 n_dir = N_ROOTDIR * 32 / SS(fs);
simon 0:560a6744936c 1741 break;
simon 0:560a6744936c 1742 default:
simon 0:560a6744936c 1743 n_fat = ((n_clust * 4) + 8 + SS(fs) - 1) / SS(fs);
simon 0:560a6744936c 1744 n_rsv = 33 - partition;
simon 0:560a6744936c 1745 n_dir = 0;
simon 0:560a6744936c 1746 }
simon 0:560a6744936c 1747 b_fat = b_part + n_rsv; /* FATs start sector */
simon 0:560a6744936c 1748 b_dir = b_fat + n_fat * N_FATS; /* Directory start sector */
simon 0:560a6744936c 1749 b_data = b_dir + n_dir; /* Data start sector */
simon 0:560a6744936c 1750
simon 0:560a6744936c 1751 /* Align data start sector to erase block boundary (for flash memory media) */
simon 0:560a6744936c 1752 if (disk_ioctl(drv, GET_BLOCK_SIZE, &n) != RES_OK) return FR_MKFS_ABORTED;
simon 0:560a6744936c 1753 n = (b_data + n - 1) & ~(n - 1);
simon 0:560a6744936c 1754 n_fat += (n - b_data) / N_FATS;
simon 0:560a6744936c 1755 /* b_dir and b_data are no longer used below */
simon 0:560a6744936c 1756
simon 0:560a6744936c 1757 /* Determine number of cluster and final check of validity of the FAT type */
simon 0:560a6744936c 1758 n_clust = (n_part - n_rsv - n_fat * N_FATS - n_dir) / allocsize;
simon 0:560a6744936c 1759 if ( (fmt == FS_FAT16 && n_clust < 0xFF5)
simon 0:560a6744936c 1760 || (fmt == FS_FAT32 && n_clust < 0xFFF5))
simon 0:560a6744936c 1761 return FR_MKFS_ABORTED;
simon 0:560a6744936c 1762
simon 0:560a6744936c 1763 /* Create partition table if needed */
simon 0:560a6744936c 1764 if (!partition) {
simon 0:560a6744936c 1765 DWORD n_disk = b_part + n_part;
simon 0:560a6744936c 1766
simon 0:560a6744936c 1767 tbl = &fs->win[MBR_Table];
simon 0:560a6744936c 1768 ST_DWORD(&tbl[0], 0x00010180); /* Partition start in CHS */
simon 0:560a6744936c 1769 if (n_disk < 63UL * 255 * 1024) { /* Partition end in CHS */
simon 0:560a6744936c 1770 n_disk = n_disk / 63 / 255;
simon 0:560a6744936c 1771 tbl[7] = (BYTE)n_disk;
simon 0:560a6744936c 1772 tbl[6] = (BYTE)((n_disk >> 2) | 63);
simon 0:560a6744936c 1773 } else {
simon 0:560a6744936c 1774 ST_WORD(&tbl[6], 0xFFFF);
simon 0:560a6744936c 1775 }
simon 0:560a6744936c 1776 tbl[5] = 254;
simon 0:560a6744936c 1777 if (fmt != FS_FAT32) /* System ID */
simon 0:560a6744936c 1778 tbl[4] = (n_part < 0x10000) ? 0x04 : 0x06;
simon 0:560a6744936c 1779 else
simon 0:560a6744936c 1780 tbl[4] = 0x0c;
simon 0:560a6744936c 1781 ST_DWORD(&tbl[8], 63); /* Partition start in LBA */
simon 0:560a6744936c 1782 ST_DWORD(&tbl[12], n_part); /* Partition size in LBA */
simon 0:560a6744936c 1783 ST_WORD(&tbl[64], 0xAA55); /* Signature */
simon 0:560a6744936c 1784 if (disk_write(drv, fs->win, 0, 1) != RES_OK)
simon 0:560a6744936c 1785 return FR_RW_ERROR;
simon 0:560a6744936c 1786 }
simon 0:560a6744936c 1787
simon 0:560a6744936c 1788 /* Create boot record */
simon 0:560a6744936c 1789 tbl = fs->win; /* Clear buffer */
simon 0:560a6744936c 1790 memset(tbl, 0, SS(fs));
simon 0:560a6744936c 1791 ST_DWORD(&tbl[BS_jmpBoot], 0x90FEEB); /* Boot code (jmp $, nop) */
simon 0:560a6744936c 1792 ST_WORD(&tbl[BPB_BytsPerSec], SS(fs)); /* Sector size */
simon 0:560a6744936c 1793 tbl[BPB_SecPerClus] = (BYTE)allocsize; /* Sectors per cluster */
simon 0:560a6744936c 1794 ST_WORD(&tbl[BPB_RsvdSecCnt], n_rsv); /* Reserved sectors */
simon 0:560a6744936c 1795 tbl[BPB_NumFATs] = N_FATS; /* Number of FATs */
simon 0:560a6744936c 1796 ST_WORD(&tbl[BPB_RootEntCnt], SS(fs) / 32 * n_dir); /* Number of rootdir entries */
simon 0:560a6744936c 1797 if (n_part < 0x10000) { /* Number of total sectors */
simon 0:560a6744936c 1798 ST_WORD(&tbl[BPB_TotSec16], n_part);
simon 0:560a6744936c 1799 } else {
simon 0:560a6744936c 1800 ST_DWORD(&tbl[BPB_TotSec32], n_part);
simon 0:560a6744936c 1801 }
simon 0:560a6744936c 1802 tbl[BPB_Media] = 0xF8; /* Media descripter */
simon 0:560a6744936c 1803 ST_WORD(&tbl[BPB_SecPerTrk], 63); /* Number of sectors per track */
simon 0:560a6744936c 1804 ST_WORD(&tbl[BPB_NumHeads], 255); /* Number of heads */
simon 0:560a6744936c 1805 ST_DWORD(&tbl[BPB_HiddSec], b_part); /* Hidden sectors */
simon 0:560a6744936c 1806 n = get_fattime(); /* Use current time as a VSN */
simon 0:560a6744936c 1807 if (fmt != FS_FAT32) {
simon 0:560a6744936c 1808 ST_DWORD(&tbl[BS_VolID], n); /* Volume serial number */
simon 0:560a6744936c 1809 ST_WORD(&tbl[BPB_FATSz16], n_fat); /* Number of secters per FAT */
simon 0:560a6744936c 1810 tbl[BS_DrvNum] = 0x80; /* Drive number */
simon 0:560a6744936c 1811 tbl[BS_BootSig] = 0x29; /* Extended boot signature */
simon 0:560a6744936c 1812 memcpy(&tbl[BS_VolLab], "NO NAME FAT ", 19); /* Volume lavel, FAT signature */
simon 0:560a6744936c 1813 } else {
simon 0:560a6744936c 1814 ST_DWORD(&tbl[BS_VolID32], n); /* Volume serial number */
simon 0:560a6744936c 1815 ST_DWORD(&tbl[BPB_FATSz32], n_fat); /* Number of secters per FAT */
simon 0:560a6744936c 1816 ST_DWORD(&tbl[BPB_RootClus], 2); /* Root directory cluster (2) */
simon 0:560a6744936c 1817 ST_WORD(&tbl[BPB_FSInfo], 1); /* FSInfo record (bs+1) */
simon 0:560a6744936c 1818 ST_WORD(&tbl[BPB_BkBootSec], 6); /* Backup boot record (bs+6) */
simon 0:560a6744936c 1819 tbl[BS_DrvNum32] = 0x80; /* Drive number */
simon 0:560a6744936c 1820 tbl[BS_BootSig32] = 0x29; /* Extended boot signature */
simon 0:560a6744936c 1821 memcpy(&tbl[BS_VolLab32], "NO NAME FAT32 ", 19); /* Volume lavel, FAT signature */
simon 0:560a6744936c 1822 }
simon 0:560a6744936c 1823 ST_WORD(&tbl[BS_55AA], 0xAA55); /* Signature */
simon 0:560a6744936c 1824 if (disk_write(drv, tbl, b_part+0, 1) != RES_OK)
simon 0:560a6744936c 1825 return FR_RW_ERROR;
simon 0:560a6744936c 1826 if (fmt == FS_FAT32)
simon 0:560a6744936c 1827 disk_write(drv, tbl, b_part+6, 1);
simon 0:560a6744936c 1828
simon 0:560a6744936c 1829 /* Initialize FAT area */
simon 0:560a6744936c 1830 for (m = 0; m < N_FATS; m++) {
simon 0:560a6744936c 1831 memset(tbl, 0, SS(fs)); /* 1st sector of the FAT */
simon 0:560a6744936c 1832 if (fmt != FS_FAT32) {
simon 0:560a6744936c 1833 n = (fmt == FS_FAT12) ? 0x00FFFFF8 : 0xFFFFFFF8;
simon 0:560a6744936c 1834 ST_DWORD(&tbl[0], n); /* Reserve cluster #0-1 (FAT12/16) */
simon 0:560a6744936c 1835 } else {
simon 0:560a6744936c 1836 ST_DWORD(&tbl[0], 0xFFFFFFF8); /* Reserve cluster #0-1 (FAT32) */
simon 0:560a6744936c 1837 ST_DWORD(&tbl[4], 0xFFFFFFFF);
simon 0:560a6744936c 1838 ST_DWORD(&tbl[8], 0x0FFFFFFF); /* Reserve cluster #2 for root dir */
simon 0:560a6744936c 1839 }
simon 0:560a6744936c 1840 if (disk_write(drv, tbl, b_fat++, 1) != RES_OK)
simon 0:560a6744936c 1841 return FR_RW_ERROR;
simon 0:560a6744936c 1842 memset(tbl, 0, SS(fs)); /* Following FAT entries are filled by zero */
simon 0:560a6744936c 1843 for (n = 1; n < n_fat; n++) {
simon 0:560a6744936c 1844 if (disk_write(drv, tbl, b_fat++, 1) != RES_OK)
simon 0:560a6744936c 1845 return FR_RW_ERROR;
simon 0:560a6744936c 1846 }
simon 0:560a6744936c 1847 }
simon 0:560a6744936c 1848
simon 0:560a6744936c 1849 /* Initialize Root directory */
simon 0:560a6744936c 1850 m = (BYTE)((fmt == FS_FAT32) ? allocsize : n_dir);
simon 0:560a6744936c 1851 do {
simon 0:560a6744936c 1852 if (disk_write(drv, tbl, b_fat++, 1) != RES_OK)
simon 0:560a6744936c 1853 return FR_RW_ERROR;
simon 0:560a6744936c 1854 } while (--m);
simon 0:560a6744936c 1855
simon 0:560a6744936c 1856 /* Create FSInfo record if needed */
simon 0:560a6744936c 1857 if (fmt == FS_FAT32) {
simon 0:560a6744936c 1858 ST_WORD(&tbl[BS_55AA], 0xAA55);
simon 0:560a6744936c 1859 ST_DWORD(&tbl[FSI_LeadSig], 0x41615252);
simon 0:560a6744936c 1860 ST_DWORD(&tbl[FSI_StrucSig], 0x61417272);
simon 0:560a6744936c 1861 ST_DWORD(&tbl[FSI_Free_Count], n_clust - 1);
simon 0:560a6744936c 1862 ST_DWORD(&tbl[FSI_Nxt_Free], 0xFFFFFFFF);
simon 0:560a6744936c 1863 disk_write(drv, tbl, b_part+1, 1);
simon 0:560a6744936c 1864 disk_write(drv, tbl, b_part+7, 1);
simon 0:560a6744936c 1865 }
simon 0:560a6744936c 1866
simon 0:560a6744936c 1867 return (disk_ioctl(drv, CTRL_SYNC, NULL) == RES_OK) ? FR_OK : FR_RW_ERROR;
simon 0:560a6744936c 1868 }
simon 0:560a6744936c 1869
simon 0:560a6744936c 1870 #endif /* _USE_MKFS && !_FS_READONLY */
simon 0:560a6744936c 1871
simon 0:560a6744936c 1872
simon 0:560a6744936c 1873
simon 0:560a6744936c 1874
simon 0:560a6744936c 1875 #if _USE_STRFUNC >= 1
simon 0:560a6744936c 1876 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 1877 /* Get a string from the file */
simon 0:560a6744936c 1878 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 1879 char* fgets (
simon 0:560a6744936c 1880 char* buff, /* Pointer to the string buffer to read */
simon 0:560a6744936c 1881 int len, /* Size of string buffer */
simon 0:560a6744936c 1882 FIL* fil /* Pointer to the file object */
simon 0:560a6744936c 1883 )
simon 0:560a6744936c 1884 {
simon 0:560a6744936c 1885 int i = 0;
simon 0:560a6744936c 1886 char *p = buff;
simon 0:560a6744936c 1887 UINT rc;
simon 0:560a6744936c 1888
simon 0:560a6744936c 1889
simon 0:560a6744936c 1890 while (i < len - 1) { /* Read bytes until buffer gets filled */
simon 0:560a6744936c 1891 f_read(fil, p, 1, &rc);
simon 0:560a6744936c 1892 if (rc != 1) break; /* Break when no data to read */
simon 0:560a6744936c 1893 #if _USE_STRFUNC >= 2
simon 0:560a6744936c 1894 if (*p == '\r') continue; /* Strip '\r' */
simon 0:560a6744936c 1895 #endif
simon 0:560a6744936c 1896 i++;
simon 0:560a6744936c 1897 if (*p++ == '\n') break; /* Break when reached end of line */
simon 0:560a6744936c 1898 }
simon 0:560a6744936c 1899 *p = 0;
simon 0:560a6744936c 1900 return i ? buff : 0; /* When no data read (eof or error), return with error. */
simon 0:560a6744936c 1901 }
simon 0:560a6744936c 1902
simon 0:560a6744936c 1903
simon 0:560a6744936c 1904
simon 0:560a6744936c 1905 #if !_FS_READONLY
simon 0:560a6744936c 1906 #include <stdarg.h>
simon 0:560a6744936c 1907 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 1908 /* Put a character to the file */
simon 0:560a6744936c 1909 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 1910 int fputc (
simon 0:560a6744936c 1911 int chr, /* A character to be output */
simon 0:560a6744936c 1912 FIL* fil /* Ponter to the file object */
simon 0:560a6744936c 1913 )
simon 0:560a6744936c 1914 {
simon 0:560a6744936c 1915 UINT bw;
simon 0:560a6744936c 1916 char c;
simon 0:560a6744936c 1917
simon 0:560a6744936c 1918
simon 0:560a6744936c 1919 #if _USE_STRFUNC >= 2
simon 0:560a6744936c 1920 if (chr == '\n') fputc ('\r', fil); /* LF -> CRLF conversion */
simon 0:560a6744936c 1921 #endif
simon 0:560a6744936c 1922 if (!fil) { /* Special value may be used to switch the destination to any other device */
simon 0:560a6744936c 1923 /* put_console(chr); */
simon 0:560a6744936c 1924 return chr;
simon 0:560a6744936c 1925 }
simon 0:560a6744936c 1926 c = (char)chr;
simon 0:560a6744936c 1927 f_write(fil, &c, 1, &bw); /* Write a byte to the file */
simon 0:560a6744936c 1928 return bw ? chr : EOF; /* Return the resulut */
simon 0:560a6744936c 1929 }
simon 0:560a6744936c 1930
simon 0:560a6744936c 1931
simon 0:560a6744936c 1932
simon 0:560a6744936c 1933
simon 0:560a6744936c 1934 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 1935 /* Put a string to the file */
simon 0:560a6744936c 1936 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 1937 int fputs (
simon 0:560a6744936c 1938 const char* str, /* Pointer to the string to be output */
simon 0:560a6744936c 1939 FIL* fil /* Pointer to the file object */
simon 0:560a6744936c 1940 )
simon 0:560a6744936c 1941 {
simon 0:560a6744936c 1942 int n;
simon 0:560a6744936c 1943
simon 0:560a6744936c 1944
simon 0:560a6744936c 1945 for (n = 0; *str; str++, n++) {
simon 0:560a6744936c 1946 if (fputc(*str, fil) == EOF) return EOF;
simon 0:560a6744936c 1947 }
simon 0:560a6744936c 1948 return n;
simon 0:560a6744936c 1949 }
simon 0:560a6744936c 1950
simon 0:560a6744936c 1951
simon 0:560a6744936c 1952
simon 0:560a6744936c 1953
simon 0:560a6744936c 1954 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 1955 /* Put a formatted string to the file */
simon 0:560a6744936c 1956 /*-----------------------------------------------------------------------*/
simon 0:560a6744936c 1957 int fprintf (
simon 0:560a6744936c 1958 FIL* fil, /* Pointer to the file object */
simon 0:560a6744936c 1959 const char* str, /* Pointer to the format string */
simon 0:560a6744936c 1960 ... /* Optional arguments... */
simon 0:560a6744936c 1961 )
simon 0:560a6744936c 1962 {
simon 0:560a6744936c 1963 va_list arp;
simon 0:560a6744936c 1964 UCHAR c, f, r;
simon 0:560a6744936c 1965 ULONG val;
simon 0:560a6744936c 1966 char s[16];
simon 0:560a6744936c 1967 int i, w, res, cc;
simon 0:560a6744936c 1968
simon 0:560a6744936c 1969
simon 0:560a6744936c 1970 va_start(arp, str);
simon 0:560a6744936c 1971
simon 0:560a6744936c 1972 for (cc = res = 0; cc != EOF; res += cc) {
simon 0:560a6744936c 1973 c = *str++;
simon 0:560a6744936c 1974 if (c == 0) break; /* End of string */
simon 0:560a6744936c 1975 if (c != '%') { /* Non escape cahracter */
simon 0:560a6744936c 1976 cc = fputc(c, fil);
simon 0:560a6744936c 1977 if (cc != EOF) cc = 1;
simon 0:560a6744936c 1978 continue;
simon 0:560a6744936c 1979 }
simon 0:560a6744936c 1980 w = f = 0;
simon 0:560a6744936c 1981 c = *str++;
simon 0:560a6744936c 1982 if (c == '0') { /* Flag: '0' padding */
simon 0:560a6744936c 1983 f = 1; c = *str++;
simon 0:560a6744936c 1984 }
simon 0:560a6744936c 1985 while (c >= '0' && c <= '9') { /* Precision */
simon 0:560a6744936c 1986 w = w * 10 + (c - '0');
simon 0:560a6744936c 1987 c = *str++;
simon 0:560a6744936c 1988 }
simon 0:560a6744936c 1989 if (c == 'l') { /* Prefix: Size is long int */
simon 0:560a6744936c 1990 f |= 2; c = *str++;
simon 0:560a6744936c 1991 }
simon 0:560a6744936c 1992 if (c == 's') { /* Type is string */
simon 0:560a6744936c 1993 cc = fputs(va_arg(arp, char*), fil);
simon 0:560a6744936c 1994 continue;
simon 0:560a6744936c 1995 }
simon 0:560a6744936c 1996 if (c == 'c') { /* Type is character */
simon 0:560a6744936c 1997 cc = fputc(va_arg(arp, char), fil);
simon 0:560a6744936c 1998 if (cc != EOF) cc = 1;
simon 0:560a6744936c 1999 continue;
simon 0:560a6744936c 2000 }
simon 0:560a6744936c 2001 r = 0;
simon 0:560a6744936c 2002 if (c == 'd') r = 10; /* Type is signed decimal */
simon 0:560a6744936c 2003 if (c == 'u') r = 10; /* Type is unsigned decimal */
simon 0:560a6744936c 2004 if (c == 'X') r = 16; /* Type is unsigned hexdecimal */
simon 0:560a6744936c 2005 if (r == 0) break; /* Unknown type */
simon 0:560a6744936c 2006 if (f & 2) { /* Get the value */
simon 0:560a6744936c 2007 val = (ULONG)va_arg(arp, long);
simon 0:560a6744936c 2008 } else {
simon 0:560a6744936c 2009 val = (c == 'd') ? (ULONG)(long)va_arg(arp, int) : (ULONG)va_arg(arp, unsigned int);
simon 0:560a6744936c 2010 }
simon 0:560a6744936c 2011 /* Put numeral string */
simon 0:560a6744936c 2012 if (c == 'd') {
simon 0:560a6744936c 2013 if (val >= 0x80000000) {
simon 0:560a6744936c 2014 val = 0 - val;
simon 0:560a6744936c 2015 f |= 4;
simon 0:560a6744936c 2016 }
simon 0:560a6744936c 2017 }
simon 0:560a6744936c 2018 i = sizeof(s) - 1; s[i] = 0;
simon 0:560a6744936c 2019 do {
simon 0:560a6744936c 2020 c = (UCHAR)(val % r + '0');
simon 0:560a6744936c 2021 if (c > '9') c += 7;
simon 0:560a6744936c 2022 s[--i] = c;
simon 0:560a6744936c 2023 val /= r;
simon 0:560a6744936c 2024 } while (i && val);
simon 0:560a6744936c 2025 if (i && (f & 4)) s[--i] = '-';
simon 0:560a6744936c 2026 w = sizeof(s) - 1 - w;
simon 0:560a6744936c 2027 while (i && i > w) s[--i] = (f & 1) ? '0' : ' ';
simon 0:560a6744936c 2028 cc = fputs(&s[i], fil);
simon 0:560a6744936c 2029 }
simon 0:560a6744936c 2030
simon 0:560a6744936c 2031 va_end(arp);
simon 0:560a6744936c 2032 return (cc == EOF) ? cc : res;
simon 0:560a6744936c 2033 }
simon 0:560a6744936c 2034
simon 0:560a6744936c 2035 #endif /* !_FS_READONLY */
simon 0:560a6744936c 2036 #endif /* _USE_STRFUNC >= 1*/