aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Borner2020-04-30 20:27:06 +0200
committerMarvin Borner2020-04-30 20:27:06 +0200
commit6c5e31b1e5534748f89233cdebf778247b040cbc (patch)
treed85b9c4905ad7c6d56c29133bca9b9cc2eb28159
parent8b52d7698e4a9a5abaf730c7da1c4f865cb78f8e (diff)
Started vfs
-rwxr-xr-xrun1
-rw-r--r--src/kernel/fs/ext2.c92
-rw-r--r--src/kernel/fs/vfs.c49
-rw-r--r--src/kernel/fs/vfs.h63
-rw-r--r--src/kernel/kernel.c2
-rw-r--r--src/userspace/libgui/init.c6
-rw-r--r--src/userspace/programs/init.c3
-rw-r--r--src/userspace/programs/sh.c4
8 files changed, 214 insertions, 6 deletions
diff --git a/run b/run
index e9e9b6c..8a28d35 100755
--- a/run
+++ b/run
@@ -121,6 +121,7 @@ make_build() {
mkdir -p ./mnt/etc/
mkdir -p ./mnt/usr/
mkdir -p ./mnt/bin/
+ mkdir -p ./mnt/dev/
cp ./build/res/font.bin ./mnt/bin/font
cp ./build/user/* ./mnt/bin/
echo "Hello world, ext2!" | tee -a ./mnt/etc/test
diff --git a/src/kernel/fs/ext2.c b/src/kernel/fs/ext2.c
index 5fb0158..1d404f4 100644
--- a/src/kernel/fs/ext2.c
+++ b/src/kernel/fs/ext2.c
@@ -3,6 +3,7 @@
#include <stdbool.h>
#include <kernel/fs/ata.h>
#include <kernel/fs/ext2.h>
+#include <kernel/fs/vfs.h>
#include <kernel/system.h>
#include <kernel/memory/alloc.h>
#include <kernel/lib/lib.h>
@@ -251,4 +252,95 @@ uint8_t *read_file(char *path)
warn("File not found");
return NULL;
}
+}
+
+void ext2_vfs_open(struct fs_node *node)
+{
+ node->inode = ext2_look_up_path(node->name);
+
+ if (node->inode != 0) {
+ struct ext2_file *file = kmalloc(sizeof *file);
+ ext2_open_inode(node->inode, file);
+
+ node->impl = file;
+
+ // TODO: More file metadata
+ }
+}
+
+void ext2_vfs_close(struct fs_node *node)
+{
+ kfree(node->impl);
+}
+
+uint32_t ext2_vfs_read(struct fs_node *node, size_t offset, size_t size, char *buf)
+{
+ if (offset != ((struct ext2_file *)node->impl)->pos) {
+ panic("Seeking is currently unsupported for Ext2 files\n");
+ return 0;
+ }
+
+ return (uint32_t)ext2_read(node->impl, (uint8_t *)buf, size);
+}
+
+uint32_t ext2_vfs_write(struct fs_node *node, size_t offset, size_t size, char *buf)
+{
+ panic("Writing to Ext2 is currently unsupported\n");
+
+ return 0;
+}
+
+struct dirent *ext2_vfs_read_dir(struct fs_node *node, size_t index)
+{
+ struct ext2_dirent ext2_dir;
+
+ if (ext2_next_dirent(node->impl, &ext2_dir)) {
+ struct dirent *dir = kmalloc(sizeof *dir);
+
+ dir->inode = ext2_dir.inode_num;
+ strcpy(dir->name, (char *)ext2_dir.name);
+
+ return dir;
+ } else {
+ return NULL;
+ }
+}
+
+struct fs_node *ext2_vfs_find_dir(struct fs_node *node, char *name)
+{
+ uint32_t inode = ext2_find_in_dir(node->inode, name);
+ if (inode == 0) {
+ return NULL;
+ } else {
+ struct fs_node *found = kmalloc(sizeof *found);
+ found->inode = inode;
+
+ return found;
+ }
+}
+
+void ext2_mount(struct fs_node *mountpoint)
+{
+ assert(mountpoint->node_ptr == NULL && (mountpoint->type & MOUNTPOINT_NODE) == 0);
+ assert((mountpoint->type & DIR_NODE) != 0);
+
+ struct fs_node *ext2_root = (struct fs_node *)kmalloc(sizeof(struct fs_node));
+ ext2_root->name[0] = '\0';
+ ext2_root->permissions = 0;
+ ext2_root->uid = 0;
+ ext2_root->gid = 0;
+ ext2_root->inode = ROOT_INODE;
+ ext2_root->length = 0;
+ ext2_root->type = DIR_NODE;
+ ext2_root->read = ext2_vfs_read;
+ ext2_root->write = ext2_vfs_write;
+ ext2_root->open = ext2_vfs_open;
+ ext2_root->close = ext2_vfs_close;
+ ext2_root->read_dir = ext2_vfs_read_dir;
+ ext2_root->find_dir = ext2_vfs_find_dir;
+ ext2_root->node_ptr = NULL;
+ ext2_root->impl = NULL;
+
+ mountpoint->type |= MOUNTPOINT_NODE;
+ mountpoint->node_ptr = ext2_root;
} \ No newline at end of file
diff --git a/src/kernel/fs/vfs.c b/src/kernel/fs/vfs.c
new file mode 100644
index 0000000..98ec6fb
--- /dev/null
+++ b/src/kernel/fs/vfs.c
@@ -0,0 +1,49 @@
+#include <stdint.h>
+#include <stddef.h>
+#include <kernel/fs/vfs.h>
+
+struct fs_node *fs_root = NULL;
+
+uint32_t fs_read(struct fs_node *node, uint32_t offset, uint32_t size, char *buf)
+{
+ if (node->read != NULL)
+ return node->read(node, offset, size, buf);
+ else
+ return 0;
+}
+
+uint32_t fs_write(struct fs_node *node, uint32_t offset, uint32_t size, char *buf)
+{
+ if (node->write != NULL)
+ return node->write(node, offset, size, buf);
+ else
+ return 0;
+}
+
+void fs_open(struct fs_node *node)
+{
+ if (node->open != NULL)
+ node->open(node);
+}
+
+void fs_close(struct fs_node *node)
+{
+ if (node->close != NULL)
+ node->close(node);
+}
+
+struct dirent *fs_read_directory(struct fs_node *node, uint32_t index)
+{
+ if ((node->type & DIR_NODE) != 0 && node->find_dir != NULL)
+ return node->read_dir(node, index);
+ else
+ return (struct dirent *)NULL;
+}
+
+struct fs_node *fs_find_directory(struct fs_node *node, char *name)
+{
+ if ((node->type & DIR_NODE) != 0 && node->find_dir != NULL)
+ return node->find_dir(node, name);
+ else
+ return (struct fs_node *)NULL;
+} \ No newline at end of file
diff --git a/src/kernel/fs/vfs.h b/src/kernel/fs/vfs.h
new file mode 100644
index 0000000..fe897cc
--- /dev/null
+++ b/src/kernel/fs/vfs.h
@@ -0,0 +1,63 @@
+#ifndef MELVIX_VFS_H
+#define MELVIX_VFS_H
+
+#include <stdint.h>
+
+#define MAX_NAME_LENGTH 128
+
+enum node_type {
+ FILE_NODE = 1,
+ DIR_NODE,
+ CHAR_DEV_NODE,
+ BLOCK_DEV_NODE,
+ PIPE_NODE,
+ SYMLINK_NODE,
+ MOUNTPOINT_NODE = 8
+};
+
+struct fs_node;
+struct dirent;
+
+typedef uint32_t (*read)(struct fs_node *, uint32_t, uint32_t, char *);
+typedef uint32_t (*write)(struct fs_node *, uint32_t, uint32_t, char *);
+typedef void (*open)(struct fs_node *);
+typedef void (*close)(struct fs_node *);
+typedef struct dirent *(*read_dir)(struct fs_node *, uint32_t);
+typedef struct fs_node *(*find_dir)(struct fs_node *, char *);
+
+struct fs_node {
+ char name[MAX_NAME_LENGTH];
+ uint32_t length;
+ uint32_t inode;
+ uint32_t permissions;
+ uint32_t uid;
+ uint32_t gid;
+ enum node_type type;
+
+ struct fs_node *node_ptr;
+
+ void *impl;
+
+ read read;
+ write write;
+ open open;
+ close close;
+ read_dir read_dir;
+ find_dir find_dir;
+};
+
+struct dirent {
+ char name[MAX_NAME_LENGTH];
+ uint32_t inode;
+};
+
+extern struct fs_node *fs_root;
+
+uint32_t fs_read(struct fs_node *node, uint32_t offset, uint32_t size, char *buf);
+uint32_t fs_write(struct fs_node *node, uint32_t offset, uint32_t size, char *buf);
+void fs_open(struct fs_node *node);
+void fs_close(struct fs_node *node);
+struct dirent *fs_read_directory(struct fs_node *node, uint32_t index);
+struct fs_node *fs_find_directory(struct fs_node *node, char *name);
+
+#endif \ No newline at end of file
diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c
index c5b063a..3f506d9 100644
--- a/src/kernel/kernel.c
+++ b/src/kernel/kernel.c
@@ -16,6 +16,7 @@
#include <kernel/lib/stdio.h>
#include <kernel/fs/ata.h>
#include <kernel/fs/ext2.h>
+#include <kernel/fs/vfs.h>
#include <kernel/cmos/rtc.h>
#include <kernel/memory/alloc.h>
@@ -61,6 +62,7 @@ void kernel_main(uint32_t magic, uint32_t multiboot_address, uint32_t esp)
ata_init();
ext2_init_fs();
+ ext2_mount(fs_root);
load_binaries();
set_optimal_resolution();
diff --git a/src/userspace/libgui/init.c b/src/userspace/libgui/init.c
index 04d7ac3..ecc65d2 100644
--- a/src/userspace/libgui/init.c
+++ b/src/userspace/libgui/init.c
@@ -20,7 +20,7 @@ void gui_init()
// TODO: Why tf is the kheap magic stored in the first few bytes?!
fb = (pointers->mode_info->framebuffer << 16);
- gui_screen_clear();
- printf("%dx%dx%d\n", vbe_width, vbe_height, vbe_bpl << 3);
- printf("0x%x\n", fb);
+ /* gui_screen_clear(); */
+ /* printf("%dx%dx%d\n", vbe_width, vbe_height, vbe_bpl << 3); */
+ /* printf("0x%x\n", fb); */
} \ No newline at end of file
diff --git a/src/userspace/programs/init.c b/src/userspace/programs/init.c
index 3c1d51f..4127e19 100644
--- a/src/userspace/programs/init.c
+++ b/src/userspace/programs/init.c
@@ -5,8 +5,9 @@
void main()
{
- printf("Initializing userspace...\n");
gui_init();
+ gui_screen_clear();
+ printf("Initializing userspace...\n");
syscall_exec("/bin/sh");
while (1) {
diff --git a/src/userspace/programs/sh.c b/src/userspace/programs/sh.c
index 8da493e..6913e05 100644
--- a/src/userspace/programs/sh.c
+++ b/src/userspace/programs/sh.c
@@ -1,9 +1,9 @@
-#include <syscall.h>
#include <stdio.h>
+#include <syscall.h>
+#include <gui.h>
void main()
{
- printf("Test for printf! %d\n", 42);
printf("[~] ");
while (1) {