Subversion Repositories NaviCtrl

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 ingob 1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + Copyright (c) 2008 Ingo Busker, Holger Buss
171 ingob 6
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
1 ingob 7
// + FOR NON COMMERCIAL USE ONLY
8
// + www.MikroKopter.com
9
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
88 killagreg 10
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
11
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
12
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
13
// + bzgl. der Nutzungsbedingungen aufzunehmen.
1 ingob 14
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
15
// + Verkauf von Luftbildaufnahmen, usw.
16
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
88 killagreg 17
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
1 ingob 18
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
21
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
22
// + eindeutig als Ursprung verlinkt werden
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
25
// + Benutzung auf eigene Gefahr
26
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
27
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
171 ingob 28
// + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
1 ingob 29
// + mit unserer Zustimmung zulässig
30
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
32
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
88 killagreg 33
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
1 ingob 34
// + this list of conditions and the following disclaimer.
35
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
36
// +     from this software without specific prior written permission.
88 killagreg 37
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permitted
1 ingob 38
// +     for non-commercial use (directly or indirectly)
88 killagreg 39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
1 ingob 40
// +     with our written permission
88 killagreg 41
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
42
// +     clearly linked as origin
171 ingob 43
// +   * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
1 ingob 44
//
45
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
46
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
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
88 killagreg 55
// +  POSSIBILITY OF SUCH DAMAGE.
1 ingob 56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
41 ingob 57
#include <stdio.h>
58
#include "91x_lib.h"
119 killagreg 59
#include "timer1.h"
41 ingob 60
#include "fat16.h"
61
#include "sdc.h"
62
#include "uart1.h"
1 ingob 63
 
64
//________________________________________________________________________________________________________________________________________
88 killagreg 65
// Module name:                 fat16.c
1 ingob 66
// Compiler used:               avr-gcc 3.4.5
211 killagreg 67
// Last Modifikation:   20.03.2010
68
// Version:                             2.10
88 killagreg 69
// Authors:                             Stephan Busker & Gregor Stobrawa
41 ingob 70
// Description:                 Source files for FAT16 implementation with read and write-access
71
//                                              Copyright (C) 2008 Stephan Busker & Gregor Stobrawa
1 ingob 72
//........................................................................................................................................
41 ingob 73
// Functions:                   extern s16              Fat16_Init(void);
88 killagreg 74
//                                              extern s16              Fat16_Deinit(void);
211 killagreg 75
//                                              extern s8*              FAT16_GetVolumeLabel(void);
41 ingob 76
//                                              extern File_t * fopen_(const u8 *filename, const s8 mode);
77
//                                              extern s16              fclose_(File_t *File);
211 killagreg 78
//                                              extern u8               fexist_(s8 * const filename);
41 ingob 79
//                                              extern s16              fflush_(File_t *File);
80
//                                              extern s16      fseek_(File_t *File, s32 offset, s16 origin);
81
//                                              extern s16              fgetc_(File_t *File);
82
//                                              extern s16              fputc_(u8 c, File_t *File);
88 killagreg 83
//                                              extern u32              fread_(void *buffer, u32 size, u32 count, File_t *File);
41 ingob 84
//                                              extern u32              fwrite_(void *buffer, u32 size, u32 count, File_t *File);
85
//                                              extern s16              fputs_(const u8 *string, File_t *File);
86
//                                              extern u8 *     fgets_(u8 *string, s16 length, File_t *File);
211 killagreg 87
//                                              extern u8               feof_(File_t * const file);
1 ingob 88
//........................................................................................................................................
41 ingob 89
// ext. functions:              extern SD_Result_t SDC_Init(void;)
90
//                                              extern SD_Result_t SDC_Deinit(void);
88 killagreg 91
//                      extern SD_Result_t SDC_GetSector (u32,u8 *);
41 ingob 92
//                                              extern SD_Result_t SDC_PutSector (u32,u8 *);
1 ingob 93
//........................................................................................................................................
94
//
95
// URL:                                 www.Mikro-Control.de
96
// mailto:                              stephan.busker@mikro-control.de
97
//________________________________________________________________________________________________________________________________________
98
 
41 ingob 99
/*
100
FAT16 Drive Layout:
101
Description                                             Offset
102
Volume Boot Sector                                      Start of Partition
103
Fat Tables                                                      Start + # of Reserved Sectors
104
Root Directory Entry                            Start + # of Reserved + (# of Sectors Per FAT * 2)
105
Data Area (Starts with Cluster #2)      Start + # of Reserved + (# of Sectors Per FAT * 2) + ((Maximum Root Directory Entries * 32) / Bytes per Sector)
106
*/
1 ingob 107
 
108
 
41 ingob 109
/*
110
________________________________________________________________________________________________________________________________________
88 killagreg 111
 
112
        Structure of an partition entry
41 ingob 113
________________________________________________________________________________________________________________________________________
1 ingob 114
 
41 ingob 115
        Partition Entry is 16 bytes long
116
*/
117
typedef struct
118
{
119
        u8      PartitionState;                         // Current State of Partition (00h=Inactive, 80h=Active)
120
        u8      BeginningHead;                          // Beginning of Partition - Head
121
        u16     BeginningCylSec;                        // Beginning of Partition - Cylinder/Sector (See Below)
122
        u8      Type;                                           // Type of Partition (See List Below)
123
        u8      EndHead;                                        // End of Partition - Head
124
        u16     EndCylSec;                                      // End of Partition - Cylinder/Sector
125
        u32     NoSectorsBeforePartition;       // Number of Sectors between the MBR and the First Sector in the Partition
126
        u32     NoSectorsPartition      ;               // Number of Sectors in the Partition
88 killagreg 127
} __attribute__((packed)) PartitionEntry_t;
1 ingob 128
 
41 ingob 129
/*
130
Coding of Cylinder/Sector words
1 ingob 131
 
41 ingob 132
Cylinder is 10 bits:  [7:0] at [15:8] and [9:8] at [7:6]
88 killagreg 133
Sector is 5 bits:  [5:0] at [5:0]
41 ingob 134
*/
1 ingob 135
 
41 ingob 136
// Partition Types:
137
#define PART_TYPE_UNKNOWN                       0x00
138
#define PART_TYPE_FAT12                         0x01
139
#define PART_TYPE_XENIX                         0x02
140
#define PART_TYPE_FAT16_ST_32_MB        0x04
141
#define PART_TYPE_EXTDOS                        0x05
142
#define PART_TYPE_FAT16_LT_32_MB        0x06
143
#define PART_TYPE_NTFS                          0x07
144
#define PART_TYPE_FAT32                         0x0B
145
#define PART_TYPE_FAT32LBA                      0x0C
146
#define PART_TYPE_FAT16LBA                      0x0E
147
#define PART_TYPE_EXTDOSLBA                     0x0F
148
#define PART_TYPE_EISA                          0x12
149
#define PART_TYPE_ONTRACK                       0x33
150
#define PART_TYPE_NOVELL                        0x40
151
#define PART_TYPE_DYNAMIC                       0x42
152
#define PART_TYPE_PCIX                          0x4B
153
#define PART_TYPE_LINUX_SWAP            0x82
154
#define PART_TYPE_LINUX_NATIVE          0x83
155
#define PART_TYPE_LINUX_LVM                     0x8E
156
#define PART_TYPE_PHOENIXSAVE           0xA0
157
#define PART_TYPE_FREEBSD                       0xA5
158
#define PART_TYPE_OPENBSD                       0xA6
159
#define PART_TYPE_NETNBSD                       0xA9
160
#define PART_TYPE_CPM                           0xDB
161
#define PART_TYPE_DBFS                          0xE0
162
#define PART_TYPE_BBT                           0xFF
1 ingob 163
 
164
 
41 ingob 165
/*
166
________________________________________________________________________________________________________________________________________
88 killagreg 167
 
168
        Structure of the MasterBootRecord
41 ingob 169
________________________________________________________________________________________________________________________________________
1 ingob 170
 
41 ingob 171
        Master Boot Record is 512 bytes long
172
        The Master Boot Record is the same for pretty much all Operating Systems.
173
        It is located on the first Sector of the Hard Drive, at Cylinder 0, Head 0, Sector 1
174
*/
175
typedef struct
176
{
88 killagreg 177
        u8                              ExecutableCode[446];    // 446 bytes for machine start code
178
        PartitionEntry_t        PartitionEntry1;                // 16 bytes for partition entry 1
179
        PartitionEntry_t        PartitionEntry2;                // 16 bytes for partition entry 2
180
        PartitionEntry_t        PartitionEntry3;                // 16 bytes for partition entry 3
181
        PartitionEntry_t        PartitionEntry4;                // 16 bytes for partition entry 4
182
        u16                                     ExecutableMarker;               // BIOS-Signature (0x55 0xAA)
183
} __attribute__((packed)) MBR_Entry_t;
1 ingob 184
 
185
 
41 ingob 186
/*
187
________________________________________________________________________________________________________________________________________
88 killagreg 188
 
189
        Structure of the VolumeBootRecord
41 ingob 190
________________________________________________________________________________________________________________________________________
24 StephanB 191
 
41 ingob 192
        The Volume Boot Record is 512 bytes long
193
        This information is located in the first sector of every partition.
194
*/
195
typedef struct
196
{
197
        u8  JumpCode[3];                        // Jump Code + NOP
88 killagreg 198
        s8  OEMName[8];                         // OEM Name
41 ingob 199
        u16 BytesPerSector;                     // Bytes Per Sector
200
        u8  SectorsPerCluster;          // Sectors Per Cluster
201
        u16 ReservedSectors;            // Reserved Sectors
202
        u8  NoFATCopies;                        // Number of Copies of FAT
88 killagreg 203
        u16 MaxRootEntries;                     // Maximum Root Directory Entries
41 ingob 204
        u16 NoSectorsInPartSml32MB;     // Number of Sectors in Partition Smaller than 32 MB
205
        u8  MediaDescriptor;            // Media Descriptor (0xF8 for Hard Disks)
206
        u16 SectorsPerFAT;                      // Sectors Per FAT
207
        u16 SectorsPerTrack;            // Sectors Per Track
208
        u16 NoHeads;                            // Number of Heads
209
        u32 NoHiddenSectors;            // Number of Hidden Sectors     in Partition
210
        u32 NoSectors;                          // Number of Sectors in Partition
211
        u16     DriveNo;                                // Logical Drive Number of Partition
212
        u8  ExtendedSig;                        // Extended Signature (0x29)
213
        u32 SerialNo;                           // Serial Number of the Partition
214
        s8  VolumeName[11];                     // Volume Name of the Partititon
215
        s8  FATName[8];                         // FAT Name (FAT16)
88 killagreg 216
        u8  ExecutableCode[446];        // 446 bytes for machine start code
217
        u16 ExecutableMarker;           // Executable Marker (0x55 0xAA)
218
} __attribute__((packed)) VBR_Entry_t;
1 ingob 219
 
220
 
221
 
41 ingob 222
/*
223
________________________________________________________________________________________________________________________________________
224
 
88 killagreg 225
        Structure of an directory entry
41 ingob 226
________________________________________________________________________________________________________________________________________
227
 
228
        Directory entry is 32 bytes.
229
*/
230
typedef struct
1 ingob 231
{
41 ingob 232
        s8      Name[8];                                        // 8 bytes name, padded with spaces.
233
        u8      Extension[3];                           // 3 bytes extension, padded with spaces.
234
        u8      Attribute;                                      // attribute of the directory entry (unused,archive,read-only,system,directory,volume)
211 killagreg 235
        u8  Res1;                                               // should be zero
236
        u8  CreationTime10ms;                   // subsecond resolution of creation time
237
        u16 CreationTime;                               // Time of creation h:m:s
238
        u16 CreationDate;                               // Date of creation Y.M.D
239
        u16 LastAccessDate;             // The date where the file was last accessed
240
        u8      Res2[2];                                    // should be zero
241
        u16 ModTime;                                    // date of last write access
242
        u16 ModDate;                                    // date of last write access to the file or directory.
41 ingob 243
        u16 StartCluster;                               // first cluster of the file or directory.
244
        u32 Size;                                               // size of the file or directory in bytes.
245
}  __attribute__((packed)) DirEntry_t;
1 ingob 246
 
41 ingob 247
#define SLOT_EMPTY              0x00    // slot has never been used
248
#define SLOT_E5                 0x05    // the real value is 0xe5
249
#define SLOT_DELETED            0xE5    // file in this slot deleted
1 ingob 250
 
41 ingob 251
#define ATTR_NONE               0x00    // normal file
252
#define ATTR_READONLY           0x01    // file is readonly
253
#define ATTR_HIDDEN                     0x02    // file is hidden
254
#define ATTR_SYSTEM                     0x04    // file is a system file
255
#define ATTR_VOLUMELABEL        0x08    // entry is a volume label
256
#define ATTR_LONG_FILENAME      0x0F    // this is a long filename entry
257
#define ATTR_SUBDIRECTORY       0x10    // entry is a directory name
258
#define ATTR_ARCHIVE            0x20    // file is new or modified
24 StephanB 259
 
260
 
41 ingob 261
/*
262
________________________________________________________________________________________________________________________________________
88 killagreg 263
 
264
        Structure of an entry within the fileallocationtable.
41 ingob 265
________________________________________________________________________________________________________________________________________
266
*/
267
typedef struct
268
{
269
        u16  NextCluster;                               // the next cluster of the file.
270
} __attribute__((packed)) Fat16Entry_t;
1 ingob 271
 
41 ingob 272
// secial fat entries
273
#define FAT16_CLUSTER_FREE                      0x0000
274
#define FAT16_CLUSTER_RESERVED          0x0001
275
#define FAT16_CLUSTER_USED_MIN          0x0002
276
#define FAT16_CLUSTER_USED_MAX          0xFFEF
277
#define FAT16_CLUSTER_ROOTDIR_MIN       0xFFF0
278
#define FAT16_CLUSTER_ROOTDIR_MAX       0xFFF6
279
#define FAT16_CLUSTER_BAD                       0xFFF7
280
#define FAT16_CLUSTER_LAST_MIN          0xFFF8
281
#define FAT16_CLUSTER_LAST_MAX          0xFFFF
282
 
