From ae27f28d046cd879e00b36579c29572658fab100 Mon Sep 17 00:00:00 2001 From: Karina Date: Wed, 28 Jan 2026 06:29:41 +0400 Subject: [PATCH] feat: basic vfs --- common/bootinfo.h | 2 +- kernel/inc/fs/vfs.h | 5 +++ kernel/src/fs/cpio.c | 12 +++++-- kernel/src/fs/vfs.c | 38 ++++++++++++++++++++ kernel/src/kmain.c | 5 ++- kernel/src/shell/dbgcmd.c | 76 +++++++++++---------------------------- 6 files changed, 78 insertions(+), 60 deletions(-) diff --git a/common/bootinfo.h b/common/bootinfo.h index e097b57..83b123b 100644 --- a/common/bootinfo.h +++ b/common/bootinfo.h @@ -34,4 +34,4 @@ typedef struct { BI_Initramfs initramfs; } Bootinfo; -#define BOOTINFO_MAGIC 0x7E833055 // termOS \ No newline at end of file +#define BOOTINFO_MAGIC 0x7465726D // term \ No newline at end of file diff --git a/kernel/inc/fs/vfs.h b/kernel/inc/fs/vfs.h index cd0af68..179c19d 100644 --- a/kernel/inc/fs/vfs.h +++ b/kernel/inc/fs/vfs.h @@ -31,3 +31,8 @@ struct fs_node { struct fs_node* ptr; struct fs_node* next; }; + +void vfs_init(fs_node* root_node); +u64 vfs_read(fs_node* node, u64 offset, u64 size, u8* buff); +fs_node* vfs_open(const char* path); +void vfs_close(fs_node* node); \ No newline at end of file diff --git a/kernel/src/fs/cpio.c b/kernel/src/fs/cpio.c index ec11762..2ddeba4 100644 --- a/kernel/src/fs/cpio.c +++ b/kernel/src/fs/cpio.c @@ -52,6 +52,7 @@ u64 hex_to_u64(const char* s, i32 len) { u64 cpio_read(fs_node* node, u64 offset, u64 size, u8* buff) { if (offset > node->len) return 0; // EOF if ((offset + size) > node->len) size = node->len - offset; + memcpy(buff, (char*)node->impl_data + offset, size); return size; } @@ -67,6 +68,8 @@ fs_node* cpio_mount(void* base, u64 size) { root->flags = FS_DIR; root->ops = &cpio_ops; + fs_node* tail = nullptr; + while (ptr < end) { cpio_header* header = (cpio_header*)ptr; @@ -84,7 +87,7 @@ fs_node* cpio_mount(void* base, u64 size) { fs_node* new_node = malloc(sizeof(fs_node)); if (!new_node) panic("CPIO: Failed to malloc for new node!"); memset(new_node, 0, sizeof(fs_node)); - strcpy(new_node->name, filename); + strncpy(new_node->name, filename, sizeof(new_node->name) - 1); new_node->len = filesize; new_node->inode = hex_to_u64(header->c_ino, 8); @@ -95,7 +98,12 @@ fs_node* cpio_mount(void* base, u64 size) { if ((mode & 0xF000) == 0x4000) new_node->flags = FS_DIR; else new_node->flags = FS_FILE; - kprintf("^bCPIO^!: Found file '^y%s^!' (size ^y%d^!) at ^y%x^!\n", filename, filesize, &file_content); + if (root->ptr == nullptr) root->ptr = new_node; + else if (tail) tail->next = new_node; + + tail = new_node; + + kprintf("^bCPIO^!: Found file '^y%s^!' (size ^y%d^!) at ^y%x^!\n", filename, filesize, file_content); u64 data_len = ALIGN4(filesize); ptr += offset_to_data + data_len; diff --git a/kernel/src/fs/vfs.c b/kernel/src/fs/vfs.c index 41242e3..7cf08e9 100644 --- a/kernel/src/fs/vfs.c +++ b/kernel/src/fs/vfs.c @@ -1,3 +1,41 @@ // SPDX-License-Identifier: GPL-3.0-or-later // Copyright (c) 2026 0xKarinyash +#include +#include + +fs_node* fs_root = nullptr; + +void vfs_init(fs_node* root_node) { + fs_root = root_node; +} + +u64 vfs_read(fs_node* node, u64 offset, u64 size, u8* buff) { + if (!node) return 0; + if (!node->ops->read) return 0; + + return node->ops->read(node, offset, size, buff); +} + +fs_node* vfs_open(const char* path) { + if (!fs_root) return nullptr; + if (strcmp(path, "/") == 0) return fs_root; + + const char* search_name = path; + if (path[0] == '/') search_name++; + + fs_node* curr = fs_root->ptr; + while (curr != nullptr) { + if (strcmp(curr->name, search_name) == 0) { + if (curr->ops->open) curr->ops->open(curr); + return curr; + } + curr = curr->next; + } + + return nullptr; +} + +void vfs_close(fs_node* node) { + if (node && node->ops->close) node->ops->close(node); +} \ No newline at end of file diff --git a/kernel/src/kmain.c b/kernel/src/kmain.c index a557656..6a1236a 100644 --- a/kernel/src/kmain.c +++ b/kernel/src/kmain.c @@ -24,6 +24,7 @@ #include #include +#include #define FG_COLOR 0xffffff #define BG_COLOR 0x111111 @@ -60,7 +61,9 @@ void kmain(Bootinfo* info) { info = (Bootinfo*)PHYS_TO_HHDM((u64)info); - cpio_mount(PHYS_TO_HHDM(info->initramfs.addr), info->initramfs.size); + fs_node* root = cpio_mount(PHYS_TO_HHDM(info->initramfs.addr), info->initramfs.size); + vfs_init(root); + kprintf("VFS initialized"); u32 *fb = (u32*)info->framebuffer.base; if (!fb) return serial_write("No framebuffer found!!"); diff --git a/kernel/src/shell/dbgcmd.c b/kernel/src/shell/dbgcmd.c index e34e1e8..120773f 100644 --- a/kernel/src/shell/dbgcmd.c +++ b/kernel/src/shell/dbgcmd.c @@ -3,65 +3,29 @@ // contents of this file will be changed CONSTANTLY // im just testing new stuff here -#include "core/panic.h" -#include "drivers/shitgui.h" -#include #include -#include #include - -#include - -SG_Window* win_a = nullptr; -SG_Window* win_b = nullptr; - -void a() { - SG_Point sizes = {0}; - sizes.x = win_a->ctx->width; - sizes.y = win_a->ctx->height; - - u64 t = 0; - while (true) { - for (u32 y = 0; y < sizes.y; y++) { - for (u32 x = 0; x < sizes.x; x++) { - win_a->ctx->fb[y * sizes.x + x] = (x * y) ^ t; - } - } - t++; - } -} -void b() { - SG_Point sizes = {0}; - sizes.x = win_b->ctx->width; - sizes.y = win_b->ctx->height; - - u64 t = 0; - while (true) { - for (u32 y = 0; y < sizes.y; y++) { - for (u32 x = 0; x < sizes.x; x++) { - win_b->ctx->fb[y * sizes.x + x] = (x ^ y) ^ t; - } - } - t++; - } -} +#include +#include u64 debug() { - kprintf("scheduler test\n"); - SG_Point sizes = {0}; - sizes.x = 256; - sizes.y = 256; - SG_Point pos_a = {0}; - pos_a.x = 100; - pos_a.y = 40; - SG_Point pos_b = {0}; - pos_b.x = 512; - pos_b.y = 40; - win_a = create_window("A", &sizes, &pos_a); - if (!win_a) panic("No win a"); - win_b = create_window("B", &sizes, &pos_b); - if (!win_b) panic("No win b"); - sched_spawn(a); - sched_spawn(b); + kprintf("Debug: Trying to open /test1.txt...\n"); + + fs_node* file = vfs_open("/test1.txt"); + + if (file == nullptr) { + kprintf("Error: File not found! Check initrd or filename.\n"); + return 1; + } + + kprintf("Success!\n"); + kprintf("\tFilename: %s\n", file->name); + kprintf("\tSize: %d bytes\n", file->len); + + char buf[64]; + memset(buf, 0, 64); + vfs_read(file, 0, 63, (u8*)buf); + kprintf("\tContent: %s\n", buf); + return 0; } \ No newline at end of file