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