From 9fdc55130834070324f25eb123735809748c71a6 Mon Sep 17 00:00:00 2001 From: Ruslan Isaev Date: Sun, 17 Aug 2025 16:03:41 +0300 Subject: [PATCH] syscalls: improve working with files using vfs --- grubshit/aaa | Bin 8820 -> 8816 bytes grubshit/aaa.asm | 2 +- grubshit/aaa.o | Bin 880 -> 880 bytes include/vfs.h | 4 ++- src/fs/vfs.c | 47 +++++++++++++++++++++++++++++++++-- src/kernel/kernel.c | 13 +++------- src/tasking/exec_from_file.c | 6 +---- src/tasking/syscalls.c | 7 ++++-- 8 files changed, 58 insertions(+), 21 deletions(-) diff --git a/grubshit/aaa b/grubshit/aaa index db56b221084b69d215b9a0c7c4016053d8db654b..e3df0f990dc915589ac8766de6943db8a3a5604e 100755 GIT binary patch delta 186 zcmez3^1)?-0%OEP#bj1h1_lP5jh)l^l{6NxaO`4WWMDYkuvlXu3&#!?pww<4pJ69R zs#{cJvy;F+ewLEd;*!Z+3PzKy6x0|wCnqY%3xec<>VSZmK@Ui3OrEG9uLu(r0E)T- zMOC4y1Q?hYe1LqN$rlygGpbHLs3^}0QmZ=osiHcg#$;Y4b=DgS3=Dyj4HbkZ`zQ&p KYAS+M19<@9dMD=q delta 199 zcmez1^2KF>0%OcX#bj1(1_lO$jh)l^m2?)caO`4WWMDYkuvljy3&#!?pww<4pJ69R zs#{cNvy;F+elGpgq{N(j{fyGe$t(&+lPwff82Km1D##0hQ~^~30W*Uhkkpynt01ok z6BPi8x&lSDp{fKJm>7J3e1pkn72Y#yPu{C2&k9njJ^7)cI-|~HP9=55JChBS)FpHk Ofo1_!!T`ugAOHYiB`3uI diff --git a/grubshit/aaa.asm b/grubshit/aaa.asm index d3590ba..64c3078 100644 --- a/grubshit/aaa.asm +++ b/grubshit/aaa.asm @@ -1,5 +1,5 @@ section .data - file_path db '/ebalo/huy.txt', 0 ; File path + file_path db 'test.txt', 0 ; File path message db 'Hello, World from KatauOS!', 10, 0 ; Message with newline message_len equ $ - message - 1 ; Length excluding null terminator diff --git a/grubshit/aaa.o b/grubshit/aaa.o index b7c32e7d6f42f313fe95cfb3030a8388cab7f93f..5fcd3576dc88f3b2e96329cb890ea510cc630e5a 100644 GIT binary patch delta 32 ocmeys_JM6e4WsJDT4qL;lGNgo$z6N~f(+t!A diff --git a/include/vfs.h b/include/vfs.h index 41c3745..e17214b 100644 --- a/include/vfs.h +++ b/include/vfs.h @@ -5,12 +5,14 @@ #include "../include/atapi.h" #include "../include/lib9660.h" -extern l9660_fs global_fs; +extern l9660_fs* global_fs; +extern l9660_dir* root_dir; bool read_sector_callback(l9660_fs *fs, void *buf, uint32_t sector); void mount_fs(); void list_files(l9660_dir *dir); size_t vfs_read_file(l9660_file *file, uint8_t* buffer); +size_t vfs_read_file_length(l9660_file *file, uint8_t* buffer, size_t length); #endif \ No newline at end of file diff --git a/src/fs/vfs.c b/src/fs/vfs.c index 4042c9e..9f03275 100644 --- a/src/fs/vfs.c +++ b/src/fs/vfs.c @@ -2,8 +2,10 @@ #include "../include/stdio.h" #include "../include/string.h" #include "../include/paging.h" +#include "../include/liballoc.h" -l9660_fs global_fs; +l9660_fs* global_fs; +l9660_dir* root_dir; bool read_sector_callback(l9660_fs *fs, void *buf, uint32_t sector) { // The 'fs' parameter is not needed for our simple case, so we can ignore it. @@ -20,13 +22,26 @@ bool read_sector_callback(l9660_fs *fs, void *buf, uint32_t sector) { void mount_fs() { - l9660_status status = l9660_openfs(&global_fs, read_sector_callback); + global_fs = malloc(sizeof(l9660_fs)); + + l9660_status status = l9660_openfs(global_fs, read_sector_callback); if (status != L9660_OK) { printf("Error opening ISO9660 filesystem!\n"); return; } printf("FS mounted\n"); + + root_dir = malloc(sizeof(l9660_dir)); + + status = l9660_fs_open_root(root_dir, global_fs); + + if(status != L9660_OK) + { + printf("Error opening root dir\n"); + return; + } + printf("root dir opened\n"); } void list_files(l9660_dir *dir) @@ -73,5 +88,33 @@ size_t vfs_read_file(l9660_file *file, uint8_t* buffer) total_read += read; } + return total_read; +} + +size_t vfs_read_file_length(l9660_file *file, uint8_t* buffer, size_t length) +{ + size_t total_read = 0; + l9660_status status; + + while(total_read < length) { + char buf[128]; + size_t to_read = (length - total_read) < 128 ? (length - total_read) : 128; + size_t read = 0; + + status = l9660_read(file, buf, to_read, &read); + + if (status != L9660_OK) { + printf("An error occurred during file read!\n"); + break; + } + + //check for EOF + if (read == 0) + break; + + memcpy(buffer + total_read, buf, read); + total_read += read; + } + return total_read; } \ No newline at end of file diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 7807270..698b2c0 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -28,21 +28,14 @@ void apply_pic_masks() void test_iso9660() { - l9660_dir g_root_dir; l9660_file g_test_file; l9660_status status; - // 2. Open the root directory - status = l9660_fs_open_root(&g_root_dir, &global_fs); - if (status != L9660_OK) { - printf("Error opening root directory!\n"); - return; - } printf("Files in root directory:\n"); for (;;) { l9660_dirent *dent; - status = l9660_readdir(&g_root_dir, &dent); + status = l9660_readdir(root_dir, &dent); if (dent == NULL || status != L9660_OK) { break; // End of directory or error @@ -58,11 +51,11 @@ void test_iso9660() printf("\nReading content of /TEST.TXT:\n"); // Re-open the root directory to reset its position before searching - l9660_fs_open_root(&g_root_dir, &global_fs); + l9660_fs_open_root(root_dir, global_fs); l9660_dir boot_dir; l9660_dir grub_dir; - status = l9660_opendirat(&boot_dir, &g_root_dir, "boot"); + status = l9660_opendirat(&boot_dir, root_dir, "boot"); printf("STATUS %X\n", status); status = l9660_opendirat(&grub_dir, &boot_dir, "grub"); printf("STATUS %X\n", status); diff --git a/src/tasking/exec_from_file.c b/src/tasking/exec_from_file.c index b9b0b44..0e91ca6 100644 --- a/src/tasking/exec_from_file.c +++ b/src/tasking/exec_from_file.c @@ -49,13 +49,9 @@ void exec_from_file(const char* filename, const char* dirname) uint8_t* file; l9660_dir dir; - l9660_dir root_dir; l9660_status status; - status = l9660_fs_open_root(&root_dir, &global_fs); - debug_log("l9660_fs_open_root status: 0x%X\n", status); - - status = l9660_opendirat(&dir, &root_dir, dirname); + status = l9660_opendirat(&dir, root_dir, dirname); debug_log("l9660_opendirat status: 0x%X\n", status); //list_files(&dir); diff --git a/src/tasking/syscalls.c b/src/tasking/syscalls.c index 5aa7add..6db3768 100644 --- a/src/tasking/syscalls.c +++ b/src/tasking/syscalls.c @@ -6,6 +6,7 @@ #include "../include/string.h" #include "../include/screen.h" #include "../include/fat.h" +#include "../include/vfs.h" #define STDIN_FILENO 0 #define STDOUT_FILENO 1 @@ -157,7 +158,8 @@ int sys_read(TrapFrame *tf) { return -EBADF; } - return 0;//fat_fread(current->file_descriptors[tf->ebx], (void*)tf->ecx, tf->edx); + return vfs_read_file_length(current->file_descriptors[tf->ebx], (uint8_t*)tf->ecx, tf->edx); + //return 0;//fat_fread(current->file_descriptors[tf->ebx], (void*)tf->ecx, tf->edx); } } @@ -208,7 +210,8 @@ int sys_open(TrapFrame *tf) //debug_log("\n====i: %X\n", i); if(current->file_descriptors[i] == NULL) { - current->file_descriptors[i] = malloc(sizeof(file_t)); // Kernel malloc + current->file_descriptors[i] = (l9660_file*)malloc(sizeof(l9660_file)); // Kernel malloc + l9660_openat(current->file_descriptors[i], root_dir, filename); //debug_log("\n====FILE_DESCRIPTOR: %X\n", i); int result = 0;//fat_fopen(current->file_descriptors[i], filename, mode_str); if (result < 0) {