88 killagreg 283
/*****************************************************************************************************************************************/
41 ingob 284
/*                                                                                                                                                                                                                                                                               */
285
/*      Global variables needed for read- or write-acces to the FAT16- filesystem.                                                                                                                       */
286
/*                                                                                                                                                                                                                                                                               */
287
/*****************************************************************************************************************************************/
288
 
289
#define MBR_SECTOR                                      0x00    // the masterboot record is located in sector 0.
290
#define DIRENTRY_SIZE                           32              //bytes
291
#define DIRENTRIES_PER_SECTOR           BYTES_PER_SECTOR/DIRENTRY_SIZE
292
#define FAT16_BYTES                                     2
293
#define FAT16_ENTRIES_PER_SECTOR        BYTES_PER_SECTOR/FAT16_BYTES
294
 
211 killagreg 295
#define SECTOR_UNDEFINED        0x00000000L
296
#define CLUSTER_UNDEFINED       0x0000
297
 
88 killagreg 298
#define FSTATE_UNUSED   0
41 ingob 299
#define FSTATE_USED             1
300
 
301
typedef struct
1 ingob 302
{
41 ingob 303
        u8      IsValid;                                // 0 means invalid, else valid
304
        u8      SectorsPerCluster;              // how many sectors does a cluster contain?
305
        u8      FatCopies;                              // Numbers of copies of the FAT
306
        u16     MaxRootEntries;                 // Possible number of entries in the root directory.
307
        u16     SectorsPerFat;                  // how many sectors does a fat16 contain?
308
        u32 FirstFatSector;                     // sector of the start of the fat
309
        u32 FirstRootDirSector;         // sector of the rootdirectory
310
        u32 FirstDataSector;            // sector of the first cluster containing data (cluster2).
311
        u32 LastDataSector;                     // the last data sector of the partition
210 killagreg 312
        u8 VolumeLabel[12];                     // the volume label
41 ingob 313
} Partition_t;
1 ingob 314
 
41 ingob 315
Partition_t     Partition;                                      // Structure holds partition information
1 ingob 316
 
41 ingob 317
File_t FilePointer[FILE_MAX_OPEN];      // Allocate Memmoryspace for each filepointer used.
1 ingob 318
 
41 ingob 319
 
320
/****************************************************************************************************************************************/
211 killagreg 321
/*      Function:               FileDate(DateTime_t *);                                                                                                                                                                                         */
41 ingob 322
/*                                                                                                                                                                                                                                                                              */
211 killagreg 323
/*      Description:    This function calculates the DOS date from a pointer to a time structure.                                                                                       */
41 ingob 324
/*                                                                                                                                                                                                                                                                              */
211 killagreg 325
/*      Returnvalue:    Returns the DOS date.                                                                                                                                                                                           */
41 ingob 326
/****************************************************************************************************************************************/
211 killagreg 327
u16 FileDate(DateTime_t * pTimeStruct)
328
{
329
        u16 date = 0;
330
        if(pTimeStruct == NULL)   return date;
331
        if(!(pTimeStruct->Valid)) return date;
41 ingob 332
 
211 killagreg 333
        date |= (0x007F & (u16)(pTimeStruct->Year - 1980))<<9; // set year
334
        date |= (0x000F & (u16)(pTimeStruct->Month))<<5; // set month
335
        date |= (0x001F & (u16)(pTimeStruct->Day));
336
        return date;
337
}
338
 
339
/****************************************************************************************************************************************/
340
/*      Function:               FileTime(DateTime_t *);                                                                                                                                                                                         */
341
/*                                                                                                                                                                                                                                                                              */
342
/*      Description:    This function calculates the DOS time from a pointer to a time structure.                                                                                       */
343
/*                                                                                                                                                                                                                                                                              */
344
/*      Returnvalue:    Returns the DOS time.                                                                                                                                                                                           */
345
/****************************************************************************************************************************************/
346
 
347
u16 FileTime(DateTime_t * pTimeStruct)
1 ingob 348
{
211 killagreg 349
        u16 time = 0;
350
        if(pTimeStruct == NULL)   return time;
351
        if(!(pTimeStruct->Valid)) return time;
24 StephanB 352
 
211 killagreg 353
        time |= (0x001F & (u16)(pTimeStruct->Hour))<<11;
354
        time |= (0x003F & (u16)(pTimeStruct->Min))<<5;
355
        time |= (0x001F & (u16)(pTimeStruct->Sec/2));
356
        return time;
1 ingob 357
}
358
 
41 ingob 359
/****************************************************************************************************************************************/
360
/*      Function:               LockFilePointer();                                                                                                                                                                                                      */
361
/*                                                                                                                                                                                                                                                                              */
362
/*      Description:    This function trys to lock a free file pointer.                                                                                                                                         */
363
/*                                                                                                                                                                                                                                                                              */
364
/*      Returnvalue:    Returns the Filepointer on success or 0.                                                                                                                                                        */
365
/****************************************************************************************************************************************/
366
File_t * LockFilePointer(void)
1 ingob 367
{
41 ingob 368
        u8 i;
369
        File_t * File = 0;
370
        for(i = 0; i < FILE_MAX_OPEN; i++)
1 ingob 371
        {
88 killagreg 372
                if(FilePointer[i].State == FSTATE_UNUSED)               // found an unused one
1 ingob 373
                {
88 killagreg 374
                        File = &FilePointer[i];                                         // set pointer to that entry
41 ingob 375
                        FilePointer[i].State = FSTATE_USED;                     // mark it as used
376
                        break;
1 ingob 377
                }
41 ingob 378
        }
379
        return(File);
1 ingob 380
}
381
 
41 ingob 382
/****************************************************************************************************************************************/
383
/*      Function:               UnlockFilePointer(file_t *);                                                                                                                                                                            */
384
/*                                                                                                                                                                                                                                                                              */
385
/*      Description:    This function trys to unlock a file pointer.                                                                                                                                            */
386
/*                                                                                                                                                                                                                                                                              */
387
/*      Returnvalue:    Returns 1 if file pointer was freed else 0.                                                                                                                                                     */
388
/****************************************************************************************************************************************/
389
u8 UnlockFilePointer(File_t * file)
1 ingob 390
{
41 ingob 391
        u8 cnt;
392
        if(file == NULL) return(0);
393
        for(cnt = 0; cnt < FILE_MAX_OPEN; cnt++)
1 ingob 394
        {
41 ingob 395
                if(&FilePointer[cnt] == file)                                           // filepointer to be freed found?
1 ingob 396
                {
88 killagreg 397
                        file->State = FSTATE_UNUSED;
211 killagreg 398
                        file->FirstSectorOfFirstCluster = SECTOR_UNDEFINED;                     // Sectorpointer to the first sector of the first datacluster of the file.
399
                        file->FirstSectorOfCurrCluster  = SECTOR_UNDEFINED;                     // Pointer to the cluster which is edited at the moment.
41 ingob 400
                        file->SectorOfCurrCluster               = 0;                    // The sector which is edited at the moment (cluster_pointer + sector_index).
401
                        file->ByteOfCurrSector                  = 0;                    // The bytelocation within the current sector (cluster_pointer + sector_index + byte_index).
402
                        file->Mode                                              = 0;                    // mode of fileoperation (read,write)
403
                        file->Size                                              = 0;                    // the size of the opend file in bytes.
404
                        file->Position                                  = 0;                    // pointer to a character within the file 0 < fileposition < filesize
211 killagreg 405
                        file->SectorInCache                     = SECTOR_UNDEFINED;                     // the last sector read, wich is still in the sectorbuffer.
406
                        file->DirectorySector                   = SECTOR_UNDEFINED;                     // the sectorposition where the directoryentry has been made.
41 ingob 407
                        file->DirectoryIndex                    = 0;                    // the index to the directoryentry within the specified sector.
408
                        file->Attribute                                 = 0;                    // the attribute of the file opened.
88 killagreg 409
                        file = NULL;
41 ingob 410
                        return(1);
1 ingob 411
                }
41 ingob 412
        }
413
        return(0);
414
}
1 ingob 415
 
41 ingob 416
/****************************************************************************************************************************************/
417
/*      Function:               SeperateDirName(s8*, s8*);                                                                                                                                                                              */
418
/*                                                                                                                                                                                                                                                                              */
419
/*      Description:    This function seperates the first dirname from filepath and brings them                                                                                         */
420
/*                                      into the needed format ('test.txt' -> 'TEST    TXT')                                                                                                                            */
210 killagreg 421
/*                                      The subpath is the pointer to the remaining substring of the filepath                                                                                           */
41 ingob 422
/*                                                                                                                                                                                                                                                                              */
423
/*      Returnvalue:    Return NULL on error or pointer to subpath                                                                                                                                                                                                      */
424
/****************************************************************************************************************************************/
425
s8* SeperateDirName(const s8 *filepath, s8 *dirname)
426
{
427
        s8* subpath = NULL;
428
        u8 readpointer  = 0;
429
        u8 writepointer = 0;
1 ingob 430
 
88 killagreg 431
        // search subpath from beginning of filepath
41 ingob 432
        subpath = NULL;
433
        readpointer     = 0;
434
        if(filepath[0] == '/') readpointer = 1; // ignore first '/'
88 killagreg 435
        while(subpath == NULL)  // search the filepath until a subpath was found.
1 ingob 436
        {
88 killagreg 437
                if(((filepath[readpointer] == 0) || (filepath[readpointer] == '/')))    // if '/' found or end of filepath reached
438
                {
41 ingob 439
                        subpath = (s8*)&filepath[readpointer];                          // store the position of the first "/" found after the beginning of the filenpath
440
                }
441
                readpointer++;
442
        }
1 ingob 443
 
41 ingob 444
        // clear dirname with spaces
445
        dirname[11] = 0; // terminate dirname
446
        for(writepointer = 0; writepointer < 11; writepointer++) dirname[writepointer] = ' ';
447
        writepointer = 0;
448
        // start seperating the dirname from the filepath.
449
        readpointer = 0;
450
        if(filepath[0] == '/') readpointer = 1; // ignore first '/'
451
        while( &filepath[readpointer] < subpath)
1 ingob 452
        {
41 ingob 453
                if(writepointer >= 11) return(NULL);            // dirname to long
454
                if(filepath[readpointer] == '.')                        // seperating dirname and extension.
1 ingob 455
                {
41 ingob 456
                        if(writepointer <= 8)
1 ingob 457
                        {
41 ingob 458
                                readpointer++;                                          // next character in filename
459
                                writepointer = 8;                                       // jump to start of extension
1 ingob 460
                        }
88 killagreg 461
                        else return(NULL);                                              // dirbasename to long
41 ingob 462
                }
463
                else
464
                {
465
                        if((0x60 < filepath[readpointer]) && (filepath[readpointer] < 0x7B))
466
                        {
467
                                dirname[writepointer] = (filepath[readpointer] - 0x20);                                 // all characters must be upper case.
468
                        }
1 ingob 469
                        else
470
                        {
41 ingob 471
                                dirname[writepointer] = filepath[readpointer];
88 killagreg 472
                        }
41 ingob 473
                        readpointer++;
474
                        writepointer++;
1 ingob 475
                }
41 ingob 476
        }
88 killagreg 477
        return(subpath);
1 ingob 478
}
479
 
480
 
41 ingob 481
/**************************************************************************************************************************************+*/
482
/*      Function:       Fat16ClusterToSector( u16 cluster);                                                                                                                                                                             */
483
/*                                                                                                                                                                                                                                                                              */
484
/*      Description:    This function converts a cluster number given by the fat to the corresponding                                                                           */
485
/*                                      sector that points to the start of the data area that is represented by the cluster number.                                                     */
486
/*                                                                                                                                                                                                                                                                              */
487
/*      Returnvalue: The sector number with the data area of the given cluster                                                                                                                          */
488
/****************************************************************************************************************************************/
489
u32     Fat16ClusterToSector(u16 cluster)
490
{
211 killagreg 491
        if(!Partition.IsValid) return SECTOR_UNDEFINED;
218 killagreg 492
        if ((cluster < 2) || (cluster == CLUSTER_UNDEFINED))
211 killagreg 493
        {
494
                return SECTOR_UNDEFINED;
495
        }
496
        else
497
        {
498
                return ( (cluster - 2) * Partition.SectorsPerCluster) + Partition.FirstDataSector; // the first data sector     is represented by the 2nd cluster
499
        }
41 ingob 500
}
501
 
502
/****************************************************************************************************************************************/
503
/*      Function:       SectorToFat16Cluster( u32 sector);                                                                                                                                                                              */
504
/*                                                                                                                                                                                                                                                                              */
505
/*      Description:    This function converts a given sector number given to the corresponding                                                                                         */
506
/*                                      cluster number in the fat that represents this data area.                                                                                                                       */
507
/*                                                                                                                                                                                                                                                                              */
508
/*      Returnvalue: The cluster number representing the data area of the sector.                                                                                                                       */
509
/****************************************************************************************************************************************/
510
u16     SectorToFat16Cluster(u32 sector)
511
{
211 killagreg 512
        if(!Partition.IsValid) return CLUSTER_UNDEFINED;
513
        if((sector == SECTOR_UNDEFINED) || (sector < Partition.FirstDataSector)) return CLUSTER_UNDEFINED;
514
        else return ((u16)((sector - Partition.FirstDataSector) / Partition.SectorsPerCluster) + 2);
41 ingob 515
}
516
 
517
 
518
/****************************************************************************************************************************************/
210 killagreg 519
/*      Function:       Fat16_IsValid(void);                                                                                                                                                                                                    */
90 killagreg 520
/*                                                                                                                                                                                                                                                                              */
210 killagreg 521
/*      Description:    This function return the Fat 16 filesystem state                                                                                                                                        */
90 killagreg 522
/*                                                                                                                                                                                                                                                                              */
523
/*      Returnvalue: The function returns "1" on success                                                                                                                                                                        */
524
/****************************************************************************************************************************************/
525
u8 Fat16_IsValid(void)
526
{
527
        return(Partition.IsValid);
528
}
529
 
