feat: basic vfs

This commit is contained in:
Karina
2026-01-28 06:29:41 +04:00
parent 0761153d24
commit ae27f28d04
6 changed files with 78 additions and 60 deletions
+1 -1
View File
@@ -34,4 +34,4 @@ typedef struct {
BI_Initramfs initramfs;
} Bootinfo;
#define BOOTINFO_MAGIC 0x7E833055 // termOS
#define BOOTINFO_MAGIC 0x7465726D // term
+5
View File
@@ -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);
+10 -2
View File
@@ -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;
+38
View File
@@ -1,3 +1,41 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (c) 2026 0xKarinyash
#include <fs/vfs.h>
#include <core/string.h>
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);
}
+4 -1
View File
@@ -24,6 +24,7 @@
#include <mm/heap.h>
#include <fs/cpio.h>
#include <fs/vfs.h>
#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!!");
+20 -56
View File
@@ -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 <drivers/timer.h>
#include <drivers/console.h>
#include <types.h>
#include <shell/dbgcmd.h>
#include <core/scheduler.h>
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 <fs/vfs.h>
#include <mm/memory.h>
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;
}