src/kernel/fat.c (view raw)
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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
#include "cedos/file.h"
#include "cedos/fat.h"
#include "string.h"
#include "assert.h"
#include "cedos/mm/memory.h"
#include <stdint.h>
file_operations_t FAT_fops = {
NULL, /* open */
FAT_openat, /* openat */
FAT_read, /* read */
NULL, /* write */
FAT_dir_next, /* dir_next */
FAT_lseek, /* lseek */
FAT_tell /* tell */
};
typedef struct {
char jmp[3];
char oemname[8];
uint16_t bytes_per_sect;
uint8_t sect_per_cluster;
uint16_t num_reserved_sectors;
uint8_t num_FAT;
uint16_t max_root_dir_entries;
uint16_t total_log_sectors;
uint8_t media_desc;
uint16_t log_sect_per_fat;
} __attribute__((packed)) BOOT_SECT;
typedef struct {
char name[8];
char ext[3];
uint8_t file_attr;
uint8_t user_attr;
uint8_t del_char;
uint16_t create_time;
uint16_t create_date;
uint16_t last_access_date;
uint16_t access_rights;
uint16_t last_modified_time;
uint16_t last_modified_date;
uint16_t start_of_clusters;
uint32_t file_size;
} __attribute__((packed)) DIR_ENTRY;
typedef struct {
uint8_t seq_num;
uint16_t part_1[5];
uint8_t file_attr;
uint8_t user_attr;
uint8_t del_char;
uint16_t part_2[6];
uint16_t start_of_clusters;
uint16_t part_3[2];
} __attribute__((packed)) VFAT_LFN_ENTRY;
void *FAT_addr;
BOOT_SECT *boot_sect;
uint32_t FAT1_lba;
uint32_t FAT2_lba;
uint32_t root_lba;
uint32_t data_lba;
uint8_t *cluster_buffer;
uint32_t cluster_size;
void FAT_init() {
// open image file
FAT_addr = (void*)(0x10000);
boot_sect = (BOOT_SECT*)(FAT_addr);
FAT1_lba = boot_sect->num_reserved_sectors;
FAT2_lba = FAT1_lba + boot_sect->log_sect_per_fat;
root_lba = FAT1_lba + (boot_sect->log_sect_per_fat * boot_sect->num_FAT);
long root_dir_size = boot_sect->max_root_dir_entries * sizeof(DIR_ENTRY);
data_lba = root_lba + (root_dir_size / boot_sect->bytes_per_sect);
cluster_size = boot_sect->bytes_per_sect * boot_sect->sect_per_cluster;
cluster_buffer = os_kernel_malloc(cluster_size);
}
void *FAT_read_sector_offset(uint32_t lba, uint32_t *offset) {
if (offset != NULL) {
lba += (*offset) / boot_sect->bytes_per_sect;
*offset = (*offset) % boot_sect->bytes_per_sect;
}
return (void*)((long)(FAT_addr) + (long)(lba * boot_sect->bytes_per_sect));
}
void *FAT_read_cluster(uint16_t cluster, void *buffer) {
// TODO: perform memcpy
void *addr = FAT_read_sector_offset(data_lba + ((cluster - 2) * boot_sect->sect_per_cluster), NULL);
uint32_t cluster_size = boot_sect->bytes_per_sect * boot_sect->sect_per_cluster;
memcpy(buffer, addr, cluster_size);
return (void*)((uint8_t*)(buffer) + cluster_size);
}
int FAT_root_dir_next(int index, char *fname_buffer, uint16_t *first_cluster, uint32_t *file_size) {
//memset(fname_buffer, 0, sizeof(fname_buffer));
while (1) {
// index overflow
if (index >= boot_sect->max_root_dir_entries) {
return -1;
}
uint32_t offset = index * sizeof(DIR_ENTRY);
void *sect = FAT_read_sector_offset(root_lba, &offset);
DIR_ENTRY *dir_entry = (DIR_ENTRY *)((uint32_t)(sect) + offset);
// if first character of name is 0, then no subsequent entry is in use
if (dir_entry->name[0] == 0x00) {
return -1;
}
// deleted file
if (dir_entry->name[0] == (char)(0xE5)) {
index++;
continue;
}
// VFAT LFN entry
if (dir_entry->file_attr == 0x0F && dir_entry->start_of_clusters == 0 && dir_entry->file_size != 0) {
VFAT_LFN_ENTRY *lfn_entry = (VFAT_LFN_ENTRY*)(dir_entry);
int offset = 13 * ((lfn_entry->seq_num & 0x3F) - 1);
// read long file name
for (int i = 0; i < 5; i++) {
fname_buffer[offset++] = lfn_entry->part_1[i];
}
for (int i = 0; i < 6; i++) {
fname_buffer[offset++] = lfn_entry->part_2[i];
}
for (int i = 0; i < 2; i++) {
fname_buffer[offset++] = lfn_entry->part_3[i];
}
index++;
continue;
}
if (index == 0 && (dir_entry->file_attr & 0x08) && dir_entry->file_size == 0) {
// volume label
index++;
continue;
} else if ((dir_entry->file_attr & 0x10) && dir_entry->file_size == 0) {
// subdirectory
} else {
// regular file
}
*file_size = dir_entry->file_size;
*first_cluster = dir_entry->start_of_clusters;
// if no VFAT LFN exists, use DOS name
if (fname_buffer[0] == 0) {
for (int i = 0; i < 8; i++) {
fname_buffer[i] = dir_entry->name[i];
}
fname_buffer[8] = '.';
for (int i = 0; i < 3; i++) {
fname_buffer[i + 9] = dir_entry->ext[i];
}
fname_buffer[12] = 0;
}
return index + 1;
}
}
int FAT_dir_next(file_t *file, int index, char *fname_buffer) {
uint16_t first_cluster;
uint32_t file_size;
// TODO: subdirectories
return FAT_root_dir_next(index, fname_buffer, &first_cluster, &file_size);
}
uint16_t FAT_next_cluster(uint16_t cluster) {
// assuming FAT12
uint32_t *offset = (cluster >> 1) * 3;
uint8_t *sect = FAT_read_sector_offset(FAT1_lba, &offset);
sect += (uint32_t)(offset);
if (cluster & 0x01) {
uint16_t high = (uint16_t)(sect[2]);
uint16_t low = (uint16_t)(sect[1] & 0xF0) >> 4;
return (high << 4) | low;
} else {
uint16_t low = (uint16_t)(sect[0]);
uint16_t high = (uint16_t)(sect[1] & 0x0F) << 8;
return low | high;
}
}
int FAT_openat(file_t *root, file_t *handle, const char *fname, int flags) {
int i = 0;
// TODO: take fd into consideration (open file in that subdirectory)
uint16_t first_cluster;
while (1) {
char buffer[832];
uint32_t file_size;
i = FAT_root_dir_next(i, buffer, &first_cluster, &file_size);
if (i <= 0) { return -1; }
if (strcmp(buffer, fname) == 0) {
// file found
handle->pos = 0;
handle->size = file_size;
handle->fops = &FAT_fops;
handle->fat_cluster = first_cluster;
return 0;
}
}
}
uint32_t FAT_read(file_t *file, uint8_t *buffer, uint32_t count) {
uint16_t cluster = file->fat_cluster;
fpos_t offset = file->pos;
size_t file_size = file->size;
uint32_t size = 0;
if (offset + count > file_size) {
count = file_size - offset;
}
while (offset >= cluster_size) {
cluster = FAT_next_cluster(cluster);
if (cluster == 0xFFF || cluster == 0x000) { return -1; }
offset -= cluster_size;
}
while (count > 0) {
if (cluster == 0xFFF || cluster == 0x000) { break; }
FAT_read_cluster(cluster, cluster_buffer);
cluster = FAT_next_cluster(cluster);
uint32_t memcpy_size;
if (offset + count > cluster_size) {
memcpy_size = (cluster_size - offset);
} else {
memcpy_size = count;
}
memcpy(buffer, (cluster_buffer + offset), memcpy_size);
offset = 0;
count -= memcpy_size;
buffer += memcpy_size;
size += memcpy_size;
}
file->pos += size;
return size;
}
off_t FAT_lseek(file_t *file, off_t offset, int whence) {
if (whence == SEEK_SET) {
file->pos = offset;
} else if (whence == SEEK_CUR) {
file->pos += offset;
} else if (whence == SEEK_END) {
// to be implemented
} else {
kpanic("Wrong whence!");
}
}
off_t FAT_tell(file_t *file) {
return file->pos;
}