devfs: add console to devfs

This commit is contained in:
Michael Shaw 2024-02-12 23:11:10 -05:00
commit 3b247ad5e9
4 changed files with 61 additions and 11 deletions

View file

@ -6,6 +6,12 @@
#include <dev/console.h> #include <dev/console.h>
#include <dev/serial.h> #include <dev/serial.h>
#include <fs/vfs.h>
#include <fs/devfs.h>
ssize_t console_write_dev(void *dev, void *buf, size_t offset, size_t count);
ssize_t console_read_dev(void *dev, void *buf, size_t offset, size_t count);
static struct { static struct {
int rows; int rows;
int cols; int cols;
@ -20,6 +26,11 @@ static struct {
size_t l_resize_cols; size_t l_resize_cols;
} console; } console;
static struct devfs_dev_ops console_dev_ops = {
.read = (devfs_dev_ops_read)console_read_dev,
.write = (devfs_dev_ops_write)console_write_dev,
};
static bool is_printable(char c) static bool is_printable(char c)
{ {
return (c >= 0x20 && c <= 0x7E); return (c >= 0x20 && c <= 0x7E);
@ -30,6 +41,9 @@ void console_init()
memset(console.line, 0, 512); memset(console.line, 0, 512);
console.resizemode = 0; console.resizemode = 0;
console.l_line = 0; console.l_line = 0;
devfs_insert(NULL, "tty", VFS_VNO_CHARDEV, &console_dev_ops);
kprintf(LOG_SUCCESS "Console ready\n"); kprintf(LOG_SUCCESS "Console ready\n");
} }
@ -149,8 +163,18 @@ ssize_t console_write(const char *buf, size_t count)
ssize_t console_read(char *buf, size_t count) ssize_t console_read(char *buf, size_t count)
{ {
if(console.l_line == 0) if (console.l_line == 0)
return -EAGAIN; return -EAGAIN;
return 0; return 0;
} }
ssize_t console_write_dev(void *dev, void *buf, size_t offset, size_t count)
{
return console_write(buf, count);
}
size_t console_read_dev(void *dev, void *buf, size_t offset, size_t count)
{
return console_read(buf, count);
}

View file

@ -3,17 +3,28 @@
#include <kernel/slab.h> #include <kernel/slab.h>
#include <kernel/block.h> #include <kernel/block.h>
#include <fs/vfs.h> #include <fs/vfs.h>
#include <fs/devfs.h>
struct fs *devfs; static struct fs *devfs;
static struct devfs_dev_ops block_dev_ops = {
.read = (devfs_dev_ops_read)block_read,
.write = (devfs_dev_ops_write)block_write,
};
static int devfs_read(struct vnode *vnode, void *buf, size_t offset, size_t size) static int devfs_read(struct vnode *vnode, void *buf, size_t offset, size_t size)
{ {
return 0; if (!vnode->priv_data)
return -EINVAL;
struct devfs_dev_ops *ops = vnode->priv_data;
return ops->read(vnode->priv_data, buf, offset, size);
} }
static int devfs_write(struct vnode *vnode, void *buf, size_t offset, size_t size) static int devfs_write(struct vnode *vnode, void *buf, size_t offset, size_t size)
{ {
return 0; if (!vnode->priv_data)
return -EINVAL;
struct devfs_dev_ops *ops = vnode->priv_data;
return ops->write(vnode->priv_data, buf, offset, size);
} }
/* devfs does not support unlink */ /* devfs does not support unlink */
@ -38,11 +49,14 @@ static int devfs_open_dir(struct fs *fs, struct vnode *out)
return -EINVAL; return -EINVAL;
} }
int devfs_insert(struct vnode *dir, const char *name, uint32_t flags) int devfs_insert(struct vnode *dir, const char *name, uint32_t flags, struct devfs_dev_ops *ops)
{ {
if (!dir || !name) if (!name || !ops)
return -EINVAL; return -EINVAL;
if (!dir)
dir = devfs->root;
struct vnode *dev_vnode = vfs_create_vno(); struct vnode *dev_vnode = vfs_create_vno();
if (!dev_vnode) if (!dev_vnode)
return -ENOMEM; return -ENOMEM;
@ -50,6 +64,7 @@ int devfs_insert(struct vnode *dir, const char *name, uint32_t flags)
dev_vnode->fs = dir->fs; dev_vnode->fs = dir->fs;
dev_vnode->flags = flags; dev_vnode->flags = flags;
dev_vnode->no_free = true; dev_vnode->no_free = true;
dev_vnode->priv_data = ops;
spinlock_acquire(&dir->lock); spinlock_acquire(&dir->lock);
@ -86,7 +101,7 @@ struct fs *devfs_init()
int res = vfs_mount(devfs, "dev"); int res = vfs_mount(devfs, "dev");
if(res < 0) { if (res < 0) {
kprintf(LOG_ERROR "devfs: failed to mount devfs: %s\n", strerror(-res)); kprintf(LOG_ERROR "devfs: failed to mount devfs: %s\n", strerror(-res));
goto out; goto out;
} }
@ -99,7 +114,7 @@ struct fs *devfs_init()
if (n > 0) { if (n > 0) {
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
devfs_insert(devfs->root, bdevs[i]->name, VFS_VNO_BLKDEV); devfs_insert(devfs->root, bdevs[i]->name, VFS_VNO_BLKDEV, &block_dev_ops);
} }
} }

View file

@ -2,6 +2,18 @@
#ifndef _DEVFS_H_ #ifndef _DEVFS_H_
#define _DEVFS_H_ #define _DEVFS_H_
#include <kernel/common.h>
typedef ssize_t (*devfs_dev_ops_write)(void *dev, void *buf, size_t offset, size_t size);
typedef ssize_t (*devfs_dev_ops_read)(void *dev, void *buf, size_t offset, size_t size);
struct vnode;
struct devfs_dev_ops {
devfs_dev_ops_write write;
devfs_dev_ops_read read;
};
struct fs *devfs_init(); struct fs *devfs_init();
int devfs_insert(struct vnode *dir, const char *name, uint32_t flags, struct devfs_dev_ops *ops);
#endif /* _DEVFS_H_ */ #endif /* _DEVFS_H_ */

View file

@ -53,7 +53,6 @@ void prompt()
kprintf(COL_YELLOW_BLACK "vbos # " COL_RESET); kprintf(COL_YELLOW_BLACK "vbos # " COL_RESET);
} }
void panic() void panic()
{ {
kprintf(LOG_ERROR "KERNEL PANIC\n"); kprintf(LOG_ERROR "KERNEL PANIC\n");
@ -186,8 +185,6 @@ void kmain()
_pic_init(); _pic_init();
apic_init(); apic_init();
console_init();
for (unsigned i = 0; i < smp_resp->cpu_count; i++) { for (unsigned i = 0; i < smp_resp->cpu_count; i++) {
struct limine_smp_info *info = smp_resp->cpus[i]; struct limine_smp_info *info = smp_resp->cpus[i];
@ -201,6 +198,8 @@ void kmain()
for (unsigned i = 0; i < 100000; i++) for (unsigned i = 0; i < 100000; i++)
; ;
console_init();
kprintf("\n"); kprintf("\n");
prompt(); prompt();
load_stack_and_park(ptr, ptr); load_stack_and_park(ptr, ptr);