This repository was archived by the owner on Jun 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFAT.h
More file actions
228 lines (197 loc) · 6.95 KB
/
FAT.h
File metadata and controls
228 lines (197 loc) · 6.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#ifndef FAT_H_
#define FAT_H_
#include "string.h"
#include "lib_c.h"
#include "lib_asm.h"
//FAT constant values
#define END_CLUSTER_32 0x0FFFFFF8 //Use OSDev.org's suggestion of 0x0FFFFFF8 even though MSYS docs > OSdev.org.
#define BAD_CLUSTER_32 0x0FFFFFF7
#define FREE_CLUSTER_32 0x00000000
#define END_CLUSTER_16 0xFFF8
#define BAD_CLUSTER_16 0xFFF7
#define FREE_CLUSTER_16 0x0000
#define END_CLUSTER_12 0xFF8
#define BAD_CLUSTER_12 0xFF7
#define FREE_CLUSTER_12 0x000
#define CLEAN_EXIT_BMASK_16 0x8000
#define HARD_ERR_BMASK_16 0x4000
#define CLEAN_EXIT_BMASK_32 0x08000000
#define HARD_ERR_BMASK_32 0x04000000
#define FILE_READ_ONLY 0x01
#define FILE_HIDDEN 0x02
#define FILE_SYSTEM 0x04
#define FILE_VOLUME_ID 0x08
#define FILE_DIRECTORY 0x10
#define FILE_ARCHIVE 0x20
#define FILE_LONG_NAME (FILE_READ_ONLY|FILE_HIDDEN|FILE_SYSTEM|FILE_VOLUME_ID)
#define FILE_LONG_NAME_MASK (FILE_READ_ONLY|FILE_HIDDEN|FILE_SYSTEM|FILE_VOLUME_ID|FILE_DIRECTORY|FILE_ARCHIVE)
#define FILE_LAST_LONG_ENTRY 0x40
#define ENTRY_FREE 0xE5
#define ENTRY_END 0x00
#define ENTRY_JAPAN 0x05
#define LAST_LONG_ENTRY 0x40
#define LOWERCASE_ISSUE 0x01 //E.g.: "test txt"
#define BAD_CHARACTER 0x02 //E.g.: "tes&t txt"
#define BAD_TERMINATION 0x04 //missing null character at the end
#define NOT_CONVERTED_YET 0x08 //still contains a dot: E.g."test.txt"
#define TOO_MANY_DOTS 0x10 //E.g.: "test..txt"; may or may not have already been converted
#define GET_CLUSTER_FROM_ENTRY(x) (x.low_bits | (x.high_bits << (fat_type / 2)))
#define GET_ENTRY_LOW_BITS(x) (x & ((fat_type /2) -1))
#define GET_ENTRY_HIGH_BITS(x) (x >> (fat_type / 2))
#define CONCAT_ENTRY_HL_BITS(high, low) ((high << (fat_type / 2)) | low)
#ifndef NULL
#define NULL 0
#endif
#ifndef BOOL
#define BOOL short
#endif
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#ifndef DISK_READ_LOCATION
#define DISK_READ_LOCATION 0x40000
#endif
#ifndef DISK_WRITE_LOCATION
#define DISK_WRITE_LOCATION 0x40000
#endif
extern int int13h_read(unsigned long sector, unsigned char num);
extern int int13h_read_o(unsigned long sector, unsigned char num, unsigned long memoffset);
extern int int13h_write(unsigned long sector, unsigned char num);
extern int int13h_write_o(unsigned long sector, unsigned char num, unsigned long memoffset);
extern void drawtext(int charnum);
extern int height;
extern int width;
//Declarations to use when testing with a virtual disk
//FAT directory and bootsector structures
typedef struct fat_extBS_32
{
//extended fat32 stuff
unsigned int table_size_32;
unsigned short extended_flags;
unsigned short fat_version;
unsigned int root_cluster;
unsigned short fat_info;
unsigned short backup_BS_sector;
unsigned char reserved_0[12];
unsigned char drive_number;
unsigned char reserved_1;
unsigned char boot_signature;
unsigned int volume_id;
unsigned char volume_label[11];
unsigned char fat_type_label[8];
}
__attribute__((packed))
fat_extBS_32_t;
typedef struct fat_extBS_16
{
//extended fat12 and fat16 stuff
unsigned char bios_drive_num;
unsigned char reserved1;
unsigned char boot_signature;
unsigned int volume_id;
unsigned char volume_label[11];
unsigned char fat_type_label[8];
}
__attribute__((packed))
fat_extBS_16_t;
typedef struct fat_BS
{
unsigned char bootjmp[3];
unsigned char oem_name[8];
unsigned short bytes_per_sector;
unsigned char sectors_per_cluster;
unsigned short reserved_sector_count;
unsigned char table_count;
unsigned short root_entry_count;
unsigned short total_sectors_16;
unsigned char media_type;
unsigned short table_size_16;
unsigned short sectors_per_track;
unsigned short head_side_count;
unsigned int hidden_sector_count;
unsigned int total_sectors_32;
//this will be cast to it's specific type once the driver actually knows what type of FAT this is.
unsigned char extended_section[54];
}
__attribute__((packed))
fat_BS_t;
/* from http://wiki.osdev.org/FAT */
typedef struct directory_entry
{
unsigned char file_name[11];
unsigned char attributes;
unsigned char reserved0;
unsigned char creation_time_tenths;
unsigned short creation_time;
unsigned short creation_date;
unsigned short last_accessed;
unsigned short high_bits;
unsigned short last_modification_time;
unsigned short last_modification_date;
unsigned short low_bits;
unsigned int file_size;
}
__attribute__((packed))
directory_entry_t;
typedef struct fsInfo
{
unsigned int lead_signature; //should contain 0x41615252
unsigned char reserved1[480];
unsigned int structure_signature; //should contain 0x61417272
unsigned int free_space; //contains last known free cluster count. 0xFFFFFFFF indicates count is unknown.
unsigned int last_written; //contains last-written cluster number to help FAT drivers find a free cluster. 0xFFFFFFFF indicates that cluster number is unknown.
unsigned char reserved2[12];
unsigned int trail_signature; //should contain 0xAA550000
}
__attribute__((packed))
FSInfo_t;
typedef struct long_entry
{
unsigned char order;
unsigned char first_five[10]; //first 5, 2-byte characters
unsigned char attributes; //MUST BE FILE_LONG_NAME
unsigned char type; //indicates a sub-component of a long name (leave as 0)
unsigned char checksum;
unsigned char next_six[12]; //next 6, 2-byte characters
unsigned short zero; //must be zero - otherwise meaningless
unsigned char last_two[4]; //last 2, 2-byte characters
}
__attribute__((packed))
long_entry_t;
//Global variables
extern unsigned int fat_type;
extern unsigned int first_fat_sector;
extern unsigned int first_data_sector;
extern unsigned int total_clusters;
extern fat_BS_t bootsect;
//unsigned int fat_type;
//unsigned int first_fat_sector;
//unsigned int first_data_sector;
//unsigned int total_clusters;
//fat_BS_t bootsect;
//FAT functions (see the .c file for function descriptions)
int FATInitialize();
int FATRead(unsigned int clusterNum);
int FATWrite(unsigned int clusterNum, unsigned int clusterVal);
unsigned int allocateFreeFAT();
int clusterRead(unsigned int clusterNum, unsigned int clusterOffset);
int clusterWrite(void* contentsToWrite, unsigned int contentSize, unsigned int contentBuffOffset, unsigned int clusterNum);
int directoryList(const unsigned int cluster, unsigned char attributesToAdd, short exclusive);
int directorySearch(const char* filepart, const unsigned int cluster, directory_entry_t* file, unsigned int* entryOffset);
int directoryAdd(const unsigned int cluster, directory_entry_t* file_to_add);
int getFile(const char* filePath, char** fileContents, directory_entry_t* fileMeta, unsigned int readInOffset);
int putFile(const char* filePath, char** fileContents, directory_entry_t* fileMeta);
unsigned short CurrentTime();
unsigned char CurrentTimeTenths();
unsigned short CurrentDate();
unsigned char ChkSum(unsigned char *pFcbName);
BOOL testIfFATFormat(char * input);
char* convertToFATFormat(char* input);
void convertFromFATFormat(char* input, char* output);
void d_printhex( unsigned long num, int digits );
void d_printss( char *s );
void d_printsss( char *s, int n );
#endif