Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
mbed-os/features/filesystem/fat/ChaN/ff.cpp@0:2f0e1e23c242, 2018-04-13 (annotated)
- Committer:
- glebiuskv
- Date:
- Fri Apr 13 08:53:46 2018 +0000
- Revision:
- 0:2f0e1e23c242
initial
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
glebiuskv | 0:2f0e1e23c242 | 1 | /*----------------------------------------------------------------------------/ |
glebiuskv | 0:2f0e1e23c242 | 2 | / FatFs - Generic FAT Filesystem Module R0.13a / |
glebiuskv | 0:2f0e1e23c242 | 3 | /-----------------------------------------------------------------------------/ |
glebiuskv | 0:2f0e1e23c242 | 4 | / |
glebiuskv | 0:2f0e1e23c242 | 5 | / Copyright (C) 2017, ChaN, all right reserved. |
glebiuskv | 0:2f0e1e23c242 | 6 | / |
glebiuskv | 0:2f0e1e23c242 | 7 | / FatFs module is an open source software. Redistribution and use of FatFs in |
glebiuskv | 0:2f0e1e23c242 | 8 | / source and binary forms, with or without modification, are permitted provided |
glebiuskv | 0:2f0e1e23c242 | 9 | / that the following condition is met: |
glebiuskv | 0:2f0e1e23c242 | 10 | / |
glebiuskv | 0:2f0e1e23c242 | 11 | / 1. Redistributions of source code must retain the above copyright notice, |
glebiuskv | 0:2f0e1e23c242 | 12 | / this condition and the following disclaimer. |
glebiuskv | 0:2f0e1e23c242 | 13 | / |
glebiuskv | 0:2f0e1e23c242 | 14 | / This software is provided by the copyright holder and contributors "AS IS" |
glebiuskv | 0:2f0e1e23c242 | 15 | / and any warranties related to this software are DISCLAIMED. |
glebiuskv | 0:2f0e1e23c242 | 16 | / The copyright owner or contributors be NOT LIABLE for any damages caused |
glebiuskv | 0:2f0e1e23c242 | 17 | / by use of this software. |
glebiuskv | 0:2f0e1e23c242 | 18 | / |
glebiuskv | 0:2f0e1e23c242 | 19 | /----------------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 20 | |
glebiuskv | 0:2f0e1e23c242 | 21 | |
glebiuskv | 0:2f0e1e23c242 | 22 | #include "ff.h" /* Declarations of FatFs API */ |
glebiuskv | 0:2f0e1e23c242 | 23 | #include "diskio.h" /* Declarations of device I/O functions */ |
glebiuskv | 0:2f0e1e23c242 | 24 | |
glebiuskv | 0:2f0e1e23c242 | 25 | |
glebiuskv | 0:2f0e1e23c242 | 26 | /*-------------------------------------------------------------------------- |
glebiuskv | 0:2f0e1e23c242 | 27 | |
glebiuskv | 0:2f0e1e23c242 | 28 | Module Private Definitions |
glebiuskv | 0:2f0e1e23c242 | 29 | |
glebiuskv | 0:2f0e1e23c242 | 30 | ---------------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 31 | |
glebiuskv | 0:2f0e1e23c242 | 32 | #if FF_DEFINED != 89352 /* Revision ID */ |
glebiuskv | 0:2f0e1e23c242 | 33 | #error Wrong include file (ff.h). |
glebiuskv | 0:2f0e1e23c242 | 34 | #endif |
glebiuskv | 0:2f0e1e23c242 | 35 | |
glebiuskv | 0:2f0e1e23c242 | 36 | |
glebiuskv | 0:2f0e1e23c242 | 37 | /* Character code support macros */ |
glebiuskv | 0:2f0e1e23c242 | 38 | #define IsUpper(c) ((c) >= 'A' && (c) <= 'Z') |
glebiuskv | 0:2f0e1e23c242 | 39 | #define IsLower(c) ((c) >= 'a' && (c) <= 'z') |
glebiuskv | 0:2f0e1e23c242 | 40 | #define IsDigit(c) ((c) >= '0' && (c) <= '9') |
glebiuskv | 0:2f0e1e23c242 | 41 | #define IsSurrogate(c) ((c) >= 0xD800 && (c) <= 0xDFFF) |
glebiuskv | 0:2f0e1e23c242 | 42 | #define IsSurrogateH(c) ((c) >= 0xD800 && (c) <= 0xDBFF) |
glebiuskv | 0:2f0e1e23c242 | 43 | #define IsSurrogateL(c) ((c) >= 0xDC00 && (c) <= 0xDFFF) |
glebiuskv | 0:2f0e1e23c242 | 44 | |
glebiuskv | 0:2f0e1e23c242 | 45 | |
glebiuskv | 0:2f0e1e23c242 | 46 | /* Additional file attribute bits for internal use */ |
glebiuskv | 0:2f0e1e23c242 | 47 | #define AM_VOL 0x08 /* Volume label */ |
glebiuskv | 0:2f0e1e23c242 | 48 | #define AM_LFN 0x0F /* LFN entry */ |
glebiuskv | 0:2f0e1e23c242 | 49 | #define AM_MASK 0x3F /* Mask of defined bits */ |
glebiuskv | 0:2f0e1e23c242 | 50 | |
glebiuskv | 0:2f0e1e23c242 | 51 | |
glebiuskv | 0:2f0e1e23c242 | 52 | /* Additional file access control and file status flags for internal use */ |
glebiuskv | 0:2f0e1e23c242 | 53 | #define FA_SEEKEND 0x20 /* Seek to end of the file on file open */ |
glebiuskv | 0:2f0e1e23c242 | 54 | #define FA_MODIFIED 0x40 /* File has been modified */ |
glebiuskv | 0:2f0e1e23c242 | 55 | #define FA_DIRTY 0x80 /* FIL.buf[] needs to be written-back */ |
glebiuskv | 0:2f0e1e23c242 | 56 | |
glebiuskv | 0:2f0e1e23c242 | 57 | |
glebiuskv | 0:2f0e1e23c242 | 58 | /* Name status flags in fn[11] */ |
glebiuskv | 0:2f0e1e23c242 | 59 | #define NSFLAG 11 /* Index of the name status byte */ |
glebiuskv | 0:2f0e1e23c242 | 60 | #define NS_LOSS 0x01 /* Out of 8.3 format */ |
glebiuskv | 0:2f0e1e23c242 | 61 | #define NS_LFN 0x02 /* Force to create LFN entry */ |
glebiuskv | 0:2f0e1e23c242 | 62 | #define NS_LAST 0x04 /* Last segment */ |
glebiuskv | 0:2f0e1e23c242 | 63 | #define NS_BODY 0x08 /* Lower case flag (body) */ |
glebiuskv | 0:2f0e1e23c242 | 64 | #define NS_EXT 0x10 /* Lower case flag (ext) */ |
glebiuskv | 0:2f0e1e23c242 | 65 | #define NS_DOT 0x20 /* Dot entry */ |
glebiuskv | 0:2f0e1e23c242 | 66 | #define NS_NOLFN 0x40 /* Do not find LFN */ |
glebiuskv | 0:2f0e1e23c242 | 67 | #define NS_NONAME 0x80 /* Not followed */ |
glebiuskv | 0:2f0e1e23c242 | 68 | |
glebiuskv | 0:2f0e1e23c242 | 69 | |
glebiuskv | 0:2f0e1e23c242 | 70 | /* Limits and boundaries */ |
glebiuskv | 0:2f0e1e23c242 | 71 | #define MAX_DIR 0x200000 /* Max size of FAT directory */ |
glebiuskv | 0:2f0e1e23c242 | 72 | #define MAX_DIR_EX 0x10000000 /* Max size of exFAT directory */ |
glebiuskv | 0:2f0e1e23c242 | 73 | #define MAX_FAT12 0xFF5 /* Max FAT12 clusters (differs from specs, but right for real DOS/Windows behavior) */ |
glebiuskv | 0:2f0e1e23c242 | 74 | #define MAX_FAT16 0xFFF5 /* Max FAT16 clusters (differs from specs, but right for real DOS/Windows behavior) */ |
glebiuskv | 0:2f0e1e23c242 | 75 | #define MAX_FAT32 0x0FFFFFF5 /* Max FAT32 clusters (not specified, practical limit) */ |
glebiuskv | 0:2f0e1e23c242 | 76 | #define MAX_EXFAT 0x7FFFFFFD /* Max exFAT clusters (differs from specs, implementation limit) */ |
glebiuskv | 0:2f0e1e23c242 | 77 | |
glebiuskv | 0:2f0e1e23c242 | 78 | |
glebiuskv | 0:2f0e1e23c242 | 79 | /* FatFs refers the FAT structure as simple byte array instead of structure member |
glebiuskv | 0:2f0e1e23c242 | 80 | / because the C structure is not binary compatible between different platforms */ |
glebiuskv | 0:2f0e1e23c242 | 81 | |
glebiuskv | 0:2f0e1e23c242 | 82 | #define BS_JmpBoot 0 /* x86 jump instruction (3-byte) */ |
glebiuskv | 0:2f0e1e23c242 | 83 | #define BS_OEMName 3 /* OEM name (8-byte) */ |
glebiuskv | 0:2f0e1e23c242 | 84 | #define BPB_BytsPerSec 11 /* Sector size [byte] (WORD) */ |
glebiuskv | 0:2f0e1e23c242 | 85 | #define BPB_SecPerClus 13 /* Cluster size [sector] (BYTE) */ |
glebiuskv | 0:2f0e1e23c242 | 86 | #define BPB_RsvdSecCnt 14 /* Size of reserved area [sector] (WORD) */ |
glebiuskv | 0:2f0e1e23c242 | 87 | #define BPB_NumFATs 16 /* Number of FATs (BYTE) */ |
glebiuskv | 0:2f0e1e23c242 | 88 | #define BPB_RootEntCnt 17 /* Size of root directory area for FAT [entry] (WORD) */ |
glebiuskv | 0:2f0e1e23c242 | 89 | #define BPB_TotSec16 19 /* Volume size (16-bit) [sector] (WORD) */ |
glebiuskv | 0:2f0e1e23c242 | 90 | #define BPB_Media 21 /* Media descriptor byte (BYTE) */ |
glebiuskv | 0:2f0e1e23c242 | 91 | #define BPB_FATSz16 22 /* FAT size (16-bit) [sector] (WORD) */ |
glebiuskv | 0:2f0e1e23c242 | 92 | #define BPB_SecPerTrk 24 /* Number of sectors per track for int13h [sector] (WORD) */ |
glebiuskv | 0:2f0e1e23c242 | 93 | #define BPB_NumHeads 26 /* Number of heads for int13h (WORD) */ |
glebiuskv | 0:2f0e1e23c242 | 94 | #define BPB_HiddSec 28 /* Volume offset from top of the drive (DWORD) */ |
glebiuskv | 0:2f0e1e23c242 | 95 | #define BPB_TotSec32 32 /* Volume size (32-bit) [sector] (DWORD) */ |
glebiuskv | 0:2f0e1e23c242 | 96 | #define BS_DrvNum 36 /* Physical drive number for int13h (BYTE) */ |
glebiuskv | 0:2f0e1e23c242 | 97 | #define BS_NTres 37 /* WindowsNT error flag (BYTE) */ |
glebiuskv | 0:2f0e1e23c242 | 98 | #define BS_BootSig 38 /* Extended boot signature (BYTE) */ |
glebiuskv | 0:2f0e1e23c242 | 99 | #define BS_VolID 39 /* Volume serial number (DWORD) */ |
glebiuskv | 0:2f0e1e23c242 | 100 | #define BS_VolLab 43 /* Volume label string (8-byte) */ |
glebiuskv | 0:2f0e1e23c242 | 101 | #define BS_FilSysType 54 /* Filesystem type string (8-byte) */ |
glebiuskv | 0:2f0e1e23c242 | 102 | #define BS_BootCode 62 /* Boot code (448-byte) */ |
glebiuskv | 0:2f0e1e23c242 | 103 | #define BS_55AA 510 /* Signature word (WORD) */ |
glebiuskv | 0:2f0e1e23c242 | 104 | |
glebiuskv | 0:2f0e1e23c242 | 105 | #define BPB_FATSz32 36 /* FAT32: FAT size [sector] (DWORD) */ |
glebiuskv | 0:2f0e1e23c242 | 106 | #define BPB_ExtFlags32 40 /* FAT32: Extended flags (WORD) */ |
glebiuskv | 0:2f0e1e23c242 | 107 | #define BPB_FSVer32 42 /* FAT32: Filesystem version (WORD) */ |
glebiuskv | 0:2f0e1e23c242 | 108 | #define BPB_RootClus32 44 /* FAT32: Root directory cluster (DWORD) */ |
glebiuskv | 0:2f0e1e23c242 | 109 | #define BPB_FSInfo32 48 /* FAT32: Offset of FSINFO sector (WORD) */ |
glebiuskv | 0:2f0e1e23c242 | 110 | #define BPB_BkBootSec32 50 /* FAT32: Offset of backup boot sector (WORD) */ |
glebiuskv | 0:2f0e1e23c242 | 111 | #define BS_DrvNum32 64 /* FAT32: Physical drive number for int13h (BYTE) */ |
glebiuskv | 0:2f0e1e23c242 | 112 | #define BS_NTres32 65 /* FAT32: Error flag (BYTE) */ |
glebiuskv | 0:2f0e1e23c242 | 113 | #define BS_BootSig32 66 /* FAT32: Extended boot signature (BYTE) */ |
glebiuskv | 0:2f0e1e23c242 | 114 | #define BS_VolID32 67 /* FAT32: Volume serial number (DWORD) */ |
glebiuskv | 0:2f0e1e23c242 | 115 | #define BS_VolLab32 71 /* FAT32: Volume label string (8-byte) */ |
glebiuskv | 0:2f0e1e23c242 | 116 | #define BS_FilSysType32 82 /* FAT32: Filesystem type string (8-byte) */ |
glebiuskv | 0:2f0e1e23c242 | 117 | #define BS_BootCode32 90 /* FAT32: Boot code (420-byte) */ |
glebiuskv | 0:2f0e1e23c242 | 118 | |
glebiuskv | 0:2f0e1e23c242 | 119 | #define BPB_ZeroedEx 11 /* exFAT: MBZ field (53-byte) */ |
glebiuskv | 0:2f0e1e23c242 | 120 | #define BPB_VolOfsEx 64 /* exFAT: Volume offset from top of the drive [sector] (QWORD) */ |
glebiuskv | 0:2f0e1e23c242 | 121 | #define BPB_TotSecEx 72 /* exFAT: Volume size [sector] (QWORD) */ |
glebiuskv | 0:2f0e1e23c242 | 122 | #define BPB_FatOfsEx 80 /* exFAT: FAT offset from top of the volume [sector] (DWORD) */ |
glebiuskv | 0:2f0e1e23c242 | 123 | #define BPB_FatSzEx 84 /* exFAT: FAT size [sector] (DWORD) */ |
glebiuskv | 0:2f0e1e23c242 | 124 | #define BPB_DataOfsEx 88 /* exFAT: Data offset from top of the volume [sector] (DWORD) */ |
glebiuskv | 0:2f0e1e23c242 | 125 | #define BPB_NumClusEx 92 /* exFAT: Number of clusters (DWORD) */ |
glebiuskv | 0:2f0e1e23c242 | 126 | #define BPB_RootClusEx 96 /* exFAT: Root directory start cluster (DWORD) */ |
glebiuskv | 0:2f0e1e23c242 | 127 | #define BPB_VolIDEx 100 /* exFAT: Volume serial number (DWORD) */ |
glebiuskv | 0:2f0e1e23c242 | 128 | #define BPB_FSVerEx 104 /* exFAT: Filesystem version (WORD) */ |
glebiuskv | 0:2f0e1e23c242 | 129 | #define BPB_VolFlagEx 106 /* exFAT: Volume flags (WORD) */ |
glebiuskv | 0:2f0e1e23c242 | 130 | #define BPB_BytsPerSecEx 108 /* exFAT: Log2 of sector size in unit of byte (BYTE) */ |
glebiuskv | 0:2f0e1e23c242 | 131 | #define BPB_SecPerClusEx 109 /* exFAT: Log2 of cluster size in unit of sector (BYTE) */ |
glebiuskv | 0:2f0e1e23c242 | 132 | #define BPB_NumFATsEx 110 /* exFAT: Number of FATs (BYTE) */ |
glebiuskv | 0:2f0e1e23c242 | 133 | #define BPB_DrvNumEx 111 /* exFAT: Physical drive number for int13h (BYTE) */ |
glebiuskv | 0:2f0e1e23c242 | 134 | #define BPB_PercInUseEx 112 /* exFAT: Percent in use (BYTE) */ |
glebiuskv | 0:2f0e1e23c242 | 135 | #define BPB_RsvdEx 113 /* exFAT: Reserved (7-byte) */ |
glebiuskv | 0:2f0e1e23c242 | 136 | #define BS_BootCodeEx 120 /* exFAT: Boot code (390-byte) */ |
glebiuskv | 0:2f0e1e23c242 | 137 | |
glebiuskv | 0:2f0e1e23c242 | 138 | #define DIR_Name 0 /* Short file name (11-byte) */ |
glebiuskv | 0:2f0e1e23c242 | 139 | #define DIR_Attr 11 /* Attribute (BYTE) */ |
glebiuskv | 0:2f0e1e23c242 | 140 | #define DIR_NTres 12 /* Lower case flag (BYTE) */ |
glebiuskv | 0:2f0e1e23c242 | 141 | #define DIR_CrtTime10 13 /* Created time sub-second (BYTE) */ |
glebiuskv | 0:2f0e1e23c242 | 142 | #define DIR_CrtTime 14 /* Created time (DWORD) */ |
glebiuskv | 0:2f0e1e23c242 | 143 | #define DIR_LstAccDate 18 /* Last accessed date (WORD) */ |
glebiuskv | 0:2f0e1e23c242 | 144 | #define DIR_FstClusHI 20 /* Higher 16-bit of first cluster (WORD) */ |
glebiuskv | 0:2f0e1e23c242 | 145 | #define DIR_ModTime 22 /* Modified time (DWORD) */ |
glebiuskv | 0:2f0e1e23c242 | 146 | #define DIR_FstClusLO 26 /* Lower 16-bit of first cluster (WORD) */ |
glebiuskv | 0:2f0e1e23c242 | 147 | #define DIR_FileSize 28 /* File size (DWORD) */ |
glebiuskv | 0:2f0e1e23c242 | 148 | #define LDIR_Ord 0 /* LFN: LFN order and LLE flag (BYTE) */ |
glebiuskv | 0:2f0e1e23c242 | 149 | #define LDIR_Attr 11 /* LFN: LFN attribute (BYTE) */ |
glebiuskv | 0:2f0e1e23c242 | 150 | #define LDIR_Type 12 /* LFN: Entry type (BYTE) */ |
glebiuskv | 0:2f0e1e23c242 | 151 | #define LDIR_Chksum 13 /* LFN: Checksum of the SFN (BYTE) */ |
glebiuskv | 0:2f0e1e23c242 | 152 | #define LDIR_FstClusLO 26 /* LFN: MBZ field (WORD) */ |
glebiuskv | 0:2f0e1e23c242 | 153 | #define XDIR_Type 0 /* exFAT: Type of exFAT directory entry (BYTE) */ |
glebiuskv | 0:2f0e1e23c242 | 154 | #define XDIR_NumLabel 1 /* exFAT: Number of volume label characters (BYTE) */ |
glebiuskv | 0:2f0e1e23c242 | 155 | #define XDIR_Label 2 /* exFAT: Volume label (11-WORD) */ |
glebiuskv | 0:2f0e1e23c242 | 156 | #define XDIR_CaseSum 4 /* exFAT: Sum of case conversion table (DWORD) */ |
glebiuskv | 0:2f0e1e23c242 | 157 | #define XDIR_NumSec 1 /* exFAT: Number of secondary entries (BYTE) */ |
glebiuskv | 0:2f0e1e23c242 | 158 | #define XDIR_SetSum 2 /* exFAT: Sum of the set of directory entries (WORD) */ |
glebiuskv | 0:2f0e1e23c242 | 159 | #define XDIR_Attr 4 /* exFAT: File attribute (WORD) */ |
glebiuskv | 0:2f0e1e23c242 | 160 | #define XDIR_CrtTime 8 /* exFAT: Created time (DWORD) */ |
glebiuskv | 0:2f0e1e23c242 | 161 | #define XDIR_ModTime 12 /* exFAT: Modified time (DWORD) */ |
glebiuskv | 0:2f0e1e23c242 | 162 | #define XDIR_AccTime 16 /* exFAT: Last accessed time (DWORD) */ |
glebiuskv | 0:2f0e1e23c242 | 163 | #define XDIR_CrtTime10 20 /* exFAT: Created time subsecond (BYTE) */ |
glebiuskv | 0:2f0e1e23c242 | 164 | #define XDIR_ModTime10 21 /* exFAT: Modified time subsecond (BYTE) */ |
glebiuskv | 0:2f0e1e23c242 | 165 | #define XDIR_CrtTZ 22 /* exFAT: Created timezone (BYTE) */ |
glebiuskv | 0:2f0e1e23c242 | 166 | #define XDIR_ModTZ 23 /* exFAT: Modified timezone (BYTE) */ |
glebiuskv | 0:2f0e1e23c242 | 167 | #define XDIR_AccTZ 24 /* exFAT: Last accessed timezone (BYTE) */ |
glebiuskv | 0:2f0e1e23c242 | 168 | #define XDIR_GenFlags 33 /* exFAT: General secondary flags (BYTE) */ |
glebiuskv | 0:2f0e1e23c242 | 169 | #define XDIR_NumName 35 /* exFAT: Number of file name characters (BYTE) */ |
glebiuskv | 0:2f0e1e23c242 | 170 | #define XDIR_NameHash 36 /* exFAT: Hash of file name (WORD) */ |
glebiuskv | 0:2f0e1e23c242 | 171 | #define XDIR_ValidFileSize 40 /* exFAT: Valid file size (QWORD) */ |
glebiuskv | 0:2f0e1e23c242 | 172 | #define XDIR_FstClus 52 /* exFAT: First cluster of the file data (DWORD) */ |
glebiuskv | 0:2f0e1e23c242 | 173 | #define XDIR_FileSize 56 /* exFAT: File/Directory size (QWORD) */ |
glebiuskv | 0:2f0e1e23c242 | 174 | |
glebiuskv | 0:2f0e1e23c242 | 175 | #define SZDIRE 32 /* Size of a directory entry */ |
glebiuskv | 0:2f0e1e23c242 | 176 | #define DDEM 0xE5 /* Deleted directory entry mark set to DIR_Name[0] */ |
glebiuskv | 0:2f0e1e23c242 | 177 | #define RDDEM 0x05 /* Replacement of the character collides with DDEM */ |
glebiuskv | 0:2f0e1e23c242 | 178 | #define LLEF 0x40 /* Last long entry flag in LDIR_Ord */ |
glebiuskv | 0:2f0e1e23c242 | 179 | |
glebiuskv | 0:2f0e1e23c242 | 180 | #define FSI_LeadSig 0 /* FAT32 FSI: Leading signature (DWORD) */ |
glebiuskv | 0:2f0e1e23c242 | 181 | #define FSI_StrucSig 484 /* FAT32 FSI: Structure signature (DWORD) */ |
glebiuskv | 0:2f0e1e23c242 | 182 | #define FSI_Free_Count 488 /* FAT32 FSI: Number of free clusters (DWORD) */ |
glebiuskv | 0:2f0e1e23c242 | 183 | #define FSI_Nxt_Free 492 /* FAT32 FSI: Last allocated cluster (DWORD) */ |
glebiuskv | 0:2f0e1e23c242 | 184 | |
glebiuskv | 0:2f0e1e23c242 | 185 | #define MBR_Table 446 /* MBR: Offset of partition table in the MBR */ |
glebiuskv | 0:2f0e1e23c242 | 186 | #define SZ_PTE 16 /* MBR: Size of a partition table entry */ |
glebiuskv | 0:2f0e1e23c242 | 187 | #define PTE_Boot 0 /* MBR PTE: Boot indicator */ |
glebiuskv | 0:2f0e1e23c242 | 188 | #define PTE_StHead 1 /* MBR PTE: Start head */ |
glebiuskv | 0:2f0e1e23c242 | 189 | #define PTE_StSec 2 /* MBR PTE: Start sector */ |
glebiuskv | 0:2f0e1e23c242 | 190 | #define PTE_StCyl 3 /* MBR PTE: Start cylinder */ |
glebiuskv | 0:2f0e1e23c242 | 191 | #define PTE_System 4 /* MBR PTE: System ID */ |
glebiuskv | 0:2f0e1e23c242 | 192 | #define PTE_EdHead 5 /* MBR PTE: End head */ |
glebiuskv | 0:2f0e1e23c242 | 193 | #define PTE_EdSec 6 /* MBR PTE: End sector */ |
glebiuskv | 0:2f0e1e23c242 | 194 | #define PTE_EdCyl 7 /* MBR PTE: End cylinder */ |
glebiuskv | 0:2f0e1e23c242 | 195 | #define PTE_StLba 8 /* MBR PTE: Start in LBA */ |
glebiuskv | 0:2f0e1e23c242 | 196 | #define PTE_SizLba 12 /* MBR PTE: Size in LBA */ |
glebiuskv | 0:2f0e1e23c242 | 197 | |
glebiuskv | 0:2f0e1e23c242 | 198 | |
glebiuskv | 0:2f0e1e23c242 | 199 | /* Post process on fatal error in the file operations */ |
glebiuskv | 0:2f0e1e23c242 | 200 | #define ABORT(fs, res) { fp->err = (BYTE)(res); LEAVE_FF(fs, res); } |
glebiuskv | 0:2f0e1e23c242 | 201 | |
glebiuskv | 0:2f0e1e23c242 | 202 | |
glebiuskv | 0:2f0e1e23c242 | 203 | /* Re-entrancy related */ |
glebiuskv | 0:2f0e1e23c242 | 204 | #if FF_FS_REENTRANT |
glebiuskv | 0:2f0e1e23c242 | 205 | #if FF_USE_LFN == 1 |
glebiuskv | 0:2f0e1e23c242 | 206 | #error Static LFN work area cannot be used at thread-safe configuration |
glebiuskv | 0:2f0e1e23c242 | 207 | #endif |
glebiuskv | 0:2f0e1e23c242 | 208 | #define LEAVE_FF(fs, res) { unlock_fs(fs, res); return res; } |
glebiuskv | 0:2f0e1e23c242 | 209 | #else |
glebiuskv | 0:2f0e1e23c242 | 210 | #define LEAVE_FF(fs, res) return res |
glebiuskv | 0:2f0e1e23c242 | 211 | #endif |
glebiuskv | 0:2f0e1e23c242 | 212 | |
glebiuskv | 0:2f0e1e23c242 | 213 | |
glebiuskv | 0:2f0e1e23c242 | 214 | /* Definitions of volume - physical location conversion */ |
glebiuskv | 0:2f0e1e23c242 | 215 | #if FF_MULTI_PARTITION |
glebiuskv | 0:2f0e1e23c242 | 216 | #define LD2PD(vol) VolToPart[vol].pd /* Get physical drive number */ |
glebiuskv | 0:2f0e1e23c242 | 217 | #define LD2PT(vol) VolToPart[vol].pt /* Get partition index */ |
glebiuskv | 0:2f0e1e23c242 | 218 | #else |
glebiuskv | 0:2f0e1e23c242 | 219 | #define LD2PD(vol) (BYTE)(vol) /* Each logical drive is bound to the same physical drive number */ |
glebiuskv | 0:2f0e1e23c242 | 220 | #define LD2PT(vol) 0 /* Find first valid partition or in SFD */ |
glebiuskv | 0:2f0e1e23c242 | 221 | #endif |
glebiuskv | 0:2f0e1e23c242 | 222 | |
glebiuskv | 0:2f0e1e23c242 | 223 | |
glebiuskv | 0:2f0e1e23c242 | 224 | /* Definitions of sector size */ |
glebiuskv | 0:2f0e1e23c242 | 225 | #if (FF_MAX_SS < FF_MIN_SS) || (FF_MAX_SS != 512 && FF_MAX_SS != 1024 && FF_MAX_SS != 2048 && FF_MAX_SS != 4096) || (FF_MIN_SS != 512 && FF_MIN_SS != 1024 && FF_MIN_SS != 2048 && FF_MIN_SS != 4096) |
glebiuskv | 0:2f0e1e23c242 | 226 | #error Wrong sector size configuration |
glebiuskv | 0:2f0e1e23c242 | 227 | #endif |
glebiuskv | 0:2f0e1e23c242 | 228 | #if FF_MAX_SS == FF_MIN_SS |
glebiuskv | 0:2f0e1e23c242 | 229 | #define SS(fs) ((UINT)FF_MAX_SS) /* Fixed sector size */ |
glebiuskv | 0:2f0e1e23c242 | 230 | #else |
glebiuskv | 0:2f0e1e23c242 | 231 | #define SS(fs) ((fs)->ssize) /* Variable sector size */ |
glebiuskv | 0:2f0e1e23c242 | 232 | #endif |
glebiuskv | 0:2f0e1e23c242 | 233 | |
glebiuskv | 0:2f0e1e23c242 | 234 | |
glebiuskv | 0:2f0e1e23c242 | 235 | /* Timestamp */ |
glebiuskv | 0:2f0e1e23c242 | 236 | #if FF_FS_NORTC == 1 |
glebiuskv | 0:2f0e1e23c242 | 237 | #if FF_NORTC_YEAR < 1980 || FF_NORTC_YEAR > 2107 || FF_NORTC_MON < 1 || FF_NORTC_MON > 12 || FF_NORTC_MDAY < 1 || FF_NORTC_MDAY > 31 |
glebiuskv | 0:2f0e1e23c242 | 238 | #error Invalid FF_FS_NORTC settings |
glebiuskv | 0:2f0e1e23c242 | 239 | #endif |
glebiuskv | 0:2f0e1e23c242 | 240 | #define GET_FATTIME() ((DWORD)(FF_NORTC_YEAR - 1980) << 25 | (DWORD)FF_NORTC_MON << 21 | (DWORD)FF_NORTC_MDAY << 16) |
glebiuskv | 0:2f0e1e23c242 | 241 | #else |
glebiuskv | 0:2f0e1e23c242 | 242 | #define GET_FATTIME() get_fattime() |
glebiuskv | 0:2f0e1e23c242 | 243 | #endif |
glebiuskv | 0:2f0e1e23c242 | 244 | |
glebiuskv | 0:2f0e1e23c242 | 245 | |
glebiuskv | 0:2f0e1e23c242 | 246 | /* File lock controls */ |
glebiuskv | 0:2f0e1e23c242 | 247 | #if FF_FS_LOCK != 0 |
glebiuskv | 0:2f0e1e23c242 | 248 | #if FF_FS_READONLY |
glebiuskv | 0:2f0e1e23c242 | 249 | #error FF_FS_LOCK must be 0 at read-only configuration |
glebiuskv | 0:2f0e1e23c242 | 250 | #endif |
glebiuskv | 0:2f0e1e23c242 | 251 | typedef struct { |
glebiuskv | 0:2f0e1e23c242 | 252 | FATFS *fs; /* Object ID 1, volume (NULL:blank entry) */ |
glebiuskv | 0:2f0e1e23c242 | 253 | DWORD clu; /* Object ID 2, containing directory (0:root) */ |
glebiuskv | 0:2f0e1e23c242 | 254 | DWORD ofs; /* Object ID 3, offset in the directory */ |
glebiuskv | 0:2f0e1e23c242 | 255 | WORD ctr; /* Object open counter, 0:none, 0x01..0xFF:read mode open count, 0x100:write mode */ |
glebiuskv | 0:2f0e1e23c242 | 256 | } FILESEM; |
glebiuskv | 0:2f0e1e23c242 | 257 | #endif |
glebiuskv | 0:2f0e1e23c242 | 258 | |
glebiuskv | 0:2f0e1e23c242 | 259 | |
glebiuskv | 0:2f0e1e23c242 | 260 | /* SBCS up-case tables (\x80-\xFF) */ |
glebiuskv | 0:2f0e1e23c242 | 261 | #define TBL_CT437 {0x80,0x9A,0x45,0x41,0x8E,0x41,0x8F,0x80,0x45,0x45,0x45,0x49,0x49,0x49,0x8E,0x8F, \ |
glebiuskv | 0:2f0e1e23c242 | 262 | 0x90,0x92,0x92,0x4F,0x99,0x4F,0x55,0x55,0x59,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \ |
glebiuskv | 0:2f0e1e23c242 | 263 | 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \ |
glebiuskv | 0:2f0e1e23c242 | 264 | 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \ |
glebiuskv | 0:2f0e1e23c242 | 265 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \ |
glebiuskv | 0:2f0e1e23c242 | 266 | 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ |
glebiuskv | 0:2f0e1e23c242 | 267 | 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \ |
glebiuskv | 0:2f0e1e23c242 | 268 | 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF} |
glebiuskv | 0:2f0e1e23c242 | 269 | #define TBL_CT720 {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \ |
glebiuskv | 0:2f0e1e23c242 | 270 | 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \ |
glebiuskv | 0:2f0e1e23c242 | 271 | 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \ |
glebiuskv | 0:2f0e1e23c242 | 272 | 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \ |
glebiuskv | 0:2f0e1e23c242 | 273 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \ |
glebiuskv | 0:2f0e1e23c242 | 274 | 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ |
glebiuskv | 0:2f0e1e23c242 | 275 | 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \ |
glebiuskv | 0:2f0e1e23c242 | 276 | 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF} |
glebiuskv | 0:2f0e1e23c242 | 277 | #define TBL_CT737 {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \ |
glebiuskv | 0:2f0e1e23c242 | 278 | 0x90,0x92,0x92,0x93,0x94,0x95,0x96,0x97,0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87, \ |
glebiuskv | 0:2f0e1e23c242 | 279 | 0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0xAA,0x92,0x93,0x94,0x95,0x96, \ |
glebiuskv | 0:2f0e1e23c242 | 280 | 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \ |
glebiuskv | 0:2f0e1e23c242 | 281 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \ |
glebiuskv | 0:2f0e1e23c242 | 282 | 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ |
glebiuskv | 0:2f0e1e23c242 | 283 | 0x97,0xEA,0xEB,0xEC,0xE4,0xED,0xEE,0xEF,0xF5,0xF0,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \ |
glebiuskv | 0:2f0e1e23c242 | 284 | 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF} |
glebiuskv | 0:2f0e1e23c242 | 285 | #define TBL_CT771 {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \ |
glebiuskv | 0:2f0e1e23c242 | 286 | 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \ |
glebiuskv | 0:2f0e1e23c242 | 287 | 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \ |
glebiuskv | 0:2f0e1e23c242 | 288 | 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \ |
glebiuskv | 0:2f0e1e23c242 | 289 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \ |
glebiuskv | 0:2f0e1e23c242 | 290 | 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDC,0xDE,0xDE, \ |
glebiuskv | 0:2f0e1e23c242 | 291 | 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \ |
glebiuskv | 0:2f0e1e23c242 | 292 | 0xF0,0xF0,0xF2,0xF2,0xF4,0xF4,0xF6,0xF6,0xF8,0xF8,0xFA,0xFA,0xFC,0xFC,0xFE,0xFF} |
glebiuskv | 0:2f0e1e23c242 | 293 | #define TBL_CT775 {0x80,0x9A,0x91,0xA0,0x8E,0x95,0x8F,0x80,0xAD,0xED,0x8A,0x8A,0xA1,0x8D,0x8E,0x8F, \ |
glebiuskv | 0:2f0e1e23c242 | 294 | 0x90,0x92,0x92,0xE2,0x99,0x95,0x96,0x97,0x97,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9F, \ |
glebiuskv | 0:2f0e1e23c242 | 295 | 0xA0,0xA1,0xE0,0xA3,0xA3,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \ |
glebiuskv | 0:2f0e1e23c242 | 296 | 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \ |
glebiuskv | 0:2f0e1e23c242 | 297 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \ |
glebiuskv | 0:2f0e1e23c242 | 298 | 0xB5,0xB6,0xB7,0xB8,0xBD,0xBE,0xC6,0xC7,0xA5,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ |
glebiuskv | 0:2f0e1e23c242 | 299 | 0xE0,0xE1,0xE2,0xE3,0xE5,0xE5,0xE6,0xE3,0xE8,0xE8,0xEA,0xEA,0xEE,0xED,0xEE,0xEF, \ |
glebiuskv | 0:2f0e1e23c242 | 300 | 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF} |
glebiuskv | 0:2f0e1e23c242 | 301 | #define TBL_CT850 {0x43,0x55,0x45,0x41,0x41,0x41,0x41,0x43,0x45,0x45,0x45,0x49,0x49,0x49,0x41,0x41, \ |
glebiuskv | 0:2f0e1e23c242 | 302 | 0x45,0x92,0x92,0x4F,0x4F,0x4F,0x55,0x55,0x59,0x4F,0x55,0x4F,0x9C,0x4F,0x9E,0x9F, \ |
glebiuskv | 0:2f0e1e23c242 | 303 | 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \ |
glebiuskv | 0:2f0e1e23c242 | 304 | 0xB0,0xB1,0xB2,0xB3,0xB4,0x41,0x41,0x41,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \ |
glebiuskv | 0:2f0e1e23c242 | 305 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0x41,0x41,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \ |
glebiuskv | 0:2f0e1e23c242 | 306 | 0xD1,0xD1,0x45,0x45,0x45,0x49,0x49,0x49,0x49,0xD9,0xDA,0xDB,0xDC,0xDD,0x49,0xDF, \ |
glebiuskv | 0:2f0e1e23c242 | 307 | 0x4F,0xE1,0x4F,0x4F,0x4F,0x4F,0xE6,0xE8,0xE8,0x55,0x55,0x55,0x59,0x59,0xEE,0xEF, \ |
glebiuskv | 0:2f0e1e23c242 | 308 | 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF} |
glebiuskv | 0:2f0e1e23c242 | 309 | #define TBL_CT852 {0x80,0x9A,0x90,0xB6,0x8E,0xDE,0x8F,0x80,0x9D,0xD3,0x8A,0x8A,0xD7,0x8D,0x8E,0x8F, \ |
glebiuskv | 0:2f0e1e23c242 | 310 | 0x90,0x91,0x91,0xE2,0x99,0x95,0x95,0x97,0x97,0x99,0x9A,0x9B,0x9B,0x9D,0x9E,0xAC, \ |
glebiuskv | 0:2f0e1e23c242 | 311 | 0xB5,0xD6,0xE0,0xE9,0xA4,0xA4,0xA6,0xA6,0xA8,0xA8,0xAA,0x8D,0xAC,0xB8,0xAE,0xAF, \ |
glebiuskv | 0:2f0e1e23c242 | 312 | 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBD,0xBF, \ |
glebiuskv | 0:2f0e1e23c242 | 313 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC6,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \ |
glebiuskv | 0:2f0e1e23c242 | 314 | 0xD1,0xD1,0xD2,0xD3,0xD2,0xD5,0xD6,0xD7,0xB7,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ |
glebiuskv | 0:2f0e1e23c242 | 315 | 0xE0,0xE1,0xE2,0xE3,0xE3,0xD5,0xE6,0xE6,0xE8,0xE9,0xE8,0xEB,0xED,0xED,0xDD,0xEF, \ |
glebiuskv | 0:2f0e1e23c242 | 316 | 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xEB,0xFC,0xFC,0xFE,0xFF} |
glebiuskv | 0:2f0e1e23c242 | 317 | #define TBL_CT855 {0x81,0x81,0x83,0x83,0x85,0x85,0x87,0x87,0x89,0x89,0x8B,0x8B,0x8D,0x8D,0x8F,0x8F, \ |
glebiuskv | 0:2f0e1e23c242 | 318 | 0x91,0x91,0x93,0x93,0x95,0x95,0x97,0x97,0x99,0x99,0x9B,0x9B,0x9D,0x9D,0x9F,0x9F, \ |
glebiuskv | 0:2f0e1e23c242 | 319 | 0xA1,0xA1,0xA3,0xA3,0xA5,0xA5,0xA7,0xA7,0xA9,0xA9,0xAB,0xAB,0xAD,0xAD,0xAE,0xAF, \ |
glebiuskv | 0:2f0e1e23c242 | 320 | 0xB0,0xB1,0xB2,0xB3,0xB4,0xB6,0xB6,0xB8,0xB8,0xB9,0xBA,0xBB,0xBC,0xBE,0xBE,0xBF, \ |
glebiuskv | 0:2f0e1e23c242 | 321 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC7,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \ |
glebiuskv | 0:2f0e1e23c242 | 322 | 0xD1,0xD1,0xD3,0xD3,0xD5,0xD5,0xD7,0xD7,0xDD,0xD9,0xDA,0xDB,0xDC,0xDD,0xE0,0xDF, \ |
glebiuskv | 0:2f0e1e23c242 | 323 | 0xE0,0xE2,0xE2,0xE4,0xE4,0xE6,0xE6,0xE8,0xE8,0xEA,0xEA,0xEC,0xEC,0xEE,0xEE,0xEF, \ |
glebiuskv | 0:2f0e1e23c242 | 324 | 0xF0,0xF2,0xF2,0xF4,0xF4,0xF6,0xF6,0xF8,0xF8,0xFA,0xFA,0xFC,0xFC,0xFD,0xFE,0xFF} |
glebiuskv | 0:2f0e1e23c242 | 325 | #define TBL_CT857 {0x80,0x9A,0x90,0xB6,0x8E,0xB7,0x8F,0x80,0xD2,0xD3,0xD4,0xD8,0xD7,0x49,0x8E,0x8F, \ |
glebiuskv | 0:2f0e1e23c242 | 326 | 0x90,0x92,0x92,0xE2,0x99,0xE3,0xEA,0xEB,0x98,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9E, \ |
glebiuskv | 0:2f0e1e23c242 | 327 | 0xB5,0xD6,0xE0,0xE9,0xA5,0xA5,0xA6,0xA6,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \ |
glebiuskv | 0:2f0e1e23c242 | 328 | 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \ |
glebiuskv | 0:2f0e1e23c242 | 329 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC7,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \ |
glebiuskv | 0:2f0e1e23c242 | 330 | 0xD0,0xD1,0xD2,0xD3,0xD4,0x49,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ |
glebiuskv | 0:2f0e1e23c242 | 331 | 0xE0,0xE1,0xE2,0xE3,0xE5,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xDE,0xED,0xEE,0xEF, \ |
glebiuskv | 0:2f0e1e23c242 | 332 | 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF} |
glebiuskv | 0:2f0e1e23c242 | 333 | #define TBL_CT860 {0x80,0x9A,0x90,0x8F,0x8E,0x91,0x86,0x80,0x89,0x89,0x92,0x8B,0x8C,0x98,0x8E,0x8F, \ |
glebiuskv | 0:2f0e1e23c242 | 334 | 0x90,0x91,0x92,0x8C,0x99,0xA9,0x96,0x9D,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \ |
glebiuskv | 0:2f0e1e23c242 | 335 | 0x86,0x8B,0x9F,0x96,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \ |
glebiuskv | 0:2f0e1e23c242 | 336 | 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \ |
glebiuskv | 0:2f0e1e23c242 | 337 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \ |
glebiuskv | 0:2f0e1e23c242 | 338 | 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ |
glebiuskv | 0:2f0e1e23c242 | 339 | 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \ |
glebiuskv | 0:2f0e1e23c242 | 340 | 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF} |
glebiuskv | 0:2f0e1e23c242 | 341 | #define TBL_CT861 {0x80,0x9A,0x90,0x41,0x8E,0x41,0x8F,0x80,0x45,0x45,0x45,0x8B,0x8B,0x8D,0x8E,0x8F, \ |
glebiuskv | 0:2f0e1e23c242 | 342 | 0x90,0x92,0x92,0x4F,0x99,0x8D,0x55,0x97,0x97,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9F, \ |
glebiuskv | 0:2f0e1e23c242 | 343 | 0xA4,0xA5,0xA6,0xA7,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \ |
glebiuskv | 0:2f0e1e23c242 | 344 | 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \ |
glebiuskv | 0:2f0e1e23c242 | 345 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \ |
glebiuskv | 0:2f0e1e23c242 | 346 | 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ |
glebiuskv | 0:2f0e1e23c242 | 347 | 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \ |
glebiuskv | 0:2f0e1e23c242 | 348 | 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF} |
glebiuskv | 0:2f0e1e23c242 | 349 | #define TBL_CT862 {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \ |
glebiuskv | 0:2f0e1e23c242 | 350 | 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \ |
glebiuskv | 0:2f0e1e23c242 | 351 | 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \ |
glebiuskv | 0:2f0e1e23c242 | 352 | 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \ |
glebiuskv | 0:2f0e1e23c242 | 353 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \ |
glebiuskv | 0:2f0e1e23c242 | 354 | 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ |
glebiuskv | 0:2f0e1e23c242 | 355 | 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \ |
glebiuskv | 0:2f0e1e23c242 | 356 | 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF} |
glebiuskv | 0:2f0e1e23c242 | 357 | #define TBL_CT863 {0x43,0x55,0x45,0x41,0x41,0x41,0x86,0x43,0x45,0x45,0x45,0x49,0x49,0x8D,0x41,0x8F, \ |
glebiuskv | 0:2f0e1e23c242 | 358 | 0x45,0x45,0x45,0x4F,0x45,0x49,0x55,0x55,0x98,0x4F,0x55,0x9B,0x9C,0x55,0x55,0x9F, \ |
glebiuskv | 0:2f0e1e23c242 | 359 | 0xA0,0xA1,0x4F,0x55,0xA4,0xA5,0xA6,0xA7,0x49,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \ |
glebiuskv | 0:2f0e1e23c242 | 360 | 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \ |
glebiuskv | 0:2f0e1e23c242 | 361 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \ |
glebiuskv | 0:2f0e1e23c242 | 362 | 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ |
glebiuskv | 0:2f0e1e23c242 | 363 | 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \ |
glebiuskv | 0:2f0e1e23c242 | 364 | 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF} |
glebiuskv | 0:2f0e1e23c242 | 365 | #define TBL_CT864 {0x80,0x9A,0x45,0x41,0x8E,0x41,0x8F,0x80,0x45,0x45,0x45,0x49,0x49,0x49,0x8E,0x8F, \ |
glebiuskv | 0:2f0e1e23c242 | 366 | 0x90,0x92,0x92,0x4F,0x99,0x4F,0x55,0x55,0x59,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \ |
glebiuskv | 0:2f0e1e23c242 | 367 | 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \ |
glebiuskv | 0:2f0e1e23c242 | 368 | 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \ |
glebiuskv | 0:2f0e1e23c242 | 369 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \ |
glebiuskv | 0:2f0e1e23c242 | 370 | 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ |
glebiuskv | 0:2f0e1e23c242 | 371 | 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \ |
glebiuskv | 0:2f0e1e23c242 | 372 | 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF} |
glebiuskv | 0:2f0e1e23c242 | 373 | #define TBL_CT865 {0x80,0x9A,0x90,0x41,0x8E,0x41,0x8F,0x80,0x45,0x45,0x45,0x49,0x49,0x49,0x8E,0x8F, \ |
glebiuskv | 0:2f0e1e23c242 | 374 | 0x90,0x92,0x92,0x4F,0x99,0x4F,0x55,0x55,0x59,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \ |
glebiuskv | 0:2f0e1e23c242 | 375 | 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \ |
glebiuskv | 0:2f0e1e23c242 | 376 | 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \ |
glebiuskv | 0:2f0e1e23c242 | 377 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \ |
glebiuskv | 0:2f0e1e23c242 | 378 | 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ |
glebiuskv | 0:2f0e1e23c242 | 379 | 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \ |
glebiuskv | 0:2f0e1e23c242 | 380 | 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF} |
glebiuskv | 0:2f0e1e23c242 | 381 | #define TBL_CT866 {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \ |
glebiuskv | 0:2f0e1e23c242 | 382 | 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \ |
glebiuskv | 0:2f0e1e23c242 | 383 | 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \ |
glebiuskv | 0:2f0e1e23c242 | 384 | 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \ |
glebiuskv | 0:2f0e1e23c242 | 385 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \ |
glebiuskv | 0:2f0e1e23c242 | 386 | 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ |
glebiuskv | 0:2f0e1e23c242 | 387 | 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \ |
glebiuskv | 0:2f0e1e23c242 | 388 | 0xF0,0xF0,0xF2,0xF2,0xF4,0xF4,0xF6,0xF6,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF} |
glebiuskv | 0:2f0e1e23c242 | 389 | #define TBL_CT869 {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \ |
glebiuskv | 0:2f0e1e23c242 | 390 | 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x86,0x9C,0x8D,0x8F,0x90, \ |
glebiuskv | 0:2f0e1e23c242 | 391 | 0x91,0x90,0x92,0x95,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \ |
glebiuskv | 0:2f0e1e23c242 | 392 | 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \ |
glebiuskv | 0:2f0e1e23c242 | 393 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \ |
glebiuskv | 0:2f0e1e23c242 | 394 | 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xA4,0xA5,0xA6,0xD9,0xDA,0xDB,0xDC,0xA7,0xA8,0xDF, \ |
glebiuskv | 0:2f0e1e23c242 | 395 | 0xA9,0xAA,0xAC,0xAD,0xB5,0xB6,0xB7,0xB8,0xBD,0xBE,0xC6,0xC7,0xCF,0xCF,0xD0,0xEF, \ |
glebiuskv | 0:2f0e1e23c242 | 396 | 0xF0,0xF1,0xD1,0xD2,0xD3,0xF5,0xD4,0xF7,0xF8,0xF9,0xD5,0x96,0x95,0x98,0xFE,0xFF} |
glebiuskv | 0:2f0e1e23c242 | 397 | |
glebiuskv | 0:2f0e1e23c242 | 398 | |
glebiuskv | 0:2f0e1e23c242 | 399 | /* DBCS code range |----- 1st byte -----| |----------- 2nd byte -----------| */ |
glebiuskv | 0:2f0e1e23c242 | 400 | #define TBL_DC932 {0x81, 0x9F, 0xE0, 0xFC, 0x40, 0x7E, 0x80, 0xFC, 0x00, 0x00} |
glebiuskv | 0:2f0e1e23c242 | 401 | #define TBL_DC936 {0x81, 0xFE, 0x00, 0x00, 0x40, 0x7E, 0x80, 0xFE, 0x00, 0x00} |
glebiuskv | 0:2f0e1e23c242 | 402 | #define TBL_DC949 {0x81, 0xFE, 0x00, 0x00, 0x41, 0x5A, 0x61, 0x7A, 0x81, 0xFE} |
glebiuskv | 0:2f0e1e23c242 | 403 | #define TBL_DC950 {0x81, 0xFE, 0x00, 0x00, 0x40, 0x7E, 0xA1, 0xFE, 0x00, 0x00} |
glebiuskv | 0:2f0e1e23c242 | 404 | |
glebiuskv | 0:2f0e1e23c242 | 405 | |
glebiuskv | 0:2f0e1e23c242 | 406 | /* Macros for table definitions */ |
glebiuskv | 0:2f0e1e23c242 | 407 | #define MERGE_2STR(a, b) a ## b |
glebiuskv | 0:2f0e1e23c242 | 408 | #define MKCVTBL(hd, cp) MERGE_2STR(hd, cp) |
glebiuskv | 0:2f0e1e23c242 | 409 | |
glebiuskv | 0:2f0e1e23c242 | 410 | |
glebiuskv | 0:2f0e1e23c242 | 411 | |
glebiuskv | 0:2f0e1e23c242 | 412 | |
glebiuskv | 0:2f0e1e23c242 | 413 | /*-------------------------------------------------------------------------- |
glebiuskv | 0:2f0e1e23c242 | 414 | |
glebiuskv | 0:2f0e1e23c242 | 415 | Module Private Work Area |
glebiuskv | 0:2f0e1e23c242 | 416 | |
glebiuskv | 0:2f0e1e23c242 | 417 | ---------------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 418 | /* Remark: Variables defined here without initial value shall be guaranteed |
glebiuskv | 0:2f0e1e23c242 | 419 | / zero/null at start-up. If not, the linker option or start-up routine is |
glebiuskv | 0:2f0e1e23c242 | 420 | / not compliance with C standard. */ |
glebiuskv | 0:2f0e1e23c242 | 421 | |
glebiuskv | 0:2f0e1e23c242 | 422 | /*--------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 423 | /* File/Volume controls */ |
glebiuskv | 0:2f0e1e23c242 | 424 | /*--------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 425 | |
glebiuskv | 0:2f0e1e23c242 | 426 | #if FF_VOLUMES < 1 || FF_VOLUMES > 10 |
glebiuskv | 0:2f0e1e23c242 | 427 | #error Wrong FF_VOLUMES setting |
glebiuskv | 0:2f0e1e23c242 | 428 | #endif |
glebiuskv | 0:2f0e1e23c242 | 429 | static FATFS *FatFs[FF_VOLUMES]; /* Pointer to the filesystem objects (logical drives) */ |
glebiuskv | 0:2f0e1e23c242 | 430 | static WORD Fsid; /* File system mount ID */ |
glebiuskv | 0:2f0e1e23c242 | 431 | |
glebiuskv | 0:2f0e1e23c242 | 432 | #if FF_FS_RPATH != 0 && FF_VOLUMES >= 2 |
glebiuskv | 0:2f0e1e23c242 | 433 | static BYTE CurrVol; /* Current drive */ |
glebiuskv | 0:2f0e1e23c242 | 434 | #endif |
glebiuskv | 0:2f0e1e23c242 | 435 | |
glebiuskv | 0:2f0e1e23c242 | 436 | #if FF_FS_LOCK != 0 |
glebiuskv | 0:2f0e1e23c242 | 437 | static FILESEM Files[FF_FS_LOCK]; /* Open object lock semaphores */ |
glebiuskv | 0:2f0e1e23c242 | 438 | #endif |
glebiuskv | 0:2f0e1e23c242 | 439 | |
glebiuskv | 0:2f0e1e23c242 | 440 | |
glebiuskv | 0:2f0e1e23c242 | 441 | |
glebiuskv | 0:2f0e1e23c242 | 442 | /*--------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 443 | /* LFN/Directory working buffer */ |
glebiuskv | 0:2f0e1e23c242 | 444 | /*--------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 445 | |
glebiuskv | 0:2f0e1e23c242 | 446 | #if FF_USE_LFN == 0 /* Non-LFN configuration */ |
glebiuskv | 0:2f0e1e23c242 | 447 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 448 | #error LFN must be enabled when enable exFAT |
glebiuskv | 0:2f0e1e23c242 | 449 | #endif |
glebiuskv | 0:2f0e1e23c242 | 450 | #define DEF_NAMBUF |
glebiuskv | 0:2f0e1e23c242 | 451 | #define INIT_NAMBUF(fs) |
glebiuskv | 0:2f0e1e23c242 | 452 | #define FREE_NAMBUF() |
glebiuskv | 0:2f0e1e23c242 | 453 | #define LEAVE_MKFS(res) return res |
glebiuskv | 0:2f0e1e23c242 | 454 | |
glebiuskv | 0:2f0e1e23c242 | 455 | #else /* LFN configurations */ |
glebiuskv | 0:2f0e1e23c242 | 456 | #if FF_MAX_LFN < 12 || FF_MAX_LFN > 255 |
glebiuskv | 0:2f0e1e23c242 | 457 | #error Wrong setting of FF_MAX_LFN |
glebiuskv | 0:2f0e1e23c242 | 458 | #endif |
glebiuskv | 0:2f0e1e23c242 | 459 | #if FF_LFN_BUF < 12 || FF_SFN_BUF < 12 || FF_LFN_BUF < FF_SFN_BUF |
glebiuskv | 0:2f0e1e23c242 | 460 | #error Wrong setting of FF_LFN_BUF or FF_SFN_BUF |
glebiuskv | 0:2f0e1e23c242 | 461 | #endif |
glebiuskv | 0:2f0e1e23c242 | 462 | #if FF_LFN_UNICODE < 0 || FF_LFN_UNICODE > 2 |
glebiuskv | 0:2f0e1e23c242 | 463 | #error Wrong setting of FF_LFN_UNICODE |
glebiuskv | 0:2f0e1e23c242 | 464 | #endif |
glebiuskv | 0:2f0e1e23c242 | 465 | static const BYTE LfnOfs[] = {1,3,5,7,9,14,16,18,20,22,24,28,30}; /* FAT: Offset of LFN characters in the directory entry */ |
glebiuskv | 0:2f0e1e23c242 | 466 | #define MAXDIRB(nc) ((nc + 44U) / 15 * SZDIRE) /* exFAT: Size of directory entry block scratchpad buffer needed for the name length */ |
glebiuskv | 0:2f0e1e23c242 | 467 | |
glebiuskv | 0:2f0e1e23c242 | 468 | #if FF_USE_LFN == 1 /* LFN enabled with static working buffer */ |
glebiuskv | 0:2f0e1e23c242 | 469 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 470 | static BYTE DirBuf[MAXDIRB(FF_MAX_LFN)]; /* Directory entry block scratchpad buffer */ |
glebiuskv | 0:2f0e1e23c242 | 471 | #endif |
glebiuskv | 0:2f0e1e23c242 | 472 | static WCHAR LfnBuf[FF_MAX_LFN + 1]; /* LFN working buffer */ |
glebiuskv | 0:2f0e1e23c242 | 473 | #define DEF_NAMBUF |
glebiuskv | 0:2f0e1e23c242 | 474 | #define INIT_NAMBUF(fs) |
glebiuskv | 0:2f0e1e23c242 | 475 | #define FREE_NAMBUF() |
glebiuskv | 0:2f0e1e23c242 | 476 | #define LEAVE_MKFS(res) return res |
glebiuskv | 0:2f0e1e23c242 | 477 | |
glebiuskv | 0:2f0e1e23c242 | 478 | #elif FF_USE_LFN == 2 /* LFN enabled with dynamic working buffer on the stack */ |
glebiuskv | 0:2f0e1e23c242 | 479 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 480 | #define DEF_NAMBUF WCHAR lbuf[FF_MAX_LFN+1]; BYTE dbuf[MAXDIRB(FF_MAX_LFN)]; /* LFN working buffer and directory entry block scratchpad buffer */ |
glebiuskv | 0:2f0e1e23c242 | 481 | #define INIT_NAMBUF(fs) { (fs)->lfnbuf = lbuf; (fs)->dirbuf = dbuf; } |
glebiuskv | 0:2f0e1e23c242 | 482 | #define FREE_NAMBUF() |
glebiuskv | 0:2f0e1e23c242 | 483 | #else |
glebiuskv | 0:2f0e1e23c242 | 484 | #define DEF_NAMBUF WCHAR lbuf[FF_MAX_LFN+1]; /* LFN working buffer */ |
glebiuskv | 0:2f0e1e23c242 | 485 | #define INIT_NAMBUF(fs) { (fs)->lfnbuf = lbuf; } |
glebiuskv | 0:2f0e1e23c242 | 486 | #define FREE_NAMBUF() |
glebiuskv | 0:2f0e1e23c242 | 487 | #endif |
glebiuskv | 0:2f0e1e23c242 | 488 | #define LEAVE_MKFS(res) return res |
glebiuskv | 0:2f0e1e23c242 | 489 | |
glebiuskv | 0:2f0e1e23c242 | 490 | #elif FF_USE_LFN == 3 /* LFN enabled with dynamic working buffer on the heap */ |
glebiuskv | 0:2f0e1e23c242 | 491 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 492 | #define DEF_NAMBUF WCHAR *lfn; /* Pointer to LFN working buffer and directory entry block scratchpad buffer */ |
glebiuskv | 0:2f0e1e23c242 | 493 | #define INIT_NAMBUF(fs) { lfn = ff_memalloc((FF_MAX_LFN+1)*2 + MAXDIRB(FF_MAX_LFN)); if (!lfn) LEAVE_FF(fs, FR_NOT_ENOUGH_CORE); (fs)->lfnbuf = lfn; (fs)->dirbuf = (BYTE*)(lfn+FF_MAX_LFN+1); } |
glebiuskv | 0:2f0e1e23c242 | 494 | #define FREE_NAMBUF() ff_memfree(lfn) |
glebiuskv | 0:2f0e1e23c242 | 495 | #else |
glebiuskv | 0:2f0e1e23c242 | 496 | #define DEF_NAMBUF WCHAR *lfn; /* Pointer to LFN working buffer */ |
glebiuskv | 0:2f0e1e23c242 | 497 | #define INIT_NAMBUF(fs) { lfn = (WCHAR *)ff_memalloc((FF_MAX_LFN+1)*2); if (!lfn) LEAVE_FF(fs, FR_NOT_ENOUGH_CORE); (fs)->lfnbuf = lfn; } |
glebiuskv | 0:2f0e1e23c242 | 498 | #define FREE_NAMBUF() ff_memfree(lfn) |
glebiuskv | 0:2f0e1e23c242 | 499 | #endif |
glebiuskv | 0:2f0e1e23c242 | 500 | #define LEAVE_MKFS(res) { if (!work) ff_memfree(buf); return res; } |
glebiuskv | 0:2f0e1e23c242 | 501 | #define MAX_MALLOC 0x8000 |
glebiuskv | 0:2f0e1e23c242 | 502 | |
glebiuskv | 0:2f0e1e23c242 | 503 | #else |
glebiuskv | 0:2f0e1e23c242 | 504 | #error Wrong setting of FF_USE_LFN |
glebiuskv | 0:2f0e1e23c242 | 505 | |
glebiuskv | 0:2f0e1e23c242 | 506 | #endif /* FF_USE_LFN == 1 */ |
glebiuskv | 0:2f0e1e23c242 | 507 | #endif /* FF_USE_LFN == 0 */ |
glebiuskv | 0:2f0e1e23c242 | 508 | |
glebiuskv | 0:2f0e1e23c242 | 509 | |
glebiuskv | 0:2f0e1e23c242 | 510 | |
glebiuskv | 0:2f0e1e23c242 | 511 | /*--------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 512 | /* Code conversion tables */ |
glebiuskv | 0:2f0e1e23c242 | 513 | /*--------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 514 | |
glebiuskv | 0:2f0e1e23c242 | 515 | #if FF_CODE_PAGE == 0 /* Run-time code page configuration */ |
glebiuskv | 0:2f0e1e23c242 | 516 | #define CODEPAGE CodePage |
glebiuskv | 0:2f0e1e23c242 | 517 | static WORD CodePage; /* Current code page */ |
glebiuskv | 0:2f0e1e23c242 | 518 | static const BYTE *ExCvt, *DbcTbl; /* Pointer to current SBCS up-case table and DBCS code range table below */ |
glebiuskv | 0:2f0e1e23c242 | 519 | static const BYTE Ct437[] = TBL_CT437; |
glebiuskv | 0:2f0e1e23c242 | 520 | static const BYTE Ct720[] = TBL_CT720; |
glebiuskv | 0:2f0e1e23c242 | 521 | static const BYTE Ct737[] = TBL_CT737; |
glebiuskv | 0:2f0e1e23c242 | 522 | static const BYTE Ct771[] = TBL_CT771; |
glebiuskv | 0:2f0e1e23c242 | 523 | static const BYTE Ct775[] = TBL_CT775; |
glebiuskv | 0:2f0e1e23c242 | 524 | static const BYTE Ct850[] = TBL_CT850; |
glebiuskv | 0:2f0e1e23c242 | 525 | static const BYTE Ct852[] = TBL_CT852; |
glebiuskv | 0:2f0e1e23c242 | 526 | static const BYTE Ct855[] = TBL_CT855; |
glebiuskv | 0:2f0e1e23c242 | 527 | static const BYTE Ct857[] = TBL_CT857; |
glebiuskv | 0:2f0e1e23c242 | 528 | static const BYTE Ct860[] = TBL_CT860; |
glebiuskv | 0:2f0e1e23c242 | 529 | static const BYTE Ct861[] = TBL_CT861; |
glebiuskv | 0:2f0e1e23c242 | 530 | static const BYTE Ct862[] = TBL_CT862; |
glebiuskv | 0:2f0e1e23c242 | 531 | static const BYTE Ct863[] = TBL_CT863; |
glebiuskv | 0:2f0e1e23c242 | 532 | static const BYTE Ct864[] = TBL_CT864; |
glebiuskv | 0:2f0e1e23c242 | 533 | static const BYTE Ct865[] = TBL_CT865; |
glebiuskv | 0:2f0e1e23c242 | 534 | static const BYTE Ct866[] = TBL_CT866; |
glebiuskv | 0:2f0e1e23c242 | 535 | static const BYTE Ct869[] = TBL_CT869; |
glebiuskv | 0:2f0e1e23c242 | 536 | static const BYTE Dc932[] = TBL_DC932; |
glebiuskv | 0:2f0e1e23c242 | 537 | static const BYTE Dc936[] = TBL_DC936; |
glebiuskv | 0:2f0e1e23c242 | 538 | static const BYTE Dc949[] = TBL_DC949; |
glebiuskv | 0:2f0e1e23c242 | 539 | static const BYTE Dc950[] = TBL_DC950; |
glebiuskv | 0:2f0e1e23c242 | 540 | |
glebiuskv | 0:2f0e1e23c242 | 541 | #elif FF_CODE_PAGE < 900 /* Static code page configuration (SBCS) */ |
glebiuskv | 0:2f0e1e23c242 | 542 | #define CODEPAGE FF_CODE_PAGE |
glebiuskv | 0:2f0e1e23c242 | 543 | static const BYTE ExCvt[] = MKCVTBL(TBL_CT, FF_CODE_PAGE); |
glebiuskv | 0:2f0e1e23c242 | 544 | |
glebiuskv | 0:2f0e1e23c242 | 545 | #else /* Static code page configuration (DBCS) */ |
glebiuskv | 0:2f0e1e23c242 | 546 | #define CODEPAGE FF_CODE_PAGE |
glebiuskv | 0:2f0e1e23c242 | 547 | static const BYTE DbcTbl[] = MKCVTBL(TBL_DC, FF_CODE_PAGE); |
glebiuskv | 0:2f0e1e23c242 | 548 | |
glebiuskv | 0:2f0e1e23c242 | 549 | #endif |
glebiuskv | 0:2f0e1e23c242 | 550 | |
glebiuskv | 0:2f0e1e23c242 | 551 | |
glebiuskv | 0:2f0e1e23c242 | 552 | |
glebiuskv | 0:2f0e1e23c242 | 553 | |
glebiuskv | 0:2f0e1e23c242 | 554 | /*-------------------------------------------------------------------------- |
glebiuskv | 0:2f0e1e23c242 | 555 | |
glebiuskv | 0:2f0e1e23c242 | 556 | Module Private Functions |
glebiuskv | 0:2f0e1e23c242 | 557 | |
glebiuskv | 0:2f0e1e23c242 | 558 | ---------------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 559 | |
glebiuskv | 0:2f0e1e23c242 | 560 | |
glebiuskv | 0:2f0e1e23c242 | 561 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 562 | /* Load/Store multi-byte word in the FAT structure */ |
glebiuskv | 0:2f0e1e23c242 | 563 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 564 | |
glebiuskv | 0:2f0e1e23c242 | 565 | static |
glebiuskv | 0:2f0e1e23c242 | 566 | WORD ld_word (const BYTE* ptr) /* Load a 2-byte little-endian word */ |
glebiuskv | 0:2f0e1e23c242 | 567 | { |
glebiuskv | 0:2f0e1e23c242 | 568 | WORD rv; |
glebiuskv | 0:2f0e1e23c242 | 569 | |
glebiuskv | 0:2f0e1e23c242 | 570 | rv = ptr[1]; |
glebiuskv | 0:2f0e1e23c242 | 571 | rv = rv << 8 | ptr[0]; |
glebiuskv | 0:2f0e1e23c242 | 572 | return rv; |
glebiuskv | 0:2f0e1e23c242 | 573 | } |
glebiuskv | 0:2f0e1e23c242 | 574 | |
glebiuskv | 0:2f0e1e23c242 | 575 | static |
glebiuskv | 0:2f0e1e23c242 | 576 | DWORD ld_dword (const BYTE* ptr) /* Load a 4-byte little-endian word */ |
glebiuskv | 0:2f0e1e23c242 | 577 | { |
glebiuskv | 0:2f0e1e23c242 | 578 | DWORD rv; |
glebiuskv | 0:2f0e1e23c242 | 579 | |
glebiuskv | 0:2f0e1e23c242 | 580 | rv = ptr[3]; |
glebiuskv | 0:2f0e1e23c242 | 581 | rv = rv << 8 | ptr[2]; |
glebiuskv | 0:2f0e1e23c242 | 582 | rv = rv << 8 | ptr[1]; |
glebiuskv | 0:2f0e1e23c242 | 583 | rv = rv << 8 | ptr[0]; |
glebiuskv | 0:2f0e1e23c242 | 584 | return rv; |
glebiuskv | 0:2f0e1e23c242 | 585 | } |
glebiuskv | 0:2f0e1e23c242 | 586 | |
glebiuskv | 0:2f0e1e23c242 | 587 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 588 | static |
glebiuskv | 0:2f0e1e23c242 | 589 | QWORD ld_qword (const BYTE* ptr) /* Load an 8-byte little-endian word */ |
glebiuskv | 0:2f0e1e23c242 | 590 | { |
glebiuskv | 0:2f0e1e23c242 | 591 | QWORD rv; |
glebiuskv | 0:2f0e1e23c242 | 592 | |
glebiuskv | 0:2f0e1e23c242 | 593 | rv = ptr[7]; |
glebiuskv | 0:2f0e1e23c242 | 594 | rv = rv << 8 | ptr[6]; |
glebiuskv | 0:2f0e1e23c242 | 595 | rv = rv << 8 | ptr[5]; |
glebiuskv | 0:2f0e1e23c242 | 596 | rv = rv << 8 | ptr[4]; |
glebiuskv | 0:2f0e1e23c242 | 597 | rv = rv << 8 | ptr[3]; |
glebiuskv | 0:2f0e1e23c242 | 598 | rv = rv << 8 | ptr[2]; |
glebiuskv | 0:2f0e1e23c242 | 599 | rv = rv << 8 | ptr[1]; |
glebiuskv | 0:2f0e1e23c242 | 600 | rv = rv << 8 | ptr[0]; |
glebiuskv | 0:2f0e1e23c242 | 601 | return rv; |
glebiuskv | 0:2f0e1e23c242 | 602 | } |
glebiuskv | 0:2f0e1e23c242 | 603 | #endif |
glebiuskv | 0:2f0e1e23c242 | 604 | |
glebiuskv | 0:2f0e1e23c242 | 605 | #if !FF_FS_READONLY |
glebiuskv | 0:2f0e1e23c242 | 606 | static |
glebiuskv | 0:2f0e1e23c242 | 607 | void st_word (BYTE* ptr, WORD val) /* Store a 2-byte word in little-endian */ |
glebiuskv | 0:2f0e1e23c242 | 608 | { |
glebiuskv | 0:2f0e1e23c242 | 609 | *ptr++ = (BYTE)val; val >>= 8; |
glebiuskv | 0:2f0e1e23c242 | 610 | *ptr++ = (BYTE)val; |
glebiuskv | 0:2f0e1e23c242 | 611 | } |
glebiuskv | 0:2f0e1e23c242 | 612 | |
glebiuskv | 0:2f0e1e23c242 | 613 | static |
glebiuskv | 0:2f0e1e23c242 | 614 | void st_dword (BYTE* ptr, DWORD val) /* Store a 4-byte word in little-endian */ |
glebiuskv | 0:2f0e1e23c242 | 615 | { |
glebiuskv | 0:2f0e1e23c242 | 616 | *ptr++ = (BYTE)val; val >>= 8; |
glebiuskv | 0:2f0e1e23c242 | 617 | *ptr++ = (BYTE)val; val >>= 8; |
glebiuskv | 0:2f0e1e23c242 | 618 | *ptr++ = (BYTE)val; val >>= 8; |
glebiuskv | 0:2f0e1e23c242 | 619 | *ptr++ = (BYTE)val; |
glebiuskv | 0:2f0e1e23c242 | 620 | } |
glebiuskv | 0:2f0e1e23c242 | 621 | |
glebiuskv | 0:2f0e1e23c242 | 622 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 623 | static |
glebiuskv | 0:2f0e1e23c242 | 624 | void st_qword (BYTE* ptr, QWORD val) /* Store an 8-byte word in little-endian */ |
glebiuskv | 0:2f0e1e23c242 | 625 | { |
glebiuskv | 0:2f0e1e23c242 | 626 | *ptr++ = (BYTE)val; val >>= 8; |
glebiuskv | 0:2f0e1e23c242 | 627 | *ptr++ = (BYTE)val; val >>= 8; |
glebiuskv | 0:2f0e1e23c242 | 628 | *ptr++ = (BYTE)val; val >>= 8; |
glebiuskv | 0:2f0e1e23c242 | 629 | *ptr++ = (BYTE)val; val >>= 8; |
glebiuskv | 0:2f0e1e23c242 | 630 | *ptr++ = (BYTE)val; val >>= 8; |
glebiuskv | 0:2f0e1e23c242 | 631 | *ptr++ = (BYTE)val; val >>= 8; |
glebiuskv | 0:2f0e1e23c242 | 632 | *ptr++ = (BYTE)val; val >>= 8; |
glebiuskv | 0:2f0e1e23c242 | 633 | *ptr++ = (BYTE)val; |
glebiuskv | 0:2f0e1e23c242 | 634 | } |
glebiuskv | 0:2f0e1e23c242 | 635 | #endif |
glebiuskv | 0:2f0e1e23c242 | 636 | #endif /* !FF_FS_READONLY */ |
glebiuskv | 0:2f0e1e23c242 | 637 | |
glebiuskv | 0:2f0e1e23c242 | 638 | |
glebiuskv | 0:2f0e1e23c242 | 639 | |
glebiuskv | 0:2f0e1e23c242 | 640 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 641 | /* String functions */ |
glebiuskv | 0:2f0e1e23c242 | 642 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 643 | |
glebiuskv | 0:2f0e1e23c242 | 644 | /* Copy memory to memory */ |
glebiuskv | 0:2f0e1e23c242 | 645 | static |
glebiuskv | 0:2f0e1e23c242 | 646 | void mem_cpy (void* dst, const void* src, UINT cnt) |
glebiuskv | 0:2f0e1e23c242 | 647 | { |
glebiuskv | 0:2f0e1e23c242 | 648 | BYTE *d = (BYTE*)dst; |
glebiuskv | 0:2f0e1e23c242 | 649 | const BYTE *s = (const BYTE*)src; |
glebiuskv | 0:2f0e1e23c242 | 650 | |
glebiuskv | 0:2f0e1e23c242 | 651 | if (cnt != 0) { |
glebiuskv | 0:2f0e1e23c242 | 652 | do { |
glebiuskv | 0:2f0e1e23c242 | 653 | *d++ = *s++; |
glebiuskv | 0:2f0e1e23c242 | 654 | } while (--cnt); |
glebiuskv | 0:2f0e1e23c242 | 655 | } |
glebiuskv | 0:2f0e1e23c242 | 656 | } |
glebiuskv | 0:2f0e1e23c242 | 657 | |
glebiuskv | 0:2f0e1e23c242 | 658 | |
glebiuskv | 0:2f0e1e23c242 | 659 | /* Fill memory block */ |
glebiuskv | 0:2f0e1e23c242 | 660 | static |
glebiuskv | 0:2f0e1e23c242 | 661 | void mem_set (void* dst, int val, UINT cnt) |
glebiuskv | 0:2f0e1e23c242 | 662 | { |
glebiuskv | 0:2f0e1e23c242 | 663 | BYTE *d = (BYTE*)dst; |
glebiuskv | 0:2f0e1e23c242 | 664 | |
glebiuskv | 0:2f0e1e23c242 | 665 | do { |
glebiuskv | 0:2f0e1e23c242 | 666 | *d++ = (BYTE)val; |
glebiuskv | 0:2f0e1e23c242 | 667 | } while (--cnt); |
glebiuskv | 0:2f0e1e23c242 | 668 | } |
glebiuskv | 0:2f0e1e23c242 | 669 | |
glebiuskv | 0:2f0e1e23c242 | 670 | |
glebiuskv | 0:2f0e1e23c242 | 671 | /* Compare memory block */ |
glebiuskv | 0:2f0e1e23c242 | 672 | static |
glebiuskv | 0:2f0e1e23c242 | 673 | int mem_cmp (const void* dst, const void* src, UINT cnt) /* ZR:same, NZ:different */ |
glebiuskv | 0:2f0e1e23c242 | 674 | { |
glebiuskv | 0:2f0e1e23c242 | 675 | const BYTE *d = (const BYTE *)dst, *s = (const BYTE *)src; |
glebiuskv | 0:2f0e1e23c242 | 676 | int r = 0; |
glebiuskv | 0:2f0e1e23c242 | 677 | |
glebiuskv | 0:2f0e1e23c242 | 678 | do { |
glebiuskv | 0:2f0e1e23c242 | 679 | r = *d++ - *s++; |
glebiuskv | 0:2f0e1e23c242 | 680 | } while (--cnt && r == 0); |
glebiuskv | 0:2f0e1e23c242 | 681 | |
glebiuskv | 0:2f0e1e23c242 | 682 | return r; |
glebiuskv | 0:2f0e1e23c242 | 683 | } |
glebiuskv | 0:2f0e1e23c242 | 684 | |
glebiuskv | 0:2f0e1e23c242 | 685 | |
glebiuskv | 0:2f0e1e23c242 | 686 | /* Check if chr is contained in the string */ |
glebiuskv | 0:2f0e1e23c242 | 687 | static |
glebiuskv | 0:2f0e1e23c242 | 688 | int chk_chr (const char* str, int chr) /* NZ:contained, ZR:not contained */ |
glebiuskv | 0:2f0e1e23c242 | 689 | { |
glebiuskv | 0:2f0e1e23c242 | 690 | while (*str && *str != chr) str++; |
glebiuskv | 0:2f0e1e23c242 | 691 | return *str; |
glebiuskv | 0:2f0e1e23c242 | 692 | } |
glebiuskv | 0:2f0e1e23c242 | 693 | |
glebiuskv | 0:2f0e1e23c242 | 694 | |
glebiuskv | 0:2f0e1e23c242 | 695 | /* Test if the character is DBC 1st byte */ |
glebiuskv | 0:2f0e1e23c242 | 696 | static |
glebiuskv | 0:2f0e1e23c242 | 697 | int dbc_1st (BYTE c) |
glebiuskv | 0:2f0e1e23c242 | 698 | { |
glebiuskv | 0:2f0e1e23c242 | 699 | #if FF_CODE_PAGE == 0 /* Variable code page */ |
glebiuskv | 0:2f0e1e23c242 | 700 | if (DbcTbl && c >= DbcTbl[0]) { |
glebiuskv | 0:2f0e1e23c242 | 701 | if (c <= DbcTbl[1]) return 1; /* 1st byte range 1 */ |
glebiuskv | 0:2f0e1e23c242 | 702 | if (c >= DbcTbl[2] && c <= DbcTbl[3]) return 1; /* 1st byte range 2 */ |
glebiuskv | 0:2f0e1e23c242 | 703 | } |
glebiuskv | 0:2f0e1e23c242 | 704 | #elif FF_CODE_PAGE >= 900 /* DBCS fixed code page */ |
glebiuskv | 0:2f0e1e23c242 | 705 | if (c >= DbcTbl[0]) { |
glebiuskv | 0:2f0e1e23c242 | 706 | if (c <= DbcTbl[1]) return 1; |
glebiuskv | 0:2f0e1e23c242 | 707 | if (c >= DbcTbl[2] && c <= DbcTbl[3]) return 1; |
glebiuskv | 0:2f0e1e23c242 | 708 | } |
glebiuskv | 0:2f0e1e23c242 | 709 | #else /* SBCS fixed code page */ |
glebiuskv | 0:2f0e1e23c242 | 710 | if (c != 0) return 0; /* Always false */ |
glebiuskv | 0:2f0e1e23c242 | 711 | #endif |
glebiuskv | 0:2f0e1e23c242 | 712 | return 0; |
glebiuskv | 0:2f0e1e23c242 | 713 | } |
glebiuskv | 0:2f0e1e23c242 | 714 | |
glebiuskv | 0:2f0e1e23c242 | 715 | |
glebiuskv | 0:2f0e1e23c242 | 716 | /* Test if the character is DBC 2nd byte */ |
glebiuskv | 0:2f0e1e23c242 | 717 | static |
glebiuskv | 0:2f0e1e23c242 | 718 | int dbc_2nd (BYTE c) |
glebiuskv | 0:2f0e1e23c242 | 719 | { |
glebiuskv | 0:2f0e1e23c242 | 720 | #if FF_CODE_PAGE == 0 /* Variable code page */ |
glebiuskv | 0:2f0e1e23c242 | 721 | if (DbcTbl && c >= DbcTbl[4]) { |
glebiuskv | 0:2f0e1e23c242 | 722 | if (c <= DbcTbl[5]) return 1; /* 2nd byte range 1 */ |
glebiuskv | 0:2f0e1e23c242 | 723 | if (c >= DbcTbl[6] && c <= DbcTbl[7]) return 1; /* 2nd byte range 2 */ |
glebiuskv | 0:2f0e1e23c242 | 724 | if (c >= DbcTbl[8] && c <= DbcTbl[9]) return 1; /* 2nd byte range 3 */ |
glebiuskv | 0:2f0e1e23c242 | 725 | } |
glebiuskv | 0:2f0e1e23c242 | 726 | #elif FF_CODE_PAGE >= 900 /* DBCS fixed code page */ |
glebiuskv | 0:2f0e1e23c242 | 727 | if (c >= DbcTbl[4]) { |
glebiuskv | 0:2f0e1e23c242 | 728 | if (c <= DbcTbl[5]) return 1; |
glebiuskv | 0:2f0e1e23c242 | 729 | if (c >= DbcTbl[6] && c <= DbcTbl[7]) return 1; |
glebiuskv | 0:2f0e1e23c242 | 730 | if (c >= DbcTbl[8] && c <= DbcTbl[9]) return 1; |
glebiuskv | 0:2f0e1e23c242 | 731 | } |
glebiuskv | 0:2f0e1e23c242 | 732 | #else /* SBCS fixed code page */ |
glebiuskv | 0:2f0e1e23c242 | 733 | if (c != 0) return 0; /* Always false */ |
glebiuskv | 0:2f0e1e23c242 | 734 | #endif |
glebiuskv | 0:2f0e1e23c242 | 735 | return 0; |
glebiuskv | 0:2f0e1e23c242 | 736 | } |
glebiuskv | 0:2f0e1e23c242 | 737 | |
glebiuskv | 0:2f0e1e23c242 | 738 | |
glebiuskv | 0:2f0e1e23c242 | 739 | #if FF_USE_LFN |
glebiuskv | 0:2f0e1e23c242 | 740 | |
glebiuskv | 0:2f0e1e23c242 | 741 | /* Get a character from TCHAR string in defined API encodeing */ |
glebiuskv | 0:2f0e1e23c242 | 742 | static |
glebiuskv | 0:2f0e1e23c242 | 743 | DWORD tchar2uni ( /* Returns character in UTF-16 encoding (>=0x10000 on double encoding unit, 0xFFFFFFFF on decode error) */ |
glebiuskv | 0:2f0e1e23c242 | 744 | const TCHAR** str /* Pointer to pointer to TCHAR string in configured encoding */ |
glebiuskv | 0:2f0e1e23c242 | 745 | ) |
glebiuskv | 0:2f0e1e23c242 | 746 | { |
glebiuskv | 0:2f0e1e23c242 | 747 | DWORD uc; |
glebiuskv | 0:2f0e1e23c242 | 748 | const TCHAR *p = *str; |
glebiuskv | 0:2f0e1e23c242 | 749 | |
glebiuskv | 0:2f0e1e23c242 | 750 | #if FF_LFN_UNICODE == 1 /* UTF-16 input */ |
glebiuskv | 0:2f0e1e23c242 | 751 | WCHAR wc; |
glebiuskv | 0:2f0e1e23c242 | 752 | |
glebiuskv | 0:2f0e1e23c242 | 753 | uc = *p++; |
glebiuskv | 0:2f0e1e23c242 | 754 | if (IsSurrogate(uc)) { /* Surrogate? */ |
glebiuskv | 0:2f0e1e23c242 | 755 | wc = *p++; /* Get low surrogate */ |
glebiuskv | 0:2f0e1e23c242 | 756 | if (!IsSurrogateH(uc) || !IsSurrogateL(wc)) return 0xFFFFFFFF; /* Wrong surrogate? */ |
glebiuskv | 0:2f0e1e23c242 | 757 | uc = uc << 16 | wc; |
glebiuskv | 0:2f0e1e23c242 | 758 | } |
glebiuskv | 0:2f0e1e23c242 | 759 | |
glebiuskv | 0:2f0e1e23c242 | 760 | #elif FF_LFN_UNICODE == 2 /* UTF-8 input */ |
glebiuskv | 0:2f0e1e23c242 | 761 | BYTE b; |
glebiuskv | 0:2f0e1e23c242 | 762 | int nf; |
glebiuskv | 0:2f0e1e23c242 | 763 | |
glebiuskv | 0:2f0e1e23c242 | 764 | uc = (BYTE)*p++; /* Get a byte */ |
glebiuskv | 0:2f0e1e23c242 | 765 | if (uc & 0x80) { /* Multiple byte code? */ |
glebiuskv | 0:2f0e1e23c242 | 766 | if ((uc & 0xE0) == 0xC0) { /* 2-byte sequence? */ |
glebiuskv | 0:2f0e1e23c242 | 767 | uc &= 0x1F; nf = 1; |
glebiuskv | 0:2f0e1e23c242 | 768 | } else { |
glebiuskv | 0:2f0e1e23c242 | 769 | if ((uc & 0xF0) == 0xE0) { /* 3-byte sequence? */ |
glebiuskv | 0:2f0e1e23c242 | 770 | uc &= 0x0F; nf = 2; |
glebiuskv | 0:2f0e1e23c242 | 771 | } else { |
glebiuskv | 0:2f0e1e23c242 | 772 | if ((uc & 0xF8) == 0xF0) { /* 4-byte sequence? */ |
glebiuskv | 0:2f0e1e23c242 | 773 | uc &= 0x07; nf = 3; |
glebiuskv | 0:2f0e1e23c242 | 774 | } else { /* Wrong sequence */ |
glebiuskv | 0:2f0e1e23c242 | 775 | return 0xFFFFFFFF; |
glebiuskv | 0:2f0e1e23c242 | 776 | } |
glebiuskv | 0:2f0e1e23c242 | 777 | } |
glebiuskv | 0:2f0e1e23c242 | 778 | } |
glebiuskv | 0:2f0e1e23c242 | 779 | do { /* Get trailing bytes */ |
glebiuskv | 0:2f0e1e23c242 | 780 | b = (BYTE)*p++; |
glebiuskv | 0:2f0e1e23c242 | 781 | if ((b & 0xC0) != 0x80) return 0xFFFFFFFF; /* Wrong sequence? */ |
glebiuskv | 0:2f0e1e23c242 | 782 | uc = uc << 6 | (b & 0x3F); |
glebiuskv | 0:2f0e1e23c242 | 783 | } while (--nf != 0); |
glebiuskv | 0:2f0e1e23c242 | 784 | if (uc < 0x80 || IsSurrogate(uc) || uc >= 0x110000) return 0xFFFFFFFF; /* Wrong code? */ |
glebiuskv | 0:2f0e1e23c242 | 785 | if (uc >= 0x10000) uc = 0xD800DC00 | ((uc - 0x10000) << 6 & 0x3FF0000) | (uc & 0x3FF); /* Make a surrogate pair if needed */ |
glebiuskv | 0:2f0e1e23c242 | 786 | } |
glebiuskv | 0:2f0e1e23c242 | 787 | |
glebiuskv | 0:2f0e1e23c242 | 788 | #else /* ANSI/OEM input */ |
glebiuskv | 0:2f0e1e23c242 | 789 | BYTE b; |
glebiuskv | 0:2f0e1e23c242 | 790 | WCHAR wc; |
glebiuskv | 0:2f0e1e23c242 | 791 | |
glebiuskv | 0:2f0e1e23c242 | 792 | wc = (BYTE)*p++; /* Get a byte */ |
glebiuskv | 0:2f0e1e23c242 | 793 | if (dbc_1st((BYTE)wc)) { /* Is it a DBC 1st byte? */ |
glebiuskv | 0:2f0e1e23c242 | 794 | b = (BYTE)*p++; /* Get 2nd byte */ |
glebiuskv | 0:2f0e1e23c242 | 795 | if (!dbc_2nd(b)) return 0xFFFFFFFF; /* Invalid code? */ |
glebiuskv | 0:2f0e1e23c242 | 796 | wc = (wc << 8) + b; /* Make a DBC */ |
glebiuskv | 0:2f0e1e23c242 | 797 | } |
glebiuskv | 0:2f0e1e23c242 | 798 | if (wc != 0) { |
glebiuskv | 0:2f0e1e23c242 | 799 | wc = ff_oem2uni(wc, CODEPAGE); /* ANSI/OEM ==> Unicode */ |
glebiuskv | 0:2f0e1e23c242 | 800 | if (wc == 0) return 0xFFFFFFFF; /* Invalid code? */ |
glebiuskv | 0:2f0e1e23c242 | 801 | } |
glebiuskv | 0:2f0e1e23c242 | 802 | uc = wc; |
glebiuskv | 0:2f0e1e23c242 | 803 | |
glebiuskv | 0:2f0e1e23c242 | 804 | #endif |
glebiuskv | 0:2f0e1e23c242 | 805 | *str = p; /* Next read pointer */ |
glebiuskv | 0:2f0e1e23c242 | 806 | return uc; |
glebiuskv | 0:2f0e1e23c242 | 807 | } |
glebiuskv | 0:2f0e1e23c242 | 808 | |
glebiuskv | 0:2f0e1e23c242 | 809 | |
glebiuskv | 0:2f0e1e23c242 | 810 | /* Output a TCHAR string in defined API encoding */ |
glebiuskv | 0:2f0e1e23c242 | 811 | static |
glebiuskv | 0:2f0e1e23c242 | 812 | BYTE put_utf ( /* Returns number of encoding units written (0:buffer overflow or wrong encoding) */ |
glebiuskv | 0:2f0e1e23c242 | 813 | DWORD chr, /* UTF-16 encoded character (Double encoding unit char if >=0x10000) */ |
glebiuskv | 0:2f0e1e23c242 | 814 | TCHAR* buf, /* Output buffer */ |
glebiuskv | 0:2f0e1e23c242 | 815 | UINT szb /* Size of the buffer */ |
glebiuskv | 0:2f0e1e23c242 | 816 | ) |
glebiuskv | 0:2f0e1e23c242 | 817 | { |
glebiuskv | 0:2f0e1e23c242 | 818 | #if FF_LFN_UNICODE == 1 /* UTF-16 output */ |
glebiuskv | 0:2f0e1e23c242 | 819 | WCHAR hs, wc; |
glebiuskv | 0:2f0e1e23c242 | 820 | |
glebiuskv | 0:2f0e1e23c242 | 821 | hs = (WCHAR)(chr >> 16); |
glebiuskv | 0:2f0e1e23c242 | 822 | wc = (WCHAR)chr; |
glebiuskv | 0:2f0e1e23c242 | 823 | if (hs == 0) { /* Single encoding unit? */ |
glebiuskv | 0:2f0e1e23c242 | 824 | if (szb < 1 || IsSurrogate(wc)) return 0; /* Buffer overflow or wrong code? */ |
glebiuskv | 0:2f0e1e23c242 | 825 | *buf = wc; |
glebiuskv | 0:2f0e1e23c242 | 826 | return 1; |
glebiuskv | 0:2f0e1e23c242 | 827 | } |
glebiuskv | 0:2f0e1e23c242 | 828 | if (szb < 2 || !IsSurrogateH(hs) || !IsSurrogateL(wc)) return 0; /* Buffer overflow or wrong surrogate? */ |
glebiuskv | 0:2f0e1e23c242 | 829 | *buf++ = hs; |
glebiuskv | 0:2f0e1e23c242 | 830 | *buf++ = wc; |
glebiuskv | 0:2f0e1e23c242 | 831 | return 2; |
glebiuskv | 0:2f0e1e23c242 | 832 | |
glebiuskv | 0:2f0e1e23c242 | 833 | #elif FF_LFN_UNICODE == 2 /* UTF-8 output */ |
glebiuskv | 0:2f0e1e23c242 | 834 | DWORD hc; |
glebiuskv | 0:2f0e1e23c242 | 835 | |
glebiuskv | 0:2f0e1e23c242 | 836 | if (chr < 0x80) { /* Single byte code? */ |
glebiuskv | 0:2f0e1e23c242 | 837 | if (szb < 1) return 0; /* Buffer overflow? */ |
glebiuskv | 0:2f0e1e23c242 | 838 | *buf = (TCHAR)chr; |
glebiuskv | 0:2f0e1e23c242 | 839 | return 1; |
glebiuskv | 0:2f0e1e23c242 | 840 | } |
glebiuskv | 0:2f0e1e23c242 | 841 | if (chr < 0x800) { /* 2-byte sequence? */ |
glebiuskv | 0:2f0e1e23c242 | 842 | if (szb < 2) return 0; /* Buffer overflow? */ |
glebiuskv | 0:2f0e1e23c242 | 843 | *buf++ = (TCHAR)(0xC0 | (chr >> 6 & 0x1F)); |
glebiuskv | 0:2f0e1e23c242 | 844 | *buf++ = (TCHAR)(0x80 | (chr >> 0 & 0x3F)); |
glebiuskv | 0:2f0e1e23c242 | 845 | return 2; |
glebiuskv | 0:2f0e1e23c242 | 846 | } |
glebiuskv | 0:2f0e1e23c242 | 847 | if (chr < 0x10000) { /* 3-byte sequence? */ |
glebiuskv | 0:2f0e1e23c242 | 848 | if (szb < 3 || IsSurrogate(chr)) return 0; /* Buffer overflow or wrong code? */ |
glebiuskv | 0:2f0e1e23c242 | 849 | *buf++ = (TCHAR)(0xE0 | (chr >> 12 & 0x0F)); |
glebiuskv | 0:2f0e1e23c242 | 850 | *buf++ = (TCHAR)(0x80 | (chr >> 6 & 0x3F)); |
glebiuskv | 0:2f0e1e23c242 | 851 | *buf++ = (TCHAR)(0x80 | (chr >> 0 & 0x3F)); |
glebiuskv | 0:2f0e1e23c242 | 852 | return 3; |
glebiuskv | 0:2f0e1e23c242 | 853 | } |
glebiuskv | 0:2f0e1e23c242 | 854 | /* 4-byte sequence */ |
glebiuskv | 0:2f0e1e23c242 | 855 | if (szb < 4) return 0; /* Buffer overflow? */ |
glebiuskv | 0:2f0e1e23c242 | 856 | hc = ((chr & 0xFFFF0000) - 0xD8000000) >> 6; /* Get high 10 bits */ |
glebiuskv | 0:2f0e1e23c242 | 857 | chr = (chr & 0xFFFF) - 0xDC00; /* Get low 10 bits */ |
glebiuskv | 0:2f0e1e23c242 | 858 | if (hc >= 0x100000 || chr >= 0x400) return 0; /* Wrong surrogate? */ |
glebiuskv | 0:2f0e1e23c242 | 859 | chr = (hc | chr) + 0x10000; |
glebiuskv | 0:2f0e1e23c242 | 860 | *buf++ = (TCHAR)(0xF0 | (chr >> 18 & 0x07)); |
glebiuskv | 0:2f0e1e23c242 | 861 | *buf++ = (TCHAR)(0x80 | (chr >> 12 & 0x3F)); |
glebiuskv | 0:2f0e1e23c242 | 862 | *buf++ = (TCHAR)(0x80 | (chr >> 6 & 0x3F)); |
glebiuskv | 0:2f0e1e23c242 | 863 | *buf++ = (TCHAR)(0x80 | (chr >> 0 & 0x3F)); |
glebiuskv | 0:2f0e1e23c242 | 864 | return 4; |
glebiuskv | 0:2f0e1e23c242 | 865 | |
glebiuskv | 0:2f0e1e23c242 | 866 | #else /* ANSI/OEM output */ |
glebiuskv | 0:2f0e1e23c242 | 867 | WCHAR wc; |
glebiuskv | 0:2f0e1e23c242 | 868 | |
glebiuskv | 0:2f0e1e23c242 | 869 | wc = ff_uni2oem(chr, CODEPAGE); |
glebiuskv | 0:2f0e1e23c242 | 870 | if (wc >= 0x100) { /* Is this a DBC? */ |
glebiuskv | 0:2f0e1e23c242 | 871 | if (szb < 2) return 0; |
glebiuskv | 0:2f0e1e23c242 | 872 | *buf++ = (char)(wc >> 8); /* Store DBC 1st byte */ |
glebiuskv | 0:2f0e1e23c242 | 873 | *buf++ = (TCHAR)wc; /* Store DBC 2nd byte */ |
glebiuskv | 0:2f0e1e23c242 | 874 | return 2; |
glebiuskv | 0:2f0e1e23c242 | 875 | } |
glebiuskv | 0:2f0e1e23c242 | 876 | if (wc == 0 || szb < 1) return 0; /* Invalid char or buffer overflow? */ |
glebiuskv | 0:2f0e1e23c242 | 877 | *buf++ = (TCHAR)wc; /* Store the character */ |
glebiuskv | 0:2f0e1e23c242 | 878 | return 1; |
glebiuskv | 0:2f0e1e23c242 | 879 | #endif |
glebiuskv | 0:2f0e1e23c242 | 880 | } |
glebiuskv | 0:2f0e1e23c242 | 881 | #endif /* FF_USE_LFN */ |
glebiuskv | 0:2f0e1e23c242 | 882 | |
glebiuskv | 0:2f0e1e23c242 | 883 | |
glebiuskv | 0:2f0e1e23c242 | 884 | #if FF_FS_REENTRANT |
glebiuskv | 0:2f0e1e23c242 | 885 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 886 | /* Request/Release grant to access the volume */ |
glebiuskv | 0:2f0e1e23c242 | 887 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 888 | static |
glebiuskv | 0:2f0e1e23c242 | 889 | int lock_fs ( /* 1:Ok, 0:timeout */ |
glebiuskv | 0:2f0e1e23c242 | 890 | FATFS* fs /* Filesystem object */ |
glebiuskv | 0:2f0e1e23c242 | 891 | ) |
glebiuskv | 0:2f0e1e23c242 | 892 | { |
glebiuskv | 0:2f0e1e23c242 | 893 | return ff_req_grant(fs->sobj); |
glebiuskv | 0:2f0e1e23c242 | 894 | } |
glebiuskv | 0:2f0e1e23c242 | 895 | |
glebiuskv | 0:2f0e1e23c242 | 896 | |
glebiuskv | 0:2f0e1e23c242 | 897 | static |
glebiuskv | 0:2f0e1e23c242 | 898 | void unlock_fs ( |
glebiuskv | 0:2f0e1e23c242 | 899 | FATFS* fs, /* Filesystem object */ |
glebiuskv | 0:2f0e1e23c242 | 900 | FRESULT res /* Result code to be returned */ |
glebiuskv | 0:2f0e1e23c242 | 901 | ) |
glebiuskv | 0:2f0e1e23c242 | 902 | { |
glebiuskv | 0:2f0e1e23c242 | 903 | if (fs && res != FR_NOT_ENABLED && res != FR_INVALID_DRIVE && res != FR_TIMEOUT) { |
glebiuskv | 0:2f0e1e23c242 | 904 | ff_rel_grant(fs->sobj); |
glebiuskv | 0:2f0e1e23c242 | 905 | } |
glebiuskv | 0:2f0e1e23c242 | 906 | } |
glebiuskv | 0:2f0e1e23c242 | 907 | |
glebiuskv | 0:2f0e1e23c242 | 908 | #endif |
glebiuskv | 0:2f0e1e23c242 | 909 | |
glebiuskv | 0:2f0e1e23c242 | 910 | |
glebiuskv | 0:2f0e1e23c242 | 911 | |
glebiuskv | 0:2f0e1e23c242 | 912 | #if FF_FS_LOCK != 0 |
glebiuskv | 0:2f0e1e23c242 | 913 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 914 | /* File lock control functions */ |
glebiuskv | 0:2f0e1e23c242 | 915 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 916 | |
glebiuskv | 0:2f0e1e23c242 | 917 | static |
glebiuskv | 0:2f0e1e23c242 | 918 | FRESULT chk_lock ( /* Check if the file can be accessed */ |
glebiuskv | 0:2f0e1e23c242 | 919 | FATFS_DIR* dp, /* Directory object pointing the file to be checked */ |
glebiuskv | 0:2f0e1e23c242 | 920 | int acc /* Desired access type (0:Read mode open, 1:Write mode open, 2:Delete or rename) */ |
glebiuskv | 0:2f0e1e23c242 | 921 | ) |
glebiuskv | 0:2f0e1e23c242 | 922 | { |
glebiuskv | 0:2f0e1e23c242 | 923 | UINT i, be; |
glebiuskv | 0:2f0e1e23c242 | 924 | |
glebiuskv | 0:2f0e1e23c242 | 925 | /* Search open object table for the object */ |
glebiuskv | 0:2f0e1e23c242 | 926 | be = 0; |
glebiuskv | 0:2f0e1e23c242 | 927 | for (i = 0; i < FF_FS_LOCK; i++) { |
glebiuskv | 0:2f0e1e23c242 | 928 | if (Files[i].fs) { /* Existing entry */ |
glebiuskv | 0:2f0e1e23c242 | 929 | if (Files[i].fs == dp->obj.fs && /* Check if the object matches with an open object */ |
glebiuskv | 0:2f0e1e23c242 | 930 | Files[i].clu == dp->obj.sclust && |
glebiuskv | 0:2f0e1e23c242 | 931 | Files[i].ofs == dp->dptr) break; |
glebiuskv | 0:2f0e1e23c242 | 932 | } else { /* Blank entry */ |
glebiuskv | 0:2f0e1e23c242 | 933 | be = 1; |
glebiuskv | 0:2f0e1e23c242 | 934 | } |
glebiuskv | 0:2f0e1e23c242 | 935 | } |
glebiuskv | 0:2f0e1e23c242 | 936 | if (i == FF_FS_LOCK) { /* The object has not been opened */ |
glebiuskv | 0:2f0e1e23c242 | 937 | return (!be && acc != 2) ? FR_TOO_MANY_OPEN_FILES : FR_OK; /* Is there a blank entry for new object? */ |
glebiuskv | 0:2f0e1e23c242 | 938 | } |
glebiuskv | 0:2f0e1e23c242 | 939 | |
glebiuskv | 0:2f0e1e23c242 | 940 | /* The object was opened. Reject any open against writing file and all write mode open */ |
glebiuskv | 0:2f0e1e23c242 | 941 | return (acc != 0 || Files[i].ctr == 0x100) ? FR_LOCKED : FR_OK; |
glebiuskv | 0:2f0e1e23c242 | 942 | } |
glebiuskv | 0:2f0e1e23c242 | 943 | |
glebiuskv | 0:2f0e1e23c242 | 944 | |
glebiuskv | 0:2f0e1e23c242 | 945 | static |
glebiuskv | 0:2f0e1e23c242 | 946 | int enq_lock (void) /* Check if an entry is available for a new object */ |
glebiuskv | 0:2f0e1e23c242 | 947 | { |
glebiuskv | 0:2f0e1e23c242 | 948 | UINT i; |
glebiuskv | 0:2f0e1e23c242 | 949 | |
glebiuskv | 0:2f0e1e23c242 | 950 | for (i = 0; i < FF_FS_LOCK && Files[i].fs; i++) ; |
glebiuskv | 0:2f0e1e23c242 | 951 | return (i == FF_FS_LOCK) ? 0 : 1; |
glebiuskv | 0:2f0e1e23c242 | 952 | } |
glebiuskv | 0:2f0e1e23c242 | 953 | |
glebiuskv | 0:2f0e1e23c242 | 954 | |
glebiuskv | 0:2f0e1e23c242 | 955 | static |
glebiuskv | 0:2f0e1e23c242 | 956 | UINT inc_lock ( /* Increment object open counter and returns its index (0:Internal error) */ |
glebiuskv | 0:2f0e1e23c242 | 957 | FATFS_DIR* dp,/* Directory object pointing the file to register or increment */ |
glebiuskv | 0:2f0e1e23c242 | 958 | int acc /* Desired access (0:Read, 1:Write, 2:Delete/Rename) */ |
glebiuskv | 0:2f0e1e23c242 | 959 | ) |
glebiuskv | 0:2f0e1e23c242 | 960 | { |
glebiuskv | 0:2f0e1e23c242 | 961 | UINT i; |
glebiuskv | 0:2f0e1e23c242 | 962 | |
glebiuskv | 0:2f0e1e23c242 | 963 | |
glebiuskv | 0:2f0e1e23c242 | 964 | for (i = 0; i < FF_FS_LOCK; i++) { /* Find the object */ |
glebiuskv | 0:2f0e1e23c242 | 965 | if (Files[i].fs == dp->obj.fs && |
glebiuskv | 0:2f0e1e23c242 | 966 | Files[i].clu == dp->obj.sclust && |
glebiuskv | 0:2f0e1e23c242 | 967 | Files[i].ofs == dp->dptr) break; |
glebiuskv | 0:2f0e1e23c242 | 968 | } |
glebiuskv | 0:2f0e1e23c242 | 969 | |
glebiuskv | 0:2f0e1e23c242 | 970 | if (i == FF_FS_LOCK) { /* Not opened. Register it as new. */ |
glebiuskv | 0:2f0e1e23c242 | 971 | for (i = 0; i < FF_FS_LOCK && Files[i].fs; i++) ; |
glebiuskv | 0:2f0e1e23c242 | 972 | if (i == FF_FS_LOCK) return 0; /* No free entry to register (int err) */ |
glebiuskv | 0:2f0e1e23c242 | 973 | Files[i].fs = dp->obj.fs; |
glebiuskv | 0:2f0e1e23c242 | 974 | Files[i].clu = dp->obj.sclust; |
glebiuskv | 0:2f0e1e23c242 | 975 | Files[i].ofs = dp->dptr; |
glebiuskv | 0:2f0e1e23c242 | 976 | Files[i].ctr = 0; |
glebiuskv | 0:2f0e1e23c242 | 977 | } |
glebiuskv | 0:2f0e1e23c242 | 978 | |
glebiuskv | 0:2f0e1e23c242 | 979 | if (acc >= 1 && Files[i].ctr) return 0; /* Access violation (int err) */ |
glebiuskv | 0:2f0e1e23c242 | 980 | |
glebiuskv | 0:2f0e1e23c242 | 981 | Files[i].ctr = acc ? 0x100 : Files[i].ctr + 1; /* Set semaphore value */ |
glebiuskv | 0:2f0e1e23c242 | 982 | |
glebiuskv | 0:2f0e1e23c242 | 983 | return i + 1; /* Index number origin from 1 */ |
glebiuskv | 0:2f0e1e23c242 | 984 | } |
glebiuskv | 0:2f0e1e23c242 | 985 | |
glebiuskv | 0:2f0e1e23c242 | 986 | |
glebiuskv | 0:2f0e1e23c242 | 987 | static |
glebiuskv | 0:2f0e1e23c242 | 988 | FRESULT dec_lock ( /* Decrement object open counter */ |
glebiuskv | 0:2f0e1e23c242 | 989 | UINT i /* Semaphore index (1..) */ |
glebiuskv | 0:2f0e1e23c242 | 990 | ) |
glebiuskv | 0:2f0e1e23c242 | 991 | { |
glebiuskv | 0:2f0e1e23c242 | 992 | WORD n; |
glebiuskv | 0:2f0e1e23c242 | 993 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 994 | |
glebiuskv | 0:2f0e1e23c242 | 995 | |
glebiuskv | 0:2f0e1e23c242 | 996 | if (--i < FF_FS_LOCK) { /* Index number origin from 0 */ |
glebiuskv | 0:2f0e1e23c242 | 997 | n = Files[i].ctr; |
glebiuskv | 0:2f0e1e23c242 | 998 | if (n == 0x100) n = 0; /* If write mode open, delete the entry */ |
glebiuskv | 0:2f0e1e23c242 | 999 | if (n > 0) n--; /* Decrement read mode open count */ |
glebiuskv | 0:2f0e1e23c242 | 1000 | Files[i].ctr = n; |
glebiuskv | 0:2f0e1e23c242 | 1001 | if (n == 0) Files[i].fs = 0; /* Delete the entry if open count gets zero */ |
glebiuskv | 0:2f0e1e23c242 | 1002 | res = FR_OK; |
glebiuskv | 0:2f0e1e23c242 | 1003 | } else { |
glebiuskv | 0:2f0e1e23c242 | 1004 | res = FR_INT_ERR; /* Invalid index nunber */ |
glebiuskv | 0:2f0e1e23c242 | 1005 | } |
glebiuskv | 0:2f0e1e23c242 | 1006 | return res; |
glebiuskv | 0:2f0e1e23c242 | 1007 | } |
glebiuskv | 0:2f0e1e23c242 | 1008 | |
glebiuskv | 0:2f0e1e23c242 | 1009 | |
glebiuskv | 0:2f0e1e23c242 | 1010 | static |
glebiuskv | 0:2f0e1e23c242 | 1011 | void clear_lock ( /* Clear lock entries of the volume */ |
glebiuskv | 0:2f0e1e23c242 | 1012 | FATFS *fs |
glebiuskv | 0:2f0e1e23c242 | 1013 | ) |
glebiuskv | 0:2f0e1e23c242 | 1014 | { |
glebiuskv | 0:2f0e1e23c242 | 1015 | UINT i; |
glebiuskv | 0:2f0e1e23c242 | 1016 | |
glebiuskv | 0:2f0e1e23c242 | 1017 | for (i = 0; i < FF_FS_LOCK; i++) { |
glebiuskv | 0:2f0e1e23c242 | 1018 | if (Files[i].fs == fs) Files[i].fs = 0; |
glebiuskv | 0:2f0e1e23c242 | 1019 | } |
glebiuskv | 0:2f0e1e23c242 | 1020 | } |
glebiuskv | 0:2f0e1e23c242 | 1021 | |
glebiuskv | 0:2f0e1e23c242 | 1022 | #endif /* FF_FS_LOCK != 0 */ |
glebiuskv | 0:2f0e1e23c242 | 1023 | |
glebiuskv | 0:2f0e1e23c242 | 1024 | |
glebiuskv | 0:2f0e1e23c242 | 1025 | |
glebiuskv | 0:2f0e1e23c242 | 1026 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1027 | /* Move/Flush disk access window in the filesystem object */ |
glebiuskv | 0:2f0e1e23c242 | 1028 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1029 | #if !FF_FS_READONLY |
glebiuskv | 0:2f0e1e23c242 | 1030 | static |
glebiuskv | 0:2f0e1e23c242 | 1031 | FRESULT sync_window ( /* Returns FR_OK or FR_DISK_ERR */ |
glebiuskv | 0:2f0e1e23c242 | 1032 | FATFS* fs /* Filesystem object */ |
glebiuskv | 0:2f0e1e23c242 | 1033 | ) |
glebiuskv | 0:2f0e1e23c242 | 1034 | { |
glebiuskv | 0:2f0e1e23c242 | 1035 | FRESULT res = FR_OK; |
glebiuskv | 0:2f0e1e23c242 | 1036 | |
glebiuskv | 0:2f0e1e23c242 | 1037 | |
glebiuskv | 0:2f0e1e23c242 | 1038 | if (fs->wflag) { /* Is the disk access window dirty */ |
glebiuskv | 0:2f0e1e23c242 | 1039 | if (disk_write(fs->pdrv, fs->win, fs->winsect, 1) == RES_OK) { /* Write back the window */ |
glebiuskv | 0:2f0e1e23c242 | 1040 | fs->wflag = 0; /* Clear window dirty flag */ |
glebiuskv | 0:2f0e1e23c242 | 1041 | if (fs->winsect - fs->fatbase < fs->fsize) { /* Is it in the 1st FAT? */ |
glebiuskv | 0:2f0e1e23c242 | 1042 | if (fs->n_fats == 2) disk_write(fs->pdrv, fs->win, fs->winsect + fs->fsize, 1); /* Reflect it to 2nd FAT if needed */ |
glebiuskv | 0:2f0e1e23c242 | 1043 | } |
glebiuskv | 0:2f0e1e23c242 | 1044 | } else { |
glebiuskv | 0:2f0e1e23c242 | 1045 | res = FR_DISK_ERR; |
glebiuskv | 0:2f0e1e23c242 | 1046 | } |
glebiuskv | 0:2f0e1e23c242 | 1047 | } |
glebiuskv | 0:2f0e1e23c242 | 1048 | return res; |
glebiuskv | 0:2f0e1e23c242 | 1049 | } |
glebiuskv | 0:2f0e1e23c242 | 1050 | #endif |
glebiuskv | 0:2f0e1e23c242 | 1051 | |
glebiuskv | 0:2f0e1e23c242 | 1052 | |
glebiuskv | 0:2f0e1e23c242 | 1053 | static |
glebiuskv | 0:2f0e1e23c242 | 1054 | FRESULT move_window ( /* Returns FR_OK or FR_DISK_ERR */ |
glebiuskv | 0:2f0e1e23c242 | 1055 | FATFS* fs, /* Filesystem object */ |
glebiuskv | 0:2f0e1e23c242 | 1056 | DWORD sector /* Sector number to make appearance in the fs->win[] */ |
glebiuskv | 0:2f0e1e23c242 | 1057 | ) |
glebiuskv | 0:2f0e1e23c242 | 1058 | { |
glebiuskv | 0:2f0e1e23c242 | 1059 | FRESULT res = FR_OK; |
glebiuskv | 0:2f0e1e23c242 | 1060 | |
glebiuskv | 0:2f0e1e23c242 | 1061 | |
glebiuskv | 0:2f0e1e23c242 | 1062 | if (sector != fs->winsect) { /* Window offset changed? */ |
glebiuskv | 0:2f0e1e23c242 | 1063 | #if !FF_FS_READONLY |
glebiuskv | 0:2f0e1e23c242 | 1064 | res = sync_window(fs); /* Write-back changes */ |
glebiuskv | 0:2f0e1e23c242 | 1065 | #endif |
glebiuskv | 0:2f0e1e23c242 | 1066 | if (res == FR_OK) { /* Fill sector window with new data */ |
glebiuskv | 0:2f0e1e23c242 | 1067 | if (disk_read(fs->pdrv, fs->win, sector, 1) != RES_OK) { |
glebiuskv | 0:2f0e1e23c242 | 1068 | sector = 0xFFFFFFFF; /* Invalidate window if read data is not valid */ |
glebiuskv | 0:2f0e1e23c242 | 1069 | res = FR_DISK_ERR; |
glebiuskv | 0:2f0e1e23c242 | 1070 | } |
glebiuskv | 0:2f0e1e23c242 | 1071 | fs->winsect = sector; |
glebiuskv | 0:2f0e1e23c242 | 1072 | } |
glebiuskv | 0:2f0e1e23c242 | 1073 | } |
glebiuskv | 0:2f0e1e23c242 | 1074 | return res; |
glebiuskv | 0:2f0e1e23c242 | 1075 | } |
glebiuskv | 0:2f0e1e23c242 | 1076 | |
glebiuskv | 0:2f0e1e23c242 | 1077 | |
glebiuskv | 0:2f0e1e23c242 | 1078 | |
glebiuskv | 0:2f0e1e23c242 | 1079 | |
glebiuskv | 0:2f0e1e23c242 | 1080 | #if !FF_FS_READONLY |
glebiuskv | 0:2f0e1e23c242 | 1081 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1082 | /* Synchronize filesystem and data on the storage */ |
glebiuskv | 0:2f0e1e23c242 | 1083 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1084 | |
glebiuskv | 0:2f0e1e23c242 | 1085 | static |
glebiuskv | 0:2f0e1e23c242 | 1086 | FRESULT sync_fs ( /* Returns FR_OK or FR_DISK_ERR */ |
glebiuskv | 0:2f0e1e23c242 | 1087 | FATFS* fs /* Filesystem object */ |
glebiuskv | 0:2f0e1e23c242 | 1088 | ) |
glebiuskv | 0:2f0e1e23c242 | 1089 | { |
glebiuskv | 0:2f0e1e23c242 | 1090 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 1091 | |
glebiuskv | 0:2f0e1e23c242 | 1092 | |
glebiuskv | 0:2f0e1e23c242 | 1093 | res = sync_window(fs); |
glebiuskv | 0:2f0e1e23c242 | 1094 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 1095 | if (fs->fs_type == FS_FAT32 && fs->fsi_flag == 1) { /* FAT32: Update FSInfo sector if needed */ |
glebiuskv | 0:2f0e1e23c242 | 1096 | /* Create FSInfo structure */ |
glebiuskv | 0:2f0e1e23c242 | 1097 | mem_set(fs->win, 0, SS(fs)); |
glebiuskv | 0:2f0e1e23c242 | 1098 | st_word(fs->win + BS_55AA, 0xAA55); |
glebiuskv | 0:2f0e1e23c242 | 1099 | st_dword(fs->win + FSI_LeadSig, 0x41615252); |
glebiuskv | 0:2f0e1e23c242 | 1100 | st_dword(fs->win + FSI_StrucSig, 0x61417272); |
glebiuskv | 0:2f0e1e23c242 | 1101 | st_dword(fs->win + FSI_Free_Count, fs->free_clst); |
glebiuskv | 0:2f0e1e23c242 | 1102 | st_dword(fs->win + FSI_Nxt_Free, fs->last_clst); |
glebiuskv | 0:2f0e1e23c242 | 1103 | /* Write it into the FSInfo sector */ |
glebiuskv | 0:2f0e1e23c242 | 1104 | fs->winsect = fs->volbase + 1; |
glebiuskv | 0:2f0e1e23c242 | 1105 | disk_write(fs->pdrv, fs->win, fs->winsect, 1); |
glebiuskv | 0:2f0e1e23c242 | 1106 | fs->fsi_flag = 0; |
glebiuskv | 0:2f0e1e23c242 | 1107 | } |
glebiuskv | 0:2f0e1e23c242 | 1108 | /* Make sure that no pending write process in the lower layer */ |
glebiuskv | 0:2f0e1e23c242 | 1109 | if (disk_ioctl(fs->pdrv, CTRL_SYNC, 0) != RES_OK) res = FR_DISK_ERR; |
glebiuskv | 0:2f0e1e23c242 | 1110 | } |
glebiuskv | 0:2f0e1e23c242 | 1111 | |
glebiuskv | 0:2f0e1e23c242 | 1112 | return res; |
glebiuskv | 0:2f0e1e23c242 | 1113 | } |
glebiuskv | 0:2f0e1e23c242 | 1114 | |
glebiuskv | 0:2f0e1e23c242 | 1115 | #endif |
glebiuskv | 0:2f0e1e23c242 | 1116 | |
glebiuskv | 0:2f0e1e23c242 | 1117 | |
glebiuskv | 0:2f0e1e23c242 | 1118 | |
glebiuskv | 0:2f0e1e23c242 | 1119 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1120 | /* Get physical sector number from cluster number */ |
glebiuskv | 0:2f0e1e23c242 | 1121 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1122 | |
glebiuskv | 0:2f0e1e23c242 | 1123 | static |
glebiuskv | 0:2f0e1e23c242 | 1124 | DWORD clst2sect ( /* !=0:Sector number, 0:Failed (invalid cluster#) */ |
glebiuskv | 0:2f0e1e23c242 | 1125 | FATFS* fs, /* Filesystem object */ |
glebiuskv | 0:2f0e1e23c242 | 1126 | DWORD clst /* Cluster# to be converted */ |
glebiuskv | 0:2f0e1e23c242 | 1127 | ) |
glebiuskv | 0:2f0e1e23c242 | 1128 | { |
glebiuskv | 0:2f0e1e23c242 | 1129 | clst -= 2; /* Cluster number is origin from 2 */ |
glebiuskv | 0:2f0e1e23c242 | 1130 | if (clst >= fs->n_fatent - 2) return 0; /* Is it invalid cluster number? */ |
glebiuskv | 0:2f0e1e23c242 | 1131 | return fs->database + fs->csize * clst; /* Start sector number of the cluster */ |
glebiuskv | 0:2f0e1e23c242 | 1132 | } |
glebiuskv | 0:2f0e1e23c242 | 1133 | |
glebiuskv | 0:2f0e1e23c242 | 1134 | |
glebiuskv | 0:2f0e1e23c242 | 1135 | |
glebiuskv | 0:2f0e1e23c242 | 1136 | |
glebiuskv | 0:2f0e1e23c242 | 1137 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1138 | /* FAT access - Read value of a FAT entry */ |
glebiuskv | 0:2f0e1e23c242 | 1139 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1140 | |
glebiuskv | 0:2f0e1e23c242 | 1141 | static |
glebiuskv | 0:2f0e1e23c242 | 1142 | DWORD get_fat ( /* 0xFFFFFFFF:Disk error, 1:Internal error, 2..0x7FFFFFFF:Cluster status */ |
glebiuskv | 0:2f0e1e23c242 | 1143 | FFOBJID* obj, /* Corresponding object */ |
glebiuskv | 0:2f0e1e23c242 | 1144 | DWORD clst /* Cluster number to get the value */ |
glebiuskv | 0:2f0e1e23c242 | 1145 | ) |
glebiuskv | 0:2f0e1e23c242 | 1146 | { |
glebiuskv | 0:2f0e1e23c242 | 1147 | UINT wc, bc; |
glebiuskv | 0:2f0e1e23c242 | 1148 | DWORD val; |
glebiuskv | 0:2f0e1e23c242 | 1149 | FATFS *fs = obj->fs; |
glebiuskv | 0:2f0e1e23c242 | 1150 | |
glebiuskv | 0:2f0e1e23c242 | 1151 | |
glebiuskv | 0:2f0e1e23c242 | 1152 | if (clst < 2 || clst >= fs->n_fatent) { /* Check if in valid range */ |
glebiuskv | 0:2f0e1e23c242 | 1153 | val = 1; /* Internal error */ |
glebiuskv | 0:2f0e1e23c242 | 1154 | |
glebiuskv | 0:2f0e1e23c242 | 1155 | } else { |
glebiuskv | 0:2f0e1e23c242 | 1156 | val = 0xFFFFFFFF; /* Default value falls on disk error */ |
glebiuskv | 0:2f0e1e23c242 | 1157 | |
glebiuskv | 0:2f0e1e23c242 | 1158 | switch (fs->fs_type) { |
glebiuskv | 0:2f0e1e23c242 | 1159 | case FS_FAT12 : |
glebiuskv | 0:2f0e1e23c242 | 1160 | bc = (UINT)clst; bc += bc / 2; |
glebiuskv | 0:2f0e1e23c242 | 1161 | if (move_window(fs, fs->fatbase + (bc / SS(fs))) != FR_OK) break; |
glebiuskv | 0:2f0e1e23c242 | 1162 | wc = fs->win[bc++ % SS(fs)]; /* Get 1st byte of the entry */ |
glebiuskv | 0:2f0e1e23c242 | 1163 | if (move_window(fs, fs->fatbase + (bc / SS(fs))) != FR_OK) break; |
glebiuskv | 0:2f0e1e23c242 | 1164 | wc |= fs->win[bc % SS(fs)] << 8; /* Merge 2nd byte of the entry */ |
glebiuskv | 0:2f0e1e23c242 | 1165 | val = (clst & 1) ? (wc >> 4) : (wc & 0xFFF); /* Adjust bit position */ |
glebiuskv | 0:2f0e1e23c242 | 1166 | break; |
glebiuskv | 0:2f0e1e23c242 | 1167 | |
glebiuskv | 0:2f0e1e23c242 | 1168 | case FS_FAT16 : |
glebiuskv | 0:2f0e1e23c242 | 1169 | if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 2))) != FR_OK) break; |
glebiuskv | 0:2f0e1e23c242 | 1170 | val = ld_word(fs->win + clst * 2 % SS(fs)); /* Simple WORD array */ |
glebiuskv | 0:2f0e1e23c242 | 1171 | break; |
glebiuskv | 0:2f0e1e23c242 | 1172 | |
glebiuskv | 0:2f0e1e23c242 | 1173 | case FS_FAT32 : |
glebiuskv | 0:2f0e1e23c242 | 1174 | if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 4))) != FR_OK) break; |
glebiuskv | 0:2f0e1e23c242 | 1175 | val = ld_dword(fs->win + clst * 4 % SS(fs)) & 0x0FFFFFFF; /* Simple DWORD array but mask out upper 4 bits */ |
glebiuskv | 0:2f0e1e23c242 | 1176 | break; |
glebiuskv | 0:2f0e1e23c242 | 1177 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 1178 | case FS_EXFAT : |
glebiuskv | 0:2f0e1e23c242 | 1179 | if (obj->objsize != 0) { |
glebiuskv | 0:2f0e1e23c242 | 1180 | DWORD cofs = clst - obj->sclust; /* Offset from start cluster */ |
glebiuskv | 0:2f0e1e23c242 | 1181 | DWORD clen = (DWORD)((obj->objsize - 1) / SS(fs)) / fs->csize; /* Number of clusters - 1 */ |
glebiuskv | 0:2f0e1e23c242 | 1182 | |
glebiuskv | 0:2f0e1e23c242 | 1183 | if (obj->stat == 2 && cofs <= clen) { /* Is it a contiguous chain? */ |
glebiuskv | 0:2f0e1e23c242 | 1184 | val = (cofs == clen) ? 0x7FFFFFFF : clst + 1; /* No data on the FAT, generate the value */ |
glebiuskv | 0:2f0e1e23c242 | 1185 | break; |
glebiuskv | 0:2f0e1e23c242 | 1186 | } |
glebiuskv | 0:2f0e1e23c242 | 1187 | if (obj->stat == 3 && cofs < obj->n_cont) { /* Is it in the 1st fragment? */ |
glebiuskv | 0:2f0e1e23c242 | 1188 | val = clst + 1; /* Generate the value */ |
glebiuskv | 0:2f0e1e23c242 | 1189 | break; |
glebiuskv | 0:2f0e1e23c242 | 1190 | } |
glebiuskv | 0:2f0e1e23c242 | 1191 | if (obj->stat != 2) { /* Get value from FAT if FAT chain is valid */ |
glebiuskv | 0:2f0e1e23c242 | 1192 | if (obj->n_frag != 0) { /* Is it on the growing edge? */ |
glebiuskv | 0:2f0e1e23c242 | 1193 | val = 0x7FFFFFFF; /* Generate EOC */ |
glebiuskv | 0:2f0e1e23c242 | 1194 | } else { |
glebiuskv | 0:2f0e1e23c242 | 1195 | if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 4))) != FR_OK) break; |
glebiuskv | 0:2f0e1e23c242 | 1196 | val = ld_dword(fs->win + clst * 4 % SS(fs)) & 0x7FFFFFFF; |
glebiuskv | 0:2f0e1e23c242 | 1197 | } |
glebiuskv | 0:2f0e1e23c242 | 1198 | break; |
glebiuskv | 0:2f0e1e23c242 | 1199 | } |
glebiuskv | 0:2f0e1e23c242 | 1200 | } |
glebiuskv | 0:2f0e1e23c242 | 1201 | /* go to default */ |
glebiuskv | 0:2f0e1e23c242 | 1202 | #endif |
glebiuskv | 0:2f0e1e23c242 | 1203 | default: |
glebiuskv | 0:2f0e1e23c242 | 1204 | val = 1; /* Internal error */ |
glebiuskv | 0:2f0e1e23c242 | 1205 | } |
glebiuskv | 0:2f0e1e23c242 | 1206 | } |
glebiuskv | 0:2f0e1e23c242 | 1207 | |
glebiuskv | 0:2f0e1e23c242 | 1208 | return val; |
glebiuskv | 0:2f0e1e23c242 | 1209 | } |
glebiuskv | 0:2f0e1e23c242 | 1210 | |
glebiuskv | 0:2f0e1e23c242 | 1211 | |
glebiuskv | 0:2f0e1e23c242 | 1212 | |
glebiuskv | 0:2f0e1e23c242 | 1213 | |
glebiuskv | 0:2f0e1e23c242 | 1214 | #if !FF_FS_READONLY |
glebiuskv | 0:2f0e1e23c242 | 1215 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1216 | /* FAT access - Change value of a FAT entry */ |
glebiuskv | 0:2f0e1e23c242 | 1217 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1218 | |
glebiuskv | 0:2f0e1e23c242 | 1219 | static |
glebiuskv | 0:2f0e1e23c242 | 1220 | FRESULT put_fat ( /* FR_OK(0):succeeded, !=0:error */ |
glebiuskv | 0:2f0e1e23c242 | 1221 | FATFS* fs, /* Corresponding filesystem object */ |
glebiuskv | 0:2f0e1e23c242 | 1222 | DWORD clst, /* FAT index number (cluster number) to be changed */ |
glebiuskv | 0:2f0e1e23c242 | 1223 | DWORD val /* New value to be set to the entry */ |
glebiuskv | 0:2f0e1e23c242 | 1224 | ) |
glebiuskv | 0:2f0e1e23c242 | 1225 | { |
glebiuskv | 0:2f0e1e23c242 | 1226 | UINT bc; |
glebiuskv | 0:2f0e1e23c242 | 1227 | BYTE *p; |
glebiuskv | 0:2f0e1e23c242 | 1228 | FRESULT res = FR_INT_ERR; |
glebiuskv | 0:2f0e1e23c242 | 1229 | |
glebiuskv | 0:2f0e1e23c242 | 1230 | |
glebiuskv | 0:2f0e1e23c242 | 1231 | if (clst >= 2 && clst < fs->n_fatent) { /* Check if in valid range */ |
glebiuskv | 0:2f0e1e23c242 | 1232 | switch (fs->fs_type) { |
glebiuskv | 0:2f0e1e23c242 | 1233 | case FS_FAT12 : |
glebiuskv | 0:2f0e1e23c242 | 1234 | bc = (UINT)clst; bc += bc / 2; /* bc: byte offset of the entry */ |
glebiuskv | 0:2f0e1e23c242 | 1235 | res = move_window(fs, fs->fatbase + (bc / SS(fs))); |
glebiuskv | 0:2f0e1e23c242 | 1236 | if (res != FR_OK) break; |
glebiuskv | 0:2f0e1e23c242 | 1237 | p = fs->win + bc++ % SS(fs); |
glebiuskv | 0:2f0e1e23c242 | 1238 | *p = (clst & 1) ? ((*p & 0x0F) | ((BYTE)val << 4)) : (BYTE)val; /* Put 1st byte */ |
glebiuskv | 0:2f0e1e23c242 | 1239 | fs->wflag = 1; |
glebiuskv | 0:2f0e1e23c242 | 1240 | res = move_window(fs, fs->fatbase + (bc / SS(fs))); |
glebiuskv | 0:2f0e1e23c242 | 1241 | if (res != FR_OK) break; |
glebiuskv | 0:2f0e1e23c242 | 1242 | p = fs->win + bc % SS(fs); |
glebiuskv | 0:2f0e1e23c242 | 1243 | *p = (clst & 1) ? (BYTE)(val >> 4) : ((*p & 0xF0) | ((BYTE)(val >> 8) & 0x0F)); /* Put 2nd byte */ |
glebiuskv | 0:2f0e1e23c242 | 1244 | fs->wflag = 1; |
glebiuskv | 0:2f0e1e23c242 | 1245 | break; |
glebiuskv | 0:2f0e1e23c242 | 1246 | |
glebiuskv | 0:2f0e1e23c242 | 1247 | case FS_FAT16 : |
glebiuskv | 0:2f0e1e23c242 | 1248 | res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 2))); |
glebiuskv | 0:2f0e1e23c242 | 1249 | if (res != FR_OK) break; |
glebiuskv | 0:2f0e1e23c242 | 1250 | st_word(fs->win + clst * 2 % SS(fs), (WORD)val); /* Simple WORD array */ |
glebiuskv | 0:2f0e1e23c242 | 1251 | fs->wflag = 1; |
glebiuskv | 0:2f0e1e23c242 | 1252 | break; |
glebiuskv | 0:2f0e1e23c242 | 1253 | |
glebiuskv | 0:2f0e1e23c242 | 1254 | case FS_FAT32 : |
glebiuskv | 0:2f0e1e23c242 | 1255 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 1256 | case FS_EXFAT : |
glebiuskv | 0:2f0e1e23c242 | 1257 | #endif |
glebiuskv | 0:2f0e1e23c242 | 1258 | res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 4))); |
glebiuskv | 0:2f0e1e23c242 | 1259 | if (res != FR_OK) break; |
glebiuskv | 0:2f0e1e23c242 | 1260 | if (!FF_FS_EXFAT || fs->fs_type != FS_EXFAT) { |
glebiuskv | 0:2f0e1e23c242 | 1261 | val = (val & 0x0FFFFFFF) | (ld_dword(fs->win + clst * 4 % SS(fs)) & 0xF0000000); |
glebiuskv | 0:2f0e1e23c242 | 1262 | } |
glebiuskv | 0:2f0e1e23c242 | 1263 | st_dword(fs->win + clst * 4 % SS(fs), val); |
glebiuskv | 0:2f0e1e23c242 | 1264 | fs->wflag = 1; |
glebiuskv | 0:2f0e1e23c242 | 1265 | break; |
glebiuskv | 0:2f0e1e23c242 | 1266 | } |
glebiuskv | 0:2f0e1e23c242 | 1267 | } |
glebiuskv | 0:2f0e1e23c242 | 1268 | return res; |
glebiuskv | 0:2f0e1e23c242 | 1269 | } |
glebiuskv | 0:2f0e1e23c242 | 1270 | |
glebiuskv | 0:2f0e1e23c242 | 1271 | #endif /* !FF_FS_READONLY */ |
glebiuskv | 0:2f0e1e23c242 | 1272 | |
glebiuskv | 0:2f0e1e23c242 | 1273 | |
glebiuskv | 0:2f0e1e23c242 | 1274 | |
glebiuskv | 0:2f0e1e23c242 | 1275 | |
glebiuskv | 0:2f0e1e23c242 | 1276 | #if FF_FS_EXFAT && !FF_FS_READONLY |
glebiuskv | 0:2f0e1e23c242 | 1277 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1278 | /* exFAT: Accessing FAT and Allocation Bitmap */ |
glebiuskv | 0:2f0e1e23c242 | 1279 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1280 | |
glebiuskv | 0:2f0e1e23c242 | 1281 | /*--------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1282 | /* Find a contiguous free cluster block */ |
glebiuskv | 0:2f0e1e23c242 | 1283 | /*--------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1284 | |
glebiuskv | 0:2f0e1e23c242 | 1285 | static |
glebiuskv | 0:2f0e1e23c242 | 1286 | DWORD find_bitmap ( /* 0:Not found, 2..:Cluster block found, 0xFFFFFFFF:Disk error */ |
glebiuskv | 0:2f0e1e23c242 | 1287 | FATFS* fs, /* Filesystem object */ |
glebiuskv | 0:2f0e1e23c242 | 1288 | DWORD clst, /* Cluster number to scan from */ |
glebiuskv | 0:2f0e1e23c242 | 1289 | DWORD ncl /* Number of contiguous clusters to find (1..) */ |
glebiuskv | 0:2f0e1e23c242 | 1290 | ) |
glebiuskv | 0:2f0e1e23c242 | 1291 | { |
glebiuskv | 0:2f0e1e23c242 | 1292 | BYTE bm, bv; |
glebiuskv | 0:2f0e1e23c242 | 1293 | UINT i; |
glebiuskv | 0:2f0e1e23c242 | 1294 | DWORD val, scl, ctr; |
glebiuskv | 0:2f0e1e23c242 | 1295 | |
glebiuskv | 0:2f0e1e23c242 | 1296 | |
glebiuskv | 0:2f0e1e23c242 | 1297 | clst -= 2; /* The first bit in the bitmap corresponds to cluster #2 */ |
glebiuskv | 0:2f0e1e23c242 | 1298 | if (clst >= fs->n_fatent - 2) clst = 0; |
glebiuskv | 0:2f0e1e23c242 | 1299 | scl = val = clst; ctr = 0; |
glebiuskv | 0:2f0e1e23c242 | 1300 | for (;;) { |
glebiuskv | 0:2f0e1e23c242 | 1301 | if (move_window(fs, fs->database + val / 8 / SS(fs)) != FR_OK) return 0xFFFFFFFF; /* (assuming bitmap is located top of the cluster heap) */ |
glebiuskv | 0:2f0e1e23c242 | 1302 | i = val / 8 % SS(fs); bm = 1 << (val % 8); |
glebiuskv | 0:2f0e1e23c242 | 1303 | do { |
glebiuskv | 0:2f0e1e23c242 | 1304 | do { |
glebiuskv | 0:2f0e1e23c242 | 1305 | bv = fs->win[i] & bm; bm <<= 1; /* Get bit value */ |
glebiuskv | 0:2f0e1e23c242 | 1306 | if (++val >= fs->n_fatent - 2) { /* Next cluster (with wrap-around) */ |
glebiuskv | 0:2f0e1e23c242 | 1307 | val = 0; bm = 0; i = SS(fs); |
glebiuskv | 0:2f0e1e23c242 | 1308 | } |
glebiuskv | 0:2f0e1e23c242 | 1309 | if (bv == 0) { /* Is it a free cluster? */ |
glebiuskv | 0:2f0e1e23c242 | 1310 | if (++ctr == ncl) return scl + 2; /* Check if run length is sufficient for required */ |
glebiuskv | 0:2f0e1e23c242 | 1311 | } else { |
glebiuskv | 0:2f0e1e23c242 | 1312 | scl = val; ctr = 0; /* Encountered a cluster in-use, restart to scan */ |
glebiuskv | 0:2f0e1e23c242 | 1313 | } |
glebiuskv | 0:2f0e1e23c242 | 1314 | if (val == clst) return 0; /* All cluster scanned? */ |
glebiuskv | 0:2f0e1e23c242 | 1315 | } while (bm != 0); |
glebiuskv | 0:2f0e1e23c242 | 1316 | bm = 1; |
glebiuskv | 0:2f0e1e23c242 | 1317 | } while (++i < SS(fs)); |
glebiuskv | 0:2f0e1e23c242 | 1318 | } |
glebiuskv | 0:2f0e1e23c242 | 1319 | } |
glebiuskv | 0:2f0e1e23c242 | 1320 | |
glebiuskv | 0:2f0e1e23c242 | 1321 | |
glebiuskv | 0:2f0e1e23c242 | 1322 | /*----------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1323 | /* Set/Clear a block of allocation bitmap */ |
glebiuskv | 0:2f0e1e23c242 | 1324 | /*----------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1325 | |
glebiuskv | 0:2f0e1e23c242 | 1326 | static |
glebiuskv | 0:2f0e1e23c242 | 1327 | FRESULT change_bitmap ( |
glebiuskv | 0:2f0e1e23c242 | 1328 | FATFS* fs, /* Filesystem object */ |
glebiuskv | 0:2f0e1e23c242 | 1329 | DWORD clst, /* Cluster number to change from */ |
glebiuskv | 0:2f0e1e23c242 | 1330 | DWORD ncl, /* Number of clusters to be changed */ |
glebiuskv | 0:2f0e1e23c242 | 1331 | int bv /* bit value to be set (0 or 1) */ |
glebiuskv | 0:2f0e1e23c242 | 1332 | ) |
glebiuskv | 0:2f0e1e23c242 | 1333 | { |
glebiuskv | 0:2f0e1e23c242 | 1334 | BYTE bm; |
glebiuskv | 0:2f0e1e23c242 | 1335 | UINT i; |
glebiuskv | 0:2f0e1e23c242 | 1336 | DWORD sect; |
glebiuskv | 0:2f0e1e23c242 | 1337 | |
glebiuskv | 0:2f0e1e23c242 | 1338 | |
glebiuskv | 0:2f0e1e23c242 | 1339 | clst -= 2; /* The first bit corresponds to cluster #2 */ |
glebiuskv | 0:2f0e1e23c242 | 1340 | sect = fs->database + clst / 8 / SS(fs); /* Sector address (assuming bitmap is located top of the cluster heap) */ |
glebiuskv | 0:2f0e1e23c242 | 1341 | i = clst / 8 % SS(fs); /* Byte offset in the sector */ |
glebiuskv | 0:2f0e1e23c242 | 1342 | bm = 1 << (clst % 8); /* Bit mask in the byte */ |
glebiuskv | 0:2f0e1e23c242 | 1343 | for (;;) { |
glebiuskv | 0:2f0e1e23c242 | 1344 | if (move_window(fs, sect++) != FR_OK) return FR_DISK_ERR; |
glebiuskv | 0:2f0e1e23c242 | 1345 | do { |
glebiuskv | 0:2f0e1e23c242 | 1346 | do { |
glebiuskv | 0:2f0e1e23c242 | 1347 | if (bv == (int)((fs->win[i] & bm) != 0)) return FR_INT_ERR; /* Is the bit expected value? */ |
glebiuskv | 0:2f0e1e23c242 | 1348 | fs->win[i] ^= bm; /* Flip the bit */ |
glebiuskv | 0:2f0e1e23c242 | 1349 | fs->wflag = 1; |
glebiuskv | 0:2f0e1e23c242 | 1350 | if (--ncl == 0) return FR_OK; /* All bits processed? */ |
glebiuskv | 0:2f0e1e23c242 | 1351 | } while (bm <<= 1); /* Next bit */ |
glebiuskv | 0:2f0e1e23c242 | 1352 | bm = 1; |
glebiuskv | 0:2f0e1e23c242 | 1353 | } while (++i < SS(fs)); /* Next byte */ |
glebiuskv | 0:2f0e1e23c242 | 1354 | i = 0; |
glebiuskv | 0:2f0e1e23c242 | 1355 | } |
glebiuskv | 0:2f0e1e23c242 | 1356 | } |
glebiuskv | 0:2f0e1e23c242 | 1357 | |
glebiuskv | 0:2f0e1e23c242 | 1358 | |
glebiuskv | 0:2f0e1e23c242 | 1359 | /*---------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1360 | /* Fill the first fragment of the FAT chain */ |
glebiuskv | 0:2f0e1e23c242 | 1361 | /*---------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1362 | |
glebiuskv | 0:2f0e1e23c242 | 1363 | static |
glebiuskv | 0:2f0e1e23c242 | 1364 | FRESULT fill_first_frag ( |
glebiuskv | 0:2f0e1e23c242 | 1365 | FFOBJID* obj /* Pointer to the corresponding object */ |
glebiuskv | 0:2f0e1e23c242 | 1366 | ) |
glebiuskv | 0:2f0e1e23c242 | 1367 | { |
glebiuskv | 0:2f0e1e23c242 | 1368 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 1369 | DWORD cl, n; |
glebiuskv | 0:2f0e1e23c242 | 1370 | |
glebiuskv | 0:2f0e1e23c242 | 1371 | |
glebiuskv | 0:2f0e1e23c242 | 1372 | if (obj->stat == 3) { /* Has the object been changed 'fragmented' in this session? */ |
glebiuskv | 0:2f0e1e23c242 | 1373 | for (cl = obj->sclust, n = obj->n_cont; n; cl++, n--) { /* Create cluster chain on the FAT */ |
glebiuskv | 0:2f0e1e23c242 | 1374 | res = put_fat(obj->fs, cl, cl + 1); |
glebiuskv | 0:2f0e1e23c242 | 1375 | if (res != FR_OK) return res; |
glebiuskv | 0:2f0e1e23c242 | 1376 | } |
glebiuskv | 0:2f0e1e23c242 | 1377 | obj->stat = 0; /* Change status 'FAT chain is valid' */ |
glebiuskv | 0:2f0e1e23c242 | 1378 | } |
glebiuskv | 0:2f0e1e23c242 | 1379 | return FR_OK; |
glebiuskv | 0:2f0e1e23c242 | 1380 | } |
glebiuskv | 0:2f0e1e23c242 | 1381 | |
glebiuskv | 0:2f0e1e23c242 | 1382 | |
glebiuskv | 0:2f0e1e23c242 | 1383 | /*---------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1384 | /* Fill the last fragment of the FAT chain */ |
glebiuskv | 0:2f0e1e23c242 | 1385 | /*---------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1386 | |
glebiuskv | 0:2f0e1e23c242 | 1387 | static |
glebiuskv | 0:2f0e1e23c242 | 1388 | FRESULT fill_last_frag ( |
glebiuskv | 0:2f0e1e23c242 | 1389 | FFOBJID* obj, /* Pointer to the corresponding object */ |
glebiuskv | 0:2f0e1e23c242 | 1390 | DWORD lcl, /* Last cluster of the fragment */ |
glebiuskv | 0:2f0e1e23c242 | 1391 | DWORD term /* Value to set the last FAT entry */ |
glebiuskv | 0:2f0e1e23c242 | 1392 | ) |
glebiuskv | 0:2f0e1e23c242 | 1393 | { |
glebiuskv | 0:2f0e1e23c242 | 1394 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 1395 | |
glebiuskv | 0:2f0e1e23c242 | 1396 | |
glebiuskv | 0:2f0e1e23c242 | 1397 | while (obj->n_frag > 0) { /* Create the chain of last fragment */ |
glebiuskv | 0:2f0e1e23c242 | 1398 | res = put_fat(obj->fs, lcl - obj->n_frag + 1, (obj->n_frag > 1) ? lcl - obj->n_frag + 2 : term); |
glebiuskv | 0:2f0e1e23c242 | 1399 | if (res != FR_OK) return res; |
glebiuskv | 0:2f0e1e23c242 | 1400 | obj->n_frag--; |
glebiuskv | 0:2f0e1e23c242 | 1401 | } |
glebiuskv | 0:2f0e1e23c242 | 1402 | return FR_OK; |
glebiuskv | 0:2f0e1e23c242 | 1403 | } |
glebiuskv | 0:2f0e1e23c242 | 1404 | |
glebiuskv | 0:2f0e1e23c242 | 1405 | #endif /* FF_FS_EXFAT && !FF_FS_READONLY */ |
glebiuskv | 0:2f0e1e23c242 | 1406 | |
glebiuskv | 0:2f0e1e23c242 | 1407 | |
glebiuskv | 0:2f0e1e23c242 | 1408 | |
glebiuskv | 0:2f0e1e23c242 | 1409 | #if !FF_FS_READONLY |
glebiuskv | 0:2f0e1e23c242 | 1410 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1411 | /* FAT handling - Remove a cluster chain */ |
glebiuskv | 0:2f0e1e23c242 | 1412 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1413 | static |
glebiuskv | 0:2f0e1e23c242 | 1414 | FRESULT remove_chain ( /* FR_OK(0):succeeded, !=0:error */ |
glebiuskv | 0:2f0e1e23c242 | 1415 | FFOBJID* obj, /* Corresponding object */ |
glebiuskv | 0:2f0e1e23c242 | 1416 | DWORD clst, /* Cluster to remove a chain from */ |
glebiuskv | 0:2f0e1e23c242 | 1417 | DWORD pclst /* Previous cluster of clst (0:entire chain) */ |
glebiuskv | 0:2f0e1e23c242 | 1418 | ) |
glebiuskv | 0:2f0e1e23c242 | 1419 | { |
glebiuskv | 0:2f0e1e23c242 | 1420 | FRESULT res = FR_OK; |
glebiuskv | 0:2f0e1e23c242 | 1421 | DWORD nxt; |
glebiuskv | 0:2f0e1e23c242 | 1422 | FATFS *fs = obj->fs; |
glebiuskv | 0:2f0e1e23c242 | 1423 | #if FF_FS_EXFAT || FF_USE_TRIM |
glebiuskv | 0:2f0e1e23c242 | 1424 | DWORD scl = clst, ecl = clst; |
glebiuskv | 0:2f0e1e23c242 | 1425 | #endif |
glebiuskv | 0:2f0e1e23c242 | 1426 | #if FF_USE_TRIM |
glebiuskv | 0:2f0e1e23c242 | 1427 | DWORD rt[2]; |
glebiuskv | 0:2f0e1e23c242 | 1428 | #endif |
glebiuskv | 0:2f0e1e23c242 | 1429 | |
glebiuskv | 0:2f0e1e23c242 | 1430 | if (clst < 2 || clst >= fs->n_fatent) return FR_INT_ERR; /* Check if in valid range */ |
glebiuskv | 0:2f0e1e23c242 | 1431 | |
glebiuskv | 0:2f0e1e23c242 | 1432 | /* Mark the previous cluster 'EOC' on the FAT if it exists */ |
glebiuskv | 0:2f0e1e23c242 | 1433 | if (pclst != 0 && (!FF_FS_EXFAT || fs->fs_type != FS_EXFAT || obj->stat != 2)) { |
glebiuskv | 0:2f0e1e23c242 | 1434 | res = put_fat(fs, pclst, 0xFFFFFFFF); |
glebiuskv | 0:2f0e1e23c242 | 1435 | if (res != FR_OK) return res; |
glebiuskv | 0:2f0e1e23c242 | 1436 | } |
glebiuskv | 0:2f0e1e23c242 | 1437 | |
glebiuskv | 0:2f0e1e23c242 | 1438 | /* Remove the chain */ |
glebiuskv | 0:2f0e1e23c242 | 1439 | do { |
glebiuskv | 0:2f0e1e23c242 | 1440 | nxt = get_fat(obj, clst); /* Get cluster status */ |
glebiuskv | 0:2f0e1e23c242 | 1441 | if (nxt == 0) break; /* Empty cluster? */ |
glebiuskv | 0:2f0e1e23c242 | 1442 | if (nxt == 1) return FR_INT_ERR; /* Internal error? */ |
glebiuskv | 0:2f0e1e23c242 | 1443 | if (nxt == 0xFFFFFFFF) return FR_DISK_ERR; /* Disk error? */ |
glebiuskv | 0:2f0e1e23c242 | 1444 | if (!FF_FS_EXFAT || fs->fs_type != FS_EXFAT) { |
glebiuskv | 0:2f0e1e23c242 | 1445 | res = put_fat(fs, clst, 0); /* Mark the cluster 'free' on the FAT */ |
glebiuskv | 0:2f0e1e23c242 | 1446 | if (res != FR_OK) return res; |
glebiuskv | 0:2f0e1e23c242 | 1447 | } |
glebiuskv | 0:2f0e1e23c242 | 1448 | if (fs->free_clst < fs->n_fatent - 2) { /* Update FSINFO */ |
glebiuskv | 0:2f0e1e23c242 | 1449 | fs->free_clst++; |
glebiuskv | 0:2f0e1e23c242 | 1450 | fs->fsi_flag |= 1; |
glebiuskv | 0:2f0e1e23c242 | 1451 | } |
glebiuskv | 0:2f0e1e23c242 | 1452 | #if FF_FS_EXFAT || FF_USE_TRIM |
glebiuskv | 0:2f0e1e23c242 | 1453 | if (ecl + 1 == nxt) { /* Is next cluster contiguous? */ |
glebiuskv | 0:2f0e1e23c242 | 1454 | ecl = nxt; |
glebiuskv | 0:2f0e1e23c242 | 1455 | } else { /* End of contiguous cluster block */ |
glebiuskv | 0:2f0e1e23c242 | 1456 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 1457 | if (fs->fs_type == FS_EXFAT) { |
glebiuskv | 0:2f0e1e23c242 | 1458 | res = change_bitmap(fs, scl, ecl - scl + 1, 0); /* Mark the cluster block 'free' on the bitmap */ |
glebiuskv | 0:2f0e1e23c242 | 1459 | if (res != FR_OK) return res; |
glebiuskv | 0:2f0e1e23c242 | 1460 | } |
glebiuskv | 0:2f0e1e23c242 | 1461 | #endif |
glebiuskv | 0:2f0e1e23c242 | 1462 | #if FF_USE_TRIM |
glebiuskv | 0:2f0e1e23c242 | 1463 | rt[0] = clst2sect(fs, scl); /* Start of data area freed */ |
glebiuskv | 0:2f0e1e23c242 | 1464 | rt[1] = clst2sect(fs, ecl) + fs->csize - 1; /* End of data area freed */ |
glebiuskv | 0:2f0e1e23c242 | 1465 | disk_ioctl(fs->pdrv, CTRL_TRIM, rt); /* Inform device the data in the block is no longer needed */ |
glebiuskv | 0:2f0e1e23c242 | 1466 | #endif |
glebiuskv | 0:2f0e1e23c242 | 1467 | scl = ecl = nxt; |
glebiuskv | 0:2f0e1e23c242 | 1468 | } |
glebiuskv | 0:2f0e1e23c242 | 1469 | #endif |
glebiuskv | 0:2f0e1e23c242 | 1470 | clst = nxt; /* Next cluster */ |
glebiuskv | 0:2f0e1e23c242 | 1471 | } while (clst < fs->n_fatent); /* Repeat while not the last link */ |
glebiuskv | 0:2f0e1e23c242 | 1472 | |
glebiuskv | 0:2f0e1e23c242 | 1473 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 1474 | /* Some post processes for chain status */ |
glebiuskv | 0:2f0e1e23c242 | 1475 | if (fs->fs_type == FS_EXFAT) { |
glebiuskv | 0:2f0e1e23c242 | 1476 | if (pclst == 0) { /* Has the entire chain been removed? */ |
glebiuskv | 0:2f0e1e23c242 | 1477 | obj->stat = 0; /* Change the chain status 'initial' */ |
glebiuskv | 0:2f0e1e23c242 | 1478 | } else { |
glebiuskv | 0:2f0e1e23c242 | 1479 | if (obj->stat == 0) { /* Is it a fragmented chain from the beginning of this session? */ |
glebiuskv | 0:2f0e1e23c242 | 1480 | clst = obj->sclust; /* Follow the chain to check if it gets contiguous */ |
glebiuskv | 0:2f0e1e23c242 | 1481 | while (clst != pclst) { |
glebiuskv | 0:2f0e1e23c242 | 1482 | nxt = get_fat(obj, clst); |
glebiuskv | 0:2f0e1e23c242 | 1483 | if (nxt < 2) return FR_INT_ERR; |
glebiuskv | 0:2f0e1e23c242 | 1484 | if (nxt == 0xFFFFFFFF) return FR_DISK_ERR; |
glebiuskv | 0:2f0e1e23c242 | 1485 | if (nxt != clst + 1) break; /* Not contiguous? */ |
glebiuskv | 0:2f0e1e23c242 | 1486 | clst++; |
glebiuskv | 0:2f0e1e23c242 | 1487 | } |
glebiuskv | 0:2f0e1e23c242 | 1488 | if (clst == pclst) { /* Has the chain got contiguous again? */ |
glebiuskv | 0:2f0e1e23c242 | 1489 | obj->stat = 2; /* Change the chain status 'contiguous' */ |
glebiuskv | 0:2f0e1e23c242 | 1490 | } |
glebiuskv | 0:2f0e1e23c242 | 1491 | } else { |
glebiuskv | 0:2f0e1e23c242 | 1492 | if (obj->stat == 3 && pclst >= obj->sclust && pclst <= obj->sclust + obj->n_cont) { /* Was the chain fragmented in this session and got contiguous again? */ |
glebiuskv | 0:2f0e1e23c242 | 1493 | obj->stat = 2; /* Change the chain status 'contiguous' */ |
glebiuskv | 0:2f0e1e23c242 | 1494 | } |
glebiuskv | 0:2f0e1e23c242 | 1495 | } |
glebiuskv | 0:2f0e1e23c242 | 1496 | } |
glebiuskv | 0:2f0e1e23c242 | 1497 | } |
glebiuskv | 0:2f0e1e23c242 | 1498 | #endif |
glebiuskv | 0:2f0e1e23c242 | 1499 | return FR_OK; |
glebiuskv | 0:2f0e1e23c242 | 1500 | } |
glebiuskv | 0:2f0e1e23c242 | 1501 | |
glebiuskv | 0:2f0e1e23c242 | 1502 | |
glebiuskv | 0:2f0e1e23c242 | 1503 | |
glebiuskv | 0:2f0e1e23c242 | 1504 | |
glebiuskv | 0:2f0e1e23c242 | 1505 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1506 | /* FAT handling - Stretch a chain or Create a new chain */ |
glebiuskv | 0:2f0e1e23c242 | 1507 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1508 | static |
glebiuskv | 0:2f0e1e23c242 | 1509 | DWORD create_chain ( /* 0:No free cluster, 1:Internal error, 0xFFFFFFFF:Disk error, >=2:New cluster# */ |
glebiuskv | 0:2f0e1e23c242 | 1510 | FFOBJID* obj, /* Corresponding object */ |
glebiuskv | 0:2f0e1e23c242 | 1511 | DWORD clst /* Cluster# to stretch, 0:Create a new chain */ |
glebiuskv | 0:2f0e1e23c242 | 1512 | ) |
glebiuskv | 0:2f0e1e23c242 | 1513 | { |
glebiuskv | 0:2f0e1e23c242 | 1514 | DWORD cs, ncl, scl; |
glebiuskv | 0:2f0e1e23c242 | 1515 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 1516 | FATFS *fs = obj->fs; |
glebiuskv | 0:2f0e1e23c242 | 1517 | |
glebiuskv | 0:2f0e1e23c242 | 1518 | |
glebiuskv | 0:2f0e1e23c242 | 1519 | if (clst == 0) { /* Create a new chain */ |
glebiuskv | 0:2f0e1e23c242 | 1520 | scl = fs->last_clst; /* Suggested cluster to start to find */ |
glebiuskv | 0:2f0e1e23c242 | 1521 | if (scl == 0 || scl >= fs->n_fatent) scl = 1; |
glebiuskv | 0:2f0e1e23c242 | 1522 | } |
glebiuskv | 0:2f0e1e23c242 | 1523 | else { /* Stretch a chain */ |
glebiuskv | 0:2f0e1e23c242 | 1524 | cs = get_fat(obj, clst); /* Check the cluster status */ |
glebiuskv | 0:2f0e1e23c242 | 1525 | if (cs < 2) return 1; /* Test for insanity */ |
glebiuskv | 0:2f0e1e23c242 | 1526 | if (cs == 0xFFFFFFFF) return cs; /* Test for disk error */ |
glebiuskv | 0:2f0e1e23c242 | 1527 | if (cs < fs->n_fatent) return cs; /* It is already followed by next cluster */ |
glebiuskv | 0:2f0e1e23c242 | 1528 | scl = clst; /* Cluster to start to find */ |
glebiuskv | 0:2f0e1e23c242 | 1529 | } |
glebiuskv | 0:2f0e1e23c242 | 1530 | if (fs->free_clst == 0) return 0; /* No free cluster */ |
glebiuskv | 0:2f0e1e23c242 | 1531 | |
glebiuskv | 0:2f0e1e23c242 | 1532 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 1533 | if (fs->fs_type == FS_EXFAT) { /* On the exFAT volume */ |
glebiuskv | 0:2f0e1e23c242 | 1534 | ncl = find_bitmap(fs, scl, 1); /* Find a free cluster */ |
glebiuskv | 0:2f0e1e23c242 | 1535 | if (ncl == 0 || ncl == 0xFFFFFFFF) return ncl; /* No free cluster or hard error? */ |
glebiuskv | 0:2f0e1e23c242 | 1536 | res = change_bitmap(fs, ncl, 1, 1); /* Mark the cluster 'in use' */ |
glebiuskv | 0:2f0e1e23c242 | 1537 | if (res == FR_INT_ERR) return 1; |
glebiuskv | 0:2f0e1e23c242 | 1538 | if (res == FR_DISK_ERR) return 0xFFFFFFFF; |
glebiuskv | 0:2f0e1e23c242 | 1539 | if (clst == 0) { /* Is it a new chain? */ |
glebiuskv | 0:2f0e1e23c242 | 1540 | obj->stat = 2; /* Set status 'contiguous' */ |
glebiuskv | 0:2f0e1e23c242 | 1541 | } else { /* It is a stretched chain */ |
glebiuskv | 0:2f0e1e23c242 | 1542 | if (obj->stat == 2 && ncl != scl + 1) { /* Is the chain got fragmented? */ |
glebiuskv | 0:2f0e1e23c242 | 1543 | obj->n_cont = scl - obj->sclust; /* Set size of the contiguous part */ |
glebiuskv | 0:2f0e1e23c242 | 1544 | obj->stat = 3; /* Change status 'just fragmented' */ |
glebiuskv | 0:2f0e1e23c242 | 1545 | } |
glebiuskv | 0:2f0e1e23c242 | 1546 | } |
glebiuskv | 0:2f0e1e23c242 | 1547 | if (obj->stat != 2) { /* Is the file non-contiguous? */ |
glebiuskv | 0:2f0e1e23c242 | 1548 | if (ncl == clst + 1) { /* Is the cluster next to previous one? */ |
glebiuskv | 0:2f0e1e23c242 | 1549 | obj->n_frag = obj->n_frag ? obj->n_frag + 1 : 2; /* Increment size of last framgent */ |
glebiuskv | 0:2f0e1e23c242 | 1550 | } else { /* New fragment */ |
glebiuskv | 0:2f0e1e23c242 | 1551 | if (obj->n_frag == 0) obj->n_frag = 1; |
glebiuskv | 0:2f0e1e23c242 | 1552 | res = fill_last_frag(obj, clst, ncl); /* Fill last fragment on the FAT and link it to new one */ |
glebiuskv | 0:2f0e1e23c242 | 1553 | if (res == FR_OK) obj->n_frag = 1; |
glebiuskv | 0:2f0e1e23c242 | 1554 | } |
glebiuskv | 0:2f0e1e23c242 | 1555 | } |
glebiuskv | 0:2f0e1e23c242 | 1556 | } else |
glebiuskv | 0:2f0e1e23c242 | 1557 | #endif |
glebiuskv | 0:2f0e1e23c242 | 1558 | { /* On the FAT/FAT32 volume */ |
glebiuskv | 0:2f0e1e23c242 | 1559 | ncl = 0; |
glebiuskv | 0:2f0e1e23c242 | 1560 | if (scl == clst) { /* Stretching an existing chain? */ |
glebiuskv | 0:2f0e1e23c242 | 1561 | ncl = scl + 1; /* Test if next cluster is free */ |
glebiuskv | 0:2f0e1e23c242 | 1562 | if (ncl >= fs->n_fatent) ncl = 2; |
glebiuskv | 0:2f0e1e23c242 | 1563 | cs = get_fat(obj, ncl); /* Get next cluster status */ |
glebiuskv | 0:2f0e1e23c242 | 1564 | if (cs == 1 || cs == 0xFFFFFFFF) return cs; /* Test for error */ |
glebiuskv | 0:2f0e1e23c242 | 1565 | if (cs != 0) { /* Not free? */ |
glebiuskv | 0:2f0e1e23c242 | 1566 | cs = fs->last_clst; /* Start at suggested cluster if it is valid */ |
glebiuskv | 0:2f0e1e23c242 | 1567 | if (cs >= 2 && cs < fs->n_fatent) scl = cs; |
glebiuskv | 0:2f0e1e23c242 | 1568 | ncl = 0; |
glebiuskv | 0:2f0e1e23c242 | 1569 | } |
glebiuskv | 0:2f0e1e23c242 | 1570 | } |
glebiuskv | 0:2f0e1e23c242 | 1571 | if (ncl == 0) { /* The new cluster cannot be contiguous and find another fragment */ |
glebiuskv | 0:2f0e1e23c242 | 1572 | ncl = scl; /* Start cluster */ |
glebiuskv | 0:2f0e1e23c242 | 1573 | for (;;) { |
glebiuskv | 0:2f0e1e23c242 | 1574 | ncl++; /* Next cluster */ |
glebiuskv | 0:2f0e1e23c242 | 1575 | if (ncl >= fs->n_fatent) { /* Check wrap-around */ |
glebiuskv | 0:2f0e1e23c242 | 1576 | ncl = 2; |
glebiuskv | 0:2f0e1e23c242 | 1577 | if (ncl > scl) return 0; /* No free cluster found? */ |
glebiuskv | 0:2f0e1e23c242 | 1578 | } |
glebiuskv | 0:2f0e1e23c242 | 1579 | cs = get_fat(obj, ncl); /* Get the cluster status */ |
glebiuskv | 0:2f0e1e23c242 | 1580 | if (cs == 0) break; /* Found a free cluster? */ |
glebiuskv | 0:2f0e1e23c242 | 1581 | if (cs == 1 || cs == 0xFFFFFFFF) return cs; /* Test for error */ |
glebiuskv | 0:2f0e1e23c242 | 1582 | if (ncl == scl) return 0; /* No free cluster found? */ |
glebiuskv | 0:2f0e1e23c242 | 1583 | } |
glebiuskv | 0:2f0e1e23c242 | 1584 | } |
glebiuskv | 0:2f0e1e23c242 | 1585 | res = put_fat(fs, ncl, 0xFFFFFFFF); /* Mark the new cluster 'EOC' */ |
glebiuskv | 0:2f0e1e23c242 | 1586 | if (res == FR_OK && clst != 0) { |
glebiuskv | 0:2f0e1e23c242 | 1587 | res = put_fat(fs, clst, ncl); /* Link it from the previous one if needed */ |
glebiuskv | 0:2f0e1e23c242 | 1588 | } |
glebiuskv | 0:2f0e1e23c242 | 1589 | } |
glebiuskv | 0:2f0e1e23c242 | 1590 | |
glebiuskv | 0:2f0e1e23c242 | 1591 | if (res == FR_OK) { /* Update FSINFO if function succeeded. */ |
glebiuskv | 0:2f0e1e23c242 | 1592 | fs->last_clst = ncl; |
glebiuskv | 0:2f0e1e23c242 | 1593 | if (fs->free_clst <= fs->n_fatent - 2) fs->free_clst--; |
glebiuskv | 0:2f0e1e23c242 | 1594 | fs->fsi_flag |= 1; |
glebiuskv | 0:2f0e1e23c242 | 1595 | } else { |
glebiuskv | 0:2f0e1e23c242 | 1596 | ncl = (res == FR_DISK_ERR) ? 0xFFFFFFFF : 1; /* Failed. Generate error status */ |
glebiuskv | 0:2f0e1e23c242 | 1597 | } |
glebiuskv | 0:2f0e1e23c242 | 1598 | |
glebiuskv | 0:2f0e1e23c242 | 1599 | return ncl; /* Return new cluster number or error status */ |
glebiuskv | 0:2f0e1e23c242 | 1600 | } |
glebiuskv | 0:2f0e1e23c242 | 1601 | |
glebiuskv | 0:2f0e1e23c242 | 1602 | #endif /* !FF_FS_READONLY */ |
glebiuskv | 0:2f0e1e23c242 | 1603 | |
glebiuskv | 0:2f0e1e23c242 | 1604 | |
glebiuskv | 0:2f0e1e23c242 | 1605 | |
glebiuskv | 0:2f0e1e23c242 | 1606 | |
glebiuskv | 0:2f0e1e23c242 | 1607 | #if FF_USE_FASTSEEK |
glebiuskv | 0:2f0e1e23c242 | 1608 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1609 | /* FAT handling - Convert offset into cluster with link map table */ |
glebiuskv | 0:2f0e1e23c242 | 1610 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1611 | |
glebiuskv | 0:2f0e1e23c242 | 1612 | static |
glebiuskv | 0:2f0e1e23c242 | 1613 | DWORD clmt_clust ( /* <2:Error, >=2:Cluster number */ |
glebiuskv | 0:2f0e1e23c242 | 1614 | FIL* fp, /* Pointer to the file object */ |
glebiuskv | 0:2f0e1e23c242 | 1615 | FSIZE_t ofs /* File offset to be converted to cluster# */ |
glebiuskv | 0:2f0e1e23c242 | 1616 | ) |
glebiuskv | 0:2f0e1e23c242 | 1617 | { |
glebiuskv | 0:2f0e1e23c242 | 1618 | DWORD cl, ncl, *tbl; |
glebiuskv | 0:2f0e1e23c242 | 1619 | FATFS *fs = fp->obj.fs; |
glebiuskv | 0:2f0e1e23c242 | 1620 | |
glebiuskv | 0:2f0e1e23c242 | 1621 | |
glebiuskv | 0:2f0e1e23c242 | 1622 | tbl = fp->cltbl + 1; /* Top of CLMT */ |
glebiuskv | 0:2f0e1e23c242 | 1623 | cl = (DWORD)(ofs / SS(fs) / fs->csize); /* Cluster order from top of the file */ |
glebiuskv | 0:2f0e1e23c242 | 1624 | for (;;) { |
glebiuskv | 0:2f0e1e23c242 | 1625 | ncl = *tbl++; /* Number of cluters in the fragment */ |
glebiuskv | 0:2f0e1e23c242 | 1626 | if (ncl == 0) return 0; /* End of table? (error) */ |
glebiuskv | 0:2f0e1e23c242 | 1627 | if (cl < ncl) break; /* In this fragment? */ |
glebiuskv | 0:2f0e1e23c242 | 1628 | cl -= ncl; tbl++; /* Next fragment */ |
glebiuskv | 0:2f0e1e23c242 | 1629 | } |
glebiuskv | 0:2f0e1e23c242 | 1630 | return cl + *tbl; /* Return the cluster number */ |
glebiuskv | 0:2f0e1e23c242 | 1631 | } |
glebiuskv | 0:2f0e1e23c242 | 1632 | |
glebiuskv | 0:2f0e1e23c242 | 1633 | #endif /* FF_USE_FASTSEEK */ |
glebiuskv | 0:2f0e1e23c242 | 1634 | |
glebiuskv | 0:2f0e1e23c242 | 1635 | |
glebiuskv | 0:2f0e1e23c242 | 1636 | |
glebiuskv | 0:2f0e1e23c242 | 1637 | |
glebiuskv | 0:2f0e1e23c242 | 1638 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1639 | /* Directory handling - Fill a cluster with zeros */ |
glebiuskv | 0:2f0e1e23c242 | 1640 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1641 | |
glebiuskv | 0:2f0e1e23c242 | 1642 | #if !FF_FS_READONLY |
glebiuskv | 0:2f0e1e23c242 | 1643 | static |
glebiuskv | 0:2f0e1e23c242 | 1644 | FRESULT dir_clear ( /* Returns FR_OK or FR_DISK_ERR */ |
glebiuskv | 0:2f0e1e23c242 | 1645 | FATFS *fs, /* Filesystem object */ |
glebiuskv | 0:2f0e1e23c242 | 1646 | DWORD clst /* Directory table to clear */ |
glebiuskv | 0:2f0e1e23c242 | 1647 | ) |
glebiuskv | 0:2f0e1e23c242 | 1648 | { |
glebiuskv | 0:2f0e1e23c242 | 1649 | DWORD sect; |
glebiuskv | 0:2f0e1e23c242 | 1650 | UINT n, szb; |
glebiuskv | 0:2f0e1e23c242 | 1651 | BYTE *ibuf; |
glebiuskv | 0:2f0e1e23c242 | 1652 | |
glebiuskv | 0:2f0e1e23c242 | 1653 | |
glebiuskv | 0:2f0e1e23c242 | 1654 | if (sync_window(fs) != FR_OK) return FR_DISK_ERR; /* Flush disk access window */ |
glebiuskv | 0:2f0e1e23c242 | 1655 | sect = clst2sect(fs, clst); /* Top of the cluster */ |
glebiuskv | 0:2f0e1e23c242 | 1656 | fs->winsect = sect; /* Set window to top of the cluster */ |
glebiuskv | 0:2f0e1e23c242 | 1657 | mem_set(fs->win, 0, SS(fs)); /* Clear window buffer */ |
glebiuskv | 0:2f0e1e23c242 | 1658 | #if FF_USE_LFN == 3 /* Quick table clear by using multi-secter write */ |
glebiuskv | 0:2f0e1e23c242 | 1659 | /* Allocate a temporary buffer */ |
glebiuskv | 0:2f0e1e23c242 | 1660 | for (szb = ((DWORD)fs->csize * SS(fs) >= MAX_MALLOC) ? MAX_MALLOC : fs->csize * SS(fs); szb > SS(fs) && !(ibuf = (BYTE *)ff_memalloc(szb)); szb /= 2) ; |
glebiuskv | 0:2f0e1e23c242 | 1661 | if (szb > SS(fs)) { /* Buffer allocated? */ |
glebiuskv | 0:2f0e1e23c242 | 1662 | mem_set(ibuf, 0, szb); |
glebiuskv | 0:2f0e1e23c242 | 1663 | szb /= SS(fs); /* Bytes -> Sectors */ |
glebiuskv | 0:2f0e1e23c242 | 1664 | for (n = 0; n < fs->csize && disk_write(fs->pdrv, ibuf, sect + n, szb) == RES_OK; n += szb) ; /* Fill the cluster with 0 */ |
glebiuskv | 0:2f0e1e23c242 | 1665 | ff_memfree(ibuf); |
glebiuskv | 0:2f0e1e23c242 | 1666 | } else |
glebiuskv | 0:2f0e1e23c242 | 1667 | #endif |
glebiuskv | 0:2f0e1e23c242 | 1668 | { |
glebiuskv | 0:2f0e1e23c242 | 1669 | ibuf = fs->win; szb = 1; /* Use window buffer (single-sector writes may take a time) */ |
glebiuskv | 0:2f0e1e23c242 | 1670 | for (n = 0; n < fs->csize && disk_write(fs->pdrv, ibuf, sect + n, szb) == RES_OK; n += szb) ; /* Fill the cluster with 0 */ |
glebiuskv | 0:2f0e1e23c242 | 1671 | } |
glebiuskv | 0:2f0e1e23c242 | 1672 | return (n == fs->csize) ? FR_OK : FR_DISK_ERR; |
glebiuskv | 0:2f0e1e23c242 | 1673 | } |
glebiuskv | 0:2f0e1e23c242 | 1674 | #endif /* !FF_FS_READONLY */ |
glebiuskv | 0:2f0e1e23c242 | 1675 | |
glebiuskv | 0:2f0e1e23c242 | 1676 | |
glebiuskv | 0:2f0e1e23c242 | 1677 | |
glebiuskv | 0:2f0e1e23c242 | 1678 | |
glebiuskv | 0:2f0e1e23c242 | 1679 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1680 | /* Directory handling - Set directory index */ |
glebiuskv | 0:2f0e1e23c242 | 1681 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1682 | |
glebiuskv | 0:2f0e1e23c242 | 1683 | static |
glebiuskv | 0:2f0e1e23c242 | 1684 | FRESULT dir_sdi ( /* FR_OK(0):succeeded, !=0:error */ |
glebiuskv | 0:2f0e1e23c242 | 1685 | FATFS_DIR* dp, /* Pointer to directory object */ |
glebiuskv | 0:2f0e1e23c242 | 1686 | DWORD ofs /* Offset of directory table */ |
glebiuskv | 0:2f0e1e23c242 | 1687 | ) |
glebiuskv | 0:2f0e1e23c242 | 1688 | { |
glebiuskv | 0:2f0e1e23c242 | 1689 | DWORD csz, clst; |
glebiuskv | 0:2f0e1e23c242 | 1690 | FATFS *fs = dp->obj.fs; |
glebiuskv | 0:2f0e1e23c242 | 1691 | |
glebiuskv | 0:2f0e1e23c242 | 1692 | |
glebiuskv | 0:2f0e1e23c242 | 1693 | if (ofs >= (DWORD)((FF_FS_EXFAT && fs->fs_type == FS_EXFAT) ? MAX_DIR_EX : MAX_DIR) || ofs % SZDIRE) { /* Check range of offset and alignment */ |
glebiuskv | 0:2f0e1e23c242 | 1694 | return FR_INT_ERR; |
glebiuskv | 0:2f0e1e23c242 | 1695 | } |
glebiuskv | 0:2f0e1e23c242 | 1696 | dp->dptr = ofs; /* Set current offset */ |
glebiuskv | 0:2f0e1e23c242 | 1697 | clst = dp->obj.sclust; /* Table start cluster (0:root) */ |
glebiuskv | 0:2f0e1e23c242 | 1698 | if (clst == 0 && fs->fs_type >= FS_FAT32) { /* Replace cluster# 0 with root cluster# */ |
glebiuskv | 0:2f0e1e23c242 | 1699 | clst = fs->dirbase; |
glebiuskv | 0:2f0e1e23c242 | 1700 | if (FF_FS_EXFAT) dp->obj.stat = 0; /* exFAT: Root dir has an FAT chain */ |
glebiuskv | 0:2f0e1e23c242 | 1701 | } |
glebiuskv | 0:2f0e1e23c242 | 1702 | |
glebiuskv | 0:2f0e1e23c242 | 1703 | if (clst == 0) { /* Static table (root-directory on the FAT volume) */ |
glebiuskv | 0:2f0e1e23c242 | 1704 | if (ofs / SZDIRE >= fs->n_rootdir) return FR_INT_ERR; /* Is index out of range? */ |
glebiuskv | 0:2f0e1e23c242 | 1705 | dp->sect = fs->dirbase; |
glebiuskv | 0:2f0e1e23c242 | 1706 | |
glebiuskv | 0:2f0e1e23c242 | 1707 | } else { /* Dynamic table (sub-directory or root-directory on the FAT32/exFAT volume) */ |
glebiuskv | 0:2f0e1e23c242 | 1708 | csz = (DWORD)fs->csize * SS(fs); /* Bytes per cluster */ |
glebiuskv | 0:2f0e1e23c242 | 1709 | while (ofs >= csz) { /* Follow cluster chain */ |
glebiuskv | 0:2f0e1e23c242 | 1710 | clst = get_fat(&dp->obj, clst); /* Get next cluster */ |
glebiuskv | 0:2f0e1e23c242 | 1711 | if (clst == 0xFFFFFFFF) return FR_DISK_ERR; /* Disk error */ |
glebiuskv | 0:2f0e1e23c242 | 1712 | if (clst < 2 || clst >= fs->n_fatent) return FR_INT_ERR; /* Reached to end of table or internal error */ |
glebiuskv | 0:2f0e1e23c242 | 1713 | ofs -= csz; |
glebiuskv | 0:2f0e1e23c242 | 1714 | } |
glebiuskv | 0:2f0e1e23c242 | 1715 | dp->sect = clst2sect(fs, clst); |
glebiuskv | 0:2f0e1e23c242 | 1716 | } |
glebiuskv | 0:2f0e1e23c242 | 1717 | dp->clust = clst; /* Current cluster# */ |
glebiuskv | 0:2f0e1e23c242 | 1718 | if (dp->sect == 0) return FR_INT_ERR; |
glebiuskv | 0:2f0e1e23c242 | 1719 | dp->sect += ofs / SS(fs); /* Sector# of the directory entry */ |
glebiuskv | 0:2f0e1e23c242 | 1720 | dp->dir = fs->win + (ofs % SS(fs)); /* Pointer to the entry in the win[] */ |
glebiuskv | 0:2f0e1e23c242 | 1721 | |
glebiuskv | 0:2f0e1e23c242 | 1722 | return FR_OK; |
glebiuskv | 0:2f0e1e23c242 | 1723 | } |
glebiuskv | 0:2f0e1e23c242 | 1724 | |
glebiuskv | 0:2f0e1e23c242 | 1725 | |
glebiuskv | 0:2f0e1e23c242 | 1726 | |
glebiuskv | 0:2f0e1e23c242 | 1727 | |
glebiuskv | 0:2f0e1e23c242 | 1728 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1729 | /* Directory handling - Move directory table index next */ |
glebiuskv | 0:2f0e1e23c242 | 1730 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1731 | |
glebiuskv | 0:2f0e1e23c242 | 1732 | static |
glebiuskv | 0:2f0e1e23c242 | 1733 | FRESULT dir_next ( /* FR_OK(0):succeeded, FR_NO_FILE:End of table, FR_DENIED:Could not stretch */ |
glebiuskv | 0:2f0e1e23c242 | 1734 | FATFS_DIR* dp, /* Pointer to the directory object */ |
glebiuskv | 0:2f0e1e23c242 | 1735 | int stretch /* 0: Do not stretch table, 1: Stretch table if needed */ |
glebiuskv | 0:2f0e1e23c242 | 1736 | ) |
glebiuskv | 0:2f0e1e23c242 | 1737 | { |
glebiuskv | 0:2f0e1e23c242 | 1738 | DWORD ofs, clst; |
glebiuskv | 0:2f0e1e23c242 | 1739 | FATFS *fs = dp->obj.fs; |
glebiuskv | 0:2f0e1e23c242 | 1740 | |
glebiuskv | 0:2f0e1e23c242 | 1741 | |
glebiuskv | 0:2f0e1e23c242 | 1742 | ofs = dp->dptr + SZDIRE; /* Next entry */ |
glebiuskv | 0:2f0e1e23c242 | 1743 | if (dp->sect == 0 || ofs >= (DWORD)((FF_FS_EXFAT && fs->fs_type == FS_EXFAT) ? MAX_DIR_EX : MAX_DIR)) return FR_NO_FILE; /* Report EOT when offset has reached max value */ |
glebiuskv | 0:2f0e1e23c242 | 1744 | |
glebiuskv | 0:2f0e1e23c242 | 1745 | if (ofs % SS(fs) == 0) { /* Sector changed? */ |
glebiuskv | 0:2f0e1e23c242 | 1746 | dp->sect++; /* Next sector */ |
glebiuskv | 0:2f0e1e23c242 | 1747 | |
glebiuskv | 0:2f0e1e23c242 | 1748 | if (dp->clust == 0) { /* Static table */ |
glebiuskv | 0:2f0e1e23c242 | 1749 | if (ofs / SZDIRE >= fs->n_rootdir) { /* Report EOT if it reached end of static table */ |
glebiuskv | 0:2f0e1e23c242 | 1750 | dp->sect = 0; return FR_NO_FILE; |
glebiuskv | 0:2f0e1e23c242 | 1751 | } |
glebiuskv | 0:2f0e1e23c242 | 1752 | } |
glebiuskv | 0:2f0e1e23c242 | 1753 | else { /* Dynamic table */ |
glebiuskv | 0:2f0e1e23c242 | 1754 | if ((ofs / SS(fs) & (fs->csize - 1)) == 0) { /* Cluster changed? */ |
glebiuskv | 0:2f0e1e23c242 | 1755 | clst = get_fat(&dp->obj, dp->clust); /* Get next cluster */ |
glebiuskv | 0:2f0e1e23c242 | 1756 | if (clst <= 1) return FR_INT_ERR; /* Internal error */ |
glebiuskv | 0:2f0e1e23c242 | 1757 | if (clst == 0xFFFFFFFF) return FR_DISK_ERR; /* Disk error */ |
glebiuskv | 0:2f0e1e23c242 | 1758 | if (clst >= fs->n_fatent) { /* It reached end of dynamic table */ |
glebiuskv | 0:2f0e1e23c242 | 1759 | #if !FF_FS_READONLY |
glebiuskv | 0:2f0e1e23c242 | 1760 | if (!stretch) { /* If no stretch, report EOT */ |
glebiuskv | 0:2f0e1e23c242 | 1761 | dp->sect = 0; return FR_NO_FILE; |
glebiuskv | 0:2f0e1e23c242 | 1762 | } |
glebiuskv | 0:2f0e1e23c242 | 1763 | clst = create_chain(&dp->obj, dp->clust); /* Allocate a cluster */ |
glebiuskv | 0:2f0e1e23c242 | 1764 | if (clst == 0) return FR_DENIED; /* No free cluster */ |
glebiuskv | 0:2f0e1e23c242 | 1765 | if (clst == 1) return FR_INT_ERR; /* Internal error */ |
glebiuskv | 0:2f0e1e23c242 | 1766 | if (clst == 0xFFFFFFFF) return FR_DISK_ERR; /* Disk error */ |
glebiuskv | 0:2f0e1e23c242 | 1767 | if (dir_clear(fs, clst) != FR_OK) return FR_DISK_ERR; /* Clean up the stretched table */ |
glebiuskv | 0:2f0e1e23c242 | 1768 | if (FF_FS_EXFAT) dp->obj.stat |= 4; /* exFAT: The directory has been stretched */ |
glebiuskv | 0:2f0e1e23c242 | 1769 | #else |
glebiuskv | 0:2f0e1e23c242 | 1770 | if (!stretch) dp->sect = 0; /* (this line is to suppress compiler warning) */ |
glebiuskv | 0:2f0e1e23c242 | 1771 | dp->sect = 0; return FR_NO_FILE; /* Report EOT */ |
glebiuskv | 0:2f0e1e23c242 | 1772 | #endif |
glebiuskv | 0:2f0e1e23c242 | 1773 | } |
glebiuskv | 0:2f0e1e23c242 | 1774 | dp->clust = clst; /* Initialize data for new cluster */ |
glebiuskv | 0:2f0e1e23c242 | 1775 | dp->sect = clst2sect(fs, clst); |
glebiuskv | 0:2f0e1e23c242 | 1776 | } |
glebiuskv | 0:2f0e1e23c242 | 1777 | } |
glebiuskv | 0:2f0e1e23c242 | 1778 | } |
glebiuskv | 0:2f0e1e23c242 | 1779 | dp->dptr = ofs; /* Current entry */ |
glebiuskv | 0:2f0e1e23c242 | 1780 | dp->dir = fs->win + ofs % SS(fs); /* Pointer to the entry in the win[] */ |
glebiuskv | 0:2f0e1e23c242 | 1781 | |
glebiuskv | 0:2f0e1e23c242 | 1782 | return FR_OK; |
glebiuskv | 0:2f0e1e23c242 | 1783 | } |
glebiuskv | 0:2f0e1e23c242 | 1784 | |
glebiuskv | 0:2f0e1e23c242 | 1785 | |
glebiuskv | 0:2f0e1e23c242 | 1786 | |
glebiuskv | 0:2f0e1e23c242 | 1787 | |
glebiuskv | 0:2f0e1e23c242 | 1788 | #if !FF_FS_READONLY |
glebiuskv | 0:2f0e1e23c242 | 1789 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1790 | /* Directory handling - Reserve a block of directory entries */ |
glebiuskv | 0:2f0e1e23c242 | 1791 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1792 | |
glebiuskv | 0:2f0e1e23c242 | 1793 | static |
glebiuskv | 0:2f0e1e23c242 | 1794 | FRESULT dir_alloc ( /* FR_OK(0):succeeded, !=0:error */ |
glebiuskv | 0:2f0e1e23c242 | 1795 | FATFS_DIR* dp, /* Pointer to the directory object */ |
glebiuskv | 0:2f0e1e23c242 | 1796 | UINT nent /* Number of contiguous entries to allocate */ |
glebiuskv | 0:2f0e1e23c242 | 1797 | ) |
glebiuskv | 0:2f0e1e23c242 | 1798 | { |
glebiuskv | 0:2f0e1e23c242 | 1799 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 1800 | UINT n; |
glebiuskv | 0:2f0e1e23c242 | 1801 | FATFS *fs = dp->obj.fs; |
glebiuskv | 0:2f0e1e23c242 | 1802 | |
glebiuskv | 0:2f0e1e23c242 | 1803 | |
glebiuskv | 0:2f0e1e23c242 | 1804 | res = dir_sdi(dp, 0); |
glebiuskv | 0:2f0e1e23c242 | 1805 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 1806 | n = 0; |
glebiuskv | 0:2f0e1e23c242 | 1807 | do { |
glebiuskv | 0:2f0e1e23c242 | 1808 | res = move_window(fs, dp->sect); |
glebiuskv | 0:2f0e1e23c242 | 1809 | if (res != FR_OK) break; |
glebiuskv | 0:2f0e1e23c242 | 1810 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 1811 | if ((fs->fs_type == FS_EXFAT) ? (int)((dp->dir[XDIR_Type] & 0x80) == 0) : (int)(dp->dir[DIR_Name] == DDEM || dp->dir[DIR_Name] == 0)) { |
glebiuskv | 0:2f0e1e23c242 | 1812 | #else |
glebiuskv | 0:2f0e1e23c242 | 1813 | if (dp->dir[DIR_Name] == DDEM || dp->dir[DIR_Name] == 0) { |
glebiuskv | 0:2f0e1e23c242 | 1814 | #endif |
glebiuskv | 0:2f0e1e23c242 | 1815 | if (++n == nent) break; /* A block of contiguous free entries is found */ |
glebiuskv | 0:2f0e1e23c242 | 1816 | } else { |
glebiuskv | 0:2f0e1e23c242 | 1817 | n = 0; /* Not a blank entry. Restart to search */ |
glebiuskv | 0:2f0e1e23c242 | 1818 | } |
glebiuskv | 0:2f0e1e23c242 | 1819 | res = dir_next(dp, 1); |
glebiuskv | 0:2f0e1e23c242 | 1820 | } while (res == FR_OK); /* Next entry with table stretch enabled */ |
glebiuskv | 0:2f0e1e23c242 | 1821 | } |
glebiuskv | 0:2f0e1e23c242 | 1822 | |
glebiuskv | 0:2f0e1e23c242 | 1823 | if (res == FR_NO_FILE) res = FR_DENIED; /* No directory entry to allocate */ |
glebiuskv | 0:2f0e1e23c242 | 1824 | return res; |
glebiuskv | 0:2f0e1e23c242 | 1825 | } |
glebiuskv | 0:2f0e1e23c242 | 1826 | |
glebiuskv | 0:2f0e1e23c242 | 1827 | #endif /* !FF_FS_READONLY */ |
glebiuskv | 0:2f0e1e23c242 | 1828 | |
glebiuskv | 0:2f0e1e23c242 | 1829 | |
glebiuskv | 0:2f0e1e23c242 | 1830 | |
glebiuskv | 0:2f0e1e23c242 | 1831 | |
glebiuskv | 0:2f0e1e23c242 | 1832 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1833 | /* FAT: Directory handling - Load/Store start cluster number */ |
glebiuskv | 0:2f0e1e23c242 | 1834 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1835 | |
glebiuskv | 0:2f0e1e23c242 | 1836 | static |
glebiuskv | 0:2f0e1e23c242 | 1837 | DWORD ld_clust ( /* Returns the top cluster value of the SFN entry */ |
glebiuskv | 0:2f0e1e23c242 | 1838 | FATFS* fs, /* Pointer to the fs object */ |
glebiuskv | 0:2f0e1e23c242 | 1839 | const BYTE* dir /* Pointer to the key entry */ |
glebiuskv | 0:2f0e1e23c242 | 1840 | ) |
glebiuskv | 0:2f0e1e23c242 | 1841 | { |
glebiuskv | 0:2f0e1e23c242 | 1842 | DWORD cl; |
glebiuskv | 0:2f0e1e23c242 | 1843 | |
glebiuskv | 0:2f0e1e23c242 | 1844 | cl = ld_word(dir + DIR_FstClusLO); |
glebiuskv | 0:2f0e1e23c242 | 1845 | if (fs->fs_type == FS_FAT32) { |
glebiuskv | 0:2f0e1e23c242 | 1846 | cl |= (DWORD)ld_word(dir + DIR_FstClusHI) << 16; |
glebiuskv | 0:2f0e1e23c242 | 1847 | } |
glebiuskv | 0:2f0e1e23c242 | 1848 | |
glebiuskv | 0:2f0e1e23c242 | 1849 | return cl; |
glebiuskv | 0:2f0e1e23c242 | 1850 | } |
glebiuskv | 0:2f0e1e23c242 | 1851 | |
glebiuskv | 0:2f0e1e23c242 | 1852 | |
glebiuskv | 0:2f0e1e23c242 | 1853 | #if !FF_FS_READONLY |
glebiuskv | 0:2f0e1e23c242 | 1854 | static |
glebiuskv | 0:2f0e1e23c242 | 1855 | void st_clust ( |
glebiuskv | 0:2f0e1e23c242 | 1856 | FATFS* fs, /* Pointer to the fs object */ |
glebiuskv | 0:2f0e1e23c242 | 1857 | BYTE* dir, /* Pointer to the key entry */ |
glebiuskv | 0:2f0e1e23c242 | 1858 | DWORD cl /* Value to be set */ |
glebiuskv | 0:2f0e1e23c242 | 1859 | ) |
glebiuskv | 0:2f0e1e23c242 | 1860 | { |
glebiuskv | 0:2f0e1e23c242 | 1861 | st_word(dir + DIR_FstClusLO, (WORD)cl); |
glebiuskv | 0:2f0e1e23c242 | 1862 | if (fs->fs_type == FS_FAT32) { |
glebiuskv | 0:2f0e1e23c242 | 1863 | st_word(dir + DIR_FstClusHI, (WORD)(cl >> 16)); |
glebiuskv | 0:2f0e1e23c242 | 1864 | } |
glebiuskv | 0:2f0e1e23c242 | 1865 | } |
glebiuskv | 0:2f0e1e23c242 | 1866 | #endif |
glebiuskv | 0:2f0e1e23c242 | 1867 | |
glebiuskv | 0:2f0e1e23c242 | 1868 | |
glebiuskv | 0:2f0e1e23c242 | 1869 | |
glebiuskv | 0:2f0e1e23c242 | 1870 | #if FF_USE_LFN |
glebiuskv | 0:2f0e1e23c242 | 1871 | /*--------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1872 | /* FAT-LFN: Compare a part of file name with an LFN entry */ |
glebiuskv | 0:2f0e1e23c242 | 1873 | /*--------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1874 | static |
glebiuskv | 0:2f0e1e23c242 | 1875 | int cmp_lfn ( /* 1:matched, 0:not matched */ |
glebiuskv | 0:2f0e1e23c242 | 1876 | const WCHAR* lfnbuf, /* Pointer to the LFN working buffer to be compared */ |
glebiuskv | 0:2f0e1e23c242 | 1877 | BYTE* dir /* Pointer to the directory entry containing the part of LFN */ |
glebiuskv | 0:2f0e1e23c242 | 1878 | ) |
glebiuskv | 0:2f0e1e23c242 | 1879 | { |
glebiuskv | 0:2f0e1e23c242 | 1880 | UINT i, s; |
glebiuskv | 0:2f0e1e23c242 | 1881 | WCHAR wc, uc; |
glebiuskv | 0:2f0e1e23c242 | 1882 | |
glebiuskv | 0:2f0e1e23c242 | 1883 | |
glebiuskv | 0:2f0e1e23c242 | 1884 | if (ld_word(dir + LDIR_FstClusLO) != 0) return 0; /* Check LDIR_FstClusLO */ |
glebiuskv | 0:2f0e1e23c242 | 1885 | |
glebiuskv | 0:2f0e1e23c242 | 1886 | i = ((dir[LDIR_Ord] & 0x3F) - 1) * 13; /* Offset in the LFN buffer */ |
glebiuskv | 0:2f0e1e23c242 | 1887 | |
glebiuskv | 0:2f0e1e23c242 | 1888 | for (wc = 1, s = 0; s < 13; s++) { /* Process all characters in the entry */ |
glebiuskv | 0:2f0e1e23c242 | 1889 | uc = ld_word(dir + LfnOfs[s]); /* Pick an LFN character */ |
glebiuskv | 0:2f0e1e23c242 | 1890 | if (wc != 0) { |
glebiuskv | 0:2f0e1e23c242 | 1891 | if (i >= FF_MAX_LFN || ff_wtoupper(uc) != ff_wtoupper(lfnbuf[i++])) { /* Compare it */ |
glebiuskv | 0:2f0e1e23c242 | 1892 | return 0; /* Not matched */ |
glebiuskv | 0:2f0e1e23c242 | 1893 | } |
glebiuskv | 0:2f0e1e23c242 | 1894 | wc = uc; |
glebiuskv | 0:2f0e1e23c242 | 1895 | } else { |
glebiuskv | 0:2f0e1e23c242 | 1896 | if (uc != 0xFFFF) return 0; /* Check filler */ |
glebiuskv | 0:2f0e1e23c242 | 1897 | } |
glebiuskv | 0:2f0e1e23c242 | 1898 | } |
glebiuskv | 0:2f0e1e23c242 | 1899 | |
glebiuskv | 0:2f0e1e23c242 | 1900 | if ((dir[LDIR_Ord] & LLEF) && wc && lfnbuf[i]) return 0; /* Last segment matched but different length */ |
glebiuskv | 0:2f0e1e23c242 | 1901 | |
glebiuskv | 0:2f0e1e23c242 | 1902 | return 1; /* The part of LFN matched */ |
glebiuskv | 0:2f0e1e23c242 | 1903 | } |
glebiuskv | 0:2f0e1e23c242 | 1904 | |
glebiuskv | 0:2f0e1e23c242 | 1905 | |
glebiuskv | 0:2f0e1e23c242 | 1906 | #if FF_FS_MINIMIZE <= 1 || FF_FS_RPATH >= 2 || FF_USE_LABEL || FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 1907 | /*-----------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1908 | /* FAT-LFN: Pick a part of file name from an LFN entry */ |
glebiuskv | 0:2f0e1e23c242 | 1909 | /*-----------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1910 | static |
glebiuskv | 0:2f0e1e23c242 | 1911 | int pick_lfn ( /* 1:succeeded, 0:buffer overflow or invalid LFN entry */ |
glebiuskv | 0:2f0e1e23c242 | 1912 | WCHAR* lfnbuf, /* Pointer to the LFN working buffer */ |
glebiuskv | 0:2f0e1e23c242 | 1913 | BYTE* dir /* Pointer to the LFN entry */ |
glebiuskv | 0:2f0e1e23c242 | 1914 | ) |
glebiuskv | 0:2f0e1e23c242 | 1915 | { |
glebiuskv | 0:2f0e1e23c242 | 1916 | UINT i, s; |
glebiuskv | 0:2f0e1e23c242 | 1917 | WCHAR wc, uc; |
glebiuskv | 0:2f0e1e23c242 | 1918 | |
glebiuskv | 0:2f0e1e23c242 | 1919 | |
glebiuskv | 0:2f0e1e23c242 | 1920 | if (ld_word(dir + LDIR_FstClusLO) != 0) return 0; /* Check LDIR_FstClusLO is 0 */ |
glebiuskv | 0:2f0e1e23c242 | 1921 | |
glebiuskv | 0:2f0e1e23c242 | 1922 | i = ((dir[LDIR_Ord] & ~LLEF) - 1) * 13; /* Offset in the LFN buffer */ |
glebiuskv | 0:2f0e1e23c242 | 1923 | |
glebiuskv | 0:2f0e1e23c242 | 1924 | for (wc = 1, s = 0; s < 13; s++) { /* Process all characters in the entry */ |
glebiuskv | 0:2f0e1e23c242 | 1925 | uc = ld_word(dir + LfnOfs[s]); /* Pick an LFN character */ |
glebiuskv | 0:2f0e1e23c242 | 1926 | if (wc != 0) { |
glebiuskv | 0:2f0e1e23c242 | 1927 | if (i >= FF_MAX_LFN) return 0; /* Buffer overflow? */ |
glebiuskv | 0:2f0e1e23c242 | 1928 | lfnbuf[i++] = wc = uc; /* Store it */ |
glebiuskv | 0:2f0e1e23c242 | 1929 | } else { |
glebiuskv | 0:2f0e1e23c242 | 1930 | if (uc != 0xFFFF) return 0; /* Check filler */ |
glebiuskv | 0:2f0e1e23c242 | 1931 | } |
glebiuskv | 0:2f0e1e23c242 | 1932 | } |
glebiuskv | 0:2f0e1e23c242 | 1933 | |
glebiuskv | 0:2f0e1e23c242 | 1934 | if (dir[LDIR_Ord] & LLEF) { /* Put terminator if it is the last LFN part */ |
glebiuskv | 0:2f0e1e23c242 | 1935 | if (i >= FF_MAX_LFN) return 0; /* Buffer overflow? */ |
glebiuskv | 0:2f0e1e23c242 | 1936 | lfnbuf[i] = 0; |
glebiuskv | 0:2f0e1e23c242 | 1937 | } |
glebiuskv | 0:2f0e1e23c242 | 1938 | |
glebiuskv | 0:2f0e1e23c242 | 1939 | return 1; /* The part of LFN is valid */ |
glebiuskv | 0:2f0e1e23c242 | 1940 | } |
glebiuskv | 0:2f0e1e23c242 | 1941 | #endif |
glebiuskv | 0:2f0e1e23c242 | 1942 | |
glebiuskv | 0:2f0e1e23c242 | 1943 | |
glebiuskv | 0:2f0e1e23c242 | 1944 | #if !FF_FS_READONLY |
glebiuskv | 0:2f0e1e23c242 | 1945 | /*-----------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1946 | /* FAT-LFN: Create an entry of LFN entries */ |
glebiuskv | 0:2f0e1e23c242 | 1947 | /*-----------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1948 | static |
glebiuskv | 0:2f0e1e23c242 | 1949 | void put_lfn ( |
glebiuskv | 0:2f0e1e23c242 | 1950 | const WCHAR* lfn, /* Pointer to the LFN */ |
glebiuskv | 0:2f0e1e23c242 | 1951 | BYTE* dir, /* Pointer to the LFN entry to be created */ |
glebiuskv | 0:2f0e1e23c242 | 1952 | BYTE ord, /* LFN order (1-20) */ |
glebiuskv | 0:2f0e1e23c242 | 1953 | BYTE sum /* Checksum of the corresponding SFN */ |
glebiuskv | 0:2f0e1e23c242 | 1954 | ) |
glebiuskv | 0:2f0e1e23c242 | 1955 | { |
glebiuskv | 0:2f0e1e23c242 | 1956 | UINT i, s; |
glebiuskv | 0:2f0e1e23c242 | 1957 | WCHAR wc; |
glebiuskv | 0:2f0e1e23c242 | 1958 | |
glebiuskv | 0:2f0e1e23c242 | 1959 | |
glebiuskv | 0:2f0e1e23c242 | 1960 | dir[LDIR_Chksum] = sum; /* Set checksum */ |
glebiuskv | 0:2f0e1e23c242 | 1961 | dir[LDIR_Attr] = AM_LFN; /* Set attribute. LFN entry */ |
glebiuskv | 0:2f0e1e23c242 | 1962 | dir[LDIR_Type] = 0; |
glebiuskv | 0:2f0e1e23c242 | 1963 | st_word(dir + LDIR_FstClusLO, 0); |
glebiuskv | 0:2f0e1e23c242 | 1964 | |
glebiuskv | 0:2f0e1e23c242 | 1965 | i = (ord - 1) * 13; /* Get offset in the LFN working buffer */ |
glebiuskv | 0:2f0e1e23c242 | 1966 | s = wc = 0; |
glebiuskv | 0:2f0e1e23c242 | 1967 | do { |
glebiuskv | 0:2f0e1e23c242 | 1968 | if (wc != 0xFFFF) wc = lfn[i++]; /* Get an effective character */ |
glebiuskv | 0:2f0e1e23c242 | 1969 | st_word(dir + LfnOfs[s], wc); /* Put it */ |
glebiuskv | 0:2f0e1e23c242 | 1970 | if (wc == 0) wc = 0xFFFF; /* Padding characters for left locations */ |
glebiuskv | 0:2f0e1e23c242 | 1971 | } while (++s < 13); |
glebiuskv | 0:2f0e1e23c242 | 1972 | if (wc == 0xFFFF || !lfn[i]) ord |= LLEF; /* Last LFN part is the start of LFN sequence */ |
glebiuskv | 0:2f0e1e23c242 | 1973 | dir[LDIR_Ord] = ord; /* Set the LFN order */ |
glebiuskv | 0:2f0e1e23c242 | 1974 | } |
glebiuskv | 0:2f0e1e23c242 | 1975 | |
glebiuskv | 0:2f0e1e23c242 | 1976 | #endif /* !FF_FS_READONLY */ |
glebiuskv | 0:2f0e1e23c242 | 1977 | #endif /* FF_USE_LFN */ |
glebiuskv | 0:2f0e1e23c242 | 1978 | |
glebiuskv | 0:2f0e1e23c242 | 1979 | |
glebiuskv | 0:2f0e1e23c242 | 1980 | |
glebiuskv | 0:2f0e1e23c242 | 1981 | #if FF_USE_LFN && !FF_FS_READONLY |
glebiuskv | 0:2f0e1e23c242 | 1982 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1983 | /* FAT-LFN: Create a Numbered SFN */ |
glebiuskv | 0:2f0e1e23c242 | 1984 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 1985 | |
glebiuskv | 0:2f0e1e23c242 | 1986 | static |
glebiuskv | 0:2f0e1e23c242 | 1987 | void gen_numname ( |
glebiuskv | 0:2f0e1e23c242 | 1988 | BYTE* dst, /* Pointer to the buffer to store numbered SFN */ |
glebiuskv | 0:2f0e1e23c242 | 1989 | const BYTE* src, /* Pointer to SFN */ |
glebiuskv | 0:2f0e1e23c242 | 1990 | const WCHAR* lfn, /* Pointer to LFN */ |
glebiuskv | 0:2f0e1e23c242 | 1991 | UINT seq /* Sequence number */ |
glebiuskv | 0:2f0e1e23c242 | 1992 | ) |
glebiuskv | 0:2f0e1e23c242 | 1993 | { |
glebiuskv | 0:2f0e1e23c242 | 1994 | BYTE ns[8], c; |
glebiuskv | 0:2f0e1e23c242 | 1995 | UINT i, j; |
glebiuskv | 0:2f0e1e23c242 | 1996 | WCHAR wc; |
glebiuskv | 0:2f0e1e23c242 | 1997 | DWORD sr; |
glebiuskv | 0:2f0e1e23c242 | 1998 | |
glebiuskv | 0:2f0e1e23c242 | 1999 | |
glebiuskv | 0:2f0e1e23c242 | 2000 | mem_cpy(dst, src, 11); |
glebiuskv | 0:2f0e1e23c242 | 2001 | |
glebiuskv | 0:2f0e1e23c242 | 2002 | if (seq > 5) { /* In case of many collisions, generate a hash number instead of sequential number */ |
glebiuskv | 0:2f0e1e23c242 | 2003 | sr = seq; |
glebiuskv | 0:2f0e1e23c242 | 2004 | while (*lfn) { /* Create a CRC */ |
glebiuskv | 0:2f0e1e23c242 | 2005 | wc = *lfn++; |
glebiuskv | 0:2f0e1e23c242 | 2006 | for (i = 0; i < 16; i++) { |
glebiuskv | 0:2f0e1e23c242 | 2007 | sr = (sr << 1) + (wc & 1); |
glebiuskv | 0:2f0e1e23c242 | 2008 | wc >>= 1; |
glebiuskv | 0:2f0e1e23c242 | 2009 | if (sr & 0x10000) sr ^= 0x11021; |
glebiuskv | 0:2f0e1e23c242 | 2010 | } |
glebiuskv | 0:2f0e1e23c242 | 2011 | } |
glebiuskv | 0:2f0e1e23c242 | 2012 | seq = (UINT)sr; |
glebiuskv | 0:2f0e1e23c242 | 2013 | } |
glebiuskv | 0:2f0e1e23c242 | 2014 | |
glebiuskv | 0:2f0e1e23c242 | 2015 | /* itoa (hexdecimal) */ |
glebiuskv | 0:2f0e1e23c242 | 2016 | i = 7; |
glebiuskv | 0:2f0e1e23c242 | 2017 | do { |
glebiuskv | 0:2f0e1e23c242 | 2018 | c = (BYTE)((seq % 16) + '0'); |
glebiuskv | 0:2f0e1e23c242 | 2019 | if (c > '9') c += 7; |
glebiuskv | 0:2f0e1e23c242 | 2020 | ns[i--] = c; |
glebiuskv | 0:2f0e1e23c242 | 2021 | seq /= 16; |
glebiuskv | 0:2f0e1e23c242 | 2022 | } while (seq); |
glebiuskv | 0:2f0e1e23c242 | 2023 | ns[i] = '~'; |
glebiuskv | 0:2f0e1e23c242 | 2024 | |
glebiuskv | 0:2f0e1e23c242 | 2025 | /* Append the number to the SFN body */ |
glebiuskv | 0:2f0e1e23c242 | 2026 | for (j = 0; j < i && dst[j] != ' '; j++) { |
glebiuskv | 0:2f0e1e23c242 | 2027 | if (dbc_1st(dst[j])) { |
glebiuskv | 0:2f0e1e23c242 | 2028 | if (j == i - 1) break; |
glebiuskv | 0:2f0e1e23c242 | 2029 | j++; |
glebiuskv | 0:2f0e1e23c242 | 2030 | } |
glebiuskv | 0:2f0e1e23c242 | 2031 | } |
glebiuskv | 0:2f0e1e23c242 | 2032 | do { |
glebiuskv | 0:2f0e1e23c242 | 2033 | dst[j++] = (i < 8) ? ns[i++] : ' '; |
glebiuskv | 0:2f0e1e23c242 | 2034 | } while (j < 8); |
glebiuskv | 0:2f0e1e23c242 | 2035 | } |
glebiuskv | 0:2f0e1e23c242 | 2036 | #endif /* FF_USE_LFN && !FF_FS_READONLY */ |
glebiuskv | 0:2f0e1e23c242 | 2037 | |
glebiuskv | 0:2f0e1e23c242 | 2038 | |
glebiuskv | 0:2f0e1e23c242 | 2039 | |
glebiuskv | 0:2f0e1e23c242 | 2040 | #if FF_USE_LFN |
glebiuskv | 0:2f0e1e23c242 | 2041 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 2042 | /* FAT-LFN: Calculate checksum of an SFN entry */ |
glebiuskv | 0:2f0e1e23c242 | 2043 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 2044 | |
glebiuskv | 0:2f0e1e23c242 | 2045 | static |
glebiuskv | 0:2f0e1e23c242 | 2046 | BYTE sum_sfn ( |
glebiuskv | 0:2f0e1e23c242 | 2047 | const BYTE* dir /* Pointer to the SFN entry */ |
glebiuskv | 0:2f0e1e23c242 | 2048 | ) |
glebiuskv | 0:2f0e1e23c242 | 2049 | { |
glebiuskv | 0:2f0e1e23c242 | 2050 | BYTE sum = 0; |
glebiuskv | 0:2f0e1e23c242 | 2051 | UINT n = 11; |
glebiuskv | 0:2f0e1e23c242 | 2052 | |
glebiuskv | 0:2f0e1e23c242 | 2053 | do { |
glebiuskv | 0:2f0e1e23c242 | 2054 | sum = (sum >> 1) + (sum << 7) + *dir++; |
glebiuskv | 0:2f0e1e23c242 | 2055 | } while (--n); |
glebiuskv | 0:2f0e1e23c242 | 2056 | return sum; |
glebiuskv | 0:2f0e1e23c242 | 2057 | } |
glebiuskv | 0:2f0e1e23c242 | 2058 | |
glebiuskv | 0:2f0e1e23c242 | 2059 | #endif /* FF_USE_LFN */ |
glebiuskv | 0:2f0e1e23c242 | 2060 | |
glebiuskv | 0:2f0e1e23c242 | 2061 | |
glebiuskv | 0:2f0e1e23c242 | 2062 | |
glebiuskv | 0:2f0e1e23c242 | 2063 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 2064 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 2065 | /* exFAT: Checksum */ |
glebiuskv | 0:2f0e1e23c242 | 2066 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 2067 | |
glebiuskv | 0:2f0e1e23c242 | 2068 | static |
glebiuskv | 0:2f0e1e23c242 | 2069 | WORD xdir_sum ( /* Get checksum of the directoly entry block */ |
glebiuskv | 0:2f0e1e23c242 | 2070 | const BYTE* dir /* Directory entry block to be calculated */ |
glebiuskv | 0:2f0e1e23c242 | 2071 | ) |
glebiuskv | 0:2f0e1e23c242 | 2072 | { |
glebiuskv | 0:2f0e1e23c242 | 2073 | UINT i, szblk; |
glebiuskv | 0:2f0e1e23c242 | 2074 | WORD sum; |
glebiuskv | 0:2f0e1e23c242 | 2075 | |
glebiuskv | 0:2f0e1e23c242 | 2076 | |
glebiuskv | 0:2f0e1e23c242 | 2077 | szblk = (dir[XDIR_NumSec] + 1) * SZDIRE; |
glebiuskv | 0:2f0e1e23c242 | 2078 | for (i = sum = 0; i < szblk; i++) { |
glebiuskv | 0:2f0e1e23c242 | 2079 | if (i == XDIR_SetSum) { /* Skip sum field */ |
glebiuskv | 0:2f0e1e23c242 | 2080 | i++; |
glebiuskv | 0:2f0e1e23c242 | 2081 | } else { |
glebiuskv | 0:2f0e1e23c242 | 2082 | sum = ((sum & 1) ? 0x8000 : 0) + (sum >> 1) + dir[i]; |
glebiuskv | 0:2f0e1e23c242 | 2083 | } |
glebiuskv | 0:2f0e1e23c242 | 2084 | } |
glebiuskv | 0:2f0e1e23c242 | 2085 | return sum; |
glebiuskv | 0:2f0e1e23c242 | 2086 | } |
glebiuskv | 0:2f0e1e23c242 | 2087 | |
glebiuskv | 0:2f0e1e23c242 | 2088 | |
glebiuskv | 0:2f0e1e23c242 | 2089 | |
glebiuskv | 0:2f0e1e23c242 | 2090 | static |
glebiuskv | 0:2f0e1e23c242 | 2091 | WORD xname_sum ( /* Get check sum (to be used as hash) of the name */ |
glebiuskv | 0:2f0e1e23c242 | 2092 | const WCHAR* name /* File name to be calculated */ |
glebiuskv | 0:2f0e1e23c242 | 2093 | ) |
glebiuskv | 0:2f0e1e23c242 | 2094 | { |
glebiuskv | 0:2f0e1e23c242 | 2095 | WCHAR chr; |
glebiuskv | 0:2f0e1e23c242 | 2096 | WORD sum = 0; |
glebiuskv | 0:2f0e1e23c242 | 2097 | |
glebiuskv | 0:2f0e1e23c242 | 2098 | |
glebiuskv | 0:2f0e1e23c242 | 2099 | while ((chr = *name++) != 0) { |
glebiuskv | 0:2f0e1e23c242 | 2100 | chr = (WCHAR)ff_wtoupper(chr); /* File name needs to be upper-case converted */ |
glebiuskv | 0:2f0e1e23c242 | 2101 | sum = ((sum & 1) ? 0x8000 : 0) + (sum >> 1) + (chr & 0xFF); |
glebiuskv | 0:2f0e1e23c242 | 2102 | sum = ((sum & 1) ? 0x8000 : 0) + (sum >> 1) + (chr >> 8); |
glebiuskv | 0:2f0e1e23c242 | 2103 | } |
glebiuskv | 0:2f0e1e23c242 | 2104 | return sum; |
glebiuskv | 0:2f0e1e23c242 | 2105 | } |
glebiuskv | 0:2f0e1e23c242 | 2106 | |
glebiuskv | 0:2f0e1e23c242 | 2107 | |
glebiuskv | 0:2f0e1e23c242 | 2108 | #if !FF_FS_READONLY && FF_USE_MKFS |
glebiuskv | 0:2f0e1e23c242 | 2109 | static |
glebiuskv | 0:2f0e1e23c242 | 2110 | DWORD xsum32 ( |
glebiuskv | 0:2f0e1e23c242 | 2111 | BYTE dat, /* Byte to be calculated */ |
glebiuskv | 0:2f0e1e23c242 | 2112 | DWORD sum /* Previous sum */ |
glebiuskv | 0:2f0e1e23c242 | 2113 | ) |
glebiuskv | 0:2f0e1e23c242 | 2114 | { |
glebiuskv | 0:2f0e1e23c242 | 2115 | sum = ((sum & 1) ? 0x80000000 : 0) + (sum >> 1) + dat; |
glebiuskv | 0:2f0e1e23c242 | 2116 | return sum; |
glebiuskv | 0:2f0e1e23c242 | 2117 | } |
glebiuskv | 0:2f0e1e23c242 | 2118 | #endif |
glebiuskv | 0:2f0e1e23c242 | 2119 | |
glebiuskv | 0:2f0e1e23c242 | 2120 | |
glebiuskv | 0:2f0e1e23c242 | 2121 | #if FF_FS_MINIMIZE <= 1 || FF_FS_RPATH >= 2 |
glebiuskv | 0:2f0e1e23c242 | 2122 | /*------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 2123 | /* exFAT: Get object information from a directory block */ |
glebiuskv | 0:2f0e1e23c242 | 2124 | /*------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 2125 | |
glebiuskv | 0:2f0e1e23c242 | 2126 | static |
glebiuskv | 0:2f0e1e23c242 | 2127 | void get_xfileinfo ( |
glebiuskv | 0:2f0e1e23c242 | 2128 | BYTE* dirb, /* Pointer to the direcotry entry block 85+C0+C1s */ |
glebiuskv | 0:2f0e1e23c242 | 2129 | FILINFO* fno /* Buffer to store the extracted file information */ |
glebiuskv | 0:2f0e1e23c242 | 2130 | ) |
glebiuskv | 0:2f0e1e23c242 | 2131 | { |
glebiuskv | 0:2f0e1e23c242 | 2132 | WCHAR wc, hs; |
glebiuskv | 0:2f0e1e23c242 | 2133 | UINT di, si, nc; |
glebiuskv | 0:2f0e1e23c242 | 2134 | |
glebiuskv | 0:2f0e1e23c242 | 2135 | /* Get file name from the entry block */ |
glebiuskv | 0:2f0e1e23c242 | 2136 | si = SZDIRE * 2; /* 1st C1 entry */ |
glebiuskv | 0:2f0e1e23c242 | 2137 | nc = hs = di = 0; |
glebiuskv | 0:2f0e1e23c242 | 2138 | while (nc < dirb[XDIR_NumName]) { |
glebiuskv | 0:2f0e1e23c242 | 2139 | if (si >= MAXDIRB(FF_MAX_LFN)) { di = 0; break; } /* Truncated directory block? */ |
glebiuskv | 0:2f0e1e23c242 | 2140 | if ((si % SZDIRE) == 0) si += 2; /* Skip entry type field */ |
glebiuskv | 0:2f0e1e23c242 | 2141 | wc = ld_word(dirb + si); si += 2; nc++; /* Get a character */ |
glebiuskv | 0:2f0e1e23c242 | 2142 | if (hs == 0 && IsSurrogate(wc)) { /* Is it a surrogate? */ |
glebiuskv | 0:2f0e1e23c242 | 2143 | hs = wc; continue; /* Get low surrogate */ |
glebiuskv | 0:2f0e1e23c242 | 2144 | } |
glebiuskv | 0:2f0e1e23c242 | 2145 | wc = put_utf((DWORD)hs << 16 | wc, &fno->fname[di], FF_LFN_BUF - di); /* Store it in UTF-16 or UTF-8 encoding */ |
glebiuskv | 0:2f0e1e23c242 | 2146 | if (wc == 0) { di = 0; break; } /* Buffer overflow or wrong encoding? */ |
glebiuskv | 0:2f0e1e23c242 | 2147 | di += wc; |
glebiuskv | 0:2f0e1e23c242 | 2148 | hs = 0; |
glebiuskv | 0:2f0e1e23c242 | 2149 | } |
glebiuskv | 0:2f0e1e23c242 | 2150 | if (hs != 0) di = 0; /* Broken surrogate pair? */ |
glebiuskv | 0:2f0e1e23c242 | 2151 | if (di == 0) fno->fname[di++] = '?'; /* Inaccessible object name? */ |
glebiuskv | 0:2f0e1e23c242 | 2152 | fno->fname[di] = 0; /* Terminate the name */ |
glebiuskv | 0:2f0e1e23c242 | 2153 | fno->altname[0] = 0; /* exFAT does not have SFN */ |
glebiuskv | 0:2f0e1e23c242 | 2154 | |
glebiuskv | 0:2f0e1e23c242 | 2155 | fno->fattrib = dirb[XDIR_Attr]; /* Attribute */ |
glebiuskv | 0:2f0e1e23c242 | 2156 | fno->fsize = (fno->fattrib & AM_DIR) ? 0 : ld_qword(dirb + XDIR_FileSize); /* Size */ |
glebiuskv | 0:2f0e1e23c242 | 2157 | fno->ftime = ld_word(dirb + XDIR_ModTime + 0); /* Time */ |
glebiuskv | 0:2f0e1e23c242 | 2158 | fno->fdate = ld_word(dirb + XDIR_ModTime + 2); /* Date */ |
glebiuskv | 0:2f0e1e23c242 | 2159 | } |
glebiuskv | 0:2f0e1e23c242 | 2160 | |
glebiuskv | 0:2f0e1e23c242 | 2161 | #endif /* FF_FS_MINIMIZE <= 1 || FF_FS_RPATH >= 2 */ |
glebiuskv | 0:2f0e1e23c242 | 2162 | |
glebiuskv | 0:2f0e1e23c242 | 2163 | |
glebiuskv | 0:2f0e1e23c242 | 2164 | /*-----------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 2165 | /* exFAT: Get a directry entry block */ |
glebiuskv | 0:2f0e1e23c242 | 2166 | /*-----------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 2167 | |
glebiuskv | 0:2f0e1e23c242 | 2168 | static |
glebiuskv | 0:2f0e1e23c242 | 2169 | FRESULT load_xdir ( /* FR_INT_ERR: invalid entry block */ |
glebiuskv | 0:2f0e1e23c242 | 2170 | FATFS_DIR* dp /* Reading direcotry object pointing top of the entry block to load */ |
glebiuskv | 0:2f0e1e23c242 | 2171 | ) |
glebiuskv | 0:2f0e1e23c242 | 2172 | { |
glebiuskv | 0:2f0e1e23c242 | 2173 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 2174 | UINT i, sz_ent; |
glebiuskv | 0:2f0e1e23c242 | 2175 | BYTE* dirb = dp->obj.fs->dirbuf; /* Pointer to the on-memory direcotry entry block 85+C0+C1s */ |
glebiuskv | 0:2f0e1e23c242 | 2176 | |
glebiuskv | 0:2f0e1e23c242 | 2177 | |
glebiuskv | 0:2f0e1e23c242 | 2178 | /* Load 85 entry */ |
glebiuskv | 0:2f0e1e23c242 | 2179 | res = move_window(dp->obj.fs, dp->sect); |
glebiuskv | 0:2f0e1e23c242 | 2180 | if (res != FR_OK) return res; |
glebiuskv | 0:2f0e1e23c242 | 2181 | if (dp->dir[XDIR_Type] != 0x85) return FR_INT_ERR; /* Invalid order */ |
glebiuskv | 0:2f0e1e23c242 | 2182 | mem_cpy(dirb + 0 * SZDIRE, dp->dir, SZDIRE); |
glebiuskv | 0:2f0e1e23c242 | 2183 | sz_ent = (dirb[XDIR_NumSec] + 1) * SZDIRE; |
glebiuskv | 0:2f0e1e23c242 | 2184 | if (sz_ent < 3 * SZDIRE || sz_ent > 19 * SZDIRE) return FR_INT_ERR; |
glebiuskv | 0:2f0e1e23c242 | 2185 | |
glebiuskv | 0:2f0e1e23c242 | 2186 | /* Load C0 entry */ |
glebiuskv | 0:2f0e1e23c242 | 2187 | res = dir_next(dp, 0); |
glebiuskv | 0:2f0e1e23c242 | 2188 | if (res == FR_NO_FILE) res = FR_INT_ERR; /* It cannot be */ |
glebiuskv | 0:2f0e1e23c242 | 2189 | if (res != FR_OK) return res; |
glebiuskv | 0:2f0e1e23c242 | 2190 | res = move_window(dp->obj.fs, dp->sect); |
glebiuskv | 0:2f0e1e23c242 | 2191 | if (res != FR_OK) return res; |
glebiuskv | 0:2f0e1e23c242 | 2192 | if (dp->dir[XDIR_Type] != 0xC0) return FR_INT_ERR; /* Invalid order */ |
glebiuskv | 0:2f0e1e23c242 | 2193 | mem_cpy(dirb + 1 * SZDIRE, dp->dir, SZDIRE); |
glebiuskv | 0:2f0e1e23c242 | 2194 | if (MAXDIRB(dirb[XDIR_NumName]) > sz_ent) return FR_INT_ERR; |
glebiuskv | 0:2f0e1e23c242 | 2195 | |
glebiuskv | 0:2f0e1e23c242 | 2196 | /* Load C1 entries */ |
glebiuskv | 0:2f0e1e23c242 | 2197 | i = 2 * SZDIRE; /* C1 offset to load */ |
glebiuskv | 0:2f0e1e23c242 | 2198 | do { |
glebiuskv | 0:2f0e1e23c242 | 2199 | res = dir_next(dp, 0); |
glebiuskv | 0:2f0e1e23c242 | 2200 | if (res == FR_NO_FILE) res = FR_INT_ERR; /* It cannot be */ |
glebiuskv | 0:2f0e1e23c242 | 2201 | if (res != FR_OK) return res; |
glebiuskv | 0:2f0e1e23c242 | 2202 | res = move_window(dp->obj.fs, dp->sect); |
glebiuskv | 0:2f0e1e23c242 | 2203 | if (res != FR_OK) return res; |
glebiuskv | 0:2f0e1e23c242 | 2204 | if (dp->dir[XDIR_Type] != 0xC1) return FR_INT_ERR; /* Invalid order */ |
glebiuskv | 0:2f0e1e23c242 | 2205 | if (i < MAXDIRB(FF_MAX_LFN)) mem_cpy(dirb + i, dp->dir, SZDIRE); |
glebiuskv | 0:2f0e1e23c242 | 2206 | } while ((i += SZDIRE) < sz_ent); |
glebiuskv | 0:2f0e1e23c242 | 2207 | |
glebiuskv | 0:2f0e1e23c242 | 2208 | /* Sanity check (do it for only accessible object) */ |
glebiuskv | 0:2f0e1e23c242 | 2209 | if (i <= MAXDIRB(FF_MAX_LFN)) { |
glebiuskv | 0:2f0e1e23c242 | 2210 | if (xdir_sum(dirb) != ld_word(dirb + XDIR_SetSum)) return FR_INT_ERR; |
glebiuskv | 0:2f0e1e23c242 | 2211 | } |
glebiuskv | 0:2f0e1e23c242 | 2212 | return FR_OK; |
glebiuskv | 0:2f0e1e23c242 | 2213 | } |
glebiuskv | 0:2f0e1e23c242 | 2214 | |
glebiuskv | 0:2f0e1e23c242 | 2215 | |
glebiuskv | 0:2f0e1e23c242 | 2216 | /*------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 2217 | /* exFAT: Initialize object allocation info with loaded entry block */ |
glebiuskv | 0:2f0e1e23c242 | 2218 | /*------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 2219 | |
glebiuskv | 0:2f0e1e23c242 | 2220 | static |
glebiuskv | 0:2f0e1e23c242 | 2221 | void init_alloc_info ( |
glebiuskv | 0:2f0e1e23c242 | 2222 | FATFS* fs, /* Filesystem object */ |
glebiuskv | 0:2f0e1e23c242 | 2223 | FFOBJID* obj /* Object allocation information to be initialized */ |
glebiuskv | 0:2f0e1e23c242 | 2224 | ) |
glebiuskv | 0:2f0e1e23c242 | 2225 | { |
glebiuskv | 0:2f0e1e23c242 | 2226 | obj->sclust = ld_dword(fs->dirbuf + XDIR_FstClus); /* Start cluster */ |
glebiuskv | 0:2f0e1e23c242 | 2227 | obj->objsize = ld_qword(fs->dirbuf + XDIR_FileSize); /* Size */ |
glebiuskv | 0:2f0e1e23c242 | 2228 | obj->stat = fs->dirbuf[XDIR_GenFlags] & 2; /* Allocation status */ |
glebiuskv | 0:2f0e1e23c242 | 2229 | obj->n_frag = 0; /* No last fragment info */ |
glebiuskv | 0:2f0e1e23c242 | 2230 | } |
glebiuskv | 0:2f0e1e23c242 | 2231 | |
glebiuskv | 0:2f0e1e23c242 | 2232 | |
glebiuskv | 0:2f0e1e23c242 | 2233 | |
glebiuskv | 0:2f0e1e23c242 | 2234 | #if !FF_FS_READONLY || FF_FS_RPATH != 0 |
glebiuskv | 0:2f0e1e23c242 | 2235 | /*------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 2236 | /* exFAT: Load the object's directory entry block */ |
glebiuskv | 0:2f0e1e23c242 | 2237 | /*------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 2238 | static |
glebiuskv | 0:2f0e1e23c242 | 2239 | FRESULT load_obj_xdir ( |
glebiuskv | 0:2f0e1e23c242 | 2240 | FATFS_DIR* dp, /* Blank directory object to be used to access containing direcotry */ |
glebiuskv | 0:2f0e1e23c242 | 2241 | const FFOBJID* obj /* Object with its containing directory information */ |
glebiuskv | 0:2f0e1e23c242 | 2242 | ) |
glebiuskv | 0:2f0e1e23c242 | 2243 | { |
glebiuskv | 0:2f0e1e23c242 | 2244 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 2245 | |
glebiuskv | 0:2f0e1e23c242 | 2246 | /* Open object containing directory */ |
glebiuskv | 0:2f0e1e23c242 | 2247 | dp->obj.fs = obj->fs; |
glebiuskv | 0:2f0e1e23c242 | 2248 | dp->obj.sclust = obj->c_scl; |
glebiuskv | 0:2f0e1e23c242 | 2249 | dp->obj.stat = (BYTE)obj->c_size; |
glebiuskv | 0:2f0e1e23c242 | 2250 | dp->obj.objsize = obj->c_size & 0xFFFFFF00; |
glebiuskv | 0:2f0e1e23c242 | 2251 | dp->obj.n_frag = 0; |
glebiuskv | 0:2f0e1e23c242 | 2252 | dp->blk_ofs = obj->c_ofs; |
glebiuskv | 0:2f0e1e23c242 | 2253 | |
glebiuskv | 0:2f0e1e23c242 | 2254 | res = dir_sdi(dp, dp->blk_ofs); /* Goto object's entry block */ |
glebiuskv | 0:2f0e1e23c242 | 2255 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 2256 | res = load_xdir(dp); /* Load the object's entry block */ |
glebiuskv | 0:2f0e1e23c242 | 2257 | } |
glebiuskv | 0:2f0e1e23c242 | 2258 | return res; |
glebiuskv | 0:2f0e1e23c242 | 2259 | } |
glebiuskv | 0:2f0e1e23c242 | 2260 | #endif |
glebiuskv | 0:2f0e1e23c242 | 2261 | |
glebiuskv | 0:2f0e1e23c242 | 2262 | |
glebiuskv | 0:2f0e1e23c242 | 2263 | #if !FF_FS_READONLY |
glebiuskv | 0:2f0e1e23c242 | 2264 | /*----------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 2265 | /* exFAT: Store the directory entry block */ |
glebiuskv | 0:2f0e1e23c242 | 2266 | /*----------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 2267 | static |
glebiuskv | 0:2f0e1e23c242 | 2268 | FRESULT store_xdir ( |
glebiuskv | 0:2f0e1e23c242 | 2269 | FATFS_DIR* dp /* Pointer to the direcotry object */ |
glebiuskv | 0:2f0e1e23c242 | 2270 | ) |
glebiuskv | 0:2f0e1e23c242 | 2271 | { |
glebiuskv | 0:2f0e1e23c242 | 2272 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 2273 | UINT nent; |
glebiuskv | 0:2f0e1e23c242 | 2274 | BYTE* dirb = dp->obj.fs->dirbuf; /* Pointer to the direcotry entry block 85+C0+C1s */ |
glebiuskv | 0:2f0e1e23c242 | 2275 | |
glebiuskv | 0:2f0e1e23c242 | 2276 | /* Create set sum */ |
glebiuskv | 0:2f0e1e23c242 | 2277 | st_word(dirb + XDIR_SetSum, xdir_sum(dirb)); |
glebiuskv | 0:2f0e1e23c242 | 2278 | nent = dirb[XDIR_NumSec] + 1; |
glebiuskv | 0:2f0e1e23c242 | 2279 | |
glebiuskv | 0:2f0e1e23c242 | 2280 | /* Store the direcotry entry block to the directory */ |
glebiuskv | 0:2f0e1e23c242 | 2281 | res = dir_sdi(dp, dp->blk_ofs); |
glebiuskv | 0:2f0e1e23c242 | 2282 | while (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 2283 | res = move_window(dp->obj.fs, dp->sect); |
glebiuskv | 0:2f0e1e23c242 | 2284 | if (res != FR_OK) break; |
glebiuskv | 0:2f0e1e23c242 | 2285 | mem_cpy(dp->dir, dirb, SZDIRE); |
glebiuskv | 0:2f0e1e23c242 | 2286 | dp->obj.fs->wflag = 1; |
glebiuskv | 0:2f0e1e23c242 | 2287 | if (--nent == 0) break; |
glebiuskv | 0:2f0e1e23c242 | 2288 | dirb += SZDIRE; |
glebiuskv | 0:2f0e1e23c242 | 2289 | res = dir_next(dp, 0); |
glebiuskv | 0:2f0e1e23c242 | 2290 | } |
glebiuskv | 0:2f0e1e23c242 | 2291 | return (res == FR_OK || res == FR_DISK_ERR) ? res : FR_INT_ERR; |
glebiuskv | 0:2f0e1e23c242 | 2292 | } |
glebiuskv | 0:2f0e1e23c242 | 2293 | |
glebiuskv | 0:2f0e1e23c242 | 2294 | |
glebiuskv | 0:2f0e1e23c242 | 2295 | |
glebiuskv | 0:2f0e1e23c242 | 2296 | /*-------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 2297 | /* exFAT: Create a new directory enrty block */ |
glebiuskv | 0:2f0e1e23c242 | 2298 | /*-------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 2299 | |
glebiuskv | 0:2f0e1e23c242 | 2300 | static |
glebiuskv | 0:2f0e1e23c242 | 2301 | void create_xdir ( |
glebiuskv | 0:2f0e1e23c242 | 2302 | BYTE* dirb, /* Pointer to the direcotry entry block buffer */ |
glebiuskv | 0:2f0e1e23c242 | 2303 | const WCHAR* lfn /* Pointer to the object name */ |
glebiuskv | 0:2f0e1e23c242 | 2304 | ) |
glebiuskv | 0:2f0e1e23c242 | 2305 | { |
glebiuskv | 0:2f0e1e23c242 | 2306 | UINT i; |
glebiuskv | 0:2f0e1e23c242 | 2307 | BYTE nc1, nlen; |
glebiuskv | 0:2f0e1e23c242 | 2308 | WCHAR wc; |
glebiuskv | 0:2f0e1e23c242 | 2309 | |
glebiuskv | 0:2f0e1e23c242 | 2310 | |
glebiuskv | 0:2f0e1e23c242 | 2311 | /* Create 85,C0 entry */ |
glebiuskv | 0:2f0e1e23c242 | 2312 | mem_set(dirb, 0, 2 * SZDIRE); |
glebiuskv | 0:2f0e1e23c242 | 2313 | dirb[0 * SZDIRE + XDIR_Type] = 0x85; /* 85 entry */ |
glebiuskv | 0:2f0e1e23c242 | 2314 | dirb[1 * SZDIRE + XDIR_Type] = 0xC0; /* C0 entry */ |
glebiuskv | 0:2f0e1e23c242 | 2315 | |
glebiuskv | 0:2f0e1e23c242 | 2316 | /* Create C1 entries */ |
glebiuskv | 0:2f0e1e23c242 | 2317 | i = SZDIRE * 2; /* Top of C1 entries */ |
glebiuskv | 0:2f0e1e23c242 | 2318 | nlen = nc1 = 0; wc = 1; |
glebiuskv | 0:2f0e1e23c242 | 2319 | do { |
glebiuskv | 0:2f0e1e23c242 | 2320 | dirb[i++] = 0xC1; dirb[i++] = 0; /* Entry type C1 */ |
glebiuskv | 0:2f0e1e23c242 | 2321 | do { /* Fill name field */ |
glebiuskv | 0:2f0e1e23c242 | 2322 | if (wc != 0 && (wc = lfn[nlen]) != 0) nlen++; /* Get a character if exist */ |
glebiuskv | 0:2f0e1e23c242 | 2323 | st_word(dirb + i, wc); /* Store it */ |
glebiuskv | 0:2f0e1e23c242 | 2324 | i += 2; |
glebiuskv | 0:2f0e1e23c242 | 2325 | } while (i % SZDIRE != 0); |
glebiuskv | 0:2f0e1e23c242 | 2326 | nc1++; |
glebiuskv | 0:2f0e1e23c242 | 2327 | } while (lfn[nlen]); /* Fill next entry if any char follows */ |
glebiuskv | 0:2f0e1e23c242 | 2328 | |
glebiuskv | 0:2f0e1e23c242 | 2329 | dirb[XDIR_NumName] = nlen; /* Set name length */ |
glebiuskv | 0:2f0e1e23c242 | 2330 | dirb[XDIR_NumSec] = 1 + nc1; /* Set secondary count (C0 + C1s) */ |
glebiuskv | 0:2f0e1e23c242 | 2331 | st_word(dirb + XDIR_NameHash, xname_sum(lfn)); /* Set name hash */ |
glebiuskv | 0:2f0e1e23c242 | 2332 | } |
glebiuskv | 0:2f0e1e23c242 | 2333 | |
glebiuskv | 0:2f0e1e23c242 | 2334 | #endif /* !FF_FS_READONLY */ |
glebiuskv | 0:2f0e1e23c242 | 2335 | #endif /* FF_FS_EXFAT */ |
glebiuskv | 0:2f0e1e23c242 | 2336 | |
glebiuskv | 0:2f0e1e23c242 | 2337 | |
glebiuskv | 0:2f0e1e23c242 | 2338 | |
glebiuskv | 0:2f0e1e23c242 | 2339 | #if FF_FS_MINIMIZE <= 1 || FF_FS_RPATH >= 2 || FF_USE_LABEL || FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 2340 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 2341 | /* Read an object from the directory */ |
glebiuskv | 0:2f0e1e23c242 | 2342 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 2343 | |
glebiuskv | 0:2f0e1e23c242 | 2344 | #define dir_read_file(dp) dir_read(dp, 0) |
glebiuskv | 0:2f0e1e23c242 | 2345 | #define dir_read_label(dp) dir_read(dp, 1) |
glebiuskv | 0:2f0e1e23c242 | 2346 | |
glebiuskv | 0:2f0e1e23c242 | 2347 | static |
glebiuskv | 0:2f0e1e23c242 | 2348 | FRESULT dir_read ( |
glebiuskv | 0:2f0e1e23c242 | 2349 | FATFS_DIR* dp, /* Pointer to the directory object */ |
glebiuskv | 0:2f0e1e23c242 | 2350 | int vol /* Filtered by 0:file/directory or 1:volume label */ |
glebiuskv | 0:2f0e1e23c242 | 2351 | ) |
glebiuskv | 0:2f0e1e23c242 | 2352 | { |
glebiuskv | 0:2f0e1e23c242 | 2353 | FRESULT res = FR_NO_FILE; |
glebiuskv | 0:2f0e1e23c242 | 2354 | FATFS *fs = dp->obj.fs; |
glebiuskv | 0:2f0e1e23c242 | 2355 | BYTE a, c; |
glebiuskv | 0:2f0e1e23c242 | 2356 | #if FF_USE_LFN |
glebiuskv | 0:2f0e1e23c242 | 2357 | BYTE ord = 0xFF, sum = 0xFF; |
glebiuskv | 0:2f0e1e23c242 | 2358 | #endif |
glebiuskv | 0:2f0e1e23c242 | 2359 | |
glebiuskv | 0:2f0e1e23c242 | 2360 | while (dp->sect) { |
glebiuskv | 0:2f0e1e23c242 | 2361 | res = move_window(fs, dp->sect); |
glebiuskv | 0:2f0e1e23c242 | 2362 | if (res != FR_OK) break; |
glebiuskv | 0:2f0e1e23c242 | 2363 | c = dp->dir[DIR_Name]; /* Test for the entry type */ |
glebiuskv | 0:2f0e1e23c242 | 2364 | if (c == 0) { |
glebiuskv | 0:2f0e1e23c242 | 2365 | res = FR_NO_FILE; break; /* Reached to end of the directory */ |
glebiuskv | 0:2f0e1e23c242 | 2366 | } |
glebiuskv | 0:2f0e1e23c242 | 2367 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 2368 | if (fs->fs_type == FS_EXFAT) { /* On the exFAT volume */ |
glebiuskv | 0:2f0e1e23c242 | 2369 | if (FF_USE_LABEL && vol) { |
glebiuskv | 0:2f0e1e23c242 | 2370 | if (c == 0x83) break; /* Volume label entry? */ |
glebiuskv | 0:2f0e1e23c242 | 2371 | } else { |
glebiuskv | 0:2f0e1e23c242 | 2372 | if (c == 0x85) { /* Start of the file entry block? */ |
glebiuskv | 0:2f0e1e23c242 | 2373 | dp->blk_ofs = dp->dptr; /* Get location of the block */ |
glebiuskv | 0:2f0e1e23c242 | 2374 | res = load_xdir(dp); /* Load the entry block */ |
glebiuskv | 0:2f0e1e23c242 | 2375 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 2376 | dp->obj.attr = fs->dirbuf[XDIR_Attr] & AM_MASK; /* Get attribute */ |
glebiuskv | 0:2f0e1e23c242 | 2377 | } |
glebiuskv | 0:2f0e1e23c242 | 2378 | break; |
glebiuskv | 0:2f0e1e23c242 | 2379 | } |
glebiuskv | 0:2f0e1e23c242 | 2380 | } |
glebiuskv | 0:2f0e1e23c242 | 2381 | } else |
glebiuskv | 0:2f0e1e23c242 | 2382 | #endif |
glebiuskv | 0:2f0e1e23c242 | 2383 | { /* On the FAT/FAT32 volume */ |
glebiuskv | 0:2f0e1e23c242 | 2384 | dp->obj.attr = a = dp->dir[DIR_Attr] & AM_MASK; /* Get attribute */ |
glebiuskv | 0:2f0e1e23c242 | 2385 | #if FF_USE_LFN /* LFN configuration */ |
glebiuskv | 0:2f0e1e23c242 | 2386 | if (c == DDEM || c == '.' || (int)((a & ~AM_ARC) == AM_VOL) != vol) { /* An entry without valid data */ |
glebiuskv | 0:2f0e1e23c242 | 2387 | ord = 0xFF; |
glebiuskv | 0:2f0e1e23c242 | 2388 | } else { |
glebiuskv | 0:2f0e1e23c242 | 2389 | if (a == AM_LFN) { /* An LFN entry is found */ |
glebiuskv | 0:2f0e1e23c242 | 2390 | if (c & LLEF) { /* Is it start of an LFN sequence? */ |
glebiuskv | 0:2f0e1e23c242 | 2391 | sum = dp->dir[LDIR_Chksum]; |
glebiuskv | 0:2f0e1e23c242 | 2392 | c &= (BYTE)~LLEF; ord = c; |
glebiuskv | 0:2f0e1e23c242 | 2393 | dp->blk_ofs = dp->dptr; |
glebiuskv | 0:2f0e1e23c242 | 2394 | } |
glebiuskv | 0:2f0e1e23c242 | 2395 | /* Check LFN validity and capture it */ |
glebiuskv | 0:2f0e1e23c242 | 2396 | ord = (c == ord && sum == dp->dir[LDIR_Chksum] && pick_lfn(fs->lfnbuf, dp->dir)) ? ord - 1 : 0xFF; |
glebiuskv | 0:2f0e1e23c242 | 2397 | } else { /* An SFN entry is found */ |
glebiuskv | 0:2f0e1e23c242 | 2398 | if (ord != 0 || sum != sum_sfn(dp->dir)) { /* Is there a valid LFN? */ |
glebiuskv | 0:2f0e1e23c242 | 2399 | dp->blk_ofs = 0xFFFFFFFF; /* It has no LFN. */ |
glebiuskv | 0:2f0e1e23c242 | 2400 | } |
glebiuskv | 0:2f0e1e23c242 | 2401 | break; |
glebiuskv | 0:2f0e1e23c242 | 2402 | } |
glebiuskv | 0:2f0e1e23c242 | 2403 | } |
glebiuskv | 0:2f0e1e23c242 | 2404 | #else /* Non LFN configuration */ |
glebiuskv | 0:2f0e1e23c242 | 2405 | if (c != DDEM && c != '.' && a != AM_LFN && (int)((a & ~AM_ARC) == AM_VOL) == vol) { /* Is it a valid entry? */ |
glebiuskv | 0:2f0e1e23c242 | 2406 | break; |
glebiuskv | 0:2f0e1e23c242 | 2407 | } |
glebiuskv | 0:2f0e1e23c242 | 2408 | #endif |
glebiuskv | 0:2f0e1e23c242 | 2409 | } |
glebiuskv | 0:2f0e1e23c242 | 2410 | res = dir_next(dp, 0); /* Next entry */ |
glebiuskv | 0:2f0e1e23c242 | 2411 | if (res != FR_OK) break; |
glebiuskv | 0:2f0e1e23c242 | 2412 | } |
glebiuskv | 0:2f0e1e23c242 | 2413 | |
glebiuskv | 0:2f0e1e23c242 | 2414 | if (res != FR_OK) dp->sect = 0; /* Terminate the read operation on error or EOT */ |
glebiuskv | 0:2f0e1e23c242 | 2415 | return res; |
glebiuskv | 0:2f0e1e23c242 | 2416 | } |
glebiuskv | 0:2f0e1e23c242 | 2417 | |
glebiuskv | 0:2f0e1e23c242 | 2418 | #endif /* FF_FS_MINIMIZE <= 1 || FF_USE_LABEL || FF_FS_RPATH >= 2 */ |
glebiuskv | 0:2f0e1e23c242 | 2419 | |
glebiuskv | 0:2f0e1e23c242 | 2420 | |
glebiuskv | 0:2f0e1e23c242 | 2421 | |
glebiuskv | 0:2f0e1e23c242 | 2422 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 2423 | /* Directory handling - Find an object in the directory */ |
glebiuskv | 0:2f0e1e23c242 | 2424 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 2425 | |
glebiuskv | 0:2f0e1e23c242 | 2426 | static |
glebiuskv | 0:2f0e1e23c242 | 2427 | FRESULT dir_find ( /* FR_OK(0):succeeded, !=0:error */ |
glebiuskv | 0:2f0e1e23c242 | 2428 | FATFS_DIR* dp /* Pointer to the directory object with the file name */ |
glebiuskv | 0:2f0e1e23c242 | 2429 | ) |
glebiuskv | 0:2f0e1e23c242 | 2430 | { |
glebiuskv | 0:2f0e1e23c242 | 2431 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 2432 | FATFS *fs = dp->obj.fs; |
glebiuskv | 0:2f0e1e23c242 | 2433 | BYTE c; |
glebiuskv | 0:2f0e1e23c242 | 2434 | #if FF_USE_LFN |
glebiuskv | 0:2f0e1e23c242 | 2435 | BYTE a, ord, sum; |
glebiuskv | 0:2f0e1e23c242 | 2436 | #endif |
glebiuskv | 0:2f0e1e23c242 | 2437 | |
glebiuskv | 0:2f0e1e23c242 | 2438 | res = dir_sdi(dp, 0); /* Rewind directory object */ |
glebiuskv | 0:2f0e1e23c242 | 2439 | if (res != FR_OK) return res; |
glebiuskv | 0:2f0e1e23c242 | 2440 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 2441 | if (fs->fs_type == FS_EXFAT) { /* On the exFAT volume */ |
glebiuskv | 0:2f0e1e23c242 | 2442 | BYTE nc; |
glebiuskv | 0:2f0e1e23c242 | 2443 | UINT di, ni; |
glebiuskv | 0:2f0e1e23c242 | 2444 | WORD hash = xname_sum(fs->lfnbuf); /* Hash value of the name to find */ |
glebiuskv | 0:2f0e1e23c242 | 2445 | |
glebiuskv | 0:2f0e1e23c242 | 2446 | while ((res = dir_read_file(dp)) == FR_OK) { /* Read an item */ |
glebiuskv | 0:2f0e1e23c242 | 2447 | #if FF_MAX_LFN < 255 |
glebiuskv | 0:2f0e1e23c242 | 2448 | if (fs->dirbuf[XDIR_NumName] > FF_MAX_LFN) continue; /* Skip comparison if inaccessible object name */ |
glebiuskv | 0:2f0e1e23c242 | 2449 | #endif |
glebiuskv | 0:2f0e1e23c242 | 2450 | if (ld_word(fs->dirbuf + XDIR_NameHash) != hash) continue; /* Skip comparison if hash mismatched */ |
glebiuskv | 0:2f0e1e23c242 | 2451 | for (nc = fs->dirbuf[XDIR_NumName], di = SZDIRE * 2, ni = 0; nc; nc--, di += 2, ni++) { /* Compare the name */ |
glebiuskv | 0:2f0e1e23c242 | 2452 | if ((di % SZDIRE) == 0) di += 2; |
glebiuskv | 0:2f0e1e23c242 | 2453 | if (ff_wtoupper(ld_word(fs->dirbuf + di)) != ff_wtoupper(fs->lfnbuf[ni])) break; |
glebiuskv | 0:2f0e1e23c242 | 2454 | } |
glebiuskv | 0:2f0e1e23c242 | 2455 | if (nc == 0 && !fs->lfnbuf[ni]) break; /* Name matched? */ |
glebiuskv | 0:2f0e1e23c242 | 2456 | } |
glebiuskv | 0:2f0e1e23c242 | 2457 | return res; |
glebiuskv | 0:2f0e1e23c242 | 2458 | } |
glebiuskv | 0:2f0e1e23c242 | 2459 | #endif |
glebiuskv | 0:2f0e1e23c242 | 2460 | /* On the FAT/FAT32 volume */ |
glebiuskv | 0:2f0e1e23c242 | 2461 | #if FF_USE_LFN |
glebiuskv | 0:2f0e1e23c242 | 2462 | ord = sum = 0xFF; dp->blk_ofs = 0xFFFFFFFF; /* Reset LFN sequence */ |
glebiuskv | 0:2f0e1e23c242 | 2463 | #endif |
glebiuskv | 0:2f0e1e23c242 | 2464 | do { |
glebiuskv | 0:2f0e1e23c242 | 2465 | res = move_window(fs, dp->sect); |
glebiuskv | 0:2f0e1e23c242 | 2466 | if (res != FR_OK) break; |
glebiuskv | 0:2f0e1e23c242 | 2467 | c = dp->dir[DIR_Name]; |
glebiuskv | 0:2f0e1e23c242 | 2468 | if (c == 0) { res = FR_NO_FILE; break; } /* Reached to end of table */ |
glebiuskv | 0:2f0e1e23c242 | 2469 | #if FF_USE_LFN /* LFN configuration */ |
glebiuskv | 0:2f0e1e23c242 | 2470 | dp->obj.attr = a = dp->dir[DIR_Attr] & AM_MASK; |
glebiuskv | 0:2f0e1e23c242 | 2471 | if (c == DDEM || ((a & AM_VOL) && a != AM_LFN)) { /* An entry without valid data */ |
glebiuskv | 0:2f0e1e23c242 | 2472 | ord = 0xFF; dp->blk_ofs = 0xFFFFFFFF; /* Reset LFN sequence */ |
glebiuskv | 0:2f0e1e23c242 | 2473 | } else { |
glebiuskv | 0:2f0e1e23c242 | 2474 | if (a == AM_LFN) { /* An LFN entry is found */ |
glebiuskv | 0:2f0e1e23c242 | 2475 | if (!(dp->fn[NSFLAG] & NS_NOLFN)) { |
glebiuskv | 0:2f0e1e23c242 | 2476 | if (c & LLEF) { /* Is it start of LFN sequence? */ |
glebiuskv | 0:2f0e1e23c242 | 2477 | sum = dp->dir[LDIR_Chksum]; |
glebiuskv | 0:2f0e1e23c242 | 2478 | c &= (BYTE)~LLEF; ord = c; /* LFN start order */ |
glebiuskv | 0:2f0e1e23c242 | 2479 | dp->blk_ofs = dp->dptr; /* Start offset of LFN */ |
glebiuskv | 0:2f0e1e23c242 | 2480 | } |
glebiuskv | 0:2f0e1e23c242 | 2481 | /* Check validity of the LFN entry and compare it with given name */ |
glebiuskv | 0:2f0e1e23c242 | 2482 | ord = (c == ord && sum == dp->dir[LDIR_Chksum] && cmp_lfn(fs->lfnbuf, dp->dir)) ? ord - 1 : 0xFF; |
glebiuskv | 0:2f0e1e23c242 | 2483 | } |
glebiuskv | 0:2f0e1e23c242 | 2484 | } else { /* An SFN entry is found */ |
glebiuskv | 0:2f0e1e23c242 | 2485 | if (ord == 0 && sum == sum_sfn(dp->dir)) break; /* LFN matched? */ |
glebiuskv | 0:2f0e1e23c242 | 2486 | if (!(dp->fn[NSFLAG] & NS_LOSS) && !mem_cmp(dp->dir, dp->fn, 11)) break; /* SFN matched? */ |
glebiuskv | 0:2f0e1e23c242 | 2487 | ord = 0xFF; dp->blk_ofs = 0xFFFFFFFF; /* Reset LFN sequence */ |
glebiuskv | 0:2f0e1e23c242 | 2488 | } |
glebiuskv | 0:2f0e1e23c242 | 2489 | } |
glebiuskv | 0:2f0e1e23c242 | 2490 | #else /* Non LFN configuration */ |
glebiuskv | 0:2f0e1e23c242 | 2491 | dp->obj.attr = dp->dir[DIR_Attr] & AM_MASK; |
glebiuskv | 0:2f0e1e23c242 | 2492 | if (!(dp->dir[DIR_Attr] & AM_VOL) && !mem_cmp(dp->dir, dp->fn, 11)) break; /* Is it a valid entry? */ |
glebiuskv | 0:2f0e1e23c242 | 2493 | #endif |
glebiuskv | 0:2f0e1e23c242 | 2494 | res = dir_next(dp, 0); /* Next entry */ |
glebiuskv | 0:2f0e1e23c242 | 2495 | } while (res == FR_OK); |
glebiuskv | 0:2f0e1e23c242 | 2496 | |
glebiuskv | 0:2f0e1e23c242 | 2497 | return res; |
glebiuskv | 0:2f0e1e23c242 | 2498 | } |
glebiuskv | 0:2f0e1e23c242 | 2499 | |
glebiuskv | 0:2f0e1e23c242 | 2500 | |
glebiuskv | 0:2f0e1e23c242 | 2501 | |
glebiuskv | 0:2f0e1e23c242 | 2502 | |
glebiuskv | 0:2f0e1e23c242 | 2503 | #if !FF_FS_READONLY |
glebiuskv | 0:2f0e1e23c242 | 2504 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 2505 | /* Register an object to the directory */ |
glebiuskv | 0:2f0e1e23c242 | 2506 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 2507 | |
glebiuskv | 0:2f0e1e23c242 | 2508 | static |
glebiuskv | 0:2f0e1e23c242 | 2509 | FRESULT dir_register ( /* FR_OK:succeeded, FR_DENIED:no free entry or too many SFN collision, FR_DISK_ERR:disk error */ |
glebiuskv | 0:2f0e1e23c242 | 2510 | FATFS_DIR* dp /* Target directory with object name to be created */ |
glebiuskv | 0:2f0e1e23c242 | 2511 | ) |
glebiuskv | 0:2f0e1e23c242 | 2512 | { |
glebiuskv | 0:2f0e1e23c242 | 2513 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 2514 | FATFS *fs = dp->obj.fs; |
glebiuskv | 0:2f0e1e23c242 | 2515 | #if FF_USE_LFN /* LFN configuration */ |
glebiuskv | 0:2f0e1e23c242 | 2516 | UINT n, nlen, nent; |
glebiuskv | 0:2f0e1e23c242 | 2517 | BYTE sn[12], sum; |
glebiuskv | 0:2f0e1e23c242 | 2518 | |
glebiuskv | 0:2f0e1e23c242 | 2519 | |
glebiuskv | 0:2f0e1e23c242 | 2520 | if (dp->fn[NSFLAG] & (NS_DOT | NS_NONAME)) return FR_INVALID_NAME; /* Check name validity */ |
glebiuskv | 0:2f0e1e23c242 | 2521 | for (nlen = 0; fs->lfnbuf[nlen]; nlen++) ; /* Get lfn length */ |
glebiuskv | 0:2f0e1e23c242 | 2522 | |
glebiuskv | 0:2f0e1e23c242 | 2523 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 2524 | if (fs->fs_type == FS_EXFAT) { /* On the exFAT volume */ |
glebiuskv | 0:2f0e1e23c242 | 2525 | nent = (nlen + 14) / 15 + 2; /* Number of entries to allocate (85+C0+C1s) */ |
glebiuskv | 0:2f0e1e23c242 | 2526 | res = dir_alloc(dp, nent); /* Allocate entries */ |
glebiuskv | 0:2f0e1e23c242 | 2527 | if (res != FR_OK) return res; |
glebiuskv | 0:2f0e1e23c242 | 2528 | dp->blk_ofs = dp->dptr - SZDIRE * (nent - 1); /* Set the allocated entry block offset */ |
glebiuskv | 0:2f0e1e23c242 | 2529 | |
glebiuskv | 0:2f0e1e23c242 | 2530 | if (dp->obj.stat & 4) { /* Has the directory been stretched? */ |
glebiuskv | 0:2f0e1e23c242 | 2531 | dp->obj.stat &= ~4; |
glebiuskv | 0:2f0e1e23c242 | 2532 | res = fill_first_frag(&dp->obj); /* Fill the first fragment on the FAT if needed */ |
glebiuskv | 0:2f0e1e23c242 | 2533 | if (res != FR_OK) return res; |
glebiuskv | 0:2f0e1e23c242 | 2534 | res = fill_last_frag(&dp->obj, dp->clust, 0xFFFFFFFF); /* Fill the last fragment on the FAT if needed */ |
glebiuskv | 0:2f0e1e23c242 | 2535 | if (res != FR_OK) return res; |
glebiuskv | 0:2f0e1e23c242 | 2536 | if (dp->obj.sclust != 0) { /* Is it a sub directory? */ |
glebiuskv | 0:2f0e1e23c242 | 2537 | FATFS_DIR dj; |
glebiuskv | 0:2f0e1e23c242 | 2538 | |
glebiuskv | 0:2f0e1e23c242 | 2539 | res = load_obj_xdir(&dj, &dp->obj); /* Load the object status */ |
glebiuskv | 0:2f0e1e23c242 | 2540 | if (res != FR_OK) return res; |
glebiuskv | 0:2f0e1e23c242 | 2541 | dp->obj.objsize += (DWORD)fs->csize * SS(fs); /* Increase the directory size by cluster size */ |
glebiuskv | 0:2f0e1e23c242 | 2542 | st_qword(fs->dirbuf + XDIR_FileSize, dp->obj.objsize); /* Update the allocation status */ |
glebiuskv | 0:2f0e1e23c242 | 2543 | st_qword(fs->dirbuf + XDIR_ValidFileSize, dp->obj.objsize); |
glebiuskv | 0:2f0e1e23c242 | 2544 | fs->dirbuf[XDIR_GenFlags] = dp->obj.stat | 1; |
glebiuskv | 0:2f0e1e23c242 | 2545 | res = store_xdir(&dj); /* Store the object status */ |
glebiuskv | 0:2f0e1e23c242 | 2546 | if (res != FR_OK) return res; |
glebiuskv | 0:2f0e1e23c242 | 2547 | } |
glebiuskv | 0:2f0e1e23c242 | 2548 | } |
glebiuskv | 0:2f0e1e23c242 | 2549 | |
glebiuskv | 0:2f0e1e23c242 | 2550 | create_xdir(fs->dirbuf, fs->lfnbuf); /* Create on-memory directory block to be written later */ |
glebiuskv | 0:2f0e1e23c242 | 2551 | return FR_OK; |
glebiuskv | 0:2f0e1e23c242 | 2552 | } |
glebiuskv | 0:2f0e1e23c242 | 2553 | #endif |
glebiuskv | 0:2f0e1e23c242 | 2554 | /* On the FAT/FAT32 volume */ |
glebiuskv | 0:2f0e1e23c242 | 2555 | mem_cpy(sn, dp->fn, 12); |
glebiuskv | 0:2f0e1e23c242 | 2556 | if (sn[NSFLAG] & NS_LOSS) { /* When LFN is out of 8.3 format, generate a numbered name */ |
glebiuskv | 0:2f0e1e23c242 | 2557 | dp->fn[NSFLAG] = NS_NOLFN; /* Find only SFN */ |
glebiuskv | 0:2f0e1e23c242 | 2558 | for (n = 1; n < 100; n++) { |
glebiuskv | 0:2f0e1e23c242 | 2559 | gen_numname(dp->fn, sn, fs->lfnbuf, n); /* Generate a numbered name */ |
glebiuskv | 0:2f0e1e23c242 | 2560 | res = dir_find(dp); /* Check if the name collides with existing SFN */ |
glebiuskv | 0:2f0e1e23c242 | 2561 | if (res != FR_OK) break; |
glebiuskv | 0:2f0e1e23c242 | 2562 | } |
glebiuskv | 0:2f0e1e23c242 | 2563 | if (n == 100) return FR_DENIED; /* Abort if too many collisions */ |
glebiuskv | 0:2f0e1e23c242 | 2564 | if (res != FR_NO_FILE) return res; /* Abort if the result is other than 'not collided' */ |
glebiuskv | 0:2f0e1e23c242 | 2565 | dp->fn[NSFLAG] = sn[NSFLAG]; |
glebiuskv | 0:2f0e1e23c242 | 2566 | } |
glebiuskv | 0:2f0e1e23c242 | 2567 | |
glebiuskv | 0:2f0e1e23c242 | 2568 | /* Create an SFN with/without LFNs. */ |
glebiuskv | 0:2f0e1e23c242 | 2569 | nent = (sn[NSFLAG] & NS_LFN) ? (nlen + 12) / 13 + 1 : 1; /* Number of entries to allocate */ |
glebiuskv | 0:2f0e1e23c242 | 2570 | res = dir_alloc(dp, nent); /* Allocate entries */ |
glebiuskv | 0:2f0e1e23c242 | 2571 | if (res == FR_OK && --nent) { /* Set LFN entry if needed */ |
glebiuskv | 0:2f0e1e23c242 | 2572 | res = dir_sdi(dp, dp->dptr - nent * SZDIRE); |
glebiuskv | 0:2f0e1e23c242 | 2573 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 2574 | sum = sum_sfn(dp->fn); /* Checksum value of the SFN tied to the LFN */ |
glebiuskv | 0:2f0e1e23c242 | 2575 | do { /* Store LFN entries in bottom first */ |
glebiuskv | 0:2f0e1e23c242 | 2576 | res = move_window(fs, dp->sect); |
glebiuskv | 0:2f0e1e23c242 | 2577 | if (res != FR_OK) break; |
glebiuskv | 0:2f0e1e23c242 | 2578 | put_lfn(fs->lfnbuf, dp->dir, (BYTE)nent, sum); |
glebiuskv | 0:2f0e1e23c242 | 2579 | fs->wflag = 1; |
glebiuskv | 0:2f0e1e23c242 | 2580 | res = dir_next(dp, 0); /* Next entry */ |
glebiuskv | 0:2f0e1e23c242 | 2581 | } while (res == FR_OK && --nent); |
glebiuskv | 0:2f0e1e23c242 | 2582 | } |
glebiuskv | 0:2f0e1e23c242 | 2583 | } |
glebiuskv | 0:2f0e1e23c242 | 2584 | |
glebiuskv | 0:2f0e1e23c242 | 2585 | #else /* Non LFN configuration */ |
glebiuskv | 0:2f0e1e23c242 | 2586 | res = dir_alloc(dp, 1); /* Allocate an entry for SFN */ |
glebiuskv | 0:2f0e1e23c242 | 2587 | |
glebiuskv | 0:2f0e1e23c242 | 2588 | #endif |
glebiuskv | 0:2f0e1e23c242 | 2589 | |
glebiuskv | 0:2f0e1e23c242 | 2590 | /* Set SFN entry */ |
glebiuskv | 0:2f0e1e23c242 | 2591 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 2592 | res = move_window(fs, dp->sect); |
glebiuskv | 0:2f0e1e23c242 | 2593 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 2594 | mem_set(dp->dir, 0, SZDIRE); /* Clean the entry */ |
glebiuskv | 0:2f0e1e23c242 | 2595 | mem_cpy(dp->dir + DIR_Name, dp->fn, 11); /* Put SFN */ |
glebiuskv | 0:2f0e1e23c242 | 2596 | #if FF_USE_LFN |
glebiuskv | 0:2f0e1e23c242 | 2597 | dp->dir[DIR_NTres] = dp->fn[NSFLAG] & (NS_BODY | NS_EXT); /* Put NT flag */ |
glebiuskv | 0:2f0e1e23c242 | 2598 | #endif |
glebiuskv | 0:2f0e1e23c242 | 2599 | fs->wflag = 1; |
glebiuskv | 0:2f0e1e23c242 | 2600 | } |
glebiuskv | 0:2f0e1e23c242 | 2601 | } |
glebiuskv | 0:2f0e1e23c242 | 2602 | |
glebiuskv | 0:2f0e1e23c242 | 2603 | return res; |
glebiuskv | 0:2f0e1e23c242 | 2604 | } |
glebiuskv | 0:2f0e1e23c242 | 2605 | |
glebiuskv | 0:2f0e1e23c242 | 2606 | #endif /* !FF_FS_READONLY */ |
glebiuskv | 0:2f0e1e23c242 | 2607 | |
glebiuskv | 0:2f0e1e23c242 | 2608 | |
glebiuskv | 0:2f0e1e23c242 | 2609 | |
glebiuskv | 0:2f0e1e23c242 | 2610 | #if !FF_FS_READONLY && FF_FS_MINIMIZE == 0 |
glebiuskv | 0:2f0e1e23c242 | 2611 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 2612 | /* Remove an object from the directory */ |
glebiuskv | 0:2f0e1e23c242 | 2613 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 2614 | |
glebiuskv | 0:2f0e1e23c242 | 2615 | static |
glebiuskv | 0:2f0e1e23c242 | 2616 | FRESULT dir_remove ( /* FR_OK:Succeeded, FR_DISK_ERR:A disk error */ |
glebiuskv | 0:2f0e1e23c242 | 2617 | FATFS_DIR* dp /* Directory object pointing the entry to be removed */ |
glebiuskv | 0:2f0e1e23c242 | 2618 | ) |
glebiuskv | 0:2f0e1e23c242 | 2619 | { |
glebiuskv | 0:2f0e1e23c242 | 2620 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 2621 | FATFS *fs = dp->obj.fs; |
glebiuskv | 0:2f0e1e23c242 | 2622 | #if FF_USE_LFN /* LFN configuration */ |
glebiuskv | 0:2f0e1e23c242 | 2623 | DWORD last = dp->dptr; |
glebiuskv | 0:2f0e1e23c242 | 2624 | |
glebiuskv | 0:2f0e1e23c242 | 2625 | res = (dp->blk_ofs == 0xFFFFFFFF) ? FR_OK : dir_sdi(dp, dp->blk_ofs); /* Goto top of the entry block if LFN is exist */ |
glebiuskv | 0:2f0e1e23c242 | 2626 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 2627 | do { |
glebiuskv | 0:2f0e1e23c242 | 2628 | res = move_window(fs, dp->sect); |
glebiuskv | 0:2f0e1e23c242 | 2629 | if (res != FR_OK) break; |
glebiuskv | 0:2f0e1e23c242 | 2630 | if (FF_FS_EXFAT && fs->fs_type == FS_EXFAT) { /* On the exFAT volume */ |
glebiuskv | 0:2f0e1e23c242 | 2631 | dp->dir[XDIR_Type] &= 0x7F; /* Clear the entry InUse flag. */ |
glebiuskv | 0:2f0e1e23c242 | 2632 | } else { /* On the FAT/FAT32 volume */ |
glebiuskv | 0:2f0e1e23c242 | 2633 | dp->dir[DIR_Name] = DDEM; /* Mark the entry 'deleted'. */ |
glebiuskv | 0:2f0e1e23c242 | 2634 | } |
glebiuskv | 0:2f0e1e23c242 | 2635 | fs->wflag = 1; |
glebiuskv | 0:2f0e1e23c242 | 2636 | if (dp->dptr >= last) break; /* If reached last entry then all entries of the object has been deleted. */ |
glebiuskv | 0:2f0e1e23c242 | 2637 | res = dir_next(dp, 0); /* Next entry */ |
glebiuskv | 0:2f0e1e23c242 | 2638 | } while (res == FR_OK); |
glebiuskv | 0:2f0e1e23c242 | 2639 | if (res == FR_NO_FILE) res = FR_INT_ERR; |
glebiuskv | 0:2f0e1e23c242 | 2640 | } |
glebiuskv | 0:2f0e1e23c242 | 2641 | #else /* Non LFN configuration */ |
glebiuskv | 0:2f0e1e23c242 | 2642 | |
glebiuskv | 0:2f0e1e23c242 | 2643 | res = move_window(fs, dp->sect); |
glebiuskv | 0:2f0e1e23c242 | 2644 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 2645 | dp->dir[DIR_Name] = DDEM; /* Mark the entry 'deleted'.*/ |
glebiuskv | 0:2f0e1e23c242 | 2646 | fs->wflag = 1; |
glebiuskv | 0:2f0e1e23c242 | 2647 | } |
glebiuskv | 0:2f0e1e23c242 | 2648 | #endif |
glebiuskv | 0:2f0e1e23c242 | 2649 | |
glebiuskv | 0:2f0e1e23c242 | 2650 | return res; |
glebiuskv | 0:2f0e1e23c242 | 2651 | } |
glebiuskv | 0:2f0e1e23c242 | 2652 | |
glebiuskv | 0:2f0e1e23c242 | 2653 | #endif /* !FF_FS_READONLY && FF_FS_MINIMIZE == 0 */ |
glebiuskv | 0:2f0e1e23c242 | 2654 | |
glebiuskv | 0:2f0e1e23c242 | 2655 | |
glebiuskv | 0:2f0e1e23c242 | 2656 | |
glebiuskv | 0:2f0e1e23c242 | 2657 | #if FF_FS_MINIMIZE <= 1 || FF_FS_RPATH >= 2 |
glebiuskv | 0:2f0e1e23c242 | 2658 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 2659 | /* Get file information from directory entry */ |
glebiuskv | 0:2f0e1e23c242 | 2660 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 2661 | |
glebiuskv | 0:2f0e1e23c242 | 2662 | static |
glebiuskv | 0:2f0e1e23c242 | 2663 | void get_fileinfo ( |
glebiuskv | 0:2f0e1e23c242 | 2664 | FATFS_DIR* dp, /* Pointer to the directory object */ |
glebiuskv | 0:2f0e1e23c242 | 2665 | FILINFO* fno /* Pointer to the file information to be filled */ |
glebiuskv | 0:2f0e1e23c242 | 2666 | ) |
glebiuskv | 0:2f0e1e23c242 | 2667 | { |
glebiuskv | 0:2f0e1e23c242 | 2668 | UINT si, di; |
glebiuskv | 0:2f0e1e23c242 | 2669 | #if FF_USE_LFN |
glebiuskv | 0:2f0e1e23c242 | 2670 | WCHAR wc, hs; |
glebiuskv | 0:2f0e1e23c242 | 2671 | FATFS *fs = dp->obj.fs; |
glebiuskv | 0:2f0e1e23c242 | 2672 | #else |
glebiuskv | 0:2f0e1e23c242 | 2673 | TCHAR c; |
glebiuskv | 0:2f0e1e23c242 | 2674 | #endif |
glebiuskv | 0:2f0e1e23c242 | 2675 | |
glebiuskv | 0:2f0e1e23c242 | 2676 | |
glebiuskv | 0:2f0e1e23c242 | 2677 | fno->fname[0] = 0; /* Invaidate file info */ |
glebiuskv | 0:2f0e1e23c242 | 2678 | if (dp->sect == 0) return; /* Exit if read pointer has reached end of directory */ |
glebiuskv | 0:2f0e1e23c242 | 2679 | |
glebiuskv | 0:2f0e1e23c242 | 2680 | #if FF_USE_LFN /* LFN configuration */ |
glebiuskv | 0:2f0e1e23c242 | 2681 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 2682 | if (fs->fs_type == FS_EXFAT) { /* On the exFAT volume */ |
glebiuskv | 0:2f0e1e23c242 | 2683 | get_xfileinfo(fs->dirbuf, fno); |
glebiuskv | 0:2f0e1e23c242 | 2684 | return; |
glebiuskv | 0:2f0e1e23c242 | 2685 | } else |
glebiuskv | 0:2f0e1e23c242 | 2686 | #endif |
glebiuskv | 0:2f0e1e23c242 | 2687 | { /* On the FAT/FAT32 volume */ |
glebiuskv | 0:2f0e1e23c242 | 2688 | if (dp->blk_ofs != 0xFFFFFFFF) { /* Get LFN if available */ |
glebiuskv | 0:2f0e1e23c242 | 2689 | si = di = hs = 0; |
glebiuskv | 0:2f0e1e23c242 | 2690 | while (fs->lfnbuf[si] != 0) { |
glebiuskv | 0:2f0e1e23c242 | 2691 | wc = fs->lfnbuf[si++]; /* Get an LFN character (UTF-16) */ |
glebiuskv | 0:2f0e1e23c242 | 2692 | if (hs == 0 && IsSurrogate(wc)) { /* Is it a surrogate? */ |
glebiuskv | 0:2f0e1e23c242 | 2693 | hs = wc; continue; /* Get low surrogate */ |
glebiuskv | 0:2f0e1e23c242 | 2694 | } |
glebiuskv | 0:2f0e1e23c242 | 2695 | wc = put_utf((DWORD)hs << 16 | wc, &fno->fname[di], FF_LFN_BUF - di); /* Store it in UTF-16 or UTF-8 encoding */ |
glebiuskv | 0:2f0e1e23c242 | 2696 | if (wc == 0) { di = 0; break; } /* Invalid char or buffer overflow? */ |
glebiuskv | 0:2f0e1e23c242 | 2697 | di += wc; |
glebiuskv | 0:2f0e1e23c242 | 2698 | hs = 0; |
glebiuskv | 0:2f0e1e23c242 | 2699 | } |
glebiuskv | 0:2f0e1e23c242 | 2700 | if (hs != 0) di = 0; /* Broken surrogate pair? */ |
glebiuskv | 0:2f0e1e23c242 | 2701 | fno->fname[di] = 0; /* Terminate the LFN (null string means LFN is invalid) */ |
glebiuskv | 0:2f0e1e23c242 | 2702 | } |
glebiuskv | 0:2f0e1e23c242 | 2703 | } |
glebiuskv | 0:2f0e1e23c242 | 2704 | |
glebiuskv | 0:2f0e1e23c242 | 2705 | si = di = 0; |
glebiuskv | 0:2f0e1e23c242 | 2706 | while (si < 11) { /* Get SFN from SFN entry */ |
glebiuskv | 0:2f0e1e23c242 | 2707 | wc = dp->dir[si++]; /* Get a char */ |
glebiuskv | 0:2f0e1e23c242 | 2708 | if (wc == ' ') continue; /* Skip padding spaces */ |
glebiuskv | 0:2f0e1e23c242 | 2709 | if (wc == RDDEM) wc = DDEM; /* Restore replaced DDEM character */ |
glebiuskv | 0:2f0e1e23c242 | 2710 | if (si == 9 && di < FF_SFN_BUF) fno->altname[di++] = '.'; /* Insert a . if extension is exist */ |
glebiuskv | 0:2f0e1e23c242 | 2711 | #if FF_LFN_UNICODE >= 1 /* Unicode output */ |
glebiuskv | 0:2f0e1e23c242 | 2712 | if (dbc_1st((BYTE)wc) && si != 8 && si != 11 && dbc_2nd(dp->dir[si])) { /* Make a DBC if needed */ |
glebiuskv | 0:2f0e1e23c242 | 2713 | wc = wc << 8 | dp->dir[si++]; |
glebiuskv | 0:2f0e1e23c242 | 2714 | } |
glebiuskv | 0:2f0e1e23c242 | 2715 | wc = ff_oem2uni(wc, CODEPAGE); /* ANSI/OEM -> Unicode */ |
glebiuskv | 0:2f0e1e23c242 | 2716 | if (wc == 0) { di = 0; break; } /* Wrong char in the current code page? */ |
glebiuskv | 0:2f0e1e23c242 | 2717 | wc = put_utf(wc, &fno->altname[di], FF_SFN_BUF - di); /* Store it in UTF-16 or UTF-8 */ |
glebiuskv | 0:2f0e1e23c242 | 2718 | if (wc == 0) { di = 0; break; } /* Buffer overflow? */ |
glebiuskv | 0:2f0e1e23c242 | 2719 | di += wc; |
glebiuskv | 0:2f0e1e23c242 | 2720 | #else /* ANSI/OEM output */ |
glebiuskv | 0:2f0e1e23c242 | 2721 | fno->altname[di++] = (TCHAR)wc; /* Store it without any conversion */ |
glebiuskv | 0:2f0e1e23c242 | 2722 | #endif |
glebiuskv | 0:2f0e1e23c242 | 2723 | } |
glebiuskv | 0:2f0e1e23c242 | 2724 | fno->altname[di] = 0; /* Terminate the SFN (null string means SFN is invalid) */ |
glebiuskv | 0:2f0e1e23c242 | 2725 | |
glebiuskv | 0:2f0e1e23c242 | 2726 | if (fno->fname[0] == 0) { /* If LFN is invalid, altname[] needs to be copied to fname[] */ |
glebiuskv | 0:2f0e1e23c242 | 2727 | if (di == 0) { /* If LFN and SFN both are invalid, this object is inaccesible */ |
glebiuskv | 0:2f0e1e23c242 | 2728 | fno->fname[di++] = '?'; |
glebiuskv | 0:2f0e1e23c242 | 2729 | } else { |
glebiuskv | 0:2f0e1e23c242 | 2730 | for (si = di = 0; fno->altname[si]; si++, di++) { /* Copy altname[] to fname[] with case information */ |
glebiuskv | 0:2f0e1e23c242 | 2731 | wc = (WCHAR)fno->altname[si]; |
glebiuskv | 0:2f0e1e23c242 | 2732 | if (IsUpper(wc) && (dp->dir[DIR_NTres] & ((si >= 9) ? NS_EXT : NS_BODY))) wc += 0x20; |
glebiuskv | 0:2f0e1e23c242 | 2733 | fno->fname[di] = (TCHAR)wc; |
glebiuskv | 0:2f0e1e23c242 | 2734 | } |
glebiuskv | 0:2f0e1e23c242 | 2735 | } |
glebiuskv | 0:2f0e1e23c242 | 2736 | fno->fname[di] = 0; /* Terminate the LFN */ |
glebiuskv | 0:2f0e1e23c242 | 2737 | if (!dp->dir[DIR_NTres]) fno->altname[0] = 0; /* Altname is not needed if neither LFN nor case info is exist. */ |
glebiuskv | 0:2f0e1e23c242 | 2738 | } |
glebiuskv | 0:2f0e1e23c242 | 2739 | |
glebiuskv | 0:2f0e1e23c242 | 2740 | #else /* Non-LFN configuration */ |
glebiuskv | 0:2f0e1e23c242 | 2741 | si = di = 0; |
glebiuskv | 0:2f0e1e23c242 | 2742 | while (si < 11) { /* Copy name body and extension */ |
glebiuskv | 0:2f0e1e23c242 | 2743 | c = (TCHAR)dp->dir[si++]; |
glebiuskv | 0:2f0e1e23c242 | 2744 | if (c == ' ') continue; /* Skip padding spaces */ |
glebiuskv | 0:2f0e1e23c242 | 2745 | if (c == RDDEM) c = DDEM; /* Restore replaced DDEM character */ |
glebiuskv | 0:2f0e1e23c242 | 2746 | if (si == 9) fno->fname[di++] = '.';/* Insert a . if extension is exist */ |
glebiuskv | 0:2f0e1e23c242 | 2747 | fno->fname[di++] = c; |
glebiuskv | 0:2f0e1e23c242 | 2748 | } |
glebiuskv | 0:2f0e1e23c242 | 2749 | fno->fname[di] = 0; |
glebiuskv | 0:2f0e1e23c242 | 2750 | #endif |
glebiuskv | 0:2f0e1e23c242 | 2751 | |
glebiuskv | 0:2f0e1e23c242 | 2752 | fno->fattrib = dp->dir[DIR_Attr]; /* Attribute */ |
glebiuskv | 0:2f0e1e23c242 | 2753 | fno->fsize = ld_dword(dp->dir + DIR_FileSize); /* Size */ |
glebiuskv | 0:2f0e1e23c242 | 2754 | fno->ftime = ld_word(dp->dir + DIR_ModTime + 0); /* Time */ |
glebiuskv | 0:2f0e1e23c242 | 2755 | fno->fdate = ld_word(dp->dir + DIR_ModTime + 2); /* Date */ |
glebiuskv | 0:2f0e1e23c242 | 2756 | } |
glebiuskv | 0:2f0e1e23c242 | 2757 | |
glebiuskv | 0:2f0e1e23c242 | 2758 | #endif /* FF_FS_MINIMIZE <= 1 || FF_FS_RPATH >= 2 */ |
glebiuskv | 0:2f0e1e23c242 | 2759 | |
glebiuskv | 0:2f0e1e23c242 | 2760 | |
glebiuskv | 0:2f0e1e23c242 | 2761 | |
glebiuskv | 0:2f0e1e23c242 | 2762 | #if FF_USE_FIND && FF_FS_MINIMIZE <= 1 |
glebiuskv | 0:2f0e1e23c242 | 2763 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 2764 | /* Pattern matching */ |
glebiuskv | 0:2f0e1e23c242 | 2765 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 2766 | |
glebiuskv | 0:2f0e1e23c242 | 2767 | static |
glebiuskv | 0:2f0e1e23c242 | 2768 | DWORD get_achar ( /* Get a character and advances ptr */ |
glebiuskv | 0:2f0e1e23c242 | 2769 | const TCHAR** ptr /* Pointer to pointer to the ANSI/OEM or Unicode string */ |
glebiuskv | 0:2f0e1e23c242 | 2770 | ) |
glebiuskv | 0:2f0e1e23c242 | 2771 | { |
glebiuskv | 0:2f0e1e23c242 | 2772 | DWORD chr; |
glebiuskv | 0:2f0e1e23c242 | 2773 | |
glebiuskv | 0:2f0e1e23c242 | 2774 | |
glebiuskv | 0:2f0e1e23c242 | 2775 | #if FF_USE_LFN && FF_LFN_UNICODE >= 1 /* Unicode input */ |
glebiuskv | 0:2f0e1e23c242 | 2776 | chr = tchar2uni(ptr); |
glebiuskv | 0:2f0e1e23c242 | 2777 | if (chr == 0xFFFFFFFF) chr = 0; /* Wrong UTF encoding is recognized as end of the string */ |
glebiuskv | 0:2f0e1e23c242 | 2778 | chr = ff_wtoupper(chr); |
glebiuskv | 0:2f0e1e23c242 | 2779 | |
glebiuskv | 0:2f0e1e23c242 | 2780 | #else /* ANSI/OEM input */ |
glebiuskv | 0:2f0e1e23c242 | 2781 | chr = (BYTE)*(*ptr)++; /* Get a byte */ |
glebiuskv | 0:2f0e1e23c242 | 2782 | if (IsLower(chr)) chr -= 0x20; /* To upper ASCII char */ |
glebiuskv | 0:2f0e1e23c242 | 2783 | #if FF_CODE_PAGE == 0 |
glebiuskv | 0:2f0e1e23c242 | 2784 | if (ExCvt && chr >= 0x80) chr = ExCvt[chr - 0x80]; /* To upper SBCS extended char */ |
glebiuskv | 0:2f0e1e23c242 | 2785 | #elif FF_CODE_PAGE < 900 |
glebiuskv | 0:2f0e1e23c242 | 2786 | if (chr >= 0x80) chr = ExCvt[chr - 0x80]; /* To upper SBCS extended char */ |
glebiuskv | 0:2f0e1e23c242 | 2787 | #endif |
glebiuskv | 0:2f0e1e23c242 | 2788 | #if FF_CODE_PAGE == 0 || FF_CODE_PAGE >= 900 |
glebiuskv | 0:2f0e1e23c242 | 2789 | if (dbc_1st((BYTE)chr)) { /* Get DBC 2nd byte if needed */ |
glebiuskv | 0:2f0e1e23c242 | 2790 | chr = dbc_2nd((BYTE)**ptr) ? chr << 8 | (BYTE)*(*ptr)++ : 0; |
glebiuskv | 0:2f0e1e23c242 | 2791 | } |
glebiuskv | 0:2f0e1e23c242 | 2792 | #endif |
glebiuskv | 0:2f0e1e23c242 | 2793 | |
glebiuskv | 0:2f0e1e23c242 | 2794 | #endif |
glebiuskv | 0:2f0e1e23c242 | 2795 | return chr; |
glebiuskv | 0:2f0e1e23c242 | 2796 | } |
glebiuskv | 0:2f0e1e23c242 | 2797 | |
glebiuskv | 0:2f0e1e23c242 | 2798 | |
glebiuskv | 0:2f0e1e23c242 | 2799 | static |
glebiuskv | 0:2f0e1e23c242 | 2800 | int pattern_matching ( /* 0:not matched, 1:matched */ |
glebiuskv | 0:2f0e1e23c242 | 2801 | const TCHAR* pat, /* Matching pattern */ |
glebiuskv | 0:2f0e1e23c242 | 2802 | const TCHAR* nam, /* String to be tested */ |
glebiuskv | 0:2f0e1e23c242 | 2803 | int skip, /* Number of pre-skip chars (number of ?s) */ |
glebiuskv | 0:2f0e1e23c242 | 2804 | int inf /* Infinite search (* specified) */ |
glebiuskv | 0:2f0e1e23c242 | 2805 | ) |
glebiuskv | 0:2f0e1e23c242 | 2806 | { |
glebiuskv | 0:2f0e1e23c242 | 2807 | const TCHAR *pp, *np; |
glebiuskv | 0:2f0e1e23c242 | 2808 | DWORD pc, nc; |
glebiuskv | 0:2f0e1e23c242 | 2809 | int nm, nx; |
glebiuskv | 0:2f0e1e23c242 | 2810 | |
glebiuskv | 0:2f0e1e23c242 | 2811 | |
glebiuskv | 0:2f0e1e23c242 | 2812 | while (skip--) { /* Pre-skip name chars */ |
glebiuskv | 0:2f0e1e23c242 | 2813 | if (!get_achar(&nam)) return 0; /* Branch mismatched if less name chars */ |
glebiuskv | 0:2f0e1e23c242 | 2814 | } |
glebiuskv | 0:2f0e1e23c242 | 2815 | if (*pat == 0 && inf) return 1; /* (short circuit) */ |
glebiuskv | 0:2f0e1e23c242 | 2816 | |
glebiuskv | 0:2f0e1e23c242 | 2817 | do { |
glebiuskv | 0:2f0e1e23c242 | 2818 | pp = pat; np = nam; /* Top of pattern and name to match */ |
glebiuskv | 0:2f0e1e23c242 | 2819 | for (;;) { |
glebiuskv | 0:2f0e1e23c242 | 2820 | if (*pp == '?' || *pp == '*') { /* Wildcard? */ |
glebiuskv | 0:2f0e1e23c242 | 2821 | nm = nx = 0; |
glebiuskv | 0:2f0e1e23c242 | 2822 | do { /* Analyze the wildcard block */ |
glebiuskv | 0:2f0e1e23c242 | 2823 | if (*pp++ == '?') nm++; else nx = 1; |
glebiuskv | 0:2f0e1e23c242 | 2824 | } while (*pp == '?' || *pp == '*'); |
glebiuskv | 0:2f0e1e23c242 | 2825 | if (pattern_matching(pp, np, nm, nx)) return 1; /* Test new branch (recurs upto number of wildcard blocks in the pattern) */ |
glebiuskv | 0:2f0e1e23c242 | 2826 | nc = *np; break; /* Branch mismatched */ |
glebiuskv | 0:2f0e1e23c242 | 2827 | } |
glebiuskv | 0:2f0e1e23c242 | 2828 | pc = get_achar(&pp); /* Get a pattern char */ |
glebiuskv | 0:2f0e1e23c242 | 2829 | nc = get_achar(&np); /* Get a name char */ |
glebiuskv | 0:2f0e1e23c242 | 2830 | if (pc != nc) break; /* Branch mismatched? */ |
glebiuskv | 0:2f0e1e23c242 | 2831 | if (pc == 0) return 1; /* Branch matched? (matched at end of both strings) */ |
glebiuskv | 0:2f0e1e23c242 | 2832 | } |
glebiuskv | 0:2f0e1e23c242 | 2833 | get_achar(&nam); /* nam++ */ |
glebiuskv | 0:2f0e1e23c242 | 2834 | } while (inf && nc); /* Retry until end of name if infinite search is specified */ |
glebiuskv | 0:2f0e1e23c242 | 2835 | |
glebiuskv | 0:2f0e1e23c242 | 2836 | return 0; |
glebiuskv | 0:2f0e1e23c242 | 2837 | } |
glebiuskv | 0:2f0e1e23c242 | 2838 | |
glebiuskv | 0:2f0e1e23c242 | 2839 | #endif /* FF_USE_FIND && FF_FS_MINIMIZE <= 1 */ |
glebiuskv | 0:2f0e1e23c242 | 2840 | |
glebiuskv | 0:2f0e1e23c242 | 2841 | |
glebiuskv | 0:2f0e1e23c242 | 2842 | |
glebiuskv | 0:2f0e1e23c242 | 2843 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 2844 | /* Pick a top segment and create the object name in directory form */ |
glebiuskv | 0:2f0e1e23c242 | 2845 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 2846 | |
glebiuskv | 0:2f0e1e23c242 | 2847 | static |
glebiuskv | 0:2f0e1e23c242 | 2848 | FRESULT create_name ( /* FR_OK: successful, FR_INVALID_NAME: could not create */ |
glebiuskv | 0:2f0e1e23c242 | 2849 | FATFS_DIR* dp, /* Pointer to the directory object */ |
glebiuskv | 0:2f0e1e23c242 | 2850 | const TCHAR** path /* Pointer to pointer to the segment in the path string */ |
glebiuskv | 0:2f0e1e23c242 | 2851 | ) |
glebiuskv | 0:2f0e1e23c242 | 2852 | { |
glebiuskv | 0:2f0e1e23c242 | 2853 | #if FF_USE_LFN /* LFN configuration */ |
glebiuskv | 0:2f0e1e23c242 | 2854 | BYTE b, cf; |
glebiuskv | 0:2f0e1e23c242 | 2855 | WCHAR wc, *lfn; |
glebiuskv | 0:2f0e1e23c242 | 2856 | DWORD uc; |
glebiuskv | 0:2f0e1e23c242 | 2857 | UINT i, ni, si, di; |
glebiuskv | 0:2f0e1e23c242 | 2858 | const TCHAR *p; |
glebiuskv | 0:2f0e1e23c242 | 2859 | |
glebiuskv | 0:2f0e1e23c242 | 2860 | |
glebiuskv | 0:2f0e1e23c242 | 2861 | /* Create LFN into LFN working buffer */ |
glebiuskv | 0:2f0e1e23c242 | 2862 | p = *path; lfn = dp->obj.fs->lfnbuf; di = 0; |
glebiuskv | 0:2f0e1e23c242 | 2863 | for (;;) { |
glebiuskv | 0:2f0e1e23c242 | 2864 | uc = tchar2uni(&p); /* Get a character */ |
glebiuskv | 0:2f0e1e23c242 | 2865 | if (uc == 0xFFFFFFFF) return FR_INVALID_NAME; /* Invalid code or UTF decode error */ |
glebiuskv | 0:2f0e1e23c242 | 2866 | if (uc >= 0x10000) lfn[di++] = (WCHAR)(uc >> 16); /* Store high surrogate if needed */ |
glebiuskv | 0:2f0e1e23c242 | 2867 | wc = (WCHAR)uc; |
glebiuskv | 0:2f0e1e23c242 | 2868 | if (wc < ' ' || wc == '/' || wc == '\\') break; /* Break if end of the path or a separator is found */ |
glebiuskv | 0:2f0e1e23c242 | 2869 | if (wc < 0x80 && chk_chr("\"*:<>\?|\x7F", wc)) return FR_INVALID_NAME; /* Reject illegal characters for LFN */ |
glebiuskv | 0:2f0e1e23c242 | 2870 | if (di >= FF_MAX_LFN) return FR_INVALID_NAME; /* Reject too long name */ |
glebiuskv | 0:2f0e1e23c242 | 2871 | lfn[di++] = wc; /* Store the Unicode character */ |
glebiuskv | 0:2f0e1e23c242 | 2872 | } |
glebiuskv | 0:2f0e1e23c242 | 2873 | while (*p == '/' || *p == '\\') p++; /* Skip duplicated separators if exist */ |
glebiuskv | 0:2f0e1e23c242 | 2874 | *path = p; /* Return pointer to the next segment */ |
glebiuskv | 0:2f0e1e23c242 | 2875 | cf = (wc < ' ') ? NS_LAST : 0; /* Set last segment flag if end of the path */ |
glebiuskv | 0:2f0e1e23c242 | 2876 | |
glebiuskv | 0:2f0e1e23c242 | 2877 | #if FF_FS_RPATH != 0 |
glebiuskv | 0:2f0e1e23c242 | 2878 | if ((di == 1 && lfn[di - 1] == '.') || |
glebiuskv | 0:2f0e1e23c242 | 2879 | (di == 2 && lfn[di - 1] == '.' && lfn[di - 2] == '.')) { /* Is this segment a dot name? */ |
glebiuskv | 0:2f0e1e23c242 | 2880 | lfn[di] = 0; |
glebiuskv | 0:2f0e1e23c242 | 2881 | for (i = 0; i < 11; i++) { /* Create dot name for SFN entry */ |
glebiuskv | 0:2f0e1e23c242 | 2882 | dp->fn[i] = (i < di) ? '.' : ' '; |
glebiuskv | 0:2f0e1e23c242 | 2883 | } |
glebiuskv | 0:2f0e1e23c242 | 2884 | dp->fn[i] = cf | NS_DOT; /* This is a dot entry */ |
glebiuskv | 0:2f0e1e23c242 | 2885 | return FR_OK; |
glebiuskv | 0:2f0e1e23c242 | 2886 | } |
glebiuskv | 0:2f0e1e23c242 | 2887 | #endif |
glebiuskv | 0:2f0e1e23c242 | 2888 | while (di) { /* Snip off trailing spaces and dots if exist */ |
glebiuskv | 0:2f0e1e23c242 | 2889 | wc = lfn[di - 1]; |
glebiuskv | 0:2f0e1e23c242 | 2890 | if (wc != ' ' && wc != '.') break; |
glebiuskv | 0:2f0e1e23c242 | 2891 | di--; |
glebiuskv | 0:2f0e1e23c242 | 2892 | } |
glebiuskv | 0:2f0e1e23c242 | 2893 | lfn[di] = 0; /* LFN is created into the working buffer */ |
glebiuskv | 0:2f0e1e23c242 | 2894 | if (di == 0) return FR_INVALID_NAME; /* Reject null name */ |
glebiuskv | 0:2f0e1e23c242 | 2895 | |
glebiuskv | 0:2f0e1e23c242 | 2896 | /* Create SFN in directory form */ |
glebiuskv | 0:2f0e1e23c242 | 2897 | for (si = 0; lfn[si] == ' '; si++) ; /* Remove leading spaces */ |
glebiuskv | 0:2f0e1e23c242 | 2898 | if (si > 0 || lfn[si] == '.') cf |= NS_LOSS | NS_LFN; /* Is there any leading space or dot? */ |
glebiuskv | 0:2f0e1e23c242 | 2899 | while (di > 0 && lfn[di - 1] != '.') di--; /* Find last dot (di<=si: no extension) */ |
glebiuskv | 0:2f0e1e23c242 | 2900 | |
glebiuskv | 0:2f0e1e23c242 | 2901 | mem_set(dp->fn, ' ', 11); |
glebiuskv | 0:2f0e1e23c242 | 2902 | i = b = 0; ni = 8; |
glebiuskv | 0:2f0e1e23c242 | 2903 | for (;;) { |
glebiuskv | 0:2f0e1e23c242 | 2904 | wc = lfn[si++]; /* Get an LFN character */ |
glebiuskv | 0:2f0e1e23c242 | 2905 | if (wc == 0) break; /* Break on end of the LFN */ |
glebiuskv | 0:2f0e1e23c242 | 2906 | if (wc == ' ' || (wc == '.' && si != di)) { /* Remove embedded spaces and dots */ |
glebiuskv | 0:2f0e1e23c242 | 2907 | cf |= NS_LOSS | NS_LFN; |
glebiuskv | 0:2f0e1e23c242 | 2908 | continue; |
glebiuskv | 0:2f0e1e23c242 | 2909 | } |
glebiuskv | 0:2f0e1e23c242 | 2910 | |
glebiuskv | 0:2f0e1e23c242 | 2911 | if (i >= ni || si == di) { /* End of field? */ |
glebiuskv | 0:2f0e1e23c242 | 2912 | if (ni == 11) { /* Name extension overflow? */ |
glebiuskv | 0:2f0e1e23c242 | 2913 | cf |= NS_LOSS | NS_LFN; |
glebiuskv | 0:2f0e1e23c242 | 2914 | break; |
glebiuskv | 0:2f0e1e23c242 | 2915 | } |
glebiuskv | 0:2f0e1e23c242 | 2916 | if (si != di) cf |= NS_LOSS | NS_LFN; /* Name body overflow? */ |
glebiuskv | 0:2f0e1e23c242 | 2917 | if (si > di) break; /* No name extension? */ |
glebiuskv | 0:2f0e1e23c242 | 2918 | si = di; i = 8; ni = 11; b <<= 2; /* Enter name extension */ |
glebiuskv | 0:2f0e1e23c242 | 2919 | continue; |
glebiuskv | 0:2f0e1e23c242 | 2920 | } |
glebiuskv | 0:2f0e1e23c242 | 2921 | |
glebiuskv | 0:2f0e1e23c242 | 2922 | if (wc >= 0x80) { /* Is this a non-ASCII character? */ |
glebiuskv | 0:2f0e1e23c242 | 2923 | cf |= NS_LFN; /* LFN entry needs to be created */ |
glebiuskv | 0:2f0e1e23c242 | 2924 | #if FF_CODE_PAGE == 0 |
glebiuskv | 0:2f0e1e23c242 | 2925 | if (ExCvt) { /* At SBCS */ |
glebiuskv | 0:2f0e1e23c242 | 2926 | wc = ff_uni2oem(wc, CODEPAGE); /* Unicode ==> ANSI/OEM code */ |
glebiuskv | 0:2f0e1e23c242 | 2927 | if (wc & 0x80) wc = ExCvt[wc & 0x7F]; /* Convert extended character to upper (SBCS) */ |
glebiuskv | 0:2f0e1e23c242 | 2928 | } else { /* At DBCS */ |
glebiuskv | 0:2f0e1e23c242 | 2929 | wc = ff_uni2oem(ff_wtoupper(wc), CODEPAGE); /* Unicode ==> Upper convert ==> ANSI/OEM code */ |
glebiuskv | 0:2f0e1e23c242 | 2930 | } |
glebiuskv | 0:2f0e1e23c242 | 2931 | #elif FF_CODE_PAGE < 900 /* SBCS cfg */ |
glebiuskv | 0:2f0e1e23c242 | 2932 | wc = ff_uni2oem(wc, CODEPAGE); /* Unicode ==> ANSI/OEM code */ |
glebiuskv | 0:2f0e1e23c242 | 2933 | if (wc & 0x80) wc = ExCvt[wc & 0x7F]; /* Convert extended character to upper (SBCS) */ |
glebiuskv | 0:2f0e1e23c242 | 2934 | #else /* DBCS cfg */ |
glebiuskv | 0:2f0e1e23c242 | 2935 | wc = ff_uni2oem(ff_wtoupper(wc), CODEPAGE); /* Unicode ==> Upper convert ==> ANSI/OEM code */ |
glebiuskv | 0:2f0e1e23c242 | 2936 | #endif |
glebiuskv | 0:2f0e1e23c242 | 2937 | } |
glebiuskv | 0:2f0e1e23c242 | 2938 | |
glebiuskv | 0:2f0e1e23c242 | 2939 | if (wc >= 0x100) { /* Is this a DBC? */ |
glebiuskv | 0:2f0e1e23c242 | 2940 | if (i >= ni - 1) { /* Field overflow? */ |
glebiuskv | 0:2f0e1e23c242 | 2941 | cf |= NS_LOSS | NS_LFN; |
glebiuskv | 0:2f0e1e23c242 | 2942 | i = ni; continue; /* Next field */ |
glebiuskv | 0:2f0e1e23c242 | 2943 | } |
glebiuskv | 0:2f0e1e23c242 | 2944 | dp->fn[i++] = (BYTE)(wc >> 8); /* Put 1st byte */ |
glebiuskv | 0:2f0e1e23c242 | 2945 | } else { /* SBC */ |
glebiuskv | 0:2f0e1e23c242 | 2946 | if (wc == 0 || chk_chr("+,;=[]", wc)) { /* Replace illegal characters for SFN if needed */ |
glebiuskv | 0:2f0e1e23c242 | 2947 | wc = '_'; cf |= NS_LOSS | NS_LFN;/* Lossy conversion */ |
glebiuskv | 0:2f0e1e23c242 | 2948 | } else { |
glebiuskv | 0:2f0e1e23c242 | 2949 | if (IsUpper(wc)) { /* ASCII upper case? */ |
glebiuskv | 0:2f0e1e23c242 | 2950 | b |= 2; |
glebiuskv | 0:2f0e1e23c242 | 2951 | } |
glebiuskv | 0:2f0e1e23c242 | 2952 | if (IsLower(wc)) { /* ASCII lower case? */ |
glebiuskv | 0:2f0e1e23c242 | 2953 | b |= 1; wc -= 0x20; |
glebiuskv | 0:2f0e1e23c242 | 2954 | } |
glebiuskv | 0:2f0e1e23c242 | 2955 | } |
glebiuskv | 0:2f0e1e23c242 | 2956 | } |
glebiuskv | 0:2f0e1e23c242 | 2957 | dp->fn[i++] = (BYTE)wc; |
glebiuskv | 0:2f0e1e23c242 | 2958 | } |
glebiuskv | 0:2f0e1e23c242 | 2959 | |
glebiuskv | 0:2f0e1e23c242 | 2960 | if (dp->fn[0] == DDEM) dp->fn[0] = RDDEM; /* If the first character collides with DDEM, replace it with RDDEM */ |
glebiuskv | 0:2f0e1e23c242 | 2961 | |
glebiuskv | 0:2f0e1e23c242 | 2962 | if (ni == 8) b <<= 2; /* Shift capital flags if no extension */ |
glebiuskv | 0:2f0e1e23c242 | 2963 | if ((b & 0x0C) == 0x0C || (b & 0x03) == 0x03) cf |= NS_LFN; /* LFN entry needs to be created if composite capitals */ |
glebiuskv | 0:2f0e1e23c242 | 2964 | if (!(cf & NS_LFN)) { /* When LFN is in 8.3 format without extended character, NT flags are created */ |
glebiuskv | 0:2f0e1e23c242 | 2965 | if (b & 0x01) cf |= NS_EXT; /* NT flag (Extension has small capital letters only) */ |
glebiuskv | 0:2f0e1e23c242 | 2966 | if (b & 0x04) cf |= NS_BODY; /* NT flag (Body has small capital letters only) */ |
glebiuskv | 0:2f0e1e23c242 | 2967 | } |
glebiuskv | 0:2f0e1e23c242 | 2968 | |
glebiuskv | 0:2f0e1e23c242 | 2969 | dp->fn[NSFLAG] = cf; /* SFN is created into dp->fn[] */ |
glebiuskv | 0:2f0e1e23c242 | 2970 | |
glebiuskv | 0:2f0e1e23c242 | 2971 | return FR_OK; |
glebiuskv | 0:2f0e1e23c242 | 2972 | |
glebiuskv | 0:2f0e1e23c242 | 2973 | |
glebiuskv | 0:2f0e1e23c242 | 2974 | #else /* FF_USE_LFN : Non-LFN configuration */ |
glebiuskv | 0:2f0e1e23c242 | 2975 | BYTE c, d, *sfn; |
glebiuskv | 0:2f0e1e23c242 | 2976 | UINT ni, si, i; |
glebiuskv | 0:2f0e1e23c242 | 2977 | const char *p; |
glebiuskv | 0:2f0e1e23c242 | 2978 | |
glebiuskv | 0:2f0e1e23c242 | 2979 | /* Create file name in directory form */ |
glebiuskv | 0:2f0e1e23c242 | 2980 | p = *path; sfn = dp->fn; |
glebiuskv | 0:2f0e1e23c242 | 2981 | mem_set(sfn, ' ', 11); |
glebiuskv | 0:2f0e1e23c242 | 2982 | si = i = 0; ni = 8; |
glebiuskv | 0:2f0e1e23c242 | 2983 | #if FF_FS_RPATH != 0 |
glebiuskv | 0:2f0e1e23c242 | 2984 | if (p[si] == '.') { /* Is this a dot entry? */ |
glebiuskv | 0:2f0e1e23c242 | 2985 | for (;;) { |
glebiuskv | 0:2f0e1e23c242 | 2986 | c = (BYTE)p[si++]; |
glebiuskv | 0:2f0e1e23c242 | 2987 | if (c != '.' || si >= 3) break; |
glebiuskv | 0:2f0e1e23c242 | 2988 | sfn[i++] = c; |
glebiuskv | 0:2f0e1e23c242 | 2989 | } |
glebiuskv | 0:2f0e1e23c242 | 2990 | if (c != '/' && c != '\\' && c > ' ') return FR_INVALID_NAME; |
glebiuskv | 0:2f0e1e23c242 | 2991 | *path = p + si; /* Return pointer to the next segment */ |
glebiuskv | 0:2f0e1e23c242 | 2992 | sfn[NSFLAG] = (c <= ' ') ? NS_LAST | NS_DOT : NS_DOT; /* Set last segment flag if end of the path */ |
glebiuskv | 0:2f0e1e23c242 | 2993 | return FR_OK; |
glebiuskv | 0:2f0e1e23c242 | 2994 | } |
glebiuskv | 0:2f0e1e23c242 | 2995 | #endif |
glebiuskv | 0:2f0e1e23c242 | 2996 | for (;;) { |
glebiuskv | 0:2f0e1e23c242 | 2997 | c = (BYTE)p[si++]; /* Get a byte */ |
glebiuskv | 0:2f0e1e23c242 | 2998 | if (c <= ' ') break; /* Break if end of the path name */ |
glebiuskv | 0:2f0e1e23c242 | 2999 | if (c == '/' || c == '\\') { /* Break if a separator is found */ |
glebiuskv | 0:2f0e1e23c242 | 3000 | while (p[si] == '/' || p[si] == '\\') si++; /* Skip duplicated separator if exist */ |
glebiuskv | 0:2f0e1e23c242 | 3001 | break; |
glebiuskv | 0:2f0e1e23c242 | 3002 | } |
glebiuskv | 0:2f0e1e23c242 | 3003 | if (c == '.' || i >= ni) { /* End of body or field overflow? */ |
glebiuskv | 0:2f0e1e23c242 | 3004 | if (ni == 11 || c != '.') return FR_INVALID_NAME; /* Field overflow or invalid dot? */ |
glebiuskv | 0:2f0e1e23c242 | 3005 | i = 8; ni = 11; /* Enter file extension field */ |
glebiuskv | 0:2f0e1e23c242 | 3006 | continue; |
glebiuskv | 0:2f0e1e23c242 | 3007 | } |
glebiuskv | 0:2f0e1e23c242 | 3008 | #if FF_CODE_PAGE == 0 |
glebiuskv | 0:2f0e1e23c242 | 3009 | if (ExCvt && c >= 0x80) { /* Is SBC extended character? */ |
glebiuskv | 0:2f0e1e23c242 | 3010 | c = ExCvt[c & 0x7F]; /* To upper SBC extended character */ |
glebiuskv | 0:2f0e1e23c242 | 3011 | } |
glebiuskv | 0:2f0e1e23c242 | 3012 | #elif FF_CODE_PAGE < 900 |
glebiuskv | 0:2f0e1e23c242 | 3013 | if (c >= 0x80) { /* Is SBC extended character? */ |
glebiuskv | 0:2f0e1e23c242 | 3014 | c = ExCvt[c & 0x7F]; /* To upper SBC extended character */ |
glebiuskv | 0:2f0e1e23c242 | 3015 | } |
glebiuskv | 0:2f0e1e23c242 | 3016 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3017 | if (dbc_1st(c)) { /* Check if it is a DBC 1st byte */ |
glebiuskv | 0:2f0e1e23c242 | 3018 | d = (BYTE)p[si++]; /* Get 2nd byte */ |
glebiuskv | 0:2f0e1e23c242 | 3019 | if (!dbc_2nd(d) || i >= ni - 1) return FR_INVALID_NAME; /* Reject invalid DBC */ |
glebiuskv | 0:2f0e1e23c242 | 3020 | sfn[i++] = c; |
glebiuskv | 0:2f0e1e23c242 | 3021 | sfn[i++] = d; |
glebiuskv | 0:2f0e1e23c242 | 3022 | } else { /* SBC */ |
glebiuskv | 0:2f0e1e23c242 | 3023 | if (chk_chr("\"*+,:;<=>\?[]|\x7F", c)) return FR_INVALID_NAME; /* Reject illegal chrs for SFN */ |
glebiuskv | 0:2f0e1e23c242 | 3024 | if (IsLower(c)) c -= 0x20; /* To upper */ |
glebiuskv | 0:2f0e1e23c242 | 3025 | sfn[i++] = c; |
glebiuskv | 0:2f0e1e23c242 | 3026 | } |
glebiuskv | 0:2f0e1e23c242 | 3027 | } |
glebiuskv | 0:2f0e1e23c242 | 3028 | *path = p + si; /* Return pointer to the next segment */ |
glebiuskv | 0:2f0e1e23c242 | 3029 | if (i == 0) return FR_INVALID_NAME; /* Reject nul string */ |
glebiuskv | 0:2f0e1e23c242 | 3030 | |
glebiuskv | 0:2f0e1e23c242 | 3031 | if (sfn[0] == DDEM) sfn[0] = RDDEM; /* If the first character collides with DDEM, replace it with RDDEM */ |
glebiuskv | 0:2f0e1e23c242 | 3032 | sfn[NSFLAG] = (c <= ' ') ? NS_LAST : 0; /* Set last segment flag if end of the path */ |
glebiuskv | 0:2f0e1e23c242 | 3033 | |
glebiuskv | 0:2f0e1e23c242 | 3034 | return FR_OK; |
glebiuskv | 0:2f0e1e23c242 | 3035 | #endif /* FF_USE_LFN */ |
glebiuskv | 0:2f0e1e23c242 | 3036 | } |
glebiuskv | 0:2f0e1e23c242 | 3037 | |
glebiuskv | 0:2f0e1e23c242 | 3038 | |
glebiuskv | 0:2f0e1e23c242 | 3039 | |
glebiuskv | 0:2f0e1e23c242 | 3040 | |
glebiuskv | 0:2f0e1e23c242 | 3041 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 3042 | /* Follow a file path */ |
glebiuskv | 0:2f0e1e23c242 | 3043 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 3044 | |
glebiuskv | 0:2f0e1e23c242 | 3045 | static |
glebiuskv | 0:2f0e1e23c242 | 3046 | FRESULT follow_path ( /* FR_OK(0): successful, !=0: error code */ |
glebiuskv | 0:2f0e1e23c242 | 3047 | FATFS_DIR* dp, /* Directory object to return last directory and found object */ |
glebiuskv | 0:2f0e1e23c242 | 3048 | const TCHAR* path /* Full-path string to find a file or directory */ |
glebiuskv | 0:2f0e1e23c242 | 3049 | ) |
glebiuskv | 0:2f0e1e23c242 | 3050 | { |
glebiuskv | 0:2f0e1e23c242 | 3051 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 3052 | BYTE ns; |
glebiuskv | 0:2f0e1e23c242 | 3053 | FATFS *fs = dp->obj.fs; |
glebiuskv | 0:2f0e1e23c242 | 3054 | |
glebiuskv | 0:2f0e1e23c242 | 3055 | |
glebiuskv | 0:2f0e1e23c242 | 3056 | #if FF_FS_RPATH != 0 |
glebiuskv | 0:2f0e1e23c242 | 3057 | if (*path != '/' && *path != '\\') { /* Without heading separator */ |
glebiuskv | 0:2f0e1e23c242 | 3058 | dp->obj.sclust = fs->cdir; /* Start from current directory */ |
glebiuskv | 0:2f0e1e23c242 | 3059 | } else |
glebiuskv | 0:2f0e1e23c242 | 3060 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3061 | { /* With heading separator */ |
glebiuskv | 0:2f0e1e23c242 | 3062 | while (*path == '/' || *path == '\\') path++; /* Strip heading separator */ |
glebiuskv | 0:2f0e1e23c242 | 3063 | dp->obj.sclust = 0; /* Start from root directory */ |
glebiuskv | 0:2f0e1e23c242 | 3064 | } |
glebiuskv | 0:2f0e1e23c242 | 3065 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 3066 | dp->obj.n_frag = 0; /* Invalidate last fragment counter of the object */ |
glebiuskv | 0:2f0e1e23c242 | 3067 | #if FF_FS_RPATH != 0 |
glebiuskv | 0:2f0e1e23c242 | 3068 | if (fs->fs_type == FS_EXFAT && dp->obj.sclust) { /* exFAT: Retrieve the sub-directory's status */ |
glebiuskv | 0:2f0e1e23c242 | 3069 | FATFS_DIR dj; |
glebiuskv | 0:2f0e1e23c242 | 3070 | |
glebiuskv | 0:2f0e1e23c242 | 3071 | dp->obj.c_scl = fs->cdc_scl; |
glebiuskv | 0:2f0e1e23c242 | 3072 | dp->obj.c_size = fs->cdc_size; |
glebiuskv | 0:2f0e1e23c242 | 3073 | dp->obj.c_ofs = fs->cdc_ofs; |
glebiuskv | 0:2f0e1e23c242 | 3074 | res = load_obj_xdir(&dj, &dp->obj); |
glebiuskv | 0:2f0e1e23c242 | 3075 | if (res != FR_OK) return res; |
glebiuskv | 0:2f0e1e23c242 | 3076 | dp->obj.objsize = ld_dword(fs->dirbuf + XDIR_FileSize); |
glebiuskv | 0:2f0e1e23c242 | 3077 | dp->obj.stat = fs->dirbuf[XDIR_GenFlags] & 2; |
glebiuskv | 0:2f0e1e23c242 | 3078 | } |
glebiuskv | 0:2f0e1e23c242 | 3079 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3080 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3081 | |
glebiuskv | 0:2f0e1e23c242 | 3082 | if ((UINT)*path < ' ') { /* Null path name is the origin directory itself */ |
glebiuskv | 0:2f0e1e23c242 | 3083 | dp->fn[NSFLAG] = NS_NONAME; |
glebiuskv | 0:2f0e1e23c242 | 3084 | res = dir_sdi(dp, 0); |
glebiuskv | 0:2f0e1e23c242 | 3085 | |
glebiuskv | 0:2f0e1e23c242 | 3086 | } else { /* Follow path */ |
glebiuskv | 0:2f0e1e23c242 | 3087 | for (;;) { |
glebiuskv | 0:2f0e1e23c242 | 3088 | res = create_name(dp, &path); /* Get a segment name of the path */ |
glebiuskv | 0:2f0e1e23c242 | 3089 | if (res != FR_OK) break; |
glebiuskv | 0:2f0e1e23c242 | 3090 | res = dir_find(dp); /* Find an object with the segment name */ |
glebiuskv | 0:2f0e1e23c242 | 3091 | ns = dp->fn[NSFLAG]; |
glebiuskv | 0:2f0e1e23c242 | 3092 | if (res != FR_OK) { /* Failed to find the object */ |
glebiuskv | 0:2f0e1e23c242 | 3093 | if (res == FR_NO_FILE) { /* Object is not found */ |
glebiuskv | 0:2f0e1e23c242 | 3094 | if (FF_FS_RPATH && (ns & NS_DOT)) { /* If dot entry is not exist, stay there */ |
glebiuskv | 0:2f0e1e23c242 | 3095 | if (!(ns & NS_LAST)) continue; /* Continue to follow if not last segment */ |
glebiuskv | 0:2f0e1e23c242 | 3096 | dp->fn[NSFLAG] = NS_NONAME; |
glebiuskv | 0:2f0e1e23c242 | 3097 | res = FR_OK; |
glebiuskv | 0:2f0e1e23c242 | 3098 | } else { /* Could not find the object */ |
glebiuskv | 0:2f0e1e23c242 | 3099 | if (!(ns & NS_LAST)) res = FR_NO_PATH; /* Adjust error code if not last segment */ |
glebiuskv | 0:2f0e1e23c242 | 3100 | } |
glebiuskv | 0:2f0e1e23c242 | 3101 | } |
glebiuskv | 0:2f0e1e23c242 | 3102 | break; |
glebiuskv | 0:2f0e1e23c242 | 3103 | } |
glebiuskv | 0:2f0e1e23c242 | 3104 | if (ns & NS_LAST) break; /* Last segment matched. Function completed. */ |
glebiuskv | 0:2f0e1e23c242 | 3105 | /* Get into the sub-directory */ |
glebiuskv | 0:2f0e1e23c242 | 3106 | if (!(dp->obj.attr & AM_DIR)) { /* It is not a sub-directory and cannot follow */ |
glebiuskv | 0:2f0e1e23c242 | 3107 | res = FR_NO_PATH; break; |
glebiuskv | 0:2f0e1e23c242 | 3108 | } |
glebiuskv | 0:2f0e1e23c242 | 3109 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 3110 | if (fs->fs_type == FS_EXFAT) { /* Save containing directory information for next dir */ |
glebiuskv | 0:2f0e1e23c242 | 3111 | dp->obj.c_scl = dp->obj.sclust; |
glebiuskv | 0:2f0e1e23c242 | 3112 | dp->obj.c_size = ((DWORD)dp->obj.objsize & 0xFFFFFF00) | dp->obj.stat; |
glebiuskv | 0:2f0e1e23c242 | 3113 | dp->obj.c_ofs = dp->blk_ofs; |
glebiuskv | 0:2f0e1e23c242 | 3114 | init_alloc_info(fs, &dp->obj); /* Open next directory */ |
glebiuskv | 0:2f0e1e23c242 | 3115 | } else |
glebiuskv | 0:2f0e1e23c242 | 3116 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3117 | { |
glebiuskv | 0:2f0e1e23c242 | 3118 | dp->obj.sclust = ld_clust(fs, fs->win + dp->dptr % SS(fs)); /* Open next directory */ |
glebiuskv | 0:2f0e1e23c242 | 3119 | } |
glebiuskv | 0:2f0e1e23c242 | 3120 | } |
glebiuskv | 0:2f0e1e23c242 | 3121 | } |
glebiuskv | 0:2f0e1e23c242 | 3122 | |
glebiuskv | 0:2f0e1e23c242 | 3123 | return res; |
glebiuskv | 0:2f0e1e23c242 | 3124 | } |
glebiuskv | 0:2f0e1e23c242 | 3125 | |
glebiuskv | 0:2f0e1e23c242 | 3126 | |
glebiuskv | 0:2f0e1e23c242 | 3127 | |
glebiuskv | 0:2f0e1e23c242 | 3128 | |
glebiuskv | 0:2f0e1e23c242 | 3129 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 3130 | /* Get logical drive number from path name */ |
glebiuskv | 0:2f0e1e23c242 | 3131 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 3132 | |
glebiuskv | 0:2f0e1e23c242 | 3133 | static |
glebiuskv | 0:2f0e1e23c242 | 3134 | int get_ldnumber ( /* Returns logical drive number (-1:invalid drive) */ |
glebiuskv | 0:2f0e1e23c242 | 3135 | const TCHAR** path /* Pointer to pointer to the path name */ |
glebiuskv | 0:2f0e1e23c242 | 3136 | ) |
glebiuskv | 0:2f0e1e23c242 | 3137 | { |
glebiuskv | 0:2f0e1e23c242 | 3138 | const TCHAR *tp, *tt; |
glebiuskv | 0:2f0e1e23c242 | 3139 | UINT i; |
glebiuskv | 0:2f0e1e23c242 | 3140 | int vol = -1; |
glebiuskv | 0:2f0e1e23c242 | 3141 | #if FF_STR_VOLUME_ID /* Find string drive id */ |
glebiuskv | 0:2f0e1e23c242 | 3142 | static const char* const volid[] = {FF_VOLUME_STRS}; |
glebiuskv | 0:2f0e1e23c242 | 3143 | const char *sp; |
glebiuskv | 0:2f0e1e23c242 | 3144 | char c; |
glebiuskv | 0:2f0e1e23c242 | 3145 | TCHAR tc; |
glebiuskv | 0:2f0e1e23c242 | 3146 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3147 | |
glebiuskv | 0:2f0e1e23c242 | 3148 | |
glebiuskv | 0:2f0e1e23c242 | 3149 | if (*path != 0) { /* If the pointer is not a null */ |
glebiuskv | 0:2f0e1e23c242 | 3150 | for (tt = *path; (UINT)*tt >= (FF_USE_LFN ? ' ' : '!') && *tt != ':'; tt++) ; /* Find a colon in the path */ |
glebiuskv | 0:2f0e1e23c242 | 3151 | if (*tt == ':') { /* If a colon is exist in the path name */ |
glebiuskv | 0:2f0e1e23c242 | 3152 | tp = *path; |
glebiuskv | 0:2f0e1e23c242 | 3153 | i = *tp++; |
glebiuskv | 0:2f0e1e23c242 | 3154 | if (IsDigit(i) && tp == tt) { /* Is there a numeric drive id + colon? */ |
glebiuskv | 0:2f0e1e23c242 | 3155 | if ((i -= '0') < FF_VOLUMES) { /* If drive id is found, get the value and strip it */ |
glebiuskv | 0:2f0e1e23c242 | 3156 | vol = (int)i; |
glebiuskv | 0:2f0e1e23c242 | 3157 | *path = ++tt; |
glebiuskv | 0:2f0e1e23c242 | 3158 | } |
glebiuskv | 0:2f0e1e23c242 | 3159 | } |
glebiuskv | 0:2f0e1e23c242 | 3160 | #if FF_STR_VOLUME_ID |
glebiuskv | 0:2f0e1e23c242 | 3161 | else { /* No numeric drive number, find string drive id */ |
glebiuskv | 0:2f0e1e23c242 | 3162 | i = 0; tt++; |
glebiuskv | 0:2f0e1e23c242 | 3163 | do { |
glebiuskv | 0:2f0e1e23c242 | 3164 | sp = volid[i]; tp = *path; |
glebiuskv | 0:2f0e1e23c242 | 3165 | do { /* Compare a string drive id with path name */ |
glebiuskv | 0:2f0e1e23c242 | 3166 | c = *sp++; tc = *tp++; |
glebiuskv | 0:2f0e1e23c242 | 3167 | if (IsLower(tc)) tc -= 0x20; |
glebiuskv | 0:2f0e1e23c242 | 3168 | } while (c && (TCHAR)c == tc); |
glebiuskv | 0:2f0e1e23c242 | 3169 | } while ((c || tp != tt) && ++i < FF_VOLUMES); /* Repeat for each id until pattern match */ |
glebiuskv | 0:2f0e1e23c242 | 3170 | if (i < FF_VOLUMES) { /* If a drive id is found, get the value and strip it */ |
glebiuskv | 0:2f0e1e23c242 | 3171 | vol = (int)i; |
glebiuskv | 0:2f0e1e23c242 | 3172 | *path = tt; |
glebiuskv | 0:2f0e1e23c242 | 3173 | } |
glebiuskv | 0:2f0e1e23c242 | 3174 | } |
glebiuskv | 0:2f0e1e23c242 | 3175 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3176 | } else { /* No volume id and use default drive */ |
glebiuskv | 0:2f0e1e23c242 | 3177 | #if FF_FS_RPATH != 0 && FF_VOLUMES >= 2 |
glebiuskv | 0:2f0e1e23c242 | 3178 | vol = CurrVol; /* Current drive */ |
glebiuskv | 0:2f0e1e23c242 | 3179 | #else |
glebiuskv | 0:2f0e1e23c242 | 3180 | vol = 0; /* Drive 0 */ |
glebiuskv | 0:2f0e1e23c242 | 3181 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3182 | } |
glebiuskv | 0:2f0e1e23c242 | 3183 | } |
glebiuskv | 0:2f0e1e23c242 | 3184 | return vol; |
glebiuskv | 0:2f0e1e23c242 | 3185 | } |
glebiuskv | 0:2f0e1e23c242 | 3186 | |
glebiuskv | 0:2f0e1e23c242 | 3187 | |
glebiuskv | 0:2f0e1e23c242 | 3188 | |
glebiuskv | 0:2f0e1e23c242 | 3189 | |
glebiuskv | 0:2f0e1e23c242 | 3190 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 3191 | /* Load a sector and check if it is an FAT VBR */ |
glebiuskv | 0:2f0e1e23c242 | 3192 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 3193 | |
glebiuskv | 0:2f0e1e23c242 | 3194 | static |
glebiuskv | 0:2f0e1e23c242 | 3195 | BYTE check_fs ( /* 0:FAT, 1:exFAT, 2:Valid BS but not FAT, 3:Not a BS, 4:Disk error */ |
glebiuskv | 0:2f0e1e23c242 | 3196 | FATFS* fs, /* Filesystem object */ |
glebiuskv | 0:2f0e1e23c242 | 3197 | DWORD sect /* Sector# (lba) to load and check if it is an FAT-VBR or not */ |
glebiuskv | 0:2f0e1e23c242 | 3198 | ) |
glebiuskv | 0:2f0e1e23c242 | 3199 | { |
glebiuskv | 0:2f0e1e23c242 | 3200 | fs->wflag = 0; fs->winsect = 0xFFFFFFFF; /* Invaidate window */ |
glebiuskv | 0:2f0e1e23c242 | 3201 | if (move_window(fs, sect) != FR_OK) return 4; /* Load boot record */ |
glebiuskv | 0:2f0e1e23c242 | 3202 | |
glebiuskv | 0:2f0e1e23c242 | 3203 | if (ld_word(fs->win + BS_55AA) != 0xAA55) return 3; /* Check boot record signature (always placed here even if the sector size is >512) */ |
glebiuskv | 0:2f0e1e23c242 | 3204 | |
glebiuskv | 0:2f0e1e23c242 | 3205 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 3206 | if (!mem_cmp(fs->win + BS_JmpBoot, "\xEB\x76\x90" "EXFAT ", 11)) return 1; /* Check if exFAT VBR */ |
glebiuskv | 0:2f0e1e23c242 | 3207 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3208 | if (fs->win[BS_JmpBoot] == 0xE9 || fs->win[BS_JmpBoot] == 0xEB || fs->win[BS_JmpBoot] == 0xE8) { /* Valid JumpBoot code? */ |
glebiuskv | 0:2f0e1e23c242 | 3209 | if (!mem_cmp(fs->win + BS_FilSysType, "FAT", 3)) return 0; /* Is it an FAT VBR? */ |
glebiuskv | 0:2f0e1e23c242 | 3210 | if (!mem_cmp(fs->win + BS_FilSysType32, "FAT32", 5)) return 0; /* Is it an FAT32 VBR? */ |
glebiuskv | 0:2f0e1e23c242 | 3211 | } |
glebiuskv | 0:2f0e1e23c242 | 3212 | return 2; /* Valid BS but not FAT */ |
glebiuskv | 0:2f0e1e23c242 | 3213 | } |
glebiuskv | 0:2f0e1e23c242 | 3214 | |
glebiuskv | 0:2f0e1e23c242 | 3215 | |
glebiuskv | 0:2f0e1e23c242 | 3216 | |
glebiuskv | 0:2f0e1e23c242 | 3217 | |
glebiuskv | 0:2f0e1e23c242 | 3218 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 3219 | /* Determine logical drive number and mount the volume if needed */ |
glebiuskv | 0:2f0e1e23c242 | 3220 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 3221 | |
glebiuskv | 0:2f0e1e23c242 | 3222 | static |
glebiuskv | 0:2f0e1e23c242 | 3223 | FRESULT find_volume ( /* FR_OK(0): successful, !=0: any error occurred */ |
glebiuskv | 0:2f0e1e23c242 | 3224 | const TCHAR** path, /* Pointer to pointer to the path name (drive number) */ |
glebiuskv | 0:2f0e1e23c242 | 3225 | FATFS** rfs, /* Pointer to pointer to the found filesystem object */ |
glebiuskv | 0:2f0e1e23c242 | 3226 | BYTE mode /* !=0: Check write protection for write access */ |
glebiuskv | 0:2f0e1e23c242 | 3227 | ) |
glebiuskv | 0:2f0e1e23c242 | 3228 | { |
glebiuskv | 0:2f0e1e23c242 | 3229 | BYTE fmt, *pt; |
glebiuskv | 0:2f0e1e23c242 | 3230 | int vol; |
glebiuskv | 0:2f0e1e23c242 | 3231 | DSTATUS stat; |
glebiuskv | 0:2f0e1e23c242 | 3232 | DWORD bsect, fasize, tsect, sysect, nclst, szbfat, br[4]; |
glebiuskv | 0:2f0e1e23c242 | 3233 | WORD nrsv; |
glebiuskv | 0:2f0e1e23c242 | 3234 | FATFS *fs; |
glebiuskv | 0:2f0e1e23c242 | 3235 | UINT i; |
glebiuskv | 0:2f0e1e23c242 | 3236 | |
glebiuskv | 0:2f0e1e23c242 | 3237 | |
glebiuskv | 0:2f0e1e23c242 | 3238 | /* Get logical drive number */ |
glebiuskv | 0:2f0e1e23c242 | 3239 | *rfs = 0; |
glebiuskv | 0:2f0e1e23c242 | 3240 | vol = get_ldnumber(path); |
glebiuskv | 0:2f0e1e23c242 | 3241 | if (vol < 0) return FR_INVALID_DRIVE; |
glebiuskv | 0:2f0e1e23c242 | 3242 | |
glebiuskv | 0:2f0e1e23c242 | 3243 | /* Check if the filesystem object is valid or not */ |
glebiuskv | 0:2f0e1e23c242 | 3244 | fs = FatFs[vol]; /* Get pointer to the filesystem object */ |
glebiuskv | 0:2f0e1e23c242 | 3245 | if (!fs) return FR_NOT_ENABLED; /* Is the filesystem object available? */ |
glebiuskv | 0:2f0e1e23c242 | 3246 | #if FF_FS_REENTRANT |
glebiuskv | 0:2f0e1e23c242 | 3247 | if (!lock_fs(fs)) return FR_TIMEOUT; /* Lock the volume */ |
glebiuskv | 0:2f0e1e23c242 | 3248 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3249 | *rfs = fs; /* Return pointer to the filesystem object */ |
glebiuskv | 0:2f0e1e23c242 | 3250 | |
glebiuskv | 0:2f0e1e23c242 | 3251 | mode &= (BYTE)~FA_READ; /* Desired access mode, write access or not */ |
glebiuskv | 0:2f0e1e23c242 | 3252 | if (fs->fs_type != 0) { /* If the volume has been mounted */ |
glebiuskv | 0:2f0e1e23c242 | 3253 | stat = disk_status(fs->pdrv); |
glebiuskv | 0:2f0e1e23c242 | 3254 | if (!(stat & STA_NOINIT)) { /* and the physical drive is kept initialized */ |
glebiuskv | 0:2f0e1e23c242 | 3255 | if (!FF_FS_READONLY && mode && (stat & STA_PROTECT)) { /* Check write protection if needed */ |
glebiuskv | 0:2f0e1e23c242 | 3256 | return FR_WRITE_PROTECTED; |
glebiuskv | 0:2f0e1e23c242 | 3257 | } |
glebiuskv | 0:2f0e1e23c242 | 3258 | return FR_OK; /* The filesystem object is valid */ |
glebiuskv | 0:2f0e1e23c242 | 3259 | } |
glebiuskv | 0:2f0e1e23c242 | 3260 | } |
glebiuskv | 0:2f0e1e23c242 | 3261 | |
glebiuskv | 0:2f0e1e23c242 | 3262 | /* The filesystem object is not valid. */ |
glebiuskv | 0:2f0e1e23c242 | 3263 | /* Following code attempts to mount the volume. (analyze BPB and initialize the filesystem object) */ |
glebiuskv | 0:2f0e1e23c242 | 3264 | |
glebiuskv | 0:2f0e1e23c242 | 3265 | fs->fs_type = 0; /* Clear the filesystem object */ |
glebiuskv | 0:2f0e1e23c242 | 3266 | fs->pdrv = LD2PD(vol); /* Bind the logical drive and a physical drive */ |
glebiuskv | 0:2f0e1e23c242 | 3267 | stat = disk_initialize(fs->pdrv); /* Initialize the physical drive */ |
glebiuskv | 0:2f0e1e23c242 | 3268 | if (stat & STA_NOINIT) { /* Check if the initialization succeeded */ |
glebiuskv | 0:2f0e1e23c242 | 3269 | return FR_NOT_READY; /* Failed to initialize due to no medium or hard error */ |
glebiuskv | 0:2f0e1e23c242 | 3270 | } |
glebiuskv | 0:2f0e1e23c242 | 3271 | if (!FF_FS_READONLY && mode && (stat & STA_PROTECT)) { /* Check disk write protection if needed */ |
glebiuskv | 0:2f0e1e23c242 | 3272 | return FR_WRITE_PROTECTED; |
glebiuskv | 0:2f0e1e23c242 | 3273 | } |
glebiuskv | 0:2f0e1e23c242 | 3274 | #if FF_MAX_SS != FF_MIN_SS /* Get sector size (multiple sector size cfg only) */ |
glebiuskv | 0:2f0e1e23c242 | 3275 | if (disk_ioctl(fs->pdrv, GET_SECTOR_SIZE, &SS(fs)) != RES_OK) return FR_DISK_ERR; |
glebiuskv | 0:2f0e1e23c242 | 3276 | if (SS(fs) > FF_MAX_SS || SS(fs) < FF_MIN_SS || (SS(fs) & (SS(fs) - 1))) return FR_DISK_ERR; |
glebiuskv | 0:2f0e1e23c242 | 3277 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3278 | |
glebiuskv | 0:2f0e1e23c242 | 3279 | #if FF_FS_HEAPBUF |
glebiuskv | 0:2f0e1e23c242 | 3280 | if (!fs->win) { |
glebiuskv | 0:2f0e1e23c242 | 3281 | fs->win = (BYTE*)ff_memalloc(SS(fs)); /* Allocate buffer to back window if necessary */ |
glebiuskv | 0:2f0e1e23c242 | 3282 | if (!fs->win) |
glebiuskv | 0:2f0e1e23c242 | 3283 | return FR_NOT_ENOUGH_CORE; |
glebiuskv | 0:2f0e1e23c242 | 3284 | } |
glebiuskv | 0:2f0e1e23c242 | 3285 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3286 | |
glebiuskv | 0:2f0e1e23c242 | 3287 | /* Find an FAT partition on the drive. Supports only generic partitioning rules, FDISK and SFD. */ |
glebiuskv | 0:2f0e1e23c242 | 3288 | bsect = 0; |
glebiuskv | 0:2f0e1e23c242 | 3289 | fmt = check_fs(fs, bsect); /* Load sector 0 and check if it is an FAT-VBR as SFD */ |
glebiuskv | 0:2f0e1e23c242 | 3290 | if (fmt == 2 || (fmt < 2 && LD2PT(vol) != 0)) { /* Not an FAT-VBR or forced partition number */ |
glebiuskv | 0:2f0e1e23c242 | 3291 | for (i = 0; i < 4; i++) { /* Get partition offset */ |
glebiuskv | 0:2f0e1e23c242 | 3292 | pt = fs->win + (MBR_Table + i * SZ_PTE); |
glebiuskv | 0:2f0e1e23c242 | 3293 | br[i] = pt[PTE_System] ? ld_dword(pt + PTE_StLba) : 0; |
glebiuskv | 0:2f0e1e23c242 | 3294 | } |
glebiuskv | 0:2f0e1e23c242 | 3295 | i = LD2PT(vol); /* Partition number: 0:auto, 1-4:forced */ |
glebiuskv | 0:2f0e1e23c242 | 3296 | if (i != 0) i--; |
glebiuskv | 0:2f0e1e23c242 | 3297 | do { /* Find an FAT volume */ |
glebiuskv | 0:2f0e1e23c242 | 3298 | bsect = br[i]; |
glebiuskv | 0:2f0e1e23c242 | 3299 | fmt = bsect ? check_fs(fs, bsect) : 3; /* Check the partition */ |
glebiuskv | 0:2f0e1e23c242 | 3300 | } while (LD2PT(vol) == 0 && fmt >= 2 && ++i < 4); |
glebiuskv | 0:2f0e1e23c242 | 3301 | } |
glebiuskv | 0:2f0e1e23c242 | 3302 | if (fmt == 4) return FR_DISK_ERR; /* An error occured in the disk I/O layer */ |
glebiuskv | 0:2f0e1e23c242 | 3303 | if (fmt >= 2) return FR_NO_FILESYSTEM; /* No FAT volume is found */ |
glebiuskv | 0:2f0e1e23c242 | 3304 | |
glebiuskv | 0:2f0e1e23c242 | 3305 | /* An FAT volume is found (bsect). Following code initializes the filesystem object */ |
glebiuskv | 0:2f0e1e23c242 | 3306 | |
glebiuskv | 0:2f0e1e23c242 | 3307 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 3308 | if (fmt == 1) { |
glebiuskv | 0:2f0e1e23c242 | 3309 | QWORD maxlba; |
glebiuskv | 0:2f0e1e23c242 | 3310 | |
glebiuskv | 0:2f0e1e23c242 | 3311 | for (i = BPB_ZeroedEx; i < BPB_ZeroedEx + 53 && fs->win[i] == 0; i++) ; /* Check zero filler */ |
glebiuskv | 0:2f0e1e23c242 | 3312 | if (i < BPB_ZeroedEx + 53) return FR_NO_FILESYSTEM; |
glebiuskv | 0:2f0e1e23c242 | 3313 | |
glebiuskv | 0:2f0e1e23c242 | 3314 | if (ld_word(fs->win + BPB_FSVerEx) != 0x100) return FR_NO_FILESYSTEM; /* Check exFAT version (must be version 1.0) */ |
glebiuskv | 0:2f0e1e23c242 | 3315 | |
glebiuskv | 0:2f0e1e23c242 | 3316 | if (1 << fs->win[BPB_BytsPerSecEx] != SS(fs)) { /* (BPB_BytsPerSecEx must be equal to the physical sector size) */ |
glebiuskv | 0:2f0e1e23c242 | 3317 | return FR_NO_FILESYSTEM; |
glebiuskv | 0:2f0e1e23c242 | 3318 | } |
glebiuskv | 0:2f0e1e23c242 | 3319 | |
glebiuskv | 0:2f0e1e23c242 | 3320 | maxlba = ld_qword(fs->win + BPB_TotSecEx) + bsect; /* Last LBA + 1 of the volume */ |
glebiuskv | 0:2f0e1e23c242 | 3321 | if (maxlba >= 0x100000000) return FR_NO_FILESYSTEM; /* (It cannot be handled in 32-bit LBA) */ |
glebiuskv | 0:2f0e1e23c242 | 3322 | |
glebiuskv | 0:2f0e1e23c242 | 3323 | fs->fsize = ld_dword(fs->win + BPB_FatSzEx); /* Number of sectors per FAT */ |
glebiuskv | 0:2f0e1e23c242 | 3324 | |
glebiuskv | 0:2f0e1e23c242 | 3325 | fs->n_fats = fs->win[BPB_NumFATsEx]; /* Number of FATs */ |
glebiuskv | 0:2f0e1e23c242 | 3326 | if (fs->n_fats != 1) return FR_NO_FILESYSTEM; /* (Supports only 1 FAT) */ |
glebiuskv | 0:2f0e1e23c242 | 3327 | |
glebiuskv | 0:2f0e1e23c242 | 3328 | fs->csize = 1 << fs->win[BPB_SecPerClusEx]; /* Cluster size */ |
glebiuskv | 0:2f0e1e23c242 | 3329 | if (fs->csize == 0) return FR_NO_FILESYSTEM; /* (Must be 1..32768) */ |
glebiuskv | 0:2f0e1e23c242 | 3330 | |
glebiuskv | 0:2f0e1e23c242 | 3331 | nclst = ld_dword(fs->win + BPB_NumClusEx); /* Number of clusters */ |
glebiuskv | 0:2f0e1e23c242 | 3332 | if (nclst > MAX_EXFAT) return FR_NO_FILESYSTEM; /* (Too many clusters) */ |
glebiuskv | 0:2f0e1e23c242 | 3333 | fs->n_fatent = nclst + 2; |
glebiuskv | 0:2f0e1e23c242 | 3334 | |
glebiuskv | 0:2f0e1e23c242 | 3335 | /* Boundaries and Limits */ |
glebiuskv | 0:2f0e1e23c242 | 3336 | fs->volbase = bsect; |
glebiuskv | 0:2f0e1e23c242 | 3337 | fs->database = bsect + ld_dword(fs->win + BPB_DataOfsEx); |
glebiuskv | 0:2f0e1e23c242 | 3338 | fs->fatbase = bsect + ld_dword(fs->win + BPB_FatOfsEx); |
glebiuskv | 0:2f0e1e23c242 | 3339 | if (maxlba < (QWORD)fs->database + nclst * fs->csize) return FR_NO_FILESYSTEM; /* (Volume size must not be smaller than the size requiered) */ |
glebiuskv | 0:2f0e1e23c242 | 3340 | fs->dirbase = ld_dword(fs->win + BPB_RootClusEx); |
glebiuskv | 0:2f0e1e23c242 | 3341 | |
glebiuskv | 0:2f0e1e23c242 | 3342 | /* Check if bitmap location is in assumption (at the first cluster) */ |
glebiuskv | 0:2f0e1e23c242 | 3343 | if (move_window(fs, clst2sect(fs, fs->dirbase)) != FR_OK) return FR_DISK_ERR; |
glebiuskv | 0:2f0e1e23c242 | 3344 | for (i = 0; i < SS(fs); i += SZDIRE) { |
glebiuskv | 0:2f0e1e23c242 | 3345 | if (fs->win[i] == 0x81 && ld_dword(fs->win + i + 20) == 2) break; /* 81 entry with cluster #2? */ |
glebiuskv | 0:2f0e1e23c242 | 3346 | } |
glebiuskv | 0:2f0e1e23c242 | 3347 | if (i == SS(fs)) return FR_NO_FILESYSTEM; |
glebiuskv | 0:2f0e1e23c242 | 3348 | #if !FF_FS_READONLY |
glebiuskv | 0:2f0e1e23c242 | 3349 | fs->last_clst = fs->free_clst = 0xFFFFFFFF; /* Initialize cluster allocation information */ |
glebiuskv | 0:2f0e1e23c242 | 3350 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3351 | fmt = FS_EXFAT; /* FAT sub-type */ |
glebiuskv | 0:2f0e1e23c242 | 3352 | } else |
glebiuskv | 0:2f0e1e23c242 | 3353 | #endif /* FF_FS_EXFAT */ |
glebiuskv | 0:2f0e1e23c242 | 3354 | { |
glebiuskv | 0:2f0e1e23c242 | 3355 | if (ld_word(fs->win + BPB_BytsPerSec) != SS(fs)) return FR_NO_FILESYSTEM; /* (BPB_BytsPerSec must be equal to the physical sector size) */ |
glebiuskv | 0:2f0e1e23c242 | 3356 | |
glebiuskv | 0:2f0e1e23c242 | 3357 | fasize = ld_word(fs->win + BPB_FATSz16); /* Number of sectors per FAT */ |
glebiuskv | 0:2f0e1e23c242 | 3358 | if (fasize == 0) fasize = ld_dword(fs->win + BPB_FATSz32); |
glebiuskv | 0:2f0e1e23c242 | 3359 | fs->fsize = fasize; |
glebiuskv | 0:2f0e1e23c242 | 3360 | |
glebiuskv | 0:2f0e1e23c242 | 3361 | fs->n_fats = fs->win[BPB_NumFATs]; /* Number of FATs */ |
glebiuskv | 0:2f0e1e23c242 | 3362 | if (fs->n_fats != 1 && fs->n_fats != 2) return FR_NO_FILESYSTEM; /* (Must be 1 or 2) */ |
glebiuskv | 0:2f0e1e23c242 | 3363 | fasize *= fs->n_fats; /* Number of sectors for FAT area */ |
glebiuskv | 0:2f0e1e23c242 | 3364 | |
glebiuskv | 0:2f0e1e23c242 | 3365 | fs->csize = fs->win[BPB_SecPerClus]; /* Cluster size */ |
glebiuskv | 0:2f0e1e23c242 | 3366 | if (fs->csize == 0 || (fs->csize & (fs->csize - 1))) return FR_NO_FILESYSTEM; /* (Must be power of 2) */ |
glebiuskv | 0:2f0e1e23c242 | 3367 | |
glebiuskv | 0:2f0e1e23c242 | 3368 | fs->n_rootdir = ld_word(fs->win + BPB_RootEntCnt); /* Number of root directory entries */ |
glebiuskv | 0:2f0e1e23c242 | 3369 | if (fs->n_rootdir % (SS(fs) / SZDIRE)) return FR_NO_FILESYSTEM; /* (Must be sector aligned) */ |
glebiuskv | 0:2f0e1e23c242 | 3370 | |
glebiuskv | 0:2f0e1e23c242 | 3371 | tsect = ld_word(fs->win + BPB_TotSec16); /* Number of sectors on the volume */ |
glebiuskv | 0:2f0e1e23c242 | 3372 | if (tsect == 0) tsect = ld_dword(fs->win + BPB_TotSec32); |
glebiuskv | 0:2f0e1e23c242 | 3373 | |
glebiuskv | 0:2f0e1e23c242 | 3374 | nrsv = ld_word(fs->win + BPB_RsvdSecCnt); /* Number of reserved sectors */ |
glebiuskv | 0:2f0e1e23c242 | 3375 | if (nrsv == 0) return FR_NO_FILESYSTEM; /* (Must not be 0) */ |
glebiuskv | 0:2f0e1e23c242 | 3376 | |
glebiuskv | 0:2f0e1e23c242 | 3377 | /* Determine the FAT sub type */ |
glebiuskv | 0:2f0e1e23c242 | 3378 | sysect = nrsv + fasize + fs->n_rootdir / (SS(fs) / SZDIRE); /* RSV + FAT + FATFS_DIR */ |
glebiuskv | 0:2f0e1e23c242 | 3379 | if (tsect < sysect) return FR_NO_FILESYSTEM; /* (Invalid volume size) */ |
glebiuskv | 0:2f0e1e23c242 | 3380 | nclst = (tsect - sysect) / fs->csize; /* Number of clusters */ |
glebiuskv | 0:2f0e1e23c242 | 3381 | if (nclst == 0) return FR_NO_FILESYSTEM; /* (Invalid volume size) */ |
glebiuskv | 0:2f0e1e23c242 | 3382 | fmt = 0; |
glebiuskv | 0:2f0e1e23c242 | 3383 | if (nclst <= MAX_FAT32) fmt = FS_FAT32; |
glebiuskv | 0:2f0e1e23c242 | 3384 | if (nclst <= MAX_FAT16) fmt = FS_FAT16; |
glebiuskv | 0:2f0e1e23c242 | 3385 | if (nclst <= MAX_FAT12) fmt = FS_FAT12; |
glebiuskv | 0:2f0e1e23c242 | 3386 | if (fmt == 0) return FR_NO_FILESYSTEM; |
glebiuskv | 0:2f0e1e23c242 | 3387 | |
glebiuskv | 0:2f0e1e23c242 | 3388 | /* Boundaries and Limits */ |
glebiuskv | 0:2f0e1e23c242 | 3389 | fs->n_fatent = nclst + 2; /* Number of FAT entries */ |
glebiuskv | 0:2f0e1e23c242 | 3390 | fs->volbase = bsect; /* Volume start sector */ |
glebiuskv | 0:2f0e1e23c242 | 3391 | fs->fatbase = bsect + nrsv; /* FAT start sector */ |
glebiuskv | 0:2f0e1e23c242 | 3392 | fs->database = bsect + sysect; /* Data start sector */ |
glebiuskv | 0:2f0e1e23c242 | 3393 | if (fmt == FS_FAT32) { |
glebiuskv | 0:2f0e1e23c242 | 3394 | if (ld_word(fs->win + BPB_FSVer32) != 0) return FR_NO_FILESYSTEM; /* (Must be FAT32 revision 0.0) */ |
glebiuskv | 0:2f0e1e23c242 | 3395 | if (fs->n_rootdir != 0) return FR_NO_FILESYSTEM; /* (BPB_RootEntCnt must be 0) */ |
glebiuskv | 0:2f0e1e23c242 | 3396 | fs->dirbase = ld_dword(fs->win + BPB_RootClus32); /* Root directory start cluster */ |
glebiuskv | 0:2f0e1e23c242 | 3397 | szbfat = fs->n_fatent * 4; /* (Needed FAT size) */ |
glebiuskv | 0:2f0e1e23c242 | 3398 | } else { |
glebiuskv | 0:2f0e1e23c242 | 3399 | if (fs->n_rootdir == 0) return FR_NO_FILESYSTEM; /* (BPB_RootEntCnt must not be 0) */ |
glebiuskv | 0:2f0e1e23c242 | 3400 | fs->dirbase = fs->fatbase + fasize; /* Root directory start sector */ |
glebiuskv | 0:2f0e1e23c242 | 3401 | szbfat = (fmt == FS_FAT16) ? /* (Needed FAT size) */ |
glebiuskv | 0:2f0e1e23c242 | 3402 | fs->n_fatent * 2 : fs->n_fatent * 3 / 2 + (fs->n_fatent & 1); |
glebiuskv | 0:2f0e1e23c242 | 3403 | } |
glebiuskv | 0:2f0e1e23c242 | 3404 | if (fs->fsize < (szbfat + (SS(fs) - 1)) / SS(fs)) return FR_NO_FILESYSTEM; /* (BPB_FATSz must not be less than the size needed) */ |
glebiuskv | 0:2f0e1e23c242 | 3405 | |
glebiuskv | 0:2f0e1e23c242 | 3406 | #if !FF_FS_READONLY |
glebiuskv | 0:2f0e1e23c242 | 3407 | /* Get FSInfo if available */ |
glebiuskv | 0:2f0e1e23c242 | 3408 | fs->last_clst = fs->free_clst = 0xFFFFFFFF; /* Initialize cluster allocation information */ |
glebiuskv | 0:2f0e1e23c242 | 3409 | fs->fsi_flag = 0x80; |
glebiuskv | 0:2f0e1e23c242 | 3410 | #if (FF_FS_NOFSINFO & 3) != 3 |
glebiuskv | 0:2f0e1e23c242 | 3411 | if (fmt == FS_FAT32 /* Allow to update FSInfo only if BPB_FSInfo32 == 1 */ |
glebiuskv | 0:2f0e1e23c242 | 3412 | && ld_word(fs->win + BPB_FSInfo32) == 1 |
glebiuskv | 0:2f0e1e23c242 | 3413 | && move_window(fs, bsect + 1) == FR_OK) |
glebiuskv | 0:2f0e1e23c242 | 3414 | { |
glebiuskv | 0:2f0e1e23c242 | 3415 | fs->fsi_flag = 0; |
glebiuskv | 0:2f0e1e23c242 | 3416 | if (ld_word(fs->win + BS_55AA) == 0xAA55 /* Load FSInfo data if available */ |
glebiuskv | 0:2f0e1e23c242 | 3417 | && ld_dword(fs->win + FSI_LeadSig) == 0x41615252 |
glebiuskv | 0:2f0e1e23c242 | 3418 | && ld_dword(fs->win + FSI_StrucSig) == 0x61417272) |
glebiuskv | 0:2f0e1e23c242 | 3419 | { |
glebiuskv | 0:2f0e1e23c242 | 3420 | #if (FF_FS_NOFSINFO & 1) == 0 |
glebiuskv | 0:2f0e1e23c242 | 3421 | fs->free_clst = ld_dword(fs->win + FSI_Free_Count); |
glebiuskv | 0:2f0e1e23c242 | 3422 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3423 | #if (FF_FS_NOFSINFO & 2) == 0 |
glebiuskv | 0:2f0e1e23c242 | 3424 | fs->last_clst = ld_dword(fs->win + FSI_Nxt_Free); |
glebiuskv | 0:2f0e1e23c242 | 3425 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3426 | } |
glebiuskv | 0:2f0e1e23c242 | 3427 | } |
glebiuskv | 0:2f0e1e23c242 | 3428 | #endif /* (FF_FS_NOFSINFO & 3) != 3 */ |
glebiuskv | 0:2f0e1e23c242 | 3429 | #endif /* !FF_FS_READONLY */ |
glebiuskv | 0:2f0e1e23c242 | 3430 | } |
glebiuskv | 0:2f0e1e23c242 | 3431 | |
glebiuskv | 0:2f0e1e23c242 | 3432 | fs->fs_type = fmt; /* FAT sub-type */ |
glebiuskv | 0:2f0e1e23c242 | 3433 | fs->id = ++Fsid; /* Volume mount ID */ |
glebiuskv | 0:2f0e1e23c242 | 3434 | #if FF_USE_LFN == 1 |
glebiuskv | 0:2f0e1e23c242 | 3435 | fs->lfnbuf = LfnBuf; /* Static LFN working buffer */ |
glebiuskv | 0:2f0e1e23c242 | 3436 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 3437 | fs->dirbuf = DirBuf; /* Static directory block scratchpad buuffer */ |
glebiuskv | 0:2f0e1e23c242 | 3438 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3439 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3440 | #if FF_FS_RPATH != 0 |
glebiuskv | 0:2f0e1e23c242 | 3441 | fs->cdir = 0; /* Initialize current directory */ |
glebiuskv | 0:2f0e1e23c242 | 3442 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3443 | #if FF_FS_LOCK != 0 /* Clear file lock semaphores */ |
glebiuskv | 0:2f0e1e23c242 | 3444 | clear_lock(fs); |
glebiuskv | 0:2f0e1e23c242 | 3445 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3446 | return FR_OK; |
glebiuskv | 0:2f0e1e23c242 | 3447 | } |
glebiuskv | 0:2f0e1e23c242 | 3448 | |
glebiuskv | 0:2f0e1e23c242 | 3449 | |
glebiuskv | 0:2f0e1e23c242 | 3450 | |
glebiuskv | 0:2f0e1e23c242 | 3451 | |
glebiuskv | 0:2f0e1e23c242 | 3452 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 3453 | /* Check if the file/directory object is valid or not */ |
glebiuskv | 0:2f0e1e23c242 | 3454 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 3455 | |
glebiuskv | 0:2f0e1e23c242 | 3456 | static |
glebiuskv | 0:2f0e1e23c242 | 3457 | FRESULT validate ( /* Returns FR_OK or FR_INVALID_OBJECT */ |
glebiuskv | 0:2f0e1e23c242 | 3458 | FFOBJID* obj, /* Pointer to the FFOBJID, the 1st member in the FIL/FATFS_DIR object, to check validity */ |
glebiuskv | 0:2f0e1e23c242 | 3459 | FATFS** rfs /* Pointer to pointer to the owner filesystem object to return */ |
glebiuskv | 0:2f0e1e23c242 | 3460 | ) |
glebiuskv | 0:2f0e1e23c242 | 3461 | { |
glebiuskv | 0:2f0e1e23c242 | 3462 | FRESULT res = FR_INVALID_OBJECT; |
glebiuskv | 0:2f0e1e23c242 | 3463 | |
glebiuskv | 0:2f0e1e23c242 | 3464 | |
glebiuskv | 0:2f0e1e23c242 | 3465 | if (obj && obj->fs && obj->fs->fs_type && obj->id == obj->fs->id) { /* Test if the object is valid */ |
glebiuskv | 0:2f0e1e23c242 | 3466 | #if FF_FS_REENTRANT |
glebiuskv | 0:2f0e1e23c242 | 3467 | if (lock_fs(obj->fs)) { /* Obtain the filesystem object */ |
glebiuskv | 0:2f0e1e23c242 | 3468 | if (!(disk_status(obj->fs->pdrv) & STA_NOINIT)) { /* Test if the phsical drive is kept initialized */ |
glebiuskv | 0:2f0e1e23c242 | 3469 | res = FR_OK; |
glebiuskv | 0:2f0e1e23c242 | 3470 | } else { |
glebiuskv | 0:2f0e1e23c242 | 3471 | unlock_fs(obj->fs, FR_OK); |
glebiuskv | 0:2f0e1e23c242 | 3472 | } |
glebiuskv | 0:2f0e1e23c242 | 3473 | } else { |
glebiuskv | 0:2f0e1e23c242 | 3474 | res = FR_TIMEOUT; |
glebiuskv | 0:2f0e1e23c242 | 3475 | } |
glebiuskv | 0:2f0e1e23c242 | 3476 | #else |
glebiuskv | 0:2f0e1e23c242 | 3477 | if (!(disk_status(obj->fs->pdrv) & STA_NOINIT)) { /* Test if the phsical drive is kept initialized */ |
glebiuskv | 0:2f0e1e23c242 | 3478 | res = FR_OK; |
glebiuskv | 0:2f0e1e23c242 | 3479 | } |
glebiuskv | 0:2f0e1e23c242 | 3480 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3481 | } |
glebiuskv | 0:2f0e1e23c242 | 3482 | *rfs = (res == FR_OK) ? obj->fs : 0; /* Corresponding filesystem object */ |
glebiuskv | 0:2f0e1e23c242 | 3483 | return res; |
glebiuskv | 0:2f0e1e23c242 | 3484 | } |
glebiuskv | 0:2f0e1e23c242 | 3485 | |
glebiuskv | 0:2f0e1e23c242 | 3486 | |
glebiuskv | 0:2f0e1e23c242 | 3487 | |
glebiuskv | 0:2f0e1e23c242 | 3488 | |
glebiuskv | 0:2f0e1e23c242 | 3489 | /*--------------------------------------------------------------------------- |
glebiuskv | 0:2f0e1e23c242 | 3490 | |
glebiuskv | 0:2f0e1e23c242 | 3491 | Public Functions (FatFs API) |
glebiuskv | 0:2f0e1e23c242 | 3492 | |
glebiuskv | 0:2f0e1e23c242 | 3493 | ----------------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 3494 | |
glebiuskv | 0:2f0e1e23c242 | 3495 | |
glebiuskv | 0:2f0e1e23c242 | 3496 | |
glebiuskv | 0:2f0e1e23c242 | 3497 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 3498 | /* Mount/Unmount a Logical Drive */ |
glebiuskv | 0:2f0e1e23c242 | 3499 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 3500 | |
glebiuskv | 0:2f0e1e23c242 | 3501 | FRESULT f_mount ( |
glebiuskv | 0:2f0e1e23c242 | 3502 | FATFS* fs, /* Pointer to the filesystem object (NULL:unmount)*/ |
glebiuskv | 0:2f0e1e23c242 | 3503 | const TCHAR* path, /* Logical drive number to be mounted/unmounted */ |
glebiuskv | 0:2f0e1e23c242 | 3504 | BYTE opt /* Mode option 0:Do not mount (delayed mount), 1:Mount immediately */ |
glebiuskv | 0:2f0e1e23c242 | 3505 | ) |
glebiuskv | 0:2f0e1e23c242 | 3506 | { |
glebiuskv | 0:2f0e1e23c242 | 3507 | FATFS *cfs; |
glebiuskv | 0:2f0e1e23c242 | 3508 | int vol; |
glebiuskv | 0:2f0e1e23c242 | 3509 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 3510 | const TCHAR *rp = path; |
glebiuskv | 0:2f0e1e23c242 | 3511 | |
glebiuskv | 0:2f0e1e23c242 | 3512 | |
glebiuskv | 0:2f0e1e23c242 | 3513 | /* Get logical drive number */ |
glebiuskv | 0:2f0e1e23c242 | 3514 | vol = get_ldnumber(&rp); |
glebiuskv | 0:2f0e1e23c242 | 3515 | if (vol < 0) return FR_INVALID_DRIVE; |
glebiuskv | 0:2f0e1e23c242 | 3516 | cfs = FatFs[vol]; /* Pointer to fs object */ |
glebiuskv | 0:2f0e1e23c242 | 3517 | |
glebiuskv | 0:2f0e1e23c242 | 3518 | if (cfs) { |
glebiuskv | 0:2f0e1e23c242 | 3519 | #if FF_FS_LOCK != 0 |
glebiuskv | 0:2f0e1e23c242 | 3520 | clear_lock(cfs); |
glebiuskv | 0:2f0e1e23c242 | 3521 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3522 | #if FF_FS_REENTRANT /* Discard sync object of the current volume */ |
glebiuskv | 0:2f0e1e23c242 | 3523 | if (!ff_del_syncobj(cfs->sobj)) return FR_INT_ERR; |
glebiuskv | 0:2f0e1e23c242 | 3524 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3525 | cfs->fs_type = 0; /* Clear old fs object */ |
glebiuskv | 0:2f0e1e23c242 | 3526 | #if FF_FS_HEAPBUF |
glebiuskv | 0:2f0e1e23c242 | 3527 | ff_memfree(cfs->win); /* Clean up window buffer */ |
glebiuskv | 0:2f0e1e23c242 | 3528 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3529 | } |
glebiuskv | 0:2f0e1e23c242 | 3530 | |
glebiuskv | 0:2f0e1e23c242 | 3531 | if (fs) { |
glebiuskv | 0:2f0e1e23c242 | 3532 | fs->fs_type = 0; /* Clear new fs object */ |
glebiuskv | 0:2f0e1e23c242 | 3533 | #if FF_FS_REENTRANT /* Create sync object for the new volume */ |
glebiuskv | 0:2f0e1e23c242 | 3534 | if (!ff_cre_syncobj((BYTE)vol, &fs->sobj)) return FR_INT_ERR; |
glebiuskv | 0:2f0e1e23c242 | 3535 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3536 | #if FF_FS_HEAPBUF |
glebiuskv | 0:2f0e1e23c242 | 3537 | fs->win = 0; /* NULL buffer to prevent use of uninitialized buffer */ |
glebiuskv | 0:2f0e1e23c242 | 3538 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3539 | } |
glebiuskv | 0:2f0e1e23c242 | 3540 | FatFs[vol] = fs; /* Register new fs object */ |
glebiuskv | 0:2f0e1e23c242 | 3541 | |
glebiuskv | 0:2f0e1e23c242 | 3542 | if (opt == 0) return FR_OK; /* Do not mount now, it will be mounted later */ |
glebiuskv | 0:2f0e1e23c242 | 3543 | |
glebiuskv | 0:2f0e1e23c242 | 3544 | res = find_volume(&path, &fs, 0); /* Force mounted the volume */ |
glebiuskv | 0:2f0e1e23c242 | 3545 | LEAVE_FF(fs, res); |
glebiuskv | 0:2f0e1e23c242 | 3546 | } |
glebiuskv | 0:2f0e1e23c242 | 3547 | |
glebiuskv | 0:2f0e1e23c242 | 3548 | |
glebiuskv | 0:2f0e1e23c242 | 3549 | |
glebiuskv | 0:2f0e1e23c242 | 3550 | |
glebiuskv | 0:2f0e1e23c242 | 3551 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 3552 | /* Open or Create a File */ |
glebiuskv | 0:2f0e1e23c242 | 3553 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 3554 | |
glebiuskv | 0:2f0e1e23c242 | 3555 | FRESULT f_open ( |
glebiuskv | 0:2f0e1e23c242 | 3556 | FIL* fp, /* Pointer to the blank file object */ |
glebiuskv | 0:2f0e1e23c242 | 3557 | const TCHAR* path, /* Pointer to the file name */ |
glebiuskv | 0:2f0e1e23c242 | 3558 | BYTE mode /* Access mode and file open mode flags */ |
glebiuskv | 0:2f0e1e23c242 | 3559 | ) |
glebiuskv | 0:2f0e1e23c242 | 3560 | { |
glebiuskv | 0:2f0e1e23c242 | 3561 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 3562 | FATFS_DIR dj; |
glebiuskv | 0:2f0e1e23c242 | 3563 | FATFS *fs; |
glebiuskv | 0:2f0e1e23c242 | 3564 | #if !FF_FS_READONLY |
glebiuskv | 0:2f0e1e23c242 | 3565 | DWORD dw, cl, bcs, clst, sc; |
glebiuskv | 0:2f0e1e23c242 | 3566 | FSIZE_t ofs; |
glebiuskv | 0:2f0e1e23c242 | 3567 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3568 | DEF_NAMBUF |
glebiuskv | 0:2f0e1e23c242 | 3569 | |
glebiuskv | 0:2f0e1e23c242 | 3570 | |
glebiuskv | 0:2f0e1e23c242 | 3571 | if (!fp) return FR_INVALID_OBJECT; |
glebiuskv | 0:2f0e1e23c242 | 3572 | |
glebiuskv | 0:2f0e1e23c242 | 3573 | /* Get logical drive */ |
glebiuskv | 0:2f0e1e23c242 | 3574 | mode &= FF_FS_READONLY ? FA_READ : FA_READ | FA_WRITE | FA_CREATE_ALWAYS | FA_CREATE_NEW | FA_OPEN_ALWAYS | FA_OPEN_APPEND; |
glebiuskv | 0:2f0e1e23c242 | 3575 | res = find_volume(&path, &fs, mode); |
glebiuskv | 0:2f0e1e23c242 | 3576 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 3577 | dj.obj.fs = fs; |
glebiuskv | 0:2f0e1e23c242 | 3578 | INIT_NAMBUF(fs); |
glebiuskv | 0:2f0e1e23c242 | 3579 | res = follow_path(&dj, path); /* Follow the file path */ |
glebiuskv | 0:2f0e1e23c242 | 3580 | #if !FF_FS_READONLY /* Read/Write configuration */ |
glebiuskv | 0:2f0e1e23c242 | 3581 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 3582 | if (dj.fn[NSFLAG] & NS_NONAME) { /* Origin directory itself? */ |
glebiuskv | 0:2f0e1e23c242 | 3583 | res = FR_INVALID_NAME; |
glebiuskv | 0:2f0e1e23c242 | 3584 | } |
glebiuskv | 0:2f0e1e23c242 | 3585 | #if FF_FS_LOCK != 0 |
glebiuskv | 0:2f0e1e23c242 | 3586 | else { |
glebiuskv | 0:2f0e1e23c242 | 3587 | res = chk_lock(&dj, (mode & ~FA_READ) ? 1 : 0); /* Check if the file can be used */ |
glebiuskv | 0:2f0e1e23c242 | 3588 | } |
glebiuskv | 0:2f0e1e23c242 | 3589 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3590 | } |
glebiuskv | 0:2f0e1e23c242 | 3591 | /* Create or Open a file */ |
glebiuskv | 0:2f0e1e23c242 | 3592 | if (mode & (FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW)) { |
glebiuskv | 0:2f0e1e23c242 | 3593 | if (res != FR_OK) { /* No file, create new */ |
glebiuskv | 0:2f0e1e23c242 | 3594 | if (res == FR_NO_FILE) { /* There is no file to open, create a new entry */ |
glebiuskv | 0:2f0e1e23c242 | 3595 | #if FF_FS_LOCK != 0 |
glebiuskv | 0:2f0e1e23c242 | 3596 | res = enq_lock() ? dir_register(&dj) : FR_TOO_MANY_OPEN_FILES; |
glebiuskv | 0:2f0e1e23c242 | 3597 | #else |
glebiuskv | 0:2f0e1e23c242 | 3598 | res = dir_register(&dj); |
glebiuskv | 0:2f0e1e23c242 | 3599 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3600 | } |
glebiuskv | 0:2f0e1e23c242 | 3601 | mode |= FA_CREATE_ALWAYS; /* File is created */ |
glebiuskv | 0:2f0e1e23c242 | 3602 | } |
glebiuskv | 0:2f0e1e23c242 | 3603 | else { /* Any object with the same name is already existing */ |
glebiuskv | 0:2f0e1e23c242 | 3604 | if (dj.obj.attr & (AM_RDO | AM_DIR)) { /* Cannot overwrite it (R/O or FATFS_DIR) */ |
glebiuskv | 0:2f0e1e23c242 | 3605 | res = FR_DENIED; |
glebiuskv | 0:2f0e1e23c242 | 3606 | } else { |
glebiuskv | 0:2f0e1e23c242 | 3607 | if (mode & FA_CREATE_NEW) res = FR_EXIST; /* Cannot create as new file */ |
glebiuskv | 0:2f0e1e23c242 | 3608 | } |
glebiuskv | 0:2f0e1e23c242 | 3609 | } |
glebiuskv | 0:2f0e1e23c242 | 3610 | if (res == FR_OK && (mode & FA_CREATE_ALWAYS)) { /* Truncate the file if overwrite mode */ |
glebiuskv | 0:2f0e1e23c242 | 3611 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 3612 | if (fs->fs_type == FS_EXFAT) { |
glebiuskv | 0:2f0e1e23c242 | 3613 | /* Get current allocation info */ |
glebiuskv | 0:2f0e1e23c242 | 3614 | fp->obj.fs = fs; |
glebiuskv | 0:2f0e1e23c242 | 3615 | init_alloc_info(fs, &fp->obj); |
glebiuskv | 0:2f0e1e23c242 | 3616 | /* Set directory entry block initial state */ |
glebiuskv | 0:2f0e1e23c242 | 3617 | mem_set(fs->dirbuf + 2, 0, 30); /* Clear 85 entry except for NumSec */ |
glebiuskv | 0:2f0e1e23c242 | 3618 | mem_set(fs->dirbuf + 38, 0, 26); /* Clear C0 entry except for NumName and NameHash */ |
glebiuskv | 0:2f0e1e23c242 | 3619 | fs->dirbuf[XDIR_Attr] = AM_ARC; |
glebiuskv | 0:2f0e1e23c242 | 3620 | st_dword(fs->dirbuf + XDIR_CrtTime, GET_FATTIME()); |
glebiuskv | 0:2f0e1e23c242 | 3621 | fs->dirbuf[XDIR_GenFlags] = 1; |
glebiuskv | 0:2f0e1e23c242 | 3622 | res = store_xdir(&dj); |
glebiuskv | 0:2f0e1e23c242 | 3623 | if (res == FR_OK && fp->obj.sclust != 0) { /* Remove the cluster chain if exist */ |
glebiuskv | 0:2f0e1e23c242 | 3624 | res = remove_chain(&fp->obj, fp->obj.sclust, 0); |
glebiuskv | 0:2f0e1e23c242 | 3625 | fs->last_clst = fp->obj.sclust - 1; /* Reuse the cluster hole */ |
glebiuskv | 0:2f0e1e23c242 | 3626 | } |
glebiuskv | 0:2f0e1e23c242 | 3627 | } else |
glebiuskv | 0:2f0e1e23c242 | 3628 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3629 | { |
glebiuskv | 0:2f0e1e23c242 | 3630 | /* Set directory entry initial state */ |
glebiuskv | 0:2f0e1e23c242 | 3631 | cl = ld_clust(fs, dj.dir); /* Get current cluster chain */ |
glebiuskv | 0:2f0e1e23c242 | 3632 | st_dword(dj.dir + DIR_CrtTime, GET_FATTIME()); /* Set created time */ |
glebiuskv | 0:2f0e1e23c242 | 3633 | dj.dir[DIR_Attr] = AM_ARC; /* Reset attribute */ |
glebiuskv | 0:2f0e1e23c242 | 3634 | st_clust(fs, dj.dir, 0); /* Reset file allocation info */ |
glebiuskv | 0:2f0e1e23c242 | 3635 | st_dword(dj.dir + DIR_FileSize, 0); |
glebiuskv | 0:2f0e1e23c242 | 3636 | fs->wflag = 1; |
glebiuskv | 0:2f0e1e23c242 | 3637 | if (cl != 0) { /* Remove the cluster chain if exist */ |
glebiuskv | 0:2f0e1e23c242 | 3638 | dw = fs->winsect; |
glebiuskv | 0:2f0e1e23c242 | 3639 | res = remove_chain(&dj.obj, cl, 0); |
glebiuskv | 0:2f0e1e23c242 | 3640 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 3641 | res = move_window(fs, dw); |
glebiuskv | 0:2f0e1e23c242 | 3642 | fs->last_clst = cl - 1; /* Reuse the cluster hole */ |
glebiuskv | 0:2f0e1e23c242 | 3643 | } |
glebiuskv | 0:2f0e1e23c242 | 3644 | } |
glebiuskv | 0:2f0e1e23c242 | 3645 | } |
glebiuskv | 0:2f0e1e23c242 | 3646 | } |
glebiuskv | 0:2f0e1e23c242 | 3647 | } |
glebiuskv | 0:2f0e1e23c242 | 3648 | else { /* Open an existing file */ |
glebiuskv | 0:2f0e1e23c242 | 3649 | if (res == FR_OK) { /* Is the object exsiting? */ |
glebiuskv | 0:2f0e1e23c242 | 3650 | if (dj.obj.attr & AM_DIR) { /* File open against a directory */ |
glebiuskv | 0:2f0e1e23c242 | 3651 | res = FR_NO_FILE; |
glebiuskv | 0:2f0e1e23c242 | 3652 | } else { |
glebiuskv | 0:2f0e1e23c242 | 3653 | if ((mode & FA_WRITE) && (dj.obj.attr & AM_RDO)) { /* Write mode open against R/O file */ |
glebiuskv | 0:2f0e1e23c242 | 3654 | res = FR_DENIED; |
glebiuskv | 0:2f0e1e23c242 | 3655 | } |
glebiuskv | 0:2f0e1e23c242 | 3656 | } |
glebiuskv | 0:2f0e1e23c242 | 3657 | } |
glebiuskv | 0:2f0e1e23c242 | 3658 | } |
glebiuskv | 0:2f0e1e23c242 | 3659 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 3660 | if (mode & FA_CREATE_ALWAYS) mode |= FA_MODIFIED; /* Set file change flag if created or overwritten */ |
glebiuskv | 0:2f0e1e23c242 | 3661 | fp->dir_sect = fs->winsect; /* Pointer to the directory entry */ |
glebiuskv | 0:2f0e1e23c242 | 3662 | fp->dir_ptr = dj.dir; |
glebiuskv | 0:2f0e1e23c242 | 3663 | #if FF_FS_LOCK != 0 |
glebiuskv | 0:2f0e1e23c242 | 3664 | fp->obj.lockid = inc_lock(&dj, (mode & ~FA_READ) ? 1 : 0); /* Lock the file for this session */ |
glebiuskv | 0:2f0e1e23c242 | 3665 | if (fp->obj.lockid == 0) res = FR_INT_ERR; |
glebiuskv | 0:2f0e1e23c242 | 3666 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3667 | } |
glebiuskv | 0:2f0e1e23c242 | 3668 | #else /* R/O configuration */ |
glebiuskv | 0:2f0e1e23c242 | 3669 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 3670 | if (dj.fn[NSFLAG] & NS_NONAME) { /* Is it origin directory itself? */ |
glebiuskv | 0:2f0e1e23c242 | 3671 | res = FR_INVALID_NAME; |
glebiuskv | 0:2f0e1e23c242 | 3672 | } else { |
glebiuskv | 0:2f0e1e23c242 | 3673 | if (dj.obj.attr & AM_DIR) { /* Is it a directory? */ |
glebiuskv | 0:2f0e1e23c242 | 3674 | res = FR_NO_FILE; |
glebiuskv | 0:2f0e1e23c242 | 3675 | } |
glebiuskv | 0:2f0e1e23c242 | 3676 | } |
glebiuskv | 0:2f0e1e23c242 | 3677 | } |
glebiuskv | 0:2f0e1e23c242 | 3678 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3679 | |
glebiuskv | 0:2f0e1e23c242 | 3680 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 3681 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 3682 | if (fs->fs_type == FS_EXFAT) { |
glebiuskv | 0:2f0e1e23c242 | 3683 | fp->obj.c_scl = dj.obj.sclust; /* Get containing directory info */ |
glebiuskv | 0:2f0e1e23c242 | 3684 | fp->obj.c_size = ((DWORD)dj.obj.objsize & 0xFFFFFF00) | dj.obj.stat; |
glebiuskv | 0:2f0e1e23c242 | 3685 | fp->obj.c_ofs = dj.blk_ofs; |
glebiuskv | 0:2f0e1e23c242 | 3686 | init_alloc_info(fs, &fp->obj); |
glebiuskv | 0:2f0e1e23c242 | 3687 | } else |
glebiuskv | 0:2f0e1e23c242 | 3688 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3689 | { |
glebiuskv | 0:2f0e1e23c242 | 3690 | fp->obj.sclust = ld_clust(fs, dj.dir); /* Get object allocation info */ |
glebiuskv | 0:2f0e1e23c242 | 3691 | fp->obj.objsize = ld_dword(dj.dir + DIR_FileSize); |
glebiuskv | 0:2f0e1e23c242 | 3692 | } |
glebiuskv | 0:2f0e1e23c242 | 3693 | #if FF_USE_FASTSEEK |
glebiuskv | 0:2f0e1e23c242 | 3694 | fp->cltbl = 0; /* Disable fast seek mode */ |
glebiuskv | 0:2f0e1e23c242 | 3695 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3696 | fp->obj.fs = fs; /* Validate the file object */ |
glebiuskv | 0:2f0e1e23c242 | 3697 | fp->obj.id = fs->id; |
glebiuskv | 0:2f0e1e23c242 | 3698 | fp->flag = mode; /* Set file access mode */ |
glebiuskv | 0:2f0e1e23c242 | 3699 | fp->err = 0; /* Clear error flag */ |
glebiuskv | 0:2f0e1e23c242 | 3700 | fp->sect = 0; /* Invalidate current data sector */ |
glebiuskv | 0:2f0e1e23c242 | 3701 | fp->fptr = 0; /* Set file pointer top of the file */ |
glebiuskv | 0:2f0e1e23c242 | 3702 | #if !FF_FS_READONLY |
glebiuskv | 0:2f0e1e23c242 | 3703 | #if !FF_FS_TINY |
glebiuskv | 0:2f0e1e23c242 | 3704 | #if FF_FS_HEAPBUF |
glebiuskv | 0:2f0e1e23c242 | 3705 | fp->buf = (BYTE*)ff_memalloc(SS(dj.obj.fs)); /* Allocate buffer if necessary */ |
glebiuskv | 0:2f0e1e23c242 | 3706 | if (!fp->buf) |
glebiuskv | 0:2f0e1e23c242 | 3707 | return FR_NOT_ENOUGH_CORE; |
glebiuskv | 0:2f0e1e23c242 | 3708 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3709 | mem_set(fp->buf, 0, FF_MAX_SS); /* Clear sector buffer */ |
glebiuskv | 0:2f0e1e23c242 | 3710 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3711 | if ((mode & FA_SEEKEND) && fp->obj.objsize > 0) { /* Seek to end of file if FA_OPEN_APPEND is specified */ |
glebiuskv | 0:2f0e1e23c242 | 3712 | fp->fptr = fp->obj.objsize; /* Offset to seek */ |
glebiuskv | 0:2f0e1e23c242 | 3713 | bcs = (DWORD)fs->csize * SS(fs); /* Cluster size in byte */ |
glebiuskv | 0:2f0e1e23c242 | 3714 | clst = fp->obj.sclust; /* Follow the cluster chain */ |
glebiuskv | 0:2f0e1e23c242 | 3715 | for (ofs = fp->obj.objsize; res == FR_OK && ofs > bcs; ofs -= bcs) { |
glebiuskv | 0:2f0e1e23c242 | 3716 | clst = get_fat(&fp->obj, clst); |
glebiuskv | 0:2f0e1e23c242 | 3717 | if (clst <= 1) res = FR_INT_ERR; |
glebiuskv | 0:2f0e1e23c242 | 3718 | if (clst == 0xFFFFFFFF) res = FR_DISK_ERR; |
glebiuskv | 0:2f0e1e23c242 | 3719 | } |
glebiuskv | 0:2f0e1e23c242 | 3720 | fp->clust = clst; |
glebiuskv | 0:2f0e1e23c242 | 3721 | if (res == FR_OK && ofs % SS(fs)) { /* Fill sector buffer if not on the sector boundary */ |
glebiuskv | 0:2f0e1e23c242 | 3722 | if ((sc = clst2sect(fs, clst)) == 0) { |
glebiuskv | 0:2f0e1e23c242 | 3723 | res = FR_INT_ERR; |
glebiuskv | 0:2f0e1e23c242 | 3724 | } else { |
glebiuskv | 0:2f0e1e23c242 | 3725 | fp->sect = sc + (DWORD)(ofs / SS(fs)); |
glebiuskv | 0:2f0e1e23c242 | 3726 | #if !FF_FS_TINY |
glebiuskv | 0:2f0e1e23c242 | 3727 | if (disk_read(fs->pdrv, fp->buf, fp->sect, 1) != RES_OK) res = FR_DISK_ERR; |
glebiuskv | 0:2f0e1e23c242 | 3728 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3729 | } |
glebiuskv | 0:2f0e1e23c242 | 3730 | } |
glebiuskv | 0:2f0e1e23c242 | 3731 | } |
glebiuskv | 0:2f0e1e23c242 | 3732 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3733 | } |
glebiuskv | 0:2f0e1e23c242 | 3734 | |
glebiuskv | 0:2f0e1e23c242 | 3735 | FREE_NAMBUF(); |
glebiuskv | 0:2f0e1e23c242 | 3736 | } |
glebiuskv | 0:2f0e1e23c242 | 3737 | |
glebiuskv | 0:2f0e1e23c242 | 3738 | if (res != FR_OK) fp->obj.fs = 0; /* Invalidate file object on error */ |
glebiuskv | 0:2f0e1e23c242 | 3739 | |
glebiuskv | 0:2f0e1e23c242 | 3740 | LEAVE_FF(fs, res); |
glebiuskv | 0:2f0e1e23c242 | 3741 | } |
glebiuskv | 0:2f0e1e23c242 | 3742 | |
glebiuskv | 0:2f0e1e23c242 | 3743 | |
glebiuskv | 0:2f0e1e23c242 | 3744 | |
glebiuskv | 0:2f0e1e23c242 | 3745 | |
glebiuskv | 0:2f0e1e23c242 | 3746 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 3747 | /* Read File */ |
glebiuskv | 0:2f0e1e23c242 | 3748 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 3749 | |
glebiuskv | 0:2f0e1e23c242 | 3750 | FRESULT f_read ( |
glebiuskv | 0:2f0e1e23c242 | 3751 | FIL* fp, /* Pointer to the file object */ |
glebiuskv | 0:2f0e1e23c242 | 3752 | void* buff, /* Pointer to data buffer */ |
glebiuskv | 0:2f0e1e23c242 | 3753 | UINT btr, /* Number of bytes to read */ |
glebiuskv | 0:2f0e1e23c242 | 3754 | UINT* br /* Pointer to number of bytes read */ |
glebiuskv | 0:2f0e1e23c242 | 3755 | ) |
glebiuskv | 0:2f0e1e23c242 | 3756 | { |
glebiuskv | 0:2f0e1e23c242 | 3757 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 3758 | FATFS *fs; |
glebiuskv | 0:2f0e1e23c242 | 3759 | DWORD clst, sect; |
glebiuskv | 0:2f0e1e23c242 | 3760 | FSIZE_t remain; |
glebiuskv | 0:2f0e1e23c242 | 3761 | UINT rcnt, cc, csect; |
glebiuskv | 0:2f0e1e23c242 | 3762 | BYTE *rbuff = (BYTE*)buff; |
glebiuskv | 0:2f0e1e23c242 | 3763 | |
glebiuskv | 0:2f0e1e23c242 | 3764 | |
glebiuskv | 0:2f0e1e23c242 | 3765 | *br = 0; /* Clear read byte counter */ |
glebiuskv | 0:2f0e1e23c242 | 3766 | res = validate(&fp->obj, &fs); /* Check validity of the file object */ |
glebiuskv | 0:2f0e1e23c242 | 3767 | if (res != FR_OK || (res = (FRESULT)fp->err) != FR_OK) LEAVE_FF(fs, res); /* Check validity */ |
glebiuskv | 0:2f0e1e23c242 | 3768 | if (!(fp->flag & FA_READ)) LEAVE_FF(fs, FR_DENIED); /* Check access mode */ |
glebiuskv | 0:2f0e1e23c242 | 3769 | remain = fp->obj.objsize - fp->fptr; |
glebiuskv | 0:2f0e1e23c242 | 3770 | if (btr > remain) btr = (UINT)remain; /* Truncate btr by remaining bytes */ |
glebiuskv | 0:2f0e1e23c242 | 3771 | |
glebiuskv | 0:2f0e1e23c242 | 3772 | for ( ; btr; /* Repeat until all data read */ |
glebiuskv | 0:2f0e1e23c242 | 3773 | btr -= rcnt, *br += rcnt, rbuff += rcnt, fp->fptr += rcnt) { |
glebiuskv | 0:2f0e1e23c242 | 3774 | if (fp->fptr % SS(fs) == 0) { /* On the sector boundary? */ |
glebiuskv | 0:2f0e1e23c242 | 3775 | csect = (UINT)(fp->fptr / SS(fs) & (fs->csize - 1)); /* Sector offset in the cluster */ |
glebiuskv | 0:2f0e1e23c242 | 3776 | if (csect == 0) { /* On the cluster boundary? */ |
glebiuskv | 0:2f0e1e23c242 | 3777 | if (fp->fptr == 0) { /* On the top of the file? */ |
glebiuskv | 0:2f0e1e23c242 | 3778 | clst = fp->obj.sclust; /* Follow cluster chain from the origin */ |
glebiuskv | 0:2f0e1e23c242 | 3779 | } else { /* Middle or end of the file */ |
glebiuskv | 0:2f0e1e23c242 | 3780 | #if FF_USE_FASTSEEK |
glebiuskv | 0:2f0e1e23c242 | 3781 | if (fp->cltbl) { |
glebiuskv | 0:2f0e1e23c242 | 3782 | clst = clmt_clust(fp, fp->fptr); /* Get cluster# from the CLMT */ |
glebiuskv | 0:2f0e1e23c242 | 3783 | } else |
glebiuskv | 0:2f0e1e23c242 | 3784 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3785 | { |
glebiuskv | 0:2f0e1e23c242 | 3786 | clst = get_fat(&fp->obj, fp->clust); /* Follow cluster chain on the FAT */ |
glebiuskv | 0:2f0e1e23c242 | 3787 | } |
glebiuskv | 0:2f0e1e23c242 | 3788 | } |
glebiuskv | 0:2f0e1e23c242 | 3789 | if (clst < 2) ABORT(fs, FR_INT_ERR); |
glebiuskv | 0:2f0e1e23c242 | 3790 | if (clst == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR); |
glebiuskv | 0:2f0e1e23c242 | 3791 | fp->clust = clst; /* Update current cluster */ |
glebiuskv | 0:2f0e1e23c242 | 3792 | } |
glebiuskv | 0:2f0e1e23c242 | 3793 | sect = clst2sect(fs, fp->clust); /* Get current sector */ |
glebiuskv | 0:2f0e1e23c242 | 3794 | if (sect == 0) ABORT(fs, FR_INT_ERR); |
glebiuskv | 0:2f0e1e23c242 | 3795 | sect += csect; |
glebiuskv | 0:2f0e1e23c242 | 3796 | cc = btr / SS(fs); /* When remaining bytes >= sector size, */ |
glebiuskv | 0:2f0e1e23c242 | 3797 | if (cc > 0) { /* Read maximum contiguous sectors directly */ |
glebiuskv | 0:2f0e1e23c242 | 3798 | if (csect + cc > fs->csize) { /* Clip at cluster boundary */ |
glebiuskv | 0:2f0e1e23c242 | 3799 | cc = fs->csize - csect; |
glebiuskv | 0:2f0e1e23c242 | 3800 | } |
glebiuskv | 0:2f0e1e23c242 | 3801 | if (disk_read(fs->pdrv, rbuff, sect, cc) != RES_OK) ABORT(fs, FR_DISK_ERR); |
glebiuskv | 0:2f0e1e23c242 | 3802 | #if !FF_FS_READONLY && FF_FS_MINIMIZE <= 2 /* Replace one of the read sectors with cached data if it contains a dirty sector */ |
glebiuskv | 0:2f0e1e23c242 | 3803 | #if FF_FS_TINY |
glebiuskv | 0:2f0e1e23c242 | 3804 | if (fs->wflag && fs->winsect - sect < cc) { |
glebiuskv | 0:2f0e1e23c242 | 3805 | mem_cpy(rbuff + ((fs->winsect - sect) * SS(fs)), fs->win, SS(fs)); |
glebiuskv | 0:2f0e1e23c242 | 3806 | } |
glebiuskv | 0:2f0e1e23c242 | 3807 | #else |
glebiuskv | 0:2f0e1e23c242 | 3808 | if ((fp->flag & FA_DIRTY) && fp->sect - sect < cc) { |
glebiuskv | 0:2f0e1e23c242 | 3809 | mem_cpy(rbuff + ((fp->sect - sect) * SS(fs)), fp->buf, SS(fs)); |
glebiuskv | 0:2f0e1e23c242 | 3810 | } |
glebiuskv | 0:2f0e1e23c242 | 3811 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3812 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3813 | rcnt = SS(fs) * cc; /* Number of bytes transferred */ |
glebiuskv | 0:2f0e1e23c242 | 3814 | continue; |
glebiuskv | 0:2f0e1e23c242 | 3815 | } |
glebiuskv | 0:2f0e1e23c242 | 3816 | #if !FF_FS_TINY |
glebiuskv | 0:2f0e1e23c242 | 3817 | if (fp->sect != sect) { /* Load data sector if not in cache */ |
glebiuskv | 0:2f0e1e23c242 | 3818 | #if !FF_FS_READONLY |
glebiuskv | 0:2f0e1e23c242 | 3819 | if (fp->flag & FA_DIRTY) { /* Write-back dirty sector cache */ |
glebiuskv | 0:2f0e1e23c242 | 3820 | if (disk_write(fs->pdrv, fp->buf, fp->sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR); |
glebiuskv | 0:2f0e1e23c242 | 3821 | fp->flag &= (BYTE)~FA_DIRTY; |
glebiuskv | 0:2f0e1e23c242 | 3822 | } |
glebiuskv | 0:2f0e1e23c242 | 3823 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3824 | if (disk_read(fs->pdrv, fp->buf, sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR); /* Fill sector cache */ |
glebiuskv | 0:2f0e1e23c242 | 3825 | } |
glebiuskv | 0:2f0e1e23c242 | 3826 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3827 | fp->sect = sect; |
glebiuskv | 0:2f0e1e23c242 | 3828 | } |
glebiuskv | 0:2f0e1e23c242 | 3829 | rcnt = SS(fs) - (UINT)fp->fptr % SS(fs); /* Number of bytes left in the sector */ |
glebiuskv | 0:2f0e1e23c242 | 3830 | if (rcnt > btr) rcnt = btr; /* Clip it by btr if needed */ |
glebiuskv | 0:2f0e1e23c242 | 3831 | #if FF_FS_TINY |
glebiuskv | 0:2f0e1e23c242 | 3832 | if (move_window(fs, fp->sect) != FR_OK) ABORT(fs, FR_DISK_ERR); /* Move sector window */ |
glebiuskv | 0:2f0e1e23c242 | 3833 | mem_cpy(rbuff, fs->win + fp->fptr % SS(fs), rcnt); /* Extract partial sector */ |
glebiuskv | 0:2f0e1e23c242 | 3834 | #else |
glebiuskv | 0:2f0e1e23c242 | 3835 | mem_cpy(rbuff, fp->buf + fp->fptr % SS(fs), rcnt); /* Extract partial sector */ |
glebiuskv | 0:2f0e1e23c242 | 3836 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3837 | } |
glebiuskv | 0:2f0e1e23c242 | 3838 | |
glebiuskv | 0:2f0e1e23c242 | 3839 | LEAVE_FF(fs, FR_OK); |
glebiuskv | 0:2f0e1e23c242 | 3840 | } |
glebiuskv | 0:2f0e1e23c242 | 3841 | |
glebiuskv | 0:2f0e1e23c242 | 3842 | |
glebiuskv | 0:2f0e1e23c242 | 3843 | |
glebiuskv | 0:2f0e1e23c242 | 3844 | |
glebiuskv | 0:2f0e1e23c242 | 3845 | #if !FF_FS_READONLY |
glebiuskv | 0:2f0e1e23c242 | 3846 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 3847 | /* Write File */ |
glebiuskv | 0:2f0e1e23c242 | 3848 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 3849 | |
glebiuskv | 0:2f0e1e23c242 | 3850 | FRESULT f_write ( |
glebiuskv | 0:2f0e1e23c242 | 3851 | FIL* fp, /* Pointer to the file object */ |
glebiuskv | 0:2f0e1e23c242 | 3852 | const void* buff, /* Pointer to the data to be written */ |
glebiuskv | 0:2f0e1e23c242 | 3853 | UINT btw, /* Number of bytes to write */ |
glebiuskv | 0:2f0e1e23c242 | 3854 | UINT* bw /* Pointer to number of bytes written */ |
glebiuskv | 0:2f0e1e23c242 | 3855 | ) |
glebiuskv | 0:2f0e1e23c242 | 3856 | { |
glebiuskv | 0:2f0e1e23c242 | 3857 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 3858 | FATFS *fs; |
glebiuskv | 0:2f0e1e23c242 | 3859 | DWORD clst, sect; |
glebiuskv | 0:2f0e1e23c242 | 3860 | UINT wcnt, cc, csect; |
glebiuskv | 0:2f0e1e23c242 | 3861 | const BYTE *wbuff = (const BYTE*)buff; |
glebiuskv | 0:2f0e1e23c242 | 3862 | bool need_sync = false; |
glebiuskv | 0:2f0e1e23c242 | 3863 | |
glebiuskv | 0:2f0e1e23c242 | 3864 | *bw = 0; /* Clear write byte counter */ |
glebiuskv | 0:2f0e1e23c242 | 3865 | res = validate(&fp->obj, &fs); /* Check validity of the file object */ |
glebiuskv | 0:2f0e1e23c242 | 3866 | if (res != FR_OK || (res = (FRESULT)fp->err) != FR_OK) LEAVE_FF(fs, res); /* Check validity */ |
glebiuskv | 0:2f0e1e23c242 | 3867 | if (!(fp->flag & FA_WRITE)) LEAVE_FF(fs, FR_DENIED); /* Check access mode */ |
glebiuskv | 0:2f0e1e23c242 | 3868 | |
glebiuskv | 0:2f0e1e23c242 | 3869 | /* Check fptr wrap-around (file size cannot reach 4 GiB at FAT volume) */ |
glebiuskv | 0:2f0e1e23c242 | 3870 | if ((!FF_FS_EXFAT || fs->fs_type != FS_EXFAT) && (DWORD)(fp->fptr + btw) < (DWORD)fp->fptr) { |
glebiuskv | 0:2f0e1e23c242 | 3871 | btw = (UINT)(0xFFFFFFFF - (DWORD)fp->fptr); |
glebiuskv | 0:2f0e1e23c242 | 3872 | } |
glebiuskv | 0:2f0e1e23c242 | 3873 | |
glebiuskv | 0:2f0e1e23c242 | 3874 | for ( ; btw; /* Repeat until all data written */ |
glebiuskv | 0:2f0e1e23c242 | 3875 | btw -= wcnt, *bw += wcnt, wbuff += wcnt, fp->fptr += wcnt, fp->obj.objsize = (fp->fptr > fp->obj.objsize) ? fp->fptr : fp->obj.objsize) { |
glebiuskv | 0:2f0e1e23c242 | 3876 | if (fp->fptr % SS(fs) == 0) { /* On the sector boundary? */ |
glebiuskv | 0:2f0e1e23c242 | 3877 | csect = (UINT)(fp->fptr / SS(fs)) & (fs->csize - 1); /* Sector offset in the cluster */ |
glebiuskv | 0:2f0e1e23c242 | 3878 | if (csect == 0) { /* On the cluster boundary? */ |
glebiuskv | 0:2f0e1e23c242 | 3879 | if (fp->fptr == 0) { /* On the top of the file? */ |
glebiuskv | 0:2f0e1e23c242 | 3880 | clst = fp->obj.sclust; /* Follow from the origin */ |
glebiuskv | 0:2f0e1e23c242 | 3881 | if (clst == 0) { /* If no cluster is allocated, */ |
glebiuskv | 0:2f0e1e23c242 | 3882 | clst = create_chain(&fp->obj, 0); /* create a new cluster chain */ |
glebiuskv | 0:2f0e1e23c242 | 3883 | } |
glebiuskv | 0:2f0e1e23c242 | 3884 | } else { /* On the middle or end of the file */ |
glebiuskv | 0:2f0e1e23c242 | 3885 | #if FF_USE_FASTSEEK |
glebiuskv | 0:2f0e1e23c242 | 3886 | if (fp->cltbl) { |
glebiuskv | 0:2f0e1e23c242 | 3887 | clst = clmt_clust(fp, fp->fptr); /* Get cluster# from the CLMT */ |
glebiuskv | 0:2f0e1e23c242 | 3888 | } else |
glebiuskv | 0:2f0e1e23c242 | 3889 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3890 | { |
glebiuskv | 0:2f0e1e23c242 | 3891 | clst = create_chain(&fp->obj, fp->clust); /* Follow or stretch cluster chain on the FAT */ |
glebiuskv | 0:2f0e1e23c242 | 3892 | } |
glebiuskv | 0:2f0e1e23c242 | 3893 | } |
glebiuskv | 0:2f0e1e23c242 | 3894 | if (clst == 0) break; /* Could not allocate a new cluster (disk full) */ |
glebiuskv | 0:2f0e1e23c242 | 3895 | if (clst == 1) ABORT(fs, FR_INT_ERR); |
glebiuskv | 0:2f0e1e23c242 | 3896 | if (clst == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR); |
glebiuskv | 0:2f0e1e23c242 | 3897 | fp->clust = clst; /* Update current cluster */ |
glebiuskv | 0:2f0e1e23c242 | 3898 | if (fp->obj.sclust == 0) fp->obj.sclust = clst; /* Set start cluster if the first write */ |
glebiuskv | 0:2f0e1e23c242 | 3899 | #if FLUSH_ON_NEW_CLUSTER |
glebiuskv | 0:2f0e1e23c242 | 3900 | // We do not need to flush for the first cluster |
glebiuskv | 0:2f0e1e23c242 | 3901 | if (fp->fptr != 0) { |
glebiuskv | 0:2f0e1e23c242 | 3902 | need_sync = true; |
glebiuskv | 0:2f0e1e23c242 | 3903 | } |
glebiuskv | 0:2f0e1e23c242 | 3904 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3905 | } |
glebiuskv | 0:2f0e1e23c242 | 3906 | #if FF_FS_TINY |
glebiuskv | 0:2f0e1e23c242 | 3907 | if (fs->winsect == fp->sect && sync_window(fs) != FR_OK) ABORT(fs, FR_DISK_ERR); /* Write-back sector cache */ |
glebiuskv | 0:2f0e1e23c242 | 3908 | #else |
glebiuskv | 0:2f0e1e23c242 | 3909 | if (fp->flag & FA_DIRTY) { /* Write-back sector cache */ |
glebiuskv | 0:2f0e1e23c242 | 3910 | if (disk_write(fs->pdrv, fp->buf, fp->sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR); |
glebiuskv | 0:2f0e1e23c242 | 3911 | fp->flag &= (BYTE)~FA_DIRTY; |
glebiuskv | 0:2f0e1e23c242 | 3912 | } |
glebiuskv | 0:2f0e1e23c242 | 3913 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3914 | sect = clst2sect(fs, fp->clust); /* Get current sector */ |
glebiuskv | 0:2f0e1e23c242 | 3915 | if (sect == 0) ABORT(fs, FR_INT_ERR); |
glebiuskv | 0:2f0e1e23c242 | 3916 | sect += csect; |
glebiuskv | 0:2f0e1e23c242 | 3917 | cc = btw / SS(fs); /* When remaining bytes >= sector size, */ |
glebiuskv | 0:2f0e1e23c242 | 3918 | if (cc > 0) { /* Write maximum contiguous sectors directly */ |
glebiuskv | 0:2f0e1e23c242 | 3919 | if (csect + cc > fs->csize) { /* Clip at cluster boundary */ |
glebiuskv | 0:2f0e1e23c242 | 3920 | cc = fs->csize - csect; |
glebiuskv | 0:2f0e1e23c242 | 3921 | } |
glebiuskv | 0:2f0e1e23c242 | 3922 | if (disk_write(fs->pdrv, wbuff, sect, cc) != RES_OK) ABORT(fs, FR_DISK_ERR); |
glebiuskv | 0:2f0e1e23c242 | 3923 | #if FF_FS_MINIMIZE <= 2 |
glebiuskv | 0:2f0e1e23c242 | 3924 | #if FF_FS_TINY |
glebiuskv | 0:2f0e1e23c242 | 3925 | if (fs->winsect - sect < cc) { /* Refill sector cache if it gets invalidated by the direct write */ |
glebiuskv | 0:2f0e1e23c242 | 3926 | mem_cpy(fs->win, wbuff + ((fs->winsect - sect) * SS(fs)), SS(fs)); |
glebiuskv | 0:2f0e1e23c242 | 3927 | fs->wflag = 0; |
glebiuskv | 0:2f0e1e23c242 | 3928 | } |
glebiuskv | 0:2f0e1e23c242 | 3929 | #else |
glebiuskv | 0:2f0e1e23c242 | 3930 | if (fp->sect - sect < cc) { /* Refill sector cache if it gets invalidated by the direct write */ |
glebiuskv | 0:2f0e1e23c242 | 3931 | mem_cpy(fp->buf, wbuff + ((fp->sect - sect) * SS(fs)), SS(fs)); |
glebiuskv | 0:2f0e1e23c242 | 3932 | fp->flag &= (BYTE)~FA_DIRTY; |
glebiuskv | 0:2f0e1e23c242 | 3933 | } |
glebiuskv | 0:2f0e1e23c242 | 3934 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3935 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3936 | wcnt = SS(fs) * cc; /* Number of bytes transferred */ |
glebiuskv | 0:2f0e1e23c242 | 3937 | #if FLUSH_ON_NEW_SECTOR |
glebiuskv | 0:2f0e1e23c242 | 3938 | need_sync = true; |
glebiuskv | 0:2f0e1e23c242 | 3939 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3940 | continue; |
glebiuskv | 0:2f0e1e23c242 | 3941 | } |
glebiuskv | 0:2f0e1e23c242 | 3942 | #if FF_FS_TINY |
glebiuskv | 0:2f0e1e23c242 | 3943 | if (fp->fptr >= fp->obj.objsize) { /* Avoid silly cache filling on the growing edge */ |
glebiuskv | 0:2f0e1e23c242 | 3944 | if (sync_window(fs) != FR_OK) ABORT(fs, FR_DISK_ERR); |
glebiuskv | 0:2f0e1e23c242 | 3945 | fs->winsect = sect; |
glebiuskv | 0:2f0e1e23c242 | 3946 | } |
glebiuskv | 0:2f0e1e23c242 | 3947 | #else |
glebiuskv | 0:2f0e1e23c242 | 3948 | if (fp->sect != sect && /* Fill sector cache with file data */ |
glebiuskv | 0:2f0e1e23c242 | 3949 | fp->fptr < fp->obj.objsize && |
glebiuskv | 0:2f0e1e23c242 | 3950 | disk_read(fs->pdrv, fp->buf, sect, 1) != RES_OK) { |
glebiuskv | 0:2f0e1e23c242 | 3951 | ABORT(fs, FR_DISK_ERR); |
glebiuskv | 0:2f0e1e23c242 | 3952 | } |
glebiuskv | 0:2f0e1e23c242 | 3953 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3954 | fp->sect = sect; |
glebiuskv | 0:2f0e1e23c242 | 3955 | } |
glebiuskv | 0:2f0e1e23c242 | 3956 | wcnt = SS(fs) - (UINT)fp->fptr % SS(fs); /* Number of bytes left in the sector */ |
glebiuskv | 0:2f0e1e23c242 | 3957 | if (wcnt > btw) wcnt = btw; /* Clip it by btw if needed */ |
glebiuskv | 0:2f0e1e23c242 | 3958 | #if FF_FS_TINY |
glebiuskv | 0:2f0e1e23c242 | 3959 | if (move_window(fs, fp->sect) != FR_OK) ABORT(fs, FR_DISK_ERR); /* Move sector window */ |
glebiuskv | 0:2f0e1e23c242 | 3960 | mem_cpy(fs->win + fp->fptr % SS(fs), wbuff, wcnt); /* Fit data to the sector */ |
glebiuskv | 0:2f0e1e23c242 | 3961 | fs->wflag = 1; |
glebiuskv | 0:2f0e1e23c242 | 3962 | #else |
glebiuskv | 0:2f0e1e23c242 | 3963 | mem_cpy(fp->buf + fp->fptr % SS(fs), wbuff, wcnt); /* Fit data to the sector */ |
glebiuskv | 0:2f0e1e23c242 | 3964 | fp->flag |= FA_DIRTY; |
glebiuskv | 0:2f0e1e23c242 | 3965 | #endif |
glebiuskv | 0:2f0e1e23c242 | 3966 | } |
glebiuskv | 0:2f0e1e23c242 | 3967 | |
glebiuskv | 0:2f0e1e23c242 | 3968 | fp->flag |= FA_MODIFIED; /* Set file change flag */ |
glebiuskv | 0:2f0e1e23c242 | 3969 | |
glebiuskv | 0:2f0e1e23c242 | 3970 | if (need_sync) { |
glebiuskv | 0:2f0e1e23c242 | 3971 | f_sync (fp); |
glebiuskv | 0:2f0e1e23c242 | 3972 | } |
glebiuskv | 0:2f0e1e23c242 | 3973 | |
glebiuskv | 0:2f0e1e23c242 | 3974 | LEAVE_FF(fs, FR_OK); |
glebiuskv | 0:2f0e1e23c242 | 3975 | } |
glebiuskv | 0:2f0e1e23c242 | 3976 | |
glebiuskv | 0:2f0e1e23c242 | 3977 | |
glebiuskv | 0:2f0e1e23c242 | 3978 | |
glebiuskv | 0:2f0e1e23c242 | 3979 | |
glebiuskv | 0:2f0e1e23c242 | 3980 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 3981 | /* Synchronize the File */ |
glebiuskv | 0:2f0e1e23c242 | 3982 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 3983 | |
glebiuskv | 0:2f0e1e23c242 | 3984 | FRESULT f_sync ( |
glebiuskv | 0:2f0e1e23c242 | 3985 | FIL* fp /* Pointer to the file object */ |
glebiuskv | 0:2f0e1e23c242 | 3986 | ) |
glebiuskv | 0:2f0e1e23c242 | 3987 | { |
glebiuskv | 0:2f0e1e23c242 | 3988 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 3989 | FATFS *fs; |
glebiuskv | 0:2f0e1e23c242 | 3990 | DWORD tm; |
glebiuskv | 0:2f0e1e23c242 | 3991 | BYTE *dir; |
glebiuskv | 0:2f0e1e23c242 | 3992 | |
glebiuskv | 0:2f0e1e23c242 | 3993 | |
glebiuskv | 0:2f0e1e23c242 | 3994 | res = validate(&fp->obj, &fs); /* Check validity of the file object */ |
glebiuskv | 0:2f0e1e23c242 | 3995 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 3996 | if (fp->flag & FA_MODIFIED) { /* Is there any change to the file? */ |
glebiuskv | 0:2f0e1e23c242 | 3997 | #if !FF_FS_TINY |
glebiuskv | 0:2f0e1e23c242 | 3998 | if (fp->flag & FA_DIRTY) { /* Write-back cached data if needed */ |
glebiuskv | 0:2f0e1e23c242 | 3999 | if (disk_write(fs->pdrv, fp->buf, fp->sect, 1) != RES_OK) LEAVE_FF(fs, FR_DISK_ERR); |
glebiuskv | 0:2f0e1e23c242 | 4000 | fp->flag &= (BYTE)~FA_DIRTY; |
glebiuskv | 0:2f0e1e23c242 | 4001 | } |
glebiuskv | 0:2f0e1e23c242 | 4002 | #endif |
glebiuskv | 0:2f0e1e23c242 | 4003 | /* Update the directory entry */ |
glebiuskv | 0:2f0e1e23c242 | 4004 | tm = GET_FATTIME(); /* Modified time */ |
glebiuskv | 0:2f0e1e23c242 | 4005 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 4006 | if (fs->fs_type == FS_EXFAT) { |
glebiuskv | 0:2f0e1e23c242 | 4007 | res = fill_first_frag(&fp->obj); /* Fill first fragment on the FAT if needed */ |
glebiuskv | 0:2f0e1e23c242 | 4008 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 4009 | res = fill_last_frag(&fp->obj, fp->clust, 0xFFFFFFFF); /* Fill last fragment on the FAT if needed */ |
glebiuskv | 0:2f0e1e23c242 | 4010 | } |
glebiuskv | 0:2f0e1e23c242 | 4011 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 4012 | FATFS_DIR dj; |
glebiuskv | 0:2f0e1e23c242 | 4013 | DEF_NAMBUF |
glebiuskv | 0:2f0e1e23c242 | 4014 | |
glebiuskv | 0:2f0e1e23c242 | 4015 | INIT_NAMBUF(fs); |
glebiuskv | 0:2f0e1e23c242 | 4016 | res = load_obj_xdir(&dj, &fp->obj); /* Load directory entry block */ |
glebiuskv | 0:2f0e1e23c242 | 4017 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 4018 | fs->dirbuf[XDIR_Attr] |= AM_ARC; /* Set archive attribute to indicate that the file has been changed */ |
glebiuskv | 0:2f0e1e23c242 | 4019 | fs->dirbuf[XDIR_GenFlags] = fp->obj.stat | 1; /* Update file allocation information */ |
glebiuskv | 0:2f0e1e23c242 | 4020 | st_dword(fs->dirbuf + XDIR_FstClus, fp->obj.sclust); |
glebiuskv | 0:2f0e1e23c242 | 4021 | st_qword(fs->dirbuf + XDIR_FileSize, fp->obj.objsize); |
glebiuskv | 0:2f0e1e23c242 | 4022 | st_qword(fs->dirbuf + XDIR_ValidFileSize, fp->obj.objsize); |
glebiuskv | 0:2f0e1e23c242 | 4023 | st_dword(fs->dirbuf + XDIR_ModTime, tm); /* Update modified time */ |
glebiuskv | 0:2f0e1e23c242 | 4024 | fs->dirbuf[XDIR_ModTime10] = 0; |
glebiuskv | 0:2f0e1e23c242 | 4025 | st_dword(fs->dirbuf + XDIR_AccTime, 0); |
glebiuskv | 0:2f0e1e23c242 | 4026 | res = store_xdir(&dj); /* Restore it to the directory */ |
glebiuskv | 0:2f0e1e23c242 | 4027 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 4028 | res = sync_fs(fs); |
glebiuskv | 0:2f0e1e23c242 | 4029 | fp->flag &= (BYTE)~FA_MODIFIED; |
glebiuskv | 0:2f0e1e23c242 | 4030 | } |
glebiuskv | 0:2f0e1e23c242 | 4031 | } |
glebiuskv | 0:2f0e1e23c242 | 4032 | FREE_NAMBUF(); |
glebiuskv | 0:2f0e1e23c242 | 4033 | } |
glebiuskv | 0:2f0e1e23c242 | 4034 | } else |
glebiuskv | 0:2f0e1e23c242 | 4035 | #endif |
glebiuskv | 0:2f0e1e23c242 | 4036 | { |
glebiuskv | 0:2f0e1e23c242 | 4037 | res = move_window(fs, fp->dir_sect); |
glebiuskv | 0:2f0e1e23c242 | 4038 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 4039 | dir = fp->dir_ptr; |
glebiuskv | 0:2f0e1e23c242 | 4040 | dir[DIR_Attr] |= AM_ARC; /* Set archive attribute to indicate that the file has been changed */ |
glebiuskv | 0:2f0e1e23c242 | 4041 | st_clust(fp->obj.fs, dir, fp->obj.sclust); /* Update file allocation information */ |
glebiuskv | 0:2f0e1e23c242 | 4042 | st_dword(dir + DIR_FileSize, (DWORD)fp->obj.objsize); /* Update file size */ |
glebiuskv | 0:2f0e1e23c242 | 4043 | st_dword(dir + DIR_ModTime, tm); /* Update modified time */ |
glebiuskv | 0:2f0e1e23c242 | 4044 | st_word(dir + DIR_LstAccDate, 0); |
glebiuskv | 0:2f0e1e23c242 | 4045 | fs->wflag = 1; |
glebiuskv | 0:2f0e1e23c242 | 4046 | res = sync_fs(fs); /* Restore it to the directory */ |
glebiuskv | 0:2f0e1e23c242 | 4047 | fp->flag &= (BYTE)~FA_MODIFIED; |
glebiuskv | 0:2f0e1e23c242 | 4048 | } |
glebiuskv | 0:2f0e1e23c242 | 4049 | } |
glebiuskv | 0:2f0e1e23c242 | 4050 | } |
glebiuskv | 0:2f0e1e23c242 | 4051 | } |
glebiuskv | 0:2f0e1e23c242 | 4052 | |
glebiuskv | 0:2f0e1e23c242 | 4053 | LEAVE_FF(fs, res); |
glebiuskv | 0:2f0e1e23c242 | 4054 | } |
glebiuskv | 0:2f0e1e23c242 | 4055 | |
glebiuskv | 0:2f0e1e23c242 | 4056 | #endif /* !FF_FS_READONLY */ |
glebiuskv | 0:2f0e1e23c242 | 4057 | |
glebiuskv | 0:2f0e1e23c242 | 4058 | |
glebiuskv | 0:2f0e1e23c242 | 4059 | |
glebiuskv | 0:2f0e1e23c242 | 4060 | |
glebiuskv | 0:2f0e1e23c242 | 4061 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 4062 | /* Close File */ |
glebiuskv | 0:2f0e1e23c242 | 4063 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 4064 | |
glebiuskv | 0:2f0e1e23c242 | 4065 | FRESULT f_close ( |
glebiuskv | 0:2f0e1e23c242 | 4066 | FIL* fp /* Pointer to the file object to be closed */ |
glebiuskv | 0:2f0e1e23c242 | 4067 | ) |
glebiuskv | 0:2f0e1e23c242 | 4068 | { |
glebiuskv | 0:2f0e1e23c242 | 4069 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 4070 | FATFS *fs; |
glebiuskv | 0:2f0e1e23c242 | 4071 | |
glebiuskv | 0:2f0e1e23c242 | 4072 | #if !FF_FS_READONLY |
glebiuskv | 0:2f0e1e23c242 | 4073 | res = f_sync(fp); /* Flush cached data */ |
glebiuskv | 0:2f0e1e23c242 | 4074 | if (res == FR_OK) |
glebiuskv | 0:2f0e1e23c242 | 4075 | #endif |
glebiuskv | 0:2f0e1e23c242 | 4076 | { |
glebiuskv | 0:2f0e1e23c242 | 4077 | res = validate(&fp->obj, &fs); /* Lock volume */ |
glebiuskv | 0:2f0e1e23c242 | 4078 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 4079 | #if FF_FS_LOCK != 0 |
glebiuskv | 0:2f0e1e23c242 | 4080 | res = dec_lock(fp->obj.lockid); /* Decrement file open counter */ |
glebiuskv | 0:2f0e1e23c242 | 4081 | if (res == FR_OK) fp->obj.fs = 0; /* Invalidate file object */ |
glebiuskv | 0:2f0e1e23c242 | 4082 | #else |
glebiuskv | 0:2f0e1e23c242 | 4083 | fp->obj.fs = 0; /* Invalidate file object */ |
glebiuskv | 0:2f0e1e23c242 | 4084 | #endif |
glebiuskv | 0:2f0e1e23c242 | 4085 | #if FF_FS_REENTRANT |
glebiuskv | 0:2f0e1e23c242 | 4086 | unlock_fs(fs, FR_OK); /* Unlock volume */ |
glebiuskv | 0:2f0e1e23c242 | 4087 | #endif |
glebiuskv | 0:2f0e1e23c242 | 4088 | #if !FF_FS_TINY && FF_FS_HEAPBUF |
glebiuskv | 0:2f0e1e23c242 | 4089 | ff_memfree(fp->buf); /* Deallocate buffer */ |
glebiuskv | 0:2f0e1e23c242 | 4090 | #endif |
glebiuskv | 0:2f0e1e23c242 | 4091 | } |
glebiuskv | 0:2f0e1e23c242 | 4092 | } |
glebiuskv | 0:2f0e1e23c242 | 4093 | return res; |
glebiuskv | 0:2f0e1e23c242 | 4094 | } |
glebiuskv | 0:2f0e1e23c242 | 4095 | |
glebiuskv | 0:2f0e1e23c242 | 4096 | |
glebiuskv | 0:2f0e1e23c242 | 4097 | |
glebiuskv | 0:2f0e1e23c242 | 4098 | |
glebiuskv | 0:2f0e1e23c242 | 4099 | #if FF_FS_RPATH >= 1 |
glebiuskv | 0:2f0e1e23c242 | 4100 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 4101 | /* Change Current Directory or Current Drive, Get Current Directory */ |
glebiuskv | 0:2f0e1e23c242 | 4102 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 4103 | |
glebiuskv | 0:2f0e1e23c242 | 4104 | #if FF_VOLUMES >= 2 |
glebiuskv | 0:2f0e1e23c242 | 4105 | FRESULT f_chdrive ( |
glebiuskv | 0:2f0e1e23c242 | 4106 | const TCHAR* path /* Drive number */ |
glebiuskv | 0:2f0e1e23c242 | 4107 | ) |
glebiuskv | 0:2f0e1e23c242 | 4108 | { |
glebiuskv | 0:2f0e1e23c242 | 4109 | int vol; |
glebiuskv | 0:2f0e1e23c242 | 4110 | |
glebiuskv | 0:2f0e1e23c242 | 4111 | |
glebiuskv | 0:2f0e1e23c242 | 4112 | /* Get logical drive number */ |
glebiuskv | 0:2f0e1e23c242 | 4113 | vol = get_ldnumber(&path); |
glebiuskv | 0:2f0e1e23c242 | 4114 | if (vol < 0) return FR_INVALID_DRIVE; |
glebiuskv | 0:2f0e1e23c242 | 4115 | |
glebiuskv | 0:2f0e1e23c242 | 4116 | CurrVol = (BYTE)vol; /* Set it as current volume */ |
glebiuskv | 0:2f0e1e23c242 | 4117 | |
glebiuskv | 0:2f0e1e23c242 | 4118 | return FR_OK; |
glebiuskv | 0:2f0e1e23c242 | 4119 | } |
glebiuskv | 0:2f0e1e23c242 | 4120 | #endif |
glebiuskv | 0:2f0e1e23c242 | 4121 | |
glebiuskv | 0:2f0e1e23c242 | 4122 | |
glebiuskv | 0:2f0e1e23c242 | 4123 | FRESULT f_chdir ( |
glebiuskv | 0:2f0e1e23c242 | 4124 | const TCHAR* path /* Pointer to the directory path */ |
glebiuskv | 0:2f0e1e23c242 | 4125 | ) |
glebiuskv | 0:2f0e1e23c242 | 4126 | { |
glebiuskv | 0:2f0e1e23c242 | 4127 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 4128 | FATFS_DIR dj; |
glebiuskv | 0:2f0e1e23c242 | 4129 | FATFS *fs; |
glebiuskv | 0:2f0e1e23c242 | 4130 | DEF_NAMBUF |
glebiuskv | 0:2f0e1e23c242 | 4131 | |
glebiuskv | 0:2f0e1e23c242 | 4132 | /* Get logical drive */ |
glebiuskv | 0:2f0e1e23c242 | 4133 | res = find_volume(&path, &fs, 0); |
glebiuskv | 0:2f0e1e23c242 | 4134 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 4135 | dj.obj.fs = fs; |
glebiuskv | 0:2f0e1e23c242 | 4136 | INIT_NAMBUF(fs); |
glebiuskv | 0:2f0e1e23c242 | 4137 | res = follow_path(&dj, path); /* Follow the path */ |
glebiuskv | 0:2f0e1e23c242 | 4138 | if (res == FR_OK) { /* Follow completed */ |
glebiuskv | 0:2f0e1e23c242 | 4139 | if (dj.fn[NSFLAG] & NS_NONAME) { |
glebiuskv | 0:2f0e1e23c242 | 4140 | fs->cdir = dj.obj.sclust; /* It is the start directory itself */ |
glebiuskv | 0:2f0e1e23c242 | 4141 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 4142 | if (fs->fs_type == FS_EXFAT) { |
glebiuskv | 0:2f0e1e23c242 | 4143 | fs->cdc_scl = dj.obj.c_scl; |
glebiuskv | 0:2f0e1e23c242 | 4144 | fs->cdc_size = dj.obj.c_size; |
glebiuskv | 0:2f0e1e23c242 | 4145 | fs->cdc_ofs = dj.obj.c_ofs; |
glebiuskv | 0:2f0e1e23c242 | 4146 | } |
glebiuskv | 0:2f0e1e23c242 | 4147 | #endif |
glebiuskv | 0:2f0e1e23c242 | 4148 | } else { |
glebiuskv | 0:2f0e1e23c242 | 4149 | if (dj.obj.attr & AM_DIR) { /* It is a sub-directory */ |
glebiuskv | 0:2f0e1e23c242 | 4150 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 4151 | if (fs->fs_type == FS_EXFAT) { |
glebiuskv | 0:2f0e1e23c242 | 4152 | fs->cdir = ld_dword(fs->dirbuf + XDIR_FstClus); /* Sub-directory cluster */ |
glebiuskv | 0:2f0e1e23c242 | 4153 | fs->cdc_scl = dj.obj.sclust; /* Save containing directory information */ |
glebiuskv | 0:2f0e1e23c242 | 4154 | fs->cdc_size = ((DWORD)dj.obj.objsize & 0xFFFFFF00) | dj.obj.stat; |
glebiuskv | 0:2f0e1e23c242 | 4155 | fs->cdc_ofs = dj.blk_ofs; |
glebiuskv | 0:2f0e1e23c242 | 4156 | } else |
glebiuskv | 0:2f0e1e23c242 | 4157 | #endif |
glebiuskv | 0:2f0e1e23c242 | 4158 | { |
glebiuskv | 0:2f0e1e23c242 | 4159 | fs->cdir = ld_clust(fs, dj.dir); /* Sub-directory cluster */ |
glebiuskv | 0:2f0e1e23c242 | 4160 | } |
glebiuskv | 0:2f0e1e23c242 | 4161 | } else { |
glebiuskv | 0:2f0e1e23c242 | 4162 | res = FR_NO_PATH; /* Reached but a file */ |
glebiuskv | 0:2f0e1e23c242 | 4163 | } |
glebiuskv | 0:2f0e1e23c242 | 4164 | } |
glebiuskv | 0:2f0e1e23c242 | 4165 | } |
glebiuskv | 0:2f0e1e23c242 | 4166 | FREE_NAMBUF(); |
glebiuskv | 0:2f0e1e23c242 | 4167 | if (res == FR_NO_FILE) res = FR_NO_PATH; |
glebiuskv | 0:2f0e1e23c242 | 4168 | } |
glebiuskv | 0:2f0e1e23c242 | 4169 | |
glebiuskv | 0:2f0e1e23c242 | 4170 | LEAVE_FF(fs, res); |
glebiuskv | 0:2f0e1e23c242 | 4171 | } |
glebiuskv | 0:2f0e1e23c242 | 4172 | |
glebiuskv | 0:2f0e1e23c242 | 4173 | |
glebiuskv | 0:2f0e1e23c242 | 4174 | #if FF_FS_RPATH >= 2 |
glebiuskv | 0:2f0e1e23c242 | 4175 | FRESULT f_getcwd ( |
glebiuskv | 0:2f0e1e23c242 | 4176 | TCHAR* buff, /* Pointer to the directory path */ |
glebiuskv | 0:2f0e1e23c242 | 4177 | UINT len /* Size of path */ |
glebiuskv | 0:2f0e1e23c242 | 4178 | ) |
glebiuskv | 0:2f0e1e23c242 | 4179 | { |
glebiuskv | 0:2f0e1e23c242 | 4180 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 4181 | FATFS_DIR dj; |
glebiuskv | 0:2f0e1e23c242 | 4182 | FATFS *fs; |
glebiuskv | 0:2f0e1e23c242 | 4183 | UINT i, n; |
glebiuskv | 0:2f0e1e23c242 | 4184 | DWORD ccl; |
glebiuskv | 0:2f0e1e23c242 | 4185 | TCHAR *tp; |
glebiuskv | 0:2f0e1e23c242 | 4186 | FILINFO fno; |
glebiuskv | 0:2f0e1e23c242 | 4187 | DEF_NAMBUF |
glebiuskv | 0:2f0e1e23c242 | 4188 | |
glebiuskv | 0:2f0e1e23c242 | 4189 | |
glebiuskv | 0:2f0e1e23c242 | 4190 | *buff = 0; |
glebiuskv | 0:2f0e1e23c242 | 4191 | /* Get logical drive */ |
glebiuskv | 0:2f0e1e23c242 | 4192 | res = find_volume((const TCHAR**)&buff, &fs, 0); /* Get current volume */ |
glebiuskv | 0:2f0e1e23c242 | 4193 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 4194 | dj.obj.fs = fs; |
glebiuskv | 0:2f0e1e23c242 | 4195 | INIT_NAMBUF(fs); |
glebiuskv | 0:2f0e1e23c242 | 4196 | i = len; /* Bottom of buffer (directory stack base) */ |
glebiuskv | 0:2f0e1e23c242 | 4197 | if (!FF_FS_EXFAT || fs->fs_type != FS_EXFAT) { /* (Cannot do getcwd on exFAT and returns root path) */ |
glebiuskv | 0:2f0e1e23c242 | 4198 | dj.obj.sclust = fs->cdir; /* Start to follow upper directory from current directory */ |
glebiuskv | 0:2f0e1e23c242 | 4199 | while ((ccl = dj.obj.sclust) != 0) { /* Repeat while current directory is a sub-directory */ |
glebiuskv | 0:2f0e1e23c242 | 4200 | res = dir_sdi(&dj, 1 * SZDIRE); /* Get parent directory */ |
glebiuskv | 0:2f0e1e23c242 | 4201 | if (res != FR_OK) break; |
glebiuskv | 0:2f0e1e23c242 | 4202 | res = move_window(fs, dj.sect); |
glebiuskv | 0:2f0e1e23c242 | 4203 | if (res != FR_OK) break; |
glebiuskv | 0:2f0e1e23c242 | 4204 | dj.obj.sclust = ld_clust(fs, dj.dir); /* Goto parent directory */ |
glebiuskv | 0:2f0e1e23c242 | 4205 | res = dir_sdi(&dj, 0); |
glebiuskv | 0:2f0e1e23c242 | 4206 | if (res != FR_OK) break; |
glebiuskv | 0:2f0e1e23c242 | 4207 | do { /* Find the entry links to the child directory */ |
glebiuskv | 0:2f0e1e23c242 | 4208 | res = dir_read_file(&dj); |
glebiuskv | 0:2f0e1e23c242 | 4209 | if (res != FR_OK) break; |
glebiuskv | 0:2f0e1e23c242 | 4210 | if (ccl == ld_clust(fs, dj.dir)) break; /* Found the entry */ |
glebiuskv | 0:2f0e1e23c242 | 4211 | res = dir_next(&dj, 0); |
glebiuskv | 0:2f0e1e23c242 | 4212 | } while (res == FR_OK); |
glebiuskv | 0:2f0e1e23c242 | 4213 | if (res == FR_NO_FILE) res = FR_INT_ERR;/* It cannot be 'not found'. */ |
glebiuskv | 0:2f0e1e23c242 | 4214 | if (res != FR_OK) break; |
glebiuskv | 0:2f0e1e23c242 | 4215 | get_fileinfo(&dj, &fno); /* Get the directory name and push it to the buffer */ |
glebiuskv | 0:2f0e1e23c242 | 4216 | for (n = 0; fno.fname[n]; n++) ; |
glebiuskv | 0:2f0e1e23c242 | 4217 | if (i < n + 3) { |
glebiuskv | 0:2f0e1e23c242 | 4218 | res = FR_NOT_ENOUGH_CORE; break; |
glebiuskv | 0:2f0e1e23c242 | 4219 | } |
glebiuskv | 0:2f0e1e23c242 | 4220 | while (n) buff[--i] = fno.fname[--n]; |
glebiuskv | 0:2f0e1e23c242 | 4221 | buff[--i] = '/'; |
glebiuskv | 0:2f0e1e23c242 | 4222 | } |
glebiuskv | 0:2f0e1e23c242 | 4223 | } |
glebiuskv | 0:2f0e1e23c242 | 4224 | tp = buff; |
glebiuskv | 0:2f0e1e23c242 | 4225 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 4226 | #if FF_VOLUMES >= 2 |
glebiuskv | 0:2f0e1e23c242 | 4227 | *tp++ = '0' + CurrVol; /* Put drive number */ |
glebiuskv | 0:2f0e1e23c242 | 4228 | *tp++ = ':'; |
glebiuskv | 0:2f0e1e23c242 | 4229 | #endif |
glebiuskv | 0:2f0e1e23c242 | 4230 | if (i == len) { /* Root-directory */ |
glebiuskv | 0:2f0e1e23c242 | 4231 | *tp++ = '/'; |
glebiuskv | 0:2f0e1e23c242 | 4232 | } else { /* Sub-directroy */ |
glebiuskv | 0:2f0e1e23c242 | 4233 | do /* Add stacked path str */ |
glebiuskv | 0:2f0e1e23c242 | 4234 | *tp++ = buff[i++]; |
glebiuskv | 0:2f0e1e23c242 | 4235 | while (i < len); |
glebiuskv | 0:2f0e1e23c242 | 4236 | } |
glebiuskv | 0:2f0e1e23c242 | 4237 | } |
glebiuskv | 0:2f0e1e23c242 | 4238 | *tp = 0; |
glebiuskv | 0:2f0e1e23c242 | 4239 | FREE_NAMBUF(); |
glebiuskv | 0:2f0e1e23c242 | 4240 | } |
glebiuskv | 0:2f0e1e23c242 | 4241 | |
glebiuskv | 0:2f0e1e23c242 | 4242 | LEAVE_FF(fs, res); |
glebiuskv | 0:2f0e1e23c242 | 4243 | } |
glebiuskv | 0:2f0e1e23c242 | 4244 | |
glebiuskv | 0:2f0e1e23c242 | 4245 | #endif /* FF_FS_RPATH >= 2 */ |
glebiuskv | 0:2f0e1e23c242 | 4246 | #endif /* FF_FS_RPATH >= 1 */ |
glebiuskv | 0:2f0e1e23c242 | 4247 | |
glebiuskv | 0:2f0e1e23c242 | 4248 | |
glebiuskv | 0:2f0e1e23c242 | 4249 | |
glebiuskv | 0:2f0e1e23c242 | 4250 | #if FF_FS_MINIMIZE <= 2 |
glebiuskv | 0:2f0e1e23c242 | 4251 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 4252 | /* Seek File Read/Write Pointer */ |
glebiuskv | 0:2f0e1e23c242 | 4253 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 4254 | |
glebiuskv | 0:2f0e1e23c242 | 4255 | FRESULT f_lseek ( |
glebiuskv | 0:2f0e1e23c242 | 4256 | FIL* fp, /* Pointer to the file object */ |
glebiuskv | 0:2f0e1e23c242 | 4257 | FSIZE_t ofs /* File pointer from top of file */ |
glebiuskv | 0:2f0e1e23c242 | 4258 | ) |
glebiuskv | 0:2f0e1e23c242 | 4259 | { |
glebiuskv | 0:2f0e1e23c242 | 4260 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 4261 | FATFS *fs; |
glebiuskv | 0:2f0e1e23c242 | 4262 | DWORD clst, bcs, nsect; |
glebiuskv | 0:2f0e1e23c242 | 4263 | FSIZE_t ifptr; |
glebiuskv | 0:2f0e1e23c242 | 4264 | #if FF_USE_FASTSEEK |
glebiuskv | 0:2f0e1e23c242 | 4265 | DWORD cl, pcl, ncl, tcl, dsc, tlen, ulen, *tbl; |
glebiuskv | 0:2f0e1e23c242 | 4266 | #endif |
glebiuskv | 0:2f0e1e23c242 | 4267 | |
glebiuskv | 0:2f0e1e23c242 | 4268 | res = validate(&fp->obj, &fs); /* Check validity of the file object */ |
glebiuskv | 0:2f0e1e23c242 | 4269 | if (res == FR_OK) res = (FRESULT)fp->err; |
glebiuskv | 0:2f0e1e23c242 | 4270 | #if FF_FS_EXFAT && !FF_FS_READONLY |
glebiuskv | 0:2f0e1e23c242 | 4271 | if (res == FR_OK && fs->fs_type == FS_EXFAT) { |
glebiuskv | 0:2f0e1e23c242 | 4272 | res = fill_last_frag(&fp->obj, fp->clust, 0xFFFFFFFF); /* Fill last fragment on the FAT if needed */ |
glebiuskv | 0:2f0e1e23c242 | 4273 | } |
glebiuskv | 0:2f0e1e23c242 | 4274 | #endif |
glebiuskv | 0:2f0e1e23c242 | 4275 | if (res != FR_OK) LEAVE_FF(fs, res); |
glebiuskv | 0:2f0e1e23c242 | 4276 | |
glebiuskv | 0:2f0e1e23c242 | 4277 | #if FF_USE_FASTSEEK |
glebiuskv | 0:2f0e1e23c242 | 4278 | if (fp->cltbl) { /* Fast seek */ |
glebiuskv | 0:2f0e1e23c242 | 4279 | if (ofs == CREATE_LINKMAP) { /* Create CLMT */ |
glebiuskv | 0:2f0e1e23c242 | 4280 | tbl = fp->cltbl; |
glebiuskv | 0:2f0e1e23c242 | 4281 | tlen = *tbl++; ulen = 2; /* Given table size and required table size */ |
glebiuskv | 0:2f0e1e23c242 | 4282 | cl = fp->obj.sclust; /* Origin of the chain */ |
glebiuskv | 0:2f0e1e23c242 | 4283 | if (cl != 0) { |
glebiuskv | 0:2f0e1e23c242 | 4284 | do { |
glebiuskv | 0:2f0e1e23c242 | 4285 | /* Get a fragment */ |
glebiuskv | 0:2f0e1e23c242 | 4286 | tcl = cl; ncl = 0; ulen += 2; /* Top, length and used items */ |
glebiuskv | 0:2f0e1e23c242 | 4287 | do { |
glebiuskv | 0:2f0e1e23c242 | 4288 | pcl = cl; ncl++; |
glebiuskv | 0:2f0e1e23c242 | 4289 | cl = get_fat(&fp->obj, cl); |
glebiuskv | 0:2f0e1e23c242 | 4290 | if (cl <= 1) ABORT(fs, FR_INT_ERR); |
glebiuskv | 0:2f0e1e23c242 | 4291 | if (cl == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR); |
glebiuskv | 0:2f0e1e23c242 | 4292 | } while (cl == pcl + 1); |
glebiuskv | 0:2f0e1e23c242 | 4293 | if (ulen <= tlen) { /* Store the length and top of the fragment */ |
glebiuskv | 0:2f0e1e23c242 | 4294 | *tbl++ = ncl; *tbl++ = tcl; |
glebiuskv | 0:2f0e1e23c242 | 4295 | } |
glebiuskv | 0:2f0e1e23c242 | 4296 | } while (cl < fs->n_fatent); /* Repeat until end of chain */ |
glebiuskv | 0:2f0e1e23c242 | 4297 | } |
glebiuskv | 0:2f0e1e23c242 | 4298 | *fp->cltbl = ulen; /* Number of items used */ |
glebiuskv | 0:2f0e1e23c242 | 4299 | if (ulen <= tlen) { |
glebiuskv | 0:2f0e1e23c242 | 4300 | *tbl = 0; /* Terminate table */ |
glebiuskv | 0:2f0e1e23c242 | 4301 | } else { |
glebiuskv | 0:2f0e1e23c242 | 4302 | res = FR_NOT_ENOUGH_CORE; /* Given table size is smaller than required */ |
glebiuskv | 0:2f0e1e23c242 | 4303 | } |
glebiuskv | 0:2f0e1e23c242 | 4304 | } else { /* Fast seek */ |
glebiuskv | 0:2f0e1e23c242 | 4305 | if (ofs > fp->obj.objsize) ofs = fp->obj.objsize; /* Clip offset at the file size */ |
glebiuskv | 0:2f0e1e23c242 | 4306 | fp->fptr = ofs; /* Set file pointer */ |
glebiuskv | 0:2f0e1e23c242 | 4307 | if (ofs > 0) { |
glebiuskv | 0:2f0e1e23c242 | 4308 | fp->clust = clmt_clust(fp, ofs - 1); |
glebiuskv | 0:2f0e1e23c242 | 4309 | dsc = clst2sect(fs, fp->clust); |
glebiuskv | 0:2f0e1e23c242 | 4310 | if (dsc == 0) ABORT(fs, FR_INT_ERR); |
glebiuskv | 0:2f0e1e23c242 | 4311 | dsc += (DWORD)((ofs - 1) / SS(fs)) & (fs->csize - 1); |
glebiuskv | 0:2f0e1e23c242 | 4312 | if (fp->fptr % SS(fs) && dsc != fp->sect) { /* Refill sector cache if needed */ |
glebiuskv | 0:2f0e1e23c242 | 4313 | #if !FF_FS_TINY |
glebiuskv | 0:2f0e1e23c242 | 4314 | #if !FF_FS_READONLY |
glebiuskv | 0:2f0e1e23c242 | 4315 | if (fp->flag & FA_DIRTY) { /* Write-back dirty sector cache */ |
glebiuskv | 0:2f0e1e23c242 | 4316 | if (disk_write(fs->pdrv, fp->buf, fp->sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR); |
glebiuskv | 0:2f0e1e23c242 | 4317 | fp->flag &= (BYTE)~FA_DIRTY; |
glebiuskv | 0:2f0e1e23c242 | 4318 | } |
glebiuskv | 0:2f0e1e23c242 | 4319 | #endif |
glebiuskv | 0:2f0e1e23c242 | 4320 | if (disk_read(fs->pdrv, fp->buf, dsc, 1) != RES_OK) ABORT(fs, FR_DISK_ERR); /* Load current sector */ |
glebiuskv | 0:2f0e1e23c242 | 4321 | #endif |
glebiuskv | 0:2f0e1e23c242 | 4322 | fp->sect = dsc; |
glebiuskv | 0:2f0e1e23c242 | 4323 | } |
glebiuskv | 0:2f0e1e23c242 | 4324 | } |
glebiuskv | 0:2f0e1e23c242 | 4325 | } |
glebiuskv | 0:2f0e1e23c242 | 4326 | } else |
glebiuskv | 0:2f0e1e23c242 | 4327 | #endif |
glebiuskv | 0:2f0e1e23c242 | 4328 | |
glebiuskv | 0:2f0e1e23c242 | 4329 | /* Normal Seek */ |
glebiuskv | 0:2f0e1e23c242 | 4330 | { |
glebiuskv | 0:2f0e1e23c242 | 4331 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 4332 | if (fs->fs_type != FS_EXFAT && ofs >= 0x100000000) ofs = 0xFFFFFFFF; /* Clip at 4 GiB - 1 if at FATxx */ |
glebiuskv | 0:2f0e1e23c242 | 4333 | #endif |
glebiuskv | 0:2f0e1e23c242 | 4334 | if (ofs > fp->obj.objsize && (FF_FS_READONLY || !(fp->flag & FA_WRITE))) { /* In read-only mode, clip offset with the file size */ |
glebiuskv | 0:2f0e1e23c242 | 4335 | ofs = fp->obj.objsize; |
glebiuskv | 0:2f0e1e23c242 | 4336 | } |
glebiuskv | 0:2f0e1e23c242 | 4337 | ifptr = fp->fptr; |
glebiuskv | 0:2f0e1e23c242 | 4338 | fp->fptr = nsect = 0; |
glebiuskv | 0:2f0e1e23c242 | 4339 | if (ofs > 0) { |
glebiuskv | 0:2f0e1e23c242 | 4340 | bcs = (DWORD)fs->csize * SS(fs); /* Cluster size (byte) */ |
glebiuskv | 0:2f0e1e23c242 | 4341 | if (ifptr > 0 && |
glebiuskv | 0:2f0e1e23c242 | 4342 | (ofs - 1) / bcs >= (ifptr - 1) / bcs) { /* When seek to same or following cluster, */ |
glebiuskv | 0:2f0e1e23c242 | 4343 | fp->fptr = (ifptr - 1) & ~(FSIZE_t)(bcs - 1); /* start from the current cluster */ |
glebiuskv | 0:2f0e1e23c242 | 4344 | ofs -= fp->fptr; |
glebiuskv | 0:2f0e1e23c242 | 4345 | clst = fp->clust; |
glebiuskv | 0:2f0e1e23c242 | 4346 | } else { /* When seek to back cluster, */ |
glebiuskv | 0:2f0e1e23c242 | 4347 | clst = fp->obj.sclust; /* start from the first cluster */ |
glebiuskv | 0:2f0e1e23c242 | 4348 | #if !FF_FS_READONLY |
glebiuskv | 0:2f0e1e23c242 | 4349 | if (clst == 0) { /* If no cluster chain, create a new chain */ |
glebiuskv | 0:2f0e1e23c242 | 4350 | clst = create_chain(&fp->obj, 0); |
glebiuskv | 0:2f0e1e23c242 | 4351 | if (clst == 1) ABORT(fs, FR_INT_ERR); |
glebiuskv | 0:2f0e1e23c242 | 4352 | if (clst == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR); |
glebiuskv | 0:2f0e1e23c242 | 4353 | fp->obj.sclust = clst; |
glebiuskv | 0:2f0e1e23c242 | 4354 | } |
glebiuskv | 0:2f0e1e23c242 | 4355 | #endif |
glebiuskv | 0:2f0e1e23c242 | 4356 | fp->clust = clst; |
glebiuskv | 0:2f0e1e23c242 | 4357 | } |
glebiuskv | 0:2f0e1e23c242 | 4358 | if (clst != 0) { |
glebiuskv | 0:2f0e1e23c242 | 4359 | while (ofs > bcs) { /* Cluster following loop */ |
glebiuskv | 0:2f0e1e23c242 | 4360 | ofs -= bcs; fp->fptr += bcs; |
glebiuskv | 0:2f0e1e23c242 | 4361 | #if !FF_FS_READONLY |
glebiuskv | 0:2f0e1e23c242 | 4362 | if (fp->flag & FA_WRITE) { /* Check if in write mode or not */ |
glebiuskv | 0:2f0e1e23c242 | 4363 | if (FF_FS_EXFAT && fp->fptr > fp->obj.objsize) { /* No FAT chain object needs correct objsize to generate FAT value */ |
glebiuskv | 0:2f0e1e23c242 | 4364 | fp->obj.objsize = fp->fptr; |
glebiuskv | 0:2f0e1e23c242 | 4365 | fp->flag |= FA_MODIFIED; |
glebiuskv | 0:2f0e1e23c242 | 4366 | } |
glebiuskv | 0:2f0e1e23c242 | 4367 | clst = create_chain(&fp->obj, clst); /* Follow chain with forceed stretch */ |
glebiuskv | 0:2f0e1e23c242 | 4368 | if (clst == 0) { /* Clip file size in case of disk full */ |
glebiuskv | 0:2f0e1e23c242 | 4369 | ofs = 0; break; |
glebiuskv | 0:2f0e1e23c242 | 4370 | } |
glebiuskv | 0:2f0e1e23c242 | 4371 | } else |
glebiuskv | 0:2f0e1e23c242 | 4372 | #endif |
glebiuskv | 0:2f0e1e23c242 | 4373 | { |
glebiuskv | 0:2f0e1e23c242 | 4374 | clst = get_fat(&fp->obj, clst); /* Follow cluster chain if not in write mode */ |
glebiuskv | 0:2f0e1e23c242 | 4375 | } |
glebiuskv | 0:2f0e1e23c242 | 4376 | if (clst == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR); |
glebiuskv | 0:2f0e1e23c242 | 4377 | if (clst <= 1 || clst >= fs->n_fatent) ABORT(fs, FR_INT_ERR); |
glebiuskv | 0:2f0e1e23c242 | 4378 | fp->clust = clst; |
glebiuskv | 0:2f0e1e23c242 | 4379 | } |
glebiuskv | 0:2f0e1e23c242 | 4380 | fp->fptr += ofs; |
glebiuskv | 0:2f0e1e23c242 | 4381 | if (ofs % SS(fs)) { |
glebiuskv | 0:2f0e1e23c242 | 4382 | nsect = clst2sect(fs, clst); /* Current sector */ |
glebiuskv | 0:2f0e1e23c242 | 4383 | if (nsect == 0) ABORT(fs, FR_INT_ERR); |
glebiuskv | 0:2f0e1e23c242 | 4384 | nsect += (DWORD)(ofs / SS(fs)); |
glebiuskv | 0:2f0e1e23c242 | 4385 | } |
glebiuskv | 0:2f0e1e23c242 | 4386 | } |
glebiuskv | 0:2f0e1e23c242 | 4387 | } |
glebiuskv | 0:2f0e1e23c242 | 4388 | if (!FF_FS_READONLY && fp->fptr > fp->obj.objsize) { /* Set file change flag if the file size is extended */ |
glebiuskv | 0:2f0e1e23c242 | 4389 | fp->obj.objsize = fp->fptr; |
glebiuskv | 0:2f0e1e23c242 | 4390 | fp->flag |= FA_MODIFIED; |
glebiuskv | 0:2f0e1e23c242 | 4391 | } |
glebiuskv | 0:2f0e1e23c242 | 4392 | if (fp->fptr % SS(fs) && nsect != fp->sect) { /* Fill sector cache if needed */ |
glebiuskv | 0:2f0e1e23c242 | 4393 | #if !FF_FS_TINY |
glebiuskv | 0:2f0e1e23c242 | 4394 | #if !FF_FS_READONLY |
glebiuskv | 0:2f0e1e23c242 | 4395 | if (fp->flag & FA_DIRTY) { /* Write-back dirty sector cache */ |
glebiuskv | 0:2f0e1e23c242 | 4396 | if (disk_write(fs->pdrv, fp->buf, fp->sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR); |
glebiuskv | 0:2f0e1e23c242 | 4397 | fp->flag &= (BYTE)~FA_DIRTY; |
glebiuskv | 0:2f0e1e23c242 | 4398 | } |
glebiuskv | 0:2f0e1e23c242 | 4399 | #endif |
glebiuskv | 0:2f0e1e23c242 | 4400 | if (disk_read(fs->pdrv, fp->buf, nsect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR); /* Fill sector cache */ |
glebiuskv | 0:2f0e1e23c242 | 4401 | #endif |
glebiuskv | 0:2f0e1e23c242 | 4402 | fp->sect = nsect; |
glebiuskv | 0:2f0e1e23c242 | 4403 | } |
glebiuskv | 0:2f0e1e23c242 | 4404 | } |
glebiuskv | 0:2f0e1e23c242 | 4405 | |
glebiuskv | 0:2f0e1e23c242 | 4406 | LEAVE_FF(fs, res); |
glebiuskv | 0:2f0e1e23c242 | 4407 | } |
glebiuskv | 0:2f0e1e23c242 | 4408 | |
glebiuskv | 0:2f0e1e23c242 | 4409 | |
glebiuskv | 0:2f0e1e23c242 | 4410 | |
glebiuskv | 0:2f0e1e23c242 | 4411 | #if FF_FS_MINIMIZE <= 1 |
glebiuskv | 0:2f0e1e23c242 | 4412 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 4413 | /* Create a Directory Object */ |
glebiuskv | 0:2f0e1e23c242 | 4414 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 4415 | |
glebiuskv | 0:2f0e1e23c242 | 4416 | FRESULT f_opendir ( |
glebiuskv | 0:2f0e1e23c242 | 4417 | FATFS_DIR* dp, /* Pointer to directory object to create */ |
glebiuskv | 0:2f0e1e23c242 | 4418 | const TCHAR* path /* Pointer to the directory path */ |
glebiuskv | 0:2f0e1e23c242 | 4419 | ) |
glebiuskv | 0:2f0e1e23c242 | 4420 | { |
glebiuskv | 0:2f0e1e23c242 | 4421 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 4422 | FATFS *fs; |
glebiuskv | 0:2f0e1e23c242 | 4423 | DEF_NAMBUF |
glebiuskv | 0:2f0e1e23c242 | 4424 | |
glebiuskv | 0:2f0e1e23c242 | 4425 | |
glebiuskv | 0:2f0e1e23c242 | 4426 | if (!dp) return FR_INVALID_OBJECT; |
glebiuskv | 0:2f0e1e23c242 | 4427 | |
glebiuskv | 0:2f0e1e23c242 | 4428 | /* Get logical drive */ |
glebiuskv | 0:2f0e1e23c242 | 4429 | res = find_volume(&path, &fs, 0); |
glebiuskv | 0:2f0e1e23c242 | 4430 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 4431 | dp->obj.fs = fs; |
glebiuskv | 0:2f0e1e23c242 | 4432 | INIT_NAMBUF(fs); |
glebiuskv | 0:2f0e1e23c242 | 4433 | res = follow_path(dp, path); /* Follow the path to the directory */ |
glebiuskv | 0:2f0e1e23c242 | 4434 | if (res == FR_OK) { /* Follow completed */ |
glebiuskv | 0:2f0e1e23c242 | 4435 | if (!(dp->fn[NSFLAG] & NS_NONAME)) { /* It is not the origin directory itself */ |
glebiuskv | 0:2f0e1e23c242 | 4436 | if (dp->obj.attr & AM_DIR) { /* This object is a sub-directory */ |
glebiuskv | 0:2f0e1e23c242 | 4437 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 4438 | if (fs->fs_type == FS_EXFAT) { |
glebiuskv | 0:2f0e1e23c242 | 4439 | dp->obj.c_scl = dp->obj.sclust; /* Get containing directory inforamation */ |
glebiuskv | 0:2f0e1e23c242 | 4440 | dp->obj.c_size = ((DWORD)dp->obj.objsize & 0xFFFFFF00) | dp->obj.stat; |
glebiuskv | 0:2f0e1e23c242 | 4441 | dp->obj.c_ofs = dp->blk_ofs; |
glebiuskv | 0:2f0e1e23c242 | 4442 | init_alloc_info(fs, &dp->obj); /* Get object allocation info */ |
glebiuskv | 0:2f0e1e23c242 | 4443 | } else |
glebiuskv | 0:2f0e1e23c242 | 4444 | #endif |
glebiuskv | 0:2f0e1e23c242 | 4445 | { |
glebiuskv | 0:2f0e1e23c242 | 4446 | dp->obj.sclust = ld_clust(fs, dp->dir); /* Get object allocation info */ |
glebiuskv | 0:2f0e1e23c242 | 4447 | } |
glebiuskv | 0:2f0e1e23c242 | 4448 | } else { /* This object is a file */ |
glebiuskv | 0:2f0e1e23c242 | 4449 | res = FR_NO_PATH; |
glebiuskv | 0:2f0e1e23c242 | 4450 | } |
glebiuskv | 0:2f0e1e23c242 | 4451 | } |
glebiuskv | 0:2f0e1e23c242 | 4452 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 4453 | dp->obj.id = fs->id; |
glebiuskv | 0:2f0e1e23c242 | 4454 | res = dir_sdi(dp, 0); /* Rewind directory */ |
glebiuskv | 0:2f0e1e23c242 | 4455 | #if FF_FS_LOCK != 0 |
glebiuskv | 0:2f0e1e23c242 | 4456 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 4457 | if (dp->obj.sclust != 0) { |
glebiuskv | 0:2f0e1e23c242 | 4458 | dp->obj.lockid = inc_lock(dp, 0); /* Lock the sub directory */ |
glebiuskv | 0:2f0e1e23c242 | 4459 | if (!dp->obj.lockid) res = FR_TOO_MANY_OPEN_FILES; |
glebiuskv | 0:2f0e1e23c242 | 4460 | } else { |
glebiuskv | 0:2f0e1e23c242 | 4461 | dp->obj.lockid = 0; /* Root directory need not to be locked */ |
glebiuskv | 0:2f0e1e23c242 | 4462 | } |
glebiuskv | 0:2f0e1e23c242 | 4463 | } |
glebiuskv | 0:2f0e1e23c242 | 4464 | #endif |
glebiuskv | 0:2f0e1e23c242 | 4465 | } |
glebiuskv | 0:2f0e1e23c242 | 4466 | } |
glebiuskv | 0:2f0e1e23c242 | 4467 | FREE_NAMBUF(); |
glebiuskv | 0:2f0e1e23c242 | 4468 | if (res == FR_NO_FILE) res = FR_NO_PATH; |
glebiuskv | 0:2f0e1e23c242 | 4469 | } |
glebiuskv | 0:2f0e1e23c242 | 4470 | if (res != FR_OK) dp->obj.fs = 0; /* Invalidate the directory object if function faild */ |
glebiuskv | 0:2f0e1e23c242 | 4471 | |
glebiuskv | 0:2f0e1e23c242 | 4472 | LEAVE_FF(fs, res); |
glebiuskv | 0:2f0e1e23c242 | 4473 | } |
glebiuskv | 0:2f0e1e23c242 | 4474 | |
glebiuskv | 0:2f0e1e23c242 | 4475 | |
glebiuskv | 0:2f0e1e23c242 | 4476 | |
glebiuskv | 0:2f0e1e23c242 | 4477 | |
glebiuskv | 0:2f0e1e23c242 | 4478 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 4479 | /* Close Directory */ |
glebiuskv | 0:2f0e1e23c242 | 4480 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 4481 | |
glebiuskv | 0:2f0e1e23c242 | 4482 | FRESULT f_closedir ( |
glebiuskv | 0:2f0e1e23c242 | 4483 | FATFS_DIR *dp /* Pointer to the directory object to be closed */ |
glebiuskv | 0:2f0e1e23c242 | 4484 | ) |
glebiuskv | 0:2f0e1e23c242 | 4485 | { |
glebiuskv | 0:2f0e1e23c242 | 4486 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 4487 | FATFS *fs; |
glebiuskv | 0:2f0e1e23c242 | 4488 | |
glebiuskv | 0:2f0e1e23c242 | 4489 | |
glebiuskv | 0:2f0e1e23c242 | 4490 | res = validate(&dp->obj, &fs); /* Check validity of the file object */ |
glebiuskv | 0:2f0e1e23c242 | 4491 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 4492 | #if FF_FS_LOCK != 0 |
glebiuskv | 0:2f0e1e23c242 | 4493 | if (dp->obj.lockid) res = dec_lock(dp->obj.lockid); /* Decrement sub-directory open counter */ |
glebiuskv | 0:2f0e1e23c242 | 4494 | if (res == FR_OK) dp->obj.fs = 0; /* Invalidate directory object */ |
glebiuskv | 0:2f0e1e23c242 | 4495 | #else |
glebiuskv | 0:2f0e1e23c242 | 4496 | dp->obj.fs = 0; /* Invalidate directory object */ |
glebiuskv | 0:2f0e1e23c242 | 4497 | #endif |
glebiuskv | 0:2f0e1e23c242 | 4498 | #if FF_FS_REENTRANT |
glebiuskv | 0:2f0e1e23c242 | 4499 | unlock_fs(fs, FR_OK); /* Unlock volume */ |
glebiuskv | 0:2f0e1e23c242 | 4500 | #endif |
glebiuskv | 0:2f0e1e23c242 | 4501 | } |
glebiuskv | 0:2f0e1e23c242 | 4502 | return res; |
glebiuskv | 0:2f0e1e23c242 | 4503 | } |
glebiuskv | 0:2f0e1e23c242 | 4504 | |
glebiuskv | 0:2f0e1e23c242 | 4505 | |
glebiuskv | 0:2f0e1e23c242 | 4506 | |
glebiuskv | 0:2f0e1e23c242 | 4507 | |
glebiuskv | 0:2f0e1e23c242 | 4508 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 4509 | /* Read Directory Entries in Sequence */ |
glebiuskv | 0:2f0e1e23c242 | 4510 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 4511 | |
glebiuskv | 0:2f0e1e23c242 | 4512 | FRESULT f_readdir ( |
glebiuskv | 0:2f0e1e23c242 | 4513 | FATFS_DIR* dp, /* Pointer to the open directory object */ |
glebiuskv | 0:2f0e1e23c242 | 4514 | FILINFO* fno /* Pointer to file information to return */ |
glebiuskv | 0:2f0e1e23c242 | 4515 | ) |
glebiuskv | 0:2f0e1e23c242 | 4516 | { |
glebiuskv | 0:2f0e1e23c242 | 4517 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 4518 | FATFS *fs; |
glebiuskv | 0:2f0e1e23c242 | 4519 | DEF_NAMBUF |
glebiuskv | 0:2f0e1e23c242 | 4520 | |
glebiuskv | 0:2f0e1e23c242 | 4521 | |
glebiuskv | 0:2f0e1e23c242 | 4522 | res = validate(&dp->obj, &fs); /* Check validity of the directory object */ |
glebiuskv | 0:2f0e1e23c242 | 4523 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 4524 | if (!fno) { |
glebiuskv | 0:2f0e1e23c242 | 4525 | res = dir_sdi(dp, 0); /* Rewind the directory object */ |
glebiuskv | 0:2f0e1e23c242 | 4526 | } else { |
glebiuskv | 0:2f0e1e23c242 | 4527 | INIT_NAMBUF(fs); |
glebiuskv | 0:2f0e1e23c242 | 4528 | res = dir_read_file(dp); /* Read an item */ |
glebiuskv | 0:2f0e1e23c242 | 4529 | if (res == FR_NO_FILE) res = FR_OK; /* Ignore end of directory */ |
glebiuskv | 0:2f0e1e23c242 | 4530 | if (res == FR_OK) { /* A valid entry is found */ |
glebiuskv | 0:2f0e1e23c242 | 4531 | get_fileinfo(dp, fno); /* Get the object information */ |
glebiuskv | 0:2f0e1e23c242 | 4532 | res = dir_next(dp, 0); /* Increment index for next */ |
glebiuskv | 0:2f0e1e23c242 | 4533 | if (res == FR_NO_FILE) res = FR_OK; /* Ignore end of directory now */ |
glebiuskv | 0:2f0e1e23c242 | 4534 | } |
glebiuskv | 0:2f0e1e23c242 | 4535 | FREE_NAMBUF(); |
glebiuskv | 0:2f0e1e23c242 | 4536 | } |
glebiuskv | 0:2f0e1e23c242 | 4537 | } |
glebiuskv | 0:2f0e1e23c242 | 4538 | LEAVE_FF(fs, res); |
glebiuskv | 0:2f0e1e23c242 | 4539 | } |
glebiuskv | 0:2f0e1e23c242 | 4540 | |
glebiuskv | 0:2f0e1e23c242 | 4541 | |
glebiuskv | 0:2f0e1e23c242 | 4542 | |
glebiuskv | 0:2f0e1e23c242 | 4543 | #if FF_USE_FIND |
glebiuskv | 0:2f0e1e23c242 | 4544 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 4545 | /* Find Next File */ |
glebiuskv | 0:2f0e1e23c242 | 4546 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 4547 | |
glebiuskv | 0:2f0e1e23c242 | 4548 | FRESULT f_findnext ( |
glebiuskv | 0:2f0e1e23c242 | 4549 | FATFS_DIR* dp, /* Pointer to the open directory object */ |
glebiuskv | 0:2f0e1e23c242 | 4550 | FILINFO* fno /* Pointer to the file information structure */ |
glebiuskv | 0:2f0e1e23c242 | 4551 | ) |
glebiuskv | 0:2f0e1e23c242 | 4552 | { |
glebiuskv | 0:2f0e1e23c242 | 4553 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 4554 | |
glebiuskv | 0:2f0e1e23c242 | 4555 | |
glebiuskv | 0:2f0e1e23c242 | 4556 | for (;;) { |
glebiuskv | 0:2f0e1e23c242 | 4557 | res = f_readdir(dp, fno); /* Get a directory item */ |
glebiuskv | 0:2f0e1e23c242 | 4558 | if (res != FR_OK || !fno || !fno->fname[0]) break; /* Terminate if any error or end of directory */ |
glebiuskv | 0:2f0e1e23c242 | 4559 | if (pattern_matching(dp->pat, fno->fname, 0, 0)) break; /* Test for the file name */ |
glebiuskv | 0:2f0e1e23c242 | 4560 | #if FF_USE_LFN && FF_USE_FIND == 2 |
glebiuskv | 0:2f0e1e23c242 | 4561 | if (pattern_matching(dp->pat, fno->altname, 0, 0)) break; /* Test for alternative name if exist */ |
glebiuskv | 0:2f0e1e23c242 | 4562 | #endif |
glebiuskv | 0:2f0e1e23c242 | 4563 | } |
glebiuskv | 0:2f0e1e23c242 | 4564 | return res; |
glebiuskv | 0:2f0e1e23c242 | 4565 | } |
glebiuskv | 0:2f0e1e23c242 | 4566 | |
glebiuskv | 0:2f0e1e23c242 | 4567 | |
glebiuskv | 0:2f0e1e23c242 | 4568 | |
glebiuskv | 0:2f0e1e23c242 | 4569 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 4570 | /* Find First File */ |
glebiuskv | 0:2f0e1e23c242 | 4571 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 4572 | |
glebiuskv | 0:2f0e1e23c242 | 4573 | FRESULT f_findfirst ( |
glebiuskv | 0:2f0e1e23c242 | 4574 | FATFS_DIR* dp, /* Pointer to the blank directory object */ |
glebiuskv | 0:2f0e1e23c242 | 4575 | FILINFO* fno, /* Pointer to the file information structure */ |
glebiuskv | 0:2f0e1e23c242 | 4576 | const TCHAR* path, /* Pointer to the directory to open */ |
glebiuskv | 0:2f0e1e23c242 | 4577 | const TCHAR* pattern /* Pointer to the matching pattern */ |
glebiuskv | 0:2f0e1e23c242 | 4578 | ) |
glebiuskv | 0:2f0e1e23c242 | 4579 | { |
glebiuskv | 0:2f0e1e23c242 | 4580 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 4581 | |
glebiuskv | 0:2f0e1e23c242 | 4582 | |
glebiuskv | 0:2f0e1e23c242 | 4583 | dp->pat = pattern; /* Save pointer to pattern string */ |
glebiuskv | 0:2f0e1e23c242 | 4584 | res = f_opendir(dp, path); /* Open the target directory */ |
glebiuskv | 0:2f0e1e23c242 | 4585 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 4586 | res = f_findnext(dp, fno); /* Find the first item */ |
glebiuskv | 0:2f0e1e23c242 | 4587 | } |
glebiuskv | 0:2f0e1e23c242 | 4588 | return res; |
glebiuskv | 0:2f0e1e23c242 | 4589 | } |
glebiuskv | 0:2f0e1e23c242 | 4590 | |
glebiuskv | 0:2f0e1e23c242 | 4591 | #endif /* FF_USE_FIND */ |
glebiuskv | 0:2f0e1e23c242 | 4592 | |
glebiuskv | 0:2f0e1e23c242 | 4593 | |
glebiuskv | 0:2f0e1e23c242 | 4594 | |
glebiuskv | 0:2f0e1e23c242 | 4595 | #if FF_FS_MINIMIZE == 0 |
glebiuskv | 0:2f0e1e23c242 | 4596 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 4597 | /* Get File Status */ |
glebiuskv | 0:2f0e1e23c242 | 4598 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 4599 | |
glebiuskv | 0:2f0e1e23c242 | 4600 | FRESULT f_stat ( |
glebiuskv | 0:2f0e1e23c242 | 4601 | const TCHAR* path, /* Pointer to the file path */ |
glebiuskv | 0:2f0e1e23c242 | 4602 | FILINFO* fno /* Pointer to file information to return */ |
glebiuskv | 0:2f0e1e23c242 | 4603 | ) |
glebiuskv | 0:2f0e1e23c242 | 4604 | { |
glebiuskv | 0:2f0e1e23c242 | 4605 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 4606 | FATFS_DIR dj; |
glebiuskv | 0:2f0e1e23c242 | 4607 | DEF_NAMBUF |
glebiuskv | 0:2f0e1e23c242 | 4608 | |
glebiuskv | 0:2f0e1e23c242 | 4609 | |
glebiuskv | 0:2f0e1e23c242 | 4610 | /* Get logical drive */ |
glebiuskv | 0:2f0e1e23c242 | 4611 | res = find_volume(&path, &dj.obj.fs, 0); |
glebiuskv | 0:2f0e1e23c242 | 4612 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 4613 | INIT_NAMBUF(dj.obj.fs); |
glebiuskv | 0:2f0e1e23c242 | 4614 | res = follow_path(&dj, path); /* Follow the file path */ |
glebiuskv | 0:2f0e1e23c242 | 4615 | if (res == FR_OK) { /* Follow completed */ |
glebiuskv | 0:2f0e1e23c242 | 4616 | if (dj.fn[NSFLAG] & NS_NONAME) { /* It is origin directory */ |
glebiuskv | 0:2f0e1e23c242 | 4617 | res = FR_INVALID_NAME; |
glebiuskv | 0:2f0e1e23c242 | 4618 | } else { /* Found an object */ |
glebiuskv | 0:2f0e1e23c242 | 4619 | if (fno) get_fileinfo(&dj, fno); |
glebiuskv | 0:2f0e1e23c242 | 4620 | } |
glebiuskv | 0:2f0e1e23c242 | 4621 | } |
glebiuskv | 0:2f0e1e23c242 | 4622 | FREE_NAMBUF(); |
glebiuskv | 0:2f0e1e23c242 | 4623 | } |
glebiuskv | 0:2f0e1e23c242 | 4624 | |
glebiuskv | 0:2f0e1e23c242 | 4625 | LEAVE_FF(dj.obj.fs, res); |
glebiuskv | 0:2f0e1e23c242 | 4626 | } |
glebiuskv | 0:2f0e1e23c242 | 4627 | |
glebiuskv | 0:2f0e1e23c242 | 4628 | |
glebiuskv | 0:2f0e1e23c242 | 4629 | |
glebiuskv | 0:2f0e1e23c242 | 4630 | #if !FF_FS_READONLY |
glebiuskv | 0:2f0e1e23c242 | 4631 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 4632 | /* Get Number of Free Clusters */ |
glebiuskv | 0:2f0e1e23c242 | 4633 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 4634 | |
glebiuskv | 0:2f0e1e23c242 | 4635 | FRESULT f_getfree ( |
glebiuskv | 0:2f0e1e23c242 | 4636 | const TCHAR* path, /* Logical drive number */ |
glebiuskv | 0:2f0e1e23c242 | 4637 | DWORD* nclst, /* Pointer to a variable to return number of free clusters */ |
glebiuskv | 0:2f0e1e23c242 | 4638 | FATFS** fatfs /* Pointer to return pointer to corresponding filesystem object */ |
glebiuskv | 0:2f0e1e23c242 | 4639 | ) |
glebiuskv | 0:2f0e1e23c242 | 4640 | { |
glebiuskv | 0:2f0e1e23c242 | 4641 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 4642 | FATFS *fs; |
glebiuskv | 0:2f0e1e23c242 | 4643 | DWORD nfree, clst, sect, stat; |
glebiuskv | 0:2f0e1e23c242 | 4644 | UINT i; |
glebiuskv | 0:2f0e1e23c242 | 4645 | FFOBJID obj; |
glebiuskv | 0:2f0e1e23c242 | 4646 | |
glebiuskv | 0:2f0e1e23c242 | 4647 | |
glebiuskv | 0:2f0e1e23c242 | 4648 | /* Get logical drive */ |
glebiuskv | 0:2f0e1e23c242 | 4649 | res = find_volume(&path, &fs, 0); |
glebiuskv | 0:2f0e1e23c242 | 4650 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 4651 | *fatfs = fs; /* Return ptr to the fs object */ |
glebiuskv | 0:2f0e1e23c242 | 4652 | /* If free_clst is valid, return it without full FAT scan */ |
glebiuskv | 0:2f0e1e23c242 | 4653 | if (fs->free_clst <= fs->n_fatent - 2) { |
glebiuskv | 0:2f0e1e23c242 | 4654 | *nclst = fs->free_clst; |
glebiuskv | 0:2f0e1e23c242 | 4655 | } else { |
glebiuskv | 0:2f0e1e23c242 | 4656 | /* Scan FAT to obtain number of free clusters */ |
glebiuskv | 0:2f0e1e23c242 | 4657 | nfree = 0; |
glebiuskv | 0:2f0e1e23c242 | 4658 | if (fs->fs_type == FS_FAT12) { /* FAT12: Scan bit field FAT entries */ |
glebiuskv | 0:2f0e1e23c242 | 4659 | clst = 2; obj.fs = fs; |
glebiuskv | 0:2f0e1e23c242 | 4660 | do { |
glebiuskv | 0:2f0e1e23c242 | 4661 | stat = get_fat(&obj, clst); |
glebiuskv | 0:2f0e1e23c242 | 4662 | if (stat == 0xFFFFFFFF) { res = FR_DISK_ERR; break; } |
glebiuskv | 0:2f0e1e23c242 | 4663 | if (stat == 1) { res = FR_INT_ERR; break; } |
glebiuskv | 0:2f0e1e23c242 | 4664 | if (stat == 0) nfree++; |
glebiuskv | 0:2f0e1e23c242 | 4665 | } while (++clst < fs->n_fatent); |
glebiuskv | 0:2f0e1e23c242 | 4666 | } else { |
glebiuskv | 0:2f0e1e23c242 | 4667 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 4668 | if (fs->fs_type == FS_EXFAT) { /* exFAT: Scan allocation bitmap */ |
glebiuskv | 0:2f0e1e23c242 | 4669 | BYTE bm; |
glebiuskv | 0:2f0e1e23c242 | 4670 | UINT b; |
glebiuskv | 0:2f0e1e23c242 | 4671 | |
glebiuskv | 0:2f0e1e23c242 | 4672 | clst = fs->n_fatent - 2; /* Number of clusters */ |
glebiuskv | 0:2f0e1e23c242 | 4673 | sect = fs->database; /* Assuming bitmap starts at cluster 2 */ |
glebiuskv | 0:2f0e1e23c242 | 4674 | i = 0; /* Offset in the sector */ |
glebiuskv | 0:2f0e1e23c242 | 4675 | do { /* Counts numbuer of bits with zero in the bitmap */ |
glebiuskv | 0:2f0e1e23c242 | 4676 | if (i == 0) { |
glebiuskv | 0:2f0e1e23c242 | 4677 | res = move_window(fs, sect++); |
glebiuskv | 0:2f0e1e23c242 | 4678 | if (res != FR_OK) break; |
glebiuskv | 0:2f0e1e23c242 | 4679 | } |
glebiuskv | 0:2f0e1e23c242 | 4680 | for (b = 8, bm = fs->win[i]; b && clst; b--, clst--) { |
glebiuskv | 0:2f0e1e23c242 | 4681 | if (!(bm & 1)) nfree++; |
glebiuskv | 0:2f0e1e23c242 | 4682 | bm >>= 1; |
glebiuskv | 0:2f0e1e23c242 | 4683 | } |
glebiuskv | 0:2f0e1e23c242 | 4684 | i = (i + 1) % SS(fs); |
glebiuskv | 0:2f0e1e23c242 | 4685 | } while (clst); |
glebiuskv | 0:2f0e1e23c242 | 4686 | } else |
glebiuskv | 0:2f0e1e23c242 | 4687 | #endif |
glebiuskv | 0:2f0e1e23c242 | 4688 | { /* FAT16/32: Scan WORD/DWORD FAT entries */ |
glebiuskv | 0:2f0e1e23c242 | 4689 | clst = fs->n_fatent; /* Number of entries */ |
glebiuskv | 0:2f0e1e23c242 | 4690 | sect = fs->fatbase; /* Top of the FAT */ |
glebiuskv | 0:2f0e1e23c242 | 4691 | i = 0; /* Offset in the sector */ |
glebiuskv | 0:2f0e1e23c242 | 4692 | do { /* Counts numbuer of entries with zero in the FAT */ |
glebiuskv | 0:2f0e1e23c242 | 4693 | if (i == 0) { |
glebiuskv | 0:2f0e1e23c242 | 4694 | res = move_window(fs, sect++); |
glebiuskv | 0:2f0e1e23c242 | 4695 | if (res != FR_OK) break; |
glebiuskv | 0:2f0e1e23c242 | 4696 | } |
glebiuskv | 0:2f0e1e23c242 | 4697 | if (fs->fs_type == FS_FAT16) { |
glebiuskv | 0:2f0e1e23c242 | 4698 | if (ld_word(fs->win + i) == 0) nfree++; |
glebiuskv | 0:2f0e1e23c242 | 4699 | i += 2; |
glebiuskv | 0:2f0e1e23c242 | 4700 | } else { |
glebiuskv | 0:2f0e1e23c242 | 4701 | if ((ld_dword(fs->win + i) & 0x0FFFFFFF) == 0) nfree++; |
glebiuskv | 0:2f0e1e23c242 | 4702 | i += 4; |
glebiuskv | 0:2f0e1e23c242 | 4703 | } |
glebiuskv | 0:2f0e1e23c242 | 4704 | i %= SS(fs); |
glebiuskv | 0:2f0e1e23c242 | 4705 | } while (--clst); |
glebiuskv | 0:2f0e1e23c242 | 4706 | } |
glebiuskv | 0:2f0e1e23c242 | 4707 | } |
glebiuskv | 0:2f0e1e23c242 | 4708 | *nclst = nfree; /* Return the free clusters */ |
glebiuskv | 0:2f0e1e23c242 | 4709 | fs->free_clst = nfree; /* Now free_clst is valid */ |
glebiuskv | 0:2f0e1e23c242 | 4710 | fs->fsi_flag |= 1; /* FAT32: FSInfo is to be updated */ |
glebiuskv | 0:2f0e1e23c242 | 4711 | } |
glebiuskv | 0:2f0e1e23c242 | 4712 | } |
glebiuskv | 0:2f0e1e23c242 | 4713 | |
glebiuskv | 0:2f0e1e23c242 | 4714 | LEAVE_FF(fs, res); |
glebiuskv | 0:2f0e1e23c242 | 4715 | } |
glebiuskv | 0:2f0e1e23c242 | 4716 | |
glebiuskv | 0:2f0e1e23c242 | 4717 | |
glebiuskv | 0:2f0e1e23c242 | 4718 | |
glebiuskv | 0:2f0e1e23c242 | 4719 | |
glebiuskv | 0:2f0e1e23c242 | 4720 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 4721 | /* Truncate File */ |
glebiuskv | 0:2f0e1e23c242 | 4722 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 4723 | |
glebiuskv | 0:2f0e1e23c242 | 4724 | FRESULT f_truncate ( |
glebiuskv | 0:2f0e1e23c242 | 4725 | FIL* fp /* Pointer to the file object */ |
glebiuskv | 0:2f0e1e23c242 | 4726 | ) |
glebiuskv | 0:2f0e1e23c242 | 4727 | { |
glebiuskv | 0:2f0e1e23c242 | 4728 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 4729 | FATFS *fs; |
glebiuskv | 0:2f0e1e23c242 | 4730 | DWORD ncl; |
glebiuskv | 0:2f0e1e23c242 | 4731 | |
glebiuskv | 0:2f0e1e23c242 | 4732 | |
glebiuskv | 0:2f0e1e23c242 | 4733 | res = validate(&fp->obj, &fs); /* Check validity of the file object */ |
glebiuskv | 0:2f0e1e23c242 | 4734 | if (res != FR_OK || (res = (FRESULT)fp->err) != FR_OK) LEAVE_FF(fs, res); |
glebiuskv | 0:2f0e1e23c242 | 4735 | if (!(fp->flag & FA_WRITE)) LEAVE_FF(fs, FR_DENIED); /* Check access mode */ |
glebiuskv | 0:2f0e1e23c242 | 4736 | |
glebiuskv | 0:2f0e1e23c242 | 4737 | if (fp->fptr < fp->obj.objsize) { /* Process when fptr is not on the eof */ |
glebiuskv | 0:2f0e1e23c242 | 4738 | if (fp->fptr == 0) { /* When set file size to zero, remove entire cluster chain */ |
glebiuskv | 0:2f0e1e23c242 | 4739 | res = remove_chain(&fp->obj, fp->obj.sclust, 0); |
glebiuskv | 0:2f0e1e23c242 | 4740 | fp->obj.sclust = 0; |
glebiuskv | 0:2f0e1e23c242 | 4741 | } else { /* When truncate a part of the file, remove remaining clusters */ |
glebiuskv | 0:2f0e1e23c242 | 4742 | ncl = get_fat(&fp->obj, fp->clust); |
glebiuskv | 0:2f0e1e23c242 | 4743 | res = FR_OK; |
glebiuskv | 0:2f0e1e23c242 | 4744 | if (ncl == 0xFFFFFFFF) res = FR_DISK_ERR; |
glebiuskv | 0:2f0e1e23c242 | 4745 | if (ncl == 1) res = FR_INT_ERR; |
glebiuskv | 0:2f0e1e23c242 | 4746 | if (res == FR_OK && ncl < fs->n_fatent) { |
glebiuskv | 0:2f0e1e23c242 | 4747 | res = remove_chain(&fp->obj, ncl, fp->clust); |
glebiuskv | 0:2f0e1e23c242 | 4748 | } |
glebiuskv | 0:2f0e1e23c242 | 4749 | } |
glebiuskv | 0:2f0e1e23c242 | 4750 | fp->obj.objsize = fp->fptr; /* Set file size to current read/write point */ |
glebiuskv | 0:2f0e1e23c242 | 4751 | fp->flag |= FA_MODIFIED; |
glebiuskv | 0:2f0e1e23c242 | 4752 | #if !FF_FS_TINY |
glebiuskv | 0:2f0e1e23c242 | 4753 | if (res == FR_OK && (fp->flag & FA_DIRTY)) { |
glebiuskv | 0:2f0e1e23c242 | 4754 | if (disk_write(fs->pdrv, fp->buf, fp->sect, 1) != RES_OK) { |
glebiuskv | 0:2f0e1e23c242 | 4755 | res = FR_DISK_ERR; |
glebiuskv | 0:2f0e1e23c242 | 4756 | } else { |
glebiuskv | 0:2f0e1e23c242 | 4757 | fp->flag &= (BYTE)~FA_DIRTY; |
glebiuskv | 0:2f0e1e23c242 | 4758 | } |
glebiuskv | 0:2f0e1e23c242 | 4759 | } |
glebiuskv | 0:2f0e1e23c242 | 4760 | #endif |
glebiuskv | 0:2f0e1e23c242 | 4761 | if (res != FR_OK) ABORT(fs, res); |
glebiuskv | 0:2f0e1e23c242 | 4762 | } |
glebiuskv | 0:2f0e1e23c242 | 4763 | |
glebiuskv | 0:2f0e1e23c242 | 4764 | LEAVE_FF(fs, res); |
glebiuskv | 0:2f0e1e23c242 | 4765 | } |
glebiuskv | 0:2f0e1e23c242 | 4766 | |
glebiuskv | 0:2f0e1e23c242 | 4767 | |
glebiuskv | 0:2f0e1e23c242 | 4768 | |
glebiuskv | 0:2f0e1e23c242 | 4769 | |
glebiuskv | 0:2f0e1e23c242 | 4770 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 4771 | /* Delete a File/Directory */ |
glebiuskv | 0:2f0e1e23c242 | 4772 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 4773 | |
glebiuskv | 0:2f0e1e23c242 | 4774 | FRESULT f_unlink ( |
glebiuskv | 0:2f0e1e23c242 | 4775 | const TCHAR* path /* Pointer to the file or directory path */ |
glebiuskv | 0:2f0e1e23c242 | 4776 | ) |
glebiuskv | 0:2f0e1e23c242 | 4777 | { |
glebiuskv | 0:2f0e1e23c242 | 4778 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 4779 | FATFS_DIR dj, sdj; |
glebiuskv | 0:2f0e1e23c242 | 4780 | DWORD dclst = 0; |
glebiuskv | 0:2f0e1e23c242 | 4781 | FATFS *fs; |
glebiuskv | 0:2f0e1e23c242 | 4782 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 4783 | FFOBJID obj; |
glebiuskv | 0:2f0e1e23c242 | 4784 | #endif |
glebiuskv | 0:2f0e1e23c242 | 4785 | DEF_NAMBUF |
glebiuskv | 0:2f0e1e23c242 | 4786 | |
glebiuskv | 0:2f0e1e23c242 | 4787 | |
glebiuskv | 0:2f0e1e23c242 | 4788 | /* Get logical drive */ |
glebiuskv | 0:2f0e1e23c242 | 4789 | res = find_volume(&path, &fs, FA_WRITE); |
glebiuskv | 0:2f0e1e23c242 | 4790 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 4791 | dj.obj.fs = fs; |
glebiuskv | 0:2f0e1e23c242 | 4792 | INIT_NAMBUF(fs); |
glebiuskv | 0:2f0e1e23c242 | 4793 | res = follow_path(&dj, path); /* Follow the file path */ |
glebiuskv | 0:2f0e1e23c242 | 4794 | if (FF_FS_RPATH && res == FR_OK && (dj.fn[NSFLAG] & NS_DOT)) { |
glebiuskv | 0:2f0e1e23c242 | 4795 | res = FR_INVALID_NAME; /* Cannot remove dot entry */ |
glebiuskv | 0:2f0e1e23c242 | 4796 | } |
glebiuskv | 0:2f0e1e23c242 | 4797 | #if FF_FS_LOCK != 0 |
glebiuskv | 0:2f0e1e23c242 | 4798 | if (res == FR_OK) res = chk_lock(&dj, 2); /* Check if it is an open object */ |
glebiuskv | 0:2f0e1e23c242 | 4799 | #endif |
glebiuskv | 0:2f0e1e23c242 | 4800 | if (res == FR_OK) { /* The object is accessible */ |
glebiuskv | 0:2f0e1e23c242 | 4801 | if (dj.fn[NSFLAG] & NS_NONAME) { |
glebiuskv | 0:2f0e1e23c242 | 4802 | res = FR_INVALID_NAME; /* Cannot remove the origin directory */ |
glebiuskv | 0:2f0e1e23c242 | 4803 | } else { |
glebiuskv | 0:2f0e1e23c242 | 4804 | if (dj.obj.attr & AM_RDO) { |
glebiuskv | 0:2f0e1e23c242 | 4805 | res = FR_DENIED; /* Cannot remove R/O object */ |
glebiuskv | 0:2f0e1e23c242 | 4806 | } |
glebiuskv | 0:2f0e1e23c242 | 4807 | } |
glebiuskv | 0:2f0e1e23c242 | 4808 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 4809 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 4810 | obj.fs = fs; |
glebiuskv | 0:2f0e1e23c242 | 4811 | if (fs->fs_type == FS_EXFAT) { |
glebiuskv | 0:2f0e1e23c242 | 4812 | init_alloc_info(fs, &obj); |
glebiuskv | 0:2f0e1e23c242 | 4813 | dclst = obj.sclust; |
glebiuskv | 0:2f0e1e23c242 | 4814 | } else |
glebiuskv | 0:2f0e1e23c242 | 4815 | #endif |
glebiuskv | 0:2f0e1e23c242 | 4816 | { |
glebiuskv | 0:2f0e1e23c242 | 4817 | dclst = ld_clust(fs, dj.dir); |
glebiuskv | 0:2f0e1e23c242 | 4818 | } |
glebiuskv | 0:2f0e1e23c242 | 4819 | if (dj.obj.attr & AM_DIR) { /* Is it a sub-directory? */ |
glebiuskv | 0:2f0e1e23c242 | 4820 | #if FF_FS_RPATH != 0 |
glebiuskv | 0:2f0e1e23c242 | 4821 | if (dclst == fs->cdir) { /* Is it the current directory? */ |
glebiuskv | 0:2f0e1e23c242 | 4822 | res = FR_DENIED; |
glebiuskv | 0:2f0e1e23c242 | 4823 | } else |
glebiuskv | 0:2f0e1e23c242 | 4824 | #endif |
glebiuskv | 0:2f0e1e23c242 | 4825 | { |
glebiuskv | 0:2f0e1e23c242 | 4826 | sdj.obj.fs = fs; /* Open the sub-directory */ |
glebiuskv | 0:2f0e1e23c242 | 4827 | sdj.obj.sclust = dclst; |
glebiuskv | 0:2f0e1e23c242 | 4828 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 4829 | if (fs->fs_type == FS_EXFAT) { |
glebiuskv | 0:2f0e1e23c242 | 4830 | sdj.obj.objsize = obj.objsize; |
glebiuskv | 0:2f0e1e23c242 | 4831 | sdj.obj.stat = obj.stat; |
glebiuskv | 0:2f0e1e23c242 | 4832 | } |
glebiuskv | 0:2f0e1e23c242 | 4833 | #endif |
glebiuskv | 0:2f0e1e23c242 | 4834 | res = dir_sdi(&sdj, 0); |
glebiuskv | 0:2f0e1e23c242 | 4835 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 4836 | res = dir_read_file(&sdj); /* Test if the directory is empty */ |
glebiuskv | 0:2f0e1e23c242 | 4837 | if (res == FR_OK) res = FR_DENIED; /* Not empty? */ |
glebiuskv | 0:2f0e1e23c242 | 4838 | if (res == FR_NO_FILE) res = FR_OK; /* Empty? */ |
glebiuskv | 0:2f0e1e23c242 | 4839 | } |
glebiuskv | 0:2f0e1e23c242 | 4840 | } |
glebiuskv | 0:2f0e1e23c242 | 4841 | } |
glebiuskv | 0:2f0e1e23c242 | 4842 | } |
glebiuskv | 0:2f0e1e23c242 | 4843 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 4844 | res = dir_remove(&dj); /* Remove the directory entry */ |
glebiuskv | 0:2f0e1e23c242 | 4845 | if (res == FR_OK && dclst != 0) { /* Remove the cluster chain if exist */ |
glebiuskv | 0:2f0e1e23c242 | 4846 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 4847 | res = remove_chain(&obj, dclst, 0); |
glebiuskv | 0:2f0e1e23c242 | 4848 | #else |
glebiuskv | 0:2f0e1e23c242 | 4849 | res = remove_chain(&dj.obj, dclst, 0); |
glebiuskv | 0:2f0e1e23c242 | 4850 | #endif |
glebiuskv | 0:2f0e1e23c242 | 4851 | } |
glebiuskv | 0:2f0e1e23c242 | 4852 | if (res == FR_OK) res = sync_fs(fs); |
glebiuskv | 0:2f0e1e23c242 | 4853 | } |
glebiuskv | 0:2f0e1e23c242 | 4854 | } |
glebiuskv | 0:2f0e1e23c242 | 4855 | FREE_NAMBUF(); |
glebiuskv | 0:2f0e1e23c242 | 4856 | } |
glebiuskv | 0:2f0e1e23c242 | 4857 | |
glebiuskv | 0:2f0e1e23c242 | 4858 | LEAVE_FF(fs, res); |
glebiuskv | 0:2f0e1e23c242 | 4859 | } |
glebiuskv | 0:2f0e1e23c242 | 4860 | |
glebiuskv | 0:2f0e1e23c242 | 4861 | |
glebiuskv | 0:2f0e1e23c242 | 4862 | |
glebiuskv | 0:2f0e1e23c242 | 4863 | |
glebiuskv | 0:2f0e1e23c242 | 4864 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 4865 | /* Create a Directory */ |
glebiuskv | 0:2f0e1e23c242 | 4866 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 4867 | |
glebiuskv | 0:2f0e1e23c242 | 4868 | FRESULT f_mkdir ( |
glebiuskv | 0:2f0e1e23c242 | 4869 | const TCHAR* path /* Pointer to the directory path */ |
glebiuskv | 0:2f0e1e23c242 | 4870 | ) |
glebiuskv | 0:2f0e1e23c242 | 4871 | { |
glebiuskv | 0:2f0e1e23c242 | 4872 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 4873 | FATFS_DIR dj; |
glebiuskv | 0:2f0e1e23c242 | 4874 | FATFS *fs; |
glebiuskv | 0:2f0e1e23c242 | 4875 | BYTE *dir; |
glebiuskv | 0:2f0e1e23c242 | 4876 | DWORD dcl, pcl, tm; |
glebiuskv | 0:2f0e1e23c242 | 4877 | DEF_NAMBUF |
glebiuskv | 0:2f0e1e23c242 | 4878 | |
glebiuskv | 0:2f0e1e23c242 | 4879 | |
glebiuskv | 0:2f0e1e23c242 | 4880 | /* Get logical drive */ |
glebiuskv | 0:2f0e1e23c242 | 4881 | res = find_volume(&path, &fs, FA_WRITE); |
glebiuskv | 0:2f0e1e23c242 | 4882 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 4883 | dj.obj.fs = fs; |
glebiuskv | 0:2f0e1e23c242 | 4884 | INIT_NAMBUF(fs); |
glebiuskv | 0:2f0e1e23c242 | 4885 | res = follow_path(&dj, path); /* Follow the file path */ |
glebiuskv | 0:2f0e1e23c242 | 4886 | if (res == FR_OK) res = FR_EXIST; /* Any object with same name is already existing */ |
glebiuskv | 0:2f0e1e23c242 | 4887 | if (FF_FS_RPATH && res == FR_NO_FILE && (dj.fn[NSFLAG] & NS_DOT)) { |
glebiuskv | 0:2f0e1e23c242 | 4888 | res = FR_INVALID_NAME; |
glebiuskv | 0:2f0e1e23c242 | 4889 | } |
glebiuskv | 0:2f0e1e23c242 | 4890 | if (res == FR_NO_FILE) { /* Can create a new directory */ |
glebiuskv | 0:2f0e1e23c242 | 4891 | dcl = create_chain(&dj.obj, 0); /* Allocate a cluster for the new directory table */ |
glebiuskv | 0:2f0e1e23c242 | 4892 | dj.obj.objsize = (DWORD)fs->csize * SS(fs); |
glebiuskv | 0:2f0e1e23c242 | 4893 | res = FR_OK; |
glebiuskv | 0:2f0e1e23c242 | 4894 | if (dcl == 0) res = FR_DENIED; /* No space to allocate a new cluster */ |
glebiuskv | 0:2f0e1e23c242 | 4895 | if (dcl == 1) res = FR_INT_ERR; |
glebiuskv | 0:2f0e1e23c242 | 4896 | if (dcl == 0xFFFFFFFF) res = FR_DISK_ERR; |
glebiuskv | 0:2f0e1e23c242 | 4897 | if (res == FR_OK) res = sync_window(fs); /* Flush FAT */ |
glebiuskv | 0:2f0e1e23c242 | 4898 | tm = GET_FATTIME(); |
glebiuskv | 0:2f0e1e23c242 | 4899 | if (res == FR_OK) { /* Initialize the new directory table */ |
glebiuskv | 0:2f0e1e23c242 | 4900 | res = dir_clear(fs, dcl); /* Clean up the new table */ |
glebiuskv | 0:2f0e1e23c242 | 4901 | if (res == FR_OK && (!FF_FS_EXFAT || fs->fs_type != FS_EXFAT)) { /* Create dot entries (FAT only) */ |
glebiuskv | 0:2f0e1e23c242 | 4902 | dir = fs->win; |
glebiuskv | 0:2f0e1e23c242 | 4903 | mem_set(dir + DIR_Name, ' ', 11); /* Create "." entry */ |
glebiuskv | 0:2f0e1e23c242 | 4904 | dir[DIR_Name] = '.'; |
glebiuskv | 0:2f0e1e23c242 | 4905 | dir[DIR_Attr] = AM_DIR; |
glebiuskv | 0:2f0e1e23c242 | 4906 | st_dword(dir + DIR_ModTime, tm); |
glebiuskv | 0:2f0e1e23c242 | 4907 | st_clust(fs, dir, dcl); |
glebiuskv | 0:2f0e1e23c242 | 4908 | mem_cpy(dir + SZDIRE, dir, SZDIRE); /* Create ".." entry */ |
glebiuskv | 0:2f0e1e23c242 | 4909 | dir[SZDIRE + 1] = '.'; pcl = dj.obj.sclust; |
glebiuskv | 0:2f0e1e23c242 | 4910 | st_clust(fs, dir + SZDIRE, pcl); |
glebiuskv | 0:2f0e1e23c242 | 4911 | fs->wflag = 1; |
glebiuskv | 0:2f0e1e23c242 | 4912 | } |
glebiuskv | 0:2f0e1e23c242 | 4913 | } |
glebiuskv | 0:2f0e1e23c242 | 4914 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 4915 | res = dir_register(&dj); /* Register the object to the directoy */ |
glebiuskv | 0:2f0e1e23c242 | 4916 | } |
glebiuskv | 0:2f0e1e23c242 | 4917 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 4918 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 4919 | if (fs->fs_type == FS_EXFAT) { /* Initialize directory entry block */ |
glebiuskv | 0:2f0e1e23c242 | 4920 | st_dword(fs->dirbuf + XDIR_ModTime, tm); /* Created time */ |
glebiuskv | 0:2f0e1e23c242 | 4921 | st_dword(fs->dirbuf + XDIR_FstClus, dcl); /* Table start cluster */ |
glebiuskv | 0:2f0e1e23c242 | 4922 | st_dword(fs->dirbuf + XDIR_FileSize, (DWORD)dj.obj.objsize); /* File size needs to be valid */ |
glebiuskv | 0:2f0e1e23c242 | 4923 | st_dword(fs->dirbuf + XDIR_ValidFileSize, (DWORD)dj.obj.objsize); |
glebiuskv | 0:2f0e1e23c242 | 4924 | fs->dirbuf[XDIR_GenFlags] = 3; /* Initialize the object flag */ |
glebiuskv | 0:2f0e1e23c242 | 4925 | fs->dirbuf[XDIR_Attr] = AM_DIR; /* Attribute */ |
glebiuskv | 0:2f0e1e23c242 | 4926 | res = store_xdir(&dj); |
glebiuskv | 0:2f0e1e23c242 | 4927 | } else |
glebiuskv | 0:2f0e1e23c242 | 4928 | #endif |
glebiuskv | 0:2f0e1e23c242 | 4929 | { |
glebiuskv | 0:2f0e1e23c242 | 4930 | dir = dj.dir; |
glebiuskv | 0:2f0e1e23c242 | 4931 | st_dword(dir + DIR_ModTime, tm); /* Created time */ |
glebiuskv | 0:2f0e1e23c242 | 4932 | st_clust(fs, dir, dcl); /* Table start cluster */ |
glebiuskv | 0:2f0e1e23c242 | 4933 | dir[DIR_Attr] = AM_DIR; /* Attribute */ |
glebiuskv | 0:2f0e1e23c242 | 4934 | fs->wflag = 1; |
glebiuskv | 0:2f0e1e23c242 | 4935 | } |
glebiuskv | 0:2f0e1e23c242 | 4936 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 4937 | res = sync_fs(fs); |
glebiuskv | 0:2f0e1e23c242 | 4938 | } |
glebiuskv | 0:2f0e1e23c242 | 4939 | } else { |
glebiuskv | 0:2f0e1e23c242 | 4940 | remove_chain(&dj.obj, dcl, 0); /* Could not register, remove cluster chain */ |
glebiuskv | 0:2f0e1e23c242 | 4941 | } |
glebiuskv | 0:2f0e1e23c242 | 4942 | } |
glebiuskv | 0:2f0e1e23c242 | 4943 | FREE_NAMBUF(); |
glebiuskv | 0:2f0e1e23c242 | 4944 | } |
glebiuskv | 0:2f0e1e23c242 | 4945 | |
glebiuskv | 0:2f0e1e23c242 | 4946 | LEAVE_FF(fs, res); |
glebiuskv | 0:2f0e1e23c242 | 4947 | } |
glebiuskv | 0:2f0e1e23c242 | 4948 | |
glebiuskv | 0:2f0e1e23c242 | 4949 | |
glebiuskv | 0:2f0e1e23c242 | 4950 | |
glebiuskv | 0:2f0e1e23c242 | 4951 | |
glebiuskv | 0:2f0e1e23c242 | 4952 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 4953 | /* Rename a File/Directory */ |
glebiuskv | 0:2f0e1e23c242 | 4954 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 4955 | |
glebiuskv | 0:2f0e1e23c242 | 4956 | FRESULT f_rename ( |
glebiuskv | 0:2f0e1e23c242 | 4957 | const TCHAR* path_old, /* Pointer to the object name to be renamed */ |
glebiuskv | 0:2f0e1e23c242 | 4958 | const TCHAR* path_new /* Pointer to the new name */ |
glebiuskv | 0:2f0e1e23c242 | 4959 | ) |
glebiuskv | 0:2f0e1e23c242 | 4960 | { |
glebiuskv | 0:2f0e1e23c242 | 4961 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 4962 | FATFS_DIR djo, djn; |
glebiuskv | 0:2f0e1e23c242 | 4963 | FATFS *fs; |
glebiuskv | 0:2f0e1e23c242 | 4964 | BYTE buf[FF_FS_EXFAT ? SZDIRE * 2 : SZDIRE], *dir; |
glebiuskv | 0:2f0e1e23c242 | 4965 | DWORD dw; |
glebiuskv | 0:2f0e1e23c242 | 4966 | DEF_NAMBUF |
glebiuskv | 0:2f0e1e23c242 | 4967 | |
glebiuskv | 0:2f0e1e23c242 | 4968 | |
glebiuskv | 0:2f0e1e23c242 | 4969 | get_ldnumber(&path_new); /* Snip the drive number of new name off */ |
glebiuskv | 0:2f0e1e23c242 | 4970 | res = find_volume(&path_old, &fs, FA_WRITE); /* Get logical drive of the old object */ |
glebiuskv | 0:2f0e1e23c242 | 4971 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 4972 | djo.obj.fs = fs; |
glebiuskv | 0:2f0e1e23c242 | 4973 | INIT_NAMBUF(fs); |
glebiuskv | 0:2f0e1e23c242 | 4974 | res = follow_path(&djo, path_old); /* Check old object */ |
glebiuskv | 0:2f0e1e23c242 | 4975 | if (res == FR_OK && (djo.fn[NSFLAG] & (NS_DOT | NS_NONAME))) res = FR_INVALID_NAME; /* Check validity of name */ |
glebiuskv | 0:2f0e1e23c242 | 4976 | #if FF_FS_LOCK != 0 |
glebiuskv | 0:2f0e1e23c242 | 4977 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 4978 | res = chk_lock(&djo, 2); |
glebiuskv | 0:2f0e1e23c242 | 4979 | } |
glebiuskv | 0:2f0e1e23c242 | 4980 | #endif |
glebiuskv | 0:2f0e1e23c242 | 4981 | if (res == FR_OK) { /* Object to be renamed is found */ |
glebiuskv | 0:2f0e1e23c242 | 4982 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 4983 | if (fs->fs_type == FS_EXFAT) { /* At exFAT volume */ |
glebiuskv | 0:2f0e1e23c242 | 4984 | BYTE nf, nn; |
glebiuskv | 0:2f0e1e23c242 | 4985 | WORD nh; |
glebiuskv | 0:2f0e1e23c242 | 4986 | |
glebiuskv | 0:2f0e1e23c242 | 4987 | mem_cpy(buf, fs->dirbuf, SZDIRE * 2); /* Save 85+C0 entry of old object */ |
glebiuskv | 0:2f0e1e23c242 | 4988 | mem_cpy(&djn, &djo, sizeof djo); |
glebiuskv | 0:2f0e1e23c242 | 4989 | res = follow_path(&djn, path_new); /* Make sure if new object name is not in use */ |
glebiuskv | 0:2f0e1e23c242 | 4990 | if (res == FR_OK) { /* Is new name already in use by any other object? */ |
glebiuskv | 0:2f0e1e23c242 | 4991 | res = (djn.obj.sclust == djo.obj.sclust && djn.dptr == djo.dptr) ? FR_NO_FILE : FR_EXIST; |
glebiuskv | 0:2f0e1e23c242 | 4992 | } |
glebiuskv | 0:2f0e1e23c242 | 4993 | if (res == FR_NO_FILE) { /* It is a valid path and no name collision */ |
glebiuskv | 0:2f0e1e23c242 | 4994 | res = dir_register(&djn); /* Register the new entry */ |
glebiuskv | 0:2f0e1e23c242 | 4995 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 4996 | nf = fs->dirbuf[XDIR_NumSec]; nn = fs->dirbuf[XDIR_NumName]; |
glebiuskv | 0:2f0e1e23c242 | 4997 | nh = ld_word(fs->dirbuf + XDIR_NameHash); |
glebiuskv | 0:2f0e1e23c242 | 4998 | mem_cpy(fs->dirbuf, buf, SZDIRE * 2); /* Restore 85+C0 entry */ |
glebiuskv | 0:2f0e1e23c242 | 4999 | fs->dirbuf[XDIR_NumSec] = nf; fs->dirbuf[XDIR_NumName] = nn; |
glebiuskv | 0:2f0e1e23c242 | 5000 | st_word(fs->dirbuf + XDIR_NameHash, nh); |
glebiuskv | 0:2f0e1e23c242 | 5001 | if (!(fs->dirbuf[XDIR_Attr] & AM_DIR)) fs->dirbuf[XDIR_Attr] |= AM_ARC; /* Set archive attribute if it is a file */ |
glebiuskv | 0:2f0e1e23c242 | 5002 | /* Start of critical section where an interruption can cause a cross-link */ |
glebiuskv | 0:2f0e1e23c242 | 5003 | res = store_xdir(&djn); |
glebiuskv | 0:2f0e1e23c242 | 5004 | } |
glebiuskv | 0:2f0e1e23c242 | 5005 | } |
glebiuskv | 0:2f0e1e23c242 | 5006 | } else |
glebiuskv | 0:2f0e1e23c242 | 5007 | #endif |
glebiuskv | 0:2f0e1e23c242 | 5008 | { /* At FAT/FAT32 volume */ |
glebiuskv | 0:2f0e1e23c242 | 5009 | mem_cpy(buf, djo.dir, SZDIRE); /* Save directory entry of the object */ |
glebiuskv | 0:2f0e1e23c242 | 5010 | mem_cpy(&djn, &djo, sizeof (FATFS_DIR)); /* Duplicate the directory object */ |
glebiuskv | 0:2f0e1e23c242 | 5011 | res = follow_path(&djn, path_new); /* Make sure if new object name is not in use */ |
glebiuskv | 0:2f0e1e23c242 | 5012 | if (res == FR_OK) { /* Is new name already in use by any other object? */ |
glebiuskv | 0:2f0e1e23c242 | 5013 | res = (djn.obj.sclust == djo.obj.sclust && djn.dptr == djo.dptr) ? FR_NO_FILE : FR_EXIST; |
glebiuskv | 0:2f0e1e23c242 | 5014 | } |
glebiuskv | 0:2f0e1e23c242 | 5015 | if (res == FR_NO_FILE) { /* It is a valid path and no name collision */ |
glebiuskv | 0:2f0e1e23c242 | 5016 | res = dir_register(&djn); /* Register the new entry */ |
glebiuskv | 0:2f0e1e23c242 | 5017 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 5018 | dir = djn.dir; /* Copy directory entry of the object except name */ |
glebiuskv | 0:2f0e1e23c242 | 5019 | mem_cpy(dir + 13, buf + 13, SZDIRE - 13); |
glebiuskv | 0:2f0e1e23c242 | 5020 | dir[DIR_Attr] = buf[DIR_Attr]; |
glebiuskv | 0:2f0e1e23c242 | 5021 | if (!(dir[DIR_Attr] & AM_DIR)) dir[DIR_Attr] |= AM_ARC; /* Set archive attribute if it is a file */ |
glebiuskv | 0:2f0e1e23c242 | 5022 | fs->wflag = 1; |
glebiuskv | 0:2f0e1e23c242 | 5023 | if ((dir[DIR_Attr] & AM_DIR) && djo.obj.sclust != djn.obj.sclust) { /* Update .. entry in the sub-directory if needed */ |
glebiuskv | 0:2f0e1e23c242 | 5024 | dw = clst2sect(fs, ld_clust(fs, dir)); |
glebiuskv | 0:2f0e1e23c242 | 5025 | if (dw == 0) { |
glebiuskv | 0:2f0e1e23c242 | 5026 | res = FR_INT_ERR; |
glebiuskv | 0:2f0e1e23c242 | 5027 | } else { |
glebiuskv | 0:2f0e1e23c242 | 5028 | /* Start of critical section where an interruption can cause a cross-link */ |
glebiuskv | 0:2f0e1e23c242 | 5029 | res = move_window(fs, dw); |
glebiuskv | 0:2f0e1e23c242 | 5030 | dir = fs->win + SZDIRE * 1; /* Ptr to .. entry */ |
glebiuskv | 0:2f0e1e23c242 | 5031 | if (res == FR_OK && dir[1] == '.') { |
glebiuskv | 0:2f0e1e23c242 | 5032 | st_clust(fs, dir, djn.obj.sclust); |
glebiuskv | 0:2f0e1e23c242 | 5033 | fs->wflag = 1; |
glebiuskv | 0:2f0e1e23c242 | 5034 | } |
glebiuskv | 0:2f0e1e23c242 | 5035 | } |
glebiuskv | 0:2f0e1e23c242 | 5036 | } |
glebiuskv | 0:2f0e1e23c242 | 5037 | } |
glebiuskv | 0:2f0e1e23c242 | 5038 | } |
glebiuskv | 0:2f0e1e23c242 | 5039 | } |
glebiuskv | 0:2f0e1e23c242 | 5040 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 5041 | res = dir_remove(&djo); /* Remove old entry */ |
glebiuskv | 0:2f0e1e23c242 | 5042 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 5043 | res = sync_fs(fs); |
glebiuskv | 0:2f0e1e23c242 | 5044 | } |
glebiuskv | 0:2f0e1e23c242 | 5045 | } |
glebiuskv | 0:2f0e1e23c242 | 5046 | /* End of the critical section */ |
glebiuskv | 0:2f0e1e23c242 | 5047 | } |
glebiuskv | 0:2f0e1e23c242 | 5048 | FREE_NAMBUF(); |
glebiuskv | 0:2f0e1e23c242 | 5049 | } |
glebiuskv | 0:2f0e1e23c242 | 5050 | |
glebiuskv | 0:2f0e1e23c242 | 5051 | LEAVE_FF(fs, res); |
glebiuskv | 0:2f0e1e23c242 | 5052 | } |
glebiuskv | 0:2f0e1e23c242 | 5053 | |
glebiuskv | 0:2f0e1e23c242 | 5054 | #endif /* !FF_FS_READONLY */ |
glebiuskv | 0:2f0e1e23c242 | 5055 | #endif /* FF_FS_MINIMIZE == 0 */ |
glebiuskv | 0:2f0e1e23c242 | 5056 | #endif /* FF_FS_MINIMIZE <= 1 */ |
glebiuskv | 0:2f0e1e23c242 | 5057 | #endif /* FF_FS_MINIMIZE <= 2 */ |
glebiuskv | 0:2f0e1e23c242 | 5058 | |
glebiuskv | 0:2f0e1e23c242 | 5059 | |
glebiuskv | 0:2f0e1e23c242 | 5060 | |
glebiuskv | 0:2f0e1e23c242 | 5061 | #if FF_USE_CHMOD && !FF_FS_READONLY |
glebiuskv | 0:2f0e1e23c242 | 5062 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 5063 | /* Change Attribute */ |
glebiuskv | 0:2f0e1e23c242 | 5064 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 5065 | |
glebiuskv | 0:2f0e1e23c242 | 5066 | FRESULT f_chmod ( |
glebiuskv | 0:2f0e1e23c242 | 5067 | const TCHAR* path, /* Pointer to the file path */ |
glebiuskv | 0:2f0e1e23c242 | 5068 | BYTE attr, /* Attribute bits */ |
glebiuskv | 0:2f0e1e23c242 | 5069 | BYTE mask /* Attribute mask to change */ |
glebiuskv | 0:2f0e1e23c242 | 5070 | ) |
glebiuskv | 0:2f0e1e23c242 | 5071 | { |
glebiuskv | 0:2f0e1e23c242 | 5072 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 5073 | FATFS_DIR dj; |
glebiuskv | 0:2f0e1e23c242 | 5074 | FATFS *fs; |
glebiuskv | 0:2f0e1e23c242 | 5075 | DEF_NAMBUF |
glebiuskv | 0:2f0e1e23c242 | 5076 | |
glebiuskv | 0:2f0e1e23c242 | 5077 | |
glebiuskv | 0:2f0e1e23c242 | 5078 | res = find_volume(&path, &fs, FA_WRITE); /* Get logical drive */ |
glebiuskv | 0:2f0e1e23c242 | 5079 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 5080 | dj.obj.fs = fs; |
glebiuskv | 0:2f0e1e23c242 | 5081 | INIT_NAMBUF(fs); |
glebiuskv | 0:2f0e1e23c242 | 5082 | res = follow_path(&dj, path); /* Follow the file path */ |
glebiuskv | 0:2f0e1e23c242 | 5083 | if (res == FR_OK && (dj.fn[NSFLAG] & (NS_DOT | NS_NONAME))) res = FR_INVALID_NAME; /* Check object validity */ |
glebiuskv | 0:2f0e1e23c242 | 5084 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 5085 | mask &= AM_RDO|AM_HID|AM_SYS|AM_ARC; /* Valid attribute mask */ |
glebiuskv | 0:2f0e1e23c242 | 5086 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 5087 | if (fs->fs_type == FS_EXFAT) { |
glebiuskv | 0:2f0e1e23c242 | 5088 | fs->dirbuf[XDIR_Attr] = (attr & mask) | (fs->dirbuf[XDIR_Attr] & (BYTE)~mask); /* Apply attribute change */ |
glebiuskv | 0:2f0e1e23c242 | 5089 | res = store_xdir(&dj); |
glebiuskv | 0:2f0e1e23c242 | 5090 | } else |
glebiuskv | 0:2f0e1e23c242 | 5091 | #endif |
glebiuskv | 0:2f0e1e23c242 | 5092 | { |
glebiuskv | 0:2f0e1e23c242 | 5093 | dj.dir[DIR_Attr] = (attr & mask) | (dj.dir[DIR_Attr] & (BYTE)~mask); /* Apply attribute change */ |
glebiuskv | 0:2f0e1e23c242 | 5094 | fs->wflag = 1; |
glebiuskv | 0:2f0e1e23c242 | 5095 | } |
glebiuskv | 0:2f0e1e23c242 | 5096 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 5097 | res = sync_fs(fs); |
glebiuskv | 0:2f0e1e23c242 | 5098 | } |
glebiuskv | 0:2f0e1e23c242 | 5099 | } |
glebiuskv | 0:2f0e1e23c242 | 5100 | FREE_NAMBUF(); |
glebiuskv | 0:2f0e1e23c242 | 5101 | } |
glebiuskv | 0:2f0e1e23c242 | 5102 | |
glebiuskv | 0:2f0e1e23c242 | 5103 | LEAVE_FF(fs, res); |
glebiuskv | 0:2f0e1e23c242 | 5104 | } |
glebiuskv | 0:2f0e1e23c242 | 5105 | |
glebiuskv | 0:2f0e1e23c242 | 5106 | |
glebiuskv | 0:2f0e1e23c242 | 5107 | |
glebiuskv | 0:2f0e1e23c242 | 5108 | |
glebiuskv | 0:2f0e1e23c242 | 5109 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 5110 | /* Change Timestamp */ |
glebiuskv | 0:2f0e1e23c242 | 5111 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 5112 | |
glebiuskv | 0:2f0e1e23c242 | 5113 | FRESULT f_utime ( |
glebiuskv | 0:2f0e1e23c242 | 5114 | const TCHAR* path, /* Pointer to the file/directory name */ |
glebiuskv | 0:2f0e1e23c242 | 5115 | const FILINFO* fno /* Pointer to the timestamp to be set */ |
glebiuskv | 0:2f0e1e23c242 | 5116 | ) |
glebiuskv | 0:2f0e1e23c242 | 5117 | { |
glebiuskv | 0:2f0e1e23c242 | 5118 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 5119 | FATFS_DIR dj; |
glebiuskv | 0:2f0e1e23c242 | 5120 | FATFS *fs; |
glebiuskv | 0:2f0e1e23c242 | 5121 | DEF_NAMBUF |
glebiuskv | 0:2f0e1e23c242 | 5122 | |
glebiuskv | 0:2f0e1e23c242 | 5123 | |
glebiuskv | 0:2f0e1e23c242 | 5124 | res = find_volume(&path, &fs, FA_WRITE); /* Get logical drive */ |
glebiuskv | 0:2f0e1e23c242 | 5125 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 5126 | dj.obj.fs = fs; |
glebiuskv | 0:2f0e1e23c242 | 5127 | INIT_NAMBUF(fs); |
glebiuskv | 0:2f0e1e23c242 | 5128 | res = follow_path(&dj, path); /* Follow the file path */ |
glebiuskv | 0:2f0e1e23c242 | 5129 | if (res == FR_OK && (dj.fn[NSFLAG] & (NS_DOT | NS_NONAME))) res = FR_INVALID_NAME; /* Check object validity */ |
glebiuskv | 0:2f0e1e23c242 | 5130 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 5131 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 5132 | if (fs->fs_type == FS_EXFAT) { |
glebiuskv | 0:2f0e1e23c242 | 5133 | st_dword(fs->dirbuf + XDIR_ModTime, (DWORD)fno->fdate << 16 | fno->ftime); |
glebiuskv | 0:2f0e1e23c242 | 5134 | res = store_xdir(&dj); |
glebiuskv | 0:2f0e1e23c242 | 5135 | } else |
glebiuskv | 0:2f0e1e23c242 | 5136 | #endif |
glebiuskv | 0:2f0e1e23c242 | 5137 | { |
glebiuskv | 0:2f0e1e23c242 | 5138 | st_dword(dj.dir + DIR_ModTime, (DWORD)fno->fdate << 16 | fno->ftime); |
glebiuskv | 0:2f0e1e23c242 | 5139 | fs->wflag = 1; |
glebiuskv | 0:2f0e1e23c242 | 5140 | } |
glebiuskv | 0:2f0e1e23c242 | 5141 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 5142 | res = sync_fs(fs); |
glebiuskv | 0:2f0e1e23c242 | 5143 | } |
glebiuskv | 0:2f0e1e23c242 | 5144 | } |
glebiuskv | 0:2f0e1e23c242 | 5145 | FREE_NAMBUF(); |
glebiuskv | 0:2f0e1e23c242 | 5146 | } |
glebiuskv | 0:2f0e1e23c242 | 5147 | |
glebiuskv | 0:2f0e1e23c242 | 5148 | LEAVE_FF(fs, res); |
glebiuskv | 0:2f0e1e23c242 | 5149 | } |
glebiuskv | 0:2f0e1e23c242 | 5150 | |
glebiuskv | 0:2f0e1e23c242 | 5151 | #endif /* FF_USE_CHMOD && !FF_FS_READONLY */ |
glebiuskv | 0:2f0e1e23c242 | 5152 | |
glebiuskv | 0:2f0e1e23c242 | 5153 | |
glebiuskv | 0:2f0e1e23c242 | 5154 | |
glebiuskv | 0:2f0e1e23c242 | 5155 | #if FF_USE_LABEL |
glebiuskv | 0:2f0e1e23c242 | 5156 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 5157 | /* Get Volume Label */ |
glebiuskv | 0:2f0e1e23c242 | 5158 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 5159 | |
glebiuskv | 0:2f0e1e23c242 | 5160 | FRESULT f_getlabel ( |
glebiuskv | 0:2f0e1e23c242 | 5161 | const TCHAR* path, /* Logical drive number */ |
glebiuskv | 0:2f0e1e23c242 | 5162 | TCHAR* label, /* Buffer to store the volume label */ |
glebiuskv | 0:2f0e1e23c242 | 5163 | DWORD* vsn /* Variable to store the volume serial number */ |
glebiuskv | 0:2f0e1e23c242 | 5164 | ) |
glebiuskv | 0:2f0e1e23c242 | 5165 | { |
glebiuskv | 0:2f0e1e23c242 | 5166 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 5167 | FATFS_DIR dj; |
glebiuskv | 0:2f0e1e23c242 | 5168 | FATFS *fs; |
glebiuskv | 0:2f0e1e23c242 | 5169 | UINT si, di; |
glebiuskv | 0:2f0e1e23c242 | 5170 | WCHAR wc; |
glebiuskv | 0:2f0e1e23c242 | 5171 | |
glebiuskv | 0:2f0e1e23c242 | 5172 | /* Get logical drive */ |
glebiuskv | 0:2f0e1e23c242 | 5173 | res = find_volume(&path, &fs, 0); |
glebiuskv | 0:2f0e1e23c242 | 5174 | |
glebiuskv | 0:2f0e1e23c242 | 5175 | /* Get volume label */ |
glebiuskv | 0:2f0e1e23c242 | 5176 | if (res == FR_OK && label) { |
glebiuskv | 0:2f0e1e23c242 | 5177 | dj.obj.fs = fs; dj.obj.sclust = 0; /* Open root directory */ |
glebiuskv | 0:2f0e1e23c242 | 5178 | res = dir_sdi(&dj, 0); |
glebiuskv | 0:2f0e1e23c242 | 5179 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 5180 | res = dir_read_label(&dj); /* Find a volume label entry */ |
glebiuskv | 0:2f0e1e23c242 | 5181 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 5182 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 5183 | if (fs->fs_type == FS_EXFAT) { |
glebiuskv | 0:2f0e1e23c242 | 5184 | WCHAR hs; |
glebiuskv | 0:2f0e1e23c242 | 5185 | |
glebiuskv | 0:2f0e1e23c242 | 5186 | for (si = di = hs = 0; si < dj.dir[XDIR_NumLabel]; si++) { /* Extract volume label from 83 entry */ |
glebiuskv | 0:2f0e1e23c242 | 5187 | wc = ld_word(dj.dir + XDIR_Label + si * 2); |
glebiuskv | 0:2f0e1e23c242 | 5188 | if (hs == 0 && IsSurrogate(wc)) { /* Is the code a surrogate? */ |
glebiuskv | 0:2f0e1e23c242 | 5189 | hs = wc; continue; |
glebiuskv | 0:2f0e1e23c242 | 5190 | } |
glebiuskv | 0:2f0e1e23c242 | 5191 | wc = put_utf((DWORD)hs << 16 | wc, &label[di], 4); |
glebiuskv | 0:2f0e1e23c242 | 5192 | if (wc == 0) { di = 0; break; } |
glebiuskv | 0:2f0e1e23c242 | 5193 | di += wc; |
glebiuskv | 0:2f0e1e23c242 | 5194 | hs = 0; |
glebiuskv | 0:2f0e1e23c242 | 5195 | } |
glebiuskv | 0:2f0e1e23c242 | 5196 | if (hs != 0) di = 0; /* Broken surrogate pair? */ |
glebiuskv | 0:2f0e1e23c242 | 5197 | label[di] = 0; |
glebiuskv | 0:2f0e1e23c242 | 5198 | } else |
glebiuskv | 0:2f0e1e23c242 | 5199 | #endif |
glebiuskv | 0:2f0e1e23c242 | 5200 | { |
glebiuskv | 0:2f0e1e23c242 | 5201 | si = di = 0; /* Extract volume label from AM_VOL entry */ |
glebiuskv | 0:2f0e1e23c242 | 5202 | while (si < 11) { |
glebiuskv | 0:2f0e1e23c242 | 5203 | wc = dj.dir[si++]; |
glebiuskv | 0:2f0e1e23c242 | 5204 | #if FF_USE_LFN && FF_LFN_UNICODE >= 1 /* Unicode output */ |
glebiuskv | 0:2f0e1e23c242 | 5205 | if (dbc_1st((BYTE)wc) && si < 11) wc = wc << 8 | dj.dir[si++]; /* Is it a DBC? */ |
glebiuskv | 0:2f0e1e23c242 | 5206 | wc = ff_oem2uni(wc, CODEPAGE); |
glebiuskv | 0:2f0e1e23c242 | 5207 | if (wc != 0) wc = put_utf(wc, &label[di], 4); |
glebiuskv | 0:2f0e1e23c242 | 5208 | if (wc == 0) { di = 0; break; } |
glebiuskv | 0:2f0e1e23c242 | 5209 | di += wc; |
glebiuskv | 0:2f0e1e23c242 | 5210 | #else /* ANSI/OEM output */ |
glebiuskv | 0:2f0e1e23c242 | 5211 | label[di++] = (TCHAR)wc; |
glebiuskv | 0:2f0e1e23c242 | 5212 | #endif |
glebiuskv | 0:2f0e1e23c242 | 5213 | } |
glebiuskv | 0:2f0e1e23c242 | 5214 | do { /* Truncate trailing spaces */ |
glebiuskv | 0:2f0e1e23c242 | 5215 | label[di] = 0; |
glebiuskv | 0:2f0e1e23c242 | 5216 | if (di == 0) break; |
glebiuskv | 0:2f0e1e23c242 | 5217 | } while (label[--di] == ' '); |
glebiuskv | 0:2f0e1e23c242 | 5218 | } |
glebiuskv | 0:2f0e1e23c242 | 5219 | } |
glebiuskv | 0:2f0e1e23c242 | 5220 | } |
glebiuskv | 0:2f0e1e23c242 | 5221 | if (res == FR_NO_FILE) { /* No label entry and return nul string */ |
glebiuskv | 0:2f0e1e23c242 | 5222 | label[0] = 0; |
glebiuskv | 0:2f0e1e23c242 | 5223 | res = FR_OK; |
glebiuskv | 0:2f0e1e23c242 | 5224 | } |
glebiuskv | 0:2f0e1e23c242 | 5225 | } |
glebiuskv | 0:2f0e1e23c242 | 5226 | |
glebiuskv | 0:2f0e1e23c242 | 5227 | /* Get volume serial number */ |
glebiuskv | 0:2f0e1e23c242 | 5228 | if (res == FR_OK && vsn) { |
glebiuskv | 0:2f0e1e23c242 | 5229 | res = move_window(fs, fs->volbase); |
glebiuskv | 0:2f0e1e23c242 | 5230 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 5231 | switch (fs->fs_type) { |
glebiuskv | 0:2f0e1e23c242 | 5232 | case FS_EXFAT: |
glebiuskv | 0:2f0e1e23c242 | 5233 | di = BPB_VolIDEx; break; |
glebiuskv | 0:2f0e1e23c242 | 5234 | |
glebiuskv | 0:2f0e1e23c242 | 5235 | case FS_FAT32: |
glebiuskv | 0:2f0e1e23c242 | 5236 | di = BS_VolID32; break; |
glebiuskv | 0:2f0e1e23c242 | 5237 | |
glebiuskv | 0:2f0e1e23c242 | 5238 | default: |
glebiuskv | 0:2f0e1e23c242 | 5239 | di = BS_VolID; |
glebiuskv | 0:2f0e1e23c242 | 5240 | } |
glebiuskv | 0:2f0e1e23c242 | 5241 | *vsn = ld_dword(fs->win + di); |
glebiuskv | 0:2f0e1e23c242 | 5242 | } |
glebiuskv | 0:2f0e1e23c242 | 5243 | } |
glebiuskv | 0:2f0e1e23c242 | 5244 | |
glebiuskv | 0:2f0e1e23c242 | 5245 | LEAVE_FF(fs, res); |
glebiuskv | 0:2f0e1e23c242 | 5246 | } |
glebiuskv | 0:2f0e1e23c242 | 5247 | |
glebiuskv | 0:2f0e1e23c242 | 5248 | |
glebiuskv | 0:2f0e1e23c242 | 5249 | |
glebiuskv | 0:2f0e1e23c242 | 5250 | #if !FF_FS_READONLY |
glebiuskv | 0:2f0e1e23c242 | 5251 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 5252 | /* Set Volume Label */ |
glebiuskv | 0:2f0e1e23c242 | 5253 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 5254 | |
glebiuskv | 0:2f0e1e23c242 | 5255 | FRESULT f_setlabel ( |
glebiuskv | 0:2f0e1e23c242 | 5256 | const TCHAR* label /* Volume label to set with heading logical drive number */ |
glebiuskv | 0:2f0e1e23c242 | 5257 | ) |
glebiuskv | 0:2f0e1e23c242 | 5258 | { |
glebiuskv | 0:2f0e1e23c242 | 5259 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 5260 | FATFS_DIR dj; |
glebiuskv | 0:2f0e1e23c242 | 5261 | FATFS *fs; |
glebiuskv | 0:2f0e1e23c242 | 5262 | BYTE dirvn[22]; |
glebiuskv | 0:2f0e1e23c242 | 5263 | UINT di; |
glebiuskv | 0:2f0e1e23c242 | 5264 | WCHAR wc; |
glebiuskv | 0:2f0e1e23c242 | 5265 | static const char badchr[] = "+.,;=[]\"*:<>\?|\x7F"; /* [0..] for FAT, [7..] for exFAT */ |
glebiuskv | 0:2f0e1e23c242 | 5266 | #if FF_USE_LFN |
glebiuskv | 0:2f0e1e23c242 | 5267 | DWORD dc; |
glebiuskv | 0:2f0e1e23c242 | 5268 | #endif |
glebiuskv | 0:2f0e1e23c242 | 5269 | |
glebiuskv | 0:2f0e1e23c242 | 5270 | /* Get logical drive */ |
glebiuskv | 0:2f0e1e23c242 | 5271 | res = find_volume(&label, &fs, FA_WRITE); |
glebiuskv | 0:2f0e1e23c242 | 5272 | if (res != FR_OK) LEAVE_FF(fs, res); |
glebiuskv | 0:2f0e1e23c242 | 5273 | |
glebiuskv | 0:2f0e1e23c242 | 5274 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 5275 | if (fs->fs_type == FS_EXFAT) { /* On the exFAT volume */ |
glebiuskv | 0:2f0e1e23c242 | 5276 | mem_set(dirvn, 0, 22); |
glebiuskv | 0:2f0e1e23c242 | 5277 | di = 0; |
glebiuskv | 0:2f0e1e23c242 | 5278 | while (*label) { /* Create volume label in directory form */ |
glebiuskv | 0:2f0e1e23c242 | 5279 | dc = tchar2uni(&label); /* Get a Unicode character */ |
glebiuskv | 0:2f0e1e23c242 | 5280 | if (dc >= 0x10000) { |
glebiuskv | 0:2f0e1e23c242 | 5281 | if (dc == 0xFFFFFFFF || di >= 10) { /* Wrong surrogate or buffer overflow */ |
glebiuskv | 0:2f0e1e23c242 | 5282 | dc = 0; |
glebiuskv | 0:2f0e1e23c242 | 5283 | } else { |
glebiuskv | 0:2f0e1e23c242 | 5284 | st_word(dirvn + di * 2, (WCHAR)(dc >> 16)); di++; |
glebiuskv | 0:2f0e1e23c242 | 5285 | } |
glebiuskv | 0:2f0e1e23c242 | 5286 | } |
glebiuskv | 0:2f0e1e23c242 | 5287 | if (dc == 0 || chk_chr(badchr + 7, (int)dc) || di >= 11) { /* Check validity of the volume label */ |
glebiuskv | 0:2f0e1e23c242 | 5288 | LEAVE_FF(fs, FR_INVALID_NAME); |
glebiuskv | 0:2f0e1e23c242 | 5289 | } |
glebiuskv | 0:2f0e1e23c242 | 5290 | st_word(dirvn + di * 2, (WCHAR)dc); di++; |
glebiuskv | 0:2f0e1e23c242 | 5291 | } |
glebiuskv | 0:2f0e1e23c242 | 5292 | } else |
glebiuskv | 0:2f0e1e23c242 | 5293 | #endif |
glebiuskv | 0:2f0e1e23c242 | 5294 | { /* On the FAT/FAT32 volume */ |
glebiuskv | 0:2f0e1e23c242 | 5295 | mem_set(dirvn, ' ', 11); |
glebiuskv | 0:2f0e1e23c242 | 5296 | di = 0; |
glebiuskv | 0:2f0e1e23c242 | 5297 | while (*label) { /* Create volume label in directory form */ |
glebiuskv | 0:2f0e1e23c242 | 5298 | #if FF_USE_LFN |
glebiuskv | 0:2f0e1e23c242 | 5299 | dc = tchar2uni(&label); |
glebiuskv | 0:2f0e1e23c242 | 5300 | wc = (dc < 0x10000) ? ff_uni2oem(ff_wtoupper(dc), CODEPAGE) : 0; |
glebiuskv | 0:2f0e1e23c242 | 5301 | #else /* ANSI/OEM input */ |
glebiuskv | 0:2f0e1e23c242 | 5302 | wc = (BYTE)*label++; |
glebiuskv | 0:2f0e1e23c242 | 5303 | if (dbc_1st((BYTE)wc)) wc = dbc_2nd((BYTE)*label) ? wc << 8 | (BYTE)*label++ : 0; |
glebiuskv | 0:2f0e1e23c242 | 5304 | if (IsLower(wc)) wc -= 0x20; /* To upper ASCII characters */ |
glebiuskv | 0:2f0e1e23c242 | 5305 | #if FF_CODE_PAGE == 0 |
glebiuskv | 0:2f0e1e23c242 | 5306 | if (ExCvt && wc >= 0x80) wc = ExCvt[wc - 0x80]; /* To upper extended characters (SBCS cfg) */ |
glebiuskv | 0:2f0e1e23c242 | 5307 | #elif FF_CODE_PAGE < 900 |
glebiuskv | 0:2f0e1e23c242 | 5308 | if (wc >= 0x80) wc = ExCvt[wc - 0x80]; /* To upper extended characters (SBCS cfg) */ |
glebiuskv | 0:2f0e1e23c242 | 5309 | #endif |
glebiuskv | 0:2f0e1e23c242 | 5310 | #endif |
glebiuskv | 0:2f0e1e23c242 | 5311 | if (wc == 0 || chk_chr(badchr + 0, (int)wc) || di >= (UINT)((wc >= 0x100) ? 10 : 11)) { /* Reject invalid characters for volume label */ |
glebiuskv | 0:2f0e1e23c242 | 5312 | LEAVE_FF(fs, FR_INVALID_NAME); |
glebiuskv | 0:2f0e1e23c242 | 5313 | } |
glebiuskv | 0:2f0e1e23c242 | 5314 | if (wc >= 0x100) dirvn[di++] = (BYTE)(wc >> 8); |
glebiuskv | 0:2f0e1e23c242 | 5315 | dirvn[di++] = (BYTE)wc; |
glebiuskv | 0:2f0e1e23c242 | 5316 | } |
glebiuskv | 0:2f0e1e23c242 | 5317 | if (dirvn[0] == DDEM) LEAVE_FF(fs, FR_INVALID_NAME); /* Reject illegal name (heading DDEM) */ |
glebiuskv | 0:2f0e1e23c242 | 5318 | while (di && dirvn[di - 1] == ' ') di--; /* Snip trailing spaces */ |
glebiuskv | 0:2f0e1e23c242 | 5319 | } |
glebiuskv | 0:2f0e1e23c242 | 5320 | |
glebiuskv | 0:2f0e1e23c242 | 5321 | /* Set volume label */ |
glebiuskv | 0:2f0e1e23c242 | 5322 | dj.obj.fs = fs; dj.obj.sclust = 0; /* Open root directory */ |
glebiuskv | 0:2f0e1e23c242 | 5323 | res = dir_sdi(&dj, 0); |
glebiuskv | 0:2f0e1e23c242 | 5324 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 5325 | res = dir_read_label(&dj); /* Get volume label entry */ |
glebiuskv | 0:2f0e1e23c242 | 5326 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 5327 | if (FF_FS_EXFAT && fs->fs_type == FS_EXFAT) { |
glebiuskv | 0:2f0e1e23c242 | 5328 | dj.dir[XDIR_NumLabel] = (BYTE)di; /* Change the volume label */ |
glebiuskv | 0:2f0e1e23c242 | 5329 | mem_cpy(dj.dir + XDIR_Label, dirvn, 22); |
glebiuskv | 0:2f0e1e23c242 | 5330 | } else { |
glebiuskv | 0:2f0e1e23c242 | 5331 | if (di != 0) { |
glebiuskv | 0:2f0e1e23c242 | 5332 | mem_cpy(dj.dir, dirvn, 11); /* Change the volume label */ |
glebiuskv | 0:2f0e1e23c242 | 5333 | } else { |
glebiuskv | 0:2f0e1e23c242 | 5334 | dj.dir[DIR_Name] = DDEM; /* Remove the volume label */ |
glebiuskv | 0:2f0e1e23c242 | 5335 | } |
glebiuskv | 0:2f0e1e23c242 | 5336 | } |
glebiuskv | 0:2f0e1e23c242 | 5337 | fs->wflag = 1; |
glebiuskv | 0:2f0e1e23c242 | 5338 | res = sync_fs(fs); |
glebiuskv | 0:2f0e1e23c242 | 5339 | } else { /* No volume label entry or an error */ |
glebiuskv | 0:2f0e1e23c242 | 5340 | if (res == FR_NO_FILE) { |
glebiuskv | 0:2f0e1e23c242 | 5341 | res = FR_OK; |
glebiuskv | 0:2f0e1e23c242 | 5342 | if (di != 0) { /* Create a volume label entry */ |
glebiuskv | 0:2f0e1e23c242 | 5343 | res = dir_alloc(&dj, 1); /* Allocate an entry */ |
glebiuskv | 0:2f0e1e23c242 | 5344 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 5345 | mem_set(dj.dir, 0, SZDIRE); /* Clean the entry */ |
glebiuskv | 0:2f0e1e23c242 | 5346 | if (FF_FS_EXFAT && fs->fs_type == FS_EXFAT) { |
glebiuskv | 0:2f0e1e23c242 | 5347 | dj.dir[XDIR_Type] = 0x83; /* Create 83 entry */ |
glebiuskv | 0:2f0e1e23c242 | 5348 | dj.dir[XDIR_NumLabel] = (BYTE)di; |
glebiuskv | 0:2f0e1e23c242 | 5349 | mem_cpy(dj.dir + XDIR_Label, dirvn, 22); |
glebiuskv | 0:2f0e1e23c242 | 5350 | } else { |
glebiuskv | 0:2f0e1e23c242 | 5351 | dj.dir[DIR_Attr] = AM_VOL; /* Create volume label entry */ |
glebiuskv | 0:2f0e1e23c242 | 5352 | mem_cpy(dj.dir, dirvn, 11); |
glebiuskv | 0:2f0e1e23c242 | 5353 | } |
glebiuskv | 0:2f0e1e23c242 | 5354 | fs->wflag = 1; |
glebiuskv | 0:2f0e1e23c242 | 5355 | res = sync_fs(fs); |
glebiuskv | 0:2f0e1e23c242 | 5356 | } |
glebiuskv | 0:2f0e1e23c242 | 5357 | } |
glebiuskv | 0:2f0e1e23c242 | 5358 | } |
glebiuskv | 0:2f0e1e23c242 | 5359 | } |
glebiuskv | 0:2f0e1e23c242 | 5360 | } |
glebiuskv | 0:2f0e1e23c242 | 5361 | |
glebiuskv | 0:2f0e1e23c242 | 5362 | LEAVE_FF(fs, res); |
glebiuskv | 0:2f0e1e23c242 | 5363 | } |
glebiuskv | 0:2f0e1e23c242 | 5364 | |
glebiuskv | 0:2f0e1e23c242 | 5365 | #endif /* !FF_FS_READONLY */ |
glebiuskv | 0:2f0e1e23c242 | 5366 | #endif /* FF_USE_LABEL */ |
glebiuskv | 0:2f0e1e23c242 | 5367 | |
glebiuskv | 0:2f0e1e23c242 | 5368 | |
glebiuskv | 0:2f0e1e23c242 | 5369 | |
glebiuskv | 0:2f0e1e23c242 | 5370 | #if FF_USE_EXPAND && !FF_FS_READONLY |
glebiuskv | 0:2f0e1e23c242 | 5371 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 5372 | /* Allocate a Contiguous Blocks to the File */ |
glebiuskv | 0:2f0e1e23c242 | 5373 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 5374 | |
glebiuskv | 0:2f0e1e23c242 | 5375 | FRESULT f_expand ( |
glebiuskv | 0:2f0e1e23c242 | 5376 | FIL* fp, /* Pointer to the file object */ |
glebiuskv | 0:2f0e1e23c242 | 5377 | FSIZE_t fsz, /* File size to be expanded to */ |
glebiuskv | 0:2f0e1e23c242 | 5378 | BYTE opt /* Operation mode 0:Find and prepare or 1:Find and allocate */ |
glebiuskv | 0:2f0e1e23c242 | 5379 | ) |
glebiuskv | 0:2f0e1e23c242 | 5380 | { |
glebiuskv | 0:2f0e1e23c242 | 5381 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 5382 | FATFS *fs; |
glebiuskv | 0:2f0e1e23c242 | 5383 | DWORD n, clst, stcl, scl, ncl, tcl, lclst; |
glebiuskv | 0:2f0e1e23c242 | 5384 | |
glebiuskv | 0:2f0e1e23c242 | 5385 | |
glebiuskv | 0:2f0e1e23c242 | 5386 | res = validate(&fp->obj, &fs); /* Check validity of the file object */ |
glebiuskv | 0:2f0e1e23c242 | 5387 | if (res != FR_OK || (res = (FRESULT)fp->err) != FR_OK) LEAVE_FF(fs, res); |
glebiuskv | 0:2f0e1e23c242 | 5388 | if (fsz == 0 || fp->obj.objsize != 0 || !(fp->flag & FA_WRITE)) LEAVE_FF(fs, FR_DENIED); |
glebiuskv | 0:2f0e1e23c242 | 5389 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 5390 | if (fs->fs_type != FS_EXFAT && fsz >= 0x100000000) LEAVE_FF(fs, FR_DENIED); /* Check if in size limit */ |
glebiuskv | 0:2f0e1e23c242 | 5391 | #endif |
glebiuskv | 0:2f0e1e23c242 | 5392 | n = (DWORD)fs->csize * SS(fs); /* Cluster size */ |
glebiuskv | 0:2f0e1e23c242 | 5393 | tcl = (DWORD)(fsz / n) + ((fsz & (n - 1)) ? 1 : 0); /* Number of clusters required */ |
glebiuskv | 0:2f0e1e23c242 | 5394 | stcl = fs->last_clst; lclst = 0; |
glebiuskv | 0:2f0e1e23c242 | 5395 | if (stcl < 2 || stcl >= fs->n_fatent) stcl = 2; |
glebiuskv | 0:2f0e1e23c242 | 5396 | |
glebiuskv | 0:2f0e1e23c242 | 5397 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 5398 | if (fs->fs_type == FS_EXFAT) { |
glebiuskv | 0:2f0e1e23c242 | 5399 | scl = find_bitmap(fs, stcl, tcl); /* Find a contiguous cluster block */ |
glebiuskv | 0:2f0e1e23c242 | 5400 | if (scl == 0) res = FR_DENIED; /* No contiguous cluster block was found */ |
glebiuskv | 0:2f0e1e23c242 | 5401 | if (scl == 0xFFFFFFFF) res = FR_DISK_ERR; |
glebiuskv | 0:2f0e1e23c242 | 5402 | if (res == FR_OK) { /* A contiguous free area is found */ |
glebiuskv | 0:2f0e1e23c242 | 5403 | if (opt) { /* Allocate it now */ |
glebiuskv | 0:2f0e1e23c242 | 5404 | res = change_bitmap(fs, scl, tcl, 1); /* Mark the cluster block 'in use' */ |
glebiuskv | 0:2f0e1e23c242 | 5405 | lclst = scl + tcl - 1; |
glebiuskv | 0:2f0e1e23c242 | 5406 | } else { /* Set it as suggested point for next allocation */ |
glebiuskv | 0:2f0e1e23c242 | 5407 | lclst = scl - 1; |
glebiuskv | 0:2f0e1e23c242 | 5408 | } |
glebiuskv | 0:2f0e1e23c242 | 5409 | } |
glebiuskv | 0:2f0e1e23c242 | 5410 | } else |
glebiuskv | 0:2f0e1e23c242 | 5411 | #endif |
glebiuskv | 0:2f0e1e23c242 | 5412 | { |
glebiuskv | 0:2f0e1e23c242 | 5413 | scl = clst = stcl; ncl = 0; |
glebiuskv | 0:2f0e1e23c242 | 5414 | for (;;) { /* Find a contiguous cluster block */ |
glebiuskv | 0:2f0e1e23c242 | 5415 | n = get_fat(&fp->obj, clst); |
glebiuskv | 0:2f0e1e23c242 | 5416 | if (++clst >= fs->n_fatent) clst = 2; |
glebiuskv | 0:2f0e1e23c242 | 5417 | if (n == 1) { res = FR_INT_ERR; break; } |
glebiuskv | 0:2f0e1e23c242 | 5418 | if (n == 0xFFFFFFFF) { res = FR_DISK_ERR; break; } |
glebiuskv | 0:2f0e1e23c242 | 5419 | if (n == 0) { /* Is it a free cluster? */ |
glebiuskv | 0:2f0e1e23c242 | 5420 | if (++ncl == tcl) break; /* Break if a contiguous cluster block is found */ |
glebiuskv | 0:2f0e1e23c242 | 5421 | } else { |
glebiuskv | 0:2f0e1e23c242 | 5422 | scl = clst; ncl = 0; /* Not a free cluster */ |
glebiuskv | 0:2f0e1e23c242 | 5423 | } |
glebiuskv | 0:2f0e1e23c242 | 5424 | if (clst == stcl) { res = FR_DENIED; break; } /* No contiguous cluster? */ |
glebiuskv | 0:2f0e1e23c242 | 5425 | } |
glebiuskv | 0:2f0e1e23c242 | 5426 | if (res == FR_OK) { /* A contiguous free area is found */ |
glebiuskv | 0:2f0e1e23c242 | 5427 | if (opt) { /* Allocate it now */ |
glebiuskv | 0:2f0e1e23c242 | 5428 | for (clst = scl, n = tcl; n; clst++, n--) { /* Create a cluster chain on the FAT */ |
glebiuskv | 0:2f0e1e23c242 | 5429 | res = put_fat(fs, clst, (n == 1) ? 0xFFFFFFFF : clst + 1); |
glebiuskv | 0:2f0e1e23c242 | 5430 | if (res != FR_OK) break; |
glebiuskv | 0:2f0e1e23c242 | 5431 | lclst = clst; |
glebiuskv | 0:2f0e1e23c242 | 5432 | } |
glebiuskv | 0:2f0e1e23c242 | 5433 | } else { /* Set it as suggested point for next allocation */ |
glebiuskv | 0:2f0e1e23c242 | 5434 | lclst = scl - 1; |
glebiuskv | 0:2f0e1e23c242 | 5435 | } |
glebiuskv | 0:2f0e1e23c242 | 5436 | } |
glebiuskv | 0:2f0e1e23c242 | 5437 | } |
glebiuskv | 0:2f0e1e23c242 | 5438 | |
glebiuskv | 0:2f0e1e23c242 | 5439 | if (res == FR_OK) { |
glebiuskv | 0:2f0e1e23c242 | 5440 | fs->last_clst = lclst; /* Set suggested start cluster to start next */ |
glebiuskv | 0:2f0e1e23c242 | 5441 | if (opt) { /* Is it allocated now? */ |
glebiuskv | 0:2f0e1e23c242 | 5442 | fp->obj.sclust = scl; /* Update object allocation information */ |
glebiuskv | 0:2f0e1e23c242 | 5443 | fp->obj.objsize = fsz; |
glebiuskv | 0:2f0e1e23c242 | 5444 | if (FF_FS_EXFAT) fp->obj.stat = 2; /* Set status 'contiguous chain' */ |
glebiuskv | 0:2f0e1e23c242 | 5445 | fp->flag |= FA_MODIFIED; |
glebiuskv | 0:2f0e1e23c242 | 5446 | if (fs->free_clst <= fs->n_fatent - 2) { /* Update FSINFO */ |
glebiuskv | 0:2f0e1e23c242 | 5447 | fs->free_clst -= tcl; |
glebiuskv | 0:2f0e1e23c242 | 5448 | fs->fsi_flag |= 1; |
glebiuskv | 0:2f0e1e23c242 | 5449 | } |
glebiuskv | 0:2f0e1e23c242 | 5450 | } |
glebiuskv | 0:2f0e1e23c242 | 5451 | } |
glebiuskv | 0:2f0e1e23c242 | 5452 | |
glebiuskv | 0:2f0e1e23c242 | 5453 | LEAVE_FF(fs, res); |
glebiuskv | 0:2f0e1e23c242 | 5454 | } |
glebiuskv | 0:2f0e1e23c242 | 5455 | |
glebiuskv | 0:2f0e1e23c242 | 5456 | #endif /* FF_USE_EXPAND && !FF_FS_READONLY */ |
glebiuskv | 0:2f0e1e23c242 | 5457 | |
glebiuskv | 0:2f0e1e23c242 | 5458 | |
glebiuskv | 0:2f0e1e23c242 | 5459 | |
glebiuskv | 0:2f0e1e23c242 | 5460 | #if FF_USE_FORWARD |
glebiuskv | 0:2f0e1e23c242 | 5461 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 5462 | /* Forward Data to the Stream Directly */ |
glebiuskv | 0:2f0e1e23c242 | 5463 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 5464 | |
glebiuskv | 0:2f0e1e23c242 | 5465 | FRESULT f_forward ( |
glebiuskv | 0:2f0e1e23c242 | 5466 | FIL* fp, /* Pointer to the file object */ |
glebiuskv | 0:2f0e1e23c242 | 5467 | UINT (*func)(const BYTE*,UINT), /* Pointer to the streaming function */ |
glebiuskv | 0:2f0e1e23c242 | 5468 | UINT btf, /* Number of bytes to forward */ |
glebiuskv | 0:2f0e1e23c242 | 5469 | UINT* bf /* Pointer to number of bytes forwarded */ |
glebiuskv | 0:2f0e1e23c242 | 5470 | ) |
glebiuskv | 0:2f0e1e23c242 | 5471 | { |
glebiuskv | 0:2f0e1e23c242 | 5472 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 5473 | FATFS *fs; |
glebiuskv | 0:2f0e1e23c242 | 5474 | DWORD clst, sect; |
glebiuskv | 0:2f0e1e23c242 | 5475 | FSIZE_t remain; |
glebiuskv | 0:2f0e1e23c242 | 5476 | UINT rcnt, csect; |
glebiuskv | 0:2f0e1e23c242 | 5477 | BYTE *dbuf; |
glebiuskv | 0:2f0e1e23c242 | 5478 | |
glebiuskv | 0:2f0e1e23c242 | 5479 | |
glebiuskv | 0:2f0e1e23c242 | 5480 | *bf = 0; /* Clear transfer byte counter */ |
glebiuskv | 0:2f0e1e23c242 | 5481 | res = validate(&fp->obj, &fs); /* Check validity of the file object */ |
glebiuskv | 0:2f0e1e23c242 | 5482 | if (res != FR_OK || (res = (FRESULT)fp->err) != FR_OK) LEAVE_FF(fs, res); |
glebiuskv | 0:2f0e1e23c242 | 5483 | if (!(fp->flag & FA_READ)) LEAVE_FF(fs, FR_DENIED); /* Check access mode */ |
glebiuskv | 0:2f0e1e23c242 | 5484 | |
glebiuskv | 0:2f0e1e23c242 | 5485 | remain = fp->obj.objsize - fp->fptr; |
glebiuskv | 0:2f0e1e23c242 | 5486 | if (btf > remain) btf = (UINT)remain; /* Truncate btf by remaining bytes */ |
glebiuskv | 0:2f0e1e23c242 | 5487 | |
glebiuskv | 0:2f0e1e23c242 | 5488 | for ( ; btf && (*func)(0, 0); /* Repeat until all data transferred or stream goes busy */ |
glebiuskv | 0:2f0e1e23c242 | 5489 | fp->fptr += rcnt, *bf += rcnt, btf -= rcnt) { |
glebiuskv | 0:2f0e1e23c242 | 5490 | csect = (UINT)(fp->fptr / SS(fs) & (fs->csize - 1)); /* Sector offset in the cluster */ |
glebiuskv | 0:2f0e1e23c242 | 5491 | if (fp->fptr % SS(fs) == 0) { /* On the sector boundary? */ |
glebiuskv | 0:2f0e1e23c242 | 5492 | if (csect == 0) { /* On the cluster boundary? */ |
glebiuskv | 0:2f0e1e23c242 | 5493 | clst = (fp->fptr == 0) ? /* On the top of the file? */ |
glebiuskv | 0:2f0e1e23c242 | 5494 | fp->obj.sclust : get_fat(&fp->obj, fp->clust); |
glebiuskv | 0:2f0e1e23c242 | 5495 | if (clst <= 1) ABORT(fs, FR_INT_ERR); |
glebiuskv | 0:2f0e1e23c242 | 5496 | if (clst == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR); |
glebiuskv | 0:2f0e1e23c242 | 5497 | fp->clust = clst; /* Update current cluster */ |
glebiuskv | 0:2f0e1e23c242 | 5498 | } |
glebiuskv | 0:2f0e1e23c242 | 5499 | } |
glebiuskv | 0:2f0e1e23c242 | 5500 | sect = clst2sect(fs, fp->clust); /* Get current data sector */ |
glebiuskv | 0:2f0e1e23c242 | 5501 | if (sect == 0) ABORT(fs, FR_INT_ERR); |
glebiuskv | 0:2f0e1e23c242 | 5502 | sect += csect; |
glebiuskv | 0:2f0e1e23c242 | 5503 | #if FF_FS_TINY |
glebiuskv | 0:2f0e1e23c242 | 5504 | if (move_window(fs, sect) != FR_OK) ABORT(fs, FR_DISK_ERR); /* Move sector window to the file data */ |
glebiuskv | 0:2f0e1e23c242 | 5505 | dbuf = fs->win; |
glebiuskv | 0:2f0e1e23c242 | 5506 | #else |
glebiuskv | 0:2f0e1e23c242 | 5507 | if (fp->sect != sect) { /* Fill sector cache with file data */ |
glebiuskv | 0:2f0e1e23c242 | 5508 | #if !FF_FS_READONLY |
glebiuskv | 0:2f0e1e23c242 | 5509 | if (fp->flag & FA_DIRTY) { /* Write-back dirty sector cache */ |
glebiuskv | 0:2f0e1e23c242 | 5510 | if (disk_write(fs->pdrv, fp->buf, fp->sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR); |
glebiuskv | 0:2f0e1e23c242 | 5511 | fp->flag &= (BYTE)~FA_DIRTY; |
glebiuskv | 0:2f0e1e23c242 | 5512 | } |
glebiuskv | 0:2f0e1e23c242 | 5513 | #endif |
glebiuskv | 0:2f0e1e23c242 | 5514 | if (disk_read(fs->pdrv, fp->buf, sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR); |
glebiuskv | 0:2f0e1e23c242 | 5515 | } |
glebiuskv | 0:2f0e1e23c242 | 5516 | dbuf = fp->buf; |
glebiuskv | 0:2f0e1e23c242 | 5517 | #endif |
glebiuskv | 0:2f0e1e23c242 | 5518 | fp->sect = sect; |
glebiuskv | 0:2f0e1e23c242 | 5519 | rcnt = SS(fs) - (UINT)fp->fptr % SS(fs); /* Number of bytes left in the sector */ |
glebiuskv | 0:2f0e1e23c242 | 5520 | if (rcnt > btf) rcnt = btf; /* Clip it by btr if needed */ |
glebiuskv | 0:2f0e1e23c242 | 5521 | rcnt = (*func)(dbuf + ((UINT)fp->fptr % SS(fs)), rcnt); /* Forward the file data */ |
glebiuskv | 0:2f0e1e23c242 | 5522 | if (rcnt == 0) ABORT(fs, FR_INT_ERR); |
glebiuskv | 0:2f0e1e23c242 | 5523 | } |
glebiuskv | 0:2f0e1e23c242 | 5524 | |
glebiuskv | 0:2f0e1e23c242 | 5525 | LEAVE_FF(fs, FR_OK); |
glebiuskv | 0:2f0e1e23c242 | 5526 | } |
glebiuskv | 0:2f0e1e23c242 | 5527 | #endif /* FF_USE_FORWARD */ |
glebiuskv | 0:2f0e1e23c242 | 5528 | |
glebiuskv | 0:2f0e1e23c242 | 5529 | |
glebiuskv | 0:2f0e1e23c242 | 5530 | |
glebiuskv | 0:2f0e1e23c242 | 5531 | #if FF_USE_MKFS && !FF_FS_READONLY |
glebiuskv | 0:2f0e1e23c242 | 5532 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 5533 | /* Create an FAT/exFAT volume */ |
glebiuskv | 0:2f0e1e23c242 | 5534 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 5535 | |
glebiuskv | 0:2f0e1e23c242 | 5536 | FRESULT f_mkfs ( |
glebiuskv | 0:2f0e1e23c242 | 5537 | const TCHAR* path, /* Logical drive number */ |
glebiuskv | 0:2f0e1e23c242 | 5538 | BYTE opt, /* Format option */ |
glebiuskv | 0:2f0e1e23c242 | 5539 | DWORD au, /* Size of allocation unit (cluster) [byte] */ |
glebiuskv | 0:2f0e1e23c242 | 5540 | void* work, /* Pointer to working buffer (null: use heap memory) */ |
glebiuskv | 0:2f0e1e23c242 | 5541 | UINT len /* Size of working buffer [byte] */ |
glebiuskv | 0:2f0e1e23c242 | 5542 | ) |
glebiuskv | 0:2f0e1e23c242 | 5543 | { |
glebiuskv | 0:2f0e1e23c242 | 5544 | const UINT n_fats = 1; /* Number of FATs for FAT/FAT32 volume (1 or 2) */ |
glebiuskv | 0:2f0e1e23c242 | 5545 | const UINT n_rootdir = 512; /* Number of root directory entries for FAT volume */ |
glebiuskv | 0:2f0e1e23c242 | 5546 | static const WORD cst[] = {1, 4, 16, 64, 256, 512, 0}; /* Cluster size boundary for FAT volume (4Ks unit) */ |
glebiuskv | 0:2f0e1e23c242 | 5547 | static const WORD cst32[] = {1, 2, 4, 8, 16, 32, 0}; /* Cluster size boundary for FAT32 volume (128Ks unit) */ |
glebiuskv | 0:2f0e1e23c242 | 5548 | BYTE fmt, sys, *buf, *pte, pdrv, part; |
glebiuskv | 0:2f0e1e23c242 | 5549 | WORD ss; /* Sector size */ |
glebiuskv | 0:2f0e1e23c242 | 5550 | DWORD szb_buf, sz_buf, sz_blk, n_clst, pau, sect, nsect, n; |
glebiuskv | 0:2f0e1e23c242 | 5551 | DWORD b_vol, b_fat, b_data; /* Base LBA for volume, fat, data */ |
glebiuskv | 0:2f0e1e23c242 | 5552 | DWORD sz_vol, sz_rsv, sz_fat, sz_dir; /* Size for volume, fat, dir, data */ |
glebiuskv | 0:2f0e1e23c242 | 5553 | UINT i; |
glebiuskv | 0:2f0e1e23c242 | 5554 | int vol; |
glebiuskv | 0:2f0e1e23c242 | 5555 | DSTATUS stat; |
glebiuskv | 0:2f0e1e23c242 | 5556 | #if FF_USE_TRIM || FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 5557 | DWORD tbl[3]; |
glebiuskv | 0:2f0e1e23c242 | 5558 | #endif |
glebiuskv | 0:2f0e1e23c242 | 5559 | |
glebiuskv | 0:2f0e1e23c242 | 5560 | |
glebiuskv | 0:2f0e1e23c242 | 5561 | /* Check mounted drive and clear work area */ |
glebiuskv | 0:2f0e1e23c242 | 5562 | vol = get_ldnumber(&path); /* Get target logical drive */ |
glebiuskv | 0:2f0e1e23c242 | 5563 | if (vol < 0) return FR_INVALID_DRIVE; |
glebiuskv | 0:2f0e1e23c242 | 5564 | if (FatFs[vol]) FatFs[vol]->fs_type = 0; /* Clear the volume if mounted */ |
glebiuskv | 0:2f0e1e23c242 | 5565 | pdrv = LD2PD(vol); /* Physical drive */ |
glebiuskv | 0:2f0e1e23c242 | 5566 | part = LD2PT(vol); /* Partition (0:create as new, 1-4:get from partition table) */ |
glebiuskv | 0:2f0e1e23c242 | 5567 | |
glebiuskv | 0:2f0e1e23c242 | 5568 | /* Check physical drive status */ |
glebiuskv | 0:2f0e1e23c242 | 5569 | stat = disk_initialize(pdrv); |
glebiuskv | 0:2f0e1e23c242 | 5570 | if (stat & STA_NOINIT) return FR_NOT_READY; |
glebiuskv | 0:2f0e1e23c242 | 5571 | if (stat & STA_PROTECT) return FR_WRITE_PROTECTED; |
glebiuskv | 0:2f0e1e23c242 | 5572 | if (disk_ioctl(pdrv, GET_BLOCK_SIZE, &sz_blk) != RES_OK || !sz_blk || sz_blk > 32768 || (sz_blk & (sz_blk - 1))) sz_blk = 1; /* Erase block to align data area */ |
glebiuskv | 0:2f0e1e23c242 | 5573 | #if FF_MAX_SS != FF_MIN_SS /* Get sector size of the medium if variable sector size cfg. */ |
glebiuskv | 0:2f0e1e23c242 | 5574 | if (disk_ioctl(pdrv, GET_SECTOR_SIZE, &ss) != RES_OK) return FR_DISK_ERR; |
glebiuskv | 0:2f0e1e23c242 | 5575 | if (ss > FF_MAX_SS || ss < FF_MIN_SS || (ss & (ss - 1))) return FR_DISK_ERR; |
glebiuskv | 0:2f0e1e23c242 | 5576 | #else |
glebiuskv | 0:2f0e1e23c242 | 5577 | ss = FF_MAX_SS; |
glebiuskv | 0:2f0e1e23c242 | 5578 | #endif |
glebiuskv | 0:2f0e1e23c242 | 5579 | if ((au != 0 && au < ss) || au > 0x1000000 || (au & (au - 1))) return FR_INVALID_PARAMETER; /* Check if au is valid */ |
glebiuskv | 0:2f0e1e23c242 | 5580 | au /= ss; /* Cluster size in unit of sector */ |
glebiuskv | 0:2f0e1e23c242 | 5581 | |
glebiuskv | 0:2f0e1e23c242 | 5582 | /* Get working buffer */ |
glebiuskv | 0:2f0e1e23c242 | 5583 | #if FF_USE_LFN == 3 || FF_FS_HEAPBUF |
glebiuskv | 0:2f0e1e23c242 | 5584 | if (!work) { /* Use heap memory for working buffer */ |
glebiuskv | 0:2f0e1e23c242 | 5585 | for (szb_buf = MAX_MALLOC, buf = 0; szb_buf >= ss && !(buf = (BYTE *)ff_memalloc(szb_buf)); szb_buf /= 2) ; |
glebiuskv | 0:2f0e1e23c242 | 5586 | sz_buf = szb_buf / ss; /* Size of working buffer (sector) */ |
glebiuskv | 0:2f0e1e23c242 | 5587 | } else |
glebiuskv | 0:2f0e1e23c242 | 5588 | #endif |
glebiuskv | 0:2f0e1e23c242 | 5589 | { |
glebiuskv | 0:2f0e1e23c242 | 5590 | buf = (BYTE*)work; /* Working buffer */ |
glebiuskv | 0:2f0e1e23c242 | 5591 | sz_buf = len / ss; /* Size of working buffer (sector) */ |
glebiuskv | 0:2f0e1e23c242 | 5592 | szb_buf = sz_buf * ss; /* Size of working buffer (byte) */ |
glebiuskv | 0:2f0e1e23c242 | 5593 | } |
glebiuskv | 0:2f0e1e23c242 | 5594 | if (!buf || sz_buf == 0) return FR_NOT_ENOUGH_CORE; |
glebiuskv | 0:2f0e1e23c242 | 5595 | |
glebiuskv | 0:2f0e1e23c242 | 5596 | /* Determine where the volume to be located (b_vol, sz_vol) */ |
glebiuskv | 0:2f0e1e23c242 | 5597 | if (FF_MULTI_PARTITION && part != 0) { |
glebiuskv | 0:2f0e1e23c242 | 5598 | /* Get partition information from partition table in the MBR */ |
glebiuskv | 0:2f0e1e23c242 | 5599 | if (disk_read(pdrv, buf, 0, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); /* Load MBR */ |
glebiuskv | 0:2f0e1e23c242 | 5600 | if (ld_word(buf + BS_55AA) != 0xAA55) LEAVE_MKFS(FR_MKFS_ABORTED); /* Check if MBR is valid */ |
glebiuskv | 0:2f0e1e23c242 | 5601 | pte = buf + (MBR_Table + (part - 1) * SZ_PTE); |
glebiuskv | 0:2f0e1e23c242 | 5602 | if (pte[PTE_System] == 0) LEAVE_MKFS(FR_MKFS_ABORTED); /* No partition? */ |
glebiuskv | 0:2f0e1e23c242 | 5603 | b_vol = ld_dword(pte + PTE_StLba); /* Get volume start sector */ |
glebiuskv | 0:2f0e1e23c242 | 5604 | sz_vol = ld_dword(pte + PTE_SizLba); /* Get volume size */ |
glebiuskv | 0:2f0e1e23c242 | 5605 | } else { |
glebiuskv | 0:2f0e1e23c242 | 5606 | /* Create a single-partition in this function */ |
glebiuskv | 0:2f0e1e23c242 | 5607 | if (disk_ioctl(pdrv, GET_SECTOR_COUNT, &sz_vol) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); |
glebiuskv | 0:2f0e1e23c242 | 5608 | b_vol = (opt & FM_SFD) ? 0 : 63; /* Volume start sector */ |
glebiuskv | 0:2f0e1e23c242 | 5609 | if (sz_vol < b_vol) LEAVE_MKFS(FR_MKFS_ABORTED); |
glebiuskv | 0:2f0e1e23c242 | 5610 | sz_vol -= b_vol; /* Volume size */ |
glebiuskv | 0:2f0e1e23c242 | 5611 | } |
glebiuskv | 0:2f0e1e23c242 | 5612 | if (sz_vol < 128) LEAVE_MKFS(FR_MKFS_ABORTED); /* Check if volume size is >=128s */ |
glebiuskv | 0:2f0e1e23c242 | 5613 | |
glebiuskv | 0:2f0e1e23c242 | 5614 | /* Pre-determine the FAT type */ |
glebiuskv | 0:2f0e1e23c242 | 5615 | do { |
glebiuskv | 0:2f0e1e23c242 | 5616 | if (FF_FS_EXFAT && (opt & FM_EXFAT)) { /* exFAT possible? */ |
glebiuskv | 0:2f0e1e23c242 | 5617 | if ((opt & FM_ANY) == FM_EXFAT || sz_vol >= 0x4000000 || au > 128) { /* exFAT only, vol >= 64Ms or au > 128s ? */ |
glebiuskv | 0:2f0e1e23c242 | 5618 | fmt = FS_EXFAT; break; |
glebiuskv | 0:2f0e1e23c242 | 5619 | } |
glebiuskv | 0:2f0e1e23c242 | 5620 | } |
glebiuskv | 0:2f0e1e23c242 | 5621 | if (au > 128) LEAVE_MKFS(FR_INVALID_PARAMETER); /* Too large au for FAT/FAT32 */ |
glebiuskv | 0:2f0e1e23c242 | 5622 | if (opt & FM_FAT32) { /* FAT32 possible? */ |
glebiuskv | 0:2f0e1e23c242 | 5623 | if ((opt & FM_ANY) == FM_FAT32 || !(opt & FM_FAT)) { /* FAT32 only or no-FAT? */ |
glebiuskv | 0:2f0e1e23c242 | 5624 | fmt = FS_FAT32; break; |
glebiuskv | 0:2f0e1e23c242 | 5625 | } |
glebiuskv | 0:2f0e1e23c242 | 5626 | } |
glebiuskv | 0:2f0e1e23c242 | 5627 | if (!(opt & FM_FAT)) LEAVE_MKFS(FR_INVALID_PARAMETER); /* no-FAT? */ |
glebiuskv | 0:2f0e1e23c242 | 5628 | fmt = FS_FAT16; |
glebiuskv | 0:2f0e1e23c242 | 5629 | } while (0); |
glebiuskv | 0:2f0e1e23c242 | 5630 | |
glebiuskv | 0:2f0e1e23c242 | 5631 | #if FF_FS_EXFAT |
glebiuskv | 0:2f0e1e23c242 | 5632 | if (fmt == FS_EXFAT) { /* Create an exFAT volume */ |
glebiuskv | 0:2f0e1e23c242 | 5633 | DWORD szb_bit, szb_case, sum, nb, cl; |
glebiuskv | 0:2f0e1e23c242 | 5634 | WCHAR ch, si; |
glebiuskv | 0:2f0e1e23c242 | 5635 | UINT j, st; |
glebiuskv | 0:2f0e1e23c242 | 5636 | BYTE b; |
glebiuskv | 0:2f0e1e23c242 | 5637 | |
glebiuskv | 0:2f0e1e23c242 | 5638 | if (sz_vol < 0x1000) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too small volume? */ |
glebiuskv | 0:2f0e1e23c242 | 5639 | #if FF_USE_TRIM |
glebiuskv | 0:2f0e1e23c242 | 5640 | tbl[0] = b_vol; tbl[1] = b_vol + sz_vol - 1; /* Inform the device the volume area may be erased */ |
glebiuskv | 0:2f0e1e23c242 | 5641 | disk_ioctl(pdrv, CTRL_TRIM, tbl); |
glebiuskv | 0:2f0e1e23c242 | 5642 | #endif |
glebiuskv | 0:2f0e1e23c242 | 5643 | /* Determine FAT location, data location and number of clusters */ |
glebiuskv | 0:2f0e1e23c242 | 5644 | if (au == 0) { /* au auto-selection */ |
glebiuskv | 0:2f0e1e23c242 | 5645 | au = 8; |
glebiuskv | 0:2f0e1e23c242 | 5646 | if (sz_vol >= 0x80000) au = 64; /* >= 512Ks */ |
glebiuskv | 0:2f0e1e23c242 | 5647 | if (sz_vol >= 0x4000000) au = 256; /* >= 64Ms */ |
glebiuskv | 0:2f0e1e23c242 | 5648 | } |
glebiuskv | 0:2f0e1e23c242 | 5649 | b_fat = b_vol + 32; /* FAT start at offset 32 */ |
glebiuskv | 0:2f0e1e23c242 | 5650 | sz_fat = ((sz_vol / au + 2) * 4 + ss - 1) / ss; /* Number of FAT sectors */ |
glebiuskv | 0:2f0e1e23c242 | 5651 | b_data = (b_fat + sz_fat + sz_blk - 1) & ~(sz_blk - 1); /* Align data area to the erase block boundary */ |
glebiuskv | 0:2f0e1e23c242 | 5652 | if (b_data >= sz_vol / 2) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too small volume? */ |
glebiuskv | 0:2f0e1e23c242 | 5653 | n_clst = (sz_vol - (b_data - b_vol)) / au; /* Number of clusters */ |
glebiuskv | 0:2f0e1e23c242 | 5654 | if (n_clst <16) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too few clusters? */ |
glebiuskv | 0:2f0e1e23c242 | 5655 | if (n_clst > MAX_EXFAT) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too many clusters? */ |
glebiuskv | 0:2f0e1e23c242 | 5656 | |
glebiuskv | 0:2f0e1e23c242 | 5657 | szb_bit = (n_clst + 7) / 8; /* Size of allocation bitmap */ |
glebiuskv | 0:2f0e1e23c242 | 5658 | tbl[0] = (szb_bit + au * ss - 1) / (au * ss); /* Number of allocation bitmap clusters */ |
glebiuskv | 0:2f0e1e23c242 | 5659 | |
glebiuskv | 0:2f0e1e23c242 | 5660 | /* Create a compressed up-case table */ |
glebiuskv | 0:2f0e1e23c242 | 5661 | sect = b_data + au * tbl[0]; /* Table start sector */ |
glebiuskv | 0:2f0e1e23c242 | 5662 | sum = 0; /* Table checksum to be stored in the 82 entry */ |
glebiuskv | 0:2f0e1e23c242 | 5663 | st = si = i = j = szb_case = 0; |
glebiuskv | 0:2f0e1e23c242 | 5664 | do { |
glebiuskv | 0:2f0e1e23c242 | 5665 | switch (st) { |
glebiuskv | 0:2f0e1e23c242 | 5666 | case 0: |
glebiuskv | 0:2f0e1e23c242 | 5667 | ch = (WCHAR)ff_wtoupper(si); /* Get an up-case char */ |
glebiuskv | 0:2f0e1e23c242 | 5668 | if (ch != si) { |
glebiuskv | 0:2f0e1e23c242 | 5669 | si++; break; /* Store the up-case char if exist */ |
glebiuskv | 0:2f0e1e23c242 | 5670 | } |
glebiuskv | 0:2f0e1e23c242 | 5671 | for (j = 1; (WCHAR)(si + j) && (WCHAR)(si + j) == ff_wtoupper((WCHAR)(si + j)); j++) ; /* Get run length of no-case block */ |
glebiuskv | 0:2f0e1e23c242 | 5672 | if (j >= 128) { |
glebiuskv | 0:2f0e1e23c242 | 5673 | ch = 0xFFFF; st = 2; break; /* Compress the no-case block if run is >= 128 */ |
glebiuskv | 0:2f0e1e23c242 | 5674 | } |
glebiuskv | 0:2f0e1e23c242 | 5675 | st = 1; /* Do not compress short run */ |
glebiuskv | 0:2f0e1e23c242 | 5676 | /* go to next case */ |
glebiuskv | 0:2f0e1e23c242 | 5677 | case 1: |
glebiuskv | 0:2f0e1e23c242 | 5678 | ch = si++; /* Fill the short run */ |
glebiuskv | 0:2f0e1e23c242 | 5679 | if (--j == 0) st = 0; |
glebiuskv | 0:2f0e1e23c242 | 5680 | break; |
glebiuskv | 0:2f0e1e23c242 | 5681 | |
glebiuskv | 0:2f0e1e23c242 | 5682 | default: |
glebiuskv | 0:2f0e1e23c242 | 5683 | ch = (WCHAR)j; si += j; /* Number of chars to skip */ |
glebiuskv | 0:2f0e1e23c242 | 5684 | st = 0; |
glebiuskv | 0:2f0e1e23c242 | 5685 | } |
glebiuskv | 0:2f0e1e23c242 | 5686 | sum = xsum32(buf[i + 0] = (BYTE)ch, sum); /* Put it into the write buffer */ |
glebiuskv | 0:2f0e1e23c242 | 5687 | sum = xsum32(buf[i + 1] = (BYTE)(ch >> 8), sum); |
glebiuskv | 0:2f0e1e23c242 | 5688 | i += 2; szb_case += 2; |
glebiuskv | 0:2f0e1e23c242 | 5689 | if (si == 0 || i == szb_buf) { /* Write buffered data when buffer full or end of process */ |
glebiuskv | 0:2f0e1e23c242 | 5690 | n = (i + ss - 1) / ss; |
glebiuskv | 0:2f0e1e23c242 | 5691 | if (disk_write(pdrv, buf, sect, n) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); |
glebiuskv | 0:2f0e1e23c242 | 5692 | sect += n; i = 0; |
glebiuskv | 0:2f0e1e23c242 | 5693 | } |
glebiuskv | 0:2f0e1e23c242 | 5694 | } while (si); |
glebiuskv | 0:2f0e1e23c242 | 5695 | tbl[1] = (szb_case + au * ss - 1) / (au * ss); /* Number of up-case table clusters */ |
glebiuskv | 0:2f0e1e23c242 | 5696 | tbl[2] = 1; /* Number of root dir clusters */ |
glebiuskv | 0:2f0e1e23c242 | 5697 | |
glebiuskv | 0:2f0e1e23c242 | 5698 | /* Initialize the allocation bitmap */ |
glebiuskv | 0:2f0e1e23c242 | 5699 | sect = b_data; nsect = (szb_bit + ss - 1) / ss; /* Start of bitmap and number of sectors */ |
glebiuskv | 0:2f0e1e23c242 | 5700 | nb = tbl[0] + tbl[1] + tbl[2]; /* Number of clusters in-use by system */ |
glebiuskv | 0:2f0e1e23c242 | 5701 | do { |
glebiuskv | 0:2f0e1e23c242 | 5702 | mem_set(buf, 0, szb_buf); |
glebiuskv | 0:2f0e1e23c242 | 5703 | for (i = 0; nb >= 8 && i < szb_buf; buf[i++] = 0xFF, nb -= 8) ; |
glebiuskv | 0:2f0e1e23c242 | 5704 | for (b = 1; nb != 0 && i < szb_buf; buf[i] |= b, b <<= 1, nb--) ; |
glebiuskv | 0:2f0e1e23c242 | 5705 | n = (nsect > sz_buf) ? sz_buf : nsect; /* Write the buffered data */ |
glebiuskv | 0:2f0e1e23c242 | 5706 | if (disk_write(pdrv, buf, sect, n) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); |
glebiuskv | 0:2f0e1e23c242 | 5707 | sect += n; nsect -= n; |
glebiuskv | 0:2f0e1e23c242 | 5708 | } while (nsect); |
glebiuskv | 0:2f0e1e23c242 | 5709 | |
glebiuskv | 0:2f0e1e23c242 | 5710 | /* Initialize the FAT */ |
glebiuskv | 0:2f0e1e23c242 | 5711 | sect = b_fat; nsect = sz_fat; /* Start of FAT and number of FAT sectors */ |
glebiuskv | 0:2f0e1e23c242 | 5712 | j = nb = cl = 0; |
glebiuskv | 0:2f0e1e23c242 | 5713 | do { |
glebiuskv | 0:2f0e1e23c242 | 5714 | mem_set(buf, 0, szb_buf); i = 0; /* Clear work area and reset write index */ |
glebiuskv | 0:2f0e1e23c242 | 5715 | if (cl == 0) { /* Set entry 0 and 1 */ |
glebiuskv | 0:2f0e1e23c242 | 5716 | st_dword(buf + i, 0xFFFFFFF8); i += 4; cl++; |
glebiuskv | 0:2f0e1e23c242 | 5717 | st_dword(buf + i, 0xFFFFFFFF); i += 4; cl++; |
glebiuskv | 0:2f0e1e23c242 | 5718 | } |
glebiuskv | 0:2f0e1e23c242 | 5719 | do { /* Create chains of bitmap, up-case and root dir */ |
glebiuskv | 0:2f0e1e23c242 | 5720 | while (nb != 0 && i < szb_buf) { /* Create a chain */ |
glebiuskv | 0:2f0e1e23c242 | 5721 | st_dword(buf + i, (nb > 1) ? cl + 1 : 0xFFFFFFFF); |
glebiuskv | 0:2f0e1e23c242 | 5722 | i += 4; cl++; nb--; |
glebiuskv | 0:2f0e1e23c242 | 5723 | } |
glebiuskv | 0:2f0e1e23c242 | 5724 | if (nb == 0 && j < 3) nb = tbl[j++]; /* Next chain */ |
glebiuskv | 0:2f0e1e23c242 | 5725 | } while (nb != 0 && i < szb_buf); |
glebiuskv | 0:2f0e1e23c242 | 5726 | n = (nsect > sz_buf) ? sz_buf : nsect; /* Write the buffered data */ |
glebiuskv | 0:2f0e1e23c242 | 5727 | if (disk_write(pdrv, buf, sect, n) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); |
glebiuskv | 0:2f0e1e23c242 | 5728 | sect += n; nsect -= n; |
glebiuskv | 0:2f0e1e23c242 | 5729 | } while (nsect); |
glebiuskv | 0:2f0e1e23c242 | 5730 | |
glebiuskv | 0:2f0e1e23c242 | 5731 | /* Initialize the root directory */ |
glebiuskv | 0:2f0e1e23c242 | 5732 | mem_set(buf, 0, szb_buf); |
glebiuskv | 0:2f0e1e23c242 | 5733 | buf[SZDIRE * 0 + 0] = 0x83; /* 83 entry (volume label) */ |
glebiuskv | 0:2f0e1e23c242 | 5734 | buf[SZDIRE * 1 + 0] = 0x81; /* 81 entry (allocation bitmap) */ |
glebiuskv | 0:2f0e1e23c242 | 5735 | st_dword(buf + SZDIRE * 1 + 20, 2); |
glebiuskv | 0:2f0e1e23c242 | 5736 | st_dword(buf + SZDIRE * 1 + 24, szb_bit); |
glebiuskv | 0:2f0e1e23c242 | 5737 | buf[SZDIRE * 2 + 0] = 0x82; /* 82 entry (up-case table) */ |
glebiuskv | 0:2f0e1e23c242 | 5738 | st_dword(buf + SZDIRE * 2 + 4, sum); |
glebiuskv | 0:2f0e1e23c242 | 5739 | st_dword(buf + SZDIRE * 2 + 20, 2 + tbl[0]); |
glebiuskv | 0:2f0e1e23c242 | 5740 | st_dword(buf + SZDIRE * 2 + 24, szb_case); |
glebiuskv | 0:2f0e1e23c242 | 5741 | sect = b_data + au * (tbl[0] + tbl[1]); nsect = au; /* Start of the root directory and number of sectors */ |
glebiuskv | 0:2f0e1e23c242 | 5742 | do { /* Fill root directory sectors */ |
glebiuskv | 0:2f0e1e23c242 | 5743 | n = (nsect > sz_buf) ? sz_buf : nsect; |
glebiuskv | 0:2f0e1e23c242 | 5744 | if (disk_write(pdrv, buf, sect, n) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); |
glebiuskv | 0:2f0e1e23c242 | 5745 | mem_set(buf, 0, ss); |
glebiuskv | 0:2f0e1e23c242 | 5746 | sect += n; nsect -= n; |
glebiuskv | 0:2f0e1e23c242 | 5747 | } while (nsect); |
glebiuskv | 0:2f0e1e23c242 | 5748 | |
glebiuskv | 0:2f0e1e23c242 | 5749 | /* Create two set of the exFAT VBR blocks */ |
glebiuskv | 0:2f0e1e23c242 | 5750 | sect = b_vol; |
glebiuskv | 0:2f0e1e23c242 | 5751 | for (n = 0; n < 2; n++) { |
glebiuskv | 0:2f0e1e23c242 | 5752 | /* Main record (+0) */ |
glebiuskv | 0:2f0e1e23c242 | 5753 | mem_set(buf, 0, ss); |
glebiuskv | 0:2f0e1e23c242 | 5754 | mem_cpy(buf + BS_JmpBoot, "\xEB\x76\x90" "EXFAT ", 11); /* Boot jump code (x86), OEM name */ |
glebiuskv | 0:2f0e1e23c242 | 5755 | st_dword(buf + BPB_VolOfsEx, b_vol); /* Volume offset in the physical drive [sector] */ |
glebiuskv | 0:2f0e1e23c242 | 5756 | st_dword(buf + BPB_TotSecEx, sz_vol); /* Volume size [sector] */ |
glebiuskv | 0:2f0e1e23c242 | 5757 | st_dword(buf + BPB_FatOfsEx, b_fat - b_vol); /* FAT offset [sector] */ |
glebiuskv | 0:2f0e1e23c242 | 5758 | st_dword(buf + BPB_FatSzEx, sz_fat); /* FAT size [sector] */ |
glebiuskv | 0:2f0e1e23c242 | 5759 | st_dword(buf + BPB_DataOfsEx, b_data - b_vol); /* Data offset [sector] */ |
glebiuskv | 0:2f0e1e23c242 | 5760 | st_dword(buf + BPB_NumClusEx, n_clst); /* Number of clusters */ |
glebiuskv | 0:2f0e1e23c242 | 5761 | st_dword(buf + BPB_RootClusEx, 2 + tbl[0] + tbl[1]); /* Root dir cluster # */ |
glebiuskv | 0:2f0e1e23c242 | 5762 | st_dword(buf + BPB_VolIDEx, GET_FATTIME()); /* VSN */ |
glebiuskv | 0:2f0e1e23c242 | 5763 | st_word(buf + BPB_FSVerEx, 0x100); /* Filesystem version (1.00) */ |
glebiuskv | 0:2f0e1e23c242 | 5764 | for (buf[BPB_BytsPerSecEx] = 0, i = ss; i >>= 1; buf[BPB_BytsPerSecEx]++) ; /* Log2 of sector size [byte] */ |
glebiuskv | 0:2f0e1e23c242 | 5765 | for (buf[BPB_SecPerClusEx] = 0, i = au; i >>= 1; buf[BPB_SecPerClusEx]++) ; /* Log2 of cluster size [sector] */ |
glebiuskv | 0:2f0e1e23c242 | 5766 | buf[BPB_NumFATsEx] = 1; /* Number of FATs */ |
glebiuskv | 0:2f0e1e23c242 | 5767 | buf[BPB_DrvNumEx] = 0x80; /* Drive number (for int13) */ |
glebiuskv | 0:2f0e1e23c242 | 5768 | st_word(buf + BS_BootCodeEx, 0xFEEB); /* Boot code (x86) */ |
glebiuskv | 0:2f0e1e23c242 | 5769 | st_word(buf + BS_55AA, 0xAA55); /* Signature (placed here regardless of sector size) */ |
glebiuskv | 0:2f0e1e23c242 | 5770 | for (i = sum = 0; i < ss; i++) { /* VBR checksum */ |
glebiuskv | 0:2f0e1e23c242 | 5771 | if (i != BPB_VolFlagEx && i != BPB_VolFlagEx + 1 && i != BPB_PercInUseEx) sum = xsum32(buf[i], sum); |
glebiuskv | 0:2f0e1e23c242 | 5772 | } |
glebiuskv | 0:2f0e1e23c242 | 5773 | if (disk_write(pdrv, buf, sect++, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); |
glebiuskv | 0:2f0e1e23c242 | 5774 | /* Extended bootstrap record (+1..+8) */ |
glebiuskv | 0:2f0e1e23c242 | 5775 | mem_set(buf, 0, ss); |
glebiuskv | 0:2f0e1e23c242 | 5776 | st_word(buf + ss - 2, 0xAA55); /* Signature (placed at end of sector) */ |
glebiuskv | 0:2f0e1e23c242 | 5777 | for (j = 1; j < 9; j++) { |
glebiuskv | 0:2f0e1e23c242 | 5778 | for (i = 0; i < ss; sum = xsum32(buf[i++], sum)) ; /* VBR checksum */ |
glebiuskv | 0:2f0e1e23c242 | 5779 | if (disk_write(pdrv, buf, sect++, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); |
glebiuskv | 0:2f0e1e23c242 | 5780 | } |
glebiuskv | 0:2f0e1e23c242 | 5781 | /* OEM/Reserved record (+9..+10) */ |
glebiuskv | 0:2f0e1e23c242 | 5782 | mem_set(buf, 0, ss); |
glebiuskv | 0:2f0e1e23c242 | 5783 | for ( ; j < 11; j++) { |
glebiuskv | 0:2f0e1e23c242 | 5784 | for (i = 0; i < ss; sum = xsum32(buf[i++], sum)) ; /* VBR checksum */ |
glebiuskv | 0:2f0e1e23c242 | 5785 | if (disk_write(pdrv, buf, sect++, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); |
glebiuskv | 0:2f0e1e23c242 | 5786 | } |
glebiuskv | 0:2f0e1e23c242 | 5787 | /* Sum record (+11) */ |
glebiuskv | 0:2f0e1e23c242 | 5788 | for (i = 0; i < ss; i += 4) st_dword(buf + i, sum); /* Fill with checksum value */ |
glebiuskv | 0:2f0e1e23c242 | 5789 | if (disk_write(pdrv, buf, sect++, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); |
glebiuskv | 0:2f0e1e23c242 | 5790 | } |
glebiuskv | 0:2f0e1e23c242 | 5791 | |
glebiuskv | 0:2f0e1e23c242 | 5792 | } else |
glebiuskv | 0:2f0e1e23c242 | 5793 | #endif /* FF_FS_EXFAT */ |
glebiuskv | 0:2f0e1e23c242 | 5794 | { /* Create an FAT/FAT32 volume */ |
glebiuskv | 0:2f0e1e23c242 | 5795 | do { |
glebiuskv | 0:2f0e1e23c242 | 5796 | pau = au; |
glebiuskv | 0:2f0e1e23c242 | 5797 | /* Pre-determine number of clusters and FAT sub-type */ |
glebiuskv | 0:2f0e1e23c242 | 5798 | if (fmt == FS_FAT32) { /* FAT32 volume */ |
glebiuskv | 0:2f0e1e23c242 | 5799 | if (pau == 0) { /* au auto-selection */ |
glebiuskv | 0:2f0e1e23c242 | 5800 | n = sz_vol / 0x20000; /* Volume size in unit of 128KS */ |
glebiuskv | 0:2f0e1e23c242 | 5801 | for (i = 0, pau = 1; cst32[i] && cst32[i] <= n; i++, pau <<= 1) ; /* Get from table */ |
glebiuskv | 0:2f0e1e23c242 | 5802 | } |
glebiuskv | 0:2f0e1e23c242 | 5803 | n_clst = sz_vol / pau; /* Number of clusters */ |
glebiuskv | 0:2f0e1e23c242 | 5804 | sz_fat = (n_clst * 4 + 8 + ss - 1) / ss; /* FAT size [sector] */ |
glebiuskv | 0:2f0e1e23c242 | 5805 | sz_rsv = 32; /* Number of reserved sectors */ |
glebiuskv | 0:2f0e1e23c242 | 5806 | sz_dir = 0; /* No static directory */ |
glebiuskv | 0:2f0e1e23c242 | 5807 | if (n_clst <= MAX_FAT16 || n_clst > MAX_FAT32) LEAVE_MKFS(FR_MKFS_ABORTED); |
glebiuskv | 0:2f0e1e23c242 | 5808 | } else { /* FAT volume */ |
glebiuskv | 0:2f0e1e23c242 | 5809 | if (pau == 0) { /* au auto-selection */ |
glebiuskv | 0:2f0e1e23c242 | 5810 | n = sz_vol / 0x1000; /* Volume size in unit of 4KS */ |
glebiuskv | 0:2f0e1e23c242 | 5811 | for (i = 0, pau = 1; cst[i] && cst[i] <= n; i++, pau <<= 1) ; /* Get from table */ |
glebiuskv | 0:2f0e1e23c242 | 5812 | } |
glebiuskv | 0:2f0e1e23c242 | 5813 | n_clst = sz_vol / pau; |
glebiuskv | 0:2f0e1e23c242 | 5814 | if (n_clst > MAX_FAT12) { |
glebiuskv | 0:2f0e1e23c242 | 5815 | n = n_clst * 2 + 4; /* FAT size [byte] */ |
glebiuskv | 0:2f0e1e23c242 | 5816 | } else { |
glebiuskv | 0:2f0e1e23c242 | 5817 | fmt = FS_FAT12; |
glebiuskv | 0:2f0e1e23c242 | 5818 | n = (n_clst * 3 + 1) / 2 + 3; /* FAT size [byte] */ |
glebiuskv | 0:2f0e1e23c242 | 5819 | } |
glebiuskv | 0:2f0e1e23c242 | 5820 | sz_fat = (n + ss - 1) / ss; /* FAT size [sector] */ |
glebiuskv | 0:2f0e1e23c242 | 5821 | sz_rsv = 1; /* Number of reserved sectors */ |
glebiuskv | 0:2f0e1e23c242 | 5822 | sz_dir = (DWORD)n_rootdir * SZDIRE / ss; /* Rootdir size [sector] */ |
glebiuskv | 0:2f0e1e23c242 | 5823 | } |
glebiuskv | 0:2f0e1e23c242 | 5824 | b_fat = b_vol + sz_rsv; /* FAT base */ |
glebiuskv | 0:2f0e1e23c242 | 5825 | b_data = b_fat + sz_fat * n_fats + sz_dir; /* Data base */ |
glebiuskv | 0:2f0e1e23c242 | 5826 | |
glebiuskv | 0:2f0e1e23c242 | 5827 | /* Align data base to erase block boundary (for flash memory media) */ |
glebiuskv | 0:2f0e1e23c242 | 5828 | n = ((b_data + sz_blk - 1) & ~(sz_blk - 1)) - b_data; /* Next nearest erase block from current data base */ |
glebiuskv | 0:2f0e1e23c242 | 5829 | if (fmt == FS_FAT32) { /* FAT32: Move FAT base */ |
glebiuskv | 0:2f0e1e23c242 | 5830 | sz_rsv += n; b_fat += n; |
glebiuskv | 0:2f0e1e23c242 | 5831 | } else { /* FAT: Expand FAT size */ |
glebiuskv | 0:2f0e1e23c242 | 5832 | sz_fat += n / n_fats; |
glebiuskv | 0:2f0e1e23c242 | 5833 | } |
glebiuskv | 0:2f0e1e23c242 | 5834 | |
glebiuskv | 0:2f0e1e23c242 | 5835 | /* Determine number of clusters and final check of validity of the FAT sub-type */ |
glebiuskv | 0:2f0e1e23c242 | 5836 | if (sz_vol < b_data + pau * 16 - b_vol) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too small volume */ |
glebiuskv | 0:2f0e1e23c242 | 5837 | n_clst = (sz_vol - sz_rsv - sz_fat * n_fats - sz_dir) / pau; |
glebiuskv | 0:2f0e1e23c242 | 5838 | if (fmt == FS_FAT32) { |
glebiuskv | 0:2f0e1e23c242 | 5839 | if (n_clst <= MAX_FAT16) { /* Too few clusters for FAT32 */ |
glebiuskv | 0:2f0e1e23c242 | 5840 | if (au == 0 && (au = pau / 2) != 0) continue; /* Adjust cluster size and retry */ |
glebiuskv | 0:2f0e1e23c242 | 5841 | LEAVE_MKFS(FR_MKFS_ABORTED); |
glebiuskv | 0:2f0e1e23c242 | 5842 | } |
glebiuskv | 0:2f0e1e23c242 | 5843 | } |
glebiuskv | 0:2f0e1e23c242 | 5844 | if (fmt == FS_FAT16) { |
glebiuskv | 0:2f0e1e23c242 | 5845 | if (n_clst > MAX_FAT16) { /* Too many clusters for FAT16 */ |
glebiuskv | 0:2f0e1e23c242 | 5846 | if (au == 0 && (pau * 2) <= 64) { |
glebiuskv | 0:2f0e1e23c242 | 5847 | au = pau * 2; continue; /* Adjust cluster size and retry */ |
glebiuskv | 0:2f0e1e23c242 | 5848 | } |
glebiuskv | 0:2f0e1e23c242 | 5849 | if ((opt & FM_FAT32)) { |
glebiuskv | 0:2f0e1e23c242 | 5850 | fmt = FS_FAT32; continue; /* Switch type to FAT32 and retry */ |
glebiuskv | 0:2f0e1e23c242 | 5851 | } |
glebiuskv | 0:2f0e1e23c242 | 5852 | if (au == 0 && (au = pau * 2) <= 128) continue; /* Adjust cluster size and retry */ |
glebiuskv | 0:2f0e1e23c242 | 5853 | LEAVE_MKFS(FR_MKFS_ABORTED); |
glebiuskv | 0:2f0e1e23c242 | 5854 | } |
glebiuskv | 0:2f0e1e23c242 | 5855 | if (n_clst <= MAX_FAT12) { /* Too few clusters for FAT16 */ |
glebiuskv | 0:2f0e1e23c242 | 5856 | if (au == 0 && (au = pau * 2) <= 128) continue; /* Adjust cluster size and retry */ |
glebiuskv | 0:2f0e1e23c242 | 5857 | LEAVE_MKFS(FR_MKFS_ABORTED); |
glebiuskv | 0:2f0e1e23c242 | 5858 | } |
glebiuskv | 0:2f0e1e23c242 | 5859 | } |
glebiuskv | 0:2f0e1e23c242 | 5860 | if (fmt == FS_FAT12 && n_clst > MAX_FAT12) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too many clusters for FAT12 */ |
glebiuskv | 0:2f0e1e23c242 | 5861 | |
glebiuskv | 0:2f0e1e23c242 | 5862 | /* Ok, it is the valid cluster configuration */ |
glebiuskv | 0:2f0e1e23c242 | 5863 | break; |
glebiuskv | 0:2f0e1e23c242 | 5864 | } while (1); |
glebiuskv | 0:2f0e1e23c242 | 5865 | |
glebiuskv | 0:2f0e1e23c242 | 5866 | #if FF_USE_TRIM |
glebiuskv | 0:2f0e1e23c242 | 5867 | tbl[0] = b_vol; tbl[1] = b_vol + sz_vol - 1; /* Inform the device the volume area can be erased */ |
glebiuskv | 0:2f0e1e23c242 | 5868 | disk_ioctl(pdrv, CTRL_TRIM, tbl); |
glebiuskv | 0:2f0e1e23c242 | 5869 | #endif |
glebiuskv | 0:2f0e1e23c242 | 5870 | /* Create FAT VBR */ |
glebiuskv | 0:2f0e1e23c242 | 5871 | mem_set(buf, 0, ss); |
glebiuskv | 0:2f0e1e23c242 | 5872 | mem_cpy(buf + BS_JmpBoot, "\xEB\xFE\x90" "MSDOS5.0", 11);/* Boot jump code (x86), OEM name */ |
glebiuskv | 0:2f0e1e23c242 | 5873 | st_word(buf + BPB_BytsPerSec, ss); /* Sector size [byte] */ |
glebiuskv | 0:2f0e1e23c242 | 5874 | buf[BPB_SecPerClus] = (BYTE)pau; /* Cluster size [sector] */ |
glebiuskv | 0:2f0e1e23c242 | 5875 | st_word(buf + BPB_RsvdSecCnt, (WORD)sz_rsv); /* Size of reserved area */ |
glebiuskv | 0:2f0e1e23c242 | 5876 | buf[BPB_NumFATs] = (BYTE)n_fats; /* Number of FATs */ |
glebiuskv | 0:2f0e1e23c242 | 5877 | st_word(buf + BPB_RootEntCnt, (WORD)((fmt == FS_FAT32) ? 0 : n_rootdir)); /* Number of root directory entries */ |
glebiuskv | 0:2f0e1e23c242 | 5878 | if (sz_vol < 0x10000) { |
glebiuskv | 0:2f0e1e23c242 | 5879 | st_word(buf + BPB_TotSec16, (WORD)sz_vol); /* Volume size in 16-bit LBA */ |
glebiuskv | 0:2f0e1e23c242 | 5880 | } else { |
glebiuskv | 0:2f0e1e23c242 | 5881 | st_dword(buf + BPB_TotSec32, sz_vol); /* Volume size in 32-bit LBA */ |
glebiuskv | 0:2f0e1e23c242 | 5882 | } |
glebiuskv | 0:2f0e1e23c242 | 5883 | buf[BPB_Media] = 0xF8; /* Media descriptor byte */ |
glebiuskv | 0:2f0e1e23c242 | 5884 | st_word(buf + BPB_SecPerTrk, 63); /* Number of sectors per track (for int13) */ |
glebiuskv | 0:2f0e1e23c242 | 5885 | st_word(buf + BPB_NumHeads, 255); /* Number of heads (for int13) */ |
glebiuskv | 0:2f0e1e23c242 | 5886 | st_dword(buf + BPB_HiddSec, b_vol); /* Volume offset in the physical drive [sector] */ |
glebiuskv | 0:2f0e1e23c242 | 5887 | if (fmt == FS_FAT32) { |
glebiuskv | 0:2f0e1e23c242 | 5888 | st_dword(buf + BS_VolID32, GET_FATTIME()); /* VSN */ |
glebiuskv | 0:2f0e1e23c242 | 5889 | st_dword(buf + BPB_FATSz32, sz_fat); /* FAT size [sector] */ |
glebiuskv | 0:2f0e1e23c242 | 5890 | st_dword(buf + BPB_RootClus32, 2); /* Root directory cluster # (2) */ |
glebiuskv | 0:2f0e1e23c242 | 5891 | st_word(buf + BPB_FSInfo32, 1); /* Offset of FSINFO sector (VBR + 1) */ |
glebiuskv | 0:2f0e1e23c242 | 5892 | st_word(buf + BPB_BkBootSec32, 6); /* Offset of backup VBR (VBR + 6) */ |
glebiuskv | 0:2f0e1e23c242 | 5893 | buf[BS_DrvNum32] = 0x80; /* Drive number (for int13) */ |
glebiuskv | 0:2f0e1e23c242 | 5894 | buf[BS_BootSig32] = 0x29; /* Extended boot signature */ |
glebiuskv | 0:2f0e1e23c242 | 5895 | mem_cpy(buf + BS_VolLab32, "NO NAME " "FAT32 ", 19); /* Volume label, FAT signature */ |
glebiuskv | 0:2f0e1e23c242 | 5896 | } else { |
glebiuskv | 0:2f0e1e23c242 | 5897 | st_dword(buf + BS_VolID, GET_FATTIME()); /* VSN */ |
glebiuskv | 0:2f0e1e23c242 | 5898 | st_word(buf + BPB_FATSz16, (WORD)sz_fat); /* FAT size [sector] */ |
glebiuskv | 0:2f0e1e23c242 | 5899 | buf[BS_DrvNum] = 0x80; /* Drive number (for int13) */ |
glebiuskv | 0:2f0e1e23c242 | 5900 | buf[BS_BootSig] = 0x29; /* Extended boot signature */ |
glebiuskv | 0:2f0e1e23c242 | 5901 | mem_cpy(buf + BS_VolLab, "NO NAME " "FAT ", 19); /* Volume label, FAT signature */ |
glebiuskv | 0:2f0e1e23c242 | 5902 | } |
glebiuskv | 0:2f0e1e23c242 | 5903 | st_word(buf + BS_55AA, 0xAA55); /* Signature (offset is fixed here regardless of sector size) */ |
glebiuskv | 0:2f0e1e23c242 | 5904 | if (disk_write(pdrv, buf, b_vol, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); /* Write it to the VBR sector */ |
glebiuskv | 0:2f0e1e23c242 | 5905 | |
glebiuskv | 0:2f0e1e23c242 | 5906 | /* Create FSINFO record if needed */ |
glebiuskv | 0:2f0e1e23c242 | 5907 | if (fmt == FS_FAT32) { |
glebiuskv | 0:2f0e1e23c242 | 5908 | disk_write(pdrv, buf, b_vol + 6, 1); /* Write backup VBR (VBR + 6) */ |
glebiuskv | 0:2f0e1e23c242 | 5909 | mem_set(buf, 0, ss); |
glebiuskv | 0:2f0e1e23c242 | 5910 | st_dword(buf + FSI_LeadSig, 0x41615252); |
glebiuskv | 0:2f0e1e23c242 | 5911 | st_dword(buf + FSI_StrucSig, 0x61417272); |
glebiuskv | 0:2f0e1e23c242 | 5912 | st_dword(buf + FSI_Free_Count, n_clst - 1); /* Number of free clusters */ |
glebiuskv | 0:2f0e1e23c242 | 5913 | st_dword(buf + FSI_Nxt_Free, 2); /* Last allocated cluster# */ |
glebiuskv | 0:2f0e1e23c242 | 5914 | st_word(buf + BS_55AA, 0xAA55); |
glebiuskv | 0:2f0e1e23c242 | 5915 | disk_write(pdrv, buf, b_vol + 7, 1); /* Write backup FSINFO (VBR + 7) */ |
glebiuskv | 0:2f0e1e23c242 | 5916 | disk_write(pdrv, buf, b_vol + 1, 1); /* Write original FSINFO (VBR + 1) */ |
glebiuskv | 0:2f0e1e23c242 | 5917 | } |
glebiuskv | 0:2f0e1e23c242 | 5918 | |
glebiuskv | 0:2f0e1e23c242 | 5919 | /* Initialize FAT area */ |
glebiuskv | 0:2f0e1e23c242 | 5920 | mem_set(buf, 0, (UINT)szb_buf); |
glebiuskv | 0:2f0e1e23c242 | 5921 | sect = b_fat; /* FAT start sector */ |
glebiuskv | 0:2f0e1e23c242 | 5922 | for (i = 0; i < n_fats; i++) { /* Initialize FATs each */ |
glebiuskv | 0:2f0e1e23c242 | 5923 | if (fmt == FS_FAT32) { |
glebiuskv | 0:2f0e1e23c242 | 5924 | st_dword(buf + 0, 0xFFFFFFF8); /* Entry 0 */ |
glebiuskv | 0:2f0e1e23c242 | 5925 | st_dword(buf + 4, 0xFFFFFFFF); /* Entry 1 */ |
glebiuskv | 0:2f0e1e23c242 | 5926 | st_dword(buf + 8, 0x0FFFFFFF); /* Entry 2 (root directory) */ |
glebiuskv | 0:2f0e1e23c242 | 5927 | } else { |
glebiuskv | 0:2f0e1e23c242 | 5928 | st_dword(buf + 0, (fmt == FS_FAT12) ? 0xFFFFF8 : 0xFFFFFFF8); /* Entry 0 and 1 */ |
glebiuskv | 0:2f0e1e23c242 | 5929 | } |
glebiuskv | 0:2f0e1e23c242 | 5930 | nsect = sz_fat; /* Number of FAT sectors */ |
glebiuskv | 0:2f0e1e23c242 | 5931 | do { /* Fill FAT sectors */ |
glebiuskv | 0:2f0e1e23c242 | 5932 | n = (nsect > sz_buf) ? sz_buf : nsect; |
glebiuskv | 0:2f0e1e23c242 | 5933 | if (disk_write(pdrv, buf, sect, (UINT)n) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); |
glebiuskv | 0:2f0e1e23c242 | 5934 | mem_set(buf, 0, ss); |
glebiuskv | 0:2f0e1e23c242 | 5935 | sect += n; nsect -= n; |
glebiuskv | 0:2f0e1e23c242 | 5936 | } while (nsect); |
glebiuskv | 0:2f0e1e23c242 | 5937 | } |
glebiuskv | 0:2f0e1e23c242 | 5938 | |
glebiuskv | 0:2f0e1e23c242 | 5939 | /* Initialize root directory (fill with zero) */ |
glebiuskv | 0:2f0e1e23c242 | 5940 | nsect = (fmt == FS_FAT32) ? pau : sz_dir; /* Number of root directory sectors */ |
glebiuskv | 0:2f0e1e23c242 | 5941 | do { |
glebiuskv | 0:2f0e1e23c242 | 5942 | n = (nsect > sz_buf) ? sz_buf : nsect; |
glebiuskv | 0:2f0e1e23c242 | 5943 | if (disk_write(pdrv, buf, sect, (UINT)n) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); |
glebiuskv | 0:2f0e1e23c242 | 5944 | sect += n; nsect -= n; |
glebiuskv | 0:2f0e1e23c242 | 5945 | } while (nsect); |
glebiuskv | 0:2f0e1e23c242 | 5946 | } |
glebiuskv | 0:2f0e1e23c242 | 5947 | |
glebiuskv | 0:2f0e1e23c242 | 5948 | /* Determine system ID in the partition table */ |
glebiuskv | 0:2f0e1e23c242 | 5949 | if (FF_FS_EXFAT && fmt == FS_EXFAT) { |
glebiuskv | 0:2f0e1e23c242 | 5950 | sys = 0x07; /* HPFS/NTFS/exFAT */ |
glebiuskv | 0:2f0e1e23c242 | 5951 | } else { |
glebiuskv | 0:2f0e1e23c242 | 5952 | if (fmt == FS_FAT32) { |
glebiuskv | 0:2f0e1e23c242 | 5953 | sys = 0x0C; /* FAT32X */ |
glebiuskv | 0:2f0e1e23c242 | 5954 | } else { |
glebiuskv | 0:2f0e1e23c242 | 5955 | if (sz_vol >= 0x10000) { |
glebiuskv | 0:2f0e1e23c242 | 5956 | sys = 0x06; /* FAT12/16 (large) */ |
glebiuskv | 0:2f0e1e23c242 | 5957 | } else { |
glebiuskv | 0:2f0e1e23c242 | 5958 | sys = (fmt == FS_FAT16) ? 0x04 : 0x01; /* FAT16 : FAT12 */ |
glebiuskv | 0:2f0e1e23c242 | 5959 | } |
glebiuskv | 0:2f0e1e23c242 | 5960 | } |
glebiuskv | 0:2f0e1e23c242 | 5961 | } |
glebiuskv | 0:2f0e1e23c242 | 5962 | |
glebiuskv | 0:2f0e1e23c242 | 5963 | /* Update partition information */ |
glebiuskv | 0:2f0e1e23c242 | 5964 | if (FF_MULTI_PARTITION && part != 0) { /* Created in the existing partition */ |
glebiuskv | 0:2f0e1e23c242 | 5965 | /* Update system ID in the partition table */ |
glebiuskv | 0:2f0e1e23c242 | 5966 | if (disk_read(pdrv, buf, 0, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); /* Read the MBR */ |
glebiuskv | 0:2f0e1e23c242 | 5967 | buf[MBR_Table + (part - 1) * SZ_PTE + PTE_System] = sys; /* Set system ID */ |
glebiuskv | 0:2f0e1e23c242 | 5968 | if (disk_write(pdrv, buf, 0, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); /* Write it back to the MBR */ |
glebiuskv | 0:2f0e1e23c242 | 5969 | } else { /* Created as a new single partition */ |
glebiuskv | 0:2f0e1e23c242 | 5970 | if (!(opt & FM_SFD)) { /* Create partition table if in FDISK format */ |
glebiuskv | 0:2f0e1e23c242 | 5971 | mem_set(buf, 0, ss); |
glebiuskv | 0:2f0e1e23c242 | 5972 | st_word(buf + BS_55AA, 0xAA55); /* MBR signature */ |
glebiuskv | 0:2f0e1e23c242 | 5973 | pte = buf + MBR_Table; /* Create partition table for single partition in the drive */ |
glebiuskv | 0:2f0e1e23c242 | 5974 | pte[PTE_Boot] = 0; /* Boot indicator */ |
glebiuskv | 0:2f0e1e23c242 | 5975 | pte[PTE_StHead] = 1; /* Start head */ |
glebiuskv | 0:2f0e1e23c242 | 5976 | pte[PTE_StSec] = 1; /* Start sector */ |
glebiuskv | 0:2f0e1e23c242 | 5977 | pte[PTE_StCyl] = 0; /* Start cylinder */ |
glebiuskv | 0:2f0e1e23c242 | 5978 | pte[PTE_System] = sys; /* System type */ |
glebiuskv | 0:2f0e1e23c242 | 5979 | n = (b_vol + sz_vol) / (63 * 255); /* (End CHS may be invalid) */ |
glebiuskv | 0:2f0e1e23c242 | 5980 | pte[PTE_EdHead] = 254; /* End head */ |
glebiuskv | 0:2f0e1e23c242 | 5981 | pte[PTE_EdSec] = (BYTE)(((n >> 2) & 0xC0) | 63); /* End sector */ |
glebiuskv | 0:2f0e1e23c242 | 5982 | pte[PTE_EdCyl] = (BYTE)n; /* End cylinder */ |
glebiuskv | 0:2f0e1e23c242 | 5983 | st_dword(pte + PTE_StLba, b_vol); /* Start offset in LBA */ |
glebiuskv | 0:2f0e1e23c242 | 5984 | st_dword(pte + PTE_SizLba, sz_vol); /* Size in sectors */ |
glebiuskv | 0:2f0e1e23c242 | 5985 | if (disk_write(pdrv, buf, 0, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); /* Write it to the MBR */ |
glebiuskv | 0:2f0e1e23c242 | 5986 | } |
glebiuskv | 0:2f0e1e23c242 | 5987 | } |
glebiuskv | 0:2f0e1e23c242 | 5988 | |
glebiuskv | 0:2f0e1e23c242 | 5989 | if (disk_ioctl(pdrv, CTRL_SYNC, 0) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); |
glebiuskv | 0:2f0e1e23c242 | 5990 | |
glebiuskv | 0:2f0e1e23c242 | 5991 | LEAVE_MKFS(FR_OK); |
glebiuskv | 0:2f0e1e23c242 | 5992 | } |
glebiuskv | 0:2f0e1e23c242 | 5993 | |
glebiuskv | 0:2f0e1e23c242 | 5994 | |
glebiuskv | 0:2f0e1e23c242 | 5995 | |
glebiuskv | 0:2f0e1e23c242 | 5996 | #if FF_MULTI_PARTITION |
glebiuskv | 0:2f0e1e23c242 | 5997 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 5998 | /* Create Partition Table on the Physical Drive */ |
glebiuskv | 0:2f0e1e23c242 | 5999 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 6000 | |
glebiuskv | 0:2f0e1e23c242 | 6001 | FRESULT f_fdisk ( |
glebiuskv | 0:2f0e1e23c242 | 6002 | BYTE pdrv, /* Physical drive number */ |
glebiuskv | 0:2f0e1e23c242 | 6003 | const DWORD* szt, /* Pointer to the size table for each partitions */ |
glebiuskv | 0:2f0e1e23c242 | 6004 | void* work /* Pointer to the working buffer (null: use heap memory) */ |
glebiuskv | 0:2f0e1e23c242 | 6005 | ) |
glebiuskv | 0:2f0e1e23c242 | 6006 | { |
glebiuskv | 0:2f0e1e23c242 | 6007 | UINT i, n, sz_cyl, tot_cyl, b_cyl, e_cyl, p_cyl; |
glebiuskv | 0:2f0e1e23c242 | 6008 | BYTE s_hd, e_hd, *p, *buf; = (BYTE*)work; |
glebiuskv | 0:2f0e1e23c242 | 6009 | DSTATUS stat; |
glebiuskv | 0:2f0e1e23c242 | 6010 | DWORD sz_disk, sz_part, s_part; |
glebiuskv | 0:2f0e1e23c242 | 6011 | FRESULT res; |
glebiuskv | 0:2f0e1e23c242 | 6012 | |
glebiuskv | 0:2f0e1e23c242 | 6013 | |
glebiuskv | 0:2f0e1e23c242 | 6014 | stat = disk_initialize(pdrv); |
glebiuskv | 0:2f0e1e23c242 | 6015 | if (stat & STA_NOINIT) return FR_NOT_READY; |
glebiuskv | 0:2f0e1e23c242 | 6016 | if (stat & STA_PROTECT) return FR_WRITE_PROTECTED; |
glebiuskv | 0:2f0e1e23c242 | 6017 | if (disk_ioctl(pdrv, GET_SECTOR_COUNT, &sz_disk)) return FR_DISK_ERR; |
glebiuskv | 0:2f0e1e23c242 | 6018 | |
glebiuskv | 0:2f0e1e23c242 | 6019 | buf = (BYTE*)work; |
glebiuskv | 0:2f0e1e23c242 | 6020 | #if FF_USE_LFN == 3 |
glebiuskv | 0:2f0e1e23c242 | 6021 | if (!buf) buf = ff_memalloc(FF_MAX_SS); /* Use heap memory for working buffer */ |
glebiuskv | 0:2f0e1e23c242 | 6022 | #endif |
glebiuskv | 0:2f0e1e23c242 | 6023 | if (!buf) return FR_NOT_ENOUGH_CORE; |
glebiuskv | 0:2f0e1e23c242 | 6024 | |
glebiuskv | 0:2f0e1e23c242 | 6025 | /* Determine the CHS without any consideration of the drive geometry */ |
glebiuskv | 0:2f0e1e23c242 | 6026 | for (n = 16; n < 256 && sz_disk / n / 63 > 1024; n *= 2) ; |
glebiuskv | 0:2f0e1e23c242 | 6027 | if (n == 256) n--; |
glebiuskv | 0:2f0e1e23c242 | 6028 | e_hd = n - 1; |
glebiuskv | 0:2f0e1e23c242 | 6029 | sz_cyl = 63 * n; |
glebiuskv | 0:2f0e1e23c242 | 6030 | tot_cyl = sz_disk / sz_cyl; |
glebiuskv | 0:2f0e1e23c242 | 6031 | |
glebiuskv | 0:2f0e1e23c242 | 6032 | /* Create partition table */ |
glebiuskv | 0:2f0e1e23c242 | 6033 | mem_set(buf, 0, FF_MAX_SS); |
glebiuskv | 0:2f0e1e23c242 | 6034 | p = buf + MBR_Table; b_cyl = 0; |
glebiuskv | 0:2f0e1e23c242 | 6035 | for (i = 0; i < 4; i++, p += SZ_PTE) { |
glebiuskv | 0:2f0e1e23c242 | 6036 | p_cyl = (szt[i] <= 100U) ? (DWORD)tot_cyl * szt[i] / 100 : szt[i] / sz_cyl; /* Number of cylinders */ |
glebiuskv | 0:2f0e1e23c242 | 6037 | if (p_cyl == 0) continue; |
glebiuskv | 0:2f0e1e23c242 | 6038 | s_part = (DWORD)sz_cyl * b_cyl; |
glebiuskv | 0:2f0e1e23c242 | 6039 | sz_part = (DWORD)sz_cyl * p_cyl; |
glebiuskv | 0:2f0e1e23c242 | 6040 | if (i == 0) { /* Exclude first track of cylinder 0 */ |
glebiuskv | 0:2f0e1e23c242 | 6041 | s_hd = 1; |
glebiuskv | 0:2f0e1e23c242 | 6042 | s_part += 63; sz_part -= 63; |
glebiuskv | 0:2f0e1e23c242 | 6043 | } else { |
glebiuskv | 0:2f0e1e23c242 | 6044 | s_hd = 0; |
glebiuskv | 0:2f0e1e23c242 | 6045 | } |
glebiuskv | 0:2f0e1e23c242 | 6046 | e_cyl = b_cyl + p_cyl - 1; /* End cylinder */ |
glebiuskv | 0:2f0e1e23c242 | 6047 | if (e_cyl >= tot_cyl) LEAVE_MKFS(FR_INVALID_PARAMETER); |
glebiuskv | 0:2f0e1e23c242 | 6048 | |
glebiuskv | 0:2f0e1e23c242 | 6049 | /* Set partition table */ |
glebiuskv | 0:2f0e1e23c242 | 6050 | p[1] = s_hd; /* Start head */ |
glebiuskv | 0:2f0e1e23c242 | 6051 | p[2] = (BYTE)(((b_cyl >> 2) & 0xC0) | 1); /* Start sector */ |
glebiuskv | 0:2f0e1e23c242 | 6052 | p[3] = (BYTE)b_cyl; /* Start cylinder */ |
glebiuskv | 0:2f0e1e23c242 | 6053 | p[4] = 0x07; /* System type (temporary setting) */ |
glebiuskv | 0:2f0e1e23c242 | 6054 | p[5] = e_hd; /* End head */ |
glebiuskv | 0:2f0e1e23c242 | 6055 | p[6] = (BYTE)(((e_cyl >> 2) & 0xC0) | 63); /* End sector */ |
glebiuskv | 0:2f0e1e23c242 | 6056 | p[7] = (BYTE)e_cyl; /* End cylinder */ |
glebiuskv | 0:2f0e1e23c242 | 6057 | st_dword(p + 8, s_part); /* Start sector in LBA */ |
glebiuskv | 0:2f0e1e23c242 | 6058 | st_dword(p + 12, sz_part); /* Number of sectors */ |
glebiuskv | 0:2f0e1e23c242 | 6059 | |
glebiuskv | 0:2f0e1e23c242 | 6060 | /* Next partition */ |
glebiuskv | 0:2f0e1e23c242 | 6061 | b_cyl += p_cyl; |
glebiuskv | 0:2f0e1e23c242 | 6062 | } |
glebiuskv | 0:2f0e1e23c242 | 6063 | st_word(p, 0xAA55); /* MBR signature (always at offset 510) */ |
glebiuskv | 0:2f0e1e23c242 | 6064 | |
glebiuskv | 0:2f0e1e23c242 | 6065 | /* Write it to the MBR */ |
glebiuskv | 0:2f0e1e23c242 | 6066 | res = (disk_write(pdrv, buf, 0, 1) == RES_OK && disk_ioctl(pdrv, CTRL_SYNC, 0) == RES_OK) ? FR_OK : FR_DISK_ERR; |
glebiuskv | 0:2f0e1e23c242 | 6067 | LEAVE_MKFS(res); |
glebiuskv | 0:2f0e1e23c242 | 6068 | } |
glebiuskv | 0:2f0e1e23c242 | 6069 | |
glebiuskv | 0:2f0e1e23c242 | 6070 | #endif /* FF_MULTI_PARTITION */ |
glebiuskv | 0:2f0e1e23c242 | 6071 | #endif /* FF_USE_MKFS && !FF_FS_READONLY */ |
glebiuskv | 0:2f0e1e23c242 | 6072 | |
glebiuskv | 0:2f0e1e23c242 | 6073 | |
glebiuskv | 0:2f0e1e23c242 | 6074 | |
glebiuskv | 0:2f0e1e23c242 | 6075 | |
glebiuskv | 0:2f0e1e23c242 | 6076 | #if FF_USE_STRFUNC |
glebiuskv | 0:2f0e1e23c242 | 6077 | #if FF_USE_LFN && FF_LFN_UNICODE && (FF_STRF_ENCODE < 0 || FF_STRF_ENCODE > 3) |
glebiuskv | 0:2f0e1e23c242 | 6078 | #error Wrong FF_STRF_ENCODE setting |
glebiuskv | 0:2f0e1e23c242 | 6079 | #endif |
glebiuskv | 0:2f0e1e23c242 | 6080 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 6081 | /* Get a String from the File */ |
glebiuskv | 0:2f0e1e23c242 | 6082 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 6083 | |
glebiuskv | 0:2f0e1e23c242 | 6084 | TCHAR* f_gets ( |
glebiuskv | 0:2f0e1e23c242 | 6085 | TCHAR* buff, /* Pointer to the string buffer to read */ |
glebiuskv | 0:2f0e1e23c242 | 6086 | int len, /* Size of string buffer (items) */ |
glebiuskv | 0:2f0e1e23c242 | 6087 | FIL* fp /* Pointer to the file object */ |
glebiuskv | 0:2f0e1e23c242 | 6088 | ) |
glebiuskv | 0:2f0e1e23c242 | 6089 | { |
glebiuskv | 0:2f0e1e23c242 | 6090 | int nc = 0; |
glebiuskv | 0:2f0e1e23c242 | 6091 | TCHAR *p = buff; |
glebiuskv | 0:2f0e1e23c242 | 6092 | BYTE s[2]; |
glebiuskv | 0:2f0e1e23c242 | 6093 | UINT rc; |
glebiuskv | 0:2f0e1e23c242 | 6094 | WCHAR wc; |
glebiuskv | 0:2f0e1e23c242 | 6095 | #if FF_USE_LFN && ((FF_LFN_UNICODE == 1 && FF_STRF_ENCODE == 3) || (FF_LFN_UNICODE == 2 && FF_STRF_ENCODE != 3)) |
glebiuskv | 0:2f0e1e23c242 | 6096 | DWORD dc; |
glebiuskv | 0:2f0e1e23c242 | 6097 | #endif |
glebiuskv | 0:2f0e1e23c242 | 6098 | #if FF_USE_LFN && FF_LFN_UNICODE == 1 && FF_STRF_ENCODE == 3 |
glebiuskv | 0:2f0e1e23c242 | 6099 | UINT ct; |
glebiuskv | 0:2f0e1e23c242 | 6100 | #endif |
glebiuskv | 0:2f0e1e23c242 | 6101 | |
glebiuskv | 0:2f0e1e23c242 | 6102 | #if FF_USE_LFN && FF_LFN_UNICODE == 1 /* UTF-16 output */ |
glebiuskv | 0:2f0e1e23c242 | 6103 | #if FF_STRF_ENCODE == 0 /* Read a character in ANSI/OEM */ |
glebiuskv | 0:2f0e1e23c242 | 6104 | while (nc < len - 1) { |
glebiuskv | 0:2f0e1e23c242 | 6105 | f_read(fp, s, 1, &rc); |
glebiuskv | 0:2f0e1e23c242 | 6106 | if (rc != 1) break; |
glebiuskv | 0:2f0e1e23c242 | 6107 | wc = s[0]; |
glebiuskv | 0:2f0e1e23c242 | 6108 | if (dbc_1st((BYTE)wc)) { |
glebiuskv | 0:2f0e1e23c242 | 6109 | f_read(fp, s, 1, &rc); |
glebiuskv | 0:2f0e1e23c242 | 6110 | if (rc != 1 || !dbc_2nd(s[0])) continue; |
glebiuskv | 0:2f0e1e23c242 | 6111 | wc = wc << 8 | s[0]; |
glebiuskv | 0:2f0e1e23c242 | 6112 | } |
glebiuskv | 0:2f0e1e23c242 | 6113 | wc = ff_oem2uni(wc, CODEPAGE); |
glebiuskv | 0:2f0e1e23c242 | 6114 | if (wc == 0) continue; |
glebiuskv | 0:2f0e1e23c242 | 6115 | #elif FF_STRF_ENCODE == 1 || FF_STRF_ENCODE == 2 /* Read a character in UTF-16LE/BE */ |
glebiuskv | 0:2f0e1e23c242 | 6116 | while (nc < len - 1) { |
glebiuskv | 0:2f0e1e23c242 | 6117 | f_read(fp, s, 2, &rc); |
glebiuskv | 0:2f0e1e23c242 | 6118 | if (rc != 2) break; |
glebiuskv | 0:2f0e1e23c242 | 6119 | wc = (FF_STRF_ENCODE == 1) ? s[1] << 8 | s[0] : s[0] << 8 | s[1]; |
glebiuskv | 0:2f0e1e23c242 | 6120 | #elif FF_STRF_ENCODE == 3 /* Read a character in UTF-8 */ |
glebiuskv | 0:2f0e1e23c242 | 6121 | while (nc < len - 2) { |
glebiuskv | 0:2f0e1e23c242 | 6122 | f_read(fp, s, 1, &rc); |
glebiuskv | 0:2f0e1e23c242 | 6123 | if (rc != 1) break; |
glebiuskv | 0:2f0e1e23c242 | 6124 | dc = s[0]; |
glebiuskv | 0:2f0e1e23c242 | 6125 | if (dc >= 0x80) { |
glebiuskv | 0:2f0e1e23c242 | 6126 | ct = 0; |
glebiuskv | 0:2f0e1e23c242 | 6127 | if ((dc & 0xE0) == 0xC0) { dc &= 0x1F; ct = 1; } |
glebiuskv | 0:2f0e1e23c242 | 6128 | if ((dc & 0xF0) == 0xE0) { dc &= 0x0F; ct = 2; } |
glebiuskv | 0:2f0e1e23c242 | 6129 | if ((dc & 0xF8) == 0xF0) { dc &= 0x07; ct = 3; } |
glebiuskv | 0:2f0e1e23c242 | 6130 | if (ct == 0) continue; |
glebiuskv | 0:2f0e1e23c242 | 6131 | do { |
glebiuskv | 0:2f0e1e23c242 | 6132 | f_read(fp, s, 1, &rc); |
glebiuskv | 0:2f0e1e23c242 | 6133 | if (rc != 1 || (s[0] & 0xC0) != 0x80) break; |
glebiuskv | 0:2f0e1e23c242 | 6134 | dc = dc << 6 | (s[0] & 0x3F); |
glebiuskv | 0:2f0e1e23c242 | 6135 | } while (--ct); |
glebiuskv | 0:2f0e1e23c242 | 6136 | if (ct || dc < 0x80 || dc >= 0x110000) continue; |
glebiuskv | 0:2f0e1e23c242 | 6137 | } |
glebiuskv | 0:2f0e1e23c242 | 6138 | if (dc >= 0x10000) { |
glebiuskv | 0:2f0e1e23c242 | 6139 | wc = (WCHAR)(0xD800 | ((dc >> 10) - 0x40)); |
glebiuskv | 0:2f0e1e23c242 | 6140 | *p++ = wc; nc++; |
glebiuskv | 0:2f0e1e23c242 | 6141 | wc = (WCHAR)(0xDC00 | (dc & 0x3FF)); |
glebiuskv | 0:2f0e1e23c242 | 6142 | } else { |
glebiuskv | 0:2f0e1e23c242 | 6143 | wc = (WCHAR)dc; |
glebiuskv | 0:2f0e1e23c242 | 6144 | } |
glebiuskv | 0:2f0e1e23c242 | 6145 | #endif |
glebiuskv | 0:2f0e1e23c242 | 6146 | /* Output it in UTF-16 encoding */ |
glebiuskv | 0:2f0e1e23c242 | 6147 | if (FF_USE_STRFUNC == 2 && wc == '\r') continue; |
glebiuskv | 0:2f0e1e23c242 | 6148 | *p++ = wc; nc++; |
glebiuskv | 0:2f0e1e23c242 | 6149 | if (wc == '\n') break; |
glebiuskv | 0:2f0e1e23c242 | 6150 | } |
glebiuskv | 0:2f0e1e23c242 | 6151 | |
glebiuskv | 0:2f0e1e23c242 | 6152 | #elif FF_USE_LFN && FF_LFN_UNICODE == 2 && FF_STRF_ENCODE != 3 /* UTF-8 output */ |
glebiuskv | 0:2f0e1e23c242 | 6153 | while (nc < len - 4) { |
glebiuskv | 0:2f0e1e23c242 | 6154 | #if FF_STRF_ENCODE == 0 /* Read a character in ANSI/OEM */ |
glebiuskv | 0:2f0e1e23c242 | 6155 | f_read(fp, s, 1, &rc); |
glebiuskv | 0:2f0e1e23c242 | 6156 | if (rc != 1) break; |
glebiuskv | 0:2f0e1e23c242 | 6157 | wc = s[0]; |
glebiuskv | 0:2f0e1e23c242 | 6158 | if (dbc_1st((BYTE)wc)) { |
glebiuskv | 0:2f0e1e23c242 | 6159 | f_read(fp, s, 1, &rc); |
glebiuskv | 0:2f0e1e23c242 | 6160 | if (rc != 1 || !dbc_2nd(s[0])) continue; |
glebiuskv | 0:2f0e1e23c242 | 6161 | wc = wc << 8 | s[0]; |
glebiuskv | 0:2f0e1e23c242 | 6162 | } |
glebiuskv | 0:2f0e1e23c242 | 6163 | dc = ff_oem2uni(wc, CODEPAGE); |
glebiuskv | 0:2f0e1e23c242 | 6164 | if (dc == 0) continue; |
glebiuskv | 0:2f0e1e23c242 | 6165 | #else /* Read a character in UTF-16LE/BE */ |
glebiuskv | 0:2f0e1e23c242 | 6166 | f_read(fp, s, 2, &rc); |
glebiuskv | 0:2f0e1e23c242 | 6167 | if (rc != 2) break; |
glebiuskv | 0:2f0e1e23c242 | 6168 | dc = (FF_STRF_ENCODE == 1) ? s[1] << 8 | s[0] : s[0] << 8 | s[1]; |
glebiuskv | 0:2f0e1e23c242 | 6169 | if (IsSurrogate(dc)) { |
glebiuskv | 0:2f0e1e23c242 | 6170 | f_read(fp, s, 2, &rc); |
glebiuskv | 0:2f0e1e23c242 | 6171 | if (rc != 2) break; |
glebiuskv | 0:2f0e1e23c242 | 6172 | wc = (FF_STRF_ENCODE == 1) ? s[1] << 8 | s[0] : s[0] << 8 | s[1]; |
glebiuskv | 0:2f0e1e23c242 | 6173 | if (!IsSurrogateH(dc) || !IsSurrogateL(wc)) continue; |
glebiuskv | 0:2f0e1e23c242 | 6174 | dc = ((dc & 0x3FF) + 0x40) << 10 | (wc & 0x3FF); |
glebiuskv | 0:2f0e1e23c242 | 6175 | } |
glebiuskv | 0:2f0e1e23c242 | 6176 | #endif |
glebiuskv | 0:2f0e1e23c242 | 6177 | /* Output it in UTF-8 encoding */ |
glebiuskv | 0:2f0e1e23c242 | 6178 | if (FF_USE_STRFUNC == 2 && dc == '\r') continue; |
glebiuskv | 0:2f0e1e23c242 | 6179 | if (dc < 0x80) { /* 1-byte */ |
glebiuskv | 0:2f0e1e23c242 | 6180 | *p++ = (TCHAR)dc; |
glebiuskv | 0:2f0e1e23c242 | 6181 | nc++; |
glebiuskv | 0:2f0e1e23c242 | 6182 | if (dc == '\n') break; |
glebiuskv | 0:2f0e1e23c242 | 6183 | } else { |
glebiuskv | 0:2f0e1e23c242 | 6184 | if (dc < 0x800) { /* 2-byte */ |
glebiuskv | 0:2f0e1e23c242 | 6185 | *p++ = (TCHAR)(0xC0 | (dc >> 6 & 0x1F)); |
glebiuskv | 0:2f0e1e23c242 | 6186 | *p++ = (TCHAR)(0x80 | (dc >> 0 & 0x3F)); |
glebiuskv | 0:2f0e1e23c242 | 6187 | nc += 2; |
glebiuskv | 0:2f0e1e23c242 | 6188 | } else { |
glebiuskv | 0:2f0e1e23c242 | 6189 | if (dc < 0x10000) { /* 3-byte */ |
glebiuskv | 0:2f0e1e23c242 | 6190 | *p++ = (TCHAR)(0xE0 | (dc >> 12 & 0x0F)); |
glebiuskv | 0:2f0e1e23c242 | 6191 | *p++ = (TCHAR)(0x80 | (dc >> 6 & 0x3F)); |
glebiuskv | 0:2f0e1e23c242 | 6192 | *p++ = (TCHAR)(0x80 | (dc >> 0 & 0x3F)); |
glebiuskv | 0:2f0e1e23c242 | 6193 | nc += 3; |
glebiuskv | 0:2f0e1e23c242 | 6194 | } else { /* 4-byte */ |
glebiuskv | 0:2f0e1e23c242 | 6195 | *p++ = (TCHAR)(0xF0 | (dc >> 18 & 0x07)); |
glebiuskv | 0:2f0e1e23c242 | 6196 | *p++ = (TCHAR)(0x80 | (dc >> 12 & 0x3F)); |
glebiuskv | 0:2f0e1e23c242 | 6197 | *p++ = (TCHAR)(0x80 | (dc >> 6 & 0x3F)); |
glebiuskv | 0:2f0e1e23c242 | 6198 | *p++ = (TCHAR)(0x80 | (dc >> 0 & 0x3F)); |
glebiuskv | 0:2f0e1e23c242 | 6199 | nc += 4; |
glebiuskv | 0:2f0e1e23c242 | 6200 | } |
glebiuskv | 0:2f0e1e23c242 | 6201 | } |
glebiuskv | 0:2f0e1e23c242 | 6202 | } |
glebiuskv | 0:2f0e1e23c242 | 6203 | } |
glebiuskv | 0:2f0e1e23c242 | 6204 | |
glebiuskv | 0:2f0e1e23c242 | 6205 | #else /* Byte-by-byte without any conversion (ANSI/OEM API or UTF-8 to UTF-8) */ |
glebiuskv | 0:2f0e1e23c242 | 6206 | while (nc < len - 1) { |
glebiuskv | 0:2f0e1e23c242 | 6207 | f_read(fp, s, 1, &rc); |
glebiuskv | 0:2f0e1e23c242 | 6208 | if (rc != 1) break; |
glebiuskv | 0:2f0e1e23c242 | 6209 | wc = s[0]; |
glebiuskv | 0:2f0e1e23c242 | 6210 | if (FF_USE_STRFUNC == 2 && wc == '\r') continue; |
glebiuskv | 0:2f0e1e23c242 | 6211 | *p++ = (TCHAR)wc; nc++; |
glebiuskv | 0:2f0e1e23c242 | 6212 | if (wc == '\n') break; |
glebiuskv | 0:2f0e1e23c242 | 6213 | } |
glebiuskv | 0:2f0e1e23c242 | 6214 | #endif |
glebiuskv | 0:2f0e1e23c242 | 6215 | |
glebiuskv | 0:2f0e1e23c242 | 6216 | *p = 0; |
glebiuskv | 0:2f0e1e23c242 | 6217 | return nc ? buff : 0; /* When no data read (EOF or error), return with error. */ |
glebiuskv | 0:2f0e1e23c242 | 6218 | } |
glebiuskv | 0:2f0e1e23c242 | 6219 | |
glebiuskv | 0:2f0e1e23c242 | 6220 | |
glebiuskv | 0:2f0e1e23c242 | 6221 | |
glebiuskv | 0:2f0e1e23c242 | 6222 | |
glebiuskv | 0:2f0e1e23c242 | 6223 | #if !FF_FS_READONLY |
glebiuskv | 0:2f0e1e23c242 | 6224 | #include <stdarg.h> |
glebiuskv | 0:2f0e1e23c242 | 6225 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 6226 | /* Put a Character to the File */ |
glebiuskv | 0:2f0e1e23c242 | 6227 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 6228 | |
glebiuskv | 0:2f0e1e23c242 | 6229 | typedef struct { /* Putchar output buffer and work area */ |
glebiuskv | 0:2f0e1e23c242 | 6230 | FIL *fp; /* Ptr to the writing file */ |
glebiuskv | 0:2f0e1e23c242 | 6231 | int idx, nchr; /* Write index of buf[] (-1:error), number of encoding units written */ |
glebiuskv | 0:2f0e1e23c242 | 6232 | #if FF_USE_LFN && FF_LFN_UNICODE == 1 |
glebiuskv | 0:2f0e1e23c242 | 6233 | WCHAR hs; |
glebiuskv | 0:2f0e1e23c242 | 6234 | #elif FF_USE_LFN && FF_LFN_UNICODE == 2 |
glebiuskv | 0:2f0e1e23c242 | 6235 | BYTE bs[4]; |
glebiuskv | 0:2f0e1e23c242 | 6236 | UINT wi, ct; |
glebiuskv | 0:2f0e1e23c242 | 6237 | #endif |
glebiuskv | 0:2f0e1e23c242 | 6238 | BYTE buf[64]; /* Write buffer */ |
glebiuskv | 0:2f0e1e23c242 | 6239 | } putbuff; |
glebiuskv | 0:2f0e1e23c242 | 6240 | |
glebiuskv | 0:2f0e1e23c242 | 6241 | |
glebiuskv | 0:2f0e1e23c242 | 6242 | static |
glebiuskv | 0:2f0e1e23c242 | 6243 | void putc_bfd ( /* Buffered write with code conversion */ |
glebiuskv | 0:2f0e1e23c242 | 6244 | putbuff* pb, |
glebiuskv | 0:2f0e1e23c242 | 6245 | TCHAR c |
glebiuskv | 0:2f0e1e23c242 | 6246 | ) |
glebiuskv | 0:2f0e1e23c242 | 6247 | { |
glebiuskv | 0:2f0e1e23c242 | 6248 | UINT n; |
glebiuskv | 0:2f0e1e23c242 | 6249 | int i, nc; |
glebiuskv | 0:2f0e1e23c242 | 6250 | #if FF_USE_LFN && (FF_LFN_UNICODE == 1 || (FF_LFN_UNICODE == 2 && (FF_STRF_ENCODE != 3))) |
glebiuskv | 0:2f0e1e23c242 | 6251 | WCHAR hs, wc; |
glebiuskv | 0:2f0e1e23c242 | 6252 | #endif |
glebiuskv | 0:2f0e1e23c242 | 6253 | #if FF_USE_LFN && FF_LFN_UNICODE == 2 && FF_STRF_ENCODE != 3 |
glebiuskv | 0:2f0e1e23c242 | 6254 | DWORD dc; |
glebiuskv | 0:2f0e1e23c242 | 6255 | TCHAR *tp; |
glebiuskv | 0:2f0e1e23c242 | 6256 | #endif |
glebiuskv | 0:2f0e1e23c242 | 6257 | |
glebiuskv | 0:2f0e1e23c242 | 6258 | if (FF_USE_STRFUNC == 2 && c == '\n') { /* LF -> CRLF conversion */ |
glebiuskv | 0:2f0e1e23c242 | 6259 | putc_bfd(pb, '\r'); |
glebiuskv | 0:2f0e1e23c242 | 6260 | } |
glebiuskv | 0:2f0e1e23c242 | 6261 | |
glebiuskv | 0:2f0e1e23c242 | 6262 | i = pb->idx; /* Write index of pb->buf[] */ |
glebiuskv | 0:2f0e1e23c242 | 6263 | if (i < 0) return; |
glebiuskv | 0:2f0e1e23c242 | 6264 | nc = pb->nchr; /* Write unit count */ |
glebiuskv | 0:2f0e1e23c242 | 6265 | |
glebiuskv | 0:2f0e1e23c242 | 6266 | #if FF_USE_LFN && FF_LFN_UNICODE >= 1 |
glebiuskv | 0:2f0e1e23c242 | 6267 | #if FF_USE_LFN && FF_LFN_UNICODE == 1 /* UTF-16 input */ |
glebiuskv | 0:2f0e1e23c242 | 6268 | if (IsSurrogateH(c)) { |
glebiuskv | 0:2f0e1e23c242 | 6269 | pb->hs = c; return; |
glebiuskv | 0:2f0e1e23c242 | 6270 | } |
glebiuskv | 0:2f0e1e23c242 | 6271 | wc = c; hs = pb->hs; pb->hs = 0; |
glebiuskv | 0:2f0e1e23c242 | 6272 | if (hs != 0) { |
glebiuskv | 0:2f0e1e23c242 | 6273 | if (!IsSurrogateL(wc)) hs = 0; |
glebiuskv | 0:2f0e1e23c242 | 6274 | } else { |
glebiuskv | 0:2f0e1e23c242 | 6275 | if (IsSurrogateL(wc)) return; |
glebiuskv | 0:2f0e1e23c242 | 6276 | } |
glebiuskv | 0:2f0e1e23c242 | 6277 | #if FF_STRF_ENCODE == 3 /* Write it in UTF-8 */ |
glebiuskv | 0:2f0e1e23c242 | 6278 | if (hs != 0) { /* 4-byte */ |
glebiuskv | 0:2f0e1e23c242 | 6279 | nc += 4; |
glebiuskv | 0:2f0e1e23c242 | 6280 | hs = (hs & 0x3FF) + 0x40; |
glebiuskv | 0:2f0e1e23c242 | 6281 | pb->buf[i++] = (BYTE)(0xF0 | hs >> 8); |
glebiuskv | 0:2f0e1e23c242 | 6282 | pb->buf[i++] = (BYTE)(0x80 | (hs >> 2 & 0x3F)); |
glebiuskv | 0:2f0e1e23c242 | 6283 | pb->buf[i++] = (BYTE)(0x80 | (hs & 3) << 4 | (wc >> 6 & 0x0F)); |
glebiuskv | 0:2f0e1e23c242 | 6284 | pb->buf[i++] = (BYTE)(0x80 | (wc & 0x3F)); |
glebiuskv | 0:2f0e1e23c242 | 6285 | } else { |
glebiuskv | 0:2f0e1e23c242 | 6286 | if (wc < 0x80) { /* 1-byte */ |
glebiuskv | 0:2f0e1e23c242 | 6287 | nc++; |
glebiuskv | 0:2f0e1e23c242 | 6288 | pb->buf[i++] = (BYTE)wc; |
glebiuskv | 0:2f0e1e23c242 | 6289 | } else { |
glebiuskv | 0:2f0e1e23c242 | 6290 | if (wc < 0x800) { /* 2-byte */ |
glebiuskv | 0:2f0e1e23c242 | 6291 | nc += 2; |
glebiuskv | 0:2f0e1e23c242 | 6292 | pb->buf[i++] = (BYTE)(0xC0 | wc >> 6); |
glebiuskv | 0:2f0e1e23c242 | 6293 | } else { /* 3-byte */ |
glebiuskv | 0:2f0e1e23c242 | 6294 | nc += 3; |
glebiuskv | 0:2f0e1e23c242 | 6295 | pb->buf[i++] = (BYTE)(0xE0 | wc >> 12); |
glebiuskv | 0:2f0e1e23c242 | 6296 | pb->buf[i++] = (BYTE)(0x80 | (wc >> 6 & 0x3F)); |
glebiuskv | 0:2f0e1e23c242 | 6297 | } |
glebiuskv | 0:2f0e1e23c242 | 6298 | pb->buf[i++] = (BYTE)(0x80 | (wc & 0x3F)); |
glebiuskv | 0:2f0e1e23c242 | 6299 | } |
glebiuskv | 0:2f0e1e23c242 | 6300 | } |
glebiuskv | 0:2f0e1e23c242 | 6301 | #endif |
glebiuskv | 0:2f0e1e23c242 | 6302 | #else /* UTF-8 input */ |
glebiuskv | 0:2f0e1e23c242 | 6303 | for (;;) { |
glebiuskv | 0:2f0e1e23c242 | 6304 | if (pb->ct == 0) { /* Out of multi-byte sequence? */ |
glebiuskv | 0:2f0e1e23c242 | 6305 | pb->bs[pb->wi = 0] = (BYTE)c; /* Save 1st byte */ |
glebiuskv | 0:2f0e1e23c242 | 6306 | if ((BYTE)c < 0x80) break; /* 1-byte? */ |
glebiuskv | 0:2f0e1e23c242 | 6307 | if (((BYTE)c & 0xE0) == 0xC0) pb->ct = 1; /* 2-byte? */ |
glebiuskv | 0:2f0e1e23c242 | 6308 | if (((BYTE)c & 0xF0) == 0xE0) pb->ct = 2; /* 3-byte? */ |
glebiuskv | 0:2f0e1e23c242 | 6309 | if (((BYTE)c & 0xF1) == 0xF0) pb->ct = 3; /* 4-byte? */ |
glebiuskv | 0:2f0e1e23c242 | 6310 | return; |
glebiuskv | 0:2f0e1e23c242 | 6311 | } else { /* In the multi-byte sequence */ |
glebiuskv | 0:2f0e1e23c242 | 6312 | if (((BYTE)c & 0xC0) != 0x80) { /* Broken sequence? */ |
glebiuskv | 0:2f0e1e23c242 | 6313 | pb->ct = 0; continue; |
glebiuskv | 0:2f0e1e23c242 | 6314 | } |
glebiuskv | 0:2f0e1e23c242 | 6315 | pb->bs[++pb->wi] = (BYTE)c; /* Save the trailing byte */ |
glebiuskv | 0:2f0e1e23c242 | 6316 | if (--pb->ct == 0) break; /* End of sequence? */ |
glebiuskv | 0:2f0e1e23c242 | 6317 | return; |
glebiuskv | 0:2f0e1e23c242 | 6318 | } |
glebiuskv | 0:2f0e1e23c242 | 6319 | } |
glebiuskv | 0:2f0e1e23c242 | 6320 | #if FF_STRF_ENCODE == 3 /* Write it in UTF-8 */ |
glebiuskv | 0:2f0e1e23c242 | 6321 | pb->buf[i++] = pb->bs[0]; nc++; |
glebiuskv | 0:2f0e1e23c242 | 6322 | if (pb->bs[0] >= 0xC0) { |
glebiuskv | 0:2f0e1e23c242 | 6323 | pb->buf[i++] = pb->bs[1]; nc++; |
glebiuskv | 0:2f0e1e23c242 | 6324 | } |
glebiuskv | 0:2f0e1e23c242 | 6325 | if (pb->bs[0] >= 0xE0) { |
glebiuskv | 0:2f0e1e23c242 | 6326 | pb->buf[i++] = pb->bs[2]; nc++; |
glebiuskv | 0:2f0e1e23c242 | 6327 | } |
glebiuskv | 0:2f0e1e23c242 | 6328 | if (pb->bs[0] >= 0xF0) { |
glebiuskv | 0:2f0e1e23c242 | 6329 | pb->buf[i++] = pb->bs[3]; nc++; |
glebiuskv | 0:2f0e1e23c242 | 6330 | } |
glebiuskv | 0:2f0e1e23c242 | 6331 | #else /* Write it in UTF-16 or ANSI/OEM */ |
glebiuskv | 0:2f0e1e23c242 | 6332 | tp = (TCHAR*)pb->bs; |
glebiuskv | 0:2f0e1e23c242 | 6333 | dc = tchar2uni(&tp); /* UTF-8 ==> UTF-16 */ |
glebiuskv | 0:2f0e1e23c242 | 6334 | if (dc == 0xFFFFFFFF) return; |
glebiuskv | 0:2f0e1e23c242 | 6335 | wc = (WCHAR)dc; |
glebiuskv | 0:2f0e1e23c242 | 6336 | hs = (WCHAR)(dc >> 16); |
glebiuskv | 0:2f0e1e23c242 | 6337 | #endif |
glebiuskv | 0:2f0e1e23c242 | 6338 | #endif |
glebiuskv | 0:2f0e1e23c242 | 6339 | #if FF_USE_LFN && FF_LFN_UNICODE >= 1 && FF_STRF_ENCODE != 3 |
glebiuskv | 0:2f0e1e23c242 | 6340 | #if FF_STRF_ENCODE == 2 /* Write a character in UTF-16BE */ |
glebiuskv | 0:2f0e1e23c242 | 6341 | if (hs != 0) { |
glebiuskv | 0:2f0e1e23c242 | 6342 | pb->buf[i++] = (BYTE)(hs >> 8); |
glebiuskv | 0:2f0e1e23c242 | 6343 | pb->buf[i++] = (BYTE)hs; |
glebiuskv | 0:2f0e1e23c242 | 6344 | nc++; |
glebiuskv | 0:2f0e1e23c242 | 6345 | } |
glebiuskv | 0:2f0e1e23c242 | 6346 | pb->buf[i++] = (BYTE)(wc >> 8); |
glebiuskv | 0:2f0e1e23c242 | 6347 | pb->buf[i++] = (BYTE)wc; |
glebiuskv | 0:2f0e1e23c242 | 6348 | nc++; |
glebiuskv | 0:2f0e1e23c242 | 6349 | #elif FF_STRF_ENCODE == 1 /* Write a character in UTF-16LE */ |
glebiuskv | 0:2f0e1e23c242 | 6350 | if (hs != 0) { |
glebiuskv | 0:2f0e1e23c242 | 6351 | pb->buf[i++] = (BYTE)hs; |
glebiuskv | 0:2f0e1e23c242 | 6352 | pb->buf[i++] = (BYTE)(hs >> 8); |
glebiuskv | 0:2f0e1e23c242 | 6353 | nc++; |
glebiuskv | 0:2f0e1e23c242 | 6354 | } |
glebiuskv | 0:2f0e1e23c242 | 6355 | pb->buf[i++] = (BYTE)wc; |
glebiuskv | 0:2f0e1e23c242 | 6356 | pb->buf[i++] = (BYTE)(wc >> 8); |
glebiuskv | 0:2f0e1e23c242 | 6357 | nc++; |
glebiuskv | 0:2f0e1e23c242 | 6358 | #else /* Write a character in ANSI/OEM */ |
glebiuskv | 0:2f0e1e23c242 | 6359 | if (hs != 0) return; |
glebiuskv | 0:2f0e1e23c242 | 6360 | wc = ff_uni2oem(wc, CODEPAGE); /* UTF-16 ==> ANSI/OEM */ |
glebiuskv | 0:2f0e1e23c242 | 6361 | if (wc == 0) return;; |
glebiuskv | 0:2f0e1e23c242 | 6362 | if (wc >= 0x100) { |
glebiuskv | 0:2f0e1e23c242 | 6363 | pb->buf[i++] = (BYTE)(wc >> 8); nc++; |
glebiuskv | 0:2f0e1e23c242 | 6364 | } |
glebiuskv | 0:2f0e1e23c242 | 6365 | pb->buf[i++] = (BYTE)wc; nc++; |
glebiuskv | 0:2f0e1e23c242 | 6366 | #endif |
glebiuskv | 0:2f0e1e23c242 | 6367 | #endif |
glebiuskv | 0:2f0e1e23c242 | 6368 | |
glebiuskv | 0:2f0e1e23c242 | 6369 | #else /* ANSI/OEM input */ |
glebiuskv | 0:2f0e1e23c242 | 6370 | pb->buf[i++] = (BYTE)c; |
glebiuskv | 0:2f0e1e23c242 | 6371 | nc++; |
glebiuskv | 0:2f0e1e23c242 | 6372 | #endif |
glebiuskv | 0:2f0e1e23c242 | 6373 | |
glebiuskv | 0:2f0e1e23c242 | 6374 | if (i >= (int)(sizeof pb->buf) - 4) { /* Write buffered characters to the file */ |
glebiuskv | 0:2f0e1e23c242 | 6375 | f_write(pb->fp, pb->buf, (UINT)i, &n); |
glebiuskv | 0:2f0e1e23c242 | 6376 | i = (n == (UINT)i) ? 0 : -1; |
glebiuskv | 0:2f0e1e23c242 | 6377 | } |
glebiuskv | 0:2f0e1e23c242 | 6378 | pb->idx = i; |
glebiuskv | 0:2f0e1e23c242 | 6379 | pb->nchr = nc; |
glebiuskv | 0:2f0e1e23c242 | 6380 | } |
glebiuskv | 0:2f0e1e23c242 | 6381 | |
glebiuskv | 0:2f0e1e23c242 | 6382 | |
glebiuskv | 0:2f0e1e23c242 | 6383 | static |
glebiuskv | 0:2f0e1e23c242 | 6384 | int putc_flush ( /* Flush left characters in the buffer */ |
glebiuskv | 0:2f0e1e23c242 | 6385 | putbuff* pb |
glebiuskv | 0:2f0e1e23c242 | 6386 | ) |
glebiuskv | 0:2f0e1e23c242 | 6387 | { |
glebiuskv | 0:2f0e1e23c242 | 6388 | UINT nw; |
glebiuskv | 0:2f0e1e23c242 | 6389 | |
glebiuskv | 0:2f0e1e23c242 | 6390 | if ( pb->idx >= 0 /* Flush buffered characters to the file */ |
glebiuskv | 0:2f0e1e23c242 | 6391 | && f_write(pb->fp, pb->buf, (UINT)pb->idx, &nw) == FR_OK |
glebiuskv | 0:2f0e1e23c242 | 6392 | && (UINT)pb->idx == nw) return pb->nchr; |
glebiuskv | 0:2f0e1e23c242 | 6393 | return EOF; |
glebiuskv | 0:2f0e1e23c242 | 6394 | } |
glebiuskv | 0:2f0e1e23c242 | 6395 | |
glebiuskv | 0:2f0e1e23c242 | 6396 | |
glebiuskv | 0:2f0e1e23c242 | 6397 | static |
glebiuskv | 0:2f0e1e23c242 | 6398 | void putc_init ( /* Initialize write buffer */ |
glebiuskv | 0:2f0e1e23c242 | 6399 | putbuff* pb, |
glebiuskv | 0:2f0e1e23c242 | 6400 | FIL* fp |
glebiuskv | 0:2f0e1e23c242 | 6401 | ) |
glebiuskv | 0:2f0e1e23c242 | 6402 | { |
glebiuskv | 0:2f0e1e23c242 | 6403 | mem_set(pb, 0, sizeof (putbuff)); |
glebiuskv | 0:2f0e1e23c242 | 6404 | pb->fp = fp; |
glebiuskv | 0:2f0e1e23c242 | 6405 | } |
glebiuskv | 0:2f0e1e23c242 | 6406 | |
glebiuskv | 0:2f0e1e23c242 | 6407 | |
glebiuskv | 0:2f0e1e23c242 | 6408 | |
glebiuskv | 0:2f0e1e23c242 | 6409 | int f_putc ( |
glebiuskv | 0:2f0e1e23c242 | 6410 | TCHAR c, /* A character to be output */ |
glebiuskv | 0:2f0e1e23c242 | 6411 | FIL* fp /* Pointer to the file object */ |
glebiuskv | 0:2f0e1e23c242 | 6412 | ) |
glebiuskv | 0:2f0e1e23c242 | 6413 | { |
glebiuskv | 0:2f0e1e23c242 | 6414 | putbuff pb; |
glebiuskv | 0:2f0e1e23c242 | 6415 | |
glebiuskv | 0:2f0e1e23c242 | 6416 | |
glebiuskv | 0:2f0e1e23c242 | 6417 | putc_init(&pb, fp); |
glebiuskv | 0:2f0e1e23c242 | 6418 | putc_bfd(&pb, c); /* Put the character */ |
glebiuskv | 0:2f0e1e23c242 | 6419 | return putc_flush(&pb); |
glebiuskv | 0:2f0e1e23c242 | 6420 | } |
glebiuskv | 0:2f0e1e23c242 | 6421 | |
glebiuskv | 0:2f0e1e23c242 | 6422 | |
glebiuskv | 0:2f0e1e23c242 | 6423 | |
glebiuskv | 0:2f0e1e23c242 | 6424 | |
glebiuskv | 0:2f0e1e23c242 | 6425 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 6426 | /* Put a String to the File */ |
glebiuskv | 0:2f0e1e23c242 | 6427 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 6428 | |
glebiuskv | 0:2f0e1e23c242 | 6429 | int f_puts ( |
glebiuskv | 0:2f0e1e23c242 | 6430 | const TCHAR* str, /* Pointer to the string to be output */ |
glebiuskv | 0:2f0e1e23c242 | 6431 | FIL* fp /* Pointer to the file object */ |
glebiuskv | 0:2f0e1e23c242 | 6432 | ) |
glebiuskv | 0:2f0e1e23c242 | 6433 | { |
glebiuskv | 0:2f0e1e23c242 | 6434 | putbuff pb; |
glebiuskv | 0:2f0e1e23c242 | 6435 | |
glebiuskv | 0:2f0e1e23c242 | 6436 | |
glebiuskv | 0:2f0e1e23c242 | 6437 | putc_init(&pb, fp); |
glebiuskv | 0:2f0e1e23c242 | 6438 | while (*str) putc_bfd(&pb, *str++); /* Put the string */ |
glebiuskv | 0:2f0e1e23c242 | 6439 | return putc_flush(&pb); |
glebiuskv | 0:2f0e1e23c242 | 6440 | } |
glebiuskv | 0:2f0e1e23c242 | 6441 | |
glebiuskv | 0:2f0e1e23c242 | 6442 | |
glebiuskv | 0:2f0e1e23c242 | 6443 | |
glebiuskv | 0:2f0e1e23c242 | 6444 | |
glebiuskv | 0:2f0e1e23c242 | 6445 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 6446 | /* Put a Formatted String to the File */ |
glebiuskv | 0:2f0e1e23c242 | 6447 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 6448 | |
glebiuskv | 0:2f0e1e23c242 | 6449 | int f_printf ( |
glebiuskv | 0:2f0e1e23c242 | 6450 | FIL* fp, /* Pointer to the file object */ |
glebiuskv | 0:2f0e1e23c242 | 6451 | const TCHAR* fmt, /* Pointer to the format string */ |
glebiuskv | 0:2f0e1e23c242 | 6452 | ... /* Optional arguments... */ |
glebiuskv | 0:2f0e1e23c242 | 6453 | ) |
glebiuskv | 0:2f0e1e23c242 | 6454 | { |
glebiuskv | 0:2f0e1e23c242 | 6455 | va_list arp; |
glebiuskv | 0:2f0e1e23c242 | 6456 | putbuff pb; |
glebiuskv | 0:2f0e1e23c242 | 6457 | BYTE f, r; |
glebiuskv | 0:2f0e1e23c242 | 6458 | UINT i, j, w; |
glebiuskv | 0:2f0e1e23c242 | 6459 | DWORD v; |
glebiuskv | 0:2f0e1e23c242 | 6460 | TCHAR c, d, str[32], *p; |
glebiuskv | 0:2f0e1e23c242 | 6461 | |
glebiuskv | 0:2f0e1e23c242 | 6462 | |
glebiuskv | 0:2f0e1e23c242 | 6463 | putc_init(&pb, fp); |
glebiuskv | 0:2f0e1e23c242 | 6464 | |
glebiuskv | 0:2f0e1e23c242 | 6465 | va_start(arp, fmt); |
glebiuskv | 0:2f0e1e23c242 | 6466 | |
glebiuskv | 0:2f0e1e23c242 | 6467 | for (;;) { |
glebiuskv | 0:2f0e1e23c242 | 6468 | c = *fmt++; |
glebiuskv | 0:2f0e1e23c242 | 6469 | if (c == 0) break; /* End of string */ |
glebiuskv | 0:2f0e1e23c242 | 6470 | if (c != '%') { /* Non escape character */ |
glebiuskv | 0:2f0e1e23c242 | 6471 | putc_bfd(&pb, c); |
glebiuskv | 0:2f0e1e23c242 | 6472 | continue; |
glebiuskv | 0:2f0e1e23c242 | 6473 | } |
glebiuskv | 0:2f0e1e23c242 | 6474 | w = f = 0; |
glebiuskv | 0:2f0e1e23c242 | 6475 | c = *fmt++; |
glebiuskv | 0:2f0e1e23c242 | 6476 | if (c == '0') { /* Flag: '0' padding */ |
glebiuskv | 0:2f0e1e23c242 | 6477 | f = 1; c = *fmt++; |
glebiuskv | 0:2f0e1e23c242 | 6478 | } else { |
glebiuskv | 0:2f0e1e23c242 | 6479 | if (c == '-') { /* Flag: left justified */ |
glebiuskv | 0:2f0e1e23c242 | 6480 | f = 2; c = *fmt++; |
glebiuskv | 0:2f0e1e23c242 | 6481 | } |
glebiuskv | 0:2f0e1e23c242 | 6482 | } |
glebiuskv | 0:2f0e1e23c242 | 6483 | if (c == '*') { /* Minimum width by argument */ |
glebiuskv | 0:2f0e1e23c242 | 6484 | w = va_arg(arp, int); |
glebiuskv | 0:2f0e1e23c242 | 6485 | c = *fmt++; |
glebiuskv | 0:2f0e1e23c242 | 6486 | } else { |
glebiuskv | 0:2f0e1e23c242 | 6487 | while (IsDigit(c)) { /* Minimum width */ |
glebiuskv | 0:2f0e1e23c242 | 6488 | w = w * 10 + c - '0'; |
glebiuskv | 0:2f0e1e23c242 | 6489 | c = *fmt++; |
glebiuskv | 0:2f0e1e23c242 | 6490 | } |
glebiuskv | 0:2f0e1e23c242 | 6491 | } |
glebiuskv | 0:2f0e1e23c242 | 6492 | if (c == 'l' || c == 'L') { /* Type prefix: Size is long int */ |
glebiuskv | 0:2f0e1e23c242 | 6493 | f |= 4; c = *fmt++; |
glebiuskv | 0:2f0e1e23c242 | 6494 | } |
glebiuskv | 0:2f0e1e23c242 | 6495 | if (c == 0) break; |
glebiuskv | 0:2f0e1e23c242 | 6496 | d = c; |
glebiuskv | 0:2f0e1e23c242 | 6497 | if (IsLower(d)) d -= 0x20; |
glebiuskv | 0:2f0e1e23c242 | 6498 | switch (d) { /* Atgument type is... */ |
glebiuskv | 0:2f0e1e23c242 | 6499 | case 'S' : /* String */ |
glebiuskv | 0:2f0e1e23c242 | 6500 | p = va_arg(arp, TCHAR*); |
glebiuskv | 0:2f0e1e23c242 | 6501 | for (j = 0; p[j]; j++) ; |
glebiuskv | 0:2f0e1e23c242 | 6502 | if (!(f & 2)) { /* Right padded */ |
glebiuskv | 0:2f0e1e23c242 | 6503 | while (j++ < w) putc_bfd(&pb, ' ') ; |
glebiuskv | 0:2f0e1e23c242 | 6504 | } |
glebiuskv | 0:2f0e1e23c242 | 6505 | while (*p) putc_bfd(&pb, *p++) ; /* String body */ |
glebiuskv | 0:2f0e1e23c242 | 6506 | while (j++ < w) putc_bfd(&pb, ' ') ; /* Left padded */ |
glebiuskv | 0:2f0e1e23c242 | 6507 | continue; |
glebiuskv | 0:2f0e1e23c242 | 6508 | |
glebiuskv | 0:2f0e1e23c242 | 6509 | case 'C' : /* Character */ |
glebiuskv | 0:2f0e1e23c242 | 6510 | putc_bfd(&pb, (TCHAR)va_arg(arp, int)); continue; |
glebiuskv | 0:2f0e1e23c242 | 6511 | |
glebiuskv | 0:2f0e1e23c242 | 6512 | case 'B' : /* Unsigned binary */ |
glebiuskv | 0:2f0e1e23c242 | 6513 | r = 2; break; |
glebiuskv | 0:2f0e1e23c242 | 6514 | |
glebiuskv | 0:2f0e1e23c242 | 6515 | case 'O' : /* Unsigned octal */ |
glebiuskv | 0:2f0e1e23c242 | 6516 | r = 8; break; |
glebiuskv | 0:2f0e1e23c242 | 6517 | |
glebiuskv | 0:2f0e1e23c242 | 6518 | case 'D' : /* Signed decimal */ |
glebiuskv | 0:2f0e1e23c242 | 6519 | case 'U' : /* Unsigned decimal */ |
glebiuskv | 0:2f0e1e23c242 | 6520 | r = 10; break; |
glebiuskv | 0:2f0e1e23c242 | 6521 | |
glebiuskv | 0:2f0e1e23c242 | 6522 | case 'X' : /* Unsigned hexdecimal */ |
glebiuskv | 0:2f0e1e23c242 | 6523 | r = 16; break; |
glebiuskv | 0:2f0e1e23c242 | 6524 | |
glebiuskv | 0:2f0e1e23c242 | 6525 | default: /* Unknown type (pass-through) */ |
glebiuskv | 0:2f0e1e23c242 | 6526 | putc_bfd(&pb, c); continue; |
glebiuskv | 0:2f0e1e23c242 | 6527 | } |
glebiuskv | 0:2f0e1e23c242 | 6528 | |
glebiuskv | 0:2f0e1e23c242 | 6529 | /* Get an argument and put it in numeral */ |
glebiuskv | 0:2f0e1e23c242 | 6530 | v = (f & 4) ? (DWORD)va_arg(arp, long) : ((d == 'D') ? (DWORD)(long)va_arg(arp, int) : (DWORD)va_arg(arp, unsigned int)); |
glebiuskv | 0:2f0e1e23c242 | 6531 | if (d == 'D' && (v & 0x80000000)) { |
glebiuskv | 0:2f0e1e23c242 | 6532 | v = 0 - v; |
glebiuskv | 0:2f0e1e23c242 | 6533 | f |= 8; |
glebiuskv | 0:2f0e1e23c242 | 6534 | } |
glebiuskv | 0:2f0e1e23c242 | 6535 | i = 0; |
glebiuskv | 0:2f0e1e23c242 | 6536 | do { |
glebiuskv | 0:2f0e1e23c242 | 6537 | d = (TCHAR)(v % r); v /= r; |
glebiuskv | 0:2f0e1e23c242 | 6538 | if (d > 9) d += (c == 'x') ? 0x27 : 0x07; |
glebiuskv | 0:2f0e1e23c242 | 6539 | str[i++] = d + '0'; |
glebiuskv | 0:2f0e1e23c242 | 6540 | } while (v && i < sizeof str / sizeof *str); |
glebiuskv | 0:2f0e1e23c242 | 6541 | if (f & 8) str[i++] = '-'; |
glebiuskv | 0:2f0e1e23c242 | 6542 | j = i; d = (f & 1) ? '0' : ' '; |
glebiuskv | 0:2f0e1e23c242 | 6543 | if (!(f & 2)) { |
glebiuskv | 0:2f0e1e23c242 | 6544 | while (j++ < w) putc_bfd(&pb, d); /* Right pad */ |
glebiuskv | 0:2f0e1e23c242 | 6545 | } |
glebiuskv | 0:2f0e1e23c242 | 6546 | do { |
glebiuskv | 0:2f0e1e23c242 | 6547 | putc_bfd(&pb, str[--i]); /* Number body */ |
glebiuskv | 0:2f0e1e23c242 | 6548 | } while (i); |
glebiuskv | 0:2f0e1e23c242 | 6549 | while (j++ < w) putc_bfd(&pb, d); /* Left pad */ |
glebiuskv | 0:2f0e1e23c242 | 6550 | } |
glebiuskv | 0:2f0e1e23c242 | 6551 | |
glebiuskv | 0:2f0e1e23c242 | 6552 | va_end(arp); |
glebiuskv | 0:2f0e1e23c242 | 6553 | |
glebiuskv | 0:2f0e1e23c242 | 6554 | return putc_flush(&pb); |
glebiuskv | 0:2f0e1e23c242 | 6555 | } |
glebiuskv | 0:2f0e1e23c242 | 6556 | |
glebiuskv | 0:2f0e1e23c242 | 6557 | #endif /* !FF_FS_READONLY */ |
glebiuskv | 0:2f0e1e23c242 | 6558 | #endif /* FF_USE_STRFUNC */ |
glebiuskv | 0:2f0e1e23c242 | 6559 | |
glebiuskv | 0:2f0e1e23c242 | 6560 | |
glebiuskv | 0:2f0e1e23c242 | 6561 | |
glebiuskv | 0:2f0e1e23c242 | 6562 | #if FF_CODE_PAGE == 0 |
glebiuskv | 0:2f0e1e23c242 | 6563 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 6564 | /* Set Active Codepage for the Path Name */ |
glebiuskv | 0:2f0e1e23c242 | 6565 | /*-----------------------------------------------------------------------*/ |
glebiuskv | 0:2f0e1e23c242 | 6566 | |
glebiuskv | 0:2f0e1e23c242 | 6567 | FRESULT f_setcp ( |
glebiuskv | 0:2f0e1e23c242 | 6568 | WORD cp /* Value to be set as active code page */ |
glebiuskv | 0:2f0e1e23c242 | 6569 | ) |
glebiuskv | 0:2f0e1e23c242 | 6570 | { |
glebiuskv | 0:2f0e1e23c242 | 6571 | static const WORD validcp[] = { 437, 720, 737, 771, 775, 850, 852, 857, 860, 861, 862, 863, 864, 865, 866, 869, 0}; |
glebiuskv | 0:2f0e1e23c242 | 6572 | static const BYTE *const tables[] = {Ct437, Ct720, Ct737, Ct771, Ct775, Ct850, Ct852, Ct857, Ct860, Ct861, Ct862, Ct863, Ct864, Ct865, Ct866, Ct869, 0}; |
glebiuskv | 0:2f0e1e23c242 | 6573 | UINT i; |
glebiuskv | 0:2f0e1e23c242 | 6574 | |
glebiuskv | 0:2f0e1e23c242 | 6575 | |
glebiuskv | 0:2f0e1e23c242 | 6576 | for (i = 0; validcp[i] != 0 && validcp[i] != cp; i++) ; /* Find the code page */ |
glebiuskv | 0:2f0e1e23c242 | 6577 | if (validcp[i] != cp) return FR_INVALID_PARAMETER; /* Not found? */ |
glebiuskv | 0:2f0e1e23c242 | 6578 | |
glebiuskv | 0:2f0e1e23c242 | 6579 | CodePage = cp; |
glebiuskv | 0:2f0e1e23c242 | 6580 | if (cp >= 900) { /* DBCS */ |
glebiuskv | 0:2f0e1e23c242 | 6581 | ExCvt = 0; |
glebiuskv | 0:2f0e1e23c242 | 6582 | DbcTbl = tables[i]; |
glebiuskv | 0:2f0e1e23c242 | 6583 | } else { /* SBCS */ |
glebiuskv | 0:2f0e1e23c242 | 6584 | ExCvt = tables[i]; |
glebiuskv | 0:2f0e1e23c242 | 6585 | DbcTbl = 0; |
glebiuskv | 0:2f0e1e23c242 | 6586 | } |
glebiuskv | 0:2f0e1e23c242 | 6587 | return FR_OK; |
glebiuskv | 0:2f0e1e23c242 | 6588 | } |
glebiuskv | 0:2f0e1e23c242 | 6589 | #endif /* FF_CODE_PAGE == 0 */ |
glebiuskv | 0:2f0e1e23c242 | 6590 |