530
/****************************************************************************************************************************************/
41 ingob 531
/*      Function:       Fat16_Deinit(void);                                                                                                                                                                                                             */
532
/*                                                                                                                                                                                                                                                                              */
533
/*      Description:    This function uninitializes the fat 16 api                                                                                                                                                      */
534
/*                                                                                                                                                                                                                                                                              */
535
/*      Returnvalue: The function returns "0" on success                                                                                                                                                                        */
536
/****************************************************************************************************************************************/
537
u8 Fat16_Deinit(void)
538
{
539
        s16 returnvalue = 0;
540
        u8 cnt;
146 killagreg 541
 
542
        UART1_PutString("\r\n FAT16 deinit...");
41 ingob 543
        // declare the filepointers as unused.
544
        for(cnt = 0; cnt < FILE_MAX_OPEN; cnt++)
1 ingob 545
        {
41 ingob 546
                if(FilePointer[cnt].State == FSTATE_USED)
1 ingob 547
                {
41 ingob 548
                        returnvalue += fclose_(&FilePointer[cnt]); // try to close open file pointers
1 ingob 549
                }
211 killagreg 550
                else UnlockFilePointer(&FilePointer[cnt]);
88 killagreg 551
 
1 ingob 552
        }
41 ingob 553
        SDC_Deinit();                   // uninitialize interface to sd-card
554
        Partition.IsValid = 0;  // mark data in partition structure as invalid
210 killagreg 555
        Partition.VolumeLabel[0]='\0';
146 killagreg 556
        UART1_PutString("ok");
41 ingob 557
        return(returnvalue);
1 ingob 558
}
559
 
41 ingob 560
/****************************************************************************************************************************************/
561
/*      Function:               Fat16_Init(void);                                                                                                                                                                                                       */
562
/*                                                                                                                                                                                                                                                                          */
563
/*      Description:    This function reads the Masterbootrecord and finds the position of the Volumebootrecord, the FAT and the Rootdir    */
564
/*                                      and stores the information in global variables.                                                                                                                                     */
565
/*                                                                                                                                                                                                                                                                          */
566
/*      Returnvalue:    The function returns "0" if the filesystem is initialized.                                                                                                                      */
567
/****************************************************************************************************************************************/
568
u8 Fat16_Init(void)
88 killagreg 569
{
41 ingob 570
    u8  cnt     = 0;
571
        u32     partitionfirstsector;
88 killagreg 572
        VBR_Entry_t *VBR;
41 ingob 573
        MBR_Entry_t *MBR;
574
        File_t *file;
575
        u8 result = 0;
1 ingob 576
 
110 killagreg 577
        UART1_PutString("\r\n FAT16 init...");
41 ingob 578
        Partition.IsValid = 0;
579
 
580
        // declare the filepointers as unused.
581
        for(cnt = 0; cnt < FILE_MAX_OPEN; cnt++)
1 ingob 582
        {
211 killagreg 583
                UnlockFilePointer(&FilePointer[cnt]);
1 ingob 584
        }
41 ingob 585
        // set current file pinter to first position in list
88 killagreg 586
        file = &FilePointer[0];
587
 
211 killagreg 588
        // try to initialize the sd-card.
88 killagreg 589
        if(SD_SUCCESS != SDC_Init())
590
        {
110 killagreg 591
                UART1_PutString("SD-Card could not be initialized.");
41 ingob 592
                result = 1;
88 killagreg 593
                goto end;
41 ingob 594
        }
1 ingob 595
 
41 ingob 596
        // SD-Card is initialized successfully
597
        if(SD_SUCCESS != SDC_GetSector((u32)MBR_SECTOR,file->Cache))    // Read the MasterBootRecord
1 ingob 598
        {
110 killagreg 599
                UART1_PutString("Error reading the MBR.");
41 ingob 600
                result = 2;
88 killagreg 601
                goto end;
1 ingob 602
        }
41 ingob 603
        MBR = (MBR_Entry_t *)file->Cache;                                               // Enter the MBR using the structure MBR_Entry_t.
88 killagreg 604
        if((MBR->PartitionEntry1.Type == PART_TYPE_FAT16_ST_32_MB) ||
41 ingob 605
           (MBR->PartitionEntry1.Type == PART_TYPE_FAT16_LT_32_MB) ||
606
           (MBR->PartitionEntry1.Type == PART_TYPE_FAT16LBA))
1 ingob 607
        {
41 ingob 608
                // get sector offset 1st partition
609
                partitionfirstsector = MBR->PartitionEntry1.NoSectorsBeforePartition;
610
                // Start of Partition is the Volume Boot Sector
611
                if(SD_SUCCESS != SDC_GetSector(partitionfirstsector,file->Cache)) // Read the volume boot record
1 ingob 612
                {
110 killagreg 613
                        UART1_PutString("Error reading the VBR.");
41 ingob 614
                        result = 3;
88 killagreg 615
                        goto end;
616
                }
1 ingob 617
        }
41 ingob 618
        else  // maybe the medium has no partition assuming sector 0 is the vbr
619
        {
620
                partitionfirstsector = 0;
621
        }
88 killagreg 622
 
41 ingob 623
        VBR = (VBR_Entry_t *) file->Cache;                                              // Enter the VBR using the structure VBR_Entry_t.
624
        if(VBR->BytesPerSector != BYTES_PER_SECTOR)
625
        {
110 killagreg 626
                UART1_PutString("VBR: Sector size not supported.");
41 ingob 627
                result = 4;
88 killagreg 628
                goto end;
41 ingob 629
        }
630
        Partition.SectorsPerCluster             = VBR->SectorsPerCluster;                       // Number of sectors per cluster. Depends on the memorysize of the sd-card.
631
        Partition.FatCopies                     = VBR->NoFATCopies;                                     // Number of fatcopies.
632
        Partition.MaxRootEntries                = VBR->MaxRootEntries;                          // How many Entries are possible in the rootdirectory (FAT16 allows max. 512 entries).
210 killagreg 633
        Partition.SectorsPerFat                 = VBR->SectorsPerFAT;                           // The number of sectors per FAT                                // copy volume label
634
        Partition.VolumeLabel[0] = '\0';                                                                        // set string terminator
41 ingob 635
 
636
        /* Calculate the sectorpositon of the FAT, the Rootdirectory and the first Datacluster. */
88 killagreg 637
        // Calculate the position of the FileAllocationTable:
41 ingob 638
        // Start + # of Reserved Sectors
88 killagreg 639
        Partition.FirstFatSector        =   (u32)(partitionfirstsector + (u32)(VBR->ReservedSectors));
640
        // Calculate the position of the Rootdirectory:
641
        // Start + # of Reserved Sectors + (# of Sectors Per FAT * # of FAT Copies)
41 ingob 642
        Partition.FirstRootDirSector    =   Partition.FirstFatSector + (u32)((u32)Partition.SectorsPerFat*(u32)Partition.FatCopies);
643
        // Calculate the position of the first datacluster:
88 killagreg 644
        // Start + # of Reserved + (# of Sectors Per FAT * # of FAT Copies) + ((Maximum Root Directory Entries * 32) / Bytes per Sector)
41 ingob 645
        Partition.FirstDataSector       =   Partition.FirstRootDirSector + (u32)(Partition.MaxRootEntries>>4);  // assuming 512 Byte Per Sector
646
        // Calculate the last data sector
647
        if(VBR->NoSectors == 0)
648
        {
110 killagreg 649
                UART1_PutString("VBR: Bad number of sectors.");
41 ingob 650
                result = 5;
88 killagreg 651
                goto end;
41 ingob 652
        }
653
        Partition.LastDataSector = Partition.FirstDataSector + VBR->NoSectors - 1;
654
        // check for FAT16 in VBR of first partition
655
        if(!((VBR->FATName[0]=='F') && (VBR->FATName[1]=='A') && (VBR->FATName[2]=='T') && (VBR->FATName[3]=='1')&&(VBR->FATName[4]=='6')))
656
        {
110 killagreg 657
                UART1_PutString("VBR: Partition ist not FAT16 type.");
41 ingob 658
                result = 6;
659
                goto end;
660
        }
661
        Partition.IsValid = 1; // mark data in partition structure as valid
662
        result = 0;
663
        end:
664
        if(result != 0) Fat16_Deinit();
110 killagreg 665
        else UART1_PutString("ok");
88 killagreg 666
        return(result);
1 ingob 667
}
668
 
669
 
41 ingob 670
 
671
/****************************************************************************************************************************************/
672
/* Function:    ClearCurrCluster(File_t*);                                                                                                                                                                                      */
673
/*                                                                                                                                                                                                                                                                              */
674
/* Description: This function fills the current cluster with 0.                                                                                                                                                 */
675
/*                                                                                                                                                                                                                                                                              */
676
/* Returnvalue: The function returns 1 on success else 0.                                                                                                                                                               */
677
/****************************************************************************************************************************************/
678
u8 ClearCurrCluster(File_t * file)
1 ingob 679
{
41 ingob 680
        u8 retvalue = 1;
681
        u32 i;
88 killagreg 682
 
41 ingob 683
        if((!Partition.IsValid) || (file == NULL)) return(0);
684
 
685
        for(i = 0; i < BYTES_PER_SECTOR; i++) file->Cache[i] = 0; // clear file cache
211 killagreg 686
        if(file->FirstSectorOfCurrCluster == SECTOR_UNDEFINED) return (0); // nothing to do 
41 ingob 687
        for(i = 0; i < Partition.SectorsPerCluster; i++)
1 ingob 688
        {
41 ingob 689
                file->SectorInCache = file->FirstSectorOfCurrCluster + i;
690
                if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache))
691
                {
88 killagreg 692
                        Fat16_Deinit();
41 ingob 693
                        retvalue = 0;
694
                }
1 ingob 695
        }
88 killagreg 696
        return(retvalue);
1 ingob 697
}
698
 
41 ingob 699
/*****************************************************************************************************************************************/
700
/* Function:    GetNextCluster(File_t* );                                                                                                                                                                                        */
701
/*                                                                                                                                                                                                                                                                               */
702
/* Description: This function finds the next datacluster of the file specified with File *File.                                                                                  */
703
/*                                                                                                                                                                                                                                                                               */
704
/* Returnvalue: The function returns the next cluster or 0 if the last cluster has already reached.                                                                                                      */
705
/*****************************************************************************************************************************************/
706
u16 GetNextCluster(File_t * file)
1 ingob 707
{
211 killagreg 708
        u16 cluster = CLUSTER_UNDEFINED;
41 ingob 709
        u32 fat_byte_offset, sector, byte;
710
        Fat16Entry_t * fat;
88 killagreg 711
 
41 ingob 712
        if((!Partition.IsValid) || (file == NULL)) return(cluster);
211 killagreg 713
        if(file->FirstSectorOfCurrCluster == SECTOR_UNDEFINED) return(cluster);
41 ingob 714
        // if sector is within the data area
715
        if((Partition.FirstDataSector <= file->FirstSectorOfCurrCluster)&& (file->FirstSectorOfCurrCluster <= Partition.LastDataSector))
1 ingob 716
        {
41 ingob 717
                // determine current file cluster
718
                cluster = SectorToFat16Cluster(file->FirstSectorOfCurrCluster);
719
                // calculate byte offset in the fat for corresponding entry
720
                fat_byte_offset = ((u32)cluster)<<1; // two FAT bytes (16 bits) for every cluster
721
                // calculate the sector that contains the current cluster within the fat
722
                sector = Partition.FirstFatSector + ( fat_byte_offset / BYTES_PER_SECTOR);
723
                // calculate byte offset of the current cluster within that fat sector
724
                byte = fat_byte_offset % BYTES_PER_SECTOR;
725
                // read this sector to the file cache
726
                if(file->SectorInCache != sector)
1 ingob 727
                {
41 ingob 728
                        file->SectorInCache = sector;                                           // update sector stored in buffer
729
                        if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))       // read sector from sd-card
24 StephanB 730
                        {
41 ingob 731
                                Fat16_Deinit();
732
                                return (cluster);
88 killagreg 733
                        }
734
                }
41 ingob 735
                // read the next cluster from cache
736
                fat = (Fat16Entry_t *)(&(file->Cache[byte]));
737
                cluster = fat->NextCluster;
211 killagreg 738
                // if no next cluster exist
41 ingob 739
                if(FAT16_CLUSTER_LAST_MIN <= cluster)
740
                {
211 killagreg 741
                         cluster = CLUSTER_UNDEFINED; // next cluster is undefined
1 ingob 742
                }
24 StephanB 743
                else
744
                {
41 ingob 745
                        file->FirstSectorOfCurrCluster = Fat16ClusterToSector(cluster);
746
                        file->SectorOfCurrCluster = 0;
747
                        file->ByteOfCurrSector = 0;
24 StephanB 748
                }
88 killagreg 749
        }
41 ingob 750
        return(cluster);
1 ingob 751
}
752
 
753
 
41 ingob 754
/****************************************************************************************************************************************/
755
/* Function:    FindNextFreeCluster(File_t *);                                                                                                                                                                          */
756
/*                                                                                                                                                                                                                                                                              */
757
/* Description: This function looks in the fat to find the next free cluster                                                                                                                    */
758
/*                                                                                                                                                                                                                                                                              */
759
/* Returnvalue: The function returns the cluster number of the next free cluster found within the fat.                                                                  */
760
/****************************************************************************************************************************************/
761
u16 FindNextFreeCluster(File_t *file)
1 ingob 762
{
41 ingob 763
        u32 fat_sector;                                 // current sector within the fat relative to the first sector of the fat.
764
        u32     curr_sector;                            // current sector
765
        u16     fat_entry;                                      // index to an fatentry within the actual sector (256 fatentries are possible within one sector).
211 killagreg 766
        u16     free_cluster    = CLUSTER_UNDEFINED;    // next free cluster number.
41 ingob 767
        Fat16Entry_t * fat;
88 killagreg 768
 
41 ingob 769
        if((!Partition.IsValid) || (file == NULL)) return(0);
24 StephanB 770
 
41 ingob 771
        // start searching for an empty cluster at the beginning of the fat.
88 killagreg 772
        fat_sector = 0;
773
        do
1 ingob 774
        {
41 ingob 775
                curr_sector = Partition.FirstFatSector + fat_sector;    // calculate sector to read
776
                file->SectorInCache = curr_sector;                                              // upate the sector number of file cache.
777
                if( SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))              // read sector of fat from sd-card.
778
                {
779
                        Fat16_Deinit();
780
                        return(free_cluster);
781
                }
782
 
783
                fat = (Fat16Entry_t *)file->Cache;                                              // set fat pointer to file cache
784
 
88 killagreg 785
                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.
41 ingob 786
                {
787
                        if(fat[fat_entry].NextCluster == FAT16_CLUSTER_FREE)            // empty cluster found!!
88 killagreg 788
                        {
789
                                fat[fat_entry].NextCluster = FAT16_CLUSTER_LAST_MAX;    // mark this fat-entry as used
41 ingob 790
                                if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache))               // and save the sector at the sd-card.
791
                                {
792
                                        Fat16_Deinit();
793
                                        return(free_cluster);
794
                                }
88 killagreg 795
                                free_cluster = (u16)(fat_sector * FAT16_ENTRIES_PER_SECTOR + (u32)fat_entry);
41 ingob 796
                                fat_entry = FAT16_ENTRIES_PER_SECTOR;                                   // terminate the search for a free cluster in this sector.
797
                        }
