Rev 24 | Rev 89 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 24 | Rev 41 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | #ifndef __fat16_h |
1 | #ifndef _FAT16_H |
2 | #define __fat16_h |
2 | #define _FAT16_H |
3 | - | ||
4 | - | ||
5 | - | ||
6 | //________________________________________________________________________________________________________________________________________ |
- | |
7 | // |
- | |
8 | // Userspecific definitions |
- | |
9 | // |
- | |
10 | //________________________________________________________________________________________________________________________________________ |
- | |
11 | - | ||
12 | #define __MAX_FILES_USED 1 // The number of files that can be opened simultaneously. |
- | |
13 | - | ||
14 | - | ||
15 | //________________________________________________________________________________________________________________________________________ |
- | |
16 | // |
- | |
17 | // Structure of a filepointer |
- | |
18 | // |
- | |
19 | //________________________________________________________________________________________________________________________________________ |
- | |
20 | - | ||
21 | #define _UNUSED 1 // Bits used in the attribute of an directory entry. |
- | |
22 | #define _USED 2 |
- | |
23 | #define _ARCHIVE 2 |
- | |
24 | #define _READ_ONLY 4 |
- | |
25 | #define _SYSTEM 8 |
- | |
26 | #define _DIRECTORY 16 |
- | |
27 | #define _FILE 32 |
- | |
28 | - | ||
29 | - | ||
30 | typedef struct afile |
- | |
31 | { |
- | |
32 | u32 start_cluster; // Sectorpointer to the first sector of the first datacluster of the file. |
- | |
33 | u32 cluster_pointer; // Pointer to the cluster which is edited at the moment. |
- | |
34 | u8 sector_index; // The sector which is edited at the moment (cluster_pointer + sector_index). |
- | |
35 | u16 byte_index; // The bytelocation within the current sector (cluster_pointer + sector_index + byte_index). |
- | |
36 | u8 mode; // mode of fileoperation (read,write) |
- | |
37 | u32 filesize; // the size of the opend file in bytes. |
- | |
38 | u32 fileposition; // pointer to a character within the file 0 < fileposition < filesize |
- | |
39 | u32 sector_in_buffer; // the last sector read, wich is still in the sectorbuffer. |
- | |
40 | u32 directory_root_sector; // pointer to the root of the |
- | |
41 | u32 directory_sector; // the sectorposition where the directoryentry has been made. |
- | |
42 | u8 directory_index; // the index to the directoryentry within the specified sector. |
- | |
43 | u8 attribute; // the attribute of the file opened. |
- | |
44 | u8 buffer[512]; // Buffer for read and write operation from or to the mmc. |
- | |
45 | u8 state; // state of the filepointer (used/unused/...) |
- | |
46 | } File; |
- | |
47 | - | ||
48 | - | ||
49 | typedef enum |
- | |
50 | { |
- | |
51 | NOT_INITIALIZED, |
- | |
52 | INITIALIZED |
- | |
53 | }Fat16_VolumeState_t; |
- | |
54 | - | ||
55 | //________________________________________________________________________________________________________________________________________ |
- | |
56 | // |
- | |
57 | // Structure of an item to find within the cwd |
- | |
58 | // |
- | |
59 | //________________________________________________________________________________________________________________________________________ |
- | |
60 | - | ||
61 | struct volume |
- | |
62 | { |
- | |
63 | Fat16_VolumeState_t state; // state of the volume |
- | |
64 | } str_Volume; |
- | |
65 | - | ||
66 | //________________________________________________________________________________________________________________________________________ |
- | |
67 | // |
- | |
68 | // Structure of an item to find within the cwd |
- | |
69 | // |
- | |
70 | //________________________________________________________________________________________________________________________________________ |
- | |
71 | - | ||
72 | typedef struct find |
- | |
73 | { |
- | |
74 | s8 name[12]; // filename + extension or directoryname of the item found within the cwd. |
- | |
75 | s8 searchstring[13]; |
- | |
76 | u32 cluster_pointer; // Sectorpointer to the sector of the item searched within the cwd. |
- | |
77 | u16 startcluster; // pointer to the first datacluster of the item found in the cwd. |
- | |
78 | u32 filesize; // the size of the opend file in bytes. |
- | |
79 | u32 directory_sector; // the sector within the actual cluster where the directoryentry was found. |
- | |
80 | u8 directory_index; // the index to the directoryentry within the specified sector. |
- | |
81 | u8 attribute; // the attribute of the file opened. |
- | |
82 | } Find; |
- | |
83 | - | ||
84 | //________________________________________________________________________________________________________________________________________ |
- | |
85 | // |
- | |
86 | // Directoryentries |
- | |
87 | // |
- | |
88 | //________________________________________________________________________________________________________________________________________ |
- | |
89 | - | ||
90 | - | ||
91 | - | ||
92 | struct DirEntry |
- | |
93 | { |
- | |
94 | u8 name[8]; // 8 bytes name. |
- | |
95 | u8 extension[3]; // 3 bytes extension. |
- | |
96 | u8 attribute; // attribute of the directory entry (unused,archive,read-only,system,directory,volume) |
- | |
97 | u8 reserved[10]; // reserved bytes within the directory entry. |
- | |
98 | u16 time; // time and |
- | |
99 | u16 date; // date of last write acces to the file or directory. |
- | |
100 | u16 startcluster; // first cluster of the file or directory. |
- | |
101 | u32 size; // size of the file or directory in bytes. |
- | |
102 | } __attribute__((packed)); |
- | |
103 | - | ||
104 | //________________________________________________________________________________________________________________________________________ |
- | |
105 | // |
- | |
106 | // Structure of an entry within the fileallocationtable. |
- | |
107 | // |
- | |
108 | //________________________________________________________________________________________________________________________________________ |
- | |
109 | - | ||
110 | struct FatEntry |
- | |
111 | { |
- | |
112 | u16 next_cluster; // the next cluster of the file. |
- | |
113 | } __attribute__((packed)); |
- | |
114 | - | ||
115 | - | ||
116 | - | ||
117 | - | ||
118 | - | ||
119 | //________________________________________________________________________________________________________________________________________ |
- | |
120 | // |
- | |
121 | // Partitions |
- | |
122 | // |
- | |
123 | //________________________________________________________________________________________________________________________________________ |
- | |
124 | - | ||
125 | #define _EMPTY 0x00 |
- | |
126 | #define _FAT12 0x01 |
- | |
127 | #define _FAT16_ST_32_MB 0x04 |
- | |
128 | #define _EXTENDED 0x05 |
- | |
129 | #define _FAT16_LT_32_MB 0x06 |
- | |
130 | #define _HPFS 0x07 |
- | |
131 | #define _FAT32 0x0B |
- | |
132 | #define _FAT32_BIOS_Extension 0x0C |
- | |
133 | #define _FAT16_32_MB_BIOS_Extension 0x0E |
- | |
134 | #define _EXTENDED_BIOS_Extension 0x0F |
- | |
135 | #define _EISA 0x12 |
- | |
136 | #define _DYNAMIC 0x42 |
- | |
137 | #define _Linux_Swap 0x82 |
- | |
138 | #define _Linux_Native 0x83 |
- | |
139 | #define _Linux_LVM 0x8E |
- | |
140 | #define _FreeBSD 0xA5 |
- | |
141 | #define _OpenBSD 0xA6 |
- | |
142 | #define _NetBSD 0xA9 |
- | |
143 | - | ||
144 | - | ||
145 | struct PartitionEntry |
- | |
146 | { |
- | |
147 | u8 PartitionState; |
- | |
148 | u8 BeginningHead; |
- | |
149 | u16 BeginningCylinder; |
- | |
150 | u8 Type; |
- | |
151 | u8 EndHead; |
- | |
152 | u16 EndCylinder; |
- | |
153 | u32 NoSectorsBeforePartition; |
- | |
154 | u32 NoSectorsPartition ; |
- | |
155 | } __attribute__((packed)); |
- | |
156 | - | ||
157 | - | ||
158 | //________________________________________________________________________________________________________________________________________ |
- | |
159 | // |
- | |
160 | // Structure of the VolumeBootRecord |
- | |
161 | // |
- | |
162 | //________________________________________________________________________________________________________________________________________ |
- | |
163 | - | ||
164 | struct VBR_Entry |
- | |
165 | { |
- | |
166 | u8 dummy[11]; |
- | |
167 | u16 bps; |
- | |
168 | u8 SectorsPerCluster; |
- | |
169 | u16 ReservedSectors; |
- | |
170 | u8 NoFATCopies; |
- | |
171 | u16 MaxRootEntries; |
- | |
172 | u16 dummy2; |
- | |
173 | u8 dummy3; |
- | |
174 | u16 SectorsPerFAT; |
- | |
175 | } __attribute__((packed)); |
- | |
176 | - | ||
Line 177... | Line 3... | ||
177 | 3 | ||
178 | 4 | ||
179 | //________________________________________________________________________________________________________________________________________ |
5 | //________________________________________________________________________________________________________________________________________ |
180 | // |
6 | // |
181 | // Structure of the MasterBootRecord |
7 | // Definitions |
Line -... | Line 8... | ||
- | 8 | // |
|
182 | // |
9 | //________________________________________________________________________________________________________________________________________ |
- | 10 | ||
- | 11 | //#define __USE_TIME_DATE_ATTRIBUTE |
|
- | 12 | #define FILE_MAX_OPEN 3 // The number of files that can accessed simultaneously. |
|
- | 13 | #define SEEK_SET 0 |
|
- | 14 | #define SEEK_CUR 1 |
|
- | 15 | #define SEEK_END 2 |
|
- | 16 | #define EOF (-1) |
|
183 | //________________________________________________________________________________________________________________________________________ |
17 | #define BYTES_PER_SECTOR 512 |
184 | 18 | /* |
|
- | 19 | ________________________________________________________________________________________________________________________________________ |
|
- | 20 | |
|
- | 21 | Structure of a filepointer |
|
185 | #define _MBR_SECTOR 0 // The MasterBootRecord is located in sector 0 |
22 | ________________________________________________________________________________________________________________________________________ |
- | 23 | */ |
|
- | 24 | typedef struct |
|
186 | 25 | { |
|
187 | struct MBR_Entry |
26 | u32 FirstSectorOfFirstCluster; // First sector of the first cluster of the file. |
188 | { |
27 | u32 FirstSectorOfCurrCluster; // First sector of the cluster which is edited at the moment. |
189 | u8 ExecutableCode[446]; |
28 | u8 SectorOfCurrCluster; // The sector within the current cluster. |
- | 29 | u16 ByteOfCurrSector; // The byte location within the current sector. |
|
190 | struct PartitionEntry PartitionEntry1; |
30 | u8 Mode; // Mode of fileoperation (read,write) |
- | 31 | u32 Size; // The size of the opend file in bytes. |
|
191 | struct PartitionEntry PartitionEntry2; |
32 | u32 Position; // Pointer to a character within the file 0 < fileposition < filesize |
- | 33 | u32 DirectorySector; // the sectorposition where the directoryentry has been made. |
|
- | 34 | u16 DirectoryIndex; // The index to the directoryentry within the specified sector. |
|
192 | struct PartitionEntry PartitionEntry3; |
35 | u8 Attribute; // The attribute of the file opened. |
193 | struct PartitionEntry PartitionEntry4; |
- | |
194 | u16 ExecutableMarker; |
- | |
195 | } __attribute__((packed)); |
36 | u8 Cache[BYTES_PER_SECTOR]; // Cache for read and write operation from or to the sd-card. |
Line 196... | Line 37... | ||
196 | 37 | u32 SectorInCache; // The last sector read, which is still in the sector cache. |
|
197 | 38 | u8 State; // State of the filepointer (used/unused/...) |
|
198 | 39 | } File_t; |
|
199 | 40 | ||
200 | //________________________________________________________________________________________________________________________________________ |
41 | //________________________________________________________________________________________________________________________________________ |
Line 201... | Line 42... | ||
201 | // |
42 | // |
202 | // API to the FAT16 filesystem |
- | |
203 | // |
- | |
204 | //________________________________________________________________________________________________________________________________________ |
- | |
205 | - | ||
206 | extern u8 InitFat16(void); |
- | |
207 | extern File * fopen_(s8 *fname,s8 mode); |
- | |
208 | extern s16 fflush_(File *file); |
- | |
209 | extern void fclose_(File *file); |
- | |
210 | extern u32 fread_(void *buffer, u32 size, u32 count, File *file); |
- | |
211 | extern u32 fwrite_(void *buffer, u32 size, u32 count, File *file); |
- | |
212 | extern s16 fseek_(File *file, s32 offset, s16 origin); |
- | |
213 | extern s16 fgetchar_(File *file); |
- | |
214 | extern u8 fputchar_(File *file,s8 c); |
- | |
215 | extern u8 fputs_(File *file,s8 *string); |
- | |
216 | extern s8 * fgets_(s8 *s, s16 count, File *file); |
- | |
217 | extern s16 frename_(s8 *oldname, s8 *newname); |
- | |
218 | extern u8 fexist_(s8 *fname); |
43 | // API to the FAT16 filesystem |
219 | extern u8 mkdir_(s8 *fname); |
- | |
220 | extern u8 chdir_(s8 *fname); |
- | |
221 | extern u8 findfirst_(s8 *fname, Find *item, u8 attribute); |
44 | // |
222 | extern u8 findnext_(Find *item); |
- | |
223 | extern u16 feof_(File *); |
- | |
224 | 45 | //________________________________________________________________________________________________________________________________________ |
|
225 | - | ||
226 | - | ||
227 | //________________________________________________________________________________________________________________________________________ |
- | |
228 | // |
46 | |
229 | // Functions needed internaly for the fat16 implementation |
47 | u8 Fat16_Init(void); |
230 | // |
48 | u8 Fat16_Deinit(void); |
231 | //________________________________________________________________________________________________________________________________________ |
- | |
232 | 49 | ||
233 | extern u8 SeekFileInDirectory(s8*fname, File *file); |
50 | File_t *fopen_(const s8 *filename, const s8 mode); |
234 | extern u8 CreateFileInDirectory(s8*fname, File *file); |
51 | s16 fclose_(File_t *file); |
235 | extern u16 FindNextFreeCluster(File *file); |
52 | u8 fexist_(const s8 *filename); |
236 | extern void SeperateFileName(s8*fname, s8*name); |
53 | s16 fflush_(File_t *file); |
237 | extern u8 ScanSubDirectories(s8*fname, File *file); |
- | |
238 | extern u16 GetNextCluster(File *file); |
- | |
239 | extern u8 AppendCluster(File *file); |
- | |
240 | extern u16 GetFatClusterOffset(File *file); |
- | |
241 | extern u16 GetFatSectorIndex(File *file); |
54 | s16 fseek_(File_t *file, s32 offset, s16 origin); |
242 | extern File * ReserveFilePointer(void); |
- | |
243 | extern u16 SeekSubDirectory(s8 *fname); |
- | |
244 | extern u8 CreateSubDirectory_(s8 *fname); |
- | |
245 | extern u8 FindItem(Find *); |
55 | s16 fgetc_(File_t *file); |
246 | extern void FreeFilePointer(File *file); |
- | |
247 | - | ||
248 | //________________________________________________________________________________________________________________________________________ |
- | |
249 | // |
56 | s16 fputc_(s8 c, File_t *file); |
250 | // Vaiables needed internaly for the fat16 implementation |
- | |
Line 251... | Line 57... | ||
251 | // |
57 | u32 fread_(void *buffer, u32 size, u32 count, File_t *file); |