Rev 24 | Rev 88 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 24 | Rev 41 | ||
---|---|---|---|
Line 52... | Line 52... | ||
52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
55 | // + POSSIBILITY OF SUCH DAMAGE. |
55 | // + POSSIBILITY OF SUCH DAMAGE. |
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | 57 | #include <stdio.h> |
|
- | 58 | #include "91x_lib.h" |
|
- | 59 | #include "timer.h" |
|
- | 60 | #include "fat16.h" |
|
- | 61 | #include "sdc.h" |
|
- | 62 | #include "uart1.h" |
|
Line 57... | Line -... | ||
57 | - | ||
Line 58... | Line 63... | ||
58 | #include "main.h" |
63 | |
59 | 64 | ||
60 | //________________________________________________________________________________________________________________________________________ |
65 | //________________________________________________________________________________________________________________________________________ |
61 | // Module name: fat16.c |
66 | // Module name: fat16.c |
62 | // Compiler used: avr-gcc 3.4.5 |
67 | // Compiler used: avr-gcc 3.4.5 |
63 | // Last Modifikation: 21.04.2008 |
68 | // Last Modifikation: 16.06.2008 |
64 | // Version: 1.28 |
69 | // Version: 2.00 |
65 | // Authors: Stephan Busker |
70 | // Authors: Stephan Busker & Gregor Stobrawa |
66 | // Description: Source files for FAT16 implementation with read and write-access using AVR-Mikrocontrollers |
71 | // Description: Source files for FAT16 implementation with read and write-access |
67 | // Copyright (C) 2007 Stephan Busker |
72 | // Copyright (C) 2008 Stephan Busker & Gregor Stobrawa |
68 | //........................................................................................................................................ |
73 | //........................................................................................................................................ |
69 | // Functions: extern u8 InitFat16(void); |
74 | // Functions: extern s16 Fat16_Init(void); |
70 | // u8 fopen_(s8 *fname,s8 mode, File *file); |
75 | // extern s16 Fat16_Deinit(void); |
71 | // void fclose_(File *file); |
76 | // extern File_t * fopen_(const u8 *filename, const s8 mode); |
72 | // u32 fread_(void *buffer, u32 size, u32 count, File *file); |
77 | // extern s16 fclose_(File_t *File); |
73 | // u32 fwrite_(void *buffer, u32 size, u32 count, File *file); |
- | |
74 | // s16 fseek_(File *file, s32 offset, s16 origin); |
78 | // extern s16 fflush_(File_t *File); |
75 | // s8 fgets_(s8 *string, s16 count, File *file); |
79 | // extern s16 fseek_(File_t *File, s32 offset, s16 origin); |
76 | // u8 fputchar_(File *file,s8 c); |
80 | // extern s16 fgetc_(File_t *File); |
77 | // u8 fputs_(File *file,s8 *string); |
- | |
78 | // s8 * fgets(s8 *, s16, File); |
- | |
79 | // u8 fexist_(u8*); |
- | |
80 | // u8 mkdir__(s8 *name); |
81 | // extern s16 fputc_(u8 c, File_t *File); |
81 | // u8 chdir__(s8 *name); |
82 | // extern u32 fread_(void *buffer, u32 size, u32 count, File_t *File); |
82 | // u8 findfirst_(s8 *fname, Find *item, u8 attribute); |
83 | // extern u32 fwrite_(void *buffer, u32 size, u32 count, File_t *File); |
83 | // u8 findnext_(Find *item); |
- | |
84 | // u8 fdelete_(s8 *fname) |
- | |
85 | // u8 rmdir_(s8 *dname) |
84 | // extern s16 fputs_(const u8 *string, File_t *File); |
86 | // u16 feof_(File); |
85 | // extern u8 * fgets_(u8 *string, s16 length, File_t *File); |
- | 86 | //........................................................................................................................................ |
|
- | 87 | // ext. functions: extern SD_Result_t SDC_Init(void;) |
|
87 | //........................................................................................................................................ |
88 | // extern SD_Result_t SDC_Deinit(void); |
88 | // ext. functions: extern u8 SDC_GetSector (u32,u8 *); |
89 | // extern SD_Result_t SDC_GetSector (u32,u8 *); |
89 | // extern u8 SDC_PutSector (u32,u8 *); |
90 | // extern SD_Result_t SDC_PutSector (u32,u8 *); |
90 | //........................................................................................................................................ |
91 | //........................................................................................................................................ |
91 | // |
92 | // |
92 | // URL: www.Mikro-Control.de |
93 | // URL: www.Mikro-Control.de |
Line -... | Line 94... | ||
- | 94 | // mailto: stephan.busker@mikro-control.de |
|
- | 95 | //________________________________________________________________________________________________________________________________________ |
|
- | 96 | ||
- | 97 | /* |
|
- | 98 | FAT16 Drive Layout: |
|
- | 99 | Description Offset |
|
- | 100 | Volume Boot Sector Start of Partition |
|
- | 101 | Fat Tables Start + # of Reserved Sectors |
|
93 | // mailto: stephan.busker@mikro-control.de |
102 | Root Directory Entry Start + # of Reserved + (# of Sectors Per FAT * 2) |
- | 103 | Data Area (Starts with Cluster #2) Start + # of Reserved + (# of Sectors Per FAT * 2) + ((Maximum Root Directory Entries * 32) / Bytes per Sector) |
|
- | 104 | */ |
|
- | 105 | ||
94 | //________________________________________________________________________________________________________________________________________ |
106 | |
- | 107 | /* |
|
95 | 108 | ________________________________________________________________________________________________________________________________________ |
|
- | 109 | |
|
- | 110 | Structure of an partition entry |
|
- | 111 | ________________________________________________________________________________________________________________________________________ |
|
- | 112 | ||
- | 113 | Partition Entry is 16 bytes long |
|
- | 114 | */ |
|
- | 115 | typedef struct |
|
- | 116 | { |
|
- | 117 | u8 PartitionState; // Current State of Partition (00h=Inactive, 80h=Active) |
|
- | 118 | u8 BeginningHead; // Beginning of Partition - Head |
|
- | 119 | u16 BeginningCylSec; // Beginning of Partition - Cylinder/Sector (See Below) |
|
- | 120 | u8 Type; // Type of Partition (See List Below) |
|
- | 121 | u8 EndHead; // End of Partition - Head |
|
- | 122 | u16 EndCylSec; // End of Partition - Cylinder/Sector |
|
- | 123 | u32 NoSectorsBeforePartition; // Number of Sectors between the MBR and the First Sector in the Partition |
|
- | 124 | u32 NoSectorsPartition ; // Number of Sectors in the Partition |
|
- | 125 | } __attribute__((packed)) PartitionEntry_t; |
|
- | 126 | ||
- | 127 | /* |
|
- | 128 | Coding of Cylinder/Sector words |
|
- | 129 | ||
- | 130 | Cylinder is 10 bits: [7:0] at [15:8] and [9:8] at [7:6] |
|
- | 131 | Sector is 5 bits: [5:0] at [5:0] |
|
- | 132 | */ |
|
- | 133 | ||
- | 134 | // Partition Types: |
|
- | 135 | #define PART_TYPE_UNKNOWN 0x00 |
|
- | 136 | #define PART_TYPE_FAT12 0x01 |
|
- | 137 | #define PART_TYPE_XENIX 0x02 |
|
- | 138 | #define PART_TYPE_FAT16_ST_32_MB 0x04 |
|
- | 139 | #define PART_TYPE_EXTDOS 0x05 |
|
- | 140 | #define PART_TYPE_FAT16_LT_32_MB 0x06 |
|
- | 141 | #define PART_TYPE_NTFS 0x07 |
|
- | 142 | #define PART_TYPE_FAT32 0x0B |
|
- | 143 | #define PART_TYPE_FAT32LBA 0x0C |
|
- | 144 | #define PART_TYPE_FAT16LBA 0x0E |
|
- | 145 | #define PART_TYPE_EXTDOSLBA 0x0F |
|
- | 146 | #define PART_TYPE_EISA 0x12 |
|
- | 147 | #define PART_TYPE_ONTRACK 0x33 |
|
- | 148 | #define PART_TYPE_NOVELL 0x40 |
|
- | 149 | #define PART_TYPE_DYNAMIC 0x42 |
|
- | 150 | #define PART_TYPE_PCIX 0x4B |
|
- | 151 | #define PART_TYPE_LINUX_SWAP 0x82 |
|
- | 152 | #define PART_TYPE_LINUX_NATIVE 0x83 |
|
- | 153 | #define PART_TYPE_LINUX_LVM 0x8E |
|
- | 154 | #define PART_TYPE_PHOENIXSAVE 0xA0 |
|
- | 155 | #define PART_TYPE_FREEBSD 0xA5 |
|
- | 156 | #define PART_TYPE_OPENBSD 0xA6 |
|
- | 157 | #define PART_TYPE_NETNBSD 0xA9 |
|
- | 158 | #define PART_TYPE_CPM 0xDB |
|
- | 159 | #define PART_TYPE_DBFS 0xE0 |
|
96 | 160 | #define PART_TYPE_BBT 0xFF |
|
- | 161 | ||
- | 162 | ||
- | 163 | /* |
|
- | 164 | ________________________________________________________________________________________________________________________________________ |
|
- | 165 | |
|
- | 166 | Structure of the MasterBootRecord |
|
97 | 167 | ________________________________________________________________________________________________________________________________________ |
|
- | 168 | ||
- | 169 | Master Boot Record is 512 bytes long |
|
- | 170 | The Master Boot Record is the same for pretty much all Operating Systems. |
|
- | 171 | It is located on the first Sector of the Hard Drive, at Cylinder 0, Head 0, Sector 1 |
|
- | 172 | */ |
|
- | 173 | typedef struct |
|
- | 174 | { |
|
- | 175 | u8 ExecutableCode[446]; // 446 bytes for machine start code |
|
- | 176 | PartitionEntry_t PartitionEntry1; // 16 bytes for partition entry 1 |
|
- | 177 | PartitionEntry_t PartitionEntry2; // 16 bytes for partition entry 2 |
|
- | 178 | PartitionEntry_t PartitionEntry3; // 16 bytes for partition entry 3 |
|
- | 179 | PartitionEntry_t PartitionEntry4; // 16 bytes for partition entry 4 |
|
- | 180 | u16 ExecutableMarker; // BIOS-Signature (0x55 0xAA) |
|
98 | //________________________________________________________________________________________________________________________________________ |
181 | } __attribute__((packed)) MBR_Entry_t; |
99 | // |
182 | |
- | 183 | ||
- | 184 | /* |
|
- | 185 | ________________________________________________________________________________________________________________________________________ |
|
100 | // Global variables needed for read- or write-acces to the FAT16- filesystem. |
186 | |
- | 187 | Structure of the VolumeBootRecord |
|
- | 188 | ________________________________________________________________________________________________________________________________________ |
|
- | 189 | ||
- | 190 | The Volume Boot Record is 512 bytes long |
|
- | 191 | This information is located in the first sector of every partition. |
|
- | 192 | */ |
|
- | 193 | typedef struct |
|
- | 194 | { |
|
101 | // |
195 | u8 JumpCode[3]; // Jump Code + NOP |
- | 196 | s8 OEMName[8]; // OEM Name |
|
102 | //________________________________________________________________________________________________________________________________________ |
197 | u16 BytesPerSector; // Bytes Per Sector |
103 | 198 | u8 SectorsPerCluster; // Sectors Per Cluster |
|
104 | u8 SectorsPerCluster = 0; // how many sectors does a cluster contain? |
199 | u16 ReservedSectors; // Reserved Sectors |
- | 200 | u8 NoFATCopies; // Number of Copies of FAT |
|
- | 201 | u16 MaxRootEntries; // Maximum Root Directory Entries |
|
- | 202 | u16 NoSectorsInPartSml32MB; // Number of Sectors in Partition Smaller than 32 MB |
|
- | 203 | u8 MediaDescriptor; // Media Descriptor (0xF8 for Hard Disks) |
|
105 | u8 FatCopies = 0; // Numbers of copies of the FAT |
204 | u16 SectorsPerFAT; // Sectors Per FAT |
106 | u16 PossibleRootEntries = 0; // Possible number of entries in the root directory. |
205 | u16 SectorsPerTrack; // Sectors Per Track |
- | 206 | u16 NoHeads; // Number of Heads |
|
- | 207 | u32 NoHiddenSectors; // Number of Hidden Sectors in Partition |
|
107 | u16 SectorsPerFat = 0; // how many sectors does a fat16 contain? |
208 | u32 NoSectors; // Number of Sectors in Partition |
108 | u32 ReservedSectors = 0; // Sectors reserved by the filesystem. |
209 | u16 DriveNo; // Logical Drive Number of Partition |
- | 210 | u8 ExtendedSig; // Extended Signature (0x29) |
|
109 | u32 FirstPartitionSector = 0; // Distance in sectors between the first partition and the master bootrecord. |
211 | u32 SerialNo; // Serial Number of the Partition |
110 | u32 FileAllocationTable = 0; // pointer to the first FAT |
212 | s8 VolumeName[11]; // Volume Name of the Partititon |
- | 213 | s8 FATName[8]; // FAT Name (FAT16) |
|
- | 214 | u8 ExecutableCode[446]; // 446 bytes for machine start code |
|
- | 215 | u16 ExecutableMarker; // Executable Marker (0x55 0xAA) |
|
- | 216 | } __attribute__((packed)) VBR_Entry_t; |
|
- | 217 | ||
- | 218 | ||
111 | u32 RootDirectory = 0; // Pointer to the rootdirectory of the first partition. |
219 | |
- | 220 | /* |
|
- | 221 | ________________________________________________________________________________________________________________________________________ |
|
112 | u32 FirstDataCluster = 0; // Pointer to the first cluster containing data (cluster0). |
222 | |
- | 223 | Structure of an directory entry |
|
- | 224 | ________________________________________________________________________________________________________________________________________ |
|
- | 225 | ||
- | 226 | Directory entry is 32 bytes. |
|
- | 227 | */ |
|
- | 228 | typedef struct |
|
- | 229 | { |
|
- | 230 | s8 Name[8]; // 8 bytes name, padded with spaces. |
|
113 | u32 CWD = 0; // Pointer startcluster to the current working directory |
231 | u8 Extension[3]; // 3 bytes extension, padded with spaces. |
114 | 232 | u8 Attribute; // attribute of the directory entry (unused,archive,read-only,system,directory,volume) |
|
- | 233 | u8 Reserved[10]; // reserved bytes within the directory entry. |
|
- | 234 | u32 DateTime; // date and time of last write access to the file or directory. |
|
- | 235 | u16 StartCluster; // first cluster of the file or directory. |
|
- | 236 | u32 Size; // size of the file or directory in bytes. |
|
- | 237 | } __attribute__((packed)) DirEntry_t; |
|
- | 238 | ||
115 | 239 | #define SLOT_EMPTY 0x00 // slot has never been used |
|
- | 240 | #define SLOT_E5 0x05 // the real value is 0xe5 |
|
- | 241 | #define SLOT_DELETED 0xE5 // file in this slot deleted |
|
- | 242 | ||
- | 243 | #define ATTR_NONE 0x00 // normal file |
|
- | 244 | #define ATTR_READONLY 0x01 // file is readonly |
|
116 | struct DirEntry *DirectoryEntry; // Pointer to an entry of the directory. |
245 | #define ATTR_HIDDEN 0x02 // file is hidden |
- | 246 | #define ATTR_SYSTEM 0x04 // file is a system file |
|
- | 247 | #define ATTR_VOLUMELABEL 0x08 // entry is a volume label |
|
117 | struct FatEntry *Fat; // Pointer to an entry of the fat (next clusterposition). |
248 | #define ATTR_LONG_FILENAME 0x0F // this is a long filename entry |
118 | 249 | #define ATTR_SUBDIRECTORY 0x10 // entry is a directory name |
|
- | 250 | #define ATTR_ARCHIVE 0x20 // file is new or modified |
|
119 | File FilePointer[__MAX_FILES_USED]; // Allocate Memmoryspace for each filepointer used. |
251 | |
- | 252 | ||
120 | 253 | /* |
|
- | 254 | ________________________________________________________________________________________________________________________________________ |
|
121 | 255 | |
|
- | 256 | Structure of an entry within the fileallocationtable. |
|
- | 257 | ________________________________________________________________________________________________________________________________________ |
|
- | 258 | */ |
|
- | 259 | typedef struct |
|
- | 260 | { |
|
- | 261 | u16 NextCluster; // the next cluster of the file. |
|
- | 262 | } __attribute__((packed)) Fat16Entry_t; |
|
- | 263 | ||
- | 264 | // secial fat entries |
|
- | 265 | #define FAT16_CLUSTER_FREE 0x0000 |
|
- | 266 | #define FAT16_CLUSTER_RESERVED 0x0001 |
|
- | 267 | #define FAT16_CLUSTER_USED_MIN 0x0002 |
|
- | 268 | #define FAT16_CLUSTER_USED_MAX 0xFFEF |
|
- | 269 | #define FAT16_CLUSTER_ROOTDIR_MIN 0xFFF0 |
|
- | 270 | #define FAT16_CLUSTER_ROOTDIR_MAX 0xFFF6 |
|
- | 271 | #define FAT16_CLUSTER_BAD 0xFFF7 |
|
- | 272 | #define FAT16_CLUSTER_LAST_MIN 0xFFF8 |
|
- | 273 | #define FAT16_CLUSTER_LAST_MAX 0xFFFF |
|
122 | //________________________________________________________________________________________________________________________________________ |
274 | |
- | 275 | /*****************************************************************************************************************************************/ |
|
- | 276 | /* */ |
|
- | 277 | /* Global variables needed for read- or write-acces to the FAT16- filesystem. */ |
|
123 | // Funtion: InitFat16(void); |
278 | /* */ |
- | 279 | /*****************************************************************************************************************************************/ |
|
- | 280 | ||
- | 281 | #define MBR_SECTOR 0x00 // the masterboot record is located in sector 0. |
|
- | 282 | #define DIRENTRY_SIZE 32 //bytes |
|
- | 283 | #define DIRENTRIES_PER_SECTOR BYTES_PER_SECTOR/DIRENTRY_SIZE |
|
- | 284 | #define FAT16_BYTES 2 |
|
- | 285 | #define FAT16_ENTRIES_PER_SECTOR BYTES_PER_SECTOR/FAT16_BYTES |
|
- | 286 | ||
- | 287 | #define FSTATE_UNUSED 0 |
|
124 | // |
288 | #define FSTATE_USED 1 |
- | 289 | ||
- | 290 | typedef struct |
|
- | 291 | { |
|
- | 292 | u8 IsValid; // 0 means invalid, else valid |
|
- | 293 | u8 SectorsPerCluster; // how many sectors does a cluster contain? |
|
- | 294 | u8 FatCopies; // Numbers of copies of the FAT |
|
- | 295 | u16 MaxRootEntries; // Possible number of entries in the root directory. |
|
- | 296 | u16 SectorsPerFat; // how many sectors does a fat16 contain? |
|
- | 297 | u32 FirstFatSector; // sector of the start of the fat |
|
- | 298 | u32 FirstRootDirSector; // sector of the rootdirectory |
|
- | 299 | u32 FirstDataSector; // sector of the first cluster containing data (cluster2). |
|
- | 300 | u32 LastDataSector; // the last data sector of the partition |
|
- | 301 | } Partition_t; |
|
- | 302 | ||
- | 303 | Partition_t Partition; // Structure holds partition information |
|
- | 304 | ||
- | 305 | File_t FilePointer[FILE_MAX_OPEN]; // Allocate Memmoryspace for each filepointer used. |
|
- | 306 | ||
- | 307 | ||
125 | // Description: This function reads the Masterbootrecord and finds the position of the Volumebootrecord, the FAT and the Rootdirectory |
308 | /****************************************************************************************************************************************/ |
- | 309 | /* Function: FileDateTime(DateTime_t *); */ |
|
- | 310 | /* */ |
|
126 | // and stores the information in global variables. |
311 | /* Description: This function calculates the DOS date time from a pointer to a time structure. */ |
127 | // |
312 | /* */ |
- | 313 | /* Returnvalue: Returns the DOS date time. */ |
|
- | 314 | /****************************************************************************************************************************************/ |
|
128 | // Returnvalue: The function returns "0" if the filesystem could not be initialized because no partition was found on the volume. |
315 | |
- | 316 | u32 FileDateTime(DateTime_t * pTimeStruct) |
|
- | 317 | { |
|
- | 318 | u32 datetime = 0; |
|
- | 319 | if((pTimeStruct == 0) || !(pTimeStruct->Valid)) return datetime; |
|
- | 320 | ||
- | 321 | datetime |= (0x0000007FL & (u32)(pTimeStruct->Year - 1980))<<25; // set year |
|
- | 322 | datetime |= (0x0000000FL & (u32)(pTimeStruct->Month))<<21; // set month |
|
- | 323 | datetime |= (0x0000001FL & (u32)(pTimeStruct->Day))<<16; |
|
- | 324 | datetime |= (0x0000001FL & (u32)(pTimeStruct->Hour))<<11; |
|
- | 325 | datetime |= (0x0000003FL & (u32)(pTimeStruct->Min))<<5; |
|
- | 326 | datetime |= (0x0000001FL & (u32)(pTimeStruct->Sec/2)); |
|
- | 327 | return datetime; |
|
- | 328 | } |
|
- | 329 | ||
- | 330 | ||
- | 331 | /****************************************************************************************************************************************/ |
|
- | 332 | /* Function: LockFilePointer(); */ |
|
- | 333 | /* */ |
|
- | 334 | /* Description: This function trys to lock a free file pointer. */ |
|
- | 335 | /* */ |
|
- | 336 | /* Returnvalue: Returns the Filepointer on success or 0. */ |
|
- | 337 | /****************************************************************************************************************************************/ |
|
- | 338 | File_t * LockFilePointer(void) |
|
- | 339 | { |
|
129 | //________________________________________________________________________________________________________________________________________ |
340 | u8 i; |
130 | - | ||
131 | u8 InitFat16(void) |
- | |
132 | { |
- | |
133 | u8 retvalue = 0; |
341 | File_t * File = 0; |
134 | u8 cnt = 0; |
- | |
135 | struct VBR_Entry *VBR; // Pointer to the VolumeBootRecord. |
- | |
136 | struct MBR_Entry *MBR; // Pointer to the masterbootrecord. |
- | |
137 | File *file; |
342 | for(i = 0; i < FILE_MAX_OPEN; i++) |
- | 343 | { |
|
138 | 344 | if(FilePointer[i].State == FSTATE_UNUSED) // found an unused one |
|
- | 345 | { |
|
139 | for(cnt=0;cnt<__MAX_FILES_USED;cnt++) |
346 | File = &FilePointer[i]; // set pointer to that entry |
140 | { |
- | |
141 | FilePointer[cnt].state = _UNUSED; // declare the filepointers as unused. |
- | |
142 | } |
- | |
143 | - | ||
144 | file = &FilePointer[0]; |
- | |
145 | - | ||
146 | SerialPutString("\n\rFAT16 init..."); |
- | |
147 | while((SDC_Init() != SD_SUCCESS) && (cnt++<100)); |
- | |
148 | - | ||
149 | if(cnt <100) // sdcard initialised succesfully |
- | |
150 | { |
- | |
151 | SDC_GetSector((u32)_MBR_SECTOR,file->buffer); // Read the MasterBootRecord from mmc. |
- | |
152 | MBR = (struct MBR_Entry *) file->buffer; |
- | |
153 | FirstPartitionSector = MBR->PartitionEntry1.NoSectorsBeforePartition; |
- | |
154 | if((MBR->PartitionEntry1.Type == _FAT16_32_MB_BIOS_Extension) || |
- | |
155 | (MBR->PartitionEntry1.Type == _FAT16_ST_32_MB) || |
- | |
156 | (MBR->PartitionEntry1.Type == _FAT16_LT_32_MB)) |
- | |
157 | { |
- | |
158 | SDC_GetSector(FirstPartitionSector,file->buffer); // Read the volume bootrecord from mmc. |
- | |
159 | - | ||
160 | VBR = (struct VBR_Entry *) file->buffer; // Enter the VBR using the structure VBR_Entry. |
- | |
161 | SectorsPerCluster = VBR->SectorsPerCluster; // Number of sectors per cluster. Depends on the memorysize of the sd-card. |
- | |
162 | FatCopies = VBR->NoFATCopies; // Number of fatcopies. |
- | |
163 | PossibleRootEntries = VBR->MaxRootEntries; // How many Entries are possible in the rootdirectory (FAT16 allows max. 512 entries). |
- | |
164 | SectorsPerFat = VBR->SectorsPerFAT; // The number of sectors per FAT. |
- | |
165 | ReservedSectors = VBR->ReservedSectors; // calculate the sectorpositon of the FAT, the Rootdirectory and the first Datacluster. |
- | |
166 | - | ||
167 | FileAllocationTable = (u32)(FirstPartitionSector + (u32)ReservedSectors); // Calculate the position of the FileAllocationTable. |
- | |
168 | RootDirectory = (u32)((u32)FileAllocationTable + (u32)((u32)SectorsPerFat*(u32)FatCopies)); // Calculate the position of the Rootdirectory. |
- | |
169 | FirstDataCluster = (u32)((u32)RootDirectory + ((u32)(PossibleRootEntries>>4))); // Calculate the position of the first datacluster. |
- | |
170 | CWD = RootDirectory; // The actual directory is the rootdirectory. |
- | |
171 | retvalue = 1; |
347 | FilePointer[i].State = FSTATE_USED; // mark it as used |
172 | SerialPutString("\n\rfilesystem ok"); |
348 | break; |
173 | str_Volume.state = INITIALIZED; |
- | |
174 | } |
- | |
175 | else |
- | |
176 | { |
349 | } |
- | 350 | } |
|
- | 351 | return(File); |
|
- | 352 | } |
|
- | 353 | ||
- | 354 | /****************************************************************************************************************************************/ |
|
- | 355 | /* Function: UnlockFilePointer(file_t *); */ |
|
- | 356 | /* */ |
|
- | 357 | /* Description: This function trys to unlock a file pointer. */ |
|
- | 358 | /* */ |
|
- | 359 | /* Returnvalue: Returns 1 if file pointer was freed else 0. */ |
|
- | 360 | /****************************************************************************************************************************************/ |
|
- | 361 | u8 UnlockFilePointer(File_t * file) |
|
- | 362 | { |
|
- | 363 | u8 cnt; |
|
- | 364 | if(file == NULL) return(0); |
|
- | 365 | for(cnt = 0; cnt < FILE_MAX_OPEN; cnt++) |
|
- | 366 | { |
|
- | 367 | if(&FilePointer[cnt] == file) // filepointer to be freed found? |
|
- | 368 | { |
|
- | 369 | file->State = FSTATE_UNUSED; |
|
- | 370 | file->FirstSectorOfFirstCluster = 0; // Sectorpointer to the first sector of the first datacluster of the file. |
|
- | 371 | file->FirstSectorOfCurrCluster = 0; |
|
- | 372 | file->SectorOfCurrCluster = 0; // Pointer to the cluster which is edited at the moment. |
|
- | 373 | file->SectorOfCurrCluster = 0; // The sector which is edited at the moment (cluster_pointer + sector_index). |
|
- | 374 | file->ByteOfCurrSector = 0; // The bytelocation within the current sector (cluster_pointer + sector_index + byte_index). |
|
- | 375 | file->Mode = 0; // mode of fileoperation (read,write) |
|
- | 376 | file->Size = 0; // the size of the opend file in bytes. |
|
- | 377 | file->Position = 0; // pointer to a character within the file 0 < fileposition < filesize |
|
- | 378 | file->SectorInCache = 0; // the last sector read, wich is still in the sectorbuffer. |
|
- | 379 | file->DirectorySector = 0; // the sectorposition where the directoryentry has been made. |
|
- | 380 | file->DirectoryIndex = 0; // the index to the directoryentry within the specified sector. |
|
177 | str_Volume.state = NOT_INITIALIZED; |
381 | file->Attribute = 0; // the attribute of the file opened. |
- | 382 | file = NULL; |
|
178 | SerialPutString("\n\rfilesystem nok"); |
383 | return(1); |
179 | } |
384 | } |
Line 180... | Line 385... | ||
180 | } |
385 | } |
181 | return(retvalue); |
386 | return(0); |
182 | } |
- | |
- | 387 | } |
|
183 | 388 | ||
- | 389 | /****************************************************************************************************************************************/ |
|
184 | //________________________________________________________________________________________________________________________________________ |
390 | /* Function: SeperateDirName(s8*, s8*); */ |
185 | // Funtion: File * fopen_(u8*, u8 *); |
- | |
186 | // |
391 | /* */ |
187 | // Description: This function looks for the file to be opened in the directory specified by the variable "CWD" of the drive. |
- | |
- | 392 | /* Description: This function seperates the first dirname from filepath and brings them */ |
|
188 | // If the file was found this function returns a filepoiner to the opened file otherwise NULL// |
393 | /* into the needed format ('test.txt' -> 'TEST TXT') */ |
189 | // |
- | |
190 | // Return: Pointer to the opened file or NULL |
394 | /* The subpath is the pointer to the remaining substring if the filepath */ |
191 | // |
395 | /* */ |
- | 396 | /* Returnvalue: Return NULL on error or pointer to subpath */ |
|
192 | //________________________________________________________________________________________________________________________________________ |
397 | /****************************************************************************************************************************************/ |
193 | - | ||
194 | File * fopen_(s8 *fname, s8 mode) |
398 | s8* SeperateDirName(const s8 *filepath, s8 *dirname) |
Line -... | Line 399... | ||
- | 399 | { |
|
- | 400 | s8* subpath = NULL; |
|
- | 401 | u8 readpointer = 0; |
|
- | 402 | u8 writepointer = 0; |
|
195 | { |
403 | |
196 | File *file; |
404 | // search subpath from beginning of filepath |
197 | 405 | subpath = NULL; |
|
- | 406 | readpointer = 0; |
|
- | 407 | if(filepath[0] == '/') readpointer = 1; // ignore first '/' |
|
- | 408 | while(subpath == NULL) // search the filepath until a subpath was found. |
|
- | 409 | { |
|
- | 410 | if(((filepath[readpointer] == 0) || (filepath[readpointer] == '/'))) // if '/' found or end of filepath reached |
|
Line -... | Line 411... | ||
- | 411 | { |
|
- | 412 | subpath = (s8*)&filepath[readpointer]; // store the position of the first "/" found after the beginning of the filenpath |
|
- | 413 | } |
|
- | 414 | readpointer++; |
|
- | 415 | } |
|
- | 416 | ||
- | 417 | // clear dirname with spaces |
|
- | 418 | dirname[11] = 0; // terminate dirname |
|
- | 419 | for(writepointer = 0; writepointer < 11; writepointer++) dirname[writepointer] = ' '; |
|
198 | file = ReserveFilePointer(); // reserve a filepointer. |
420 | writepointer = 0; |
- | 421 | // start seperating the dirname from the filepath. |
|
199 | 422 | readpointer = 0; |
|
200 | if(file != NULL) // A free filepointer was found. |
423 | if(filepath[0] == '/') readpointer = 1; // ignore first '/' |
201 | { |
- | |
202 | file->mode = mode; // mode of fileoperation (read,write) |
424 | while( &filepath[readpointer] < subpath) |
- | 425 | { |
|
203 | 426 | if(writepointer >= 11) return(NULL); // dirname to long |
|
204 | if(SeekFileInDirectory(fname, file)) // if file was found |
427 | if(filepath[readpointer] == '.') // seperating dirname and extension. |
- | 428 | { |
|
205 | { |
429 | if(writepointer <= 8) |
206 | SerialPutString("ok"); |
430 | { |
207 | if(mode == 'a') // open existing file for writing (append data at the end of the file) |
431 | readpointer++; // next character in filename |
208 | { |
- | |
209 | fseek_(file, 0, SEEK_END); // fseek points to the end of the file |
- | |
210 | } |
432 | writepointer = 8; // jump to start of extension |
211 | } |
433 | } |
212 | else |
434 | else return(NULL); // dirbasename to long |
213 | { |
435 | } |
214 | if((mode == 'a') || (mode == 'w')) // specified file doesn't exist so create new file for writing data. |
436 | else |
215 | { |
437 | { |
216 | if(CreateFileInDirectory(fname,file)) // Could an entry for the new file in the rootdirectory be created? |
438 | if((0x60 < filepath[readpointer]) && (filepath[readpointer] < 0x7B)) |
217 | { |
- | |
218 | return(file); |
- | |
219 | } |
- | |
220 | else |
- | |
221 | { |
- | |
222 | FreeFilePointer(file); // free the filepointer. |
- | |
223 | file = NULL; |
- | |
224 | } |
439 | { |
- | 440 | dirname[writepointer] = (filepath[readpointer] - 0x20); // all characters must be upper case. |
|
- | 441 | } |
|
225 | } |
442 | else |
226 | else // file with mode 'r' not found |
443 | { |
227 | { |
444 | dirname[writepointer] = filepath[readpointer]; |
228 | FreeFilePointer(file); // free the filepointer. |
445 | } |
Line -... | Line 446... | ||
- | 446 | readpointer++; |
|
- | 447 | writepointer++; |
|
- | 448 | } |
|
- | 449 | } |
|
- | 450 | return(subpath); |
|
- | 451 | } |
|
- | 452 | ||
- | 453 | ||
- | 454 | /**************************************************************************************************************************************+*/ |
|
- | 455 | /* Function: Fat16ClusterToSector( u16 cluster); */ |
|
- | 456 | /* */ |
|
- | 457 | /* Description: This function converts a cluster number given by the fat to the corresponding */ |
|
- | 458 | /* sector that points to the start of the data area that is represented by the cluster number. */ |
|
- | 459 | /* */ |
|
Line 229... | Line 460... | ||
229 | file = NULL; // file not found |
460 | /* Returnvalue: The sector number with the data area of the given cluster */ |
- | 461 | /****************************************************************************************************************************************/ |
|
230 | } |
462 | u32 Fat16ClusterToSector(u16 cluster) |
231 | } |
- | |
232 | } |
463 | { |
233 | return(file); |
- | |
- | 464 | if(!Partition.IsValid) return 0; |
|
- | 465 | if (cluster < 2) cluster = 2; // the 0. and 1. cluster in the fat are used for the media descriptor |
|
- | 466 | return ( (cluster - 2) * Partition.SectorsPerCluster) + Partition.FirstDataSector; // the first data sector is represented by the 2nd cluster |
|
234 | } |
467 | } |
235 | - | ||
236 | 468 | ||
237 | 469 | /****************************************************************************************************************************************/ |
|
238 | //________________________________________________________________________________________________________________________________________ |
470 | /* Function: SectorToFat16Cluster( u32 sector); */ |
- | 471 | /* */ |
|
239 | // Funtion: fflush_(File *file); |
472 | /* Description: This function converts a given sector number given to the corresponding */ |
Line -... | Line 473... | ||
- | 473 | /* cluster number in the fat that represents this data area. */ |
|
- | 474 | /* */ |
|
- | 475 | /* Returnvalue: The cluster number representing the data area of the sector. */ |
|
- | 476 | /****************************************************************************************************************************************/ |
|
- | 477 | u16 SectorToFat16Cluster(u32 sector) |
|
- | 478 | { |
|
- | 479 | if(!Partition.IsValid) return 0; |
|
- | 480 | return ((u16)((sector - Partition.FirstDataSector) / Partition.SectorsPerCluster) + 2); |
|
- | 481 | } |
|
- | 482 | ||
- | 483 | ||
240 | // |
484 | /****************************************************************************************************************************************/ |
- | 485 | /* Function: Fat16_Deinit(void); */ |
|
241 | // Description: This function writes the data already in the buffer but not yet written to the file. |
486 | /* */ |
242 | // |
487 | /* Description: This function uninitializes the fat 16 api */ |
243 | //________________________________________________________________________________________________________________________________________ |
488 | /* */ |
244 | 489 | /* Returnvalue: The function returns "0" on success */ |
|
245 | s16 fflush_(File *file) |
490 | /****************************************************************************************************************************************/ |
246 | { |
- | |
247 | u16 time=0; |
- | |
248 | u16 date=0; |
- | |
249 | - | ||
250 | - | ||
251 | if(file && ((file->mode =='a') || (file->mode =='w'))) |
- | |
- | 491 | u8 Fat16_Deinit(void) |
|
252 | { |
492 | { |
- | 493 | s16 returnvalue = 0; |
|
- | 494 | u8 cnt; |
|
253 | if(file->byte_index > 0) // has data been added to the file? |
495 | // declare the filepointers as unused. |
254 | { |
496 | for(cnt = 0; cnt < FILE_MAX_OPEN; cnt++) |
Line 255... | Line 497... | ||
255 | SDC_PutSector((u32)(file->cluster_pointer + file->sector_index),file->buffer);// save the data still in the buffer |
497 | { |
256 | } |
498 | if(FilePointer[cnt].State == FSTATE_USED) |
257 | SDC_GetSector((u32)file->directory_sector,file->buffer); // read the directoryentry for this file. |
- | |
- | 499 | { |
|
258 | DirectoryEntry = (struct DirEntry *)file->buffer; |
500 | returnvalue += fclose_(&FilePointer[cnt]); // try to close open file pointers |
- | 501 | } |
|
259 | DirectoryEntry[file->directory_index].size = (u32) file->filesize; |
502 | |
- | 503 | } |
|
260 | DirectoryEntry[file->directory_index].time = (u16) time; |
504 | SDC_Deinit(); // uninitialize interface to sd-card |
261 | DirectoryEntry[file->directory_index].date = (u16) date; |
- | |
262 | SDC_PutSector((u32)file->directory_sector,file->buffer); |
505 | Partition.IsValid = 0; // mark data in partition structure as invalid |
263 | } |
506 | return(returnvalue); |
- | 507 | } |
|
- | 508 | ||
- | 509 | /****************************************************************************************************************************************/ |
|
- | 510 | /* Function: Fat16_Init(void); */ |
|
- | 511 | /* */ |
|
- | 512 | /* Description: This function reads the Masterbootrecord and finds the position of the Volumebootrecord, the FAT and the Rootdir */ |
|
Line -... | Line 513... | ||
- | 513 | /* and stores the information in global variables. */ |
|
264 | return(0); |
514 | /* */ |
- | 515 | /* Returnvalue: The function returns "0" if the filesystem is initialized. */ |
|
- | 516 | /****************************************************************************************************************************************/ |
|
- | 517 | u8 Fat16_Init(void) |
|
265 | } |
518 | { |
266 | - | ||
267 | //________________________________________________________________________________________________________________________________________ |
- | |
268 | // Funtion: fclose_(File *file); |
519 | u8 cnt = 0; |
269 | // |
520 | u32 partitionfirstsector; |
- | 521 | VBR_Entry_t *VBR; |
|
- | 522 | MBR_Entry_t *MBR; |
|
Line 270... | Line -... | ||
270 | // Description: This function closes the open file by writing the remaining data from the buffer to the device and entering the filesize |
- | |
271 | // in the directory entry. |
523 | File_t *file; |
272 | //________________________________________________________________________________________________________________________________________ |
- | |
273 | - | ||
274 | void fclose_(File *file) |
- | |
275 | { |
- | |
276 | - | ||
277 | if(file != NULL) |
- | |
278 | { |
- | |
279 | fflush_(file); // save buffered data to the disk |
524 | u8 result = 0; |
280 | } |
525 | |
281 | FreeFilePointer(file); // and free the filepointer. |
526 | SerialPutString("\r\n FAT16 init..."); |
282 | } |
- | |
283 | 527 | Partition.IsValid = 0; |
|
284 | //________________________________________________________________________________________________________________________________________ |
528 | |
285 | // Funtion: u32 fread_(void *buffer, s32 size, s32 count, File *file); |
529 | // declare the filepointers as unused. |
286 | // |
- | |
Line 287... | Line 530... | ||
287 | // Description: This function reads count objects of the specified size from the actual position of the file to the specified buffer. |
530 | for(cnt = 0; cnt < FILE_MAX_OPEN; cnt++) |
- | 531 | { |
|
288 | // |
532 | FilePointer[cnt].State = FSTATE_UNUSED; |
- | 533 | } |
|
289 | // Returnvalue: The function returns the number of objects (not bytes) read from the file. |
534 | // set current file pinter to first position in list |
- | 535 | file = &FilePointer[0]; |
|
- | 536 | ||
- | 537 | // try to initialise the sd-card. |
|
- | 538 | if(SD_SUCCESS != SDC_Init()) |
|
- | 539 | { |
|
290 | // |
540 | SerialPutString("SD-Card could not be initialized."); |
291 | //________________________________________________________________________________________________________________________________________ |
541 | result = 1; |
- | 542 | goto end; |
|
- | 543 | } |
|
- | 544 | ||
292 | 545 | // SD-Card is initialized successfully |
|
- | 546 | if(SD_SUCCESS != SDC_GetSector((u32)MBR_SECTOR,file->Cache)) // Read the MasterBootRecord |
|
- | 547 | { |
|
293 | u32 fread_(void *buffer, u32 size, u32 count, File *file) |
548 | SerialPutString("Error reading the MBR."); |
294 | { |
549 | result = 2; |
295 | u32 object_cnt = 0; // count the number of objects read from the file. |
550 | goto end; |
296 | u32 object_size = 0; // count the number of bytes read from the actual object. |
- | |
297 | u8 *buff_pnt = 0; // a pointer to the actual bufferposition. |
551 | } |
- | 552 | MBR = (MBR_Entry_t *)file->Cache; // Enter the MBR using the structure MBR_Entry_t. |
|
298 | u8 success = 1; // no error occured during read operation to the file. |
553 | if((MBR->PartitionEntry1.Type == PART_TYPE_FAT16_ST_32_MB) || |
299 | 554 | (MBR->PartitionEntry1.Type == PART_TYPE_FAT16_LT_32_MB) || |
|
300 | buff_pnt = (u8 *) buffer; // cast the void pointer to an u8 * |
555 | (MBR->PartitionEntry1.Type == PART_TYPE_FAT16LBA)) |
Line 301... | Line -... | ||
301 | - | ||
302 | while((object_cnt < count) && success) |
- | |
303 | { |
- | |
304 | object_size = size; |
- | |
305 | while((size > 0) && success) |
- | |
306 | { |
556 | { |
307 | *buff_pnt = (u8) fgetchar_(file); // read a byte from the buffer to the opened file. |
- | |
308 | buff_pnt++; |
- | |
309 | size--; |
- | |
310 | } |
557 | // get sector offset 1st partition |
311 | if(success) object_cnt++; |
558 | partitionfirstsector = MBR->PartitionEntry1.NoSectorsBeforePartition; |
312 | } |
559 | // Start of Partition is the Volume Boot Sector |
- | 560 | if(SD_SUCCESS != SDC_GetSector(partitionfirstsector,file->Cache)) // Read the volume boot record |
|
- | 561 | { |
|
- | 562 | SerialPutString("Error reading the VBR."); |
|
313 | 563 | result = 3; |
|
314 | return(object_cnt); // return the number of objects succesfully read from the file |
564 | goto end; |
315 | } |
565 | } |
316 | - | ||
317 | //________________________________________________________________________________________________________________________________________ |
566 | } |
Line -... | Line 567... | ||
- | 567 | else // maybe the medium has no partition assuming sector 0 is the vbr |
|
- | 568 | { |
|
- | 569 | partitionfirstsector = 0; |
|
- | 570 | } |
|
- | 571 | ||
- | 572 | VBR = (VBR_Entry_t *) file->Cache; // Enter the VBR using the structure VBR_Entry_t. |
|
- | 573 | if(VBR->BytesPerSector != BYTES_PER_SECTOR) |
|
- | 574 | { |
|
- | 575 | SerialPutString("VBR: Sector size not supported."); |
|
- | 576 | result = 4; |
|
318 | // Funtion: u32 fwrite_(void *buffer, s32 size, s32 count, File *file); |
577 | goto end; |
- | 578 | } |
|
319 | // |
579 | Partition.SectorsPerCluster = VBR->SectorsPerCluster; // Number of sectors per cluster. Depends on the memorysize of the sd-card. |
- | 580 | Partition.FatCopies = VBR->NoFATCopies; // Number of fatcopies. |
|
320 | // Description: This function writes count objects of the specified size from the buffer to the actual positon within the file. |
581 | Partition.MaxRootEntries = VBR->MaxRootEntries; // How many Entries are possible in the rootdirectory (FAT16 allows max. 512 entries). |
- | 582 | Partition.SectorsPerFat = VBR->SectorsPerFAT; // The number of sectors per FAT. |
|
- | 583 | ||
- | 584 | /* Calculate the sectorpositon of the FAT, the Rootdirectory and the first Datacluster. */ |
|
321 | // |
585 | // Calculate the position of the FileAllocationTable: |
- | 586 | // Start + # of Reserved Sectors |
|
322 | // Returnvalue: The function returns the number of objects (not bytes) written to the file. |
587 | Partition.FirstFatSector = (u32)(partitionfirstsector + (u32)(VBR->ReservedSectors)); |
323 | // |
588 | // Calculate the position of the Rootdirectory: |
324 | //________________________________________________________________________________________________________________________________________ |
589 | // Start + # of Reserved Sectors + (# of Sectors Per FAT * # of FAT Copies) |
325 | 590 | Partition.FirstRootDirSector = Partition.FirstFatSector + (u32)((u32)Partition.SectorsPerFat*(u32)Partition.FatCopies); |
|
326 | u32 fwrite_(void *buffer, u32 size, u32 count, File *file) |
591 | // Calculate the position of the first datacluster: |
- | 592 | // Start + # of Reserved + (# of Sectors Per FAT * # of FAT Copies) + ((Maximum Root Directory Entries * 32) / Bytes per Sector) |
|
- | 593 | Partition.FirstDataSector = Partition.FirstRootDirSector + (u32)(Partition.MaxRootEntries>>4); // assuming 512 Byte Per Sector |
|
- | 594 | // Calculate the last data sector |
|
327 | { |
595 | if(VBR->NoSectors == 0) |
- | 596 | { |
|
- | 597 | SerialPutString("VBR: Bad number of sectors."); |
|
328 | u32 object_cnt = 0; // count the number of objects written to the file. |
598 | result = 5; |
Line 329... | Line -... | ||
329 | u32 object_size = 0; // count the number of bytes written from the actual object. |
- | |
330 | u8 *buff_pnt = 0; // a pointer to the actual bufferposition. |
- | |
Line 331... | Line -... | ||
331 | u8 success = 1; // no error occured during write operation to the file. |
- | |
332 | - | ||
333 | buff_pnt = (u8 *) buffer; // cast the void pointer to an u8 * |
- | |
334 | - | ||
335 | while((object_cnt < count) && success) |
- | |
336 | { |
- | |
337 | object_size = size; |
- | |
Line -... | Line 599... | ||
- | 599 | goto end; |
|
- | 600 | } |
|
- | 601 | Partition.LastDataSector = Partition.FirstDataSector + VBR->NoSectors - 1; |
|
- | 602 | // check for FAT16 in VBR of first partition |
|
- | 603 | if(!((VBR->FATName[0]=='F') && (VBR->FATName[1]=='A') && (VBR->FATName[2]=='T') && (VBR->FATName[3]=='1')&&(VBR->FATName[4]=='6'))) |
|
- | 604 | { |
|
- | 605 | SerialPutString("VBR: Partition ist not FAT16 type."); |
|
338 | while((size > 0) && success) |
606 | result = 6; |
339 | { |
607 | goto end; |
340 | success = fputchar_(file, *buff_pnt); // write a byte from the buffer to the opened file. |
- | |
341 | buff_pnt++; |
608 | } |
342 | size--; |
609 | Partition.IsValid = 1; // mark data in partition structure as valid |
- | 610 | result = 0; |
|
- | 611 | end: |
|
Line 343... | Line 612... | ||
343 | } |
612 | if(result != 0) Fat16_Deinit(); |
344 | if(success) object_cnt++; |
613 | else SerialPutString("ok"); |
345 | } |
614 | return(result); |
346 | - | ||
347 | return(object_cnt); // return the number of objects succesfully written to the file |
- | |
348 | } // (!!!!! objects and not bytes !!!!) |
615 | } |
349 | 616 | ||
350 | //________________________________________________________________________________________________________________________________________ |
617 | |
351 | // Funtion: s16 fseek_(File *, s32, s16) |
618 | |
- | 619 | /****************************************************************************************************************************************/ |
|
352 | // |
620 | /* Function: ClearCurrCluster(File_t*); */ |
353 | // Description: This function sets the pointer of the stream relative to the position |
- | |
354 | // specified by origin (SEEK_SET, SEEK_CUR, SEEK_END). |
- | |
355 | // |
621 | /* */ |
356 | //________________________________________________________________________________________________________________________________________ |
622 | /* Description: This function fills the current cluster with 0. */ |
357 | 623 | /* */ |
|
Line -... | Line 624... | ||
- | 624 | /* Returnvalue: The function returns 1 on success else 0. */ |
|
- | 625 | /****************************************************************************************************************************************/ |
|
358 | s16 fseek_(File *file, s32 offset, s16 origin) |
626 | u8 ClearCurrCluster(File_t * file) |
359 | { |
- | |
360 | s32 fposition = 0; |
627 | { |
- | 628 | u8 retvalue = 1; |
|
- | 629 | u32 i; |
|
- | 630 | ||
- | 631 | if((!Partition.IsValid) || (file == NULL)) return(0); |
|
361 | s16 retvalue = 1; |
632 | |
362 | u32 temp = 0; |
633 | for(i = 0; i < BYTES_PER_SECTOR; i++) file->Cache[i] = 0; // clear file cache |
363 | - | ||
364 | //...................................................... |
634 | for(i = 0; i < Partition.SectorsPerCluster; i++) |
365 | if(origin == SEEK_SET) // Fileposition relative to the beginning of the file. |
635 | { |
366 | { |
- | |
Line -... | Line 636... | ||
- | 636 | file->SectorInCache = file->FirstSectorOfCurrCluster + i; |
|
- | 637 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache)) |
|
- | 638 | { |
|
- | 639 | Fat16_Deinit(); |
|
- | 640 | retvalue = 0; |
|
- | 641 | } |
|
- | 642 | } |
|
- | 643 | return(retvalue); |
|
- | 644 | } |
|
- | 645 | ||
- | 646 | /*****************************************************************************************************************************************/ |
|
- | 647 | /* Function: GetNextCluster(File_t* ); */ |
|
- | 648 | /* */ |
|
367 | fposition = 0; |
649 | /* Description: This function finds the next datacluster of the file specified with File *File. */ |
- | 650 | /* */ |
|
- | 651 | /* Returnvalue: The function returns the next cluster or 0 if the last cluster has already reached. */ |
|
- | 652 | /*****************************************************************************************************************************************/ |
|
- | 653 | u16 GetNextCluster(File_t * file) |
|
- | 654 | { |
|
- | 655 | u16 cluster = 0; |
|
- | 656 | u32 fat_byte_offset, sector, byte; |
|
- | 657 | Fat16Entry_t * fat; |
|
- | 658 | ||
- | 659 | if((!Partition.IsValid) || (file == NULL)) return(cluster); |
|
- | 660 | // if sector is within the data area |
|
- | 661 | if((Partition.FirstDataSector <= file->FirstSectorOfCurrCluster)&& (file->FirstSectorOfCurrCluster <= Partition.LastDataSector)) |
|
- | 662 | { |
|
368 | } |
663 | // determine current file cluster |
369 | //...................................................... |
- | |
370 | else if(origin == SEEK_END) // Fileposition relative to the end of the file. |
- | |
371 | { |
- | |
372 | fposition = (s32) file->filesize; |
664 | cluster = SectorToFat16Cluster(file->FirstSectorOfCurrCluster); |
373 | } |
665 | // calculate byte offset in the fat for corresponding entry |
374 | //...................................................... |
666 | fat_byte_offset = ((u32)cluster)<<1; // two FAT bytes (16 bits) for every cluster |
375 | else if(origin == SEEK_CUR) // Fileposition relative to the current position of the file. |
667 | // calculate the sector that contains the current cluster within the fat |
376 | { |
- | |
377 | fposition = file->fileposition; |
668 | sector = Partition.FirstFatSector + ( fat_byte_offset / BYTES_PER_SECTOR); |
378 | } |
- | |
379 | - | ||
380 | fposition += offset; |
669 | // calculate byte offset of the current cluster within that fat sector |
381 | - | ||
382 | if((fposition >= 0) && (fposition <= (s32)file->filesize)) // is the pointer still within the file? |
- | |
383 | { |
- | |
384 | retvalue = 0; |
- | |
385 | file->sector_index = 0; |
- | |
386 | file->byte_index = 0; |
- | |
387 | file->fileposition = 0; |
- | |
388 | file->cluster_pointer = file->start_cluster; |
670 | byte = fat_byte_offset % BYTES_PER_SECTOR; |
389 | - | ||
390 | while(file->fileposition < fposition) |
- | |
391 | { |
- | |
392 | file->fileposition++; |
671 | // read this sector to the file cache |
393 | if(file->byte_index < 511) |
672 | if(file->SectorInCache != sector) |
394 | { |
673 | { |
395 | file->byte_index++; |
674 | file->SectorInCache = sector; // update sector stored in buffer |
Line 396... | Line -... | ||
396 | } |
- | |
397 | else |
- | |
398 | { |
- | |
399 | file->byte_index=0; // reading at the beginning of new sector. |
- | |
400 | file->sector_index++; // continue reading in next sector |
- | |
401 | if(file->sector_index >= SectorsPerCluster) // When end of cluster is reached, the next datacluster has to be searched in the FAT. |
- | |
402 | { |
- | |
Line -... | Line 675... | ||
- | 675 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache)) // read sector from sd-card |
|
- | 676 | { |
|
- | 677 | Fat16_Deinit(); |
|
- | 678 | return (cluster); |
|
- | 679 | } |
|
- | 680 | } |
|
- | 681 | // read the next cluster from cache |
|
403 | if(file->fileposition < fposition) |
682 | fat = (Fat16Entry_t *)(&(file->Cache[byte])); |
404 | { |
683 | cluster = fat->NextCluster; |
- | 684 | // if last cluster fat entry |
|
405 | file->sector_index = 0; // start reading new cluster at first sector of the cluster. |
685 | if(FAT16_CLUSTER_LAST_MIN <= cluster) |
- | 686 | { |
|
- | 687 | cluster = 0; |
|
406 | GetNextCluster(file); // Sets the clusterpointer of the file to the next datacluster. |
688 | } |
Line 407... | Line -... | ||
407 | } |
- | |
408 | } |
- | |
409 | } |
- | |
410 | } |
689 | else |
Line 411... | Line 690... | ||
411 | if(file->byte_index) |
690 | { |
412 | { |
- | |
413 | temp = (u32)((u32)file->cluster_pointer + (u32)file->sector_index); |
- | |
414 | SDC_GetSector((u32)temp,file->buffer); // FileBuffer will be written at once at the end of the cluster and has to be updated. |
- | |
415 | } |
- | |
416 | } |
- | |
417 | return(retvalue); |
- | |
418 | } |
- | |
419 | - | ||
420 | //________________________________________________________________________________________________________________________________________ |
- | |
421 | // Funtion: fgetchar_(File *file); |
691 | file->FirstSectorOfCurrCluster = Fat16ClusterToSector(cluster); |
422 | // |
692 | file->SectorOfCurrCluster = 0; |
423 | // Description: This function reads and returns one character from the specified file. |
- | |
424 | // |
693 | file->ByteOfCurrSector = 0; |
425 | // Returnvalue: The function returns the character read from the specified memorylocation as u8 casted to s16 or EOF. |
694 | } |
426 | //________________________________________________________________________________________________________________________________________ |
695 | } |
427 | 696 | return(cluster); |
|
428 | s16 fgetchar_(File *file) |
697 | } |
429 | { |
- | |
430 | s16 c = EOF; |
- | |
431 | u32 temp1; |
698 | |
432 | - | ||
433 | if(((file->fileposition)+1) < file->filesize) // wen the end of the file is not reached, get the next character. |
- | |
434 | { |
699 | |
435 | temp1 = (u32)file->cluster_pointer; // calculate the adress of the next character to be read. |
700 | /****************************************************************************************************************************************/ |
Line 436... | Line -... | ||
436 | temp1 += (u32)file->sector_index; |
- | |
437 | - | ||
438 | if(file->sector_in_buffer != temp1) // Has the content of the buffer been modified and has to be updated? |
- | |
439 | { |
- | |
440 | SDC_GetSector((u32)temp1,file->buffer); // Read the calculated cluster. |
- | |
441 | file->sector_in_buffer = (u32)temp1; |
701 | /* Function: FindNextFreeCluster(File_t *); */ |
442 | } |
- | |
Line 443... | Line 702... | ||
443 | c = (s16) file->buffer[file->byte_index]; |
702 | /* */ |
444 | file->fileposition++; |
703 | /* Description: This function looks in the fat to find the next free cluster */ |
445 | - | ||
446 | if(file->byte_index < 511) // continue reading from this sector until the end of the sector is reached. |
- | |
447 | { |
- | |
448 | file->byte_index++; |
704 | /* */ |
449 | } |
705 | /* Returnvalue: The function returns the cluster number of the next free cluster found within the fat. */ |
450 | else // has the end of an sector been reached-> |
- | |
451 | { |
706 | /****************************************************************************************************************************************/ |
452 | file->byte_index=0; // continue reading at the beginning - |
- | |
453 | file->sector_index++; // of new sector. |
- | |
454 | if(file->sector_index >= SectorsPerCluster) // When the end of an cluster is reached, the next datacluster has to be searched in the FAT. |
- | |
455 | { |
- | |
456 | file->sector_index = 0; // start reading new cluster at first sector of the cluster. |
- | |
457 | GetNextCluster(file); // Sets the clusterpointer of the file to the next datacluster. |
- | |
458 | } |
707 | u16 FindNextFreeCluster(File_t *file) |
459 | } |
708 | { |
- | 709 | u32 fat_sector; // current sector within the fat relative to the first sector of the fat. |
|
460 | } |
710 | u32 curr_sector; // current sector |
461 | return(c); |
711 | u16 fat_entry; // index to an fatentry within the actual sector (256 fatentries are possible within one sector). |
462 | } |
- | |
463 | - | ||
464 | //________________________________________________________________________________________________________________________________________ |
- | |
465 | // Funtion: fputchar_(File *file, s8 *); |
- | |
466 | // |
- | |
467 | // Description: This function writes a byte to the specified file and takes care of writing the necessary FAT- Entries. |
- | |
468 | // |
- | |
469 | // Returnvalue: The function returns a value of 0 if the data could not be written. |
- | |
470 | //________________________________________________________________________________________________________________________________________ |
712 | u16 free_cluster = 0; // next free cluster number. |
471 | - | ||
472 | u8 fputchar_(File *file,s8 c) |
- | |
473 | { |
713 | Fat16Entry_t * fat; |
474 | u32 ul_temp = 0; |
714 | |
475 | u8 retvalue = 1; |
715 | if((!Partition.IsValid) || (file == NULL)) return(0); |
- | 716 | ||
- | 717 | // start searching for an empty cluster at the beginning of the fat. |
|
- | 718 | fat_sector = 0; |
|
476 | 719 | do |
|
477 | if(file->sector_index >= SectorsPerCluster) // if end of the cluster is reached, find next free cluster |
720 | { |
Line 478... | Line -... | ||
478 | { |
- | |
479 | file->sector_index = 0; |
- | |
480 | if(!AppendCluster(file)) retvalue = 0; // append a new and free cluster at the end of the file. |
- | |
481 | } |
- | |
482 | - | ||
483 | file->buffer[file->byte_index] = c; // write databyte into the buffer. The byte will be written to the device at once |
- | |
Line -... | Line 721... | ||
- | 721 | curr_sector = Partition.FirstFatSector + fat_sector; // calculate sector to read |
|
- | 722 | file->SectorInCache = curr_sector; // upate the sector number of file cache. |
|
- | 723 | if( SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache)) // read sector of fat from sd-card. |
|
- | 724 | { |
|
- | 725 | Fat16_Deinit(); |
|
- | 726 | return(free_cluster); |
|
- | 727 | } |
|
484 | if(file->filesize == file->fileposition) file->filesize++; // a character has been written to the file so the size is inkremented but only when the character has been added at the end of the file. |
728 | |
485 | file->fileposition++; // the actual positon within the file. |
729 | fat = (Fat16Entry_t *)file->Cache; // set fat pointer to file cache |
- | 730 | ||
486 | // if the buffer contains the complete sectordata. |
731 | for(fat_entry = 0; fat_entry < FAT16_ENTRIES_PER_SECTOR; fat_entry++) // look for an free cluster at all entries in this sector of the fat. |
Line -... | Line 732... | ||
- | 732 | { |
|
487 | if(file->byte_index < 511) // if the end of this sector is not reached yet |
733 | if(fat[fat_entry].NextCluster == FAT16_CLUSTER_FREE) // empty cluster found!! |
488 | { |
734 | { |
- | 735 | fat[fat_entry].NextCluster = FAT16_CLUSTER_LAST_MAX; // mark this fat-entry as used |
|
- | 736 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache)) // and save the sector at the sd-card. |
|
- | 737 | { |
|
- | 738 | Fat16_Deinit(); |
|
489 | file->byte_index++; // the next byte will be written to the next byteposition in this sector. |
739 | return(free_cluster); |
490 | } |
740 | } |
- | 741 | free_cluster = (u16)(fat_sector * FAT16_ENTRIES_PER_SECTOR + (u32)fat_entry); |
|
491 | else // otherwise the data in the sectorbuffer will be written to the device and the next sector will be selected. |
742 | fat_entry = FAT16_ENTRIES_PER_SECTOR; // terminate the search for a free cluster in this sector. |
- | 743 | } |
|
492 | { |
744 | } |
493 | ul_temp = (u32)file->cluster_pointer; |
745 | fat_sector++; // continue the search in next fat sector |
Line 494... | Line -... | ||
494 | ul_temp += (u32)file->sector_index; |
- | |
495 | 746 | // repeat until the end of the fat is reached and no free cluster has been found so far |
|
496 | SDC_PutSector((u32)ul_temp,file->buffer); |
- | |
497 | file->byte_index=0; // and the next byte will be written at the beginning of this new sector. |
- | |
498 | file->sector_index++; |
- | |
499 | if(file->sector_index >= SectorsPerCluster) // if end of the cluster is reached, find next free cluster |
- | |
500 | { |
- | |
Line 501... | Line 747... | ||
501 | file->sector_index = 0; |
747 | }while((fat_sector < Partition.SectorsPerFat) && (!free_cluster)); |
502 | if(!AppendCluster(file)) retvalue = 0; // append a new and free cluster at the end of the file. |
748 | return(free_cluster); |
503 | } |
749 | } |
- | 750 | ||
504 | } |
751 | |
505 | return(retvalue); |
752 | /****************************************************************************************************************************************************/ |
506 | } |
- | |
507 | 753 | /* Function: s16 fseek_(File_t *, s32 *, u8) */ |
|
Line 508... | Line 754... | ||
508 | //________________________________________________________________________________________________________________________________________ |
754 | /* */ |
509 | // Funtion: fputs_(File *file, s8 *string); |
755 | /* Description: This function sets the pointer of the stream relative to the position */ |
510 | // |
756 | /* specified by origin (SEEK_SET, SEEK_CUR, SEEK_END) */ |
511 | // Description: This function writes a zero terminated string to the specified file. |
757 | /* Returnvalue: Is 1 if seek was successful */ |
- | 758 | /****************************************************************************************************************************************************/ |
|
512 | // |
759 | s16 fseek_(File_t *file, s32 offset, s16 origin) |
513 | //________________________________________________________________________________________________________________________________________ |
760 | { |
- | 761 | s32 fposition = 0; |
|
- | 762 | s16 retvalue = 1; |
|
514 | 763 | ||
- | 764 | if((!Partition.IsValid) || (file == NULL)) return(0); |
|
515 | u8 fputs_(File *file,s8 * string) |
765 | switch(origin) |
- | 766 | { |
|
516 | { |
767 | case SEEK_SET: // Fileposition relative to the beginning of the file. |
517 | u8 i=0; |
768 | fposition = 0; |
518 | 769 | break; |
|
519 | while(string[i] != 0) |
770 | case SEEK_END: // Fileposition relative to the end of the file. |
520 | { |
- | |
- | 771 | fposition = (s32)file->Size; |
|
521 | fputchar_(file,string[i]); |
772 | break; |
522 | i++; |
773 | case SEEK_CUR: // Fileposition relative to the current position of the file. |
523 | } |
- | |
524 | return(0); |
- | |
525 | } |
- | |
526 | - | ||
527 | //________________________________________________________________________________________________________________________________________ |
- | |
528 | // Funtion: s8 * fgets_(s8 *string, s16 count, File *file) |
774 | default: |
529 | // |
- | |
530 | // Description: This function reads a string from the file to the specifies string until EOF or end of line was detected.. |
- | |
531 | // |
- | |
532 | // Returnvalue: A pointer to the string or NULL if EOF was read from the file with no other characters before. |
- | |
533 | //________________________________________________________________________________________________________________________________________ |
- | |
534 | 775 | fposition = file->Position; |
|
535 | s8 * fgets_(s8 *string, s16 count, File *file) |
776 | break; |
536 | { |
777 | } |
537 | s16 buff_pnt = 0; |
778 | |
538 | s16 buff_tmp = 0; |
779 | fposition += offset; |
Line 539... | Line -... | ||
539 | s8 *retvalue = 0; |
- | |
540 | - | ||
541 | retvalue = string; |
- | |
542 | - | ||
543 | while(count > 1) // read the count-1 characters from the file to the string. |
- | |
544 | { |
- | |
545 | buff_tmp = fgetchar_(file); // read a character from the opened file. |
- | |
546 | if(buff_tmp == EOF) // is the end of the file reached, terminate the string with zero. |
- | |
547 | { |
- | |
Line -... | Line 780... | ||
- | 780 | ||
- | 781 | if((fposition >= 0) && (fposition <= (s32)file->Size)) // is the pointer still within the file? |
|
- | 782 | { |
|
- | 783 | // reset file position to start of the file |
|
- | 784 | file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster; |
|
- | 785 | file->SectorOfCurrCluster = 0; |
|
548 | if(buff_pnt != 0) // are there characters within the string? |
786 | file->ByteOfCurrSector = 0; |
549 | { |
787 | file->Position = 0; |
550 | break; |
788 | |
- | 789 | while(file->Position < fposition) // repeat until the current position is less than target |
|
- | 790 | { |
|
- | 791 | file->Position++; // increment file position |
|
- | 792 | file->ByteOfCurrSector++; // next byte in current sector |
|
- | 793 | if(file->ByteOfCurrSector >= BYTES_PER_SECTOR) |
|
Line 551... | Line 794... | ||
551 | } |
794 | { |
Line -... | Line 795... | ||
- | 795 | file->ByteOfCurrSector = 0; // reading at the beginning of new sector. |
|
- | 796 | file->SectorOfCurrCluster++; // continue reading in next sector |
|
- | 797 | if(file->SectorOfCurrCluster >= Partition.SectorsPerCluster) // if end of cluster is reached, the next datacluster has to be searched in the FAT. |
|
- | 798 | { |
|
- | 799 | if(GetNextCluster(file)) // Sets the clusterpointer of the file to the next datacluster. |
|
- | 800 | { |
|
552 | else |
801 | file->SectorOfCurrCluster = 0; |
- | 802 | } |
|
553 | { |
803 | else // the last cluster was allready reached |
554 | retvalue = NULL; // first character read was EOF -> return(NULL); |
- | |
555 | break; |
804 | { |
556 | } |
- | |
557 | } |
- | |
558 | else if(buff_tmp == 0x0A) // lf detected. |
805 | file->SectorOfCurrCluster--; // jump back to the ast sector in the last cluster |
- | 806 | file->ByteOfCurrSector = BYTES_PER_SECTOR; // set ByteOfCurrSector one byte over sector end |
|
- | 807 | } |
|
- | 808 | } |
|
- | 809 | } |
|
- | 810 | } |
|
- | 811 | } |
|
- | 812 | if(file->Position == fposition) retvalue = 0; |
|
- | 813 | return(retvalue); |
|
- | 814 | } |
|
- | 815 | ||
- | 816 | ||
559 | { |
817 | /****************************************************************************************************************************************/ |
- | 818 | /* Function: u16 DeleteClusterChain(File *file); */ |
|
- | 819 | /* */ |
|
- | 820 | /* Description: This function trances along a cluster chain in the fat and frees all clusters visited. */ |
|
- | 821 | /* */ |
|
- | 822 | /****************************************************************************************************************************************/ |
|
- | 823 | u8 DeleteClusterChain(u16 StartCluster) |
|
- | 824 | { |
|
- | 825 | u16 cluster; |
|
- | 826 | u32 fat_byte_offset, sector, byte; |
|
- | 827 | Fat16Entry_t * fat; |
|
560 | string[buff_pnt] = (s8) buff_tmp; // add lf to the string. |
828 | u8 buffer[BYTES_PER_SECTOR]; |
- | 829 | u32 sector_in_buffer = 0; |
|
- | 830 | u8 repeat = 0; |
|
- | 831 | ||
561 | buff_pnt++; |
832 | if(!Partition.IsValid) return 0; |
Line 562... | Line -... | ||
562 | break; |
- | |
563 | } |
- | |
564 | else |
- | |
565 | { |
- | |
566 | string[buff_pnt] = (s8) buff_tmp; |
- | |
567 | count--; |
- | |
568 | buff_pnt++; |
- | |
569 | } |
- | |
570 | } |
- | |
571 | string[buff_pnt] = 0; // terminate string with zero |
- | |
572 | return(string); |
- | |
573 | } |
- | |
574 | - | ||
575 | //________________________________________________________________________________________________________________________________________ |
- | |
Line -... | Line 833... | ||
- | 833 | ||
- | 834 | cluster = StartCluster; // init chain trace |
|
- | 835 | // calculate byte offset in the fat for corresponding entry |
|
- | 836 | fat_byte_offset = ((u32)cluster)<<1; // two FAT bytes (16 bits) for every cluster |
|
- | 837 | // calculate the sector that contains the current cluster within the fat |
|
- | 838 | sector = Partition.FirstFatSector + ( fat_byte_offset / BYTES_PER_SECTOR); |
|
- | 839 | // calculate byte offset of the current cluster within that fat sector |
|
- | 840 | byte = fat_byte_offset % BYTES_PER_SECTOR; |
|
- | 841 | do |
|
- | 842 | { |
|
- | 843 | if(sector != sector_in_buffer) |
|
- | 844 | { |
|
- | 845 | // read this sector to buffer |
|
- | 846 | sector_in_buffer = sector; |
|
- | 847 | if(SD_SUCCESS != SDC_GetSector(sector_in_buffer, buffer)) return 0; // read sector from sd-card |
|
- | 848 | } |
|
- | 849 | // read the next cluster from cache |
|
- | 850 | fat = (Fat16Entry_t *)(&(buffer[byte])); |
|
- | 851 | cluster = fat->NextCluster; |
|
- | 852 | if((FAT16_CLUSTER_USED_MIN <= cluster) && (cluster <= FAT16_CLUSTER_USED_MAX) ) repeat = 1; |
|
- | 853 | else repeat = 0; |
|
- | 854 | ||
- | 855 | fat->NextCluster = FAT16_CLUSTER_FREE; // mark current cluster as free |
|
Line 576... | Line -... | ||
576 | // Funtion: u8 fexist_(u8*); |
- | |
577 | // |
- | |
578 | // Description: This function searches the specified file and returns 0 if the file was not found. |
- | |
579 | // |
856 | // calculate byte offset in the fat for corresponding entry |
580 | // |
- | |
581 | // Return: 0 = file does not exist |
- | |
582 | // 1 = file exists |
857 | fat_byte_offset = ((u32)cluster)<<1; // two FAT bytes (16 bits) for every cluster |
583 | //________________________________________________________________________________________________________________________________________ |
- | |
584 | - | ||
585 | u8 fexist_(s8 *fname) |
- | |
586 | { |
- | |
587 | File *file; |
- | |
588 | - | ||
589 | file = fopen_(fname,'r'); // try to open the specified file. |
- | |
590 | - | ||
591 | if(file != NULL) |
- | |
592 | { |
- | |
593 | FreeFilePointer(file); // Free the filepoint // free the filepointer |
- | |
594 | return(1); // file was found so return(1); |
- | |
595 | } |
- | |
596 | else |
858 | // calculate the sector that contains the current cluster within the fat |
597 | { |
- | |
598 | return(0); // file not found return(0); |
- | |
599 | } |
859 | sector = Partition.FirstFatSector + ( fat_byte_offset / BYTES_PER_SECTOR); |
600 | } |
860 | // calculate byte offset of the current cluster within that fat sector |
601 | - | ||
602 | //________________________________________________________________________________________________________________________________________ |
861 | byte = fat_byte_offset % BYTES_PER_SECTOR; |
603 | // Funtion: GetNextCluster(File *file); |
862 | // if new sector is not the sector in buffer or the last cluster in the chain was traced |
604 | // |
863 | if((sector != sector_in_buffer) || !repeat) |
605 | // Description: This function finds the next datacluster of the file specified with File *file. |
- | |
606 | // |
864 | { // write sector in buffer |
607 | // Returnvalue: The function returns "0" if the last cluster has already been reached. |
- | |
608 | //________________________________________________________________________________________________________________________________________ |
- | |
609 | 865 | if(SD_SUCCESS != SDC_PutSector(sector_in_buffer,buffer)) return 0; |
|
610 | u16 GetNextCluster(File *file) |
- | |
611 | { |
866 | } |
612 | u32 fat_pointer = 0; |
- | |
613 | u32 fat_sector_offset = 0; |
867 | } |
614 | u32 ul_tmp = 0; |
- | |
615 | u8 retvalue = 0; // no new cluster found yet. |
- | |
616 | - | ||
617 | 868 | while(repeat); |
|
618 | if((file->cluster_pointer >= RootDirectory) && (file->cluster_pointer < (RootDirectory + 31))) |
- | |
619 | { // Is the next cluster searched within the rootdirectory and available? |
- | |
620 | file->cluster_pointer++; // the rootdirectory is a linear adress space of 32 clusters. |
- | |
621 | retvalue = 1; // and the next cluster has been found. |
- | |
622 | } |
- | |
623 | else if(file->cluster_pointer > (RootDirectory + 31)) // The specified cluster is within the FAT. |
- | |
624 | { |
- | |
625 | fat_sector_offset = ((file->cluster_pointer) - (FirstDataCluster)); // Calculate index of actual cluster within the FAT. |
- | |
626 | fat_sector_offset /= SectorsPerCluster; // calculate the index of the actual sector within the FAT. |
- | |
627 | fat_sector_offset += 2; // In Fat16 clusterpositions have an offset of two. |
- | |
628 | fat_pointer = (fat_sector_offset%0x100); // Calculate the sector within the cluster. |
- | |
629 | fat_sector_offset = (fat_sector_offset>>8); // and the position within the sector. |
- | |
630 | - | ||
631 | SDC_GetSector((u32)(FileAllocationTable + fat_sector_offset),file->buffer); |
- | |
632 | file->sector_in_buffer = (FileAllocationTable + fat_sector_offset); // Mark that new sector has been read. |
- | |
633 | - | ||
634 | ul_tmp = (u32)file->buffer[((fat_pointer << 1)+1)]; // Read next sector information from calculated clusterposition. |
- | |
635 | ul_tmp = (ul_tmp << 8); |
- | |
636 | ul_tmp |= (u32)file->buffer[(fat_pointer << 1)]; |
- | |
637 | ul_tmp -=2; // next datacluster is clusterposition in fat - 2. |
- | |
638 | ul_tmp *= SectorsPerCluster; // calculate sectorposition of new cluster |
- | |
639 | ul_tmp += FirstDataCluster; // in relation to first datacluster of the disk. |
- | |
640 | - | ||
641 | if(ul_tmp < 0xfff7) // has a new cluster been read or was the end of the fat reached? |
- | |
642 | { |
- | |
643 | file->cluster_pointer = (u32) ul_tmp; // continue reading the file at the beginning of new datacluster. |
869 | |
644 | retvalue = 1; // a new cluster was found. |
- | |
645 | } |
870 | return 1; |
646 | } |
871 | } |
- | 872 | ||
- | 873 | ||
- | 874 | /****************************************************************************************************************************************/ |
|
- | 875 | /* Function: u16 AppendCluster(File *file); */ |
|
- | 876 | /* */ |
|
- | 877 | /* Description: This function looks in the fat to find the next free cluster and appends it to the file. */ |
|
647 | return(retvalue); |
878 | /* */ |
648 | } |
879 | /* Returnvalue: The function returns the appened cluster number or 0 of no cluster was appended. */ |
- | 880 | /****************************************************************************************************************************************/ |
|
649 | 881 | u16 AppendCluster(File_t *file) |
|
650 | //________________________________________________________________________________________________________________________________________ |
- | |
651 | // Funtion: u16 FindNextFreeCluster(void) |
882 | { |
652 | // |
- | |
- | 883 | u16 last_cluster, new_cluster = 0; |
|
653 | // Description: This function looks in the FAT to find the next free datacluster |
884 | u32 fat_byte_offset, sector, byte; |
654 | // |
885 | Fat16Entry_t * fat; |
655 | // Returnvalue: The function returns the adress of the next free cluster found within the fAT. |
- | |
656 | //________________________________________________________________________________________________________________________________________ |
886 | |
657 | 887 | if((!Partition.IsValid) || (file == NULL)) return(new_cluster); |
|
658 | u16 FindNextFreeCluster(File *file) |
- | |
659 | { |
- | |
660 | u32 fat_pointer = 0; // Pointer to the first sector of the FAT. |
- | |
661 | u32 ul_tmp = 0; // temporary variable used to calculate a sectornumber. |
- | |
662 | u16 fat_sector_offset = 0; // index to a sector within the FAT. |
- | |
663 | u16 fat_entry = 0; // index to an fatentry within the actual sector (256 fatentries are possible within one sector). |
- | |
664 | u16 free_cluster = 0; // a pointer to the first sector of the next free cluster. |
- | |
665 | - | ||
666 | fat_pointer = (u32) FileAllocationTable; // start searching for empty cluster at the beginning of the fat. |
- | |
667 | // if the end of the fat is not reached yet and no free cluster has been found |
888 | |
668 | while((fat_sector_offset < SectorsPerFat) && (!free_cluster)) |
- | |
669 | { |
- | |
670 | ul_tmp = (u32) ((u32)fat_pointer + (u32)fat_sector_offset); |
- | |
671 | SDC_GetSector((u32)ul_tmp,file->buffer); // read next sector of FAT. |
- | |
672 | file->sector_in_buffer = ul_tmp; // remember the number of the sector in FileBuffer. |
- | |
673 | Fat = (struct FatEntry *)file->buffer; |
889 | new_cluster = FindNextFreeCluster(file); // the next free cluster found on the disk. |
674 | for(fat_entry=0;fat_entry<256;fat_entry++) // look for an free cluster at all entries in this sector of the fat. |
- | |
675 | { |
- | |
Line 676... | Line 890... | ||
676 | if(Fat[fat_entry].next_cluster == 0x0000) // empty cluster found!! |
890 | if(new_cluster) |
- | 891 | { // A free cluster was found and can be added to the end of the file. |
|
677 | { |
892 | fseek_(file, 0, SEEK_END); // jump to the end of the file |
678 | Fat[fat_entry].next_cluster = 0xffff; // mark this fat-entry as used and save it to the device. |
893 | last_cluster = SectorToFat16Cluster(file->FirstSectorOfCurrCluster); // determine current file cluster |
Line -... | Line 894... | ||
- | 894 | fat_byte_offset = ((u32)last_cluster)<<1; |
|
- | 895 | sector = Partition.FirstFatSector + ( fat_byte_offset / BYTES_PER_SECTOR); |
|
Line 679... | Line -... | ||
679 | SDC_PutSector((u32)file->sector_in_buffer,file->buffer); |
- | |
680 | free_cluster = fat_entry; // the relative position of the free cluster found in this sector of the FAT. |
896 | byte = fat_byte_offset % BYTES_PER_SECTOR; |
681 | free_cluster += (fat_sector_offset << 8); // calculate the absolute position of the free cluster in the FAT; |
- | |
682 | fat_entry = 256; // terminate the search for a free cluster in this sector. |
897 | |
683 | } |
- | |
684 | } |
898 | if(file->SectorInCache != sector) |
685 | fat_sector_offset++; |
- | |
Line 686... | Line 899... | ||
686 | } |
899 | { |
- | 900 | file->SectorInCache = sector; // update sector stored in buffer |
|
- | 901 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache)) // read sector from sd-card |
|
687 | return(free_cluster); |
902 | { |
688 | } |
- | |
689 | 903 | Fat16_Deinit(); |
|
690 | //________________________________________________________________________________________________________________________________________ |
904 | return(0); |
691 | // Funtion: u16 AppendCluster(File *file); |
- | |
692 | // |
- | |
693 | // Description: This function finds the next free datacluster on the disk and appends it to the specified file. |
905 | } |
694 | // |
906 | } |
695 | // Returnvalue: This funktion returns 1 if a cluster was appended to the specified file. |
907 | fat = (Fat16Entry_t *)(&(file->Cache[byte])); |
696 | //________________________________________________________________________________________________________________________________________ |
- | |
697 | - | ||
698 | u8 AppendCluster(File *file) |
- | |
699 | { |
- | |
700 | u16 free_cluster = 0; |
908 | fat->NextCluster = new_cluster; // append the free cluster to the end of the file in the FAT. |
701 | u32 fat_pointer = 0; |
909 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache)) // save the modified sector to the FAT. |
- | 910 | { |
|
- | 911 | Fat16_Deinit(); |
|
- | 912 | return(0); |
|
702 | u8 retvalue = 0; |
913 | } |
703 | - | ||
704 | free_cluster = FindNextFreeCluster(file); // the next free cluster found on the disk. |
914 | file->FirstSectorOfCurrCluster = Fat16ClusterToSector(new_cluster); |
705 | if(free_cluster) retvalue = 1; // A free cluster was found and can be added to the end of the file. |
915 | file->SectorOfCurrCluster = 0; |
706 | fat_pointer = FileAllocationTable; // Set Pointer to the beginnig of the FAT. |
916 | file->ByteOfCurrSector = 0; |
707 | fat_pointer += (u32)((u32)GetFatClusterOffset(file) >> 8); // find the sector in the FAT with 256 entries per sector. |
- | |
708 | 917 | } |
|
709 | SDC_GetSector(fat_pointer,file->buffer); |
918 | return(new_cluster); |
710 | Fat = (struct FatEntry *)file->buffer; |
919 | } |
711 | Fat[GetFatSectorIndex(file)].next_cluster = free_cluster; // append the free cluster to the end of the file in the FAT. |
920 | |
712 | SDC_PutSector((u32)fat_pointer,file->buffer); // save the modified sector to the FAT. |
921 | /****************************************************************************************************************************************************/ |
713 | 922 | /* Function: DirectoryEntryExist(s8 *, u8, u8, File_t *) */ |
|
714 | fat_pointer = (u32)free_cluster; |
- | |
715 | fat_pointer -= 2; |
923 | /* */ |
- | 924 | /* Description: This function searches all possible dir entries until the file or directory is found or the end of the directory is reached */ |
|
716 | fat_pointer *= SectorsPerCluster; |
925 | /* */ |
717 | fat_pointer += FirstDataCluster; |
926 | /* Returnvalue: This function returns 1 if the directory entry specified was found. */ |
- | 927 | /****************************************************************************************************************************************************/ |
|
- | 928 | u8 DirectoryEntryExist(s8 *dirname, u8 attribfilter, u8 attribmask, File_t *file) |
|
- | 929 | { |
|
- | 930 | u32 dir_sector, max_dir_sector, curr_sector; |
|
- | 931 | u16 dir_entry = 0; |
|
- | 932 | ||
- | 933 | u16 end_of_directory_not_reached = 0; |
|
- | 934 | u8 i = 0; |
|
- | 935 | u8 direntry_exist = 0; |
|
- | 936 | DirEntry_t * dir; |
|
- | 937 | ||
- | 938 | // if incomming pointers are useless return immediatly |
|
- | 939 | if((!Partition.IsValid) || (file == NULL) || (dirname == NULL)) return(direntry_exist); |
|
718 | 940 | ||
- | 941 | // dir entries can be searched only in filesclusters that have |
|
- | 942 | // a corresponding dir entry with adir-flag set in its attribute |
|
719 | file->cluster_pointer = fat_pointer; // continue wrtiting to the file in the new and free datacluster. |
943 | // or direct within the root directory area |
720 | return(retvalue); // return 1 when a new cluster was appended to the file |
944 | |
Line -... | Line 945... | ||
- | 945 | file->FirstSectorOfFirstCluster = 0; |
|
- | 946 | // no current directory exist therefore assume searching in the root |
|
- | 947 | if(file->DirectorySector == 0) |
|
- | 948 | { |
|
Line 721... | Line -... | ||
721 | } |
- | |
722 | 949 | max_dir_sector = (Partition.MaxRootEntries * DIRENTRY_SIZE)/BYTES_PER_SECTOR; |
|
723 | - | ||
724 | //________________________________________________________________________________________________________________________________________ |
- | |
725 | // Funtion: void DeleteClusterChain(u16 startcluster); |
- | |
726 | // |
- | |
727 | // Description: This function frees all the clusters used for file from the fat. |
- | |
728 | // |
- | |
729 | // Returnvalue: none |
950 | file->FirstSectorOfFirstCluster = Partition.FirstRootDirSector; |
730 | //________________________________________________________________________________________________________________________________________ |
951 | } |
- | 952 | // within the root directory area we can read sectors sequentially until the end of this area |
|
731 | 953 | else if((Partition.FirstRootDirSector <= file->DirectorySector) && (file->DirectorySector < Partition.FirstDataSector)) |
|
732 | void DeleteClusterChain(u16 startcluster) |
954 | { |
733 | { |
955 | max_dir_sector = (Partition.MaxRootEntries * DIRENTRY_SIZE)/BYTES_PER_SECTOR; |
734 | u16 fat_index = 0; |
956 | } |
735 | u16 fat_sector_offset = 0; |
957 | // within the data clusters we can read sectors sequentially only within the cluster |
- | 958 | else if((Partition.FirstDataSector <= file->DirectorySector) && (file->DirectorySector <= Partition.LastDataSector)) |
|
736 | u32 sector_in_buffer = 0; |
959 | { |
737 | u32 ul_temp = 0; |
960 | max_dir_sector = Partition.SectorsPerCluster; // limit max secters before next cluster |
738 | u8 buffer[512]; |
- | |
739 | 961 | } |
|
740 | fat_index = (startcluster % 0x100); // Calculate the sector within the cluster. |
962 | else return (direntry_exist); // bad sector range for directory sector of the file |
741 | fat_sector_offset = (startcluster >> 8); // and the position within the sector. |
- | |
742 | ul_temp = (u32)(FileAllocationTable + fat_sector_offset); |
963 | // if search area is not defined yet |
743 | - | ||
744 | - | ||
745 | do |
- | |
746 | { |
964 | if(file->FirstSectorOfFirstCluster == 0) |
- | 965 | { |
|
747 | if(sector_in_buffer != ul_temp) |
966 | // check if the directory entry of current file is existent and has the dir-flag set |
- | 967 | file->SectorInCache = file->DirectorySector; // update the sector number of file cache. |
|
- | 968 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector. |
|
- | 969 | { |
|
- | 970 | Fat16_Deinit(); |
|
- | 971 | return(direntry_exist); |
|
- | 972 | } |
|
- | 973 | dir = (DirEntry_t *)file->Cache; // set pointer to directory |
|
- | 974 | switch((u8)dir[file->DirectoryIndex].Name[0]) // check if current directory exist |
|
- | 975 | { |
|
- | 976 | case SLOT_EMPTY: |
|
- | 977 | case SLOT_DELETED: |
|
- | 978 | // the directrory pointer of this file points to a deleted or not existen directory |
|
- | 979 | // therefore no file or subdirectory can be created |
|
- | 980 | return (direntry_exist); |
|
- | 981 | break; |
|
- | 982 | default: // and is a real directory |
|
748 | { |
983 | if((dir[file->DirectoryIndex].Attribute & ATTR_SUBDIRECTORY) != ATTR_SUBDIRECTORY) |
- | 984 | { // current file is not a directory therefore no file or subdirectory can be created here |
|
- | 985 | return (direntry_exist); |
|
- | 986 | } |
|
- | 987 | break; |
|
- | 988 | } |
|
- | 989 | file->FirstSectorOfFirstCluster = Fat16ClusterToSector(dir[file->DirectoryIndex].StartCluster); |
|
- | 990 | } |
|
- | 991 | ||
- | 992 | // update current file data area position to start of first cluster |
|
- | 993 | file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster; |
|
749 | sector_in_buffer = ul_temp; |
994 | file->SectorOfCurrCluster = 0; |
- | 995 | file->ByteOfCurrSector = 0; |
|
750 | SDC_GetSector(ul_temp,buffer); |
996 | |
- | 997 | do // loop over all data clusters of the current directory entry |
|
751 | } |
998 | { |
752 | Fat = (struct FatEntry *)buffer; |
999 | dir_sector = 0; // reset sector counter within a new cluster |
- | 1000 | do // loop over all sectors of a cluster or all sectors of the root directory |
|
753 | 1001 | { |
|
754 | startcluster = Fat[fat_index].next_cluster; |
1002 | curr_sector = file->FirstSectorOfCurrCluster + dir_sector; // calculate sector number |
Line 755... | Line -... | ||
755 | Fat[fat_index].next_cluster = 0x0000; // free the cluster within the fat. |
- | |
756 | - | ||
757 | fat_index = (startcluster % 0x100); // Calculate the sector within the cluster. |
- | |
758 | fat_sector_offset = (startcluster >> 8); // and the position within the sector. |
- | |
759 | ul_temp = (u32)(FileAllocationTable + fat_sector_offset); |
- | |
760 | if((sector_in_buffer != ul_temp) || (startcluster == 0xffff)) |
- | |
761 | { |
- | |
762 | SDC_PutSector(sector_in_buffer,buffer); |
- | |
763 | } |
- | |
764 | } |
- | |
765 | while(startcluster != 0xffff); // last cluster has been deleted. |
- | |
766 | } |
- | |
Line -... | Line 1003... | ||
- | 1003 | file->SectorInCache = curr_sector; // upate the sector number of file cache. |
|
- | 1004 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read the sector |
|
- | 1005 | { |
|
- | 1006 | Fat16_Deinit(); |
|
- | 1007 | return(direntry_exist); |
|
- | 1008 | } |
|
- | 1009 | dir = (DirEntry_t *)file->Cache; // set pointer to directory |
|
- | 1010 | // search all directory entries within that sector |
|
767 | 1011 | for(dir_entry = 0; dir_entry < DIRENTRIES_PER_SECTOR; dir_entry++) |
|
768 | 1012 | { // check for existing dir entry |
|
769 | //________________________________________________________________________________________________________________________________________ |
1013 | switch((u8)dir[dir_entry].Name[0]) |
- | 1014 | { |
|
- | 1015 | case SLOT_EMPTY: |
|
770 | // Funtion: u16 GetFatClusterIndex(File *file); |
1016 | case SLOT_DELETED: |
771 | // |
1017 | // ignore empty or deleted dir entries |
772 | // Description: This function returns the clusterindex of the cluster specified by file->cluster_pointer of the specified file. |
- | |
773 | // |
1018 | break; |
774 | //________________________________________________________________________________________________________________________________________ |
- | |
775 | 1019 | default: |
|
776 | u16 GetFatClusterOffset(File *file) |
- | |
Line 777... | Line 1020... | ||
777 | { |
1020 | // if existing check attributes before names are compared will safe performance |
- | 1021 | if ((dir[dir_entry].Attribute & attribmask) != attribfilter) break; // attribute must match |
|
Line 778... | Line 1022... | ||
778 | u32 fat_sector_offset = 0; |
1022 | // then compare the name to the giveb dirname (first 11 characters include 8 chars of basename and 3 chars extension.) |
- | 1023 | i = 0; |
|
Line -... | Line 1024... | ||
- | 1024 | while((i < 11) && (dir[dir_entry].Name[i] == dirname[i])) i++; |
|
779 | 1025 | if (i < 10) break; // names does not match |
|
- | 1026 | // if dirname and attribute have matched |
|
780 | fat_sector_offset = ((file->cluster_pointer) - (FirstDataCluster)); // Calculate index of actual cluster in FAT. |
1027 | file->Attribute = dir[dir_entry].Attribute; // store attribute of found dir entry |
781 | fat_sector_offset /= SectorsPerCluster; |
- | |
782 | fat_sector_offset += 2; // In Fat16 clusterpositions have an offset of two. |
- | |
783 | - | ||
784 | return((u16)fat_sector_offset); |
- | |
785 | } |
- | |
786 | - | ||
787 | //________________________________________________________________________________________________________________________________________ |
- | |
788 | // Funtion: u16 GetFatSectorIndex(File *file); |
- | |
789 | // |
1028 | file->FirstSectorOfFirstCluster = Fat16ClusterToSector(dir[dir_entry].StartCluster); // set sector of first data cluster |
790 | // Description: This function returns the sectorindex of the cluster specified by file->cluster_pointer of the specified file. |
1029 | file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster; |
791 | // |
- | |
792 | //________________________________________________________________________________________________________________________________________ |
- | |
793 | - | ||
794 | u16 GetFatSectorIndex(File *file) |
- | |
795 | { |
- | |
796 | u16 fat_pointer = 0; |
- | |
797 | - | ||
798 | fat_pointer = GetFatClusterOffset(file); |
- | |
799 | fat_pointer = fat_pointer % 0x100; // Calculate the clusterposition in the fat |
- | |
800 | - | ||
801 | return(fat_pointer); |
1030 | file->SectorOfCurrCluster = 0; |
802 | } |
- | |
803 | - | ||
804 | //________________________________________________________________________________________________________________________________________ |
- | |
805 | // Funtion: u16 CreateFileInDirectory(u8 *, File *) |
1031 | file->ByteOfCurrSector = 0; |
- | 1032 | file->DirectorySector = curr_sector; // current sector |
|
- | 1033 | file->DirectoryIndex = dir_entry; // current direntry in current sector |
|
806 | // |
1034 | file->Size = dir[dir_entry].Size; |
807 | // Description: This function looks for the next free position in the directory and creates an entry. The type of an directoryentry is |
1035 | direntry_exist = 1; // mark as found |
808 | // specified by the attribute. |
1036 | dir_entry = DIRENTRIES_PER_SECTOR; // stop for-loop |
809 | // bit0: unused |
1037 | } // end of first byte of name check |
- | 1038 | } |
|
810 | // bit1: archive |
1039 | dir_sector++; // search next sector |
811 | // bit2: read_only |
- | |
812 | // bit3: system |
1040 | // stop if we reached the end of the cluster or the end of the root dir |
813 | // bit4: directory |
1041 | }while((dir_sector < max_dir_sector) && (!direntry_exist)); |
- | 1042 | ||
- | 1043 | // if we are seaching in the data area and the file not found in this cluster so take next cluster. |
|
- | 1044 | if(!direntry_exist && ( Partition.FirstDataSector <= file->FirstSectorOfCurrCluster)) |
|
- | 1045 | { |
|
- | 1046 | end_of_directory_not_reached = GetNextCluster(file); // updates File->FirstSectorOfCurrCluster |
|
- | 1047 | } |
|
- | 1048 | }while((end_of_directory_not_reached) && (!direntry_exist)); // repeat until a next cluster exist an no |
|
- | 1049 | return(direntry_exist); |
|
- | 1050 | } |
|
- | 1051 | ||
814 | // bit5: volume |
1052 | |
815 | //________________________________________________________________________________________________________________________________________ |
1053 | /****************************************************************************************************************************************/ |
- | 1054 | /* Function: CreateDirectoryEntry(s8 *, u16, File_t *) */ |
|
816 | 1055 | /* */ |
|
- | 1056 | /* Description: This function looks for the next free position in the directory and creates an entry. */ |
|
- | 1057 | /* The type of an directory entry is specified by the file attribute. */ |
|
- | 1058 | /* */ |
|
- | 1059 | /* Returnvalue: Return 0 on error */ |
|
- | 1060 | /****************************************************************************************************************************************/ |
|
- | 1061 | u8 CreateDirectoryEntry(s8 *dirname, u8 attrib, File_t *file) |
|
- | 1062 | { |
|
- | 1063 | u32 dir_sector, max_dir_sector, curr_sector; |
|
817 | u8 CreateFileInDirectory(s8 *fname, File *file) |
1064 | u16 dir_entry = 0; |
- | 1065 | u16 subdircluster, dircluster = 0; |
|
- | 1066 | u16 end_of_directory_not_reached = 0; |
|
- | 1067 | u8 i = 0; |
|
818 | { |
1068 | u8 retvalue = 0; |
819 | u16 rootentry = 0; // index to an entry in the rootdirectory. |
1069 | DirEntry_t *dir; |
Line 820... | Line -... | ||
820 | u16 cnt_enries_searched = 0; // count the number of rootentries which have been searched already. |
- | |
821 | u8 i = 0; |
- | |
822 | u16 sector_offset = 0; // index to the sector of the Rootentry which is searched momentarily |
- | |
823 | u8 retvalue = 0; |
1070 | |
824 | u32 cluster_temp = 0; |
- | |
825 | u16 cluster = 0; |
- | |
826 | s8 name[11] = " "; |
- | |
827 | - | ||
828 | SeperateFileName(fname,name); |
1071 | if((!Partition.IsValid) || (file == NULL) || (dirname == NULL)) return (retvalue); |
829 | 1072 | // It is not checked here that the dir entry that should be created is already existent! |
|
830 | cluster_temp = (u32)FindNextFreeCluster(file); // the next free cluster on the disk. |
- | |
831 | 1073 | ||
832 | if(cluster_temp) // if a free cluster is available: |
- | |
833 | { |
- | |
834 | cluster = (u16)cluster_temp; // remember the index of the free datacluster found for the directory entry. |
1074 | // Dir entries can be created only in file-clusters that have |
835 | cluster_temp -=2; // Clusterposition is ((position in FAT)-2). first two entries in FAT are reserved. |
- | |
836 | cluster_temp *= SectorsPerCluster; // Calculate relative sectorindex of first datacluster. |
- | |
837 | file->start_cluster = (FirstDataCluster + cluster_temp); // Calculate absolute sectorposition of first datacluster. |
- | |
838 | file->cluster_pointer = file->start_cluster; // start reading the file with the first sector of the first datacluster. |
- | |
839 | - | ||
840 | // directory starts at sector specified by dir_sector. This can be the rootdirectory or any other directory. |
- | |
841 | do |
- | |
842 | { // search the next 16 rootentries in this sector of the roordirectory. |
- | |
843 | rootentry=0; |
- | |
844 | SDC_GetSector((u32)(CWD + sector_offset),file->buffer); // Read the Rootdirectory. |
- | |
845 | DirectoryEntry = (struct DirEntry *)file->buffer; |
- | |
846 | while((rootentry<16) && (!retvalue)) |
1075 | // the dir-flag set in its attribute or within the root directory |
847 | { |
- | |
848 | if((DirectoryEntry[rootentry].attribute == 0) || (DirectoryEntry[rootentry].attribute == 0xE5)) // empty directory entry found |
- | |
849 | { |
1076 | |
850 | for(i=0;i<11;i++) DirectoryEntry[rootentry].name[i] = name[i];// Kopie the filename and the file extension to the directoryentry. |
1077 | file->FirstSectorOfFirstCluster = 0; |
851 | DirectoryEntry[rootentry].attribute = _FILE; // Set the fileattribute to archive to reserve the directoryentry. |
1078 | // no current directory exist therefore assume creating in the root |
852 | DirectoryEntry[rootentry].startcluster = cluster; // copy the location of the first datacluster to the directoryentry. |
1079 | if(file->DirectorySector == 0) |
- | 1080 | { |
|
853 | DirectoryEntry[rootentry].size = 0; // the new createted file has no content yet. |
1081 | max_dir_sector = (Partition.MaxRootEntries * DIRENTRY_SIZE)/BYTES_PER_SECTOR; |
- | 1082 | dircluster = 0; |
|
854 | file->directory_sector = (u32) (CWD + sector_offset); |
1083 | file->FirstSectorOfFirstCluster = Partition.FirstRootDirSector; |
855 | file->directory_index = (u8) rootentry; |
1084 | } |
856 | retvalue = 1; |
1085 | // within the root directory area we can read sectors sequentially until the end of this area |
857 | SDC_PutSector((u32)(CWD + sector_offset),file->buffer); |
1086 | else if((Partition.FirstRootDirSector <= file->DirectorySector) && (file->DirectorySector < Partition.FirstDataSector)) |
- | 1087 | { |
|
- | 1088 | max_dir_sector = (Partition.MaxRootEntries * DIRENTRY_SIZE)/BYTES_PER_SECTOR; |
|
- | 1089 | } |
|
- | 1090 | // within the data clusters we can read sectors sequentially only within the cluster |
|
- | 1091 | else if((Partition.FirstDataSector <= file->DirectorySector) && (file->DirectorySector <= Partition.LastDataSector)) |
|
- | 1092 | { |
|
858 | } |
- | |
- | 1093 | max_dir_sector = Partition.SectorsPerCluster; |
|
- | 1094 | } |
|
859 | rootentry++; |
1095 | else return (retvalue); // bad sector range for directory sector of the file |
- | 1096 | // if search area is not defined yet |
|
- | 1097 | if(file->FirstSectorOfFirstCluster == 0) |
|
- | 1098 | { |
|
860 | cnt_enries_searched++; |
1099 | // check if the directory entry of current file is existent and has the dir-flag set |
861 | } |
- | |
862 | if(!retvalue) // file not found in this sector so take next sector. |
- | |
863 | { |
- | |
864 | rootentry = 0; |
- | |
865 | sector_offset++; |
- | |
866 | } |
- | |
867 | } |
- | |
868 | while((cnt_enries_searched< PossibleRootEntries) && (!retvalue)); |
1100 | file->SectorInCache = file->DirectorySector; // update the sector number of file cache. |
869 | 1101 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector. |
|
870 | } |
1102 | { |
- | 1103 | Fat16_Deinit(); |
|
- | 1104 | return(retvalue); |
|
- | 1105 | } |
|
- | 1106 | dir = (DirEntry_t *)file->Cache; // set pointer to directory |
|
- | 1107 | switch((u8)dir[file->DirectoryIndex].Name[0]) // check if current directory exist |
|
- | 1108 | { |
|
- | 1109 | case SLOT_EMPTY: |
|
- | 1110 | case SLOT_DELETED: |
|
- | 1111 | return (retvalue); |
|
- | 1112 | break; |
|
- | 1113 | default: // and is a real directory |
|
- | 1114 | if((dir[file->DirectoryIndex].Attribute & ATTR_SUBDIRECTORY) != ATTR_SUBDIRECTORY) |
|
- | 1115 | { // current file is not a directory therefore no file or subdirectory can be created here |
|
- | 1116 | return (retvalue); |
|
- | 1117 | } |
|
871 | return(retvalue); // return 1 if file has been created otherwise return 0. |
1118 | break; |
872 | } |
1119 | } |
- | 1120 | dircluster = dir[file->DirectoryIndex].StartCluster; |
|
- | 1121 | file->FirstSectorOfFirstCluster = Fat16ClusterToSector(dircluster); |
|
- | 1122 | } |
|
- | 1123 | ||
- | 1124 | subdircluster = FindNextFreeCluster(file); // get the next free cluster on the disk and mark it as used. |
|
- | 1125 | if(subdircluster) |
|
- | 1126 | { |
|
- | 1127 | file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster; |
|
- | 1128 | file->SectorOfCurrCluster = 0; |
|
- | 1129 | do // loop over all clusters of current directory |
|
- | 1130 | { |
|
- | 1131 | dir_sector = 0; // reset sector counter within a new cluster |
|
- | 1132 | do // loop over all sectors of a cluster or all sectors of the root directory |
|
- | 1133 | { |
|
- | 1134 | curr_sector = file->FirstSectorOfCurrCluster + dir_sector; // calculate sector number |
|
- | 1135 | file->SectorInCache = curr_sector; // upate the sector number of file cache. |
|
873 | 1136 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector. |
|
874 | //________________________________________________________________________________________________________________________________________ |
1137 | { |
- | 1138 | Fat16_Deinit(); |
|
875 | // Funtion: u16 SeekFileInDirectory(s8 *fname, File *) |
1139 | return(retvalue); |
876 | // |
1140 | } |
877 | // Description: this function searches all possible entries withint the actual directory for the specified object. |
1141 | dir = (DirEntry_t *)file->Cache; // set pointer to directory |
878 | // |
- | |
879 | // Returnvalue: This function returns 1 if the directoryentry specified was found. |
1142 | // search all directory entries of a sector |
- | 1143 | for(dir_entry = 0; dir_entry < DIRENTRIES_PER_SECTOR; dir_entry++) |
|
880 | //________________________________________________________________________________________________________________________________________ |
1144 | { // check if current direntry is available |
881 | - | ||
882 | u8 SeekFileInDirectory(s8 *fname, File *file) |
- | |
883 | { |
- | |
884 | u16 rootentry=0; |
- | |
885 | u16 end_of_directory_not_reached = 0; // the directory has been read completely without a result. |
- | |
886 | u8 i=0; |
- | |
887 | u8 retvalue = 0; |
- | |
888 | u32 cluster_temp = 0; |
- | |
889 | s8 name[11] = " "; |
- | |
890 | - | ||
891 | SeperateFileName(fname,name); |
- | |
892 | - | ||
893 | file->cluster_pointer = CWD; // start looking for the file in the actual directory. |
- | |
894 | - | ||
895 | // directory starts at sector specified by dir_sector. This can be the rootdirectory or any other directory. |
- | |
896 | do |
- | |
897 | { // search the next 16 rootentries in this sector of the roordirectory. |
- | |
898 | rootentry=0; |
- | |
899 | SDC_GetSector((u32) file->cluster_pointer,file->buffer); // Read the Rootdirectory. |
- | |
900 | DirectoryEntry = (struct DirEntry *)file->buffer; |
- | |
901 | - | ||
902 | while((!retvalue)&&(rootentry<16)) |
- | |
903 | { |
- | |
904 | i=0; |
- | |
905 | if(DirectoryEntry[rootentry].name[0] != 0xe5) // ignore deleted items. |
- | |
906 | { |
- | |
907 | while((i<=10)&&(DirectoryEntry[rootentry].name[i] == name[i])) |
- | |
908 | { |
1145 | if(((u8)dir[dir_entry].Name[0] == SLOT_EMPTY) || ((u8)dir[dir_entry].Name[0] == SLOT_DELETED)) |
909 | i++; |
1146 | { // a free direntry was found |
910 | } |
- | |
- | 1147 | for(i = 0; i < 11; i++) dir[dir_entry].Name[i] = dirname[i]; // Set dir name |
|
911 | } |
1148 | dir[dir_entry].Attribute = attrib; // Set the attribute of the new directoryentry. |
912 | 1149 | dir[dir_entry].StartCluster = subdircluster; // copy the location of the first datacluster to the directoryentry. |
|
- | 1150 | dir[dir_entry].DateTime = FileDateTime(&SystemTime); // set date/time |
|
913 | if((i==11) && (DirectoryEntry[rootentry].attribute & _FILE)) // entry found!! -> reading startcluster of entry from offset 26. |
1151 | dir[dir_entry].Size = 0; // the new createted file has no content yet. |
914 | { |
1152 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache)) // write back to card |
915 | cluster_temp = (u32)DirectoryEntry[rootentry].startcluster; |
1153 | { |
916 | cluster_temp -=2; // Clusterposition is ((position in FAT)-2). first two entries in FAT are reserved. |
1154 | Fat16_Deinit(); |
- | 1155 | return(retvalue); |
|
- | 1156 | } |
|
- | 1157 | file->FirstSectorOfFirstCluster = Fat16ClusterToSector(subdircluster); // Calculate absolute sectorposition of first datacluster. |
|
- | 1158 | file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster; // Start reading the file with the first sector of the first datacluster. |
|
- | 1159 | file->SectorOfCurrCluster = 0; // reset sector of cureen cluster |
|
917 | cluster_temp *= (u32)SectorsPerCluster; // Calculate positon of first cluster. |
1160 | file->ByteOfCurrSector = 0; // reset the byte location within the current sector |
918 | file->start_cluster = (FirstDataCluster + cluster_temp); |
- | |
919 | file->directory_sector = (u32) file->cluster_pointer; |
1161 | file->Attribute = attrib; // set file attribute to dir attribute |
920 | file->cluster_pointer = file->start_cluster; // start reading the file with the first cluster. |
1162 | file->Size = 0; // new file has no size |
- | 1163 | file->DirectorySector = curr_sector; |
|
- | 1164 | file->DirectoryIndex = dir_entry; |
|
- | 1165 | if((attrib & ATTR_SUBDIRECTORY) == ATTR_SUBDIRECTORY) // if a new directory was created then initilize the data area |
|
- | 1166 | { |
|
- | 1167 | ClearCurrCluster(file); // fill cluster with zeros |
|
- | 1168 | file->SectorInCache = file->FirstSectorOfFirstCluster; |
|
- | 1169 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector. |
|
- | 1170 | { |
|
- | 1171 | Fat16_Deinit(); |
|
- | 1172 | return(retvalue); |
|
- | 1173 | } |
|
- | 1174 | dir = (DirEntry_t *)file->Cache; |
|
- | 1175 | // create direntry "." to current dir |
|
- | 1176 | dir[0].Name[0] = 0x2E; |
|
- | 1177 | for(i = 1; i < 11; i++) dir[0].Name[i] = ' '; |
|
- | 1178 | dir[0].Attribute = ATTR_SUBDIRECTORY; |
|
- | 1179 | dir[0].StartCluster = subdircluster; |
|
- | 1180 | dir[0].DateTime = 0; |
|
- | 1181 | dir[0].Size = 0; |
|
- | 1182 | // create direntry ".." to the upper dir |
|
- | 1183 | dir[1].Name[0] = 0x2E; |
|
- | 1184 | dir[1].Name[1] = 0x2E; |
|
- | 1185 | for(i = 2; i < 11; i++) dir[1].Name[i] = ' '; |
|
- | 1186 | dir[1].Attribute = ATTR_SUBDIRECTORY; |
|
- | 1187 | dir[1].StartCluster = dircluster; |
|
- | 1188 | dir[1].DateTime = 0; |
|
- | 1189 | dir[1].Size = 0; |
|
- | 1190 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache))// read in the sector. |
|
- | 1191 | { |
|
- | 1192 | Fat16_Deinit(); |
|
- | 1193 | return(retvalue); |
|
- | 1194 | } |
|
- | 1195 | } |
|
- | 1196 | retvalue = 1; |
|
921 | file->filesize = (u32) DirectoryEntry[rootentry].size; |
1197 | dir_entry = DIRENTRIES_PER_SECTOR; // stop for-loop |
922 | file->directory_index = (u8) rootentry; |
1198 | } |
923 | retvalue = 1; |
1199 | } |
924 | } |
- | |
925 | rootentry++; |
- | |
926 | } |
1200 | dir_sector++; // search next sector |
927 | if(!retvalue) // file not found in this sector so take next sector. |
1201 | // stop if we reached the end of the cluster or the end of the root dir |
928 | { |
1202 | }while((dir_sector < max_dir_sector) && (!retvalue)); |
929 | end_of_directory_not_reached = GetNextCluster(file); |
1203 | |
930 | } |
1204 | // if we are seaching in the data area and the file not found in this cluster so take next cluster. |
931 | } |
1205 | if(!retvalue && ( Partition.FirstDataSector <= file->FirstSectorOfCurrCluster)) |
932 | while((end_of_directory_not_reached) && (!retvalue)); |
- | |
933 | return(retvalue); |
- | |
934 | } |
1206 | { |
935 | 1207 | end_of_directory_not_reached = GetNextCluster(file); // updates File->FirstSectorOfCurrCluster |
|
936 | 1208 | } |
|
937 | 1209 | }while((end_of_directory_not_reached) && (!retvalue)); |
|
938 | //________________________________________________________________________________________________________________________________________ |
1210 | // Perhaps we are at the end of the last cluster of a directory file an have no free direntry found. |
939 | // Funtion: void SeperateFileName(u8*); |
1211 | // Then we would need to add a cluster to that file and create the new direntry there. |
940 | // |
1212 | // This code is not implemented yet, because its occurs only if more that 32*32=1024 direntries are |
941 | // Description: This function seperates the filename and the fileattribute and brings them into the needed format ('test.txt' -> 'TEST TXT'); |
- | |
942 | // |
- | |
943 | //________________________________________________________________________________________________________________________________________ |
- | |
944 | 1213 | // within a subdirectory of root. |
|
945 | void SeperateFileName(s8 *fname, s8 *name) |
- | |
946 | { |
- | |
947 | u8 readpointer = 0; |
- | |
948 | u8 writepointer = 0; |
1214 | } |
- | 1215 | return(retvalue); // return 1 if file has been created otherwise return 0. |
|
- | 1216 | } |
|
- | 1217 | ||
- | 1218 | /********************************************************************************************************************************************/ |
|
- | 1219 | /* Function: FileExist(const s8* filename, u8 attribfilter, u8 attribmask, File_t *file); */ |
|
- | 1220 | /* */ |
|
949 | u8 attribute = 1; |
1221 | /* Description: This function looks for the specified file including its subdirectories beginning */ |
- | 1222 | /* in the rootdirectory of the drive. If the file is found the Filepointer properties are */ |
|
950 | u8 i = 0; |
1223 | /* updated. */ |
Line 951... | Line -... | ||
951 | - | ||
952 | - | ||
953 | while((writepointer<=10) && (fname[readpointer]!=0)) // the rootdirectoryentry is 8bytes for filename and 3bytes for fileattribute. |
- | |
954 | { // the filename in the rootdirectory is in the format "TEST TXT" without the dot. |
- | |
955 | if(fname[readpointer]=='.') // seperating filename and attribute. |
- | |
956 | { |
- | |
957 | if(attribute) // is the filename "." or ".." ? |
- | |
958 | { |
- | |
959 | name[writepointer] = fname[readpointer]; |
- | |
Line -... | Line 1224... | ||
- | 1224 | /* */ |
|
- | 1225 | /* Returnvalue: 1 if file is found else 0. */ |
|
- | 1226 | /********************************************************************************************************************************************/ |
|
- | 1227 | u8 FileExist(const s8* filename, const u8 attribfilter, const u8 attribmask, File_t *file) |
|
- | 1228 | { |
|
- | 1229 | s8* path = 0; |
|
- | 1230 | s8* subpath = 0; |
|
- | 1231 | u8 af, am, file_exist = 0; |
|
- | 1232 | s8 dirname[12]; // 8+3 + temination character |
|
960 | readpointer++; |
1233 | |
961 | writepointer++; |
1234 | // if incomming pointers are useless return immediatly |
962 | } |
1235 | if ((filename == NULL) || (file == NULL) || (!Partition.IsValid)) return 0; |
963 | else |
1236 | |
- | 1237 | // trace along the filepath |
|
964 | { |
1238 | path = (s8*)filename; // start a the beginning of the filename string |
- | 1239 | file->DirectorySector = 0; // start at RootDirectory with file search |
|
- | 1240 | file->DirectoryIndex = 0; |
|
- | 1241 | // as long as the file was not found and the remaining path is not empty |
|
Line -... | Line 1242... | ||
- | 1242 | while((*path != 0) && !file_exist) |
|
- | 1243 | { // separate dirname and subpath from filepath string |
|
- | 1244 | subpath = SeperateDirName(path, dirname); |
|
- | 1245 | if(subpath != NULL) |
|
- | 1246 | { |
|
- | 1247 | if(*subpath == 0) |
|
- | 1248 | { // empty subpath indicates last element of dir chain |
|
965 | if(fname[(readpointer-1)] != '*') |
1249 | af = attribfilter; |
- | 1250 | am = attribmask; |
|
966 | { |
1251 | } |
- | 1252 | else // it must be a subdirectory and no volume label |
|
- | 1253 | { |
|
- | 1254 | af = ATTR_SUBDIRECTORY; |
|
967 | for(i=writepointer;i<8;i++) |
1255 | am = ATTR_SUBDIRECTORY|ATTR_VOLUMELABEL; |
- | 1256 | } |
|
- | 1257 | if(!DirectoryEntryExist(dirname, af, am, file)) |
|
968 | { |
1258 | { |
969 | name[i] = ' '; |
1259 | return (file_exist); // subdirectory does not exist |
970 | } |
1260 | } |
971 | } |
- | |
972 | readpointer++; |
- | |
973 | writepointer = 8; |
- | |
974 | } |
- | |
975 | } |
- | |
976 | else if(fname[readpointer] == '*') // wildcard found within the filename + extension. |
- | |
977 | { |
- | |
978 | if(writepointer < 8) // in extension. |
- | |
979 | { |
- | |
980 | readpointer++; |
- | |
981 | writepointer = 8; |
- | |
982 | } |
- | |
983 | else // in filename. |
1261 | else |
- | 1262 | { |
|
- | 1263 | if (*subpath == 0) |
|
- | 1264 | { |
|
- | 1265 | file_exist = 1; // last element of path chain was found with the given attribute filter |
|
- | 1266 | } |
|
- | 1267 | } |
|
984 | { |
1268 | } |
985 | writepointer = 11; // jump to the end of the string to terminate this function. |
1269 | else // error seperating the subpath |
986 | } |
1270 | { |
987 | attribute = 0; |
- | |
988 | } |
- | |
989 | else |
- | |
990 | { |
- | |
991 | if((fname[readpointer]>96) && (fname[readpointer]<123)) |
- | |
992 | { |
- | |
993 | name[writepointer]=(fname[readpointer] - 32); // all characters must be upper case. |
- | |
994 | } |
- | |
995 | else |
- | |
996 | { |
- | |
997 | name[writepointer]=fname[readpointer]; |
- | |
998 | } |
- | |
999 | readpointer++; |
- | |
1000 | writepointer++; |
- | |
1001 | attribute = 0; |
- | |
1002 | } |
- | |
1003 | } |
- | |
1004 | } |
1271 | return file_exist; // bad subdir format |
1005 | 1272 | } |
|
1006 | //________________________________________________________________________________________________________________________________________ |
1273 | path = subpath; |
1007 | // Funtion: File * ReserveFilePointer_(void); |
1274 | subpath = 0; |
1008 | // |
1275 | } |
- | 1276 | return (file_exist); |
|
- | 1277 | } |
|
1009 | // Description: This function looks for a free filepointer and reserves it. |
1278 | |
- | 1279 | ||
1010 | // |
1280 | /********************************************************************************************************************************************/ |
Line 1011... | Line 1281... | ||
1011 | // |
1281 | /* Function: FileCreate(const s8* filename, u8 attrib, File_t *file); */ |
- | 1282 | /* */ |
|
1012 | // Return: NULL = faild to reserve a filepointer |
1283 | /* Description: This function looks for the specified file including its subdirectories beginning */ |
1013 | // otherwise filepointer |
- | |
1014 | //________________________________________________________________________________________________________________________________________ |
1284 | /* in the rootdirectory of the partition. If the file is found the Filepointer properties are */ |
1015 | - | ||
1016 | File * ReserveFilePointer(void) |
- | |
- | 1285 | /* updated. If file or its subdirectories are not found they will be created */ |
|
- | 1286 | /* */ |
|
1017 | { |
1287 | /* Returnvalue: 1 if file was created else 0. */ |
1018 | File *file; |
1288 | /********************************************************************************************************************************************/ |
1019 | file = NULL; |
- | |
1020 | u8 temp; |
1289 | u8 FileCreate(const s8* filename, const u8 attrib, File_t *file) |
1021 | 1290 | { |
|
1022 | for(temp = 0;temp<__MAX_FILES_USED;temp++) |
1291 | s8 *path = 0; |
1023 | { |
- | |
1024 | if(FilePointer[temp].state == _UNUSED) // free filepointer found? |
- | |
1025 | { |
- | |
1026 | file = &FilePointer[temp]; |
- | |
1027 | FilePointer[temp].state = _USED; // mark as used. |
- | |
1028 | FilePointer[temp].mode = 0; // type of access (read/write) not defined yet. |
- | |
1029 | FilePointer[temp].start_cluster = 0; // Sectorpointer to the first sector of the first datacluster of the file. |
- | |
1030 | FilePointer[temp].cluster_pointer = 0; // Pointer to the cluster which is edited at the moment. |
- | |
1031 | FilePointer[temp].sector_index = 0; // The sector which is edited at the moment (cluster_pointer + sector_index). |
- | |
Line -... | Line 1292... | ||
- | 1292 | s8 *subpath = 0; |
|
Line -... | Line 1293... | ||
- | 1293 | u8 af, am, file_created = 0; |
|
- | 1294 | s8 dirname[12]; |
|
- | 1295 | ||
- | 1296 | // if incomming pointers are useless return immediatly |
|
Line -... | Line 1297... | ||
- | 1297 | if ((filename == NULL) || (file == NULL) || (!Partition.IsValid)) return 0; |
|
- | 1298 | ||
- | 1299 | // trace along the filepath |
|
- | 1300 | path = (s8*)filename; // start a the beginning of the filename string |
|
- | 1301 | file->DirectorySector = 0; // start at RootDirectory with file search |
|
- | 1302 | file->DirectoryIndex = 0; |
|
- | 1303 | // as long as the file was not created and the remaining file path is not empty |
|
- | 1304 | while((*path != 0) && !file_created) |
|
- | 1305 | { // separate dirname and subpath from filepath string |
|
- | 1306 | subpath = SeperateDirName(path, dirname); |
|
- | 1307 | if(subpath != NULL) |
|
- | 1308 | { |
|
- | 1309 | if(*subpath == 0) |
|
Line 1032... | Line -... | ||
1032 | FilePointer[temp].byte_index = 0; // The bytelocation within the current sector (cluster_pointer + sector_index + byte_index). |
- | |
1033 | FilePointer[temp].filesize = 0; // the size of the opend file in bytes. |
1310 | { // empty subpath indicates last element of dir chain |
1034 | FilePointer[temp].fileposition = 0; // pointer to a character within the file 0 < fileposition < filesize |
- | |
1035 | FilePointer[temp].sector_in_buffer = 0; // the last sector read, wich is still in the sectorbuffer. |
1311 | af = ATTR_NONE; |
1036 | FilePointer[temp].directory_sector = 0; // the sectorposition where the directoryentry has been made. |
- | |
1037 | FilePointer[temp].directory_index = 0; // the index to the directoryentry within the specified sector. |
- | |
1038 | FilePointer[temp].attribute = 0; // the attribute of the file opened. |
1312 | am = ATTR_SUBDIRECTORY|ATTR_VOLUMELABEL; // any file that is no subdir or volume label |
1039 | break; |
- | |
1040 | } |
- | |
1041 | } |
1313 | } |
1042 | return(file); |
1314 | else // it must be a subdirectory and no volume label |
1043 | } |
1315 | { |
1044 | 1316 | af = ATTR_SUBDIRECTORY; |
|
1045 | - | ||
1046 | //________________________________________________________________________________________________________________________________________ |
1317 | am = ATTR_SUBDIRECTORY|ATTR_VOLUMELABEL; |
1047 | // Funtion: void FreeFilePointer_(File *); |
- | |
1048 | // |
- | |
1049 | // Description: This function free's the filepointer by marking it as unused. |
1318 | } |
1050 | // |
1319 | if(!DirectoryEntryExist(dirname, af, am, file)) // if subdir or file is not existent |
1051 | // |
- | |
1052 | // Return: none |
- | |
1053 | // |
- | |
1054 | //________________________________________________________________________________________________________________________________________ |
- | |
1055 | - | ||
1056 | void FreeFilePointer(File *file) |
- | |
1057 | { |
- | |
1058 | u8 cnt = 0; |
- | |
1059 | - | ||
1060 | for(cnt=0;cnt<__MAX_FILES_USED;cnt++) // Is the filepointeradress vaild? |
- | |
1061 | { |
- | |
1062 | if(&FilePointer[cnt] == file) // filepointer found therefore it must be valid |
- | |
1063 | { |
- | |
1064 | FilePointer[cnt].state = _UNUSED; // and can be marked as unused. |
- | |
1065 | } |
- | |
1066 | } |
- | |
1067 | } |
- | |
1068 | 1320 | { // try to create subdir or file |
|
1069 | - | ||
1070 | //________________________________________________________________________________________________________________________________________ |
1321 | if(*subpath == 0) af = attrib; // if last element in dir chain take the given attribute |
- | 1322 | if(!CreateDirectoryEntry(dirname, af, file)) |
|
1071 | // Funtion: void DelteDirectoryEntry(Find *) |
1323 | { // could not be created |
1072 | // |
1324 | return(file_created); |
1073 | // Description: This function deletes the directoryentry of the specified item. |
- | |
1074 | // |
- | |
1075 | // |
1325 | } |
1076 | // returnvalue: 1 if the directory could be created. |
1326 | else if (*subpath == 0) file_created = 1; // last element of path chain was created |
1077 | //________________________________________________________________________________________________________________________________________ |
1327 | } |
1078 | - | ||
1079 | void DeleteDirectoryEntry(Find *item) |
1328 | } |
1080 | { |
1329 | else // error seperating the subpath |
1081 | u8 buffer[512]; |
- | |
1082 | 1330 | { |
|
1083 | - | ||
1084 | SDC_GetSector((u32) item->cluster_pointer,buffer); // Read the Rootdirectory. |
- | |
1085 | DirectoryEntry = (struct DirEntry *)buffer; |
1331 | return file_created; // bad subdir format |
1086 | 1332 | } |
|
1087 | DirectoryEntry[(item->directory_index)-1].attribute = 0; // free the directoryentry. |
- | |
1088 | DirectoryEntry[(item->directory_index)-1].name[0] = 0xE5; // free the directoryentry. |
1333 | path = subpath; |
1089 | SDC_PutSector((u32) item->cluster_pointer,buffer); // Read the Rootdirectory. |
1334 | subpath = 0; |
1090 | } |
- | |
1091 | 1335 | } |
|
1092 | - | ||
1093 | - | ||
1094 | - | ||
1095 | //________________________________________________________________________________________________________________________________________ |
- | |
1096 | // Funtion: u8 CreateSubDirectory(u8 *) |
- | |
1097 | // |
- | |
1098 | // Description: This function creates an directory within the directory specified by CWD |
1336 | return (file_created); |
1099 | // |
- | |
1100 | // |
1337 | } |
1101 | // returnvalue: 1 if the directory could be created. |
1338 | |
1102 | //________________________________________________________________________________________________________________________________________ |
1339 | |
1103 | - | ||
1104 | u8 CreateSubDirectory(s8 *fname) |
- | |
1105 | { |
- | |
1106 | u16 index = 0; // index to an entry in the rootdirectory. |
- | |
1107 | u16 cnt_entries_searched = 0; // count the number of rootentries which have been searched already. |
- | |
1108 | u16 i = 0; |
- | |
1109 | u16 sector_offset = 0; // index to the sector of the entry which is searched momentarily |
- | |
1110 | u8 retvalue = 0; |
1340 | /********************************************************************************************************************************************/ |
1111 | u32 cluster_temp = 0; |
1341 | /* Function: File_t * fopen_(s8* filename, s8 mode); */ |
1112 | u16 cluster = 0; |
1342 | /* */ |
1113 | File file; |
1343 | /* Description: This function looks for the specified file in the rootdirectory of the drive. If the file is found the number of the */ |
1114 | s8 name[11] = {" "}; |
1344 | /* corrosponding filepointer is returned. Only modes 'r' (reading) and 'a' append are implemented yet. */ |
1115 | 1345 | /* */ |
|
- | 1346 | /* Returnvalue: The filepointer to the file or 0 if faild. */ |
|
- | 1347 | /********************************************************************************************************************************************/ |
|
1116 | SeperateFileName(fname,name); |
1348 | File_t * fopen_(const s8 *filename, const s8 mode) |
- | 1349 | { |
|
- | 1350 | File_t *file = 0; |
|
- | 1351 | ||
- | 1352 | if((!Partition.IsValid) || (filename == 0)) return(file); |
|
- | 1353 | ||
1117 | cluster_temp = (u32)FindNextFreeCluster(&file); // the next free cluster on the disk. |
1354 | // Look for an unused filepointer in the file pointer list? |
- | 1355 | file = LockFilePointer(); |
|
- | 1356 | // if no unused file pointer was found return 0 |
|
1118 | 1357 | if(file == NULL) return(file); |
|
- | 1358 | ||
- | 1359 | // now we have found a free filepointer and claimed it |
|
1119 | if(cluster_temp) // if a free cluster is available: |
1360 | // so let initiate its property values |
1120 | { |
1361 | file->FirstSectorOfFirstCluster = 0; // Sectorpointer to the first sector of the first datacluster of the file. |
- | 1362 | file->FirstSectorOfCurrCluster = 0; // Pointer to the cluster which is edited at the moment. |
|
- | 1363 | file->SectorOfCurrCluster = 0; // The sector which is edited at the moment (cluster_pointer + sector_index). |
|
- | 1364 | file->ByteOfCurrSector = 0; // The bytelocation within the current sector (cluster_pointer + sector_index + byte_index). |
|
- | 1365 | file->Mode = mode; // mode of fileoperation (read,write) |
|
1121 | cluster = (u16)cluster_temp; // remember the index of the free datacluster found for the directory entry. |
1366 | file->Size = 0; // the size of the opened file in bytes. |
- | 1367 | file->Position = 0; // pointer to a byte within the file 0 < fileposition < filesize |
|
1122 | cluster_temp -=2; // Clusterposition is ((position in FAT)-2). first two entries in FAT are reserved. |
1368 | file->SectorInCache = 0; // the last sector read, wich is still in the sectorbuffer. |
- | 1369 | file->DirectorySector = 0; // the sectorposition where the directoryentry has been made. |
|
- | 1370 | file->DirectoryIndex = 0; // the index to the directoryentry within the specified sector. |
|
- | 1371 | file->Attribute = 0; // the attribute of the file opened. |
|
- | 1372 | ||
- | 1373 | // check if a real file (no directory) to the given filename exist |
|
- | 1374 | if(FileExist(filename, ATTR_NONE, ATTR_SUBDIRECTORY|ATTR_VOLUMELABEL, file)) |
|
1123 | cluster_temp *= SectorsPerCluster; // Calculate relative sectorindex of first datacluster. |
1375 | { // file exist |
1124 | file.start_cluster = (FirstDataCluster + cluster_temp); // Calculate absolute sectorposition of first datacluster. |
1376 | switch(mode) // check mode |
1125 | file.cluster_pointer = file.start_cluster; // start reading the file with the first sector of the first datacluster. |
- | |
1126 | 1377 | { |
|
1127 | // -Initialise new cluster to zero-------------------------------------------------------- |
1378 | case 'a': // if mode is: append to file |
- | 1379 | if((file->Attribute & ATTR_READONLY) == ATTR_READONLY) |
|
- | 1380 | { // file is marked as readonly --> do not open this file |
|
- | 1381 | fclose_(file); |
|
1128 | for(i=0;i<512;i++) |
1382 | file = NULL; |
Line 1129... | Line -... | ||
1129 | { |
- | |
1130 | file.buffer[i] = 0; // initialise buffer to zero |
1383 | } |
1131 | } |
1384 | else |
1132 | for(sector_offset=0;sector_offset<SectorsPerCluster;sector_offset++) // initialise all sectors of new cluster with buffer. |
- | |
- | 1385 | { // file is not marked as read only --> goto end of file |
|
1133 | { |
1386 | fseek_(file, 0, SEEK_END); // point to the end of the file |
1134 | SDC_PutSector((u32)(file.start_cluster + sector_offset),file.buffer); // save the initialised sector to the card. |
- | |
- | 1387 | } |
|
1135 | } |
1388 | break; |
1136 | // -Create directoryentry "." ------------------------------------------------------------- |
1389 | case 'w': // if mode is: write to file |
1137 | DirectoryEntry = (struct DirEntry *)file.buffer; |
- | |
1138 | DirectoryEntry[0].name[0] = '.'; |
1390 | if((file->Attribute & ATTR_READONLY) == ATTR_READONLY) |
1139 | DirectoryEntry[0].attribute = _DIRECTORY; |
1391 | { // file is marked as readonly --> do not open this file |
1140 | DirectoryEntry[0].startcluster = cluster; |
1392 | fclose_(file); |
1141 | // -Create directoryentry "." ------------------------------------------------------------- |
- | |
1142 | DirectoryEntry[1].name[0] = '.'; |
- | |
1143 | DirectoryEntry[1].name[1] = '.'; |
- | |
1144 | DirectoryEntry[1].attribute = _DIRECTORY; |
- | |
1145 | if(CWD == RootDirectory) |
- | |
Line 1146... | Line 1393... | ||
1146 | { |
1393 | file = NULL; |
Line 1147... | Line -... | ||
1147 | DirectoryEntry[1].startcluster = 0; |
- | |
1148 | } |
- | |
1149 | else |
- | |
1150 | { |
- | |
1151 | cluster_temp = (CWD - FirstDataCluster); |
- | |
1152 | cluster_temp /= SectorsPerCluster; |
- | |
1153 | cluster_temp -= 2; |
- | |
1154 | DirectoryEntry[1].startcluster = (u16) cluster_temp; |
- | |
1155 | } |
- | |
1156 | SDC_PutSector((u32) file.start_cluster,file.buffer); // save the initialised sector to the card. |
- | |
1157 | // -create directoryentry within the cwd -------------------------------------------------- |
1394 | } |
1158 | sector_offset = 0; |
1395 | else |
- | 1396 | { // file is not marked as read only --> goto start of file |
|
1159 | cnt_entries_searched = 0; |
1397 | // free all clusters of that file |
1160 | do |
1398 | DeleteClusterChain(SectorToFat16Cluster(file->FirstSectorOfFirstCluster)); |
1161 | { // search the next 16 rootentries in this sector of the roordirectory. |
1399 | // mar an empy cluster as the last one and store the corresponding sector |
1162 | index=0; |
1400 | file->FirstSectorOfFirstCluster = Fat16ClusterToSector(FindNextFreeCluster(file)); |
1163 | SDC_GetSector((u32)(CWD + sector_offset),file.buffer); // Read the actual directory. |
1401 | file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster; |
- | 1402 | file->SectorOfCurrCluster = 0; |
|
1164 | DirectoryEntry = (struct DirEntry *)file.buffer; |
1403 | file->ByteOfCurrSector = 0; |
1165 | while((index<16) && (!retvalue)) |
1404 | file->Size = 0; |
1166 | { |
1405 | file->Position = 0; |
1167 | if((DirectoryEntry[index].attribute == 0) || (DirectoryEntry[index].attribute == 0xE5)) // empty directory entry found |
- | |
- | 1406 | fseek_(file, 0, SEEK_SET); |
|
1168 | { |
1407 | } |
1169 | for(i=0;i<11;i++) DirectoryEntry[index].name[i] = name[i]; // Kopie the filename and the file extension to the directoryentry. |
1408 | break; |
1170 | DirectoryEntry[index].attribute = _DIRECTORY; // Set the fileattribute to archive to reserve the directoryentry. |
1409 | case 'r': // if mode is: read from file |
1171 | DirectoryEntry[index].startcluster = cluster; // copy the location of the first datacluster to the directoryentry. |
- | |
1172 | DirectoryEntry[index].size = 0; // the new createted file has no content yet. |
1410 | // goto end of file |
1173 | file.directory_sector = (u32) (CWD + sector_offset); |
1411 | fseek_(file, 0, SEEK_SET); |
- | 1412 | break; |
|
- | 1413 | default: // other modes are not supported |
|
- | 1414 | fclose_(file); |
|
1174 | file.directory_index = (u8) index; |
1415 | file = NULL; |
- | 1416 | break; |
|
1175 | retvalue = 1; |
1417 | } |
1176 | SDC_PutSector((u32)(CWD + sector_offset),file.buffer); |
1418 | return(file); |
- | 1419 | } |
|
1177 | } |
1420 | else // file does not exist |
- | 1421 | { |
|
- | 1422 | switch(mode) // check mode |
|
- | 1423 | { |
|
- | 1424 | case 'a': |
|
- | 1425 | case 'w': // if mode is write or append |
|
- | 1426 | // try to create the file |
|
1178 | index++; |
1427 | if(!FileCreate(filename, ATTR_ARCHIVE, file)) |
1179 | cnt_entries_searched++; |
- | |
1180 | } |
1428 | { // if it could not be created |
1181 | if(!retvalue) // file not found in this sector so take next sector. |
1429 | fclose_(file); |
Line -... | Line 1430... | ||
- | 1430 | file = NULL; |
|
- | 1431 | } |
|
- | 1432 | break; |
|
- | 1433 | case 'r': // else opened for 'r' |
|
- | 1434 | default: // of unsupported mode |
|
- | 1435 | fclose_(file); |
|
- | 1436 | file = NULL; |
|
- | 1437 | break; |
|
- | 1438 | } |
|
1182 | { |
1439 | return(file); |
- | 1440 | } |
|
1183 | index = 0; |
1441 | // we should never come to this point |
1184 | sector_offset++; |
1442 | fclose_(file); |
1185 | } |
1443 | file = NULL; |
1186 | } |
- | |
1187 | while((cnt_entries_searched< PossibleRootEntries) && (!retvalue)); |
1444 | return(file); |
- | 1445 | } |
|
1188 | 1446 | ||
1189 | } |
1447 | /****************************************************************************************************************************************************/ |
- | 1448 | /* Function: fflush_(File *); */ |
|
1190 | return(retvalue); // return 1 if file has been created otherwise return 0. |
1449 | /* */ |
- | 1450 | /* Description: This function writes the data already in the buffer but not yet written to the file. */ |
|
1191 | } |
1451 | /* */ |
1192 | - | ||
- | 1452 | /* Returnvalue: 0 on success EOF on error */ |
|
- | 1453 | /****************************************************************************************************************************************************/ |
|
- | 1454 | s16 fflush_(File_t *file) |
|
- | 1455 | { |
|
1193 | 1456 | DirEntry_t *dir; |
|
1194 | //________________________________________________________________________________________________________________________________________ |
1457 | |
- | 1458 | if((file == NULL) || (!Partition.IsValid)) return (EOF); |
|
1195 | // Funtion: u16 SeekSubDirectory(s8 *fname) |
1459 | |
Line -... | Line 1460... | ||
- | 1460 | switch(file->Mode) |
|
- | 1461 | { |
|
- | 1462 | case 'a': |
|
- | 1463 | case 'w': |
|
1196 | // |
1464 | if(file->ByteOfCurrSector > 0) // has data been added to the file? |
- | 1465 | { |
|
Line 1197... | Line 1466... | ||
1197 | // Description: looks for the specified directory within the CWD. |
1466 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache))// save the data still in the buffer |
1198 | // |
1467 | { |
1199 | // Returnvalue: If the specified directory was found the startcluster is returned. otherwise 0. |
1468 | Fat16_Deinit(); |
1200 | //________________________________________________________________________________________________________________________________________ |
1469 | return(EOF); |
1201 | - | ||
1202 | u16 SeekSubDirectory(s8 *fname) |
- | |
1203 | { |
1470 | } |
- | 1471 | } |
|
1204 | u16 index = 0; |
1472 | file->SectorInCache = file->DirectorySector; |
1205 | u16 end_of_directory_not_reached = 0; // the directory has been read completely without a result. |
1473 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache)) // read the directory entry for this file. |
1206 | u8 i = 0; |
- | |
1207 | u16 cluster_temp = 0; |
- | |
1208 | s8 name[11] = " "; |
1474 | { |
1209 | File file; |
- | |
1210 | - | ||
1211 | SeperateFileName(fname,name); |
1475 | Fat16_Deinit(); |
1212 | 1476 | return(EOF); |
|
1213 | file.cluster_pointer = CWD; // start looking for the file in the actual directory. |
- | |
1214 | file.start_cluster = CWD; // start looking for the file in the actual directory. |
1477 | } |
1215 | - | ||
- | 1478 | ||
1216 | // directory starts at sector specified by dir_sector. This can be the rootdirectory or any other directory. |
1479 | dir = (DirEntry_t *)file->Cache; |
1217 | do |
1480 | dir[file->DirectoryIndex].Size = file->Size; // update file size |
- | 1481 | dir[file->DirectoryIndex].DateTime = FileDateTime(&SystemTime); // update date time |
|
1218 | { // search the next 16 rootentries in this sector of the roordirectory. |
1482 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache)) // write back to sd-card |
1219 | index=0; |
- | |
1220 | SDC_GetSector((u32) file.cluster_pointer,file.buffer); // Read the Rootdirectory. |
- | |
1221 | DirectoryEntry = (struct DirEntry *)file.buffer; |
1483 | { |
1222 | - | ||
1223 | while((!cluster_temp)&&(index<16)) |
- | |
1224 | { |
- | |
1225 | i=0; |
- | |
1226 | if(DirectoryEntry[index].name[0] != 0xe5) // ignore deleted items. |
- | |
1227 | { |
- | |
Line 1228... | Line 1484... | ||
1228 | while((i<=10)&&(DirectoryEntry[index].name[i] == name[i])) |
1484 | Fat16_Deinit(); |
1229 | { |
- | |
1230 | i++; |
1485 | return(EOF); |
1231 | } |
- | |
1232 | } |
1486 | } |
1233 | - | ||
1234 | if((i==11) && (DirectoryEntry[index].attribute & _DIRECTORY)) // entry found!! -> reading startcluster of entry from offset 26. |
- | |
1235 | { |
- | |
1236 | cluster_temp = (u16)DirectoryEntry[index].startcluster; |
1487 | break; |
1237 | } |
1488 | case 'r': |
1238 | index++; |
1489 | default: |
1239 | } |
1490 | return(EOF); |
1240 | if(!cluster_temp) // file not found in this sector so take next sector. |
1491 | break; |
1241 | { |
1492 | |
- | 1493 | } |
|
- | 1494 | return(0); |
|
1242 | end_of_directory_not_reached = GetNextCluster(&file); |
1495 | } |
1243 | } |
1496 | |
1244 | } |
1497 | /****************************************************************************************************************************************/ |
Line 1245... | Line -... | ||
1245 | while((end_of_directory_not_reached) && (!cluster_temp)); |
- | |
1246 | return(cluster_temp); |
1498 | /* Function: fclose_(File *file); */ |
- | 1499 | /* */ |
|
1247 | } |
1500 | /* Description: This function closes the open file by writing the remaining data */ |
1248 | - | ||
1249 | 1501 | /* from the buffer to the device and entering the filesize in the directory entry. */ |
|
1250 | - | ||
- | 1502 | /* */ |
|
- | 1503 | /* Returnvalue: 0 on success EOF on error */ |
|
1251 | //________________________________________________________________________________________________________________________________________ |
1504 | /****************************************************************************************************************************************/ |
1252 | // Funtion: u8 mkdir_(u8 *) |
1505 | s16 fclose_(File_t *file) |
1253 | // |
- | |
1254 | // Description: This function checks if the directory to be created already exists. If not the directory will be created. |
1506 | { |
1255 | // |
1507 | s16 returnvalue = EOF; |
1256 | // |
- | |
1257 | // returnvalue: 1 if the directory could be created. |
- | |
1258 | //________________________________________________________________________________________________________________________________________ |
- | |
1259 | 1508 | ||
1260 | u8 mkdir_(s8 *fname) |
- | |
1261 | { |
- | |
1262 | u8 retvalue = 0; |
- | |
Line -... | Line 1509... | ||
- | 1509 | if(file == NULL) return(returnvalue); |
|
Line 1263... | Line 1510... | ||
1263 | 1510 | returnvalue = fflush_(file); |
|
1264 | retvalue = SeekSubDirectory(fname); // check wether the specified directory already exists. |
1511 | UnlockFilePointer(file); |
1265 | 1512 | return(returnvalue); |
|
1266 | if(!retvalue) |
- | |
1267 | { |
- | |
1268 | CreateSubDirectory(fname); // if directory doesn't exist, create it. |
- | |
1269 | retvalue = 1; |
1513 | } |
1270 | } |
- | |
1271 | else |
- | |
1272 | { |
- | |
1273 | retvalue = 0; |
1514 | |
- | 1515 | /********************************************************************************************************************************************/ |
|
1274 | } |
1516 | /* Function: fgetc_(File *file); */ |
- | 1517 | /* */ |
|
1275 | 1518 | /* Description: This function reads and returns one character from the specified file. Is the end of the actual sector reached the */ |
|
- | 1519 | /* next sector of the cluster is read. If the last sector of the cluster read the next cluster will be searched in FAT. */ |
|
- | 1520 | /* */ |
|
1276 | return(retvalue); |
1521 | /* Returnvalue: The function returns the character read from the specified memorylocation as u8 casted to s16 or EOF. */ |
- | 1522 | /********************************************************************************************************************************************/ |
|
1277 | } |
1523 | s16 fgetc_(File_t *file) |
1278 | 1524 | { |
|
- | 1525 | s16 c = EOF; |
|
1279 | 1526 | u32 curr_sector; |
|
1280 | //________________________________________________________________________________________________________________________________________ |
1527 | |
1281 | // Funtion: u8 chdir_(u8 *) |
1528 | if( (!Partition.IsValid) || (file == NULL)) return(c); |
Line -... | Line 1529... | ||
- | 1529 | // if the end of the file is not reached, get the next character. |
|
- | 1530 | if((0 < file->Size) && ((file->Position+1) < file->Size) ) |
|
- | 1531 | { |
|
1282 | // |
1532 | curr_sector = file->FirstSectorOfCurrCluster; // calculate the sector of the next character to be read. |
- | 1533 | curr_sector += file->SectorOfCurrCluster; |
|
1283 | // Description: This function changes the CWD to the directory specified. |
1534 | |
1284 | // |
1535 | if(file->SectorInCache != curr_sector) |
1285 | // |
1536 | { |
1286 | // returnvalue: 1 if the directory could be changed. |
- | |
1287 | //________________________________________________________________________________________________________________________________________ |
1537 | file->SectorInCache = curr_sector; |
1288 | - | ||
1289 | u8 chdir_(s8 *fname) |
1538 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache,file->Cache)) |
1290 | { |
1539 | { |
- | 1540 | Fat16_Deinit(); |
|
- | 1541 | return(c); |
|
1291 | u8 retvalue = 0; |
1542 | } |
1292 | s8 name[11] = {" "}; |
1543 | } |
- | 1544 | c = (s16) file->Cache[file->ByteOfCurrSector]; |
|
1293 | 1545 | file->Position++; // increment file position |
|
- | 1546 | file->ByteOfCurrSector++; // goto next byte in sector |
|
1294 | u32 ultemp = 0; |
1547 | if(file->ByteOfCurrSector >= BYTES_PER_SECTOR) // if end of sector |
1295 | 1548 | { |
|
1296 | SeperateFileName(fname,name); |
- | |
1297 | - | ||
1298 | ultemp = (u32)SeekSubDirectory(name); |
1549 | file->ByteOfCurrSector = 0; // reset byte location |
1299 | if(ultemp >= 2) |
1550 | file->SectorOfCurrCluster++; // next sector |
1300 | { |
1551 | if(file->SectorOfCurrCluster >= Partition.SectorsPerCluster) // if end of cluster is reached, the next datacluster has to be searched in the FAT. |
1301 | ultemp -=2; // Clusterposition is ((position in FAT)-2). first two entries in FAT are reserved. |
1552 | { |
1302 | ultemp *= SectorsPerCluster; // Calculate relative sectorindex of first datacluster. |
- | |
1303 | ultemp += FirstDataCluster; |
- | |
1304 | CWD = ultemp; |
- | |
1305 | retvalue = 1; |
1553 | |
1306 | } |
- | |
1307 | else |
- | |
1308 | { |
- | |
1309 | CWD = RootDirectory; |
- | |
1310 | retvalue = 1; |
1554 | if(GetNextCluster(file)) // Sets the clusterpointer of the file to the next datacluster. |
1311 | } |
1555 | { |
1312 | 1556 | file->SectorOfCurrCluster = 0; // start reading new cluster at first sector of the cluster. |
|
1313 | return(retvalue); |
- | |
1314 | } |
1557 | } |
1315 | - | ||
1316 | - | ||
1317 | //________________________________________________________________________________________________________________________________________ |
- | |
1318 | // Funtion: u8 FindItem(s8 *fname, Find *) |
1558 | else // the last cluster was allready reached |
1319 | // |
- | |
1320 | // Description: finds an item (file or directory) within common working directory (cwd). Wildcards '*' or '?' will be considered. |
- | |
1321 | // |
- | |
1322 | // Returnvalue: If an item was found this function returns '1' else '0'. |
- | |
1323 | //________________________________________________________________________________________________________________________________________ |
- | |
1324 | - | ||
1325 | u8 FindItem(Find *item) |
- | |
1326 | { |
- | |
1327 | u16 index = 0; |
- | |
1328 | u16 end_of_directory_not_reached = 0; // the directory has been read completely without a result. |
- | |
1329 | u8 i = 0; |
- | |
1330 | u8 readpointer = 0; |
- | |
1331 | u8 writepointer = 0; |
- | |
1332 | u8 retvalue = 0; |
1559 | { |
1333 | File file; |
- | |
1334 | - | ||
1335 | - | ||
1336 | file.cluster_pointer = item->cluster_pointer; |
- | |
1337 | file.start_cluster = item->cluster_pointer; |
- | |
1338 | index = item->directory_index; |
1560 | file->SectorOfCurrCluster--; // jump back to the last sector in the last cluster |
Line 1339... | Line -... | ||
1339 | // directory starts at sector specified by dir_sector. This can be the rootdirectory or any other directory. |
- | |
1340 | do |
- | |
1341 | { // search the next 16 rootentries in this sector of the roordirectory. |
- | |
1342 | SDC_GetSector((u32) file.cluster_pointer,file.buffer); // Read the Rootdirectory. |
- | |
1343 | DirectoryEntry = (struct DirEntry *)file.buffer; |
- | |
Line -... | Line 1561... | ||
- | 1561 | file->ByteOfCurrSector = BYTES_PER_SECTOR; // set ByteOfCurrSector one byte over sector end |
|
- | 1562 | } |
|
- | 1563 | } |
|
- | 1564 | } |
|
- | 1565 | } |
|
- | 1566 | return(c); |
|
- | 1567 | } |
|
- | 1568 | ||
1344 | 1569 | /********************************************************************************************************************************************/ |
|
1345 | while((!retvalue)&&(index<16)) |
1570 | /* Function: fputc_( const s8 c, File *file); */ |
- | 1571 | /* */ |
|
- | 1572 | /* Description: This function writes a byte to the specified file and takes care of writing the necessary FAT- Entries. */ |
|
1346 | { |
1573 | /* next sector of the cluster is read. If the last sector of the cluster read the next cluster will be searched in FAT. */ |
- | 1574 | /* */ |
|
1347 | i=0; |
1575 | /* Returnvalue: The function returns the character written to the stream or EOF on error. */ |
Line -... | Line 1576... | ||
- | 1576 | /********************************************************************************************************************************************/ |
|
Line 1348... | Line -... | ||
1348 | if(DirectoryEntry[index].name[0] != 0xe5) // ignore deleted items. |
- | |
1349 | { |
- | |
1350 | while((i<=10)&&((DirectoryEntry[index].name[i] == item->searchstring[i]) || (item->searchstring[i]=='*') || item->searchstring[i]=='?')) |
- | |
1351 | { |
- | |
1352 | i++; |
- | |
1353 | } |
1577 | s16 fputc_(const s8 c, File_t *file) |
1354 | } |
- | |
Line 1355... | Line 1578... | ||
1355 | 1578 | { |
|
1356 | if((i==11) && (DirectoryEntry[index].attribute & item->attribute)) |
1579 | u32 curr_sector = 0; |
1357 | { |
1580 | |
1358 | for(readpointer=0;readpointer<=10;readpointer++) |
1581 | if((!Partition.IsValid) || (file == NULL)) return(EOF); |
1359 | { |
- | |
1360 | if(DirectoryEntry[index].name[readpointer] != ' ') |
- | |
1361 | { |
1582 | |
1362 | item->name[writepointer] = DirectoryEntry[index].name[readpointer]; // copy the name of the item found to the find_structure. |
1583 | // If file position equals to file size, then the end of file has reached. |
- | 1584 | // In this chase it has to be checked that the ByteOfCurrSector is BYTES_PER_SECTOR |
|
- | 1585 | // and a new cluster should be appended. |
|
- | 1586 | if((file->Position >= file->Size) && (file->ByteOfCurrSector >= BYTES_PER_SECTOR)) |
|
- | 1587 | { |
|
- | 1588 | if(!AppendCluster(file)) return(EOF); |
|
1363 | writepointer++; |
1589 | } |
- | 1590 | ||
- | 1591 | curr_sector = file->FirstSectorOfCurrCluster; |
|
- | 1592 | curr_sector += file->SectorOfCurrCluster; |
|
- | 1593 | if(file->SectorInCache != curr_sector) |
|
1364 | } |
1594 | { |
1365 | else if((readpointer==7) && (DirectoryEntry[index].attribute == _FILE)) // if the item found is a file |
1595 | file->SectorInCache = curr_sector; |
1366 | { |
1596 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache)) |
1367 | if(DirectoryEntry[index].name[readpointer] != ' ') |
1597 | { |
1368 | { |
1598 | Fat16_Deinit(); |
Line 1369... | Line -... | ||
1369 | item->name[writepointer] = DirectoryEntry[index].name[readpointer]; // copy the name of the item found to the find_structure. |
- | |
1370 | writepointer++; |
1599 | return(EOF); |
- | 1600 | } |
|
1371 | } |
1601 | } |
1372 | item->name[writepointer] = '.'; // then seperate the name and the extension by a '.' at index 8. |
- | |
1373 | writepointer++; |
1602 | |
1374 | } |
- | |
- | 1603 | file->Cache[file->ByteOfCurrSector] = (u8)c; // write databyte into the buffer. The byte will be written to the device at once |
|
1375 | } |
1604 | if(file->Size == file->Position) file->Size++; // a character has been written to the file so the size is incremented only when the character has been added at the end of the file. |
- | 1605 | file->Position++; // the actual positon within the file. |
|
1376 | item->startcluster = (u16)DirectoryEntry[index].startcluster; |
1606 | file->ByteOfCurrSector++; // goto next byte in sector |
1377 | item->directory_index = ++index; |
- | |
1378 | item->cluster_pointer = file.cluster_pointer; |
1607 | if(file->ByteOfCurrSector >= BYTES_PER_SECTOR) // if the end of this sector is reached yet |
1379 | retvalue = 1; |
1608 | { // save the sector to the sd-card |
- | 1609 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache)) |
|
- | 1610 | { |
|
1380 | } |
1611 | Fat16_Deinit(); |
- | 1612 | return(EOF); |
|
1381 | index++; |
1613 | } |
Line 1382... | Line 1614... | ||
1382 | } |
1614 | file->ByteOfCurrSector = 0; // reset byte location |
Line 1383... | Line -... | ||
1383 | if(!retvalue) // file not found in this sector so take next sector. |
- | |
1384 | { |
- | |
1385 | end_of_directory_not_reached = GetNextCluster(&file); |
- | |
1386 | } |
1615 | file->SectorOfCurrCluster++; // next sector |
Line -... | Line 1616... | ||
- | 1616 | if(file->SectorOfCurrCluster >= Partition.SectorsPerCluster)// if end of cluster is reached, the next datacluster has to be searched in the FAT. |
|
- | 1617 | { |
|
- | 1618 | if(!GetNextCluster(file)) // Sets the clusterpointer of the file to the next datacluster. |
|
- | 1619 | { // if current cluster was the last cluster of the file |
|
1387 | index = 0; |
1620 | if(!AppendCluster(file)) // append a new and free cluster at the end of the file. |
- | 1621 | { |
|
- | 1622 | file->SectorOfCurrCluster--; // jump back to last sector of last cluster |
|
1388 | } |
1623 | file->ByteOfCurrSector = BYTES_PER_SECTOR; // set byte location to 1 byte over sector len |
1389 | while((end_of_directory_not_reached) && (!retvalue)); |
1624 | return(EOF); |
- | 1625 | } |
|
- | 1626 | } |
|
1390 | 1627 | else // next cluster |
|
1391 | return(retvalue); |
1628 | { |
1392 | } |
- | |
1393 | 1629 | file->SectorOfCurrCluster = 0; // start reading new cluster at first sector of the cluster. |
|
1394 | 1630 | } |
|
1395 | //________________________________________________________________________________________________________________________________________ |
1631 | } |
1396 | // Funtion: u8 findfirst(s8 *fname, Find *) |
1632 | } |
1397 | // |
1633 | return(0); |
Line 1398... | Line 1634... | ||
1398 | // Description: finds the first item (file or directory) within common working directory (cwd). Wildcards '*' or '?' will be considered. |
1634 | } |
1399 | // |
1635 | |
Line 1400... | Line 1636... | ||
1400 | // Returnvalue: If an item was found this function returns '1' else '0'. |
1636 | |
- | 1637 | /****************************************************************************************************************************************/ |
|
1401 | //________________________________________________________________________________________________________________________________________ |
1638 | /* Function: fread_(void *buffer, s32 size, s32 count, File *File); */ |
1402 | - | ||
1403 | u8 findfirst_(s8 *fname, Find *item, u8 attribute) |
1639 | /* */ |
1404 | { |
- | |
1405 | u8 retvalue = 0; |
1640 | /* Description: This function reads count objects of the specified size */ |
- | 1641 | /* from the actual position of the file to the specified buffer. */ |
|
1406 | u8 i = 0; |
1642 | /* */ |
1407 | - | ||
1408 | for(i=0;i<=11;i++) |
1643 | /* Returnvalue: The function returns the number of objects (not bytes) read from the file. */ |
1409 | { |
1644 | /****************************************************************************************************************************************/ |
1410 | item->searchstring[i] = '*'; // initialise the searchstring with wildcards. |
1645 | u32 fread_(void *buffer, u32 size, u32 count, File_t *file) |
1411 | item->name[i] = 0; |
1646 | { |
1412 | } |
- | |
1413 | - | ||
Line 1414... | Line -... | ||
1414 | SeperateFileName(fname,item->searchstring); |
- | |
1415 | - | ||
1416 | item->cluster_pointer = CWD; // findfirst_ starts at the beginning of the cwd. |
- | |
1417 | item->directory_index = 0; |
1647 | u32 object_cnt = 0; // count the number of objects read from the file. |
Line 1418... | Line -... | ||
1418 | item->attribute = attribute; |
- | |
- | 1648 | u32 object_size = 0; // count the number of bytes read from the actual object. |
|
1419 | 1649 | u8 *pbuff = 0; // a pointer to the actual bufferposition. |
|
1420 | retvalue = FindItem(item); |
1650 | u8 success = 1; // no error occured during read operation to the file. |
1421 | - | ||
1422 | return(retvalue); |
1651 | s16 c; |
1423 | } |
- | |
1424 | - | ||
1425 | - | ||
1426 | //________________________________________________________________________________________________________________________________________ |
- | |
1427 | // Funtion: u8 findnext(Find *) |
- | |
1428 | // |
1652 | |
1429 | // Description: finds the first item (file or directory) within common working directory (cwd). Wildcards '*' or '?' will be considered. |
- | |
1430 | // |
1653 | if((!Partition.IsValid) || (file == NULL) || (buffer == NULL)) return(0); |
1431 | // Returnvalue: If an item was found this function returns '1' else '0'. |
1654 | |
Line -... | Line 1655... | ||
- | 1655 | pbuff = (u8 *) buffer; // cast the void pointer to an u8 * |
|
- | 1656 | ||
- | 1657 | while((object_cnt < count) && success) |
|
- | 1658 | { |
|
- | 1659 | object_size = size; |
|
- | 1660 | while((size > 0) && success) |
|
- | 1661 | { |
|
- | 1662 | c = fgetc_(file); |
|
- | 1663 | if(c != EOF) |
|
- | 1664 | { |
|
- | 1665 | *pbuff = (u8)c; // read a byte from the buffer to the opened file. |
|
- | 1666 | pbuff++; |
|
- | 1667 | size--; |
|
- | 1668 | } |
|
- | 1669 | else // error or end of file reached |
|
- | 1670 | { |
|
- | 1671 | success = 0; |
|
- | 1672 | } |
|
- | 1673 | } |
|
- | 1674 | if(success) object_cnt++; |
|
- | 1675 | } |
|
- | 1676 | return(object_cnt); // return the number of objects succesfully read from the file |
|
- | 1677 | } |
|
Line -... | Line 1678... | ||
- | 1678 | ||
1432 | //________________________________________________________________________________________________________________________________________ |
1679 | |
1433 | 1680 | /****************************************************************************************************************************************/ |
|
- | 1681 | /* Function: fwrite_(void *buffer, s32 size, s32 count, File *file); */ |
|
1434 | u8 findnext_(Find *item) |
1682 | /* */ |
1435 | { |
1683 | /* Description: This function writes count objects of the specified size */ |
- | 1684 | /* from the buffer pointer to the actual position in the file. */ |
|
- | 1685 | /* */ |
|
1436 | u8 retvalue = 0; |
1686 | /* Returnvalue: The function returns the number of objects (not bytes) read from the file. */ |
1437 | u8 i = 0; |
1687 | /****************************************************************************************************************************************/ |
1438 | - | ||
- | 1688 | u32 fwrite_(void *buffer, u32 size, u32 count, File_t *file) |
|
Line -... | Line 1689... | ||
- | 1689 | { |
|
- | 1690 | u32 object_cnt = 0; // count the number of objects written to the file. |
|
- | 1691 | u32 object_size = 0; // count the number of bytes written from the actual object. |
|
- | 1692 | u8 *pbuff = 0; // a pointer to the actual bufferposition. |
|
- | 1693 | u8 success = 1; // no error occured during write operation to the file. |
|
- | 1694 | s16 c; |
|
- | 1695 | ||
- | 1696 | if((!Partition.IsValid) || (file == NULL) || (buffer == NULL)) return(0); |
|
- | 1697 | ||
- | 1698 | pbuff = (u8 *) buffer; // cast the void pointer to an u8 * |
|
- | 1699 | ||
- | 1700 | while((object_cnt < count) && success) |
|
- | 1701 | { |
|
- | 1702 | object_size = size; |
|
- | 1703 | while((size > 0) && success) |
|
- | 1704 | { |
|
- | 1705 | c = fputc_(*pbuff, file); // write a byte from the buffer to the opened file. |
|
- | 1706 | if(c != EOF) |
|
- | 1707 | { |
|
- | 1708 | pbuff++; |
|
- | 1709 | size--; |
|
- | 1710 | } |
|
- | 1711 | else |
|
- | 1712 | { |
|
1439 | for(i=0;i<=11;i++) // copy the name of the searched item to the findstructure. |
1713 | success = 0; |
1440 | { |
1714 | } |
1441 | item->name[i] = 0; |
1715 | } |
1442 | } |
1716 | if(success) object_cnt++; |
1443 | 1717 | } |
|
1444 | retvalue = FindItem(item); // search the item. |
1718 | |
1445 | 1719 | return(object_cnt); // return the number of objects succesfully written to the file |
|
1446 | return(retvalue); |
1720 | } |
1447 | } |
1721 | |
1448 | 1722 | ||
1449 | 1723 | /****************************************************************************************************************************************/ |
|
- | 1724 | /* Function: fputs_(const s8 *string, File_t *File); */ |
|
- | 1725 | /* */ |