798
                }
799
                fat_sector++;                                                                                                   // continue the search in next fat sector
88 killagreg 800
        // repeat until the end of the fat is  reached and no free cluster has been found so far
41 ingob 801
        }while((fat_sector < Partition.SectorsPerFat) && (!free_cluster));
802
        return(free_cluster);
1 ingob 803
}
804
 
805
 
41 ingob 806
/****************************************************************************************************************************************************/
807
/* Function:    s16 fseek_(File_t *, s32 *, u8)                                                                                                                                                                                                         */
808
/*                                                                                                                                                                                                                                                                                                      */
809
/* Description: This function sets the pointer of the stream relative to the position                                                                                                                           */
810
/*                              specified by origin (SEEK_SET, SEEK_CUR, SEEK_END)                                                                                                                                                                      */
211 killagreg 811
/* Returnvalue: Is 0 if seek was successful                                                                                                                                                                                                                                                                     */
41 ingob 812
/****************************************************************************************************************************************************/
813
s16 fseek_(File_t *file, s32 offset, s16 origin)
1 ingob 814
{
41 ingob 815
        s32             fposition       = 0;
816
        s16     retvalue        = 1;
88 killagreg 817
 
211 killagreg 818
        if((!Partition.IsValid) || (file == NULL)) return(retvalue);
41 ingob 819
        switch(origin)
820
        {
821
                case SEEK_SET:                          // Fileposition relative to the beginning of the file.
88 killagreg 822
                        fposition = 0;
41 ingob 823
                        break;
824
                case SEEK_END:                          // Fileposition relative to the end of the file.
825
                        fposition = (s32)file->Size;
826
                        break;
827
                case SEEK_CUR:                          // Fileposition relative to the current position of the file.
828
                default:
829
                        fposition = file->Position;
830
                        break;
1 ingob 831
        }
41 ingob 832
 
833
        fposition += offset;
834
 
835
        if((fposition >= 0) && (fposition <= (s32)file->Size))          // is the pointer still within the file?
1 ingob 836
        {
41 ingob 837
                // reset file position to start of the file
838
                file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster;
839
                file->SectorOfCurrCluster       = 0;
840
                file->ByteOfCurrSector          = 0;
841
                file->Position                          = 0;
211 killagreg 842
                if(file->FirstSectorOfCurrCluster == SECTOR_UNDEFINED) return(retvalue);
41 ingob 843
                while(file->Position < fposition)       // repeat until the current position is less than target
1 ingob 844
                {
41 ingob 845
                        file->Position++;                               // increment file position
88 killagreg 846
                        file->ByteOfCurrSector++;               // next byte in current sector
847
                        if(file->ByteOfCurrSector >= BYTES_PER_SECTOR)
41 ingob 848
                        {
849
                                file->ByteOfCurrSector = 0;                                                                             // reading at the beginning of new sector.
850
                                file->SectorOfCurrCluster++;                                                                    // continue reading in next sector
851
                                if(file->SectorOfCurrCluster >= Partition.SectorsPerCluster)    // if end of cluster is reached, the next datacluster has to be searched in the FAT.
852
                                {
853
                                        if(GetNextCluster(file))                                                                        // Sets the clusterpointer of the file to the next datacluster.
854
                                        {
88 killagreg 855
                                                file->SectorOfCurrCluster = 0;
41 ingob 856
                                        }
857
                                        else // the last cluster was allready reached
858
                                        {
211 killagreg 859
                                                file->SectorOfCurrCluster--;                                                    // jump back to the last sector in the last cluster
41 ingob 860
                                                file->ByteOfCurrSector = BYTES_PER_SECTOR;                              // set ByteOfCurrSector one byte over sector end
861
                                        }
862
                                }
88 killagreg 863
                        }
864
                }
1 ingob 865
        }
88 killagreg 866
        if(file->Position == fposition) retvalue = 0;
1 ingob 867
        return(retvalue);
868
}
869
 
870
 
41 ingob 871
/****************************************************************************************************************************************/
872
/* Function:    u16 DeleteClusterChain(File *file);                                                                                                                                                                             */
873
/*                                                                                                                                                                                                                                                                              */
874
/* Description: This function trances along a cluster chain in the fat and frees all clusters visited.                                                                  */
875
/*                                                                                                                                                                                                                                                                              */
876
/****************************************************************************************************************************************/
877
u8 DeleteClusterChain(u16 StartCluster)
1 ingob 878
{
88 killagreg 879
        u16 cluster;
41 ingob 880
        u32 fat_byte_offset, sector, byte;
881
        Fat16Entry_t * fat;
882
        u8 buffer[BYTES_PER_SECTOR];
883
        u32 sector_in_buffer = 0;
884
        u8 repeat = 0;
885
 
211 killagreg 886
        if(!Partition.IsValid) return(0);
887
        if(StartCluster == CLUSTER_UNDEFINED) return(0);
210 killagreg 888
        cluster = StartCluster; // init chain trace
211 killagreg 889
        // if start cluster is no real cluster
890
    if(FAT16_CLUSTER_LAST_MIN <= cluster) return 1;
41 ingob 891
 
892
        // calculate byte offset in the fat for corresponding entry
893
        fat_byte_offset = ((u32)cluster)<<1; // two FAT bytes (16 bits) for every cluster
894
        // calculate the sector that contains the current cluster within the fat
895
        sector = Partition.FirstFatSector + ( fat_byte_offset / BYTES_PER_SECTOR);
896
        // calculate byte offset of the current cluster within that fat sector
897
        byte = fat_byte_offset % BYTES_PER_SECTOR;
88 killagreg 898
        do
1 ingob 899
        {
41 ingob 900
                if(sector != sector_in_buffer)
1 ingob 901
                {
41 ingob 902
                        // read this sector to buffer
903
                        sector_in_buffer = sector;
904
                        if(SD_SUCCESS != SDC_GetSector(sector_in_buffer, buffer)) return 0;     // read sector from sd-card
88 killagreg 905
                }
41 ingob 906
                // read the next cluster from cache
907
                fat = (Fat16Entry_t *)(&(buffer[byte]));
908
                cluster = fat->NextCluster;
211 killagreg 909
                fat->NextCluster =      FAT16_CLUSTER_FREE; // mark current cluster as free
910
 
911
                if((FAT16_CLUSTER_USED_MIN <= cluster) && (cluster <= FAT16_CLUSTER_USED_MAX) )
912
                {
913
                        repeat = 1;
914
                        // calculate sector byte and byte offset in the fat for the next cluster
915
                        fat_byte_offset = ((u32)cluster)<<1; // two FAT bytes (16 bits) for every cluster
916
                        // calculate the sector that contains the current cluster within the fat
917
                        sector = Partition.FirstFatSector + ( fat_byte_offset / BYTES_PER_SECTOR);
918
                        // calculate byte offset of the current cluster within that fat sector
919
                        byte = fat_byte_offset % BYTES_PER_SECTOR;
920
                }
41 ingob 921
                else repeat = 0;
922
 
923
                // if new sector is not the sector in buffer or the last cluster in the chain was traced
924
                if((sector != sector_in_buffer) || !repeat)
925
                {       // write sector in buffer
210 killagreg 926
                        if(SD_SUCCESS != SDC_PutSector(sector_in_buffer,buffer))
146 killagreg 927
                        {
928
                                Fat16_Deinit();
211 killagreg 929
                                return(0);
146 killagreg 930
                        }
1 ingob 931
                }
41 ingob 932
        }
933
        while(repeat);
934
 
935
        return 1;
1 ingob 936
}
937
 
938
 
41 ingob 939
/****************************************************************************************************************************************/
940
/* Function:    u16 AppendCluster(File *file);                                                                                                                                                                                  */
941
/*                                                                                                                                                                                                                                                                              */
942
/* Description: This function looks in the fat to find the next free cluster and appends it to the file.                                                                */
943
/*                                                                                                                                                                                                                                                                              */
211 killagreg 944
/* Returnvalue: The function returns the appened cluster number or CLUSTER_UNDEFINED of no cluster was appended.                                                */
41 ingob 945
/****************************************************************************************************************************************/
946
u16 AppendCluster(File_t *file)
1 ingob 947
{
211 killagreg 948
        u16 last_cluster, new_cluster = CLUSTER_UNDEFINED;
41 ingob 949
        u32 fat_byte_offset, sector, byte;
950
        Fat16Entry_t * fat;
88 killagreg 951
 
41 ingob 952
        if((!Partition.IsValid) || (file == NULL)) return(new_cluster);
1 ingob 953
 
41 ingob 954
        new_cluster = FindNextFreeCluster(file);        // the next free cluster found on the disk.
211 killagreg 955
        if(new_cluster != CLUSTER_UNDEFINED)
88 killagreg 956
        {       // A free cluster was found and can be added to the end of the file.
41 ingob 957
                fseek_(file, 0, SEEK_END);                                                                                                      // jump to the end of the file
88 killagreg 958
                last_cluster = SectorToFat16Cluster(file->FirstSectorOfCurrCluster);            // determine current file cluster
211 killagreg 959
                if(last_cluster != CLUSTER_UNDEFINED)
41 ingob 960
                {
211 killagreg 961
                        // update FAT entry of last cluster
962
                        fat_byte_offset = ((u32)last_cluster)<<1;
963
                        sector = Partition.FirstFatSector + ( fat_byte_offset / BYTES_PER_SECTOR);
964
                        byte = fat_byte_offset % BYTES_PER_SECTOR;
965
                        // read the sector containing the last cluster of the file
966
                        if(file->SectorInCache != sector)
41 ingob 967
                        {
211 killagreg 968
                                file->SectorInCache = sector;   // update sector stored in buffer
969
                                if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))       // read sector from sd-card
970
                                {
971
                                        Fat16_Deinit();
972
                                        return(0);
973
                                }
974
                        }
975
                        fat = (Fat16Entry_t *)(&(file->Cache[byte]));
976
                        fat->NextCluster = new_cluster;                                                 // append the free cluster to the end of the file in the FAT.
977
                        if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache))               // save the modified sector to the FAT.
978
                        {
41 ingob 979
                                Fat16_Deinit();
211 killagreg 980
                                return(0);
88 killagreg 981
                        }
982
                }
211 killagreg 983
                else // last cluster of the file is undefined
984
                {   // then the new cluster must be the first one of the file
985
                    // and its cluster number must be set in the direntry
986
                        DirEntry_t * dir;
987
                        file->SectorInCache = file->DirectorySector;                            // update the sector number of file cache.
988
                        if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector.
989
                        {
990
                                Fat16_Deinit();
991
                                return(CLUSTER_UNDEFINED);
992
                        }
993
                        dir = (DirEntry_t *)file->Cache;                                                        // set pointer to directory
994
                        dir[file->DirectoryIndex].Res1 = 0;
995
                        dir[file->DirectoryIndex].Res2[0] = 0;
996
                        dir[file->DirectoryIndex].Res2[1] = 0;
997
                        dir[file->DirectoryIndex].StartCluster = new_cluster;           // update startcluster 
998
                    dir[file->DirectoryIndex].ModTime   = FileTime(&SystemTime);// set time
999
                        dir[file->DirectoryIndex].ModDate       = FileDate(&SystemTime);// and date of modification
1000
                        dir[file->DirectoryIndex].LastAccessDate = dir[file->DirectoryIndex].ModDate;
1001
                        dir[file->DirectoryIndex].Size          = 0;
1002
                        // write sector containing the direntry
1003
                        if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache))
1004
                        {
1005
                                Fat16_Deinit();
1006
                                return(CLUSTER_UNDEFINED);
1007
                        }
1008
                        // update file info     
1009
                        file->FirstSectorOfFirstCluster = Fat16ClusterToSector(new_cluster);
1010
                        file->Size = 0;
1011
                        file->Position = 0;
41 ingob 1012
                }
211 killagreg 1013
                // update file pointes
41 ingob 1014
                file->FirstSectorOfCurrCluster = Fat16ClusterToSector(new_cluster);
1015
                file->SectorOfCurrCluster = 0;
1016
                file->ByteOfCurrSector = 0;
1017
        }
1018
        return(new_cluster);
1 ingob 1019
}
1020
 
