aboutsummaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
authorFranklin Wei <frankhwei536@gmail.com>2015-03-02 21:37:44 -0500
committerFranklin Wei <frankhwei536@gmail.com>2015-03-02 21:37:44 -0500
commita0721b4caa79b81e021678089fde0b8467daa1cd (patch)
tree56af15c29704cc4c6738345ccd6c9db3782fd8d1 /kernel
parent46e94578765d3d2f03b83421d752e5dfc3e56d34 (diff)
downloadkappa-master.zip
kappa-master.tar.gz
kappa-master.tar.bz2
kappa-master.tar.xz
Virtual filesystem WIP!HEADmaster
Diffstat (limited to 'kernel')
-rw-r--r--kernel/heap.c5
-rw-r--r--kernel/main.c33
-rw-r--r--kernel/vfs.c91
3 files changed, 129 insertions, 0 deletions
diff --git a/kernel/heap.c b/kernel/heap.c
index bb18fb4..bfe3302 100644
--- a/kernel/heap.c
+++ b/kernel/heap.c
@@ -44,3 +44,8 @@ void *kmalloc_ap(size_t sz, void **phys)
{
return kmalloc_int(sz, 1, phys);
}
+
+void kmalloc_set_addr(uint32_t addr)
+{
+ kmalloc_addr = addr;
+}
diff --git a/kernel/main.c b/kernel/main.c
index 540bdcc..e05eb99 100644
--- a/kernel/main.c
+++ b/kernel/main.c
@@ -6,6 +6,7 @@
#include "gfx.h"
#include "gfx_font.h"
#include "idt.h"
+#include "initrd.h"
#include "isr.h"
#include "irq.h"
#include "log.h"
@@ -17,6 +18,7 @@
#include "fpu.h"
#include "timer.h"
#include "version.h"
+#include "vfs.h"
#include "vgatext.h"
void gpf(struct regs_t *regs)
@@ -104,6 +106,9 @@ bool boot(struct multiboot_info_t *hdr, uint32_t magic)
{
fpu_enable();
+ /* load initrd if any, this will also prevent modules from being clobbered by kmalloc */
+ initrd_init(hdr);
+
/* this should go to port e9, which is the Bochs debug port */
printf("Testing early I/O\n");
@@ -272,9 +277,37 @@ static void keyhandler(const struct ps2_keyevent *ev)
}
}
+void fs_test(void)
+{
+ struct vfs_node *fs = vfs_init();
+ vfs_mkdir(fs, "bin");
+ vfs_mkdir(fs, "etc");
+ vfs_mkdir(fs, "bin");
+ vfs_creat(fs, "file1.txt");
+ vfs_creat(fs, "file2.txt");
+ vfs_creat(fs, "file3.txt");
+ vfs_creat(fs, "file4.txt");
+ vfs_creat(fs, "main.c");
+ DIR *file = vfs_opendir(fs);
+ struct dirent *dir;
+ printf("=== File listing of / ===\n");
+ do {
+ dir = vfs_readdir(file);
+ if(!dir)
+ break;
+ printf("%s", dir->d_name);
+ if(dir->d_type == DT_DIR)
+ putchar('/');
+ putchar('\n');
+ } while(dir);
+}
+
void main(struct multiboot_info_t *hdr, uint32_t magic)
{
bool gfx_status = boot(hdr, magic);
+
+ fs_test();
+
gfx_set_foreground(0x80FF80);
printf("Hello, world!\n");
gfx_set_foreground(GFX_WHITE);
diff --git a/kernel/vfs.c b/kernel/vfs.c
new file mode 100644
index 0000000..cc6152f
--- /dev/null
+++ b/kernel/vfs.c
@@ -0,0 +1,91 @@
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "heap.h"
+#include "vfs.h"
+
+`static struct vfs_node *new_node(void)
+{
+ struct vfs_node *ret = kmalloc(sizeof(struct vfs_node));
+ memset(ret, 0, sizeof(struct vfs_node));
+ return ret;
+}
+
+struct vfs_node *vfs_init(void)
+{
+ struct vfs_node *root = new_node();
+ root->flags = VFS_DIR;
+ root->name = malloc(1);
+ root->name[0] = '\0';
+
+ return root;
+}
+
+int vfs_mkdir(struct vfs_node *node, const char *name)
+{
+ if(!node)
+ return 0;
+ if((node->flags & VFS_TYPEMASK) != VFS_DIR)
+ return 0;
+ /* insert a new element at the beginning of the linked list of the directory's children */
+ struct vfs_node *newdir = new_node();
+ newdir->flags = VFS_DIR;
+ newdir->name = strdup(name);
+ newdir->parent = node;
+ newdir->next = node->child;
+
+ node->child = newdir;
+ return 1;
+}
+
+int vfs_creat(struct vfs_node *node, const char *name)
+{
+ if(!node)
+ return 0;
+ if((node->flags & VFS_TYPEMASK) != VFS_DIR)
+ return 0;
+ struct vfs_node *newfile = new_node();
+ newfile->flags = VFS_REG;
+ newfile->name = strdup(name);
+ newfile->parent = node;
+ newfile->next = node->child;
+
+ node->child = newfile;
+ return 1;
+}
+
+DIR *vfs_opendir(struct vfs_node *node)
+{
+ DIR *dirp = malloc(sizeof(DIR));
+ dirp->node = node->child;
+ return dirp;
+}
+
+struct dirent *vfs_readdir(DIR *dirp)
+{
+ if(!dirp->node)
+ return NULL;
+ static struct dirent ret;
+ memcpy(ret.d_name, dirp->node->name, MIN(strlen(dirp->node->name)+1, sizeof(ret.d_name)));
+ switch(dirp->node->flags & VFS_TYPEMASK)
+ {
+ case VFS_DIR:
+ ret.d_type = DT_DIR;
+ break;
+ case VFS_REG:
+ ret.d_type = DT_REG;
+ break;
+ /* fall through */
+ case VFS_SPEC:
+ default:
+ ret.d_type = DT_UNKNOWN;
+ }
+ dirp->node = dirp->node->next;
+ return &ret;
+}
+
+char *vfs_path(struct vfs_node *node)
+{
+ struct vfs_node *
+}