diff --git a/sw/kernel/filesystems/fat32.h b/sw/kernel/filesystems/fat32.h index 7c3e8af..50b508d 100644 --- a/sw/kernel/filesystems/fat32.h +++ b/sw/kernel/filesystems/fat32.h @@ -45,8 +45,8 @@ int8_t fat32_read_cluster(uint32_t cluster, void* buf); uint32_t fat32_next_cluster(uint32_t cluster); 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); +size_t fat32_file_read(int8_t fd, void* buf, size_t nbytes); +size_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 f99dc7e..d5334d1 100644 --- a/sw/kernel/filesystems/fat32_c.c +++ b/sw/kernel/filesystems/fat32_c.c @@ -13,7 +13,7 @@ int8_t fd_val; //TODO -int8_t fat32_file_write(int8_t fd, const void* buf, size_t nbytes) { +size_t fat32_file_write(int8_t fd, const void* buf, size_t nbytes) { (void)fd; (void)buf; (void)nbytes; @@ -62,7 +62,7 @@ int8_t fat32_file_open(const char* filename) { return fd; } -int8_t fat32_file_read(int8_t fd, void* buf, size_t nbytes) { +size_t fat32_file_read(int8_t fd, void* buf, size_t nbytes) { uint16_t i; uint8_t error; size_t offset; @@ -84,11 +84,17 @@ int8_t fat32_file_read(int8_t fd, void* buf, size_t nbytes) { for (i = 0; i < cluster_seq; i++) { cluster = fat32_next_cluster(cluster); } + + offset = fdesc->file_pos % 512; + + if (offset + nbytes > 512) { + cprintf("Need to do something about this!\n"); + } fat32_read_cluster(cluster, sd_buf); - memcpy(buf, sd_buf, nbytes); + memcpy(buf, sd_buf + offset, nbytes); fdesc->file_pos += nbytes; - return error; + return nbytes; } int8_t fat32_read_cluster(uint32_t cluster, void* buf) { diff --git a/sw/kernel/filesystems/fs.h b/sw/kernel/filesystems/fs.h index b9834eb..29b7cc1 100644 --- a/sw/kernel/filesystems/fs.h +++ b/sw/kernel/filesystems/fs.h @@ -7,14 +7,14 @@ #include /* syscalls for files */ -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); +size_t file_read(int8_t fd, void* buf, size_t nbytes); +size_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, size_t nbytes); -int8_t directory_write(int8_t fd, const void* buf, size_t nbytes); +size_t directory_read(int8_t fd, void* buf, size_t nbytes); +size_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 2627886..ad177ff 100644 --- a/sw/kernel/process/process.h +++ b/sw/kernel/process/process.h @@ -15,8 +15,8 @@ struct fops { int8_t (*open)(const char* filename); int8_t (*close)(int8_t fd); - int8_t (*read)(int8_t fd, void* buf, size_t nbytes); - int8_t (*write)(int8_t fd, const void* buf, size_t nbytes); + size_t (*read)(int8_t fd, void* buf, size_t nbytes); + size_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 9e8ac87..71b9da9 100644 --- a/sw/test_code/fs_test/main.c +++ b/sw/test_code/fs_test/main.c @@ -9,14 +9,15 @@ char data [256]; int main(void) { int8_t fd; size_t i; + size_t nbytes; 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++) { + while ((nbytes = fat32_file_read(fd, data, 256))){ + for (i = 0; i < nbytes; i++) { cprintf("%c", data[i]); } }