41 ingob 1021
/****************************************************************************************************************************************************/
1022
/* Function:    DirectoryEntryExist(s8 *, u8, u8, File_t *)                                                                                                                                                                                     */
1023
/*                                                                                                                                                                                                                                                                                                      */
1024
/* Description: This function searches all possible dir entries until the file or directory is found or the end of the directory is reached                     */
1025
/*                                                                                                                                                                                                                                                                                                      */
1026
/* Returnvalue: This function returns 1 if the directory entry specified was found.                                                                                                                                     */
1027
/****************************************************************************************************************************************************/
1028
u8 DirectoryEntryExist(s8 *dirname, u8 attribfilter, u8 attribmask, File_t *file)
1029
{
1030
        u32             dir_sector, max_dir_sector, curr_sector;
1031
        u16     dir_entry = 0;
24 StephanB 1032
 
41 ingob 1033
        u16     end_of_directory_not_reached = 0;
1034
        u8              i = 0;
1035
        u8      direntry_exist = 0;
1036
        DirEntry_t * dir;
24 StephanB 1037
 
41 ingob 1038
        // if incomming pointers are useless return immediatly
88 killagreg 1039
        if((!Partition.IsValid) || (file == NULL) || (dirname == NULL)) return(direntry_exist);
41 ingob 1040
 
88 killagreg 1041
        // dir entries can be searched only in filesclusters that have
1042
        // a corresponding dir entry with adir-flag set in its attribute
41 ingob 1043
        // or direct within the root directory area
88 killagreg 1044
 
211 killagreg 1045
        file->FirstSectorOfFirstCluster = SECTOR_UNDEFINED;
41 ingob 1046
        // no current directory exist therefore assume searching in the root
211 killagreg 1047
        if(file->DirectorySector == SECTOR_UNDEFINED)
24 StephanB 1048
        {
41 ingob 1049
                max_dir_sector = (Partition.MaxRootEntries * DIRENTRY_SIZE)/BYTES_PER_SECTOR;
1050
                file->FirstSectorOfFirstCluster = Partition.FirstRootDirSector;
1051
        }
1052
        // within the root directory area we can read sectors sequentially until the end of this area
1053
        else if((Partition.FirstRootDirSector <= file->DirectorySector) && (file->DirectorySector < Partition.FirstDataSector))
1054
        {
1055
                max_dir_sector = (Partition.MaxRootEntries * DIRENTRY_SIZE)/BYTES_PER_SECTOR;
1056
        }
88 killagreg 1057
        // within the data clusters we can read sectors sequentially only within the cluster
41 ingob 1058
        else if((Partition.FirstDataSector <= file->DirectorySector) && (file->DirectorySector <= Partition.LastDataSector))
1059
        {
1060
                max_dir_sector = Partition.SectorsPerCluster;                           // limit max secters before next cluster
1061
        }
1062
        else return (direntry_exist); // bad sector range for directory sector of the file
1063
        // if search area is not defined yet
211 killagreg 1064
        if(file->FirstSectorOfFirstCluster == SECTOR_UNDEFINED)
41 ingob 1065
        {
1066
                // check if the directory entry of current file is existent and has the dir-flag set
1067
                file->SectorInCache = file->DirectorySector;                            // update the sector number of file cache.
1068
                if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector.
24 StephanB 1069
                {
41 ingob 1070
                        Fat16_Deinit();
1071
                        return(direntry_exist);
24 StephanB 1072
                }
41 ingob 1073
                dir = (DirEntry_t *)file->Cache;                                                        // set pointer to directory
211 killagreg 1074
                switch((u8)dir[file->DirectoryIndex].Name[0])                           // check if current directory exist
24 StephanB 1075
                {
41 ingob 1076
                        case SLOT_EMPTY:
1077
                        case SLOT_DELETED:
1078
                                // the directrory pointer of this file points to a deleted or not existen directory
1079
                                // therefore no file or subdirectory can be created
1080
                                return (direntry_exist);
1081
                                break;
88 killagreg 1082
                        default:        // and is a real directory
41 ingob 1083
                                if((dir[file->DirectoryIndex].Attribute & ATTR_SUBDIRECTORY) != ATTR_SUBDIRECTORY)
1084
                                {       // current file is not a directory therefore no file or subdirectory can be created here
1085
                                        return (direntry_exist);
1086
                                }
1087
                                break;
24 StephanB 1088
                }
41 ingob 1089
                file->FirstSectorOfFirstCluster = Fat16ClusterToSector(dir[file->DirectoryIndex].StartCluster);
24 StephanB 1090
        }
1091
 
41 ingob 1092
        // update current file data area position to start of first cluster
88 killagreg 1093
        file->FirstSectorOfCurrCluster  = file->FirstSectorOfFirstCluster;
1094
        file->SectorOfCurrCluster               = 0;
41 ingob 1095
        file->ByteOfCurrSector                  = 0;
24 StephanB 1096
 
41 ingob 1097
        do // loop over all data clusters of the current directory entry
88 killagreg 1098
        {
41 ingob 1099
                dir_sector = 0; // reset sector counter within a new cluster
1100
                do // loop over all sectors of a cluster or all sectors of the root directory
88 killagreg 1101
                {
41 ingob 1102
                        curr_sector = file->FirstSectorOfCurrCluster + dir_sector;      // calculate sector number
1103
                        file->SectorInCache = curr_sector;                                                      // upate the sector number of file cache.
1104
                        if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read the sector
1105
                        {
1106
                                Fat16_Deinit();
88 killagreg 1107
                                return(direntry_exist);
41 ingob 1108
                        }
1109
                        dir = (DirEntry_t *)file->Cache;                                                        // set pointer to directory
1110
                        // search all directory entries within that sector
1111
                        for(dir_entry = 0; dir_entry < DIRENTRIES_PER_SECTOR; dir_entry++)
1112
                        {   // check for existing dir entry
1113
                                switch((u8)dir[dir_entry].Name[0])
1114
                                {
1115
                                        case SLOT_EMPTY:
1116
                                        case SLOT_DELETED:
1117
                                                // ignore empty or deleted dir entries
1118
                                                break;
1119
                                        default:
1120
                                                // if existing check attributes before names are compared will safe performance
1121
                                                if ((dir[dir_entry].Attribute & attribmask) != attribfilter) break; // attribute must match
1122
                                                // then compare the name to the giveb dirname (first 11 characters include 8 chars of basename and 3 chars extension.)
1123
                                                i = 0;
1124
                                                while((i < 11) && (dir[dir_entry].Name[i] == dirname[i])) i++;
1125
                                                if (i < 10) break; // names does not match
1126
                                                // if dirname and attribute have matched
1127
                                                file->Attribute = dir[dir_entry].Attribute; // store attribute of found dir entry
88 killagreg 1128
                                                file->FirstSectorOfFirstCluster = Fat16ClusterToSector(dir[dir_entry].StartCluster); // set sector of first data cluster
41 ingob 1129
                                                file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster;
1130
                                                file->SectorOfCurrCluster = 0;
1131
                                                file->ByteOfCurrSector = 0;
1132
                                                file->DirectorySector = curr_sector; // current sector
1133
                                                file->DirectoryIndex  = dir_entry; // current direntry in current sector
88 killagreg 1134
                                                file->Size = dir[dir_entry].Size;
1135
                                                direntry_exist = 1; // mark as found
1136
                                                dir_entry = DIRENTRIES_PER_SECTOR;      // stop for-loop
41 ingob 1137
                                } // end of first byte of name check
1138
                        }
1139
                        dir_sector++; // search next sector
1140
                // stop if we reached the end of the cluster or the end of the root dir
1141
                }while((dir_sector < max_dir_sector) && (!direntry_exist));
1 ingob 1142
 
41 ingob 1143
                // if we are seaching in the data area and the file not found in this cluster so take next cluster.
88 killagreg 1144
                if(!direntry_exist && ( Partition.FirstDataSector <= file->FirstSectorOfCurrCluster))
41 ingob 1145
                {
1146
                        end_of_directory_not_reached = GetNextCluster(file);  // updates File->FirstSectorOfCurrCluster
1147
                }
1148
        }while((end_of_directory_not_reached) && (!direntry_exist)); // repeat until a next cluster exist an no
1149
        return(direntry_exist);
1 ingob 1150
}
1151
 
1152
 
41 ingob 1153
/****************************************************************************************************************************************/
1154
/*      Function:               CreateDirectoryEntry(s8 *, u16, File_t *)                                                                                                                                                       */
1155
/*                                                                                                                                                                                                                                                                              */
1156
/*      Description:    This function looks for the next free position in the directory and creates an entry.                                                           */
1157
/*                                      The type of an directory entry is specified by the file attribute.                                                                                                      */
1158
/*                                                                                                                                                                                                                                                                              */
1159
/*      Returnvalue:    Return 0 on error                                                                                                                                                                                                       */
1160
/****************************************************************************************************************************************/
1161
u8 CreateDirectoryEntry(s8 *dirname, u8 attrib, File_t *file)
1 ingob 1162
{
41 ingob 1163
        u32 dir_sector, max_dir_sector, curr_sector;
1164
        u16 dir_entry   = 0;
1165
        u16 subdircluster, dircluster = 0;
1166
        u16 end_of_directory_not_reached = 0;
1167
        u8      i                       = 0;
88 killagreg 1168
        u8      retvalue        = 0;
41 ingob 1169
        DirEntry_t *dir;
1170
 
1171
        if((!Partition.IsValid) || (file == NULL) || (dirname == NULL)) return (retvalue);
1172
        // It is not checked here that the dir entry that should be created is already existent!
88 killagreg 1173
 
1174
        // Dir entries can be created only in file-clusters that have
41 ingob 1175
        // the dir-flag set in its attribute or within the root directory
88 killagreg 1176
 
211 killagreg 1177
        file->FirstSectorOfFirstCluster = SECTOR_UNDEFINED;
41 ingob 1178
        // no current directory exist therefore assume creating in the root
211 killagreg 1179
        if(file->DirectorySector == SECTOR_UNDEFINED)
24 StephanB 1180
        {
41 ingob 1181
                max_dir_sector = (Partition.MaxRootEntries * DIRENTRY_SIZE)/BYTES_PER_SECTOR;
1182
                dircluster = 0;
1183
                file->FirstSectorOfFirstCluster = Partition.FirstRootDirSector;
1184
        }
1185
        // within the root directory area we can read sectors sequentially until the end of this area
1186
        else if((Partition.FirstRootDirSector <= file->DirectorySector) && (file->DirectorySector < Partition.FirstDataSector))
1187
        {
1188
                max_dir_sector = (Partition.MaxRootEntries * DIRENTRY_SIZE)/BYTES_PER_SECTOR;
1189
        }
88 killagreg 1190
        // within the data clusters we can read sectors sequentially only within the cluster
41 ingob 1191
        else if((Partition.FirstDataSector <= file->DirectorySector) && (file->DirectorySector <= Partition.LastDataSector))
1192
        {
1193
                max_dir_sector = Partition.SectorsPerCluster;
1194
        }
88 killagreg 1195
        else return (retvalue); // bad sector range for directory sector of the file
41 ingob 1196
        // if search area is not defined yet
211 killagreg 1197
        if(file->FirstSectorOfFirstCluster == SECTOR_UNDEFINED)
41 ingob 1198
        {
1199
            // check if the directory entry of current file is existent and has the dir-flag set
1200
                file->SectorInCache = file->DirectorySector;                            // update the sector number of file cache.
88 killagreg 1201
                if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector.
41 ingob 1202
                {
1203
                        Fat16_Deinit();
1204
                        return(retvalue);
88 killagreg 1205
                }
41 ingob 1206
                dir = (DirEntry_t *)file->Cache;                                                        // set pointer to directory
218 killagreg 1207
                switch((u8)dir[file->DirectoryIndex].Name[0])                           // check if current directory exist
41 ingob 1208
                {
1209
                        case SLOT_EMPTY:
1210
                        case SLOT_DELETED:
1211
                                return (retvalue);
1212
                                break;
88 killagreg 1213
                        default:        // and is a real directory
41 ingob 1214
                                if((dir[file->DirectoryIndex].Attribute & ATTR_SUBDIRECTORY) != ATTR_SUBDIRECTORY)
1215
                                {       // current file is not a directory therefore no file or subdirectory can be created here
1216
                                        return (retvalue);
1217
                                }
1218
                                break;
1219
                }
1220
                dircluster = dir[file->DirectoryIndex].StartCluster;
88 killagreg 1221
                file->FirstSectorOfFirstCluster = Fat16ClusterToSector(dircluster);
41 ingob 1222
        }
88 killagreg 1223
 
218 killagreg 1224
        // if the new direntry is a subdirectory
1225
        if((attrib & ATTR_SUBDIRECTORY) == ATTR_SUBDIRECTORY)
1226
        {       // get a free clutser for its content
1227
                subdircluster = FindNextFreeCluster(file);      // get the next free cluster on the disk and mark it as used.
1228
        }
1229
        else // a normal file
1230
        {       // has no data cluster after creation
1231
                subdircluster = CLUSTER_UNDEFINED;
1232
        }
1233
 
211 killagreg 1234
        file->FirstSectorOfCurrCluster  = file->FirstSectorOfFirstCluster;
1235
        file->SectorOfCurrCluster               = 0;
1236
        do // loop over all clusters of current directory
41 ingob 1237
        {
211 killagreg 1238
                dir_sector = 0; // reset sector counter within a new cluster
1239
                do // loop over all sectors of a cluster or all sectors of the root directory
88 killagreg 1240
                {
211 killagreg 1241
                        curr_sector = file->FirstSectorOfCurrCluster + dir_sector;      // calculate sector number
1242
                        file->SectorInCache = curr_sector;                                                      // upate the sector number of file cache.
1243
                        if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector.
1 ingob 1244
                        {
211 killagreg 1245
                                Fat16_Deinit();
1246
                                return(retvalue);
1247
                        }
1248
 
1249
                        dir = (DirEntry_t *)file->Cache;                                                        // set pointer to directory
1250
                        // search all directory entries of a sector
1251
                        for(dir_entry = 0; dir_entry < DIRENTRIES_PER_SECTOR; dir_entry++)
1252
                        {       // check if current direntry is available
1253
                                if(((u8)dir[dir_entry].Name[0] == SLOT_EMPTY) || ((u8)dir[dir_entry].Name[0] == SLOT_DELETED))
1254
                                {       // a free direntry was found
1255
                                        for(i = 0; i < 11; i++) dir[dir_entry].Name[i] = dirname[i];            // Set dir name
1256
                                        dir[dir_entry].Attribute    = attrib;
1257
                                        dir[dir_entry].Res1 = 0;
1258
                                        dir[dir_entry].CreationTime10ms = (u8)(SystemTime.mSec/10);
1259
                                        dir[dir_entry].CreationTime     = FileTime(&SystemTime);
1260
                                        dir[dir_entry].CreationDate     = FileDate(&SystemTime);
1261
                                        dir[dir_entry].LastAccessDate = dir[dir_entry].CreationDate;
1262
                                        dir[dir_entry].Res2[0] = 0;
1263
                                        dir[dir_entry].Res2[1] = 0;
1264
                                        dir[dir_entry].ModTime = dir[dir_entry].CreationTime;
1265
                                        dir[dir_entry].ModDate = dir[dir_entry].CreationDate;
1266
                                        // Set the attribute of the new directoryentry.
1267
                                        dir[dir_entry].StartCluster = subdircluster;                                            // copy the location of the first datacluster to the directoryentry.
1268
                                        dir[dir_entry].Size             = 0;                                                                    // the new createted file has no content yet.
1269
                                        if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache))       // write back to card
1270
                                        {
1271
                                                Fat16_Deinit();
1272
                                                return(retvalue);
1273
                                        }
218 killagreg 1274
                                        file->FirstSectorOfFirstCluster = Fat16ClusterToSector(subdircluster);  // Calculate absolute sectorposition of first datacluster.
211 killagreg 1275
                                        file->FirstSectorOfCurrCluster  = file->FirstSectorOfFirstCluster;      // Start reading the file with the first sector of the first datacluster.
1276
                                        file->SectorOfCurrCluster               = 0;                                                            // reset sector of cureen cluster
1277
                                        file->ByteOfCurrSector                  = 0;                                                            // reset the byte location within the current sector
1278
                                        file->Attribute                                 = attrib;                                               // set file attribute to dir attribute
1279
                                        file->Size                                              = 0;                                                        // new file has no size
1280
                                        file->DirectorySector                   = curr_sector;
1281
                                        file->DirectoryIndex                    = dir_entry;
1282
                                        // prepare subdirectory data cluster
1283
                                        if((attrib & ATTR_SUBDIRECTORY) == ATTR_SUBDIRECTORY)                           // if a new directory was created then initilize the data area
1284
                                        {
1285
                                                ClearCurrCluster(file); // fill cluster with zeros
1286
                                                file->SectorInCache = file->FirstSectorOfFirstCluster;
1287
                                                if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector.
41 ingob 1288
                                                {
211 killagreg 1289
                                                        Fat16_Deinit();
41 ingob 1290
                                                        return(retvalue);
1291
                                                }
211 killagreg 1292
                                                dir = (DirEntry_t *)file->Cache;
1293
                                                // create direntry "." to current dir
1294
                                                dir[0].Name[0] = 0x2E;
1295
                                                for(i = 1; i < 11; i++) dir[0].Name[i] = ' ';
1296
                                                dir[0].Attribute = ATTR_SUBDIRECTORY;
1297
                                                dir[0].StartCluster = subdircluster;
1298
                                                dir[0].Size = 0;
1299
                                                // create direntry ".." to the upper dir
1300
                                                dir[1].Name[0] = 0x2E;
1301
                                                dir[1].Name[1] = 0x2E;
1302
                                                for(i = 2; i < 11; i++) dir[1].Name[i] = ' ';
1303
                                                dir[1].Attribute = ATTR_SUBDIRECTORY;
1304
                                                dir[1].StartCluster = dircluster;
1305
                                                dir[1].Size = 0;
1306
                                                if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache))// read in the sector.
88 killagreg 1307
                                                {
211 killagreg 1308
                                                        Fat16_Deinit();
1309
                                                        return(retvalue);
1310
                                                }
1311
                                        }
1312
                                        retvalue = 1;
1313
                                        dir_entry = DIRENTRIES_PER_SECTOR;      // stop for-loop
88 killagreg 1314
                                }
