-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfatSupport.c
executable file
·179 lines (150 loc) · 5.29 KB
/
fatSupport.c
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
/******************************************************************************
* Supporting functions for the FAT project:
*
* read_sector
* write_sector
*
* get_fat_entry
* set_fat_entry
*
* Authors: Andy Kinley, Archana Chidanandan, David Mutchler and others.
* March, 2004.
*****************************************************************************/
#include <stdio.h>
/******************************************************************************
* FILE_SYSTEM_ID -- the file id for the file system (here, the floppy disk
* filesystem)
* BYTES_PER_SECTOR -- the number of bytes in each sector of the filesystem
*****************************************************************************/
extern FILE* FILE_SYSTEM_ID;
extern int BYTES_PER_SECTOR;
/******************************************************************************
* read_sector
*
* Read the specified sector from the file system and store that sector in the
* given buffer
*
* sector_number: The number of the sector to read (0, 1, 2, ...)
* buffer: The array into which to store the contents of the sector that is
* read
*
* Return: the number of bytes read, or -1 if the read fails.
*****************************************************************************/
int read_sector(int sector_number, unsigned char* buffer)
{
int bytes_read;
if (fseek(FILE_SYSTEM_ID,
(long) sector_number * (long) BYTES_PER_SECTOR, SEEK_SET) != 0)
{
printf("Error accessing sector %d\n", sector_number);
return -1;
}
bytes_read = fread(buffer, sizeof(char), BYTES_PER_SECTOR, FILE_SYSTEM_ID);
if (bytes_read != BYTES_PER_SECTOR)
{
printf("Error reading sector %d\n", sector_number);
return -1;
}
return bytes_read;
}
/*****************************************************************************
* write_sector
*
* Write the contents of the given buffer to the filesystem at the specified
* sector
*
* sector_number: The number of the sector to write (0, 1, 2, ...)
* buffer: The array whose contents are to be written
*
* Return: the number of bytes written, or -1 if the read fails.
****************************************************************************/
int write_sector(int sector_number, unsigned char* buffer)
{
int bytes_written;
if (fseek(FILE_SYSTEM_ID,
(long) sector_number * (long) BYTES_PER_SECTOR, SEEK_SET) != 0)
{
printf("Error accessing sector %d\n", sector_number);
return -1;
}
bytes_written = fwrite(buffer,
sizeof(char), BYTES_PER_SECTOR, FILE_SYSTEM_ID);
if (bytes_written != BYTES_PER_SECTOR)
{
printf("Error reading sector %d\n", sector_number);
return -1;
}
return bytes_written;
}
/*****************************************************************************
* get_fat_entry
*
* Get the specified entry from the given FAT
*
* fat_entry_number: The number of the FAT entry to get (0, 1, 2, ...)
* fat: The fat table from which to get the specified entry
*
* printf("Directory listing returned %d entries:\n", dnum);
* Return: the value at the specified entry of the given FAT
****************************************************************************/
int get_fat_entry(int fat_entry_number, unsigned char* fat)
{
int offset;
int uv, wx, yz;
offset = 3 * fat_entry_number / 2;
// Two FAT12 entries are stored into three bytes;
// if these bytes are uv,wx,yz then the two FAT12 entries are xuv and yzw
// odd fat entry number, return yzw
if (fat_entry_number & 0x0001)
{
wx = (int) fat[offset];
yz = (int) fat[offset + 1];
return ( (yz << 4) | ( (wx & 0x00f0) >> 4));
}
// even fat entry number, return xuv
else
{
uv = (int) fat[offset];
wx = (int) fat[offset + 1];
return ( ((wx & 0x000f) << 8) | uv );
}
}
/******************************************************************************
* set_fat_entry
*
* Set the specified entry in the given FAT to the given value
*
* fat_entry_number: The number of the FAT entry to set (0, 1, 2, ...)
* value: The given value to place in the FAT entry
* fat: The fat table in which to set the given value at the specified entry
*****************************************************************************/
void set_fat_entry(int fat_entry_number, int value, unsigned char* fat)
{
int offset;
int uv, wx, yz, a, b, c;
offset = 3 * fat_entry_number / 2;
// Two FAT12 entries are stored into three bytes;
// if these bytes are uv,wx,yz then the two FAT12 entries are xuv and yzw
// Let 0a,bc denote the fat_entry_number, written as two bytes (high and
// low, respectively)
a = value & 0x0f00;
b = value & 0x00f0;
c = value & 0x000f;
// odd fat entry number, change yzw to abc, i.e.,
if (fat_entry_number & 0x0001)
{
// wx = cx;
fat[offset] = (unsigned char) ((c << 4) | (fat[offset] & 0x000f));
// yz = ab;
fat[offset + 1] = (unsigned char) ((a >> 4) | (b >> 4));
}
// even fat entry number, change xuv to abc, i.e.,
else
{
// uv = bc;
fat[offset] = (unsigned char) (b | c);
// wx = wa;
fat[offset + 1] = (unsigned char) ((fat[offset + 1] &
0x00f0) | (a >> 8));
}
}