summaryrefslogtreecommitdiff
path: root/src/kernel/vfs
diff options
context:
space:
mode:
Diffstat (limited to 'src/kernel/vfs')
-rw-r--r--src/kernel/vfs/initrd.cpp67
-rw-r--r--src/kernel/vfs/initrd.h22
-rw-r--r--src/kernel/vfs/vdir.cpp4
-rw-r--r--src/kernel/vfs/vdir.h2
-rw-r--r--src/kernel/vfs/vfile.cpp34
-rw-r--r--src/kernel/vfs/vfile.h23
6 files changed, 151 insertions, 1 deletions
diff --git a/src/kernel/vfs/initrd.cpp b/src/kernel/vfs/initrd.cpp
new file mode 100644
index 0000000..50aa95c
--- /dev/null
+++ b/src/kernel/vfs/initrd.cpp
@@ -0,0 +1,67 @@
+#include "initrd.h"
+#include "vfile.h"
+#include <ui/vt.h>
+
+int initrd_check(uint8_t *data) {
+ initrd_header *h = (initrd_header*)data;
+ if (h->magic == INITRD_MAGIC) return 0;
+ return -1;
+}
+
+int initrd_load(uint8_t *data, vdir *root) {
+ if (root == 0) return -1;
+
+ union {
+ uint8_t *c;
+ initrd_header *ih;
+ initrd_file_header *fh;
+ } curr;
+ curr.c = data;
+
+ if (curr.ih->magic != INITRD_MAGIC) return -2;
+
+ uint32_t files = curr.ih->files; curr.ih++;
+ for (unsigned i = 0; i < files; i++) {
+ initrd_file_header *h = curr.fh;
+ curr.fh++;
+ if (h->name_length == 0) continue;
+ char* path = (char*)curr.c;
+ curr.c += h->name_length + 1;
+ if (strlen(path) != (int)h->name_length) return -3;
+
+ node* parent = root;
+ // parse filename
+ char *member = path;
+ while (*path != 0 && parent != 0) {
+ if (*path == '/') {
+ if (member == path) {
+ member++;
+ path++;
+ } else {
+ *path = 0;
+ path++;
+ node* n = parent->get_child(member);
+ if (n == 0) {
+ n = new vdir(parent);
+ parent->add_child(member, n);
+ }
+ parent = n;
+ member = path;
+ }
+ } else {
+ path++;
+ }
+ }
+ if (parent == 0) return -5;
+ if (member == path) return -6; // filename ended by '/', bad.
+
+ // add file
+ if (h->file_length == 0) {
+ if (parent->add_child(member, new vdir(parent)) != 0) return -4;
+ } else {
+ if (parent->add_child(member, new vfile(parent, (char*)curr.c, h->file_length)) != 0) return -4;
+ curr.c += h->file_length;
+ }
+ }
+ return 0;
+}
diff --git a/src/kernel/vfs/initrd.h b/src/kernel/vfs/initrd.h
new file mode 100644
index 0000000..0cabccf
--- /dev/null
+++ b/src/kernel/vfs/initrd.h
@@ -0,0 +1,22 @@
+#ifndef DEF_VFS_INITRD_H
+#define DEF_VFS_INITRD_H
+
+#include "node.h"
+#include "vdir.h"
+
+struct initrd_header {
+ unsigned int magic; //For error checking
+ unsigned int files;
+};
+
+struct initrd_file_header {
+ unsigned int name_length;
+ unsigned int file_length;
+};
+
+#define INITRD_MAGIC 0x12379846
+
+int initrd_check(uint8_t *data); // returns 0 if initrd file, -1 otherwise
+int initrd_load(uint8_t *data, vdir *root);
+
+#endif
diff --git a/src/kernel/vfs/vdir.cpp b/src/kernel/vfs/vdir.cpp
index 299c0e7..00b67cb 100644
--- a/src/kernel/vfs/vdir.cpp
+++ b/src/kernel/vfs/vdir.cpp
@@ -10,6 +10,10 @@ vdir::vdir(node* parent) : node(parent, FT_DIR), children(8, 64) {
}
}
+vdir::~vdir() {
+ //TODO : free children
+}
+
node* vdir::get_child(char* name) {
for (unsigned i = 0; i < child_count; i++) {
vdir_child *c = children.at(i);
diff --git a/src/kernel/vfs/vdir.h b/src/kernel/vfs/vdir.h
index 7ac1b2a..366b3f1 100644
--- a/src/kernel/vfs/vdir.h
+++ b/src/kernel/vfs/vdir.h
@@ -20,7 +20,7 @@ class vdir : public node {
public:
vdir(node* parent);
- virtual ~vdir() {}
+ virtual ~vdir();
virtual int read(size_t offset, size_t len, char* buffer);
virtual int write(size_t offset, size_t len, char* buffer); // rename file
diff --git a/src/kernel/vfs/vfile.cpp b/src/kernel/vfs/vfile.cpp
new file mode 100644
index 0000000..ff57c9e
--- /dev/null
+++ b/src/kernel/vfs/vfile.cpp
@@ -0,0 +1,34 @@
+#include "vfile.h"
+
+vfile::vfile(node* parent, char* d, size_t s) : node(parent, FT_FILE) {
+ size = 0;
+ own_data = false;
+
+ data = d;
+ if (d != 0) size = s;
+}
+
+vfile::~vfile() {
+ if (own_data) kfree(data);
+}
+
+int vfile::read(size_t offset, size_t len, char* buffer) {
+ if (offset + len > size) len = size - offset;
+ memcpy(buffer, data + offset, len);
+ return len;
+}
+
+int vfile::write(size_t offset, size_t len, char* buffer) {
+ size_t new_size = MAX(offset + len, size);
+ if (!own_data || new_size > size) {
+ char* new_data = (char*)kmalloc(new_size);
+ memcpy(new_data, data, size);
+ if (new_size > size) memset(new_data + size, 0, new_size - size);
+ if (own_data) kfree(data);
+ data = new_data;
+ size = new_size;
+ own_data = true;
+ }
+ memcpy(data + offset, buffer, len);
+ return len;
+}
diff --git a/src/kernel/vfs/vfile.h b/src/kernel/vfs/vfile.h
new file mode 100644
index 0000000..9b68b68
--- /dev/null
+++ b/src/kernel/vfs/vfile.h
@@ -0,0 +1,23 @@
+#ifndef DEF_VFS_VFILE_H
+#define DEF_VFS_VFILE_H
+
+#include <lib/std.h>
+#include "node.h"
+
+class vfile : public node {
+ char *data;
+ size_t size;
+ bool own_data; // did WE malloc the content of data ?
+
+ public:
+ vfile(node* parent, char* data = 0, size_t size = 0);
+ virtual ~vfile();
+
+ // TODO: handle open(TRUNC)
+ virtual int read(size_t offset, size_t len, char* buffer);
+ virtual int write(size_t offset, size_t len, char* buffer);
+ virtual size_t get_size() { return size; }
+
+};
+
+#endif