more vfs stuff

This commit is contained in:
apio 2022-10-08 21:35:19 +02:00
parent f3d7e220ac
commit b38c52f8c7
4 changed files with 45 additions and 10 deletions

View File

@ -12,6 +12,7 @@ namespace VFS
{ {
char name[64]; char name[64];
uint64_t inode; uint64_t inode;
uint64_t length;
node_read read_func; node_read read_func;
node_finddir find_func; node_finddir find_func;
}; };

View File

@ -1,24 +1,54 @@
#define MODULE "vfs"
#include "fs/VFS.h" #include "fs/VFS.h"
#include "log/Log.h"
static VFS::Node* vfs_root; static VFS::Node* vfs_root;
int32_t VFS::read(Node* node, uint32_t offset, uint32_t length, char* buffer) int32_t VFS::read(Node* node, uint32_t offset, uint32_t length, char* buffer)
{ {
if (!node) return -1; if (!node)
if (!node->read_func) return -1; {
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); return node->read_func(node, offset, length, buffer);
} }
VFS::Node* VFS::open(const char* filename) VFS::Node* VFS::open(const char* filename)
{ {
if (!vfs_root) return 0; if (!vfs_root)
if (!vfs_root->find_func) return 0; {
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); return vfs_root->find_func(vfs_root, filename);
} }
void VFS::mount_root(Node* root) 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; vfs_root = root;
} }

View File

@ -152,6 +152,7 @@ void initrd_scan()
VFS::Node& node = nodes[i]; VFS::Node& node = nodes[i];
node.inode = i; node.inode = i;
node.read_func = initrd_read; node.read_func = initrd_read;
node.length = files[i].size;
strncpy(node.name, files[i].name, sizeof(node.name)); 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("physical base at %lx, size %lx, mapped to %p", bootboot.initrd_ptr, bootboot.initrd_size, initrd_base);
kdbgln("total blocks: %ld", get_total_blocks()); kdbgln("total blocks: %ld", get_total_blocks());
initrd_scan(); initrd_scan();
initrd_root.length = 0;
initrd_root.inode = (uint64_t)-1; initrd_root.inode = (uint64_t)-1;
strncpy(initrd_root.name, "initrd", 7); strncpy(initrd_root.name, "initrd", 7);
initrd_root.find_func = initrd_scan_root; initrd_root.find_func = initrd_scan_root;
VFS::mount_root(&initrd_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); kdbgln("mounted initrd at VFS root, total files: %d", total_files);
initrd_initialized = true; initrd_initialized = true;
} }

View File

@ -162,17 +162,17 @@ extern "C" void _start()
kinfoln("Trying VFS"); 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"); } if (!node) { kerrorln("Unable to find file in VFS"); }
else else
{ {
kinfoln("Found '%s'", node->name); kinfoln("Found '%s'", node->name);
char buffer[256]; char* buffer = (char*)kmalloc(node->length + 1);
kinfoln("Reading 255 bytes at offset 128..."); buffer[node->length] = 0;
int nwritten = VFS::read(node, 128, 255, buffer); int nread = VFS::read(node, 0, (uint32_t)node->length, buffer);
kinfoln("Read a total of %d bytes", nwritten); kdbgln("Read %d bytes", nread);
buffer[255] = 0;
kinfoln("Read: %s", buffer); kinfoln("Read: %s", buffer);
kfree(buffer);
} }
PCI::scan([](PCI::Device& dev) { PCI::scan([](PCI::Device& dev) {