This repository has been archived by the owner on Nov 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
disk.c
238 lines (218 loc) · 7.98 KB
/
disk.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
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
229
230
231
232
233
234
235
236
237
238
#include "bootloader.h"
/* Yes, we will assume little endian. But since we already have so much other
* x86-specific code, it doesn't really matter. */
struct partition_table_entry {
uint8_t bootable;
uint8_t chs_start[3];
uint8_t system_id;
uint8_t chs_end[3];
uint32_t lba_start;
uint32_t sector_count;
} __attribute__((packed));
static char *boot_sector;
static struct partition_table_entry partition_table[4];
static int boot_partition_id;
static uint32_t first_root_dir_sector, first_data_sector;
static uint32_t cluster_size;
static uint16_t *cached_fat;
struct fat_bpb {
uint8_t init_bytes[3];
char oem_identifier[8];
uint16_t bytes_per_sector;
uint8_t sectors_per_cluster;
uint16_t reserved_sectors;
uint8_t fat_count;
uint16_t root_entry_count;
uint16_t sector_count_16;
uint8_t media_descriptor;
uint16_t sectors_per_fat;
uint16_t sectors_per_track;
uint16_t head_count;
uint32_t hidden_sectors;
uint32_t sector_count_32;
char unused[476];
} __attribute__((packed));
static struct fat_bpb *bpb;
union fat_entry {
struct {
char filename[8];
char extension[3];
uint8_t attrs;
uint8_t reserved;
uint8_t create_time_fraction;
uint16_t create_time;
uint16_t create_date;
uint16_t last_access_date;
uint16_t cluster_high;
uint16_t last_mod_time;
uint16_t last_mod_date;
uint16_t cluster_low;
uint32_t file_size;
} __attribute__((packed)) reg_entry;
struct {
uint8_t seq_number;
uint16_t name1[5];
uint8_t attrs; // always 0x0F for LFN
uint8_t reserved1;
uint8_t checksum;
uint16_t name2[6];
uint16_t reserved2;
uint16_t name3[2];
} __attribute__((packed)) lfn_entry;
} __attribute__((packed));
struct file {
char name[256];
uint16_t first_cluster;
uint32_t size;
struct file *next;
};
struct file *file_list = NULL;
static char *cluster_buf = NULL;
static void read_boot_partition_sector(uint32_t index, void *buffer)
{
struct partition_table_entry *boot_entry = partition_table + boot_partition_id;
if (index >= boot_entry->sector_count) {
boot_error("Partition read out of bounds");
}
ata_read_sector(index + boot_entry->lba_start, buffer);
}
void disk_init()
{
boot_sector = malloc(512);
ata_read_sector(0, boot_sector);
for (int i = 0; i < 4; i++) {
memcpy(partition_table + i, boot_sector + 0x1BE + 16 * i, 16);
}
if (partition_table[0].system_id == 0x06) {
boot_partition_id = 0;
} else {
boot_error("Boot partition not found");
}
bpb = malloc(512);
read_boot_partition_sector(0, bpb);
first_root_dir_sector = bpb->reserved_sectors +
(uint32_t)(bpb->fat_count) * bpb->sectors_per_fat;
first_data_sector = first_root_dir_sector + bpb->root_entry_count / 16;
cluster_size = (uint32_t)(bpb->sectors_per_cluster) * 512;
cluster_buf = malloc(cluster_size);
cached_fat = malloc(512 * bpb->sectors_per_fat);
for (uint32_t i = 0; i < bpb->sectors_per_fat; i++) {
read_boot_partition_sector(bpb->reserved_sectors + i,
cached_fat + 256 * i);
}
char name_buf[300];
for (int i = 0; i < 300; i++) name_buf[i] = '\0';
union fat_entry sector_buf[16];
bool has_lfn = false;
for (uint32_t sec = first_root_dir_sector; sec < first_data_sector; sec++) {
read_boot_partition_sector(sec, sector_buf);
for (int entry = 0; entry < 16; entry++) {
if (sector_buf[entry].reg_entry.filename[0] == '\0') {
continue; // unused entry
}
if (sector_buf[entry].lfn_entry.attrs == 0x0F) {
// LFN entry
int entry_index = sector_buf[entry].lfn_entry.seq_number & 0x3F;
if (!entry_index || entry_index > 20) {
boot_error("Boot partition invalid: invalid LFN sequence number");
}
int buf_offset = 13 * (entry_index - 1);
for (int i = 0; i < 5; i++) {
name_buf[buf_offset + i] = (char)(sector_buf[entry].lfn_entry.name1[i] & 0x7F);
}
for (int i = 5; i < 11; i++) {
name_buf[buf_offset + i] = (char)(sector_buf[entry].lfn_entry.name2[i-5] & 0x7F);
}
for (int i = 11; i < 13; i++) {
name_buf[buf_offset + i] = (char)(sector_buf[entry].lfn_entry.name3[i-11] & 0x7F);
}
if (sector_buf[entry].lfn_entry.seq_number >= 0x40) {
name_buf[buf_offset + 13] = '\0';
}
has_lfn = true;
} else {
// regular entry
if (!has_lfn) {
memcpy(name_buf, sector_buf[entry].reg_entry.filename, 8);
int name_len = 8;
while (name_len && name_buf[name_len - 1] == ' ') name_len--;
if (sector_buf[entry].reg_entry.extension[0] != ' ') {
name_buf[name_len] = '.';
memcpy(name_buf + name_len + 1, sector_buf[entry].reg_entry.extension, 3);
name_len += 4;
while (name_len && name_buf[name_len - 1] == ' ') name_len--;
}
name_buf[name_len] = '\0';
}
struct file *cur_file = malloc(sizeof(struct file));
for (int i = 0; i < 255; i++) cur_file->name[i] = name_buf[i];
cur_file->name[255] = '\0'; // just to be on the safe side
cur_file->first_cluster = sector_buf[entry].reg_entry.cluster_low;
cur_file->next = file_list;
cur_file->size = sector_buf[entry].reg_entry.file_size;
file_list = cur_file;
has_lfn = false;
}
}
}
}
static void read_cluster(uint32_t id, void *buffer)
{
if (id < 2) boot_error("Attempted to read invalid cluster");
char *cbuffer = (char *)buffer;
for (int i = 0; i < bpb->sectors_per_cluster; i++) {
read_boot_partition_sector(first_data_sector + bpb->sectors_per_cluster * (id-2) + i,
cbuffer + 512 * i);
}
}
static struct file *lookup_filename(const char *name)
{
struct file *ptr = file_list;
while (ptr) {
if (!strcmp_ignore_case(ptr->name, name)) return ptr;
ptr = ptr->next;
}
return NULL;
}
bool file_exists(const char *name)
{
return lookup_filename(name) != NULL;
}
uint32_t get_file_size(const char *name)
{
struct file *file = lookup_filename(name);
if (!file) return 0;
return file->size;
}
bool read_file(const char *name, uint32_t offset, uint32_t amount, void *buffer)
{
struct file *cur_file = lookup_filename(name);
if (!cur_file) return false;
if (!amount) return true;
uint32_t cluster = cur_file->first_cluster;
if (!cluster) return false; // empty file
uint32_t cur_off = 0;
char *cbuffer = (char *)buffer;
uint32_t bytes_written = 0;
while (true) {
if (cur_off >= offset + amount) return true;
if (cluster < 2) {
boot_error("Boot partition invalid: bad cluster chain");
}
if (cluster >= 0xFFF8) return false;
if (cluster >= (uint32_t)(bpb->sectors_per_fat) * 256) {
boot_error("Boot partition invalid: bad cluster chain");
}
if (cur_off + cluster_size > offset) {
uint32_t cluster_begin = cur_off >= offset ? 0 : offset - cur_off;
uint32_t cluster_end = offset + amount >= cur_off + cluster_size
? cluster_size : offset + amount - cur_off;
read_cluster(cluster, cluster_buf);
memcpy(cbuffer + bytes_written, cluster_buf + cluster_begin,
cluster_end - cluster_begin);
bytes_written += cluster_end - cluster_begin;
}
cluster = cached_fat[cluster];
cur_off += cluster_size;
}
}