diff --git a/sw/kernel/filesystems/fat32.h b/sw/kernel/filesystems/fat32.h index 2a02f8a..d4fd80d 100644 --- a/sw/kernel/filesystems/fat32.h +++ b/sw/kernel/filesystems/fat32.h @@ -43,4 +43,8 @@ int8_t fat32_get_cluster_by_name(char* name, struct fat32_directory_entry* dentr 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_close(int8_t fd); #endif diff --git a/sw/kernel/filesystems/fat32_c.c b/sw/kernel/filesystems/fat32_c.c index 438377a..43769cc 100644 --- a/sw/kernel/filesystems/fat32_c.c +++ b/sw/kernel/filesystems/fat32_c.c @@ -1,10 +1,67 @@ -#include "fat32.h" - #include +#include #include #include #include +#include "fat32.h" +#include "fs.h" + +static struct fops fat32_file_ops = {fat32_file_open, fat32_file_close, fat32_file_read, fat32_file_write}; + +int8_t fd_val; + + +//TODO +int8_t fat32_file_write(int8_t fd, const void* buf, int8_t nbytes) { + return -1; +} + +int8_t fat32_file_close(int8_t fd) { + return -1; +} + +int8_t fat32_file_open(const int8_t* filename) { + int8_t ret; + int8_t i; + int8_t fd; + + struct fat32_directory_entry dentry; + + struct pcb* pcb = get_pcb_ptr(); + + ret = fat32_get_cluster_by_name(filename, &dentry); + if (ret) { + cprintf("Error finding cluster for filename"); + return -1; + } + + /* try to find an empty file desciptor, fail otherwise */ + //TODO We start at 3 here because 0, 1, 2 will be reserved later + for (i = 3; i < FILE_DESC_SIZE; i++) { + if (pcb->file_desc_array[i].flags == !IN_USE) { + fd = i; + break; + } + } + if (fd == -1){ + return -1; + } + + /* add process */ + pcb->file_desc_array[fd].f32_dentry = dentry; + pcb->file_desc_array[fd].flags = IN_USE; + pcb->file_desc_array[fd].file_pos = 0; + + pcb->file_desc_array[fd].file_ops = &fat32_file_ops; + + return fd; +} + +int8_t fat32_file_read(int8_t fd, const void* buf, int8_t nbytes) { + struct pcb* pcb = get_pcb_ptr(); +} + int8_t fat32_read_cluster(uint32_t cluster, void* buf) { uint8_t error; uint32_t addr = (cluster - 2) + data_start_sector; diff --git a/sw/kernel/filesystems/fs.h b/sw/kernel/filesystems/fs.h index b1e738b..c457ecc 100644 --- a/sw/kernel/filesystems/fs.h +++ b/sw/kernel/filesystems/fs.h @@ -3,16 +3,18 @@ #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 uint8_t* filename); +int8_t file_open(const int8_t* 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 uint8_t* filename); +int8_t directory_open(const int8_t* filename); int8_t directory_close(int8_t fd); diff --git a/sw/kernel/process/process.c b/sw/kernel/process/process.c new file mode 100644 index 0000000..489b371 --- /dev/null +++ b/sw/kernel/process/process.c @@ -0,0 +1,6 @@ +#include + + +struct pcb* get_pcb_ptr() { + return 0; +} \ No newline at end of file diff --git a/sw/kernel/process/process.h b/sw/kernel/process/process.h new file mode 100644 index 0000000..f624a3d --- /dev/null +++ b/sw/kernel/process/process.h @@ -0,0 +1,46 @@ +#ifndef _PROCESS_H +#define _PROCESS_H + +#include + +#include + +#define FILE_DESC_SIZE 8 + + +#define IN_USE 1 + + +struct fops { + int8_t (*open)(const int8_t* 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); +}; + +struct file_desc { + struct fops* file_ops; + uint8_t fs_type; + union { + struct fat32_directory_entry f32_dentry; + }; + uint32_t file_pos; + uint32_t flags; +}; + +/* Process Control Block struct */ +struct pcb { + struct file_desc file_desc_array[FILE_DESC_SIZE]; + int32_t is_vidmapped; + uint8_t args[128]; + uint32_t execute_return; + int32_t pid; + int32_t parent_pid; + uint32_t parent_esp; + uint32_t parent_ebp; +}; + + +struct pcb* get_pcb_ptr(); + +#endif \ No newline at end of file diff --git a/sw/test_code/fs_test/harness.c b/sw/test_code/fs_test/harness.c index cd72522..d11e150 100644 --- a/sw/test_code/fs_test/harness.c +++ b/sw/test_code/fs_test/harness.c @@ -1,9 +1,17 @@ #include #include #include +#include #define FILE_PATH "fs.fat" +struct pcb fake_pcb; + +//TODO +struct pcb* get_pcb_ptr() { + return &fake_pcb; +} + uint32_t lmulii(uint16_t a, uint16_t b) { printf("lmulii: %x * %x = %x\n", a, b, a*b); return a * b; diff --git a/sw/test_code/fs_test/main.c b/sw/test_code/fs_test/main.c index 2245139..014cc8f 100644 --- a/sw/test_code/fs_test/main.c +++ b/sw/test_code/fs_test/main.c @@ -5,35 +5,11 @@ void fat32_init(void); int main(void) { - struct fat32_directory_entry dentry; - int i; - uint32_t offset = 0; - uint32_t cluster; - uint8_t cluster_count; + int8_t fd; fat32_init(); /* This is what is going to be part of open */ - fat32_get_cluster_by_name("VERYLA~1TXT", &dentry); - cprintf("attr1: %x\n", dentry.attr1); - cprintf("cluster: %x%x\n", dentry.cluster_high, dentry.cluster_low); - cprintf("File Size: %lx\n", dentry.file_size); + fd = fat32_file_open("VERYLA~1TXT"); - - /* This will become part of read */ - cluster = (dentry.cluster_high << 16) | dentry.cluster_low; - cluster++; - fat32_read_cluster(cluster + offset/512, sd_buf); - - cluster_count = (dentry.file_size/512)/sectors_per_cluster; - cprintf("Cluster count: %x\n", cluster_count); - - while (cluster < 0xffffff0) { - for (i = 0; i < dentry.file_size && i < 512; i++) { - cprintf("%c", sd_buf[i]); - } - cluster = fat32_next_cluster(cluster); - fat32_read_cluster(cluster + offset/512, sd_buf); - } - - return 0; + cprintf("fd: %x\n", fd); } \ No newline at end of file