32 #error Wrong include file (ff.h). 36 #define ABORT(fs, res) { fp->err = (BYTE)(res); LEAVE_FF(fs, res); } 42 #error Static LFN work area cannot be used at thread-safe configuration 44 #define ENTER_FF(fs) { if (!lock_fs(fs)) return FR_TIMEOUT; } 45 #define LEAVE_FF(fs, res) { unlock_fs(fs, res); return res; } 48 #define LEAVE_FF(fs, res) return res 54 #if (_MAX_SS < _MIN_SS) || (_MAX_SS != 512 && _MAX_SS != 1024 && _MAX_SS != 2048 && _MAX_SS != 4096) || (_MIN_SS != 512 && _MIN_SS != 1024 && _MIN_SS != 2048 && _MIN_SS != 4096) 55 #error Wrong sector size configuration 57 #if _MAX_SS == _MIN_SS 58 #define SS(fs) ((UINT)_MAX_SS) 60 #define SS(fs) ((fs)->ssize) 66 #if _NORTC_YEAR < 1980 || _NORTC_YEAR > 2107 || _NORTC_MON < 1 || _NORTC_MON > 12 || _NORTC_MDAY < 1 || _NORTC_MDAY > 31 67 #error Invalid _FS_NORTC settings 69 #define GET_FATTIME() ((DWORD)(_NORTC_YEAR - 1980) << 25 | (DWORD)_NORTC_MON << 21 | (DWORD)_NORTC_MDAY << 16) 71 #define GET_FATTIME() get_fattime() 78 #error _FS_LOCK must be 0 at read-only configuration 102 #elif _CODE_PAGE == 936 110 #elif _CODE_PAGE == 949 120 #elif _CODE_PAGE == 950 128 #elif _CODE_PAGE == 437 130 #define _EXCVT {0x80,0x9A,0x45,0x41,0x8E,0x41,0x8F,0x80,0x45,0x45,0x45,0x49,0x49,0x49,0x8E,0x8F, \ 131 0x90,0x92,0x92,0x4F,0x99,0x4F,0x55,0x55,0x59,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \ 132 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \ 133 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0x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F} 150 #elif _CODE_PAGE == 737 152 #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \ 153 0x90,0x92,0x92,0x93,0x94,0x95,0x96,0x97,0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87, \ 154 0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0xAA,0x92,0x93,0x94,0x95,0x96, \ 155 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \ 156 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \ 157 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ 158 0x97,0xEA,0xEB,0xEC,0xE4,0xED,0xEE,0xEF,0xF5,0xF0,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \ 159 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF} 161 #elif _CODE_PAGE == 771 163 #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \ 164 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \ 165 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \ 166 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \ 167 0xC0,0xC1,0xC2,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 862 251 #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \ 252 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \ 253 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \ 254 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \ 255 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \ 256 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ 257 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \ 258 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF} 260 #elif _CODE_PAGE == 863 262 #define _EXCVT {0x43,0x55,0x45,0x41,0x41,0x41,0x86,0x43,0x45,0x45,0x45,0x49,0x49,0x8D,0x41,0x8F, \ 263 0x45,0x45,0x45,0x4F,0x45,0x49,0x55,0x55,0x98,0x4F,0x55,0x9B,0x9C,0x55,0x55,0x9F, \ 264 0xA0,0xA1,0x4F,0x55,0xA4,0xA5,0xA6,0xA7,0x49,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \ 265 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \ 266 0xC0,0xC1,0xC2,0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xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \ 300 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ 301 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \ 302 0xF0,0xF0,0xF2,0xF2,0xF4,0xF4,0xF6,0xF6,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF} 304 #elif _CODE_PAGE == 869 306 #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \ 307 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x86,0x9C,0x8D,0x8F,0x90, \ 308 0x91,0x90,0x92,0x95,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \ 309 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \ 310 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \ 311 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xA4,0xA5,0xA6,0xD9,0xDA,0xDB,0xDC,0xA7,0xA8,0xDF, \ 312 0xA9,0xAA,0xAC,0xAD,0xB5,0xB6,0xB7,0xB8,0xBD,0xBE,0xC6,0xC7,0xCF,0xCF,0xD0,0xEF, \ 313 0xF0,0xF1,0xD1,0xD2,0xD3,0xF5,0xD4,0xF7,0xF8,0xF9,0xD5,0x96,0x95,0x98,0xFE,0xFF} 315 #elif _CODE_PAGE == 1 317 #error Cannot enable LFN without valid code page. 322 #error Unknown code page 328 #define IsUpper(c) (((c)>='A')&&((c)<='Z')) 329 #define IsLower(c) (((c)>='a')&&((c)<='z')) 330 #define IsDigit(c) (((c)>='0')&&((c)<='9')) 335 #define IsDBCS1(c) (((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E) || ((BYTE)(c) >= _DF2S && (BYTE)(c) <= _DF2E)) 337 #define IsDBCS1(c) ((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E) 341 #define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E) || ((BYTE)(c) >= _DS3S && (BYTE)(c) <= _DS3E)) 343 #define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E)) 361 #define FA_SEEKEND 0x20 362 #define FA_MODIFIED 0x40 363 #define FA_DIRTY 0x80 374 #define NS_NOLFN 0x40 375 #define NS_NONAME 0x80 379 #define MAX_FAT12 0xFF5 380 #define MAX_FAT16 0xFFF5 381 #define MAX_FAT32 0xFFFFFF5 382 #define MAX_EXFAT 0x7FFFFFFD 383 #define MAX_DIR 0x200000 384 #define MAX_DIR_EX 0x10000000 393 #define BPB_BytsPerSec 11 394 #define BPB_SecPerClus 13 395 #define BPB_RsvdSecCnt 14 396 #define BPB_NumFATs 16 397 #define BPB_RootEntCnt 17 398 #define BPB_TotSec16 19 400 #define BPB_FATSz16 22 401 #define BPB_SecPerTrk 24 402 #define BPB_NumHeads 26 403 #define BPB_HiddSec 28 404 #define BPB_TotSec32 32 407 #define BS_BootSig 38 410 #define BS_FilSysType 54 411 #define BS_BootCode 62 414 #define BPB_FATSz32 36 415 #define BPB_ExtFlags32 40 416 #define BPB_FSVer32 42 417 #define BPB_RootClus32 44 418 #define BPB_FSInfo32 48 419 #define BPB_BkBootSec32 50 420 #define BS_DrvNum32 64 421 #define BS_NTres32 65 422 #define BS_BootSig32 66 423 #define BS_VolID32 67 424 #define BS_VolLab32 71 425 #define BS_FilSysType32 82 426 #define BS_BootCode32 90 428 #define BPB_ZeroedEx 11 429 #define BPB_VolOfsEx 64 430 #define BPB_TotSecEx 72 431 #define BPB_FatOfsEx 80 432 #define BPB_FatSzEx 84 433 #define BPB_DataOfsEx 88 434 #define BPB_NumClusEx 92 435 #define BPB_RootClusEx 96 436 #define BPB_VolIDEx 100 437 #define BPB_FSVerEx 104 438 #define BPB_VolFlagEx 106 439 #define BPB_ActFatEx 107 440 #define BPB_BytsPerSecEx 108 441 #define BPB_SecPerClusEx 109 442 #define BPB_NumFATsEx 110 443 #define BPB_DrvNumEx 111 444 #define BPB_PercInUseEx 112 445 #define BPB_RsvdEx 113 446 #define BS_BootCodeEx 120 448 #define FSI_LeadSig 0 449 #define FSI_StrucSig 484 450 #define FSI_Free_Count 488 451 #define FSI_Nxt_Free 492 453 #define MBR_Table 446 464 #define PTE_SizLba 12 469 #define DIR_CrtTime10 13 470 #define DIR_CrtTime 14 471 #define DIR_LstAccDate 18 472 #define DIR_FstClusHI 20 473 #define DIR_ModTime 22 474 #define DIR_FstClusLO 26 475 #define DIR_FileSize 28 479 #define LDIR_Chksum 13 480 #define LDIR_FstClusLO 26 482 #define XDIR_NumLabel 1 484 #define XDIR_CaseSum 4 485 #define XDIR_NumSec 1 486 #define XDIR_SetSum 2 488 #define XDIR_CrtTime 8 489 #define XDIR_ModTime 12 490 #define XDIR_AccTime 16 491 #define XDIR_CrtTime10 20 492 #define XDIR_ModTime10 21 493 #define XDIR_CrtTZ 22 494 #define XDIR_ModTZ 23 495 #define XDIR_AccTZ 24 496 #define XDIR_GenFlags 33 497 #define XDIR_NumName 35 498 #define XDIR_NameHash 36 499 #define XDIR_ValidFileSize 40 500 #define XDIR_FstClus 52 501 #define XDIR_FileSize 56 522 #if _VOLUMES < 1 || _VOLUMES > 9 523 #error Wrong _VOLUMES setting 528 #if _FS_RPATH != 0 && _VOLUMES >= 2 538 #define INIT_NAMBUF(fs) 539 #define FREE_NAMBUF() 541 #if _MAX_LFN < 12 || _MAX_LFN > 255 542 #error Wrong _MAX_LFN setting 547 static BYTE DirBuf[SZDIRE*19];
549 static WCHAR LfnBuf[_MAX_LFN+1];
551 #define INIT_NAMBUF(fs) 552 #define FREE_NAMBUF() 556 #define DEF_NAMBUF WCHAR lbuf[_MAX_LFN+1]; BYTE dbuf[SZDIRE*19]; 557 #define INIT_NAMBUF(fs) { (fs)->lfnbuf = lbuf; (fs)->dirbuf = dbuf; } 558 #define FREE_NAMBUF() 560 #define DEF_NAMBUF WCHAR lbuf[_MAX_LFN+1]; 561 #define INIT_NAMBUF(fs) { (fs)->lfnbuf = lbuf; } 562 #define FREE_NAMBUF() 567 #define DEF_NAMBUF WCHAR *lfn; 568 #define INIT_NAMBUF(fs) { lfn = ff_memalloc((_MAX_LFN+1)*2 + SZDIRE*19); if (!lfn) LEAVE_FF(fs, FR_NOT_ENOUGH_CORE); (fs)->lfnbuf = lfn; (fs)->dirbuf = (BYTE*)(lfn+_MAX_LFN+1); } 569 #define FREE_NAMBUF() ff_memfree(lfn) 571 #define DEF_NAMBUF WCHAR *lfn; 572 #define INIT_NAMBUF(fs) { lfn = ff_memalloc((_MAX_LFN+1)*2); if (!lfn) LEAVE_FF(fs, FR_NOT_ENOUGH_CORE); (fs)->lfnbuf = lfn; } 573 #define FREE_NAMBUF() ff_memfree(lfn) 577 #error Wrong _USE_LFN setting 582 static const BYTE ExCvt[] = _EXCVT;
602 WORD ld_word (
const BYTE* ptr)
607 rv = rv << 8 | ptr[0];
612 DWORD ld_dword (
const BYTE* ptr)
617 rv = rv << 8 | ptr[2];
618 rv = rv << 8 | ptr[1];
619 rv = rv << 8 | ptr[0];
625 QWORD ld_qword (
const BYTE* ptr)
630 rv = rv << 8 | ptr[6];
631 rv = rv << 8 | ptr[5];
632 rv = rv << 8 | ptr[4];
633 rv = rv << 8 | ptr[3];
634 rv = rv << 8 | ptr[2];
635 rv = rv << 8 | ptr[1];
636 rv = rv << 8 | ptr[0];
643 void st_word (BYTE* ptr, WORD val)
645 *ptr++ = (BYTE)val; val >>= 8;
650 void st_dword (BYTE* ptr, DWORD val)
652 *ptr++ = (BYTE)val; val >>= 8;
653 *ptr++ = (BYTE)val; val >>= 8;
654 *ptr++ = (BYTE)val; val >>= 8;
660 void st_qword (BYTE* ptr, QWORD val)
662 *ptr++ = (BYTE)val; val >>= 8;
663 *ptr++ = (BYTE)val; val >>= 8;
664 *ptr++ = (BYTE)val; val >>= 8;
665 *ptr++ = (BYTE)val; val >>= 8;
666 *ptr++ = (BYTE)val; val >>= 8;
667 *ptr++ = (BYTE)val; val >>= 8;
668 *ptr++ = (BYTE)val; val >>= 8;
682 void mem_cpy (
void* dst,
const void* src, UINT cnt) {
683 BYTE *d = (BYTE*)dst;
684 const BYTE *s = (
const BYTE*)src;
687 do *d++ = *s++;
while (--cnt);
693 void mem_set (
void* dst,
int val, UINT cnt) {
694 BYTE *d = (BYTE*)dst;
696 do *d++ = (BYTE)val;
while (--cnt);
701 int mem_cmp (
const void* dst,
const void* src, UINT cnt) {
702 const BYTE *d = (
const BYTE *)dst, *s = (
const BYTE *)src;
707 }
while (--cnt && r == 0);
714 int chk_chr (
const char* str,
int chr) {
715 while (*str && *str != chr) str++;
731 return ff_req_grant(fs->sobj);
741 if (fs && res != FR_NOT_ENABLED && res != FR_INVALID_DRIVE && res != FR_TIMEOUT) {
742 ff_rel_grant(fs->sobj);
764 for (i = be = 0; i <
_FS_LOCK; i++) {
766 if (Files[i].fs == dp->obj.fs &&
767 Files[i].clu == dp->obj.sclust &&
768 Files[i].ofs == dp->dptr)
break;
774 return (be || acc == 2) ? FR_OK : FR_TOO_MANY_OPEN_FILES;
778 return (acc || Files[i].ctr == 0x100) ? FR_LOCKED : FR_OK;
787 for (i = 0; i < _FS_LOCK && Files[i].fs; i++) ;
788 return (i == _FS_LOCK) ? 0 : 1;
802 if (Files[i].fs == dp->obj.fs &&
803 Files[i].clu == dp->obj.sclust &&
804 Files[i].ofs == dp->dptr)
break;
808 for (i = 0; i < _FS_LOCK && Files[i].fs; i++) ;
809 if (i == _FS_LOCK)
return 0;
810 Files[i].fs = dp->obj.fs;
811 Files[i].clu = dp->obj.sclust;
812 Files[i].ofs = dp->dptr;
816 if (acc && Files[i].ctr)
return 0;
818 Files[i].ctr = acc ? 0x100 : Files[i].ctr + 1;
833 if (--i < _FS_LOCK) {
835 if (n == 0x100) n = 0;
838 if (n == 0) Files[i].fs = 0;
855 if (Files[i].fs == fs) Files[i].fs = 0;
868 FRESULT sync_window (
879 if (disk_write(fs->drv, fs->win, wsect, 1) != RES_OK) {
883 if (wsect - fs->fatbase < fs->fsize) {
884 for (nf = fs->n_fats; nf >= 2; nf--) {
886 disk_write(fs->drv, fs->win, wsect, 1);
897 FRESULT move_window (
905 if (sector != fs->winsect) {
907 res = sync_window(fs);
910 if (disk_read(fs->drv, fs->win, sector, 1) != RES_OK) {
914 fs->winsect = sector;
936 res = sync_window(fs);
939 if (fs->fs_type == FS_FAT32 && fs->fsi_flag == 1) {
941 mem_set(fs->win, 0, SS(fs));
942 st_word(fs->win + BS_55AA, 0xAA55);
943 st_dword(fs->win + FSI_LeadSig, 0x41615252);
944 st_dword(fs->win + FSI_StrucSig, 0x61417272);
945 st_dword(fs->win + FSI_Free_Count, fs->free_clst);
946 st_dword(fs->win + FSI_Nxt_Free, fs->last_clst);
948 fs->winsect = fs->volbase + 1;
949 disk_write(fs->drv, fs->win, fs->winsect, 1);
953 if (disk_ioctl(fs->drv, CTRL_SYNC, 0) != RES_OK) res = FR_DISK_ERR;
974 if (clst >= fs->n_fatent - 2)
return 0;
975 return clst * fs->csize + fs->database;
996 if (clst < 2 || clst >= fs->n_fatent) {
1002 switch (fs->fs_type) {
1004 bc = (UINT)clst; bc += bc / 2;
1005 if (move_window(fs, fs->fatbase + (bc / SS(fs))) != FR_OK)
break;
1006 wc = fs->win[bc++ % SS(fs)];
1007 if (move_window(fs, fs->fatbase + (bc / SS(fs))) != FR_OK)
break;
1008 wc |= fs->win[bc % SS(fs)] << 8;
1009 val = (clst & 1) ? (wc >> 4) : (wc & 0xFFF);
1013 if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 2))) != FR_OK)
break;
1014 val = ld_word(fs->win + clst * 2 % SS(fs));
1018 if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 4))) != FR_OK)
break;
1019 val = ld_dword(fs->win + clst * 4 % SS(fs)) & 0x0FFFFFFF;
1024 DWORD cofs = clst - obj->sclust;
1025 DWORD clen = (DWORD)((obj->objsize - 1) / SS(fs)) / fs->csize;
1027 if (obj->stat == 2) {
1029 val = (cofs == clen) ? 0x7FFFFFFF : clst + 1;
1033 if (obj->stat == 3 && cofs < obj->n_cont) {
1037 if (obj->stat != 2) {
1038 if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 4))) != FR_OK)
break;
1039 val = ld_dword(fs->win + clst * 4 % SS(fs)) & 0x7FFFFFFF;
1070 FRESULT res = FR_INT_ERR;
1073 if (clst >= 2 && clst < fs->n_fatent) {
1074 switch (fs->fs_type) {
1076 bc = (UINT)clst; bc += bc / 2;
1077 res = move_window(fs, fs->fatbase + (bc / SS(fs)));
1078 if (res != FR_OK)
break;
1079 p = fs->win + bc++ % SS(fs);
1080 *p = (clst & 1) ? ((*p & 0x0F) | ((BYTE)val << 4)) : (BYTE)val;
1082 res = move_window(fs, fs->fatbase + (bc / SS(fs)));
1083 if (res != FR_OK)
break;
1084 p = fs->win + bc % SS(fs);
1085 *p = (clst & 1) ? (BYTE)(val >> 4) : ((*p & 0xF0) | ((BYTE)(val >> 8) & 0x0F));
1090 res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 2)));
1091 if (res != FR_OK)
break;
1092 st_word(fs->win + clst * 2 % SS(fs), (WORD)val);
1100 res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 4)));
1101 if (res != FR_OK)
break;
1102 if (!
_FS_EXFAT || fs->fs_type != FS_EXFAT) {
1103 val = (val & 0x0FFFFFFF) | (ld_dword(fs->win + clst * 4 % SS(fs)) & 0xF0000000);
1105 st_dword(fs->win + clst * 4 % SS(fs), val);
1118 #if _FS_EXFAT && !_FS_READONLY 1136 DWORD val, scl, ctr;
1140 if (clst >= fs->n_fatent - 2) clst = 0;
1141 scl = val = clst; ctr = 0;
1143 if (move_window(fs, fs->database + val / 8 / SS(fs)) != FR_OK)
return 0xFFFFFFFF;
1144 i = val / 8 % SS(fs); bm = 1 << (val % 8);
1147 bv = fs->win[i] & bm; bm <<= 1;
1148 if (++val >= fs->n_fatent - 2) {
1149 val = 0; bm = 0; i = 4096;
1152 if (++ctr == ncl)
return scl + 2;
1156 if (val == clst)
return 0;
1159 }
while (++i < SS(fs));
1169 FRESULT change_bitmap (
1182 sect = fs->database + clst / 8 / SS(fs);
1183 i = clst / 8 % SS(fs);
1184 bm = 1 << (clst % 8);
1186 if (move_window(fs, sect++) != FR_OK)
return FR_DISK_ERR;
1189 if (bv == (
int)((fs->win[i] & bm) != 0))
return FR_INT_ERR;
1192 if (--ncl == 0)
return FR_OK;
1195 }
while (++i < SS(fs));
1206 FRESULT fill_fat_chain (
1213 if (obj->stat == 3) {
1214 for (cl = obj->sclust, n = obj->n_cont; n; cl++, n--) {
1215 res = put_fat(obj->fs, cl, cl + 1);
1216 if (res != FR_OK)
return res;
1232 FRESULT remove_chain (
1238 FRESULT res = FR_OK;
1240 FATFS *fs = obj->fs;
1241 #if _FS_EXFAT || _USE_TRIM 1242 DWORD scl = clst, ecl = clst;
1248 if (clst < 2 || clst >= fs->n_fatent)
return FR_INT_ERR;
1251 if (pclst && (!
_FS_EXFAT || fs->fs_type != FS_EXFAT || obj->stat != 2)) {
1252 res = put_fat(fs, pclst, 0xFFFFFFFF);
1253 if (res != FR_OK)
return res;
1258 nxt = get_fat(obj, clst);
1259 if (nxt == 0)
break;
1260 if (nxt == 1)
return FR_INT_ERR;
1261 if (nxt == 0xFFFFFFFF)
return FR_DISK_ERR;
1262 if (!
_FS_EXFAT || fs->fs_type != FS_EXFAT) {
1263 res = put_fat(fs, clst, 0);
1264 if (res != FR_OK)
return res;
1266 if (fs->free_clst < fs->n_fatent - 2) {
1270 #if _FS_EXFAT || _USE_TRIM 1271 if (ecl + 1 == nxt) {
1275 if (fs->fs_type == FS_EXFAT) {
1276 res = change_bitmap(fs, scl, ecl - scl + 1, 0);
1277 if (res != FR_OK)
return res;
1281 rt[0] = clust2sect(fs, scl);
1282 rt[1] = clust2sect(fs, ecl) + fs->csize - 1;
1283 disk_ioctl(fs->drv, CTRL_TRIM, rt);
1289 }
while (clst < fs->n_fatent);
1292 if (fs->fs_type == FS_EXFAT) {
1296 if (obj->stat == 3 && pclst >= obj->sclust && pclst <= obj->sclust + obj->n_cont) {
1312 DWORD create_chain (
1319 FATFS *fs = obj->fs;
1323 scl = fs->last_clst;
1324 if (scl == 0 || scl >= fs->n_fatent) scl = 1;
1327 cs = get_fat(obj, clst);
1328 if (cs < 2)
return 1;
1329 if (cs == 0xFFFFFFFF)
return cs;
1330 if (cs < fs->n_fatent)
return cs;
1335 if (fs->fs_type == FS_EXFAT) {
1336 ncl = find_bitmap(fs, scl, 1);
1337 if (ncl == 0 || ncl == 0xFFFFFFFF)
return ncl;
1338 res = change_bitmap(fs, ncl, 1, 1);
1339 if (res == FR_INT_ERR)
return 1;
1340 if (res == FR_DISK_ERR)
return 0xFFFFFFFF;
1344 if (obj->stat == 2 && ncl != scl + 1) {
1345 obj->n_cont = scl - obj->sclust;
1355 if (ncl >= fs->n_fatent) {
1357 if (ncl > scl)
return 0;
1359 cs = get_fat(obj, ncl);
1361 if (cs == 1 || cs == 0xFFFFFFFF)
return cs;
1362 if (ncl == scl)
return 0;
1366 if (
_FS_EXFAT && fs->fs_type == FS_EXFAT && obj->stat == 2) {
1369 res = put_fat(fs, ncl, 0xFFFFFFFF);
1370 if (res == FR_OK && clst) {
1371 res = put_fat(fs, clst, ncl);
1376 fs->last_clst = ncl;
1377 if (fs->free_clst < fs->n_fatent - 2) fs->free_clst--;
1380 ncl = (res == FR_DISK_ERR) ? 0xFFFFFFFF : 1;
1402 DWORD cl, ncl, *tbl;
1403 FATFS *fs = fp->obj.fs;
1406 tbl = fp->cltbl + 1;
1407 cl = (DWORD)(ofs / SS(fs) / fs->csize);
1410 if (ncl == 0)
return 0;
1411 if (cl < ncl)
break;
1433 FATFS *fs = dp->obj.fs;
1436 if (ofs >= (DWORD)((
_FS_EXFAT && fs->fs_type == FS_EXFAT) ? MAX_DIR_EX : MAX_DIR) || ofs % SZDIRE) {
1440 clst = dp->obj.sclust;
1441 if (clst == 0 && fs->fs_type >= FS_FAT32) {
1447 if (ofs / SZDIRE >= fs->n_rootdir)
return FR_INT_ERR;
1448 dp->sect = fs->dirbase;
1451 csz = (DWORD)fs->csize * SS(fs);
1452 while (ofs >= csz) {
1453 clst = get_fat(&dp->obj, clst);
1454 if (clst == 0xFFFFFFFF)
return FR_DISK_ERR;
1455 if (clst < 2 || clst >= fs->n_fatent)
return FR_INT_ERR;
1458 dp->sect = clust2sect(fs, clst);
1461 if (!dp->sect)
return FR_INT_ERR;
1462 dp->sect += ofs / SS(fs);
1463 dp->dir = fs->win + (ofs % SS(fs));
1482 FATFS *fs = dp->obj.fs;
1487 ofs = dp->dptr + SZDIRE;
1488 if (!dp->sect || ofs >= (DWORD)((
_FS_EXFAT && fs->fs_type == FS_EXFAT) ? MAX_DIR_EX : MAX_DIR))
return FR_NO_FILE;
1490 if (ofs % SS(fs) == 0) {
1494 if (ofs / SZDIRE >= fs->n_rootdir) {
1495 dp->sect = 0;
return FR_NO_FILE;
1499 if ((ofs / SS(fs) & (fs->csize - 1)) == 0) {
1500 clst = get_fat(&dp->obj, dp->clust);
1501 if (clst <= 1)
return FR_INT_ERR;
1502 if (clst == 0xFFFFFFFF)
return FR_DISK_ERR;
1503 if (clst >= fs->n_fatent) {
1506 dp->sect = 0;
return FR_NO_FILE;
1508 clst = create_chain(&dp->obj, dp->clust);
1509 if (clst == 0)
return FR_DENIED;
1510 if (clst == 1)
return FR_INT_ERR;
1511 if (clst == 0xFFFFFFFF)
return FR_DISK_ERR;
1514 if (sync_window(fs) != FR_OK)
return FR_DISK_ERR;
1515 mem_set(fs->win, 0, SS(fs));
1516 for (n = 0, fs->winsect = clust2sect(fs, clst); n < fs->csize; n++, fs->winsect++) {
1518 if (sync_window(fs) != FR_OK)
return FR_DISK_ERR;
1522 if (!stretch) dp->sect = 0;
1523 dp->sect = 0;
return FR_NO_FILE;
1527 dp->sect = clust2sect(fs, clst);
1532 dp->dir = fs->win + ofs % SS(fs);
1553 FATFS *fs = dp->obj.fs;
1556 res = dir_sdi(dp, 0);
1560 res = move_window(fs, dp->sect);
1561 if (res != FR_OK)
break;
1563 if ((fs->fs_type == FS_EXFAT) ? (
int)((dp->dir[XDIR_Type] & 0x80) == 0) : (
int)(dp->dir[DIR_Name] == DDEM || dp->dir[DIR_Name] == 0)) {
1565 if (dp->dir[DIR_Name] == DDEM || dp->dir[DIR_Name] == 0) {
1567 if (++n == nent)
break;
1571 res = dir_next(dp, 1);
1572 }
while (res == FR_OK);
1575 if (res == FR_NO_FILE) res = FR_DENIED;
1596 cl = ld_word(dir + DIR_FstClusLO);
1597 if (fs->fs_type == FS_FAT32) {
1598 cl |= (DWORD)ld_word(dir + DIR_FstClusHI) << 16;
1613 st_word(dir + DIR_FstClusLO, (WORD)cl);
1614 if (fs->fs_type == FS_FAT32) {
1615 st_word(dir + DIR_FstClusHI, (WORD)(cl >> 16));
1627 const BYTE LfnOfs[] = {1,3,5,7,9,14,16,18,20,22,24,28,30};
1635 const WCHAR* lfnbuf,
1643 if (ld_word(dir + LDIR_FstClusLO) != 0)
return 0;
1645 i = ((dir[LDIR_Ord] & 0x3F) - 1) * 13;
1647 for (wc = 1, s = 0; s < 13; s++) {
1648 uc = ld_word(dir + LfnOfs[s]);
1650 if (i >= _MAX_LFN || ff_wtoupper(uc) != ff_wtoupper(lfnbuf[i++])) {
1655 if (uc != 0xFFFF)
return 0;
1659 if ((dir[LDIR_Ord] & LLEF) && wc && lfnbuf[i])
return 0;
1665 #if _FS_MINIMIZE <= 1 || _FS_RPATH >= 2 || _USE_LABEL || _FS_EXFAT 1679 if (ld_word(dir + LDIR_FstClusLO) != 0)
return 0;
1681 i = ((dir[LDIR_Ord] & 0x3F) - 1) * 13;
1683 for (wc = 1, s = 0; s < 13; s++) {
1684 uc = ld_word(dir + LfnOfs[s]);
1686 if (i >= _MAX_LFN)
return 0;
1687 lfnbuf[i++] = wc = uc;
1689 if (uc != 0xFFFF)
return 0;
1693 if (dir[LDIR_Ord] & LLEF) {
1694 if (i >= _MAX_LFN)
return 0;
1719 dir[LDIR_Chksum] = sum;
1720 dir[LDIR_Attr] = AM_LFN;
1722 st_word(dir + LDIR_FstClusLO, 0);
1727 if (wc != 0xFFFF) wc = lfn[i++];
1728 st_word(dir + LfnOfs[s], wc);
1729 if (wc == 0) wc = 0xFFFF;
1731 if (wc == 0xFFFF || !lfn[i]) ord |= LLEF;
1732 dir[LDIR_Ord] = ord;
1740 #if _USE_LFN != 0 && !_FS_READONLY 1759 mem_cpy(dst, src, 11);
1765 for (i = 0; i < 16; i++) {
1766 sr = (sr << 1) + (wc & 1);
1768 if (sr & 0x10000) sr ^= 0x11021;
1777 c = (BYTE)((seq % 16) +
'0');
1778 if (c >
'9') c += 7;
1785 for (j = 0; j < i && dst[j] !=
' '; j++) {
1786 if (IsDBCS1(dst[j])) {
1787 if (j == i - 1)
break;
1792 dst[j++] = (i < 8) ? ns[i++] :
' ';
1812 do sum = (sum >> 1) + (sum << 7) + *dir++;
while (--n);
1834 szblk = (dir[XDIR_NumSec] + 1) * SZDIRE;
1835 for (i = sum = 0; i < szblk; i++) {
1836 if (i == XDIR_SetSum) {
1839 sum = ((sum & 1) ? 0x8000 : 0) + (sum >> 1) + dir[i];
1856 while ((chr = *name++) != 0) {
1857 chr = ff_wtoupper(chr);
1858 sum = ((sum & 1) ? 0x8000 : 0) + (sum >> 1) + (chr & 0xFF);
1859 sum = ((sum & 1) ? 0x8000 : 0) + (sum >> 1) + (chr >> 8);
1865 #if !_FS_READONLY && _USE_MKFS 1872 sum = ((sum & 1) ? 0x80000000 : 0) + (sum >> 1) + dat;
1878 #if _FS_MINIMIZE <= 1 || _FS_RPATH >= 2 1884 void get_xdir_info (
1897 if (dirb[XDIR_NumName] <= _MAX_LFN) {
1898 for (si = SZDIRE * 2, di = 0; di < dirb[XDIR_NumName]; si += 2, di++) {
1899 if ((si % SZDIRE) == 0) si += 2;
1900 w = ld_word(dirb + si);
1907 for (si = SZDIRE * 2, di = nc = 0; nc < dirb[XDIR_NumName]; si += 2, nc++) {
1908 if ((si % SZDIRE) == 0) si += 2;
1909 w = ld_word(dirb + si);
1910 w = ff_convert(w, 0);
1911 if (w == 0) { di = 0;
break; }
1912 if (_DF1S && w >= 0x100) {
1913 fno->fname[di++] = (char)(w >> 8);
1915 if (di >= _MAX_LFN) { di = 0;
break; }
1916 fno->fname[di++] = (char)w;
1919 if (di == 0) fno->fname[di++] =
'?';
1922 fno->altname[0] = 0;
1923 fno->fattrib = dirb[XDIR_Attr];
1924 fno->fsize = (fno->fattrib & AM_DIR) ? 0 : ld_qword(dirb + XDIR_FileSize);
1925 fno->ftime = ld_word(dirb + XDIR_ModTime + 0);
1926 fno->fdate = ld_word(dirb + XDIR_ModTime + 2);
1943 BYTE* dirb = dp->obj.fs->dirbuf;
1947 res = move_window(dp->obj.fs, dp->sect);
1948 if (res != FR_OK)
return res;
1949 if (dp->dir[XDIR_Type] != 0x85)
return FR_INT_ERR;
1950 mem_cpy(dirb, dp->dir, SZDIRE);
1951 nent = dirb[XDIR_NumSec] + 1;
1954 res = dir_next(dp, 0);
1955 if (res != FR_OK)
return res;
1956 res = move_window(dp->obj.fs, dp->sect);
1957 if (res != FR_OK)
return res;
1958 if (dp->dir[XDIR_Type] != 0xC0)
return FR_INT_ERR;
1959 mem_cpy(dirb + SZDIRE, dp->dir, SZDIRE);
1962 if (nent < 3 || nent > 19)
return FR_NO_FILE;
1963 i = SZDIRE * 2; nent *= SZDIRE;
1965 res = dir_next(dp, 0);
1966 if (res != FR_OK)
return res;
1967 res = move_window(dp->obj.fs, dp->sect);
1968 if (res != FR_OK)
return res;
1969 if (dp->dir[XDIR_Type] != 0xC1)
return FR_INT_ERR;
1970 mem_cpy(dirb + i, dp->dir, SZDIRE);
1975 if (xdir_sum(dirb) != ld_word(dirb + XDIR_SetSum))
return FR_INT_ERR;
1981 #if !_FS_READONLY || _FS_RPATH != 0 1986 FRESULT load_obj_dir (
1995 dp->obj.fs = obj->fs;
1996 dp->obj.sclust = obj->c_scl;
1997 dp->obj.stat = (BYTE)obj->c_size;
1998 dp->obj.objsize = obj->c_size & 0xFFFFFF00;
1999 dp->blk_ofs = obj->c_ofs;
2001 res = dir_sdi(dp, dp->blk_ofs);
2003 res = load_xdir(dp);
2015 FRESULT store_xdir (
2021 BYTE* dirb = dp->obj.fs->dirbuf;
2024 st_word(dirb + XDIR_SetSum, xdir_sum(dirb));
2025 nent = dirb[XDIR_NumSec] + 1;
2028 res = dir_sdi(dp, dp->blk_ofs);
2029 while (res == FR_OK) {
2030 res = move_window(dp->obj.fs, dp->sect);
2031 if (res != FR_OK)
break;
2032 mem_cpy(dp->dir, dirb, SZDIRE);
2033 dp->obj.fs->wflag = 1;
2034 if (--nent == 0)
break;
2036 res = dir_next(dp, 0);
2038 return (res == FR_OK || res == FR_DISK_ERR) ? res : FR_INT_ERR;
2058 mem_set(dirb, 0, 2 * SZDIRE);
2059 dirb[XDIR_Type] = 0x85;
2060 dirb[XDIR_Type + SZDIRE] = 0xC0;
2061 st_word(dirb + XDIR_NameHash, xname_sum(lfn));
2064 nc = 0; nb = 1; chr = 1;
2066 dirb[i++] = 0xC1; dirb[i++] = 0;
2068 if (chr && (chr = lfn[nc]) != 0) nc++;
2069 st_word(dirb + i, chr); i += 2;
2070 }
while (i % SZDIRE);
2074 dirb[XDIR_NumName] = nc;
2075 dirb[XDIR_NumSec] = nb;
2083 #if _FS_MINIMIZE <= 1 || _FS_RPATH >= 2 || _USE_LABEL || _FS_EXFAT 2094 FRESULT res = FR_NO_FILE;
2095 FATFS *fs = dp->obj.fs;
2098 BYTE ord = 0xFF, sum = 0xFF;
2102 res = move_window(fs, dp->sect);
2103 if (res != FR_OK)
break;
2104 c = dp->dir[DIR_Name];
2105 if (c == 0) { res = FR_NO_FILE;
break; }
2107 if (fs->fs_type == FS_EXFAT) {
2109 if (c == 0x83)
break;
2112 dp->blk_ofs = dp->dptr;
2113 res = load_xdir(dp);
2115 dp->obj.attr = fs->dirbuf[XDIR_Attr] & AM_MASK;
2123 dp->obj.attr = a = dp->dir[DIR_Attr] & AM_MASK;
2125 if (c == DDEM || c ==
'.' || (
int)((a & ~AM_ARC) == AM_VOL) != vol) {
2130 sum = dp->dir[LDIR_Chksum];
2131 c &= (BYTE)~LLEF; ord = c;
2132 dp->blk_ofs = dp->dptr;
2135 ord = (c == ord && sum == dp->dir[LDIR_Chksum] && pick_lfn(fs->lfnbuf, dp->dir)) ? ord - 1 : 0xFF;
2137 if (ord || sum != sum_sfn(dp->dir)) {
2138 dp->blk_ofs = 0xFFFFFFFF;
2144 if (c != DDEM && c !=
'.' && a != AM_LFN && (
int)((a & ~AM_ARC) == AM_VOL) == vol) {
2149 res = dir_next(dp, 0);
2150 if (res != FR_OK)
break;
2153 if (res != FR_OK) dp->sect = 0;
2171 FATFS *fs = dp->obj.fs;
2177 res = dir_sdi(dp, 0);
2178 if (res != FR_OK)
return res;
2180 if (fs->fs_type == FS_EXFAT) {
2183 WORD hash = xname_sum(fs->lfnbuf);
2185 while ((res = dir_read(dp, 0)) == FR_OK) {
2186 if (ld_word(fs->dirbuf + XDIR_NameHash) != hash)
continue;
2187 for (nc = fs->dirbuf[XDIR_NumName], di = SZDIRE * 2, ni = 0; nc; nc--, di += 2, ni++) {
2188 if ((di % SZDIRE) == 0) di += 2;
2189 if (ff_wtoupper(ld_word(fs->dirbuf + di)) != ff_wtoupper(fs->lfnbuf[ni]))
break;
2191 if (nc == 0 && !fs->lfnbuf[ni])
break;
2198 ord = sum = 0xFF; dp->blk_ofs = 0xFFFFFFFF;
2201 res = move_window(fs, dp->sect);
2202 if (res != FR_OK)
break;
2203 c = dp->dir[DIR_Name];
2204 if (c == 0) { res = FR_NO_FILE;
break; }
2206 dp->obj.attr = a = dp->dir[DIR_Attr] & AM_MASK;
2207 if (c == DDEM || ((a & AM_VOL) && a != AM_LFN)) {
2208 ord = 0xFF; dp->blk_ofs = 0xFFFFFFFF;
2211 if (!(dp->fn[NSFLAG] & NS_NOLFN)) {
2213 sum = dp->dir[LDIR_Chksum];
2214 c &= (BYTE)~LLEF; ord = c;
2215 dp->blk_ofs = dp->dptr;
2218 ord = (c == ord && sum == dp->dir[LDIR_Chksum] && cmp_lfn(fs->lfnbuf, dp->dir)) ? ord - 1 : 0xFF;
2221 if (!ord && sum == sum_sfn(dp->dir))
break;
2222 if (!(dp->fn[NSFLAG] & NS_LOSS) && !mem_cmp(dp->dir, dp->fn, 11))
break;
2223 ord = 0xFF; dp->blk_ofs = 0xFFFFFFFF;
2227 dp->obj.attr = dp->dir[DIR_Attr] & AM_MASK;
2228 if (!(dp->dir[DIR_Attr] & AM_VOL) && !mem_cmp(dp->dir, dp->fn, 11))
break;
2230 res = dir_next(dp, 0);
2231 }
while (res == FR_OK);
2245 FRESULT dir_register (
2250 FATFS *fs = dp->obj.fs;
2256 if (dp->fn[NSFLAG] & (NS_DOT | NS_NONAME))
return FR_INVALID_NAME;
2257 for (nlen = 0; fs->lfnbuf[nlen]; nlen++) ;
2260 if (fs->fs_type == FS_EXFAT) {
2263 nent = (nlen + 14) / 15 + 2;
2264 res = dir_alloc(dp, nent);
2265 if (res != FR_OK)
return res;
2266 dp->blk_ofs = dp->dptr - SZDIRE * (nent - 1);
2268 if (dp->obj.sclust != 0 && (dp->obj.stat & 4)) {
2270 dp->obj.objsize += (DWORD)fs->csize * SS(fs);
2271 res = fill_fat_chain(&dp->obj);
2272 if (res != FR_OK)
return res;
2273 res = load_obj_dir(&dj, &dp->obj);
2274 if (res != FR_OK)
return res;
2275 st_qword(fs->dirbuf + XDIR_FileSize, dp->obj.objsize);
2276 st_qword(fs->dirbuf + XDIR_ValidFileSize, dp->obj.objsize);
2277 fs->dirbuf[XDIR_GenFlags] = dp->obj.stat | 1;
2278 res = store_xdir(&dj);
2279 if (res != FR_OK)
return res;
2282 create_xdir(fs->dirbuf, fs->lfnbuf);
2287 mem_cpy(sn, dp->fn, 12);
2288 if (sn[NSFLAG] & NS_LOSS) {
2289 dp->fn[NSFLAG] = NS_NOLFN;
2290 for (n = 1; n < 100; n++) {
2291 gen_numname(dp->fn, sn, fs->lfnbuf, n);
2293 if (res != FR_OK)
break;
2295 if (n == 100)
return FR_DENIED;
2296 if (res != FR_NO_FILE)
return res;
2297 dp->fn[NSFLAG] = sn[NSFLAG];
2301 nent = (sn[NSFLAG] & NS_LFN) ? (nlen + 12) / 13 + 1 : 1;
2302 res = dir_alloc(dp, nent);
2303 if (res == FR_OK && --nent) {
2304 res = dir_sdi(dp, dp->dptr - nent * SZDIRE);
2306 sum = sum_sfn(dp->fn);
2308 res = move_window(fs, dp->sect);
2309 if (res != FR_OK)
break;
2310 put_lfn(fs->lfnbuf, dp->dir, (BYTE)nent, sum);
2312 res = dir_next(dp, 0);
2313 }
while (res == FR_OK && --nent);
2318 res = dir_alloc(dp, 1);
2324 res = move_window(fs, dp->sect);
2326 mem_set(dp->dir, 0, SZDIRE);
2327 mem_cpy(dp->dir + DIR_Name, dp->fn, 11);
2329 dp->dir[DIR_NTres] = dp->fn[NSFLAG] & (NS_BODY | NS_EXT);
2342 #if !_FS_READONLY && _FS_MINIMIZE == 0 2348 FRESULT dir_remove (
2353 FATFS *fs = dp->obj.fs;
2355 DWORD last = dp->dptr;
2357 res = (dp->blk_ofs == 0xFFFFFFFF) ? FR_OK : dir_sdi(dp, dp->blk_ofs);
2360 res = move_window(fs, dp->sect);
2361 if (res != FR_OK)
break;
2363 if (
_FS_EXFAT && fs->fs_type == FS_EXFAT) {
2364 dp->dir[XDIR_Type] &= 0x7F;
2366 dp->dir[DIR_Name] = DDEM;
2369 if (dp->dptr >= last)
break;
2370 res = dir_next(dp, 0);
2371 }
while (res == FR_OK);
2372 if (res == FR_NO_FILE) res = FR_INT_ERR;
2376 res = move_window(fs, dp->sect);
2378 dp->dir[DIR_Name] = DDEM;
2390 #if _FS_MINIMIZE <= 1 || _FS_RPATH >= 2 2406 FATFS *fs = dp->obj.fs;
2411 if (!dp->sect)
return;
2415 if (fs->fs_type == FS_EXFAT) {
2416 get_xdir_info(fs->dirbuf, fno);
2421 if (dp->blk_ofs != 0xFFFFFFFF) {
2423 while ((w = fs->lfnbuf[j++]) != 0) {
2425 w = ff_convert(w, 0);
2426 if (w == 0) { i = 0;
break; }
2427 if (_DF1S && w >= 0x100) {
2428 fno->fname[i++] = (char)(w >> 8);
2431 if (i >= _MAX_LFN) { i = 0;
break; }
2432 fno->fname[i++] = (TCHAR)w;
2439 lfv = fno->fname[i];
2441 c = (TCHAR)dp->dir[i++];
2442 if (c ==
' ')
continue;
2443 if (c == RDDEM) c = (TCHAR)DDEM;
2445 if (!lfv) fno->fname[j] =
'.';
2446 fno->altname[j++] =
'.';
2449 if (IsDBCS1(c) && i != 8 && i != 11 && IsDBCS2(dp->dir[i])) {
2450 c = c << 8 | dp->dir[i++];
2452 c = ff_convert(c, 1);
2455 fno->altname[j] = c;
2457 if (IsUpper(c) && (dp->dir[DIR_NTres] & (i >= 9 ? NS_EXT : NS_BODY))) {
2466 if (!dp->dir[DIR_NTres]) j = 0;
2468 fno->altname[j] = 0;
2473 c = (TCHAR)dp->dir[i++];
2474 if (c ==
' ')
continue;
2475 if (c == RDDEM) c = (TCHAR)DDEM;
2476 if (i == 9) fno->fname[j++] =
'.';
2477 fno->fname[j++] = c;
2482 fno->fattrib = dp->dir[DIR_Attr];
2483 fno->fsize = ld_dword(dp->dir + DIR_FileSize);
2484 tm = ld_dword(dp->dir + DIR_ModTime);
2485 fno->ftime = (WORD)tm; fno->fdate = (WORD)(tm >> 16);
2492 #if _USE_FIND && _FS_MINIMIZE <= 1 2505 chr = (BYTE)*(*ptr)++;
2506 if (IsLower(chr)) chr -= 0x20;
2508 if (chr >= 0x80) chr = ExCvt[chr - 0x80];
2510 if (IsDBCS1(chr) && IsDBCS2(**ptr)) {
2511 chr = chr << 8 | (BYTE)*(*ptr)++;
2516 return ff_wtoupper(*(*ptr)++);
2522 int pattern_matching (
2529 const TCHAR *pp, *np;
2535 if (!get_achar(&nam))
return 0;
2537 if (!*pat && inf)
return 1;
2542 if (*pp ==
'?' || *pp ==
'*') {
2545 if (*pp++ ==
'?') nm++;
else nx = 1;
2546 }
while (*pp ==
'?' || *pp ==
'*');
2547 if (pattern_matching(pp, np, nm, nx))
return 1;
2550 pc = get_achar(&pp);
2551 nc = get_achar(&np);
2552 if (pc != nc)
break;
2553 if (pc == 0)
return 1;
2556 }
while (inf && nc);
2570 FRESULT create_name (
2582 p = *path; lfn = dp->obj.fs->lfnbuf; si = di = 0;
2586 if (w ==
'/' || w ==
'\\') {
2587 while (p[si] ==
'/' || p[si] ==
'\\') si++;
2590 if (di >= _MAX_LFN)
return FR_INVALID_NAME;
2596 if (!IsDBCS2(b))
return FR_INVALID_NAME;
2598 w = ff_convert(w, 1);
2599 if (!w)
return FR_INVALID_NAME;
2601 if (w < 0x80 && chk_chr(
"\"*:<>\?|\x7F", w))
return FR_INVALID_NAME;
2605 cf = (w <
' ') ? NS_LAST : 0;
2607 if ((di == 1 && lfn[di - 1] ==
'.') ||
2608 (di == 2 && lfn[di - 1] ==
'.' && lfn[di - 2] ==
'.')) {
2610 for (i = 0; i < 11; i++)
2611 dp->fn[i] = (i < di) ?
'.' :
' ';
2612 dp->fn[i] = cf | NS_DOT;
2618 if (w !=
' ' && w !=
'.')
break;
2622 if (di == 0)
return FR_INVALID_NAME;
2625 mem_set(dp->fn,
' ', 11);
2626 for (si = 0; lfn[si] ==
' ' || lfn[si] ==
'.'; si++) ;
2627 if (si) cf |= NS_LOSS | NS_LFN;
2628 while (di && lfn[di - 1] !=
'.') di--;
2634 if (w ==
' ' || (w ==
'.' && si != di)) {
2635 cf |= NS_LOSS | NS_LFN;
continue;
2638 if (i >= ni || si == di) {
2640 cf |= NS_LOSS | NS_LFN;
break;
2642 if (si != di) cf |= NS_LOSS | NS_LFN;
2644 si = di; i = 8; ni = 11;
2650 w = ff_convert(w, 0);
2651 if (w) w = ExCvt[w - 0x80];
2653 w = ff_convert(ff_wtoupper(w), 0);
2658 if (_DF1S && w >= 0x100) {
2660 cf |= NS_LOSS | NS_LFN; i = ni;
continue;
2662 dp->fn[i++] = (BYTE)(w >> 8);
2664 if (!w || chk_chr(
"+,;=[]", w)) {
2665 w =
'_'; cf |= NS_LOSS | NS_LFN;
2676 dp->fn[i++] = (BYTE)w;
2679 if (dp->fn[0] == DDEM) dp->fn[0] = RDDEM;
2681 if (ni == 8) b <<= 2;
2682 if ((b & 0x0C) == 0x0C || (b & 0x03) == 0x03) cf |= NS_LFN;
2683 if (!(cf & NS_LFN)) {
2684 if ((b & 0x03) == 0x01) cf |= NS_EXT;
2685 if ((b & 0x0C) == 0x04) cf |= NS_BODY;
2688 dp->fn[NSFLAG] = cf;
2699 p = *path; sfn = dp->fn;
2700 mem_set(sfn,
' ', 11);
2706 if (c !=
'.' || si >= 3)
break;
2709 if (c !=
'/' && c !=
'\\' && c >
' ')
return FR_INVALID_NAME;
2711 sfn[NSFLAG] = (c <=
' ') ? NS_LAST | NS_DOT : NS_DOT;
2717 if (c <=
' ')
break;
2718 if (c ==
'/' || c ==
'\\') {
2719 while (p[si] ==
'/' || p[si] ==
'\\') si++;
2722 if (c ==
'.' || i >= ni) {
2723 if (ni == 11 || c !=
'.')
return FR_INVALID_NAME;
2729 c = ExCvt[c - 0x80];
2732 return FR_INVALID_NAME;
2738 if (!IsDBCS2(d) || i >= ni - 1)
return FR_INVALID_NAME;
2742 if (chk_chr(
"\"*+,:;<=>\?[]|\x7F", c))
return FR_INVALID_NAME;
2743 if (IsLower(c)) c -= 0x20;
2748 if (i == 0)
return FR_INVALID_NAME;
2750 if (sfn[0] == DDEM) sfn[0] = RDDEM;
2751 sfn[NSFLAG] = (c <=
' ') ? NS_LAST : 0;
2765 FRESULT follow_path (
2772 _FDID *obj = &dp->obj;
2773 FATFS *fs = obj->fs;
2777 if (*path !=
'/' && *path !=
'\\') {
2778 obj->sclust = fs->cdir;
2782 while (*path ==
'/' || *path ==
'\\') path++;
2785 #if _FS_EXFAT && _FS_RPATH != 0 2786 if (fs->fs_type == FS_EXFAT && obj->sclust) {
2789 obj->c_scl = fs->cdc_scl;
2790 obj->c_size = fs->cdc_size;
2791 obj->c_ofs = fs->cdc_ofs;
2792 res = load_obj_dir(&dj, obj);
2793 if (res != FR_OK)
return res;
2794 obj->objsize = ld_dword(fs->dirbuf + XDIR_FileSize);
2795 obj->stat = fs->dirbuf[XDIR_GenFlags] & 2;
2799 if ((UINT)*path <
' ') {
2800 dp->fn[NSFLAG] = NS_NONAME;
2801 res = dir_sdi(dp, 0);
2805 res = create_name(dp, &path);
2806 if (res != FR_OK)
break;
2808 ns = dp->fn[NSFLAG];
2810 if (res == FR_NO_FILE) {
2812 if (!(ns & NS_LAST))
continue;
2813 dp->fn[NSFLAG] = NS_NONAME;
2816 if (!(ns & NS_LAST)) res = FR_NO_PATH;
2821 if (ns & NS_LAST)
break;
2823 if (!(obj->attr & AM_DIR)) {
2824 res = FR_NO_PATH;
break;
2827 if (fs->fs_type == FS_EXFAT) {
2828 obj->c_scl = obj->sclust;
2829 obj->c_size = ((DWORD)obj->objsize & 0xFFFFFF00) | obj->stat;
2830 obj->c_ofs = dp->blk_ofs;
2831 obj->sclust = ld_dword(fs->dirbuf + XDIR_FstClus);
2832 obj->stat = fs->dirbuf[XDIR_GenFlags] & 2;
2833 obj->objsize = ld_qword(fs->dirbuf + XDIR_FileSize);
2837 obj->sclust = ld_clust(fs, fs->win + dp->dptr % SS(fs));
2857 const TCHAR *tp, *tt;
2861 static const char*
const str[] = {_VOLUME_STRS};
2869 for (tt = *path; (UINT)*tt >= (
_USE_LFN ?
' ' :
'!') && *tt !=
':'; tt++) ;
2873 if (i < 10 && tp == tt) {
2883 sp = str[i]; tp = *path;
2885 c = *sp++; tc = *tp++;
2886 if (IsLower(tc)) tc -= 0x20;
2887 }
while (c && (TCHAR)c == tc);
2888 }
while ((c || tp != tt) && ++i <
_VOLUMES);
2897 #if _FS_RPATH != 0 && _VOLUMES >= 2 2919 fs->wflag = 0; fs->winsect = 0xFFFFFFFF;
2920 if (move_window(fs, sect) != FR_OK)
return 4;
2922 if (ld_word(fs->win + BS_55AA) != 0xAA55)
return 3;
2924 if (fs->win[BS_JmpBoot] == 0xE9 || (fs->win[BS_JmpBoot] == 0xEB && fs->win[BS_JmpBoot + 2] == 0x90)) {
2925 if ((ld_dword(fs->win + BS_FilSysType) & 0xFFFFFF) == 0x544146)
return 0;
2926 if (ld_dword(fs->win + BS_FilSysType32) == 0x33544146)
return 0;
2929 if (!mem_cmp(fs->win + BS_JmpBoot,
"\xEB\x76\x90" "EXFAT ", 11))
return 1;
2942 FRESULT find_volume (
2951 DWORD bsect, fasize, tsect, sysect, nclst, szbfat, br[4];
2959 vol = get_ldnumber(path);
2960 if (vol < 0)
return FR_INVALID_DRIVE;
2964 if (!fs)
return FR_NOT_ENABLED;
2969 mode &= (BYTE)~FA_READ;
2971 stat = disk_status(fs->drv);
2972 if (!(stat & STA_NOINIT)) {
2974 return FR_WRITE_PROTECTED;
2984 fs->drv = LD2PD(vol);
2985 stat = disk_initialize(fs->drv);
2986 if (stat & STA_NOINIT) {
2987 return FR_NOT_READY;
2990 return FR_WRITE_PROTECTED;
2992 #if _MAX_SS != _MIN_SS 2993 if (disk_ioctl(fs->drv, GET_SECTOR_SIZE, &SS(fs)) != RES_OK)
return FR_DISK_ERR;
2994 if (SS(fs) > _MAX_SS || SS(fs) <
_MIN_SS || (SS(fs) & (SS(fs) - 1)))
return FR_DISK_ERR;
2998 fmt = check_fs(fs, bsect);
2999 if (fmt == 2 || (fmt < 2 && LD2PT(vol) != 0)) {
3000 for (i = 0; i < 4; i++) {
3001 pt = fs->win + (MBR_Table + i * SZ_PTE);
3002 br[i] = pt[PTE_System] ? ld_dword(pt + PTE_StLba) : 0;
3008 fmt = bsect ? check_fs(fs, bsect) : 3;
3009 }
while (!LD2PT(vol) && fmt >= 2 && ++i < 4);
3011 if (fmt == 4)
return FR_DISK_ERR;
3012 if (fmt >= 2)
return FR_NO_FILESYSTEM;
3020 for (i = BPB_ZeroedEx; i < BPB_ZeroedEx + 53 && fs->win[i] == 0; i++) ;
3021 if (i < BPB_ZeroedEx + 53)
return FR_NO_FILESYSTEM;
3023 if (ld_word(fs->win + BPB_FSVerEx) != 0x100)
return FR_NO_FILESYSTEM;
3025 if (1 << fs->win[BPB_BytsPerSecEx] != SS(fs))
3026 return FR_NO_FILESYSTEM;
3028 maxlba = ld_qword(fs->win + BPB_TotSecEx) + bsect;
3029 if (maxlba >= 0x100000000)
return FR_NO_FILESYSTEM;
3031 fs->fsize = ld_dword(fs->win + BPB_FatSzEx);
3033 fs->n_fats = fs->win[BPB_NumFATsEx];
3034 if (fs->n_fats != 1)
return FR_NO_FILESYSTEM;
3036 fs->csize = 1 << fs->win[BPB_SecPerClusEx];
3037 if (fs->csize == 0)
return FR_NO_FILESYSTEM;
3039 nclst = ld_dword(fs->win + BPB_NumClusEx);
3040 if (nclst > MAX_EXFAT)
return FR_NO_FILESYSTEM;
3041 fs->n_fatent = nclst + 2;
3044 fs->volbase = bsect;
3045 fs->database = bsect + ld_dword(fs->win + BPB_DataOfsEx);
3046 fs->fatbase = bsect + ld_dword(fs->win + BPB_FatOfsEx);
3047 if (maxlba < (QWORD)fs->database + nclst * fs->csize)
return FR_NO_FILESYSTEM;
3048 fs->dirbase = ld_dword(fs->win + BPB_RootClusEx);
3051 if (move_window(fs, clust2sect(fs, fs->dirbase)) != FR_OK)
return FR_DISK_ERR;
3052 for (i = 0; i < SS(fs); i += SZDIRE) {
3053 if (fs->win[i] == 0x81 && ld_dword(fs->win + i + 20) == 2)
break;
3055 if (i == SS(fs))
return FR_NO_FILESYSTEM;
3057 fs->last_clst = fs->free_clst = 0xFFFFFFFF;
3063 if (ld_word(fs->win + BPB_BytsPerSec) != SS(fs))
return FR_NO_FILESYSTEM;
3065 fasize = ld_word(fs->win + BPB_FATSz16);
3066 if (fasize == 0) fasize = ld_dword(fs->win + BPB_FATSz32);
3069 fs->n_fats = fs->win[BPB_NumFATs];
3070 if (fs->n_fats != 1 && fs->n_fats != 2)
return FR_NO_FILESYSTEM;
3071 fasize *= fs->n_fats;
3073 fs->csize = fs->win[BPB_SecPerClus];
3074 if (fs->csize == 0 || (fs->csize & (fs->csize - 1)))
return FR_NO_FILESYSTEM;
3076 fs->n_rootdir = ld_word(fs->win + BPB_RootEntCnt);
3077 if (fs->n_rootdir % (SS(fs) / SZDIRE))
return FR_NO_FILESYSTEM;
3079 tsect = ld_word(fs->win + BPB_TotSec16);
3080 if (tsect == 0) tsect = ld_dword(fs->win + BPB_TotSec32);
3082 nrsv = ld_word(fs->win + BPB_RsvdSecCnt);
3083 if (nrsv == 0)
return FR_NO_FILESYSTEM;
3086 sysect = nrsv + fasize + fs->n_rootdir / (SS(fs) / SZDIRE);
3087 if (tsect < sysect)
return FR_NO_FILESYSTEM;
3088 nclst = (tsect - sysect) / fs->csize;
3089 if (nclst == 0)
return FR_NO_FILESYSTEM;
3091 if (nclst <= MAX_FAT16) fmt = FS_FAT16;
3092 if (nclst <= MAX_FAT12) fmt = FS_FAT12;
3095 fs->n_fatent = nclst + 2;
3096 fs->volbase = bsect;
3097 fs->fatbase = bsect + nrsv;
3098 fs->database = bsect + sysect;
3099 if (fmt == FS_FAT32) {
3100 if (ld_word(fs->win + BPB_FSVer32) != 0)
return FR_NO_FILESYSTEM;
3101 if (fs->n_rootdir)
return FR_NO_FILESYSTEM;
3102 fs->dirbase = ld_dword(fs->win + BPB_RootClus32);
3103 szbfat = fs->n_fatent * 4;
3105 if (fs->n_rootdir == 0)
return FR_NO_FILESYSTEM;
3106 fs->dirbase = fs->fatbase + fasize;
3107 szbfat = (fmt == FS_FAT16) ?
3108 fs->n_fatent * 2 : fs->n_fatent * 3 / 2 + (fs->n_fatent & 1);
3110 if (fs->fsize < (szbfat + (SS(fs) - 1)) / SS(fs))
return FR_NO_FILESYSTEM;
3114 fs->last_clst = fs->free_clst = 0xFFFFFFFF;
3115 fs->fsi_flag = 0x80;
3116 #if (_FS_NOFSINFO & 3) != 3 3118 && ld_word(fs->win + BPB_FSInfo32) == 1
3119 && move_window(fs, bsect + 1) == FR_OK)
3122 if (ld_word(fs->win + BS_55AA) == 0xAA55
3123 && ld_dword(fs->win + FSI_LeadSig) == 0x41615252
3124 && ld_dword(fs->win + FSI_StrucSig) == 0x61417272)
3126 #if (_FS_NOFSINFO & 1) == 0 3127 fs->free_clst = ld_dword(fs->win + FSI_Free_Count);
3129 #if (_FS_NOFSINFO & 2) == 0 3130 fs->last_clst = ld_dword(fs->win + FSI_Nxt_Free);
3141 fs->lfnbuf = LfnBuf;
3143 fs->dirbuf = DirBuf;
3171 if (!obj || !obj->fs || !obj->fs->fs_type || obj->fs->id != obj->id || (disk_status(obj->fs->drv) & STA_NOINIT)) {
3173 res = FR_INVALID_OBJECT;
3206 const TCHAR *rp = path;
3210 vol = get_ldnumber(&rp);
3211 if (vol < 0)
return FR_INVALID_DRIVE;
3219 if (!ff_del_syncobj(cfs->sobj))
return FR_INT_ERR;
3227 if (!ff_cre_syncobj((BYTE)vol, &fs->sobj))
return FR_INT_ERR;
3232 if (!fs || opt != 1)
return FR_OK;
3234 res = find_volume(&path, &fs, 0);
3255 DWORD dw, cl, bcs, clst, sc;
3261 if (!fp)
return FR_INVALID_OBJECT;
3264 mode &=
_FS_READONLY ? FA_READ : FA_READ | FA_WRITE | FA_CREATE_ALWAYS | FA_CREATE_NEW | FA_OPEN_ALWAYS | FA_OPEN_APPEND | FA_SEEKEND;
3265 res = find_volume(&path, &fs, mode);
3269 res = follow_path(&dj, path);
3272 if (dj.fn[NSFLAG] & NS_NONAME) {
3273 res = FR_INVALID_NAME;
3277 res = chk_lock(&dj, (mode & ~FA_READ) ? 1 : 0);
3282 if (mode & (FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW)) {
3284 if (res == FR_NO_FILE)
3286 res = enq_lock() ? dir_register(&dj) : FR_TOO_MANY_OPEN_FILES;
3288 res = dir_register(&dj);
3290 mode |= FA_CREATE_ALWAYS;
3293 if (dj.obj.attr & (AM_RDO | AM_DIR)) {
3296 if (mode & FA_CREATE_NEW) res = FR_EXIST;
3299 if (res == FR_OK && (mode & FA_CREATE_ALWAYS)) {
3302 if (fs->fs_type == FS_EXFAT) {
3305 fp->obj.sclust = ld_dword(fs->dirbuf + XDIR_FstClus);
3306 fp->obj.objsize = ld_qword(fs->dirbuf + XDIR_FileSize);
3307 fp->obj.stat = fs->dirbuf[XDIR_GenFlags] & 2;
3309 st_dword(fs->dirbuf + XDIR_CrtTime, dw);
3310 fs->dirbuf[XDIR_CrtTime10] = 0;
3311 st_dword(fs->dirbuf + XDIR_ModTime, dw);
3312 fs->dirbuf[XDIR_ModTime10] = 0;
3313 fs->dirbuf[XDIR_Attr] = AM_ARC;
3314 st_dword(fs->dirbuf + XDIR_FstClus, 0);
3315 st_qword(fs->dirbuf + XDIR_FileSize, 0);
3316 st_qword(fs->dirbuf + XDIR_ValidFileSize, 0);
3317 fs->dirbuf[XDIR_GenFlags] = 1;
3318 res = store_xdir(&dj);
3319 if (res == FR_OK && fp->obj.sclust) {
3320 res = remove_chain(&fp->obj, fp->obj.sclust, 0);
3321 fs->last_clst = fp->obj.sclust - 1;
3327 st_dword(dj.dir + DIR_CrtTime, dw);
3328 st_dword(dj.dir + DIR_ModTime, dw);
3329 dj.dir[DIR_Attr] = AM_ARC;
3330 cl = ld_clust(fs, dj.dir);
3331 st_clust(fs, dj.dir, 0);
3332 st_dword(dj.dir + DIR_FileSize, 0);
3337 res = remove_chain(&dj.obj, cl, 0);
3339 res = move_window(fs, dw);
3340 fs->last_clst = cl - 1;
3348 if (dj.obj.attr & AM_DIR) {
3351 if ((mode & FA_WRITE) && (dj.obj.attr & AM_RDO)) {
3358 if (mode & FA_CREATE_ALWAYS)
3359 mode |= FA_MODIFIED;
3360 fp->dir_sect = fs->winsect;
3361 fp->dir_ptr = dj.dir;
3363 fp->obj.lockid = inc_lock(&dj, (mode & ~FA_READ) ? 1 : 0);
3364 if (!fp->obj.lockid) res = FR_INT_ERR;
3369 if (dj.fn[NSFLAG] & NS_NONAME) {
3370 res = FR_INVALID_NAME;
3372 if (dj.obj.attr & AM_DIR) {
3381 if (fs->fs_type == FS_EXFAT) {
3382 fp->obj.sclust = ld_dword(fs->dirbuf + XDIR_FstClus);
3383 fp->obj.objsize = ld_qword(fs->dirbuf + XDIR_FileSize);
3384 fp->obj.stat = fs->dirbuf[XDIR_GenFlags] & 2;
3385 fp->obj.c_scl = dj.obj.sclust;
3386 fp->obj.c_size = ((DWORD)dj.obj.objsize & 0xFFFFFF00) | dj.obj.stat;
3387 fp->obj.c_ofs = dj.blk_ofs;
3391 fp->obj.sclust = ld_clust(fs, dj.dir);
3392 fp->obj.objsize = ld_dword(dj.dir + DIR_FileSize);
3398 fp->obj.id = fs->id;
3405 mem_set(fp->buf, 0, _MAX_SS);
3407 if ((mode & FA_SEEKEND) && fp->obj.objsize > 0) {
3408 fp->fptr = fp->obj.objsize;
3409 bcs = (DWORD)fs->csize * SS(fs);
3410 clst = fp->obj.sclust;
3411 for (ofs = fp->obj.objsize; res == FR_OK && ofs > bcs; ofs -= bcs) {
3412 clst = get_fat(&fp->obj, clst);
3413 if (clst <= 1) res = FR_INT_ERR;
3414 if (clst == 0xFFFFFFFF) res = FR_DISK_ERR;
3417 if (res == FR_OK && ofs % SS(fs)) {
3418 if ((sc = clust2sect(fs, clst)) == 0) {
3421 fp->sect = sc + (DWORD)(ofs / SS(fs));
3423 if (disk_read(fs->drv, fp->buf, fp->sect, 1) != RES_OK) res = FR_DISK_ERR;
3434 if (res != FR_OK) fp->obj.fs = 0;
3457 UINT rcnt, cc, csect;
3458 BYTE *rbuff = (BYTE*)buff;
3462 res = validate(&fp->obj, &fs);
3463 if (res != FR_OK || (res = (FRESULT)fp->err) != FR_OK) LEAVE_FF(fs, res);
3464 if (!(fp->flag & FA_READ)) LEAVE_FF(fs, FR_DENIED);
3465 remain = fp->obj.objsize - fp->fptr;
3466 if (btr > remain) btr = (UINT)remain;
3469 rbuff += rcnt, fp->fptr += rcnt, *br += rcnt, btr -= rcnt) {
3470 if (fp->fptr % SS(fs) == 0) {
3471 csect = (UINT)(fp->fptr / SS(fs) & (fs->csize - 1));
3473 if (fp->fptr == 0) {
3474 clst = fp->obj.sclust;
3478 clst = clmt_clust(fp, fp->fptr);
3482 clst = get_fat(&fp->obj, fp->clust);
3485 if (clst < 2) ABORT(fs, FR_INT_ERR);
3486 if (clst == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR);
3489 sect = clust2sect(fs, fp->clust);
3490 if (!sect) ABORT(fs, FR_INT_ERR);
3494 if (csect + cc > fs->csize) {
3495 cc = fs->csize - csect;
3497 if (disk_read(fs->drv, rbuff, sect, cc) != RES_OK) ABORT(fs, FR_DISK_ERR);
3498 #if !_FS_READONLY && _FS_MINIMIZE <= 2 3500 if (fs->wflag && fs->winsect - sect < cc) {
3501 mem_cpy(rbuff + ((fs->winsect - sect) * SS(fs)), fs->win, SS(fs));
3504 if ((fp->flag & FA_DIRTY) && fp->sect - sect < cc) {
3505 mem_cpy(rbuff + ((fp->sect - sect) * SS(fs)), fp->buf, SS(fs));
3513 if (fp->sect != sect) {
3515 if (fp->flag & FA_DIRTY) {
3516 if (disk_write(fs->drv, fp->buf, fp->sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR);
3517 fp->flag &= (BYTE)~FA_DIRTY;
3520 if (disk_read(fs->drv, fp->buf, sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR);
3525 rcnt = SS(fs) - (UINT)fp->fptr % SS(fs);
3526 if (rcnt > btr) rcnt = btr;
3528 if (move_window(fs, fp->sect) != FR_OK) ABORT(fs, FR_DISK_ERR);
3529 mem_cpy(rbuff, fs->win + fp->fptr % SS(fs), rcnt);
3531 mem_cpy(rbuff, fp->buf + fp->fptr % SS(fs), rcnt);
3535 LEAVE_FF(fs, FR_OK);
3556 UINT wcnt, cc, csect;
3557 const BYTE *wbuff = (
const BYTE*)buff;
3561 res = validate(&fp->obj, &fs);
3562 if (res != FR_OK || (res = (FRESULT)fp->err) != FR_OK) LEAVE_FF(fs, res);
3563 if (!(fp->flag & FA_WRITE)) LEAVE_FF(fs, FR_DENIED);
3566 if ((!
_FS_EXFAT || fs->fs_type != FS_EXFAT) && (DWORD)(fp->fptr + btw) < (DWORD)fp->fptr) {
3567 btw = (UINT)(0xFFFFFFFF - (DWORD)fp->fptr);
3571 wbuff += wcnt, fp->fptr += wcnt, fp->obj.objsize = (fp->fptr > fp->obj.objsize) ? fp->fptr : fp->obj.objsize, *bw += wcnt, btw -= wcnt) {
3572 if (fp->fptr % SS(fs) == 0) {
3573 csect = (UINT)(fp->fptr / SS(fs)) & (fs->csize - 1);
3575 if (fp->fptr == 0) {
3576 clst = fp->obj.sclust;
3578 clst = create_chain(&fp->obj, 0);
3583 clst = clmt_clust(fp, fp->fptr);
3587 clst = create_chain(&fp->obj, fp->clust);
3590 if (clst == 0)
break;
3591 if (clst == 1) ABORT(fs, FR_INT_ERR);
3592 if (clst == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR);
3594 if (fp->obj.sclust == 0) fp->obj.sclust = clst;
3597 if (fs->winsect == fp->sect && sync_window(fs) != FR_OK) ABORT(fs, FR_DISK_ERR);
3599 if (fp->flag & FA_DIRTY) {
3600 if (disk_write(fs->drv, fp->buf, fp->sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR);
3601 fp->flag &= (BYTE)~FA_DIRTY;
3604 sect = clust2sect(fs, fp->clust);
3605 if (!sect) ABORT(fs, FR_INT_ERR);
3609 if (csect + cc > fs->csize) {
3610 cc = fs->csize - csect;
3612 if (disk_write(fs->drv, wbuff, sect, cc) != RES_OK) ABORT(fs, FR_DISK_ERR);
3613 #if _FS_MINIMIZE <= 2 3615 if (fs->winsect - sect < cc) {
3616 mem_cpy(fs->win, wbuff + ((fs->winsect - sect) * SS(fs)), SS(fs));
3620 if (fp->sect - sect < cc) {
3621 mem_cpy(fp->buf, wbuff + ((fp->sect - sect) * SS(fs)), SS(fs));
3622 fp->flag &= (BYTE)~FA_DIRTY;
3630 if (fp->fptr >= fp->obj.objsize) {
3631 if (sync_window(fs) != FR_OK) ABORT(fs, FR_DISK_ERR);
3635 if (fp->sect != sect &&
3636 fp->fptr < fp->obj.objsize &&
3637 disk_read(fs->drv, fp->buf, sect, 1) != RES_OK) {
3638 ABORT(fs, FR_DISK_ERR);
3643 wcnt = SS(fs) - (UINT)fp->fptr % SS(fs);
3644 if (wcnt > btw) wcnt = btw;
3646 if (move_window(fs, fp->sect) != FR_OK) ABORT(fs, FR_DISK_ERR);
3647 mem_cpy(fs->win + fp->fptr % SS(fs), wbuff, wcnt);
3650 mem_cpy(fp->buf + fp->fptr % SS(fs), wbuff, wcnt);
3651 fp->flag |= FA_DIRTY;
3655 fp->flag |= FA_MODIFIED;
3657 LEAVE_FF(fs, FR_OK);
3679 res = validate(&fp->obj, &fs);
3681 if (fp->flag & FA_MODIFIED) {
3683 if (fp->flag & FA_DIRTY) {
3684 if (disk_write(fs->drv, fp->buf, fp->sect, 1) != RES_OK) LEAVE_FF(fs, FR_DISK_ERR);
3685 fp->flag &= (BYTE)~FA_DIRTY;
3691 if (fs->fs_type == FS_EXFAT) {
3692 res = fill_fat_chain(&fp->obj);
3697 res = load_obj_dir(&dj, &fp->obj);
3699 fs->dirbuf[XDIR_Attr] |= AM_ARC;
3700 fs->dirbuf[XDIR_GenFlags] = fp->obj.stat | 1;
3701 st_dword(fs->dirbuf + XDIR_FstClus, fp->obj.sclust);
3702 st_qword(fs->dirbuf + XDIR_FileSize, fp->obj.objsize);
3703 st_qword(fs->dirbuf + XDIR_ValidFileSize, fp->obj.objsize);
3704 st_dword(fs->dirbuf + XDIR_ModTime, tm);
3705 fs->dirbuf[XDIR_ModTime10] = 0;
3706 st_dword(fs->dirbuf + XDIR_AccTime, 0);
3707 res = store_xdir(&dj);
3710 fp->flag &= (BYTE)~FA_MODIFIED;
3718 res = move_window(fs, fp->dir_sect);
3721 dir[DIR_Attr] |= AM_ARC;
3722 st_clust(fp->obj.fs, dir, fp->obj.sclust);
3723 st_dword(dir + DIR_FileSize, (DWORD)fp->obj.objsize);
3724 st_dword(dir + DIR_ModTime, tm);
3725 st_word(dir + DIR_LstAccDate, 0);
3728 fp->flag &= (BYTE)~FA_MODIFIED;
3758 res = validate(&fp->obj, &fs);
3761 res = dec_lock(fp->obj.lockid);
3768 unlock_fs(fs, FR_OK);
3792 vol = get_ldnumber(&path);
3793 if (vol < 0)
return FR_INVALID_DRIVE;
3795 CurrVol = (BYTE)vol;
3812 res = find_volume(&path, &fs, 0);
3816 res = follow_path(&dj, path);
3818 if (dj.fn[NSFLAG] & NS_NONAME) {
3819 fs->cdir = dj.obj.sclust;
3821 if (fs->fs_type == FS_EXFAT) {
3822 fs->cdc_scl = dj.obj.c_scl;
3823 fs->cdc_size = dj.obj.c_size;
3824 fs->cdc_ofs = dj.obj.c_ofs;
3828 if (dj.obj.attr & AM_DIR) {
3830 if (fs->fs_type == FS_EXFAT) {
3831 fs->cdir = ld_dword(fs->dirbuf + XDIR_FstClus);
3832 fs->cdc_scl = dj.obj.sclust;
3833 fs->cdc_size = ((DWORD)dj.obj.objsize & 0xFFFFFF00) | dj.obj.stat;
3834 fs->cdc_ofs = dj.blk_ofs;
3838 fs->cdir = ld_clust(fs, dj.dir);
3846 if (res == FR_NO_FILE) res = FR_NO_PATH;
3871 res = find_volume((
const TCHAR**)&buff, &fs, 0);
3876 if (!
_FS_EXFAT || fs->fs_type != FS_EXFAT) {
3877 dj.obj.sclust = fs->cdir;
3878 while ((ccl = dj.obj.sclust) != 0) {
3879 res = dir_sdi(&dj, 1 * SZDIRE);
3880 if (res != FR_OK)
break;
3881 res = move_window(fs, dj.sect);
3882 if (res != FR_OK)
break;
3883 dj.obj.sclust = ld_clust(fs, dj.dir);
3884 res = dir_sdi(&dj, 0);
3885 if (res != FR_OK)
break;
3887 res = dir_read(&dj, 0);
3888 if (res != FR_OK)
break;
3889 if (ccl == ld_clust(fs, dj.dir))
break;
3890 res = dir_next(&dj, 0);
3891 }
while (res == FR_OK);
3892 if (res == FR_NO_FILE) res = FR_INT_ERR;
3893 if (res != FR_OK)
break;
3894 get_fileinfo(&dj, &fno);
3895 for (n = 0; fno.fname[n]; n++) ;
3897 res = FR_NOT_ENOUGH_CORE;
break;
3899 while (n) buff[--i] = fno.fname[--n];
3906 *tp++ =
'0' + CurrVol;
3929 #if _FS_MINIMIZE <= 2 3941 DWORD clst, bcs, nsect;
3944 DWORD cl, pcl, ncl, tcl, dsc, tlen, ulen, *tbl;
3947 res = validate(&fp->obj, &fs);
3948 if (res != FR_OK || (res = (FRESULT)fp->err) != FR_OK) LEAVE_FF(fs, res);
3951 if (ofs == CREATE_LINKMAP) {
3953 tlen = *tbl++; ulen = 2;
3954 cl = fp->obj.sclust;
3958 tcl = cl; ncl = 0; ulen += 2;
3961 cl = get_fat(&fp->obj, cl);
3962 if (cl <= 1) ABORT(fs, FR_INT_ERR);
3963 if (cl == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR);
3964 }
while (cl == pcl + 1);
3966 *tbl++ = ncl; *tbl++ = tcl;
3968 }
while (cl < fs->n_fatent);
3974 res = FR_NOT_ENOUGH_CORE;
3977 if (ofs > fp->obj.objsize) ofs = fp->obj.objsize;
3980 fp->clust = clmt_clust(fp, ofs - 1);
3981 dsc = clust2sect(fs, fp->clust);
3982 if (!dsc) ABORT(fs, FR_INT_ERR);
3983 dsc += (DWORD)((ofs - 1) / SS(fs)) & (fs->csize - 1);
3984 if (fp->fptr % SS(fs) && dsc != fp->sect) {
3987 if (fp->flag & FA_DIRTY) {
3988 if (disk_write(fs->drv, fp->buf, fp->sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR);
3989 fp->flag &= (BYTE)~FA_DIRTY;
3992 if (disk_read(fs->drv, fp->buf, dsc, 1) != RES_OK) ABORT(fs, FR_DISK_ERR);
4004 if (fs->fs_type != FS_EXFAT && ofs >= 0x100000000) ofs = 0xFFFFFFFF;
4006 if (ofs > fp->obj.objsize && (
_FS_READONLY || !(fp->flag & FA_WRITE))) {
4007 ofs = fp->obj.objsize;
4010 fp->fptr = nsect = 0;
4012 bcs = (DWORD)fs->csize * SS(fs);
4014 (ofs - 1) / bcs >= (ifptr - 1) / bcs) {
4015 fp->fptr = (ifptr - 1) & ~(FSIZE_t)(bcs - 1);
4019 clst = fp->obj.sclust;
4022 clst = create_chain(&fp->obj, 0);
4023 if (clst == 1) ABORT(fs, FR_INT_ERR);
4024 if (clst == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR);
4025 fp->obj.sclust = clst;
4032 ofs -= bcs; fp->fptr += bcs;
4034 if (fp->flag & FA_WRITE) {
4035 if (
_FS_EXFAT && fp->fptr > fp->obj.objsize) {
4036 fp->obj.objsize = fp->fptr;
4037 fp->flag |= FA_MODIFIED;
4039 clst = create_chain(&fp->obj, clst);
4046 clst = get_fat(&fp->obj, clst);
4048 if (clst == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR);
4049 if (clst <= 1 || clst >= fs->n_fatent) ABORT(fs, FR_INT_ERR);
4054 nsect = clust2sect(fs, clst);
4055 if (!nsect) ABORT(fs, FR_INT_ERR);
4056 nsect += (DWORD)(ofs / SS(fs));
4061 fp->obj.objsize = fp->fptr;
4062 fp->flag |= FA_MODIFIED;
4064 if (fp->fptr % SS(fs) && nsect != fp->sect) {
4067 if (fp->flag & FA_DIRTY) {
4068 if (disk_write(fs->drv, fp->buf, fp->sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR);
4069 fp->flag &= (BYTE)~FA_DIRTY;
4072 if (disk_read(fs->drv, fp->buf, nsect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR);
4083 #if _FS_MINIMIZE <= 1 4099 if (!dp)
return FR_INVALID_OBJECT;
4103 res = find_volume(&path, &fs, 0);
4107 res = follow_path(dp, path);
4109 if (!(dp->fn[NSFLAG] & NS_NONAME)) {
4110 if (obj->attr & AM_DIR) {
4112 if (fs->fs_type == FS_EXFAT) {
4113 obj->c_scl = obj->sclust;
4114 obj->c_size = ((DWORD)obj->objsize & 0xFFFFFF00) | obj->stat;
4115 obj->c_ofs = dp->blk_ofs;
4116 obj->sclust = ld_dword(fs->dirbuf + XDIR_FstClus);
4117 obj->objsize = ld_qword(fs->dirbuf + XDIR_FileSize);
4118 obj->stat = fs->dirbuf[XDIR_GenFlags] & 2;
4122 obj->sclust = ld_clust(fs, dp->dir);
4130 res = dir_sdi(dp, 0);
4134 obj->lockid = inc_lock(dp, 0);
4135 if (!obj->lockid) res = FR_TOO_MANY_OPEN_FILES;
4144 if (res == FR_NO_FILE) res = FR_NO_PATH;
4146 if (res != FR_OK) obj->fs = 0;
4158 FRESULT f_closedir (
4166 res = validate(&dp->obj, &fs);
4169 if (dp->obj.lockid) {
4170 res = dec_lock(dp->obj.lockid);
4178 unlock_fs(fs, FR_OK);
4201 res = validate(&dp->obj, &fs);
4204 res = dir_sdi(dp, 0);
4207 res = dir_read(dp, 0);
4208 if (res == FR_NO_FILE) res = FR_OK;
4210 get_fileinfo(dp, fno);
4211 res = dir_next(dp, 0);
4212 if (res == FR_NO_FILE) res = FR_OK;
4227 FRESULT f_findnext (
4236 res = f_readdir(dp, fno);
4237 if (res != FR_OK || !fno || !fno->fname[0])
break;
4238 if (pattern_matching(dp->pat, fno->fname, 0, 0))
break;
4239 #if _USE_LFN != 0 && _USE_FIND == 2 4240 if (pattern_matching(dp->pat, fno->altname, 0, 0))
break;
4252 FRESULT f_findfirst (
4256 const TCHAR* pattern
4263 res = f_opendir(dp, path);
4265 res = f_findnext(dp, fno);
4274 #if _FS_MINIMIZE == 0 4290 res = find_volume(&path, &dj.obj.fs, 0);
4292 INIT_NAMBUF(dj.obj.fs);
4293 res = follow_path(&dj, path);
4295 if (dj.fn[NSFLAG] & NS_NONAME) {
4296 res = FR_INVALID_NAME;
4298 if (fno) get_fileinfo(&dj, fno);
4304 LEAVE_FF(dj.obj.fs, res);
4322 DWORD nfree, clst, sect, stat;
4329 res = find_volume(&path, &fs, 0);
4333 if (fs->free_clst <= fs->n_fatent - 2) {
4334 *nclst = fs->free_clst;
4338 if (fs->fs_type == FS_FAT12) {
4339 clst = 2; obj.fs = fs;
4341 stat = get_fat(&obj, clst);
4342 if (stat == 0xFFFFFFFF) { res = FR_DISK_ERR;
break; }
4343 if (stat == 1) { res = FR_INT_ERR;
break; }
4344 if (stat == 0) nfree++;
4345 }
while (++clst < fs->n_fatent);
4348 if (fs->fs_type == FS_EXFAT) {
4352 clst = fs->n_fatent - 2;
4353 sect = fs->database;
4356 if (i == 0 && (res = move_window(fs, sect++)) != FR_OK)
break;
4357 for (b = 8, bm = fs->win[i]; b && clst; b--, clst--) {
4358 if (!(bm & 1)) nfree++;
4361 i = (i + 1) % SS(fs);
4366 clst = fs->n_fatent; sect = fs->fatbase;
4370 res = move_window(fs, sect++);
4371 if (res != FR_OK)
break;
4375 if (fs->fs_type == FS_FAT16) {
4376 if (ld_word(p) == 0) nfree++;
4379 if ((ld_dword(p) & 0x0FFFFFFF) == 0) nfree++;
4386 fs->free_clst = nfree;
4401 FRESULT f_truncate (
4410 res = validate(&fp->obj, &fs);
4411 if (res != FR_OK || (res = (FRESULT)fp->err) != FR_OK) LEAVE_FF(fs, res);
4412 if (!(fp->flag & FA_WRITE)) LEAVE_FF(fs, FR_DENIED);
4414 if (fp->obj.objsize > fp->fptr) {
4415 if (fp->fptr == 0) {
4416 res = remove_chain(&fp->obj, fp->obj.sclust, 0);
4419 ncl = get_fat(&fp->obj, fp->clust);
4421 if (ncl == 0xFFFFFFFF) res = FR_DISK_ERR;
4422 if (ncl == 1) res = FR_INT_ERR;
4423 if (res == FR_OK && ncl < fs->n_fatent) {
4424 res = remove_chain(&fp->obj, ncl, fp->clust);
4427 fp->obj.objsize = fp->fptr;
4428 fp->flag |= FA_MODIFIED;
4430 if (res == FR_OK && (fp->flag & FA_DIRTY)) {
4431 if (disk_write(fs->drv, fp->buf, fp->sect, 1) != RES_OK) {
4434 fp->flag &= (BYTE)~FA_DIRTY;
4438 if (res != FR_OK) ABORT(fs, res);
4466 res = find_volume(&path, &fs, FA_WRITE);
4470 res = follow_path(&dj, path);
4471 if (
_FS_RPATH && res == FR_OK && (dj.fn[NSFLAG] & NS_DOT)) {
4472 res = FR_INVALID_NAME;
4475 if (res == FR_OK) res = chk_lock(&dj, 2);
4478 if (dj.fn[NSFLAG] & NS_NONAME) {
4479 res = FR_INVALID_NAME;
4481 if (dj.obj.attr & AM_RDO) {
4488 if (fs->fs_type == FS_EXFAT) {
4489 obj.sclust = dclst = ld_dword(fs->dirbuf + XDIR_FstClus);
4490 obj.objsize = ld_qword(fs->dirbuf + XDIR_FileSize);
4491 obj.stat = fs->dirbuf[XDIR_GenFlags] & 2;
4495 dclst = ld_clust(fs, dj.dir);
4497 if (dj.obj.attr & AM_DIR) {
4499 if (dclst == fs->cdir) {
4505 sdj.obj.sclust = dclst;
4507 if (fs->fs_type == FS_EXFAT) {
4508 sdj.obj.objsize = obj.objsize;
4509 sdj.obj.stat = obj.stat;
4512 res = dir_sdi(&sdj, 0);
4514 res = dir_read(&sdj, 0);
4515 if (res == FR_OK) res = FR_DENIED;
4516 if (res == FR_NO_FILE) res = FR_OK;
4522 res = dir_remove(&dj);
4523 if (res == FR_OK && dclst) {
4525 res = remove_chain(&obj, dclst, 0);
4527 res = remove_chain(&dj.obj, dclst, 0);
4530 if (res == FR_OK) res = sync_fs(fs);
4555 DWORD dsc, dcl, pcl, tm;
4560 res = find_volume(&path, &fs, FA_WRITE);
4564 res = follow_path(&dj, path);
4565 if (res == FR_OK) res = FR_EXIST;
4566 if (
_FS_RPATH && res == FR_NO_FILE && (dj.fn[NSFLAG] & NS_DOT)) {
4567 res = FR_INVALID_NAME;
4569 if (res == FR_NO_FILE) {
4570 dcl = create_chain(&dj.obj, 0);
4571 dj.obj.objsize = (DWORD)fs->csize * SS(fs);
4573 if (dcl == 0) res = FR_DENIED;
4574 if (dcl == 1) res = FR_INT_ERR;
4575 if (dcl == 0xFFFFFFFF) res = FR_DISK_ERR;
4576 if (res == FR_OK) res = sync_window(fs);
4579 dsc = clust2sect(fs, dcl);
4581 mem_set(dir, 0, SS(fs));
4582 if (!
_FS_EXFAT || fs->fs_type != FS_EXFAT) {
4583 mem_set(dir + DIR_Name,
' ', 11);
4584 dir[DIR_Name] =
'.';
4585 dir[DIR_Attr] = AM_DIR;
4586 st_dword(dir + DIR_ModTime, tm);
4587 st_clust(fs, dir, dcl);
4588 mem_cpy(dir + SZDIRE, dir, SZDIRE);
4589 dir[SZDIRE + 1] =
'.'; pcl = dj.obj.sclust;
4590 if (fs->fs_type == FS_FAT32 && pcl == fs->dirbase) pcl = 0;
4591 st_clust(fs, dir + SZDIRE, pcl);
4593 for (n = fs->csize; n; n--) {
4594 fs->winsect = dsc++;
4596 res = sync_window(fs);
4597 if (res != FR_OK)
break;
4598 mem_set(dir, 0, SS(fs));
4601 if (res == FR_OK) res = dir_register(&dj);
4604 if (fs->fs_type == FS_EXFAT) {
4605 st_dword(fs->dirbuf + XDIR_ModTime, tm);
4606 st_dword(fs->dirbuf + XDIR_FstClus, dcl);
4607 st_dword(fs->dirbuf + XDIR_FileSize, (DWORD)dj.obj.objsize);
4608 st_dword(fs->dirbuf + XDIR_ValidFileSize, (DWORD)dj.obj.objsize);
4609 fs->dirbuf[XDIR_GenFlags] = 3;
4610 fs->dirbuf[XDIR_Attr] = AM_DIR;
4611 res = store_xdir(&dj);
4616 st_dword(dir + DIR_ModTime, tm);
4617 st_clust(fs, dir, dcl);
4618 dir[DIR_Attr] = AM_DIR;
4621 if (res == FR_OK) res = sync_fs(fs);
4623 remove_chain(&dj.obj, dcl, 0);
4640 const TCHAR* path_old,
4641 const TCHAR* path_new
4647 BYTE buf[
_FS_EXFAT ? SZDIRE * 2 : 24], *dir;
4652 get_ldnumber(&path_new);
4653 res = find_volume(&path_old, &fs, FA_WRITE);
4657 res = follow_path(&djo, path_old);
4658 if (res == FR_OK && (djo.fn[NSFLAG] & (NS_DOT | NS_NONAME))) res = FR_INVALID_NAME;
4660 if (res == FR_OK) res = chk_lock(&djo, 2);
4664 if (fs->fs_type == FS_EXFAT) {
4668 mem_cpy(buf, fs->dirbuf, SZDIRE * 2);
4669 mem_cpy(&djn, &djo,
sizeof djo);
4670 res = follow_path(&djn, path_new);
4672 res = (djn.obj.sclust == djo.obj.sclust && djn.dptr == djo.dptr) ? FR_NO_FILE : FR_EXIST;
4674 if (res == FR_NO_FILE) {
4675 res = dir_register(&djn);
4677 nf = fs->dirbuf[XDIR_NumSec]; nn = fs->dirbuf[XDIR_NumName];
4678 nh = ld_word(fs->dirbuf + XDIR_NameHash);
4679 mem_cpy(fs->dirbuf, buf, SZDIRE * 2);
4680 fs->dirbuf[XDIR_NumSec] = nf; fs->dirbuf[XDIR_NumName] = nn;
4681 st_word(fs->dirbuf + XDIR_NameHash, nh);
4683 res = store_xdir(&djn);
4689 mem_cpy(buf, djo.dir + DIR_Attr, 21);
4690 mem_cpy(&djn, &djo,
sizeof (DIR));
4691 res = follow_path(&djn, path_new);
4693 res = (djn.obj.sclust == djo.obj.sclust && djn.dptr == djo.dptr) ? FR_NO_FILE : FR_EXIST;
4695 if (res == FR_NO_FILE) {
4696 res = dir_register(&djn);
4699 mem_cpy(dir + 13, buf + 2, 19);
4700 dir[DIR_Attr] = buf[0] | AM_ARC;
4702 if ((dir[DIR_Attr] & AM_DIR) && djo.obj.sclust != djn.obj.sclust) {
4703 dw = clust2sect(fs, ld_clust(fs, dir));
4708 res = move_window(fs, dw);
4709 dir = fs->win + SZDIRE * 1;
4710 if (res == FR_OK && dir[1] ==
'.') {
4711 st_clust(fs, dir, djn.obj.sclust);
4720 res = dir_remove(&djo);
4740 #if _USE_CHMOD && !_FS_READONLY 4757 res = find_volume(&path, &fs, FA_WRITE);
4761 res = follow_path(&dj, path);
4762 if (res == FR_OK && (dj.fn[NSFLAG] & (NS_DOT | NS_NONAME))) res = FR_INVALID_NAME;
4764 mask &= AM_RDO|AM_HID|AM_SYS|AM_ARC;
4766 if (fs->fs_type == FS_EXFAT) {
4767 fs->dirbuf[XDIR_Attr] = (attr & mask) | (fs->dirbuf[XDIR_Attr] & (BYTE)~mask);
4768 res = store_xdir(&dj);
4772 dj.dir[DIR_Attr] = (attr & mask) | (dj.dir[DIR_Attr] & (BYTE)~mask);
4775 if (res == FR_OK) res = sync_fs(fs);
4801 res = find_volume(&path, &fs, FA_WRITE);
4805 res = follow_path(&dj, path);
4806 if (res == FR_OK && (dj.fn[NSFLAG] & (NS_DOT | NS_NONAME))) res = FR_INVALID_NAME;
4809 if (fs->fs_type == FS_EXFAT) {
4810 st_dword(fs->dirbuf + XDIR_ModTime, (DWORD)fno->fdate << 16 | fno->ftime);
4811 res = store_xdir(&dj);
4815 st_dword(dj.dir + DIR_ModTime, (DWORD)fno->fdate << 16 | fno->ftime);
4818 if (res == FR_OK) res = sync_fs(fs);
4835 FRESULT f_getlabel (
4845 #if _LFN_UNICODE || _FS_EXFAT 4850 res = find_volume(&path, &fs, 0);
4853 if (res == FR_OK && label) {
4854 dj.obj.fs = fs; dj.obj.sclust = 0;
4855 res = dir_sdi(&dj, 0);
4857 res = dir_read(&dj, 1);
4860 if (fs->fs_type == FS_EXFAT) {
4861 for (si = di = 0; si < dj.dir[XDIR_NumLabel]; si++) {
4862 w = ld_word(dj.dir + XDIR_Label + si * 2);
4866 w = ff_convert(w, 0);
4867 if (w == 0) w =
'?';
4868 if (_DF1S && w >= 0x100) label[di++] = (char)(w >> 8);
4869 label[di++] = (char)w;
4879 w = (si < 11) ? dj.dir[si++] :
' ';
4880 if (IsDBCS1(w) && si < 11 && IsDBCS2(dj.dir[si])) {
4881 w = w << 8 | dj.dir[si++];
4883 label[di++] = ff_convert(w, 1);
4885 label[di++] = dj.dir[si++];
4891 }
while (label[--di] ==
' ');
4895 if (res == FR_NO_FILE) {
4902 if (res == FR_OK && vsn) {
4903 res = move_window(fs, fs->volbase);
4905 switch (fs->fs_type) {
4906 case FS_EXFAT: di = BPB_VolIDEx;
break;
4907 case FS_FAT32: di = BS_VolID32;
break;
4908 default: di = BS_VolID;
4910 *vsn = ld_dword(fs->win + di);
4924 FRESULT f_setlabel (
4934 static const char badchr[] =
"\"*+,.:;<=>\?[]|\x7F";
4938 res = find_volume(&label, &fs, FA_WRITE);
4939 if (res != FR_OK) LEAVE_FF(fs, res);
4943 for (slen = 0; (UINT)label[slen] >=
' '; slen++) ;
4946 if (fs->fs_type == FS_EXFAT) {
4947 for (i = j = 0; i < slen; ) {
4951 w = (i < slen && IsDBCS2(label[i])) ? w << 8 | (BYTE)label[i++] : 0;
4953 w = ff_convert(w, 1);
4955 if (w == 0 || chk_chr(badchr, w) || j == 22) {
4956 LEAVE_FF(fs, FR_INVALID_NAME);
4958 st_word(dirvn + j, w); j += 2;
4964 for ( ; slen && label[slen - 1] ==
' '; slen--) ;
4966 dirvn[0] = 0; i = j = 0;
4969 w = ff_convert(ff_wtoupper(label[i++]), 0);
4971 w = (BYTE)label[i++];
4973 w = (j < 10 && i < slen && IsDBCS2(label[i])) ? w << 8 | (BYTE)label[i++] : 0;
4976 w = ff_convert(ff_wtoupper(ff_convert(w, 1)), 0);
4978 if (IsLower(w)) w -= 0x20;
4980 if (w >= 0x80) w = ExCvt[w - 0x80];
4982 if (!_DF1S && w >= 0x80) w = 0;
4986 if (w == 0 || chk_chr(badchr, w) || j >= (UINT)((w >= 0x100) ? 10 : 11)) {
4987 LEAVE_FF(fs, FR_INVALID_NAME);
4989 if (w >= 0x100) dirvn[j++] = (BYTE)(w >> 8);
4990 dirvn[j++] = (BYTE)w;
4992 while (j < 11) dirvn[j++] =
' ';
4993 if (dirvn[0] == DDEM) LEAVE_FF(fs, FR_INVALID_NAME);
4999 res = dir_sdi(&dj, 0);
5001 res = dir_read(&dj, 1);
5003 if (
_FS_EXFAT && fs->fs_type == FS_EXFAT) {
5004 dj.dir[XDIR_NumLabel] = (BYTE)(slen / 2);
5005 mem_cpy(dj.dir + XDIR_Label, dirvn, slen);
5008 mem_cpy(dj.dir, dirvn, 11);
5010 dj.dir[DIR_Name] = DDEM;
5016 if (res == FR_NO_FILE) {
5019 res = dir_alloc(&dj, 1);
5021 mem_set(dj.dir, 0, SZDIRE);
5022 if (
_FS_EXFAT && fs->fs_type == FS_EXFAT) {
5023 dj.dir[XDIR_Type] = 0x83;
5024 dj.dir[XDIR_NumLabel] = (BYTE)(slen / 2);
5025 mem_cpy(dj.dir + XDIR_Label, dirvn, slen);
5027 dj.dir[DIR_Attr] = AM_VOL;
5028 mem_cpy(dj.dir, dirvn, 11);
5046 #if _USE_EXPAND && !_FS_READONLY 5059 DWORD n, clst, stcl, scl, ncl, tcl, lclst;
5062 res = validate(&fp->obj, &fs);
5063 if (res != FR_OK || (res = (FRESULT)fp->err) != FR_OK) LEAVE_FF(fs, res);
5064 if (fsz == 0 || fp->obj.objsize != 0 || !(fp->flag & FA_WRITE)) LEAVE_FF(fs, FR_DENIED);
5066 if (fs->fs_type != FS_EXFAT && fsz >= 0x100000000) LEAVE_FF(fs, FR_DENIED);
5068 n = (DWORD)fs->csize * SS(fs);
5069 tcl = (DWORD)(fsz / n) + ((fsz & (n - 1)) ? 1 : 0);
5070 stcl = fs->last_clst; lclst = 0;
5071 if (stcl < 2 || stcl >= fs->n_fatent) stcl = 2;
5074 if (fs->fs_type == FS_EXFAT) {
5075 scl = find_bitmap(fs, stcl, tcl);
5076 if (scl == 0) res = FR_DENIED;
5077 if (scl == 0xFFFFFFFF) res = FR_DISK_ERR;
5080 res = change_bitmap(fs, scl, tcl, 1);
5081 lclst = scl + tcl - 1;
5089 scl = clst = stcl; ncl = 0;
5091 n = get_fat(&fp->obj, clst);
5092 if (++clst >= fs->n_fatent) clst = 2;
5093 if (n == 1) { res = FR_INT_ERR;
break; }
5094 if (n == 0xFFFFFFFF) { res = FR_DISK_ERR;
break; }
5096 if (++ncl == tcl)
break;
5098 scl = clst; ncl = 0;
5100 if (clst == stcl) { res = FR_DENIED;
break; }
5104 for (clst = scl, n = tcl; n; clst++, n--) {
5105 res = put_fat(fs, clst, (n == 1) ? 0xFFFFFFFF : clst + 1);
5106 if (res != FR_OK)
break;
5116 fs->last_clst = lclst;
5118 fp->obj.sclust = scl;
5119 fp->obj.objsize = fsz;
5121 fp->flag |= FA_MODIFIED;
5122 if (fs->free_clst < fs->n_fatent - 2) {
5123 fs->free_clst -= tcl;
5143 UINT (*func)(
const BYTE*,UINT),
5157 res = validate(&fp->obj, &fs);
5158 if (res != FR_OK || (res = (FRESULT)fp->err) != FR_OK) LEAVE_FF(fs, res);
5159 if (!(fp->flag & FA_READ)) LEAVE_FF(fs, FR_DENIED);
5161 remain = fp->obj.objsize - fp->fptr;
5162 if (btf > remain) btf = (UINT)remain;
5164 for ( ; btf && (*func)(0, 0);
5165 fp->fptr += rcnt, *bf += rcnt, btf -= rcnt) {
5166 csect = (UINT)(fp->fptr / SS(fs) & (fs->csize - 1));
5167 if (fp->fptr % SS(fs) == 0) {
5169 clst = (fp->fptr == 0) ?
5170 fp->obj.sclust : get_fat(&fp->obj, fp->clust);
5171 if (clst <= 1) ABORT(fs, FR_INT_ERR);
5172 if (clst == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR);
5176 sect = clust2sect(fs, fp->clust);
5177 if (!sect) ABORT(fs, FR_INT_ERR);
5180 if (move_window(fs, sect) != FR_OK) ABORT(fs, FR_DISK_ERR);
5183 if (fp->sect != sect) {
5185 if (fp->flag & FA_DIRTY) {
5186 if (disk_write(fs->drv, fp->buf, fp->sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR);
5187 fp->flag &= (BYTE)~FA_DIRTY;
5190 if (disk_read(fs->drv, fp->buf, sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR);
5195 rcnt = SS(fs) - (UINT)fp->fptr % SS(fs);
5196 if (rcnt > btf) rcnt = btf;
5197 rcnt = (*func)(dbuf + ((UINT)fp->fptr % SS(fs)), rcnt);
5198 if (!rcnt) ABORT(fs, FR_INT_ERR);
5201 LEAVE_FF(fs, FR_OK);
5207 #if _USE_MKFS && !_FS_READONLY 5220 const UINT n_fats = 1;
5221 const UINT n_rootdir = 512;
5222 static const WORD cst[] = {1, 4, 16, 64, 256, 512, 0};
5223 static const WORD cst32[] = {1, 2, 4, 8, 16, 32, 0};
5224 BYTE fmt, sys, *buf, *pte, pdrv, part;
5226 DWORD szb_buf, sz_buf, sz_blk, n_clst, pau, sect, nsect, n;
5227 DWORD b_vol, b_fat, b_data;
5228 DWORD sz_vol, sz_rsv, sz_fat, sz_dir;
5232 #if _USE_TRIM || _FS_EXFAT 5238 vol = get_ldnumber(&path);
5239 if (vol < 0)
return FR_INVALID_DRIVE;
5240 if (FatFs[vol]) FatFs[vol]->fs_type = 0;
5245 stat = disk_initialize(pdrv);
5246 if (stat & STA_NOINIT)
return FR_NOT_READY;
5247 if (stat & STA_PROTECT)
return FR_WRITE_PROTECTED;
5248 if (disk_ioctl(pdrv, GET_BLOCK_SIZE, &sz_blk) != RES_OK || !sz_blk || sz_blk > 32768 || (sz_blk & (sz_blk - 1))) sz_blk = 1;
5249 #if _MAX_SS != _MIN_SS 5250 if (disk_ioctl(pdrv, GET_SECTOR_SIZE, &ss) != RES_OK)
return FR_DISK_ERR;
5251 if (ss > _MAX_SS || ss <
_MIN_SS || (ss & (ss - 1)))
return FR_DISK_ERR;
5255 if ((au != 0 && au < ss) || au > 0x1000000 || (au & (au - 1)))
return FR_INVALID_PARAMETER;
5261 szb_buf = sz_buf * ss;
5262 if (!szb_buf)
return FR_MKFS_ABORTED;
5267 if (disk_read(pdrv, buf, 0, 1) != RES_OK)
return FR_DISK_ERR;
5268 if (ld_word(buf + BS_55AA) != 0xAA55)
return FR_MKFS_ABORTED;
5269 pte = buf + (MBR_Table + (part - 1) * SZ_PTE);
5270 if (!pte[PTE_System])
return FR_MKFS_ABORTED;
5271 b_vol = ld_dword(pte + PTE_StLba);
5272 sz_vol = ld_dword(pte + PTE_SizLba);
5275 if (disk_ioctl(pdrv, GET_SECTOR_COUNT, &sz_vol) != RES_OK)
return FR_DISK_ERR;
5276 b_vol = (opt & FM_SFD) ? 0 : 63;
5277 if (sz_vol < b_vol)
return FR_MKFS_ABORTED;
5280 if (sz_vol < 128)
return FR_MKFS_ABORTED;
5285 if ((opt & FM_ANY) == FM_EXFAT || sz_vol >= 0x4000000 || au > 128) {
5286 fmt = FS_EXFAT;
break;
5289 if (au > 128)
return FR_INVALID_PARAMETER;
5290 if (opt & FM_FAT32) {
5291 if ((opt & FM_ANY) == FM_FAT32 || !(opt & FM_FAT)) {
5292 fmt = FS_FAT32;
break;
5295 if (!(opt & FM_FAT))
return FR_INVALID_PARAMETER;
5300 if (fmt == FS_EXFAT) {
5301 DWORD szb_bit, szb_case, sum, nb, cl;
5306 if (sz_vol < 0x1000)
return FR_MKFS_ABORTED;
5308 tbl[0] = b_vol; tbl[1] = b_vol + sz_vol - 1;
5309 disk_ioctl(pdrv, CTRL_TRIM, tbl);
5314 if (sz_vol >= 0x80000) au = 64;
5315 if (sz_vol >= 0x4000000) au = 256;
5318 sz_fat = ((sz_vol / au + 2) * 4 + ss - 1) / ss;
5319 b_data = (b_fat + sz_fat + sz_blk - 1) & ~(sz_blk - 1);
5320 if (b_data >= sz_vol / 2)
return FR_MKFS_ABORTED;
5321 n_clst = (sz_vol - (b_data - b_vol)) / au;
5322 if (n_clst <16)
return FR_MKFS_ABORTED;
5323 if (n_clst > MAX_EXFAT)
return FR_MKFS_ABORTED;
5325 szb_bit = (n_clst + 7) / 8;
5326 tbl[0] = (szb_bit + au * ss - 1) / (au * ss);
5329 sect = b_data + au * tbl[0];
5331 st = si = i = j = szb_case = 0;
5335 ch = ff_wtoupper(si);
5339 for (j = 1; (WCHAR)(si + j) && (WCHAR)(si + j) == ff_wtoupper((WCHAR)(si + j)); j++) ;
5341 ch = 0xFFFF; st = 2;
break;
5347 if (--j == 0) st = 0;
5350 ch = (WCHAR)j; si += j;
5353 sum = xsum32(buf[i + 0] = (BYTE)ch, sum);
5354 sum = xsum32(buf[i + 1] = (BYTE)(ch >> 8), sum);
5355 i += 2; szb_case += 2;
5356 if (!si || i == szb_buf) {
5357 n = (i + ss - 1) / ss;
5358 if (disk_write(pdrv, buf, sect, n) != RES_OK)
return FR_DISK_ERR;
5362 tbl[1] = (szb_case + au * ss - 1) / (au * ss);
5366 sect = b_data; nsect = (szb_bit + ss - 1) / ss;
5367 nb = tbl[0] + tbl[1] + tbl[2];
5369 mem_set(buf, 0, szb_buf);
5370 for (i = 0; nb >= 8 && i < szb_buf; buf[i++] = 0xFF, nb -= 8) ;
5371 for (b = 1; nb && i < szb_buf; buf[i] |= b, b <<= 1, nb--) ;
5372 n = (nsect > sz_buf) ? sz_buf : nsect;
5373 if (disk_write(pdrv, buf, sect, n) != RES_OK)
return FR_DISK_ERR;
5374 sect += n; nsect -= n;
5378 sect = b_fat; nsect = sz_fat;
5381 mem_set(buf, 0, szb_buf); i = 0;
5383 st_dword(buf + i, 0xFFFFFFF8); i += 4; cl++;
5384 st_dword(buf + i, 0xFFFFFFFF); i += 4; cl++;
5387 while (nb && i < szb_buf) {
5388 st_dword(buf + i, (nb > 1) ? cl + 1 : 0xFFFFFFFF);
5391 if (!nb && j < 3) nb = tbl[j++];
5392 }
while (nb && i < szb_buf);
5393 n = (nsect > sz_buf) ? sz_buf : nsect;
5394 if (disk_write(pdrv, buf, sect, n) != RES_OK)
return FR_DISK_ERR;
5395 sect += n; nsect -= n;
5399 mem_set(buf, 0, szb_buf);
5400 buf[SZDIRE * 0 + 0] = 0x83;
5401 buf[SZDIRE * 1 + 0] = 0x81;
5402 st_dword(buf + SZDIRE * 1 + 20, 2);
5403 st_dword(buf + SZDIRE * 1 + 24, szb_bit);
5404 buf[SZDIRE * 2 + 0] = 0x82;
5405 st_dword(buf + SZDIRE * 2 + 4, sum);
5406 st_dword(buf + SZDIRE * 2 + 20, 2 + tbl[0]);
5407 st_dword(buf + SZDIRE * 2 + 24, szb_case);
5408 sect = b_data + au * (tbl[0] + tbl[1]); nsect = au;
5410 n = (nsect > sz_buf) ? sz_buf : nsect;
5411 if (disk_write(pdrv, buf, sect, n) != RES_OK)
return FR_DISK_ERR;
5412 mem_set(buf, 0, ss);
5413 sect += n; nsect -= n;
5418 for (n = 0; n < 2; n++) {
5420 mem_set(buf, 0, ss);
5421 mem_cpy(buf + BS_JmpBoot,
"\xEB\x76\x90" "EXFAT ", 11);
5422 st_dword(buf + BPB_VolOfsEx, b_vol);
5423 st_dword(buf + BPB_TotSecEx, sz_vol);
5424 st_dword(buf + BPB_FatOfsEx, b_fat - b_vol);
5425 st_dword(buf + BPB_FatSzEx, sz_fat);
5426 st_dword(buf + BPB_DataOfsEx, b_data - b_vol);
5427 st_dword(buf + BPB_NumClusEx, n_clst);
5428 st_dword(buf + BPB_RootClusEx, 2 + tbl[0] + tbl[1]);
5429 st_dword(buf + BPB_VolIDEx, GET_FATTIME());
5430 st_word(buf + BPB_FSVerEx, 0x100);
5431 for (buf[BPB_BytsPerSecEx] = 0, i = ss; i >>= 1; buf[BPB_BytsPerSecEx]++) ;
5432 for (buf[BPB_SecPerClusEx] = 0, i = au; i >>= 1; buf[BPB_SecPerClusEx]++) ;
5433 buf[BPB_NumFATsEx] = 1;
5434 buf[BPB_DrvNumEx] = 0x80;
5435 st_word(buf + BS_BootCodeEx, 0xFEEB);
5436 st_word(buf + BS_55AA, 0xAA55);
5437 for (i = sum = 0; i < ss; i++) {
5438 if (i != BPB_VolFlagEx && i != BPB_VolFlagEx + 1 && i != BPB_PercInUseEx) sum = xsum32(buf[i], sum);
5440 if (disk_write(pdrv, buf, sect++, 1) != RES_OK)
return FR_DISK_ERR;
5442 mem_set(buf, 0, ss);
5443 st_word(buf + ss - 2, 0xAA55);
5444 for (j = 1; j < 9; j++) {
5445 for (i = 0; i < ss; sum = xsum32(buf[i++], sum)) ;
5446 if (disk_write(pdrv, buf, sect++, 1) != RES_OK)
return FR_DISK_ERR;
5449 mem_set(buf, 0, ss);
5450 for ( ; j < 11; j++) {
5451 for (i = 0; i < ss; sum = xsum32(buf[i++], sum)) ;
5452 if (disk_write(pdrv, buf, sect++, 1) != RES_OK)
return FR_DISK_ERR;
5455 for (i = 0; i < ss; i += 4) st_dword(buf + i, sum);
5456 if (disk_write(pdrv, buf, sect++, 1) != RES_OK)
return FR_DISK_ERR;
5465 if (fmt == FS_FAT32) {
5467 n = sz_vol / 0x20000;
5468 for (i = 0, pau = 1; cst32[i] && cst32[i] <= n; i++, pau <<= 1) ;
5470 n_clst = sz_vol / pau;
5471 sz_fat = (n_clst * 4 + 8 + ss - 1) / ss;
5474 if (n_clst <= MAX_FAT16 || n_clst > MAX_FAT32)
return FR_MKFS_ABORTED;
5477 n = sz_vol / 0x1000;
5478 for (i = 0, pau = 1; cst[i] && cst[i] <= n; i++, pau <<= 1) ;
5480 n_clst = sz_vol / pau;
5481 if (n_clst > MAX_FAT12) {
5485 n = (n_clst * 3 + 1) / 2 + 3;
5487 sz_fat = (n + ss - 1) / ss;
5489 sz_dir = (DWORD)n_rootdir * SZDIRE / ss;
5491 b_fat = b_vol + sz_rsv;
5492 b_data = b_fat + sz_fat * n_fats + sz_dir;
5495 n = ((b_data + sz_blk - 1) & ~(sz_blk - 1)) - b_data;
5496 if (fmt == FS_FAT32) {
5497 sz_rsv += n; b_fat += n;
5499 sz_fat += n / n_fats;
5503 if (sz_vol < b_data + pau * 16 - b_vol)
return FR_MKFS_ABORTED;
5504 n_clst = (sz_vol - sz_rsv - sz_fat * n_fats - sz_dir) / pau;
5505 if (fmt == FS_FAT32) {
5506 if (n_clst <= MAX_FAT16) {
5507 if (!au && (au = pau / 2) != 0)
continue;
5508 return FR_MKFS_ABORTED;
5511 if (fmt == FS_FAT16) {
5512 if (n_clst > MAX_FAT16) {
5513 if (!au && (pau * 2) <= 64) {
5514 au = pau * 2;
continue;
5516 if ((opt & FM_FAT32)) {
5517 fmt = FS_FAT32;
continue;
5519 if (!au && (au = pau * 2) <= 128)
continue;
5520 return FR_MKFS_ABORTED;
5522 if (n_clst <= MAX_FAT12) {
5523 if (!au && (au = pau * 2) <= 128)
continue;
5524 return FR_MKFS_ABORTED;
5527 if (fmt == FS_FAT12 && n_clst > MAX_FAT12)
return FR_MKFS_ABORTED;
5534 tbl[0] = b_vol; tbl[1] = b_vol + sz_vol - 1;
5535 disk_ioctl(pdrv, CTRL_TRIM, tbl);
5538 mem_set(buf, 0, ss);
5539 mem_cpy(buf + BS_JmpBoot,
"\xEB\xFE\x90" "MSDOS5.0", 11);
5540 st_word(buf + BPB_BytsPerSec, ss);
5541 buf[BPB_SecPerClus] = (BYTE)pau;
5542 st_word(buf + BPB_RsvdSecCnt, (WORD)sz_rsv);
5543 buf[BPB_NumFATs] = (BYTE)n_fats;
5544 st_word(buf + BPB_RootEntCnt, (WORD)((fmt == FS_FAT32) ? 0 : n_rootdir));
5545 if (sz_vol < 0x10000) {
5546 st_word(buf + BPB_TotSec16, (WORD)sz_vol);
5548 st_dword(buf + BPB_TotSec32, sz_vol);
5550 buf[BPB_Media] = 0xF8;
5551 st_word(buf + BPB_SecPerTrk, 63);
5552 st_word(buf + BPB_NumHeads, 255);
5553 st_dword(buf + BPB_HiddSec, b_vol);
5554 if (fmt == FS_FAT32) {
5555 st_dword(buf + BS_VolID32, GET_FATTIME());
5556 st_dword(buf + BPB_FATSz32, sz_fat);
5557 st_dword(buf + BPB_RootClus32, 2);
5558 st_word(buf + BPB_FSInfo32, 1);
5559 st_word(buf + BPB_BkBootSec32, 6);
5560 buf[BS_DrvNum32] = 0x80;
5561 buf[BS_BootSig32] = 0x29;
5562 mem_cpy(buf + BS_VolLab32,
"NO NAME " "FAT32 ", 19);
5564 st_dword(buf + BS_VolID, GET_FATTIME());
5565 st_word(buf + BPB_FATSz16, (WORD)sz_fat);
5566 buf[BS_DrvNum] = 0x80;
5567 buf[BS_BootSig] = 0x29;
5568 mem_cpy(buf + BS_VolLab,
"NO NAME " "FAT ", 19);
5570 st_word(buf + BS_55AA, 0xAA55);
5571 if (disk_write(pdrv, buf, b_vol, 1) != RES_OK)
return FR_DISK_ERR;
5574 if (fmt == FS_FAT32) {
5575 disk_write(pdrv, buf, b_vol + 6, 1);
5576 mem_set(buf, 0, ss);
5577 st_dword(buf + FSI_LeadSig, 0x41615252);
5578 st_dword(buf + FSI_StrucSig, 0x61417272);
5579 st_dword(buf + FSI_Free_Count, n_clst - 1);
5580 st_dword(buf + FSI_Nxt_Free, 2);
5581 st_word(buf + BS_55AA, 0xAA55);
5582 disk_write(pdrv, buf, b_vol + 7, 1);
5583 disk_write(pdrv, buf, b_vol + 1, 1);
5587 mem_set(buf, 0, (UINT)szb_buf);
5589 for (i = 0; i < n_fats; i++) {
5590 if (fmt == FS_FAT32) {
5591 st_dword(buf + 0, 0xFFFFFFF8);
5592 st_dword(buf + 4, 0xFFFFFFFF);
5593 st_dword(buf + 8, 0x0FFFFFFF);
5595 st_dword(buf + 0, (fmt == FS_FAT12) ? 0xFFFFF8 : 0xFFFFFFF8);
5599 n = (nsect > sz_buf) ? sz_buf : nsect;
5600 if (disk_write(pdrv, buf, sect, (UINT)n) != RES_OK)
return FR_DISK_ERR;
5601 mem_set(buf, 0, ss);
5602 sect += n; nsect -= n;
5607 nsect = (fmt == FS_FAT32) ? pau : sz_dir;
5609 n = (nsect > sz_buf) ? sz_buf : nsect;
5610 if (disk_write(pdrv, buf, sect, (UINT)n) != RES_OK)
return FR_DISK_ERR;
5611 sect += n; nsect -= n;
5619 if (fmt == FS_FAT32) {
5622 if (sz_vol >= 0x10000) {
5625 sys = (fmt == FS_FAT16) ? 0x04 : 0x01;
5632 if (disk_read(pdrv, buf, 0, 1) != RES_OK)
return FR_DISK_ERR;
5633 buf[MBR_Table + (part - 1) * SZ_PTE + PTE_System] = sys;
5634 if (disk_write(pdrv, buf, 0, 1) != RES_OK)
return FR_DISK_ERR;
5636 if (!(opt & FM_SFD)) {
5638 mem_set(buf, 0, ss);
5639 st_word(buf + BS_55AA, 0xAA55);
5640 pte = buf + MBR_Table;
5642 pte[PTE_StHead] = 1;
5645 pte[PTE_System] = sys;
5646 n = (b_vol + sz_vol) / (63 * 255);
5647 pte[PTE_EdHead] = 254;
5648 pte[PTE_EdSec] = (BYTE)(n >> 2 | 63);
5649 pte[PTE_EdCyl] = (BYTE)n;
5650 st_dword(pte + PTE_StLba, b_vol);
5651 st_dword(pte + PTE_SizLba, sz_vol);
5652 if (disk_write(pdrv, buf, 0, 1) != RES_OK)
return FR_DISK_ERR;
5656 if (disk_ioctl(pdrv, CTRL_SYNC, 0) != RES_OK)
return FR_DISK_ERR;
5663 #if _MULTI_PARTITION 5674 UINT i, n, sz_cyl, tot_cyl, b_cyl, e_cyl, p_cyl;
5675 BYTE s_hd, e_hd, *p, *buf = (BYTE*)work;
5677 DWORD sz_disk, sz_part, s_part;
5680 stat = disk_initialize(pdrv);
5681 if (stat & STA_NOINIT)
return FR_NOT_READY;
5682 if (stat & STA_PROTECT)
return FR_WRITE_PROTECTED;
5683 if (disk_ioctl(pdrv, GET_SECTOR_COUNT, &sz_disk))
return FR_DISK_ERR;
5686 for (n = 16; n < 256 && sz_disk / n / 63 > 1024; n *= 2) ;
5690 tot_cyl = sz_disk / sz_cyl;
5693 mem_set(buf, 0, _MAX_SS);
5694 p = buf + MBR_Table; b_cyl = 0;
5695 for (i = 0; i < 4; i++, p += SZ_PTE) {
5696 p_cyl = (szt[i] <= 100U) ? (DWORD)tot_cyl * szt[i] / 100 : szt[i] / sz_cyl;
5697 if (!p_cyl)
continue;
5698 s_part = (DWORD)sz_cyl * b_cyl;
5699 sz_part = (DWORD)sz_cyl * p_cyl;
5702 s_part += 63; sz_part -= 63;
5706 e_cyl = b_cyl + p_cyl - 1;
5707 if (e_cyl >= tot_cyl)
return FR_INVALID_PARAMETER;
5711 p[2] = (BYTE)((b_cyl >> 2) + 1);
5715 p[6] = (BYTE)((e_cyl >> 2) + 63);
5717 st_dword(p + 8, s_part);
5718 st_dword(p + 12, sz_part);
5726 return (disk_write(pdrv, buf, 0, 1) != RES_OK || disk_ioctl(pdrv, CTRL_SYNC, 0) != RES_OK) ? FR_DISK_ERR : FR_OK;
5752 while (n < len - 1) {
5754 #if _STRF_ENCODE == 3 5755 f_read(fp, s, 1, &rc);
5759 if (c < 0xC0)
continue;
5761 f_read(fp, s, 1, &rc);
5763 c = (c & 0x1F) << 6 | (s[0] & 0x3F);
5764 if (c < 0x80) c =
'?';
5767 f_read(fp, s, 2, &rc);
5769 c = c << 12 | (s[0] & 0x3F) << 6 | (s[1] & 0x3F);
5770 if (c < 0x800) c =
'?';
5776 #elif _STRF_ENCODE == 2 5777 f_read(fp, s, 2, &rc);
5779 c = s[1] + (s[0] << 8);
5780 #elif _STRF_ENCODE == 1 5781 f_read(fp, s, 2, &rc);
5783 c = s[0] + (s[1] << 8);
5785 f_read(fp, s, 1, &rc);
5789 f_read(fp, s, 1, &rc);
5791 c = (c << 8) + s[0];
5793 c = ff_convert(c, 1);
5797 f_read(fp, s, 1, &rc);
5804 if (c ==
'\n')
break;
5807 return n ? buff : 0;
5844 #if _STRF_ENCODE == 3 5846 pb->buf[i++] = (BYTE)c;
5849 pb->buf[i++] = (BYTE)(0xC0 | c >> 6);
5851 pb->buf[i++] = (BYTE)(0xE0 | c >> 12);
5852 pb->buf[i++] = (BYTE)(0x80 | (c >> 6 & 0x3F));
5854 pb->buf[i++] = (BYTE)(0x80 | (c & 0x3F));
5856 #elif _STRF_ENCODE == 2 5857 pb->buf[i++] = (BYTE)(c >> 8);
5858 pb->buf[i++] = (BYTE)c;
5859 #elif _STRF_ENCODE == 1 5860 pb->buf[i++] = (BYTE)c;
5861 pb->buf[i++] = (BYTE)(c >> 8);
5863 c = ff_convert(c, 0);
5866 pb->buf[i++] = (BYTE)(c >> 8);
5867 pb->buf[i++] = (BYTE)c;
5870 pb->buf[i++] = (BYTE)c;
5873 if (i >= (
int)(
sizeof pb->buf) - 3) {
5874 f_write(pb->fp, pb->buf, (UINT)i, &bw);
5875 i = (bw == (UINT)i) ? 0 : -1;
5890 && f_write(pb->fp, pb->buf, (UINT)pb->idx, &nw) == FR_OK
5891 && (UINT)pb->idx == nw)
return pb->nchr;
5903 pb->nchr = pb->idx = 0;
5918 return putc_flush(&pb);
5937 while (*str) putc_bfd(&pb, *str++);
5938 return putc_flush(&pb);
5959 TCHAR c, d, str[32], *p;
5982 while (IsDigit(c)) {
5983 w = w * 10 + c -
'0';
5986 if (c ==
'l' || c ==
'L') {
5991 if (IsLower(d)) d -= 0x20;
5994 p = va_arg(arp, TCHAR*);
5995 for (j = 0; p[j]; j++) ;
5997 while (j++ < w) putc_bfd(&pb,
' ');
5999 while (*p) putc_bfd(&pb, *p++);
6000 while (j++ < w) putc_bfd(&pb,
' ');
6003 putc_bfd(&pb, (TCHAR)va_arg(arp,
int));
continue;
6014 putc_bfd(&pb, c);
continue;
6018 v = (f & 4) ? (DWORD)va_arg(arp,
long) : ((d ==
'D') ? (DWORD)(long)va_arg(arp, int) : (DWORD)va_arg(arp, unsigned int));
6019 if (d ==
'D' && (v & 0x80000000)) {
6025 d = (TCHAR)(v % r); v /= r;
6026 if (d > 9) d += (c ==
'x') ? 0x27 : 0x07;
6028 }
while (v && i <
sizeof str /
sizeof str[0]);
6029 if (f & 8) str[i++] =
'-';
6030 j = i; d = (f & 1) ?
'0' :
' ';
6031 while (!(f & 2) && j++ < w) putc_bfd(&pb, d);
6032 do putc_bfd(&pb, str[--i]);
while (i);
6033 while (j++ < w) putc_bfd(&pb, d);
6038 return putc_flush(&pb);
#define _USE_STRFUNC
This option switches the string functions: f_gets(), f_putc(), f_puts(), and f_printf().
#define _MIN_SS
These options configure the range of sector size to be supported (512, 1024, 2048, or 4096).
#define _FS_LOCK
The option _FS_LOCK switches the file lock function controlling duplicate file open and illegal opera...
#define _FS_EXFAT
This option switches the support of the exFAT file system (0: disable or 1: enable).
#define _USE_LFN
_USE_LFN switches the support of long file name (LFN).
#define _MULTI_PARTITION
This option switches support of multi-partition on a physical drive.
#define _FS_READONLY
This option switches the read-only configuration (0: read/write or 1: read-only). ...
#define _FS_RPATH
This option configures the support of relative path.
#define _USE_LABEL
This option switches the volume label functions: f_getlabel() and f_setlabel() (0: disable or 1: enab...
#define _VOLUMES
Number of volumes (logical drives) to be used.