diff --git a/sw/kernel/filesystems/fat32.h b/sw/kernel/filesystems/fat32.h index d4fd80d..7c3e8af 100644 --- a/sw/kernel/filesystems/fat32.h +++ b/sw/kernel/filesystems/fat32.h @@ -2,6 +2,7 @@ #define _FAT32_H #include +#include void fat32_init(); @@ -39,12 +40,13 @@ struct lfn_entry { uint16_t name_2[2]; }; -int8_t fat32_get_cluster_by_name(char* name, struct fat32_directory_entry* dentry); +int8_t fat32_get_cluster_by_name(const char* name, struct fat32_directory_entry* dentry); int8_t fat32_read_cluster(uint32_t cluster, void* buf); uint32_t fat32_next_cluster(uint32_t cluster); -int8_t fat32_file_open(const int8_t* filename); -int8_t fat32_file_read(int8_t fd, const void* buf, int8_t nbytes); -int8_t fat32_file_write(int8_t fd, const void* buf, int8_t nbytes); +int8_t fat32_file_open(const char* filename); +int8_t fat32_file_read(int8_t fd, void* buf, size_t nbytes); +int8_t fat32_file_write(int8_t fd, const void* buf, size_t nbytes); int8_t fat32_file_close(int8_t fd); + #endif diff --git a/sw/kernel/filesystems/fat32_c.c b/sw/kernel/filesystems/fat32_c.c index 43769cc..f99dc7e 100644 --- a/sw/kernel/filesystems/fat32_c.c +++ b/sw/kernel/filesystems/fat32_c.c @@ -13,15 +13,19 @@ int8_t fd_val; //TODO -int8_t fat32_file_write(int8_t fd, const void* buf, int8_t nbytes) { +int8_t fat32_file_write(int8_t fd, const void* buf, size_t nbytes) { + (void)fd; + (void)buf; + (void)nbytes; return -1; } int8_t fat32_file_close(int8_t fd) { + (void)fd; return -1; } -int8_t fat32_file_open(const int8_t* filename) { +int8_t fat32_file_open(const char* filename) { int8_t ret; int8_t i; int8_t fd; @@ -58,8 +62,33 @@ int8_t fat32_file_open(const int8_t* filename) { return fd; } -int8_t fat32_file_read(int8_t fd, const void* buf, int8_t nbytes) { +int8_t fat32_file_read(int8_t fd, void* buf, size_t nbytes) { + uint16_t i; + uint8_t error; + size_t offset; struct pcb* pcb = get_pcb_ptr(); + struct file_desc* fdesc = &pcb->file_desc_array[fd]; + uint32_t cluster_seq = fdesc->file_pos >> 9; + uint32_t cluster = ((uint32_t)fdesc->f32_dentry.cluster_high << 16) | fdesc->f32_dentry.cluster_low; + + /* validate starting position isn't past end of file */ + if (fdesc->file_pos >= fdesc->f32_dentry.file_size){ + return 0; + } + /* validate final pos isn't past end of file */ + if (fdesc->file_pos+nbytes > fdesc->f32_dentry.file_size){ + nbytes = fdesc->f32_dentry.file_size - fdesc->file_pos; + } + + + for (i = 0; i < cluster_seq; i++) { + cluster = fat32_next_cluster(cluster); + } + + fat32_read_cluster(cluster, sd_buf); + memcpy(buf, sd_buf, nbytes); + fdesc->file_pos += nbytes; + return error; } int8_t fat32_read_cluster(uint32_t cluster, void* buf) { @@ -69,6 +98,7 @@ int8_t fat32_read_cluster(uint32_t cluster, void* buf) { return error; } +// This will not handle clusters numbers that leaves a sector uint32_t fat32_next_cluster(uint32_t cluster) { uint8_t error; uint32_t addr = fat_start_sector; @@ -78,7 +108,7 @@ uint32_t fat32_next_cluster(uint32_t cluster) { return cluster_val; } -int8_t fat32_get_cluster_by_name(char* name, struct fat32_directory_entry* dentry) { +int8_t fat32_get_cluster_by_name(const char* name, struct fat32_directory_entry* dentry) { struct fat32_directory_entry* local_entry; int i = 0; @@ -90,7 +120,7 @@ int8_t fat32_get_cluster_by_name(char* name, struct fat32_directory_entry* dentr fat32_read_cluster(root_cluster, sd_buf); for (i = 0; i < 16; i++){ - local_entry = sd_buf + i*32; + local_entry = (struct fat32_directory_entry*)(sd_buf + i*32); if (local_entry->attr1 == 0xf || local_entry->attr1 & 0x8 || !local_entry->attr1) { continue; } diff --git a/sw/kernel/filesystems/fs.h b/sw/kernel/filesystems/fs.h index c457ecc..b9834eb 100644 --- a/sw/kernel/filesystems/fs.h +++ b/sw/kernel/filesystems/fs.h @@ -2,19 +2,20 @@ #define _FS_H #include +#include #include /* syscalls for files */ -int8_t file_read(int8_t fd, void* buf, int8_t nbytes); -int8_t file_write(int8_t fd, const void* buf, int8_t nbytes); -int8_t file_open(const int8_t* filename); +int8_t file_read(int8_t fd, void* buf, size_t nbytes); +int8_t file_write(int8_t fd, const void* buf, size_t nbytes); +int8_t file_open(const char* filename); int8_t file_close(int8_t fd); /* syscalls for directories */ -int8_t directory_read(int8_t fd, void* buf, int8_t nbytes); -int8_t directory_write(int8_t fd, const void* buf, int8_t nbytes); -int8_t directory_open(const int8_t* filename); +int8_t directory_read(int8_t fd, void* buf, size_t nbytes); +int8_t directory_write(int8_t fd, const void* buf, size_t nbytes); +int8_t directory_open(const char* filename); int8_t directory_close(int8_t fd); diff --git a/sw/kernel/process/process.h b/sw/kernel/process/process.h index f624a3d..2627886 100644 --- a/sw/kernel/process/process.h +++ b/sw/kernel/process/process.h @@ -4,6 +4,7 @@ #include #include +#include #define FILE_DESC_SIZE 8 @@ -12,10 +13,10 @@ struct fops { - int8_t (*open)(const int8_t* filename); + int8_t (*open)(const char* filename); int8_t (*close)(int8_t fd); - int8_t (*read)(int8_t fd, void* buf, int8_t nbytes); - int8_t (*write)(int8_t fd, const void* buf, int8_t nbytes); + int8_t (*read)(int8_t fd, void* buf, size_t nbytes); + int8_t (*write)(int8_t fd, const void* buf, size_t nbytes); }; struct file_desc { diff --git a/sw/test_code/fs_test/main.c b/sw/test_code/fs_test/main.c index 014cc8f..9e8ac87 100644 --- a/sw/test_code/fs_test/main.c +++ b/sw/test_code/fs_test/main.c @@ -4,12 +4,20 @@ void fat32_init(void); +char data [256]; + int main(void) { int8_t fd; + size_t i; fat32_init(); /* This is what is going to be part of open */ fd = fat32_file_open("VERYLA~1TXT"); - cprintf("fd: %x\n", fd); + + while (fat32_file_read(fd, data, 256)){ + for (i = 0; i < 256; i++) { + cprintf("%c", data[i]); + } + } } \ No newline at end of file