diff --git a/kernel/include/fs/VFS.h b/kernel/include/fs/VFS.h index 2ab19a1c..082dbe8f 100644 --- a/kernel/include/fs/VFS.h +++ b/kernel/include/fs/VFS.h @@ -12,6 +12,7 @@ namespace VFS { char name[64]; uint64_t inode; + uint64_t length; node_read read_func; node_finddir find_func; }; diff --git a/kernel/src/fs/VFS.cpp b/kernel/src/fs/VFS.cpp index e196b7ea..047cc645 100644 --- a/kernel/src/fs/VFS.cpp +++ b/kernel/src/fs/VFS.cpp @@ -1,24 +1,54 @@ +#define MODULE "vfs" + #include "fs/VFS.h" +#include "log/Log.h" static VFS::Node* vfs_root; int32_t VFS::read(Node* node, uint32_t offset, uint32_t length, char* buffer) { - if (!node) return -1; - if (!node->read_func) return -1; + if (!node) + { + kwarnln("read() failed: trying to read from nullptr"); + return -1; + } + if (!node->read_func) + { + kwarnln("read() failed: the chosen node doesn't support reading"); + return -1; + } return node->read_func(node, offset, length, buffer); } VFS::Node* VFS::open(const char* filename) { - if (!vfs_root) return 0; - if (!vfs_root->find_func) return 0; + if (!vfs_root) + { + kwarnln("open() failed: root not mounted"); + return 0; + } + if (!vfs_root->find_func) + { + kwarnln("open() failed: root doesn't support finding files"); + return 0; + } return vfs_root->find_func(vfs_root, filename); } void VFS::mount_root(Node* root) { + if (!root) + { + kwarnln("mount_root() failed: attempted to mount nullptr"); + return; + } + if (vfs_root) + { + kwarnln("mount_root() failed: root filesystem already mounted"); + return; + } + kinfoln("mounting node '%s' as vfs root", root->name); vfs_root = root; } \ No newline at end of file diff --git a/kernel/src/init/InitRD.cpp b/kernel/src/init/InitRD.cpp index 195efd93..3406f740 100644 --- a/kernel/src/init/InitRD.cpp +++ b/kernel/src/init/InitRD.cpp @@ -152,6 +152,7 @@ void initrd_scan() VFS::Node& node = nodes[i]; node.inode = i; node.read_func = initrd_read; + node.length = files[i].size; strncpy(node.name, files[i].name, sizeof(node.name)); } } @@ -163,10 +164,13 @@ void InitRD::init() kdbgln("physical base at %lx, size %lx, mapped to %p", bootboot.initrd_ptr, bootboot.initrd_size, initrd_base); kdbgln("total blocks: %ld", get_total_blocks()); initrd_scan(); + initrd_root.length = 0; initrd_root.inode = (uint64_t)-1; strncpy(initrd_root.name, "initrd", 7); initrd_root.find_func = initrd_scan_root; VFS::mount_root(&initrd_root); + VFS::mount_root(0); + VFS::mount_root(&initrd_root); kdbgln("mounted initrd at VFS root, total files: %d", total_files); initrd_initialized = true; } \ No newline at end of file diff --git a/kernel/src/main.cpp b/kernel/src/main.cpp index 1b55ac69..c26e715b 100644 --- a/kernel/src/main.cpp +++ b/kernel/src/main.cpp @@ -162,17 +162,17 @@ extern "C" void _start() kinfoln("Trying VFS"); - VFS::Node* node = VFS::open("sys/moon.sym"); + VFS::Node* node = VFS::open("sys/config"); if (!node) { kerrorln("Unable to find file in VFS"); } else { kinfoln("Found '%s'", node->name); - char buffer[256]; - kinfoln("Reading 255 bytes at offset 128..."); - int nwritten = VFS::read(node, 128, 255, buffer); - kinfoln("Read a total of %d bytes", nwritten); - buffer[255] = 0; + char* buffer = (char*)kmalloc(node->length + 1); + buffer[node->length] = 0; + int nread = VFS::read(node, 0, (uint32_t)node->length, buffer); + kdbgln("Read %d bytes", nread); kinfoln("Read: %s", buffer); + kfree(buffer); } PCI::scan([](PCI::Device& dev) {