211 killagreg 1315
                        }
1316
                        dir_sector++; // search next sector
1317
                // stop if we reached the end of the cluster or the end of the root dir
1318
                }while((dir_sector < max_dir_sector) && (!retvalue));
88 killagreg 1319
 
211 killagreg 1320
                // if we are seaching in the data area and the file not found in this cluster so take next cluster.
1321
                if(!retvalue && ( Partition.FirstDataSector <= file->FirstSectorOfCurrCluster))
1322
                {
1323
                        end_of_directory_not_reached = GetNextCluster(file);  // updates File->FirstSectorOfCurrCluster
1324
                }
1325
        }while((end_of_directory_not_reached) && (!retvalue));
1326
        // Perhaps we are at the end of the last cluster of a directory file and have no free direntry found.
1327
        // Then we would need to add a cluster to that file and create the new direntry there.
1328
        // This code is not implemented yet, because its occurs only if more that 32*32=1024 direntries are
1329
        // within a subdirectory of root.
1330
 
41 ingob 1331
        return(retvalue);       // return 1 if file has been created otherwise return 0.
1 ingob 1332
}
1333
 
41 ingob 1334
/********************************************************************************************************************************************/
1335
/*      Function:               FileExist(const s8* filename, u8 attribfilter, u8 attribmask, File_t *file);                                                                                    */
1336
/*                                                                                                                                                                                                                                                                                      */
1337
/*      Description:    This function looks for the specified file including its subdirectories beginning                                                                               */
1338
/*                                      in the rootdirectory of the drive. If the file is found the Filepointer properties are                                                                  */
1339
/*                                      updated.                                                                                                                                                                                                                                */
1340
/*                                                                                                                                                                                                                                                                                      */
1341
/*      Returnvalue:    1 if file is found else 0.                                                                                                                                                                                              */
1342
/********************************************************************************************************************************************/
1343
u8 FileExist(const s8* filename, const u8 attribfilter, const u8 attribmask, File_t *file)
1 ingob 1344
{
41 ingob 1345
        s8* path = 0;
1346
        s8* subpath = 0;
1347
        u8 af, am, file_exist = 0;
1348
        s8 dirname[12]; // 8+3 + temination character
1 ingob 1349
 
41 ingob 1350
        // if incomming pointers are useless return immediatly
1351
        if ((filename == NULL) || (file == NULL) || (!Partition.IsValid)) return 0;
1 ingob 1352
 
41 ingob 1353
        // trace along the filepath
1354
        path = (s8*)filename;                                                           // start a the beginning of the filename string
1355
        file->DirectorySector = 0;                                                              // start at RootDirectory with file search
88 killagreg 1356
        file->DirectoryIndex = 0;
41 ingob 1357
        // as long as the file was not found and the remaining path is not empty
1358
        while((*path != 0) && !file_exist)
1359
        {       // separate dirname and subpath from filepath string
1360
                subpath = SeperateDirName(path, dirname);
1361
                if(subpath != NULL)
88 killagreg 1362
                {
1363
                        if(*subpath == 0)
41 ingob 1364
                        {       // empty subpath indicates last element of dir chain
1365
                                af = attribfilter;
1366
                                am = attribmask;
1367
                        }
1368
                        else  // it must be a subdirectory and no volume label
1 ingob 1369
                        {
41 ingob 1370
                                af = ATTR_SUBDIRECTORY;
1371
                                am = ATTR_SUBDIRECTORY|ATTR_VOLUMELABEL;
1372
                        }
1373
                        if(!DirectoryEntryExist(dirname, af, am, file))
1374
                        {
1375
                                return (file_exist); // subdirectory does not exist
1376
                        }
88 killagreg 1377
                        else
1378
                        {
41 ingob 1379
                                if (*subpath == 0)
24 StephanB 1380
                                {
41 ingob 1381
                                        file_exist = 1; // last element of path chain was found with the given attribute filter
24 StephanB 1382
                                }
1 ingob 1383
                        }
1384
                }
41 ingob 1385
                else // error seperating the subpath
1 ingob 1386
                {
41 ingob 1387
                        return file_exist; // bad subdir format
1 ingob 1388
                }
41 ingob 1389
                path = subpath;
1390
                subpath = 0;
1 ingob 1391
        }
41 ingob 1392
        return (file_exist);
1 ingob 1393
}
1394
 
24 StephanB 1395
 
41 ingob 1396
/********************************************************************************************************************************************/
1397
/*      Function:               FileCreate(const s8* filename, u8 attrib, File_t *file);                                                                                                                                */
1398
/*                                                                                                                                                                                                                                                                                      */
1399
/*      Description:    This function looks for the specified file including its subdirectories beginning                                                                               */
1400
/*                                      in the rootdirectory of the partition. If the file is found the Filepointer properties are                                                              */
88 killagreg 1401
/*                                      updated. If file or its subdirectories are not found they will be created                                                                                               */
41 ingob 1402
/*                                                                                                                                                                                                                                                                                      */
1403
/*      Returnvalue:    1 if file was created else 0.                                                                                                                                                                                   */
1404
/********************************************************************************************************************************************/
1405
u8 FileCreate(const s8* filename, const u8 attrib, File_t *file)
1406
{
1407
        s8 *path = 0;
1408
        s8 *subpath = 0;
1409
        u8 af, am, file_created = 0;
1410
        s8 dirname[12];
24 StephanB 1411
 
41 ingob 1412
        // if incomming pointers are useless return immediatly
1413
        if ((filename == NULL) || (file == NULL) || (!Partition.IsValid)) return 0;
1 ingob 1414
 
41 ingob 1415
        // trace along the filepath
1416
        path = (s8*)filename;                                                                   // start a the beginning of the filename string
1417
        file->DirectorySector = 0;                                                              // start at RootDirectory with file search
1418
        file->DirectoryIndex = 0;
1419
        // as long as the file was not created and the remaining file path is not empty
1420
        while((*path != 0) && !file_created)
1421
        {   // separate dirname and subpath from filepath string
1422
                subpath = SeperateDirName(path, dirname);
1423
                if(subpath != NULL)
24 StephanB 1424
                {
88 killagreg 1425
                        if(*subpath == 0)
41 ingob 1426
                        {       // empty subpath indicates last element of dir chain
1427
                                af = ATTR_NONE;
1428
                                am = ATTR_SUBDIRECTORY|ATTR_VOLUMELABEL;  // any file that is no subdir or volume label
24 StephanB 1429
                        }
41 ingob 1430
                        else  // it must be a subdirectory and no volume label
24 StephanB 1431
                        {
41 ingob 1432
                                af = ATTR_SUBDIRECTORY;
1433
                                am = ATTR_SUBDIRECTORY|ATTR_VOLUMELABEL;
24 StephanB 1434
                        }
41 ingob 1435
                        if(!DirectoryEntryExist(dirname, af, am, file)) // if subdir or file is not existent
1436
                        {  // try to create subdir or file
1437
                                if(*subpath == 0) af = attrib; // if last element in dir chain take the given attribute
1438
                                if(!CreateDirectoryEntry(dirname, af, file))
1439
                                {       // could not be created
88 killagreg 1440
                                        return(file_created);
1441
                                }
41 ingob 1442
                                else if (*subpath == 0) file_created = 1; // last element of path chain was created
24 StephanB 1443
                        }
1444
                }
41 ingob 1445
                else // error seperating the subpath
24 StephanB 1446
                {
41 ingob 1447
                        return file_created; // bad subdir format
24 StephanB 1448
                }
41 ingob 1449
                path = subpath;
1450
                subpath = 0;
1451
        }
1452
        return (file_created);
24 StephanB 1453
}
1 ingob 1454
 
24 StephanB 1455
 
41 ingob 1456
/********************************************************************************************************************************************/
1457
/*      Function:               File_t * fopen_(s8* filename, s8 mode);                                                                                                                                                                 */
1458
/*                                                                                                                                                                                                                                                                                      */
1459
/*      Description:    This function looks for the specified file in the rootdirectory of the drive. If the file is found the number of the    */
1460
/*                                      corrosponding filepointer is returned. Only modes 'r' (reading) and 'a' append are implemented yet.                                             */
1461
/*                                                                                                                                                                                                                                                                                      */
1462
/*      Returnvalue:    The filepointer to the file or 0 if faild.                                                                                                                                                              */
1463
/********************************************************************************************************************************************/
89 killagreg 1464
File_t * fopen_(s8 * const filename, const s8 mode)
88 killagreg 1465
{
41 ingob 1466
        File_t *file    = 0;
88 killagreg 1467
 
41 ingob 1468
        if((!Partition.IsValid) || (filename == 0)) return(file);
1469
 
1470
        // Look for an unused filepointer in the file pointer list?
1471
        file = LockFilePointer();
1472
        // if no unused file pointer was found return 0
1473
        if(file == NULL) return(file);
1474
 
1475
        // now we have found a free filepointer and claimed it
1476
        // so let initiate its property values
211 killagreg 1477
        file->FirstSectorOfFirstCluster = SECTOR_UNDEFINED;             // Sectorpointer to the first sector of the first datacluster of the file.
1478
        file->FirstSectorOfCurrCluster  = SECTOR_UNDEFINED;             // Pointer to the cluster which is edited at the moment.
41 ingob 1479
        file->SectorOfCurrCluster               = 0;            // The sector which is edited at the moment (cluster_pointer + sector_index).
1480
        file->ByteOfCurrSector                  = 0;            // The bytelocation within the current sector (cluster_pointer + sector_index + byte_index).
1481
        file->Mode                                              = mode;         // mode of fileoperation (read,write)
1482
        file->Size                                              = 0;            // the size of the opened file in bytes.
1483
        file->Position                                  = 0;            // pointer to a byte within the file 0 < fileposition < filesize
211 killagreg 1484
        file->SectorInCache                             = SECTOR_UNDEFINED;             // the last sector read, wich is still in the sectorbuffer.
1485
        file->DirectorySector                   = SECTOR_UNDEFINED;             // the sectorposition where the directoryentry has been made.
41 ingob 1486
        file->DirectoryIndex                    = 0;            // the index to the directoryentry within the specified sector.
1487
        file->Attribute                                 = 0;            // the attribute of the file opened.
1488
 
1489
        // check if a real file (no directory) to the given filename exist
88 killagreg 1490
        if(FileExist(filename, ATTR_NONE, ATTR_SUBDIRECTORY|ATTR_VOLUMELABEL, file))
41 ingob 1491
        {  // file exist
1492
                switch(mode)  // check mode
1 ingob 1493
                {
41 ingob 1494
                        case 'a':       // if mode is: append to file
1495
                                if((file->Attribute & ATTR_READONLY) == ATTR_READONLY)
1496
                                {       // file is marked as readonly --> do not open this file
1497
                                        fclose_(file);
1498
                                        file = NULL;
1499
                                }
1500
                                else
1501
                                {       // file is not marked as read only --> goto end of file
1502
                                        fseek_(file, 0, SEEK_END);              // point to the end of the file
1503
                                }
1504
                                break;
1505
                        case 'w':       // if mode is: write to file
1506
                                if((file->Attribute & ATTR_READONLY) == ATTR_READONLY)
1507
                                {       // file is marked as readonly --> do not open this file
1508
                                        fclose_(file);
1509
                                        file = NULL;
1510
                                }
1511
                                else
211 killagreg 1512
                                {       // file is not marked as read only
1513
                                        DirEntry_t * dir;
41 ingob 1514
                                        // free all clusters of that file
1515
                                        DeleteClusterChain(SectorToFat16Cluster(file->FirstSectorOfFirstCluster));
211 killagreg 1516
                                        // update directory entry of that file
1517
                                        file->SectorInCache = file->DirectorySector;                            // update the sector number of file cache.
1518
                                        if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector.
1519
                                        {
1520
                                                Fat16_Deinit();
1521
                                                return(NULL);
1522
                                        }
1523
                                        dir = (DirEntry_t *)file->Cache;                                                                // set pointer to directory
1524
                                    dir[file->DirectoryIndex].ModTime   = FileTime(&SystemTime);        // set modification time
1525
                                        dir[file->DirectoryIndex].ModDate       = FileDate(&SystemTime);        // set modification date
1526
                                        dir[file->DirectoryIndex].LastAccessDate = dir[file->DirectoryIndex].ModDate;
1527
                                        dir[file->DirectoryIndex].StartCluster = CLUSTER_UNDEFINED;             // update startcluster 
1528
                                        dir[file->DirectoryIndex].Size          = 0;
1529
                                        // write sector containing the direntry
1530
                                        if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache))
1531
                                        {
1532
                                                Fat16_Deinit();
1533
                                                return(NULL);
1534
                                        }
1535
                                        file->FirstSectorOfFirstCluster = SECTOR_UNDEFINED;
41 ingob 1536
                                        file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster;
1537
                                        file->SectorOfCurrCluster = 0;
1538
                                        file->ByteOfCurrSector = 0;
1539
                                        file->Size = 0;
1540
                                        file->Position = 0;
1541
                                        fseek_(file, 0, SEEK_SET);
1542
                                }
1543
                                break;
1544
                        case 'r':       // if mode is: read from file
88 killagreg 1545
                                // goto end of file
41 ingob 1546
                                fseek_(file, 0, SEEK_SET);
1547
                                break;
1548
                        default: // other modes are not supported
1549
                                fclose_(file);
1550
                                file = NULL;
24 StephanB 1551
                        break;
1 ingob 1552
                }
