aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Borner2020-05-01 20:50:49 +0200
committerMarvin Borner2020-05-01 20:50:49 +0200
commit21106945c4a28d45334824c02f8b139d1db91c45 (patch)
treef215128167a17fa476ad32350c2b81fc4ebb0395
parentf0439a5dbfe2cfed3d629027d4b54b5f77f4e6de (diff)
Added devfs and basic standard devices
-rw-r--r--README.md9
-rwxr-xr-xrun7
-rw-r--r--src/kernel/fs/dev.c114
-rw-r--r--src/kernel/fs/dev.h25
-rw-r--r--src/kernel/fs/vfs.c1
-rw-r--r--src/kernel/fs/vfs.h1
-rw-r--r--src/kernel/kernel.c5
-rw-r--r--src/kernel/system.c5
8 files changed, 164 insertions, 3 deletions
diff --git a/README.md b/README.md
index a5cde22..8c88309 100644
--- a/README.md
+++ b/README.md
@@ -37,7 +37,14 @@
* Optional: Flash the built ISO to a CD drive using `sudo dd if=./iso/melvix.iso of=/dev/sdX bs=4M oflag=sync`
## Licenses
-Melvix is released under the MIT License and uses the following 3rd party applications (as stated in the regarding files):
+Melvix is released under the MIT License and uses parts of the following 3rd party projects:
+Resources:
* [Spleen font](https://github.com/fcambus/spleen) - [MIT License](https://github.com/fcambus/spleen/blob/5759e9abb130b89ba192edc5324b12ef07b7dad3/LICENSE)
* [bdf2c converter](https://github.com/pixelmatix/bdf2c) - [AGPL 3.0 License](https://github.com/pixelmatix/bdf2c/blob/b07deb7a484751b3e3fb6c952f6bc54b1b2950fd/AGPL-3.0.txt)
* [onedark.vim colorscheme](https://github.com/joshdick/onedark.vim/) - [MIT License](https://github.com/joshdick/onedark.vim/blob/fe035976117ba5c2481df3b2cad3bb0a8b045b9f/LICENSE)
+
+Knowledge:
+* [virtix - FS inspiration](https://github.com/16Bitt/virtix/) - [MIT License](https://github.com/16Bitt/virtix/blob/85a3c58f3d3b8932354e85a996a79c377139c201/LICENSE)
+* [studix - memory inspiration](https://github.com/orodley/studix) - [MIT License](https://github.com/orodley/studix/blob/d1b1d006010120551df58ff3faaf97484dfa9806/LICENSE)
+* [James Molloy's tutorials](http://jamesmolloy.co.uk/tutorial_html/)
+* [OSdev wiki](https://wiki.osdev.org) <- Very helpful!
diff --git a/run b/run
index 8a28d35..10aa8e2 100755
--- a/run
+++ b/run
@@ -121,7 +121,14 @@ make_build() {
mkdir -p ./mnt/etc/
mkdir -p ./mnt/usr/
mkdir -p ./mnt/bin/
+
+ # TODO: Device file touching should be in kernel
mkdir -p ./mnt/dev/
+ touch ./mnt/dev/stdin
+ touch ./mnt/dev/stdout
+ touch ./mnt/dev/stderr
+ touch ./mnt/dev/fb
+
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/dev.c b/src/kernel/fs/dev.c
new file mode 100644
index 0000000..968dcfe
--- /dev/null
+++ b/src/kernel/fs/dev.c
@@ -0,0 +1,114 @@
+#include <stdint.h>
+#include <stddef.h>
+#include <kernel/system.h>
+#include <kernel/fs/dev.h>
+#include <kernel/fs/vfs.h>
+#include <kernel/fs/ext2.h>
+#include <kernel/memory/alloc.h>
+#include <kernel/lib/stdlib.h>
+#include <kernel/lib/stdio.h>
+
+void dev_make(char *name, read read, write write)
+{
+ struct fs_node *dev_node = (struct fs_node *)kmalloc(sizeof(struct fs_node));
+ strcpy(dev_node->name, "/dev");
+ ext2_root->open(dev_node);
+
+ if (dev_node->inode == 0) {
+ warn("Can't make device, no path for /dev");
+ return;
+ }
+
+ ext2_root->close(dev_node);
+
+ char *path = (char *)kmalloc(strlen(name) + strlen("/dev/") + 2);
+ strcpy(path, "/dev/");
+ strcpy(&path[strlen("/dev/")], name);
+
+ // TODO: Touch dev files in the vfs instead of opening it via ext2
+ struct fs_node *node = (struct fs_node *)kmalloc(sizeof(struct fs_node));
+ strcpy(node->name, path);
+ ext2_root->open(node);
+ kfree(path);
+
+ if (node->inode == 0) {
+ warn("Couldn't resolve path");
+ return;
+ }
+
+ node->read = read;
+ node->write = write;
+
+ node->dev = (struct dev *)kmalloc(sizeof(struct dev));
+ node->dev->read = read;
+ node->dev->write = write;
+ node->dev->block_size = 512;
+}
+
+uint32_t dev_read(struct fs_node *dev, uint32_t offset, uint32_t size, char *buffer)
+{
+ if (dev->read == NULL) {
+ warn("Can't read from device");
+ return (uint32_t)-1;
+ }
+
+ return dev->read(dev, offset, size, buffer);
+}
+
+uint32_t dev_write(struct fs_node *dev, uint32_t offset, uint32_t size, char *buffer)
+{
+ if (dev->write == NULL) {
+ warn("Can't write to device");
+ return (uint32_t)-1;
+ }
+
+ return dev->write(dev, offset, size, buffer);
+}
+
+// Standard devices
+
+uint32_t stdin_read(struct fs_node *node, uint32_t offset, uint32_t size, uint8_t *buffer)
+{
+ // Do something
+ return 0;
+}
+
+void dev_stdin()
+{
+ dev_make("stdout", (read)stdin_read, NULL);
+ struct fs_node *node = (struct fs_node *)kmalloc(sizeof(struct fs_node));
+ strcpy(node->name, "/dev/stdin");
+ ext2_root->open(node);
+ node->dev->block_size = 0;
+ node->length = 0xFFFFFFFF;
+}
+
+uint32_t stdout_write(struct fs_node *node, uint32_t offset, uint32_t size, uint8_t *buffer)
+{
+ //putch(*buffer);
+ return 0;
+}
+
+void dev_stdout()
+{
+ dev_make("stdout", NULL, (write)stdout_write);
+ struct fs_node *node = (struct fs_node *)kmalloc(sizeof(struct fs_node));
+ strcpy(node->name, "/dev/stdout");
+ ext2_root->open(node);
+ node->dev->block_size = 0;
+}
+
+uint32_t stderr_write(struct fs_node *node, uint32_t offset, uint32_t size, uint8_t *buffer)
+{
+ //putch(*buffer);
+ return 0;
+}
+
+void dev_stderr()
+{
+ dev_make("stderr", NULL, (write)stderr_write);
+ struct fs_node *node = (struct fs_node *)kmalloc(sizeof(struct fs_node));
+ strcpy(node->name, "/dev/stderr");
+ ext2_root->open(node);
+ node->dev->block_size = 0;
+} \ No newline at end of file
diff --git a/src/kernel/fs/dev.h b/src/kernel/fs/dev.h
new file mode 100644
index 0000000..40263e6
--- /dev/null
+++ b/src/kernel/fs/dev.h
@@ -0,0 +1,25 @@
+#ifndef MELVIX_DEV_H
+#define MELVIX_DEV_H
+
+#include <stdint.h>
+#include <kernel/fs/vfs.h>
+
+struct dev {
+ char name[128];
+ uint32_t block_size;
+ read read;
+ write write;
+
+ uint8_t bus;
+ uint8_t drive;
+};
+
+void dev_make(char *name, read read, write write);
+uint32_t dev_read(struct fs_node *dev, uint32_t offset, uint32_t size, char *buffer);
+uint32_t dev_write(struct fs_node *dev, uint32_t offset, uint32_t size, char *buffer);
+
+void dev_stdout();
+void dev_stdin();
+void dev_stderr();
+
+#endif \ No newline at end of file
diff --git a/src/kernel/fs/vfs.c b/src/kernel/fs/vfs.c
index 68944c7..fd44f30 100644
--- a/src/kernel/fs/vfs.c
+++ b/src/kernel/fs/vfs.c
@@ -1,6 +1,7 @@
#include <stdint.h>
#include <stddef.h>
#include <kernel/fs/vfs.h>
+#include <kernel/lib/stdlib.h>
struct fs_node *fs_root = NULL;
diff --git a/src/kernel/fs/vfs.h b/src/kernel/fs/vfs.h
index 6a26c07..867fae2 100644
--- a/src/kernel/fs/vfs.h
+++ b/src/kernel/fs/vfs.h
@@ -34,6 +34,7 @@ struct fs_node {
uint32_t gid;
enum node_type type;
+ struct dev *dev;
struct fs_node *node_ptr;
void *impl;
diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c
index 08018c2..c2f0972 100644
--- a/src/kernel/kernel.c
+++ b/src/kernel/kernel.c
@@ -18,6 +18,7 @@
#include <kernel/fs/ata.h>
#include <kernel/fs/ext2.h>
#include <kernel/fs/vfs.h>
+#include <kernel/fs/dev.h>
#include <kernel/cmos/rtc.h>
#include <kernel/memory/alloc.h>
@@ -77,6 +78,10 @@ void kernel_main(uint32_t magic, uint32_t multiboot_address, uint32_t esp)
log("Content of /etc/test: %s", buf);
ext2_root->close(test);
+ dev_stdin();
+ dev_stdout();
+ dev_stderr();
+
syscalls_install();
kexec("/bin/init");
diff --git a/src/kernel/system.c b/src/kernel/system.c
index 37c81c4..103522f 100644
--- a/src/kernel/system.c
+++ b/src/kernel/system.c
@@ -65,8 +65,8 @@ const char *random_message[10] = { "Uh... Did I do that?",
void _panic(const char *f, const char *msg)
{
cli();
- _log(f, RED "PNC: %s - System halted!" RES, msg);
- printf("[%s] PNC: %s - System halted!\n\n", f, msg);
+ _log(f, RED "PNC: %s - Halting system!" RES, msg);
+ printf("[%s] PNC: %s - Halting system!\n\n", f, msg);
printf("> %s", random_message[get_time() % 10]);
halt_loop();
}
@@ -80,6 +80,7 @@ void _assert(const char *f, int x)
void halt_loop()
{
+ debug("Halted.");
cli();
loop:
hlt();