-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfat.c
192 lines (161 loc) · 4.45 KB
/
fat.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
180
181
182
183
184
185
186
187
188
189
190
191
192
#include "fat.h"
#include "lib/print.h"
#include "ata.h"
#include "string.h"
#include "paging.h"
#define SECTOR_SIZE 512
static void print_time(short arg)
{
union fat_time time;
time.w = arg;
int hours = time.hours & 0x1f;
int minutes = time.minutes & 0x3f;
int seconds = (time.seconds & 0x1f) * 2;
printk("%2.d:%2.d:%2.d", hours, minutes, seconds);
}
static void print_date(short arg)
{
union fat_date date;
date.w = arg;
char *months[] =
{
"JAN", "FEB", "MAR", "APR",
"MAY", "JUN", "JUL", "AUG",
"SEP", "OCT", "NOV", "DEC"
};
int year = (date.year & 0x7f) + 1980;
int month = date.month & 0xf;
int day = date.day & 0x1f;
printk("%d-%s-%d", day, months[month - 1], year);
}
static void print_dirent(const struct fat_directory *dir)
{
printk("%.*s %x ", sizeof dir->filename, dir->filename, dir->attributes);
print_time(dir->time_created);
printk(" ");
print_date(dir->date_created);
printk(" %x %d B", dir->cluster_lo, dir->file_size);
}
static char bootrecord_bytes[SECTOR_SIZE];
static struct boot_record *br;
static void load_bootrecord(void)
{
unsigned partition_start_sector = FS_OFFSET / SECTOR_SIZE;
ata_lba_read(partition_start_sector, 1, (void*)bootrecord_bytes);
br = (struct boot_record *)bootrecord_bytes;
}
static char fat[SECTOR_SIZE];
static void load_fat(void)
{
unsigned partition_start_sector = FS_OFFSET / SECTOR_SIZE;
unsigned fat_base = partition_start_sector + br->number_of_reserved_sectors;
ata_lba_read(fat_base, 1, (void*)fat);
}
static char dir_bytes[SECTOR_SIZE];
static struct fat_directory *dir = (struct fat_directory*)dir_bytes;
static void load_rootdir(void)
{
unsigned partition_start_sector = FS_OFFSET / SECTOR_SIZE;
unsigned fat_base = partition_start_sector + br->number_of_reserved_sectors;
unsigned first_fat_sector = fat_base
+ br->number_of_file_allocation_tables * br->number_of_sectors_per_fat;
ata_lba_read(first_fat_sector, 1, (void*)dir);
}
void fs_init(void)
{
load_bootrecord();
load_fat();
load_rootdir();
}
void print_dir(const char *param)
{
(void)param;
for (int i = 0; i < br->number_of_root_directory_entries; ++i)
{
struct fat_directory *dirp = dir + i;
if (!*dirp->filename)
break;
print_dirent(dirp);
printk("\r\n");
}
}
static struct fat_directory *find_file(const char *filename)
{
return find_starts_with(filename);
}
static void load(struct fat_directory *file, char *dst)
{
unsigned active_cluster = file->cluster_lo;
unsigned partition_start_sector = FS_OFFSET / SECTOR_SIZE;
unsigned fat_base = partition_start_sector + br->number_of_reserved_sectors;
unsigned first_fat_sector = fat_base
+ br->number_of_file_allocation_tables * br->number_of_sectors_per_fat;
do {
unsigned fat_offset = active_cluster + (active_cluster / 2);
unsigned int ent_offset = fat_offset % SECTOR_SIZE;
unsigned short table_value = *(unsigned short*)&fat[ent_offset];
if(active_cluster & 0x0001)
table_value = table_value >> 4;
else
table_value = table_value & 0x0FFF;
unsigned file_sector = first_fat_sector
+ (br->number_of_root_directory_entries
* sizeof (struct fat_directory))
/ br->number_of_bytes_per_sector
+ (active_cluster - 2)
* br->number_of_sectors_per_cluster;
if (!active_cluster)
file_sector = first_fat_sector;
ata_lba_read(file_sector, br->number_of_sectors_per_cluster, (void*)dst);
active_cluster = table_value;
dst += br->number_of_bytes_per_sector * br->number_of_sectors_per_cluster;
} while (active_cluster < 0xff8);
}
void file_close(struct file fi)
{
free(fi.data, fi.size * 4);
}
struct file file_open(const char *filename)
{
struct file ret;
struct fat_directory *file = find_starts_with(filename);
if (!file)
{
printk("file <%s> not found.\r\n", filename);
return;
}
ret.data = alloc(file->file_size * 4);
ret.size = file->file_size;
load(file, ret.data);
return ret;
}
void print_file(const char *filename)
{
struct file fi = file_open(filename);
for (int i = 0; i < fi.size; ++i)
printk("%c", fi.data[i]);
printk("\r\n");
file_close(fi);
}
void change_dir(const char *dirname)
{
struct fat_directory *file = find_file(dirname);
if (!file)
{
printk("directory <%s> not found.\r\n", dirname);
return;
}
load(file, dir_bytes);
}
struct fat_directory *find_starts_with(const char *prefix)
{
for (int i = 0; i < br->number_of_root_directory_entries; ++i)
{
struct fat_directory *dirp = dir + i;
if (!*dirp->filename)
break;
if (!strncmp(dirp->filename, prefix, strlen(prefix)))
return dirp;
}
return 0;
}