88 killagreg 1553
                return(file);
1 ingob 1554
        }
41 ingob 1555
        else // file does not exist
1556
        {
1557
                switch(mode)  // check mode
1558
                {
1559
                        case 'a':
1560
                        case 'w': // if mode is write or append
1561
                                // try to create the file
1562
                                if(!FileCreate(filename, ATTR_ARCHIVE, file))
1563
                                { // if it could not be created
1564
                                        fclose_(file);
1565
                                        file = NULL;
1566
                                }
1567
                                break;
1568
                        case 'r': // else opened for 'r'
218 killagreg 1569
                        default:  // if unsupported mode
41 ingob 1570
                                fclose_(file);
1571
                                file = NULL;
88 killagreg 1572
                                break;
41 ingob 1573
                }
88 killagreg 1574
                return(file);
41 ingob 1575
        }
1576
        // we should never come to this point
1577
        fclose_(file);
1578
        file = NULL;
24 StephanB 1579
        return(file);
1580
}
1581
 
41 ingob 1582
/****************************************************************************************************************************************************/
1583
/* Function:    fflush_(File *);                                                                                                                                                                                                                                        */
1584
/*                                                                                                                                                                                                                                                                                                      */
1585
/* Description: This function writes the data already in the buffer but not yet written to the file.                                                                                            */
1586
/*                                                                                                                                                                                                                                                                                                      */
1587
/* Returnvalue: 0 on success EOF on error                                                                                                                                                                                                                       */
1588
/****************************************************************************************************************************************************/
1589
s16     fflush_(File_t *file)
24 StephanB 1590
{
88 killagreg 1591
        DirEntry_t *dir;
1592
 
41 ingob 1593
        if((file == NULL) || (!Partition.IsValid)) return (EOF);
88 killagreg 1594
 
41 ingob 1595
        switch(file->Mode)
24 StephanB 1596
        {
41 ingob 1597
                case 'a':
1598
                case 'w':
88 killagreg 1599
                        if(file->ByteOfCurrSector > 0)                                                                          // has data been added to the file?
41 ingob 1600
                        {
88 killagreg 1601
                                if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache))// save the data still in the buffer
41 ingob 1602
                                {
88 killagreg 1603
                                        Fat16_Deinit();
1604
                                        return(EOF);
1605
                                }
41 ingob 1606
                        }
1607
                        file->SectorInCache     = file->DirectorySector;
1608
                        if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))                                       // read the directory entry for this file.
1609
                        {
1610
                                Fat16_Deinit();
88 killagreg 1611
                                return(EOF);
41 ingob 1612
                        }
88 killagreg 1613
 
41 ingob 1614
                        dir = (DirEntry_t *)file->Cache;
211 killagreg 1615
                        // update dile size and modification time & date
1616
                        dir[file->DirectoryIndex].ModTime = FileTime(&SystemTime);
1617
                        dir[file->DirectoryIndex].ModDate = FileDate(&SystemTime);
1618
                        dir[file->DirectoryIndex].LastAccessDate = dir[file->DirectoryIndex].ModDate;
41 ingob 1619
                        dir[file->DirectoryIndex].Size = file->Size;                                            // update file size
1620
                        if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache))       // write back to sd-card
1621
                        {
1622
                                Fat16_Deinit();
1623
                                return(EOF);
1624
                        }
1625
                        break;
1626
                case 'r':
1627
                default:
211 killagreg 1628
                        // do nothing!
41 ingob 1629
                        return(EOF);
1630
                        break;
1631
 
24 StephanB 1632
        }
41 ingob 1633
        return(0);
24 StephanB 1634
}
1 ingob 1635
 
41 ingob 1636
/****************************************************************************************************************************************/
1637
/*      Function:               fclose_(File *file);                                                                                                                                                                                            */
1638
/*                                                                                                                                                                                                                                                                              */
1639
/*      Description:    This function closes the open file by writing the remaining data                                                                                                        */
1640
/*                                      from the buffer to the device and entering the filesize in the directory entry.                                                                         */
1641
/*                                                                                                                                                                                                                                                                              */
1642
/*      Returnvalue:    0 on success EOF on error                                                                                                                                                                                       */
1643
/****************************************************************************************************************************************/
1644
s16 fclose_(File_t *file)
24 StephanB 1645
{
41 ingob 1646
        s16 returnvalue = EOF;
24 StephanB 1647
 
41 ingob 1648
        if(file == NULL) return(returnvalue);
1649
        returnvalue = fflush_(file);
1650
        UnlockFilePointer(file);
1651
        return(returnvalue);
24 StephanB 1652
}
1653
 
41 ingob 1654
/********************************************************************************************************************************************/
1655
/*      Function:               fgetc_(File *file);                                                                                                                                                                                                             */
1656
/*                                                                                                                                                                                                                                                                                      */
1657
/*      Description:    This function reads and returns one character from the specified file. Is the end of the actual sector reached the              */
1658
/*                                      next sector of the cluster is read. If the last sector of the cluster read the next cluster will be searched in FAT.    */
1659
/*                                                                                                                                                                                                                                                                                      */
1660
/*      Returnvalue:    The function returns the character read from the specified memorylocation as u8 casted to s16 or EOF.                                   */
1661
/********************************************************************************************************************************************/
1662
s16 fgetc_(File_t *file)
88 killagreg 1663
{
41 ingob 1664
        s16 c = EOF;
1665
        u32 curr_sector;
88 killagreg 1666
 
41 ingob 1667
        if( (!Partition.IsValid) || (file == NULL)) return(c);
1668
        // if the end of the file is not reached, get the next character.
88 killagreg 1669
        if((0 < file->Size) && ((file->Position+1) < file->Size) )
1 ingob 1670
        {
41 ingob 1671
                curr_sector  = file->FirstSectorOfCurrCluster;          // calculate the sector of the next character to be read.
88 killagreg 1672
                curr_sector += file->SectorOfCurrCluster;
1673
 
41 ingob 1674
                if(file->SectorInCache != curr_sector)
1 ingob 1675
                {
41 ingob 1676
                        file->SectorInCache = curr_sector;
1677
                        if(SD_SUCCESS != SDC_GetSector(file->SectorInCache,file->Cache))
1678
                        {
1679
                                Fat16_Deinit();
1680
                                return(c);
88 killagreg 1681
                        }
1682
                }
41 ingob 1683
                c = (s16) file->Cache[file->ByteOfCurrSector];
1684
                file->Position++;                                                                       // increment file position
88 killagreg 1685
                file->ByteOfCurrSector++;                                                       // goto next byte in sector
41 ingob 1686
                if(file->ByteOfCurrSector >= BYTES_PER_SECTOR)          // if end of sector
24 StephanB 1687
                {
41 ingob 1688
                        file->ByteOfCurrSector = 0;                                             //  reset byte location
1689
                        file->SectorOfCurrCluster++;                                    //      next sector
1690
                        if(file->SectorOfCurrCluster >= Partition.SectorsPerCluster)    // if end of cluster is reached, the next datacluster has to be searched in the FAT.
24 StephanB 1691
                        {
88 killagreg 1692
 
41 ingob 1693
                                if(GetNextCluster(file))                                                                                // Sets the clusterpointer of the file to the next datacluster.
24 StephanB 1694
                                {
41 ingob 1695
                                        file->SectorOfCurrCluster = 0;                                                          // start reading new cluster at first sector of the cluster.
1696
                                }
1697
                                else // the last cluster was allready reached
1698
                                {
1699
                                        file->SectorOfCurrCluster--;                                                    // jump back to the last sector in the last cluster
1700
                                        file->ByteOfCurrSector = BYTES_PER_SECTOR;                              // set ByteOfCurrSector one byte over sector end
1701
                                }
1 ingob 1702
                        }
24 StephanB 1703
                }
1704
        }
41 ingob 1705
        return(c);
24 StephanB 1706
}
1707
 
41 ingob 1708
/********************************************************************************************************************************************/
1709
/*      Function:               fputc_( const s8 c, File *file);                                                                                                                                                                                */
1710
/*                                                                                                                                                                                                                                                                                      */
1711
/*      Description:    This function writes a byte to the specified file and takes care of writing the necessary FAT- Entries.                                 */
1712
/*                                      next sector of the cluster is read. If the last sector of the cluster read the next cluster will be searched in FAT.    */
1713
/*                                                                                                                                                                                                                                                                                      */
1714
/*      Returnvalue:    The function returns the character written to the stream or EOF on error.                                                                                               */
1715
/********************************************************************************************************************************************/
1716
s16 fputc_(const s8 c, File_t *file)
88 killagreg 1717
{
41 ingob 1718
        u32 curr_sector  = 0;
88 killagreg 1719
 
41 ingob 1720
        if((!Partition.IsValid) || (file == NULL)) return(EOF);
211 killagreg 1721
        switch(file->Mode)
41 ingob 1722
        {
211 killagreg 1723
                case 'w':
1724
                case 'a':
1725
                        // If file position equals to file size, then the end of file has been reached.
1726
                        // In this case it has to be checked that the ByteOfCurrSector is BYTES_PER_SECTOR
1727
                        // and a new cluster should be appended.
1728
                        // If the first sector of first cluster is unvalid, then the file claims no data clusters 
1729
                        // and size should be zero, therefore append a new Cluster too.
1730
                        if(((file->Position >= file->Size) && (file->ByteOfCurrSector >= BYTES_PER_SECTOR)) || (file->FirstSectorOfFirstCluster == SECTOR_UNDEFINED))
1731
                        {
1732
                                if(CLUSTER_UNDEFINED == AppendCluster(file)) return(EOF);
1733
                        }
1734
 
1735
                        curr_sector  = file->FirstSectorOfCurrCluster;
1736
                        curr_sector += file->SectorOfCurrCluster;
1737
                        if(file->SectorInCache != curr_sector)
1738
                        {
1739
                                file->SectorInCache = curr_sector;
1740
                                if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))
24 StephanB 1741
                                {
211 killagreg 1742
                                        Fat16_Deinit();
1743
                                        return(EOF);
24 StephanB 1744
                                }
1 ingob 1745
                        }
211 killagreg 1746
 
1747
                        file->Cache[file->ByteOfCurrSector] = (u8)c;            // write databyte into the buffer. The byte will be written to the device at once
1748
                        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.
1749
                        file->Position++;                                                                       // the actual positon within the file.
1750
                        file->ByteOfCurrSector++;                                                       // goto next byte in sector
1751
                        if(file->ByteOfCurrSector >= BYTES_PER_SECTOR)          // if the end of this sector is reached yet
1752
                        {       // save the sector to the sd-card
1753
                                if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache))
1754
                                {
1755
                                        Fat16_Deinit();
1756
                                        return(EOF);
1757
                                }
1758
                                file->ByteOfCurrSector = 0;                                             //  reset byte location
1759
                                file->SectorOfCurrCluster++;                                    //      next sector
1760
                                if(file->SectorOfCurrCluster >= Partition.SectorsPerCluster)// if end of cluster is reached, the next datacluster has to be searched in the FAT.
1761
                                {
1762
                                        if(!GetNextCluster(file))                                                               // Sets the clusterpointer of the file to the next datacluster.
1763
                                        { // if current cluster was the last cluster of the file
1764
                                                if(!AppendCluster(file))                                                // append a new and free cluster at the end of the file.
1765
                                                {
1766
                                                        file->SectorOfCurrCluster--;                            // jump back to last sector of last cluster
1767
                                                        file->ByteOfCurrSector = BYTES_PER_SECTOR;      // set byte location to 1 byte over sector len
1768
                                                        return(EOF);
1769
                                                }
1770
                                        }
1771
                                        else // next cluster
1772
                                        {
1773
                                                file->SectorOfCurrCluster = 0;                                                  // start reading new cluster at first sector of the cluster.
1774
                                        }
1775
                                }
88 killagreg 1776
                        }
211 killagreg 1777
                        break;
1778
                case 'r':
1779
                default:
1780
                        return(EOF);
1781
                        break;
1782
        } // EOF switch(file->Mode)
41 ingob 1783
        return(0);
