Get something working with read

It is not reading offset correctly
This commit is contained in:
Byron Lathi
2023-12-05 22:47:24 -08:00
parent 48b39eb92d
commit 4c3c3fd731
5 changed files with 61 additions and 19 deletions

View File

@@ -2,6 +2,7 @@
#define _FAT32_H #define _FAT32_H
#include <stdint.h> #include <stdint.h>
#include <stdio.h>
void fat32_init(); void fat32_init();
@@ -39,12 +40,13 @@ struct lfn_entry {
uint16_t name_2[2]; 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); int8_t fat32_read_cluster(uint32_t cluster, void* buf);
uint32_t fat32_next_cluster(uint32_t cluster); uint32_t fat32_next_cluster(uint32_t cluster);
int8_t fat32_file_open(const int8_t* filename); int8_t fat32_file_open(const char* filename);
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);
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);
int8_t fat32_file_close(int8_t fd); int8_t fat32_file_close(int8_t fd);
#endif #endif

View File

@@ -13,15 +13,19 @@ int8_t fd_val;
//TODO //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; return -1;
} }
int8_t fat32_file_close(int8_t fd) { int8_t fat32_file_close(int8_t fd) {
(void)fd;
return -1; return -1;
} }
int8_t fat32_file_open(const int8_t* filename) { int8_t fat32_file_open(const char* filename) {
int8_t ret; int8_t ret;
int8_t i; int8_t i;
int8_t fd; int8_t fd;
@@ -58,8 +62,33 @@ int8_t fat32_file_open(const int8_t* filename) {
return fd; 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 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) { 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; return error;
} }
// This will not handle clusters numbers that leaves a sector
uint32_t fat32_next_cluster(uint32_t cluster) { uint32_t fat32_next_cluster(uint32_t cluster) {
uint8_t error; uint8_t error;
uint32_t addr = fat_start_sector; uint32_t addr = fat_start_sector;
@@ -78,7 +108,7 @@ uint32_t fat32_next_cluster(uint32_t cluster) {
return cluster_val; 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; struct fat32_directory_entry* local_entry;
int i = 0; 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); fat32_read_cluster(root_cluster, sd_buf);
for (i = 0; i < 16; i++){ 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) { if (local_entry->attr1 == 0xf || local_entry->attr1 & 0x8 || !local_entry->attr1) {
continue; continue;
} }

View File

@@ -2,19 +2,20 @@
#define _FS_H #define _FS_H
#include <stdint.h> #include <stdint.h>
#include <stdio.h>
#include <process/process.h> #include <process/process.h>
/* syscalls for files */ /* syscalls for files */
int8_t file_read(int8_t fd, void* buf, int8_t nbytes); int8_t file_read(int8_t fd, void* buf, size_t nbytes);
int8_t file_write(int8_t fd, const void* buf, int8_t nbytes); int8_t file_write(int8_t fd, const void* buf, size_t nbytes);
int8_t file_open(const int8_t* filename); int8_t file_open(const char* filename);
int8_t file_close(int8_t fd); int8_t file_close(int8_t fd);
/* syscalls for directories */ /* syscalls for directories */
int8_t directory_read(int8_t fd, void* buf, int8_t nbytes); int8_t directory_read(int8_t fd, void* buf, size_t nbytes);
int8_t directory_write(int8_t fd, const void* buf, int8_t nbytes); int8_t directory_write(int8_t fd, const void* buf, size_t nbytes);
int8_t directory_open(const int8_t* filename); int8_t directory_open(const char* filename);
int8_t directory_close(int8_t fd); int8_t directory_close(int8_t fd);

View File

@@ -4,6 +4,7 @@
#include <stdint.h> #include <stdint.h>
#include <filesystems/fat32.h> #include <filesystems/fat32.h>
#include <stdio.h>
#define FILE_DESC_SIZE 8 #define FILE_DESC_SIZE 8
@@ -12,10 +13,10 @@
struct fops { struct fops {
int8_t (*open)(const int8_t* filename); int8_t (*open)(const char* filename);
int8_t (*close)(int8_t fd); int8_t (*close)(int8_t fd);
int8_t (*read)(int8_t fd, 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, int8_t nbytes); int8_t (*write)(int8_t fd, const void* buf, size_t nbytes);
}; };
struct file_desc { struct file_desc {

View File

@@ -4,12 +4,20 @@
void fat32_init(void); void fat32_init(void);
char data [256];
int main(void) { int main(void) {
int8_t fd; int8_t fd;
size_t i;
fat32_init(); fat32_init();
/* This is what is going to be part of open */ /* This is what is going to be part of open */
fd = fat32_file_open("VERYLA~1TXT"); fd = fat32_file_open("VERYLA~1TXT");
cprintf("fd: %x\n", fd); cprintf("fd: %x\n", fd);
while (fat32_file_read(fd, data, 256)){
for (i = 0; i < 256; i++) {
cprintf("%c", data[i]);
}
}
} }