24 StephanB 1784
}
1785
 
1786
 
41 ingob 1787
/****************************************************************************************************************************************/
1788
/*      Function:               fread_(void *buffer, s32 size, s32 count, File *File);                                                                                                                          */
1789
/*                                                                                                                                                                                                                                                                              */
1790
/*      Description:    This function reads count objects of the specified size                                                                                                                         */
1791
/*                                      from the actual position of the file to the specified buffer.                                                                                                           */
1792
/*                                                                                                                                                                                                                                                                              */
1793
/*      Returnvalue:    The function returns the number of objects (not bytes) read from the file.                                                                                      */
1794
/****************************************************************************************************************************************/
1795
u32 fread_(void *buffer, u32 size, u32 count, File_t *file)
1796
{
1797
        u32 object_cnt  = 0;                                                                                    // count the number of objects read from the file.
1798
        u32 object_size = 0;                                                                                    // count the number of bytes read from the actual object.
1799
        u8 *pbuff       = 0;                                                                                    // a pointer to the actual bufferposition.
1800
        u8 success      = 1;                                                                                    // no error occured during read operation to the file.
88 killagreg 1801
        s16 c;
24 StephanB 1802
 
41 ingob 1803
        if((!Partition.IsValid) || (file == NULL) || (buffer == NULL)) return(0);
24 StephanB 1804
 
41 ingob 1805
        pbuff = (u8 *) buffer;                                                                                  // cast the void pointer to an u8 *
88 killagreg 1806
 
41 ingob 1807
        while((object_cnt < count) && success)
24 StephanB 1808
        {
41 ingob 1809
                object_size = size;
1810
                while((size > 0) && success)
1811
                {
1812
                        c = fgetc_(file);
1813
                        if(c != EOF)
1814
                        {
1815
                                *pbuff = (u8)c;                                                                         // read a byte from the buffer to the opened file.
1816
                                pbuff++;
1817
                                size--;
1818
                        }
1819
                        else // error or end of file reached
1820
                        {
88 killagreg 1821
                                success = 0;
41 ingob 1822
                        }
1823
                }
1824
                if(success) object_cnt++;
88 killagreg 1825
        }
41 ingob 1826
        return(object_cnt);                                                                                             // return the number of objects succesfully read from the file
1 ingob 1827
}
1828
 
24 StephanB 1829
 
41 ingob 1830
/****************************************************************************************************************************************/
1831
/*      Function:               fwrite_(void *buffer, s32 size, s32 count, File *file);                                                                                                                         */
1832
/*                                                                                                                                                                                                                                                                              */
1833
/*      Description:    This function writes count objects of the specified size                                                                                                                        */
1834
/*                                      from the buffer pointer to the actual position in the file.                                                                                                                     */
1835
/*                                                                                                                                                                                                                                                                              */
1836
/*      Returnvalue:    The function returns the number of objects (not bytes) read from the file.                                                                                      */
1837
/****************************************************************************************************************************************/
1838
u32 fwrite_(void *buffer, u32 size, u32 count, File_t *file)
1 ingob 1839
{
41 ingob 1840
        u32 object_cnt  = 0;                                                                                                            // count the number of objects written to the file.
1841
        u32 object_size = 0;                                                                                                            // count the number of bytes written from the actual object.
1842
        u8 *pbuff           = 0;                                                                                                                // a pointer to the actual bufferposition.
1843
        u8 success      = 1;                                                                                                            // no error occured during write operation to the file.
88 killagreg 1844
        s16 c;
24 StephanB 1845
 
41 ingob 1846
        if((!Partition.IsValid) || (file == NULL) || (buffer == NULL)) return(0);
211 killagreg 1847
        if(file->Mode == 'r') return (0); // opened read only
41 ingob 1848
        pbuff = (u8 *) buffer;                                                                                                          // cast the void pointer to an u8 *
88 killagreg 1849
 
41 ingob 1850
        while((object_cnt < count) && success)
1 ingob 1851
        {
41 ingob 1852
                object_size = size;
1853
                while((size > 0) && success)
1 ingob 1854
                {
41 ingob 1855
                        c = fputc_(*pbuff, file);                                                                               // write a byte from the buffer to the opened file.
1856
                        if(c != EOF)
24 StephanB 1857
                        {
41 ingob 1858
                                pbuff++;
1859
                                size--;
24 StephanB 1860
                        }
41 ingob 1861
                        else
24 StephanB 1862
                        {
41 ingob 1863
                                success = 0;
24 StephanB 1864
                        }
1 ingob 1865
                }
41 ingob 1866
                if(success) object_cnt++;
88 killagreg 1867
        }
1868
 
41 ingob 1869
        return(object_cnt);                                                                                                                             // return the number of objects succesfully written to the file
88 killagreg 1870
}
1 ingob 1871
 
24 StephanB 1872
 
41 ingob 1873
/****************************************************************************************************************************************/
1874
/*      Function:               fputs_(const s8 *string, File_t *File);                                                                                                                                                         */
1875
/*                                                                                                                                                                                                                                                                              */
1876
/*      Description:    This function writes a string to the specified file.                                                                                                                            */
1877
/*                                                                                                                                                                                                                                                                              */
1878
/*      Returnvalue:    The function returns a no negative value or EOF on error.                                                                                                                       */
1879
/****************************************************************************************************************************************/
89 killagreg 1880
s16 fputs_(s8 * const string, File_t * const file)
24 StephanB 1881
{
41 ingob 1882
        u8 i=0;
1883
        s16 c = 0;
88 killagreg 1884
 
211 killagreg 1885
        if((!Partition.IsValid) || (file == NULL) || (string == NULL)) return(EOF);
1886
        if(file->Mode == 'r') return(EOF);
41 ingob 1887
        while((string[i] != 0)&& (c != EOF))
24 StephanB 1888
        {
41 ingob 1889
                c = fputc_(string[i], file);
1890
                i++;
24 StephanB 1891
        }
41 ingob 1892
        return(c);
24 StephanB 1893
}
1894
 
41 ingob 1895
/****************************************************************************************************************************************/
1896
/*      Function:               fgets_(s8 *, s16 , File_t *);                                                                                                                                                                           */
1897
/*                                                                                                                                                                                                                                                                              */
1898
/*      Description:    This function reads a string from the file to the specifies string.                                                                                             */
1899
/*                                                                                                                                                                                                                                                                              */
1900
/*      Returnvalue:    A pointer to the string read from the file or 0 on error.                                                                                                                       */
1901
/****************************************************************************************************************************************/
89 killagreg 1902
s8 * fgets_(s8 * const string, s16 const length, File_t * const file)
24 StephanB 1903
{
89 killagreg 1904
        s8 *pbuff;
1905
        s16 c = 0, bytecount;
88 killagreg 1906
 
89 killagreg 1907
        if((!Partition.IsValid) || (file == NULL) || (string == NULL) || (length < 1)) return (0);
1908
        bytecount = length;
1909
        pbuff = string;                                                         // set write pointer to start of string
1910
        while(bytecount > 1)                                            // read the length-1 characters from the file to the string.
24 StephanB 1911
        {
41 ingob 1912
                c = fgetc_(file);                                               // read a character from the opened file.
1913
                switch(c)
1 ingob 1914
                {
89 killagreg 1915
                        case 0x0A:                                                      // new line
1916
                                *pbuff = 0;                                             // set string terminator
1917
                                return(string);                                 // return normal
88 killagreg 1918
 
41 ingob 1919
                        case EOF:
89 killagreg 1920
                                *pbuff = 0;                                             // set string terminator
1921
                                return(0);
1922
 
1923
                        default:
1924
                                *pbuff++ = (s8)c;                               // copy byte to string
1925
                                bytecount--;
88 killagreg 1926
                                break;
1 ingob 1927
                }
24 StephanB 1928
        }
89 killagreg 1929
        *pbuff = 0;     // set string terminator
41 ingob 1930
        return(string);
24 StephanB 1931
}
1932
 
41 ingob 1933
/****************************************************************************************************************************************/
1934
/*      Function:               fexist_(const u8*);                                                                                                                                                                                                     */
1935
/*                                                                                                                                                                                                                                                                              */
1936
/*      Description:    This function checks if a file already exist.                                                                                                                                           */
1937
/*                                                                                                                                                                                                                                                                              */
1938
/*      Returnvalue:    1 if the file exist else 0.                                                                                                                                                                                     */
1939
/****************************************************************************************************************************************/
89 killagreg 1940
u8 fexist_(s8 * const filename)
24 StephanB 1941
{
41 ingob 1942
        u8 exist = 0;
1943
        File_t *file = 0;
1944
        file = LockFilePointer();
1945
        exist = FileExist(filename, ATTR_NONE, ATTR_SUBDIRECTORY|ATTR_VOLUMELABEL, file);
1946
        UnlockFilePointer(file);
1947
        return(exist);
1 ingob 1948
}
24 StephanB 1949
 
41 ingob 1950
/****************************************************************************************************************************************/
1951
/*      Function:               feof_(File_t *File);                                                                                                                                                                                            */
1952
/*                                                                                                                                                                                                                                                                              */
1953
/*      Description:    This function checks wether the end of the file has been reached.                                                                                                                                               */
1954
/*                                                                                                                                                                                                                                                                              */
1955
/*      Returnvalue:    0 if the end of the file was not reached otherwise 1.                                                                                                                                                                           */
1956
/****************************************************************************************************************************************/
1957
u8 feof_(File_t *file)
88 killagreg 1958
{
41 ingob 1959
        if(((file->Position)+1) < (file->Size))
24 StephanB 1960
        {
1961
                return(0);
1962
        }
88 killagreg 1963
        else
24 StephanB 1964
        {
88 killagreg 1965
                return(1);
24 StephanB 1966
        }
1967
}
41 ingob 1968
 
210 killagreg 1969
/****************************************************************************************************************************************************/
1970
/* Function:    s8* FAT16_GetVolumeLabel(void)                                                                                                                                                                                                                  */
1971
/*                                                                                                                                                                                                                                                                                                      */
1972
/* Description: This function returns the volume label                                                                                                                                                                                          */
1973
/*                                                                                                                                                                                                                                                                                                      */
1974
/* Returnvalue: This function returns the pointer to the volume label or NULL if not found.                                                                                                                     */
1975
/****************************************************************************************************************************************************/
1976
s8* FAT16_GetVolumeLabel(void)
1977
{
1978
        s8              *pVolumeLabel = NULL;
1979
        u32             dir_sector, max_dir_sector, curr_sector;
1980
        u16     dir_entry = 0;
1981
        u8              i = 0;
41 ingob 1982
 
210 killagreg 1983
        DirEntry_t * dir;
1984
        File_t *file = NULL;
1985
 
1986
        // if Partition is not valud return NULL
1987
        if(!Partition.IsValid) return(pVolumeLabel);
1988
        // if Volume label was read before return it
1989
        if(Partition.VolumeLabel[0]!= '\0') return (Partition.VolumeLabel);
1990
        // try to catch a file pointer
1991
        file = LockFilePointer();
1992
        if(file == NULL) return(pVolumeLabel);
1993
        // search dir entries direct within the root directory area
1994
        file->DirectorySector = 0;
1995
        max_dir_sector = (Partition.MaxRootEntries * DIRENTRY_SIZE)/BYTES_PER_SECTOR;
1996
        file->FirstSectorOfFirstCluster = Partition.FirstRootDirSector;
1997
 
1998
        // update current file data area position to start of first cluster
1999
        file->FirstSectorOfCurrCluster  = file->FirstSectorOfFirstCluster;
2000
        file->SectorOfCurrCluster               = 0;
2001
        file->ByteOfCurrSector                  = 0;
2002
 
2003
        dir_sector = 0; // reset sector counter within a new cluster
2004
        do // loop over all sectors of the root directory
2005
        {
2006
                curr_sector = file->FirstSectorOfCurrCluster + dir_sector;      // calculate sector number
2007
                file->SectorInCache = curr_sector;                                                      // upate the sector number of file cache.
2008
                if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read the sector
2009
                {
2010
                        Fat16_Deinit();
2011
                        return(pVolumeLabel);
2012
                }
2013
                dir = (DirEntry_t *)file->Cache;                                                        // set pointer to directory
2014
                // search all directory entries within that sector
2015
                for(dir_entry = 0; dir_entry < DIRENTRIES_PER_SECTOR; dir_entry++)
2016
                {   // check for existing dir entry
2017
                        switch((u8)dir[dir_entry].Name[0])
2018
                        {
2019
                                case SLOT_EMPTY:
2020
                                case SLOT_DELETED:
2021
                                        // ignore empty or deleted dir entries
2022
                                        break;
2023
                                default:
2024
                                        // check attributes for volume label
2025
                                        if ((dir[dir_entry].Attribute & ATTR_VOLUMELABEL) != ATTR_VOLUMELABEL) break; // attribute must match
2026
                                        // (first 11 characters include 8 chars of basename and 3 chars extension.)
2027
                                        for(i = 0; i<11;i++) Partition.VolumeLabel[i] = dir[dir_entry].Name[i];
2028
                                        Partition.VolumeLabel[11] = '\0'; // terminate string
2029
                                        file->Attribute = dir[dir_entry].Attribute; // store attribute of found dir entry
2030
                                        file->FirstSectorOfFirstCluster = Fat16ClusterToSector(dir[dir_entry].StartCluster); // set sector of first data cluster
2031
                                        file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster;
219 killagreg 2032
                                        file->SectorOfCurrCluster = 0;
2033
                                        file->ByteOfCurrSector = 0;
210 killagreg 2034
                                        file->DirectorySector = curr_sector; // current sector
2035
                                        file->DirectoryIndex  = dir_entry; // current direntry in current sector
2036
                                        file->Size = dir[dir_entry].Size;
2037
                                        dir_entry = DIRENTRIES_PER_SECTOR;      // stop for-loop
2038
                                        pVolumeLabel =  Partition.VolumeLabel;
2039
                        } // end of first byte of name check
2040
                }
2041
                dir_sector++; // search next sector
2042
        // stop if we reached the end of the cluster or the end of the root dir
2043
        }while((dir_sector < max_dir_sector) && (!pVolumeLabel));
2044
 
2045
        UnlockFilePointer(file);
2046
        return(pVolumeLabel);
2047
}