Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7c7d052286 | |||
| 57d3962d5c | |||
| 1e4b96e6fb | |||
| 05ccdb3d0e | |||
| 402761ca80 | |||
| df3a785e43 | |||
| 4b17525d33 | |||
| 7833400893 | |||
| 6c2613ab96 | |||
| 1dde87423d | |||
| f177a8e65d | |||
| bba8fc7476 | |||
| b5a6c0bc97 | |||
| 474468ef04 | |||
| 7c8b46eeda | |||
| 9bbe42b95a | |||
| da4023412c | |||
| ef941d094b |
25 changed files with 526 additions and 61 deletions
1
Makefile
1
Makefile
|
|
@ -90,6 +90,7 @@ $(KERNEL): $(KOBJ) linker.ld
|
|||
.PHONY: clean
|
||||
clean:
|
||||
$(Q)$(MAKE) -f scripts/Makefile.build _clean obj=.
|
||||
$(Q)rm -rf $(INITRAMFS)
|
||||
$(Q)rm -rf $(IMG)
|
||||
$(Q)rm -rf iso_root
|
||||
$(Q)rm -rf os_root
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ void arch_early_init()
|
|||
cr4 |= (1 << 9) | (1 << 10);
|
||||
cr4_write(cr4);
|
||||
|
||||
tty_early.serial_ops = &tty_early_ops;
|
||||
tty_early.serial_ops = tty_early_ops;
|
||||
tty_early.priv = (void *)COM1;
|
||||
|
||||
tty_printf = &tty_early;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/device.h>
|
||||
#include <kernel/slab.h>
|
||||
|
||||
#include <drivers/serial.h>
|
||||
#include <drivers/pio.h>
|
||||
|
|
@ -30,11 +31,6 @@ void init_serial_port(int port)
|
|||
outb(port | 4, 0x0F); /* Enter normal operation */
|
||||
}
|
||||
|
||||
void *arch_serial_init()
|
||||
{
|
||||
return (void *)COM1;
|
||||
}
|
||||
|
||||
void arch_serial_write(struct tty *tty, char c)
|
||||
{
|
||||
/* Wait for line to be clear */
|
||||
|
|
@ -54,3 +50,23 @@ char arch_serial_read(struct tty *tty)
|
|||
|
||||
return inb(port);
|
||||
}
|
||||
|
||||
int serial_port_tty_init(struct tty **out)
|
||||
{
|
||||
init_serial_port(COM1);
|
||||
|
||||
*out = kalloc(sizeof(struct tty), ALLOC_KERN);
|
||||
|
||||
if (!(*out))
|
||||
return -ENOMEM;
|
||||
|
||||
(*out)[0].priv = (void *)COM1;
|
||||
(*out)[0].serial_ops.serial_getchar = arch_serial_read;
|
||||
(*out)[0].serial_ops.serial_putchar = arch_serial_write;
|
||||
|
||||
dev_t com1 = device_create((*out + 0), DEV_CLASS_TTY, "ttyS0");
|
||||
|
||||
(*out)[0].dev = com1;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,6 @@
|
|||
|
||||
#include <kernel/device.h>
|
||||
|
||||
void *arch_serial_init();
|
||||
int serial_port_tty_init(struct tty **out);
|
||||
|
||||
#endif /* _SERIAL_H_ */
|
||||
|
|
|
|||
|
|
@ -17,19 +17,19 @@ struct tty *tty_create(const char *name, void *priv)
|
|||
|
||||
tty->dev = tty_device;
|
||||
tty->priv = priv;
|
||||
tty->serial_ops = &arch_serial_ops;
|
||||
tty->serial_ops = arch_serial_ops;
|
||||
|
||||
return tty;
|
||||
}
|
||||
|
||||
void tty_putchar(struct tty *tty, char c)
|
||||
{
|
||||
tty->serial_ops->serial_putchar(tty, c);
|
||||
tty->serial_ops.serial_putchar(tty, c);
|
||||
}
|
||||
|
||||
char tty_getchar(struct tty *tty)
|
||||
{
|
||||
return tty->serial_ops->serial_getchar(tty);
|
||||
return tty->serial_ops.serial_getchar(tty);
|
||||
}
|
||||
|
||||
void tty_write(struct tty *tty, const char *s)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
# SPDX-License-Identifier: GPL-2.0-only
|
||||
obj-y += vfs.o
|
||||
obj-y += mount.o
|
||||
obj-y += inode.o
|
||||
obj-y += ramfs.o
|
||||
obj-y += devfs.o
|
||||
obj-y += initcpio.o
|
||||
|
|
|
|||
93
fs/vfs/devfs.c
Normal file
93
fs/vfs/devfs.c
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/block.h>
|
||||
#include <drivers/tty.h>
|
||||
|
||||
#include <fs/vfs.h>
|
||||
#include <fs/inode.h>
|
||||
#include <fs/ramfs.h>
|
||||
#include <fs/devfs.h>
|
||||
|
||||
#include <lib/errno.h>
|
||||
|
||||
static fs_t *f_devfs = NULL;
|
||||
|
||||
static ssize_t i_read_chr(inode_t *inode, char *buf, size_t size, size_t offset)
|
||||
{
|
||||
*buf = tty_getchar(inode->data);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static ssize_t i_write_chr(inode_t *inode, char *buf, size_t size, size_t offset)
|
||||
{
|
||||
for (size_t i = offset; i < size + offset; i++)
|
||||
tty_putchar(inode->data, buf[offset + i]);
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
static ssize_t i_read_blk(inode_t *inode, char *buf, size_t size, size_t offset)
|
||||
{
|
||||
return block_read(inode->data, buf, offset, size);
|
||||
}
|
||||
|
||||
static ssize_t i_write_blk(inode_t *inode, char *buf, size_t size, size_t offset)
|
||||
{
|
||||
return block_write(inode->data, buf, offset, size);
|
||||
}
|
||||
|
||||
static int devfs_insert_device(const char *name, u64 mode, dirent_t **out)
|
||||
{
|
||||
return vfs_creat(f_devfs->root_dirent, name, mode, 0, out);
|
||||
}
|
||||
|
||||
int devfs_insert_device_chr(const char *name, struct tty *device, dirent_t **out)
|
||||
{
|
||||
int res = devfs_insert_device(name, S_IFCHR, out);
|
||||
|
||||
if (res < 0)
|
||||
return res;
|
||||
|
||||
inode_t *ino = (*out)->inode;
|
||||
write_lock(&ino->rwlock);
|
||||
|
||||
ino->data = device;
|
||||
ino->i_ops.write = i_write_chr;
|
||||
ino->i_ops.read = i_read_chr;
|
||||
|
||||
write_unlock(&ino->rwlock);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
int devfs_insert_device_blk(const char *name, struct block_device *device, dirent_t **out)
|
||||
{
|
||||
int res = devfs_insert_device(name, S_IFBLK, out);
|
||||
|
||||
if (res < 0)
|
||||
return res;
|
||||
|
||||
inode_t *ino = (*out)->inode;
|
||||
write_lock(&ino->rwlock);
|
||||
|
||||
ino->data = device;
|
||||
ino->i_ops.write = i_write_blk;
|
||||
ino->i_ops.read = i_read_blk;
|
||||
|
||||
write_unlock(&ino->rwlock);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void devfs_init(void)
|
||||
{
|
||||
int res = ramfs_create(0, &f_devfs);
|
||||
if (res < 0)
|
||||
panic("failed to create devfs: %s\n", strerror(-res));
|
||||
|
||||
res = vfs_mount("/dev", f_devfs, 0);
|
||||
if (res < 0)
|
||||
panic("failed to mount /dev: %s\n", strerror(-res));
|
||||
|
||||
krdy("devfs ready\n");
|
||||
}
|
||||
|
|
@ -150,7 +150,7 @@ int cpio_read_and_init(const char *file, size_t len, fs_t **out)
|
|||
if (res < 0)
|
||||
return res;
|
||||
|
||||
res = vfs_mount(NULL, initramfs);
|
||||
res = vfs_mount(NULL, initramfs, 0);
|
||||
|
||||
if (res < 0)
|
||||
return res;
|
||||
|
|
|
|||
283
fs/vfs/mount.c
Normal file
283
fs/vfs/mount.c
Normal file
|
|
@ -0,0 +1,283 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/slab.h>
|
||||
|
||||
#include <fs/vfs.h>
|
||||
|
||||
#include <lib/errno.h>
|
||||
#include <lib/string.h>
|
||||
|
||||
static struct mount *root_mount = NULL;
|
||||
static spinlock_t mount_lock = 0;
|
||||
|
||||
fs_t *vfs_get_root(void)
|
||||
{
|
||||
if (!root_mount)
|
||||
return NULL;
|
||||
return root_mount->fs;
|
||||
}
|
||||
|
||||
struct mount *vfs_get_root_mount(void)
|
||||
{
|
||||
return root_mount;
|
||||
}
|
||||
|
||||
struct mount *mount_find_child(struct mount *parent, dirent_t *dent)
|
||||
{
|
||||
spinlock_acquire(&mount_lock);
|
||||
|
||||
list_item_t *item = parent->children.head;
|
||||
while (item) {
|
||||
struct mount *m = item->ptr;
|
||||
if (m->mountpoint == dent) {
|
||||
spinlock_release(&mount_lock);
|
||||
return m;
|
||||
}
|
||||
item = item->next;
|
||||
}
|
||||
|
||||
spinlock_release(&mount_lock);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int load_inode(dirent_t *dent)
|
||||
{
|
||||
if (dent->inode)
|
||||
return 0;
|
||||
|
||||
int res = dent->fs->fs_ops.open_inode(dent->fs, dent->ino_num, dent, &dent->inode);
|
||||
return res;
|
||||
}
|
||||
|
||||
static int mount_traverse(const char *path, struct mount **out_mount, dirent_t **out_dirent)
|
||||
{
|
||||
if (!root_mount)
|
||||
return -ENOSYS;
|
||||
|
||||
while (*path == '/')
|
||||
path++;
|
||||
|
||||
if (*path == '\0') {
|
||||
*out_mount = root_mount;
|
||||
*out_dirent = root_mount->fs->root_dirent;
|
||||
if (*out_dirent)
|
||||
dirent_inc_ref(*out_dirent);
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct mount *cur = root_mount;
|
||||
dirent_t *cur_dent = root_mount->fs->root_dirent;
|
||||
|
||||
if (!cur_dent)
|
||||
return -ENOENT;
|
||||
|
||||
dirent_inc_ref(cur_dent);
|
||||
|
||||
const char *p = path;
|
||||
|
||||
while (*p != '\0') {
|
||||
const char *comp_start = p;
|
||||
while (*p != '/' && *p != '\0')
|
||||
p++;
|
||||
|
||||
size_t comp_len = p - comp_start;
|
||||
if (comp_len > FILE_NAME_MAX_LEN) {
|
||||
dirent_dec_ref(cur_dent);
|
||||
return -ENAMETOOLONG;
|
||||
}
|
||||
|
||||
char name[FILE_NAME_MAX_LEN + 1];
|
||||
memcpy(name, comp_start, comp_len);
|
||||
name[comp_len] = '\0';
|
||||
|
||||
while (*p == '/')
|
||||
p++;
|
||||
|
||||
bool is_last = (*p == '\0');
|
||||
|
||||
dirent_t *found = NULL;
|
||||
int res = inode_find_dirent(cur_dent->inode, name, &found);
|
||||
if (res < 0) {
|
||||
dirent_dec_ref(cur_dent);
|
||||
return res;
|
||||
}
|
||||
|
||||
res = load_inode(found);
|
||||
if (res < 0) {
|
||||
dirent_dec_ref(found);
|
||||
dirent_dec_ref(cur_dent);
|
||||
return res;
|
||||
}
|
||||
|
||||
if (is_last) {
|
||||
dirent_dec_ref(cur_dent);
|
||||
*out_mount = cur;
|
||||
*out_dirent = found;
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct mount *child = NULL;
|
||||
list_item_t *item = cur->children.head;
|
||||
while (item) {
|
||||
struct mount *m = item->ptr;
|
||||
if (m->mountpoint == found) {
|
||||
child = m;
|
||||
break;
|
||||
}
|
||||
item = item->next;
|
||||
}
|
||||
|
||||
dirent_dec_ref(cur_dent);
|
||||
|
||||
if (child) {
|
||||
dirent_dec_ref(found);
|
||||
cur = child;
|
||||
cur_dent = cur->fs->root_dirent;
|
||||
if (!cur_dent)
|
||||
return -ENOENT;
|
||||
dirent_inc_ref(cur_dent);
|
||||
} else {
|
||||
cur_dent = found;
|
||||
}
|
||||
}
|
||||
|
||||
dirent_dec_ref(cur_dent);
|
||||
*out_mount = cur;
|
||||
*out_dirent = cur->fs->root_dirent;
|
||||
if (*out_dirent)
|
||||
dirent_inc_ref(*out_dirent);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int vfs_mount(const char *path, fs_t *fs, u64 flags)
|
||||
{
|
||||
if (!fs)
|
||||
return -EINVAL;
|
||||
|
||||
spinlock_acquire(&mount_lock);
|
||||
|
||||
if (!path) {
|
||||
if (root_mount) {
|
||||
spinlock_release(&mount_lock);
|
||||
return -EBUSY;
|
||||
}
|
||||
|
||||
struct mount *m = kzalloc(sizeof(*m), ALLOC_KERN);
|
||||
if (!m) {
|
||||
spinlock_release(&mount_lock);
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
m->parent = NULL;
|
||||
m->fs = fs;
|
||||
m->mountpoint = NULL;
|
||||
m->flags = flags;
|
||||
m->child_link = NULL;
|
||||
list_create(&m->children);
|
||||
|
||||
root_mount = m;
|
||||
spinlock_release(&mount_lock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct mount *parent = NULL;
|
||||
dirent_t *mp = NULL;
|
||||
int res = mount_traverse(path, &parent, &mp);
|
||||
if (res < 0) {
|
||||
spinlock_release(&mount_lock);
|
||||
return res;
|
||||
}
|
||||
|
||||
if ((mp->inode->mode & S_IFMT) != S_IFDIR) {
|
||||
dirent_dec_ref(mp);
|
||||
spinlock_release(&mount_lock);
|
||||
return -ENOTDIR;
|
||||
}
|
||||
|
||||
list_item_t *item = parent->children.head;
|
||||
while (item) {
|
||||
struct mount *m = item->ptr;
|
||||
if (m->mountpoint == mp) {
|
||||
dirent_dec_ref(mp);
|
||||
spinlock_release(&mount_lock);
|
||||
return -EBUSY;
|
||||
}
|
||||
item = item->next;
|
||||
}
|
||||
|
||||
struct mount *m = kzalloc(sizeof(*m), ALLOC_KERN);
|
||||
if (!m) {
|
||||
dirent_dec_ref(mp);
|
||||
spinlock_release(&mount_lock);
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
m->parent = parent;
|
||||
m->fs = fs;
|
||||
m->mountpoint = mp;
|
||||
m->flags = flags;
|
||||
|
||||
list_create(&m->children);
|
||||
|
||||
m->child_link = list_insert(&parent->children, m);
|
||||
if (!m->child_link) {
|
||||
dirent_dec_ref(mp);
|
||||
kfree(m);
|
||||
spinlock_release(&mount_lock);
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
spinlock_release(&mount_lock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int vfs_umount(const char *path)
|
||||
{
|
||||
if (!path)
|
||||
return -EINVAL;
|
||||
|
||||
spinlock_acquire(&mount_lock);
|
||||
|
||||
struct mount *parent = NULL;
|
||||
dirent_t *mp = NULL;
|
||||
int res = mount_traverse(path, &parent, &mp);
|
||||
if (res < 0) {
|
||||
spinlock_release(&mount_lock);
|
||||
return res;
|
||||
}
|
||||
|
||||
struct mount *target = NULL;
|
||||
list_item_t *item = parent->children.head;
|
||||
while (item) {
|
||||
struct mount *m = item->ptr;
|
||||
if (m->mountpoint == mp) {
|
||||
target = m;
|
||||
break;
|
||||
}
|
||||
item = item->next;
|
||||
}
|
||||
|
||||
dirent_dec_ref(mp);
|
||||
|
||||
if (!target) {
|
||||
spinlock_release(&mount_lock);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (target == root_mount) {
|
||||
spinlock_release(&mount_lock);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (target->children.head) {
|
||||
spinlock_release(&mount_lock);
|
||||
return -EBUSY;
|
||||
}
|
||||
|
||||
list_remove(target->child_link);
|
||||
dirent_dec_ref(target->mountpoint);
|
||||
kfree(target);
|
||||
|
||||
spinlock_release(&mount_lock);
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -243,4 +243,3 @@ void ramfs_init(void)
|
|||
|
||||
vfs_register(&ramfs_type);
|
||||
}
|
||||
DRIVER_ENTRY(ramfs_init);
|
||||
|
|
|
|||
35
fs/vfs/vfs.c
35
fs/vfs/vfs.c
|
|
@ -6,7 +6,6 @@
|
|||
#include <lib/string.h>
|
||||
#include <lib/errno.h>
|
||||
|
||||
static fs_t *root_fs = NULL;
|
||||
static list_t fs_type_list = {0};
|
||||
|
||||
static char *path_trim(const char *name)
|
||||
|
|
@ -85,7 +84,7 @@ static int next_level(const char *path, dirent_t *dent, dirent_t **out_dirent)
|
|||
return -ENOENT;
|
||||
}
|
||||
|
||||
int vfs_openat(dirent_t *in, const char *name, dirent_t **out)
|
||||
int vfs_openat(struct mount *mnt, dirent_t *in, const char *name, dirent_t **out)
|
||||
{
|
||||
if (!in || !name || !out)
|
||||
return -EINVAL;
|
||||
|
|
@ -94,6 +93,7 @@ int vfs_openat(dirent_t *in, const char *name, dirent_t **out)
|
|||
if (!path_optr)
|
||||
return -ENOMEM;
|
||||
|
||||
struct mount *cur_mount = mnt;
|
||||
dirent_t *current_dir = in;
|
||||
dirent_inc_ref(current_dir);
|
||||
|
||||
|
|
@ -118,6 +118,19 @@ int vfs_openat(dirent_t *in, const char *name, dirent_t **out)
|
|||
|
||||
dirent_dec_ref(current_dir);
|
||||
current_dir = next_dir;
|
||||
|
||||
struct mount *child = mount_find_child(cur_mount, current_dir);
|
||||
if (child) {
|
||||
dirent_dec_ref(current_dir);
|
||||
cur_mount = child;
|
||||
current_dir = child->fs->root_dirent;
|
||||
if (!current_dir) {
|
||||
ret = -ENOENT;
|
||||
goto out;
|
||||
}
|
||||
dirent_inc_ref(current_dir);
|
||||
}
|
||||
|
||||
cur_path += res;
|
||||
|
||||
while (*cur_path == '/')
|
||||
|
|
@ -140,10 +153,11 @@ int vfs_open(const char *abs_path, dirent_t **out)
|
|||
if (!abs_path || !out)
|
||||
return -EINVAL;
|
||||
|
||||
if (!root_fs->root_dirent)
|
||||
struct mount *root = vfs_get_root_mount();
|
||||
if (!root || !root->fs || !root->fs->root_dirent)
|
||||
return -ENOENT;
|
||||
|
||||
return vfs_openat(root_fs->root_dirent, abs_path, out);
|
||||
return vfs_openat(root, root->fs->root_dirent, abs_path, out);
|
||||
}
|
||||
|
||||
void vfs_close(dirent_t *dent)
|
||||
|
|
@ -284,16 +298,6 @@ int vfs_unlink(dirent_t *parent, const char *name)
|
|||
return res;
|
||||
}
|
||||
|
||||
int vfs_mount(const char *mount_point, fs_t *fs_to_mount)
|
||||
{
|
||||
if (!mount_point || !strcmp(mount_point, "/")) {
|
||||
root_fs = fs_to_mount;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -ENOSYS;
|
||||
}
|
||||
|
||||
void vfs_register(struct fs_driver *fs)
|
||||
{
|
||||
struct fs_driver *ins = kalloc(sizeof(*fs), ALLOC_KERN);
|
||||
|
|
@ -308,8 +312,5 @@ void vfs_register(struct fs_driver *fs)
|
|||
void vfs_init(void)
|
||||
{
|
||||
inode_init();
|
||||
root_fs = kalloc(sizeof(fs_t), ALLOC_KERN);
|
||||
memset(root_fs, 0, sizeof(*root_fs));
|
||||
|
||||
list_create(&fs_type_list);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ struct tty_serial_ops {
|
|||
struct tty {
|
||||
dev_t dev;
|
||||
void *priv;
|
||||
struct tty_serial_ops *serial_ops;
|
||||
struct tty_serial_ops serial_ops;
|
||||
};
|
||||
|
||||
void tty_layer_init();
|
||||
|
|
|
|||
15
include/fs/devfs.h
Normal file
15
include/fs/devfs.h
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _DEVFS_H_
|
||||
#define _DEVFS_H_
|
||||
|
||||
#include <kernel/block.h>
|
||||
|
||||
#include <drivers/tty.h>
|
||||
|
||||
#include <fs/inode.h>
|
||||
|
||||
int devfs_insert_device_chr(const char *name, struct tty *device, dirent_t **out);
|
||||
int devfs_insert_device_blk(const char *name, struct block_device *device, dirent_t **out);
|
||||
void devfs_init();
|
||||
|
||||
#endif /* _DEVFS_H_ */
|
||||
|
|
@ -9,4 +9,6 @@
|
|||
int ramfs_create(u64 flags, fs_t **out);
|
||||
int ramfs_umount(fs_t *fs);
|
||||
|
||||
void ramfs_init(void);
|
||||
|
||||
#endif /* _RAMFS_H_ */
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
#define _VFS_H_
|
||||
|
||||
#include <kernel/spinlock.h>
|
||||
#include <kernel/slab.h>
|
||||
#include <fs/inode.h>
|
||||
|
||||
#include <lib/rwlock.h>
|
||||
|
|
@ -81,19 +82,32 @@ struct _fs {
|
|||
|
||||
struct dir_context {
|
||||
void *cookie;
|
||||
bool init;
|
||||
bool init;
|
||||
};
|
||||
|
||||
struct mount {
|
||||
struct mount *parent;
|
||||
list_t children;
|
||||
list_item_t *child_link;
|
||||
fs_t *fs;
|
||||
dirent_t *mountpoint;
|
||||
u64 flags;
|
||||
};
|
||||
|
||||
void vfs_init(void);
|
||||
void vfs_register(struct fs_driver *fs);
|
||||
|
||||
int vfs_open(const char *abs_path, dirent_t **out);
|
||||
int vfs_openat(dirent_t *in, const char *name, dirent_t **out);
|
||||
int vfs_openat(struct mount *mnt, dirent_t *in, const char *name, dirent_t **out);
|
||||
void vfs_close(dirent_t *dent);
|
||||
int vfs_readdir(dirent_t *dir, struct dir_context *ctx, dirent_t **out);
|
||||
int vfs_creat(dirent_t *parent, const char *name, u64 mode, int flags, dirent_t **out);
|
||||
int vfs_unlink(dirent_t *parent, const char *name);
|
||||
int vfs_mount(const char *mount_point, fs_t *fs_to_mount);
|
||||
int vfs_mount(const char *path, fs_t *fs, u64 flags);
|
||||
int vfs_umount(const char *path);
|
||||
fs_t *vfs_get_root(void);
|
||||
struct mount *vfs_get_root_mount(void);
|
||||
struct mount *mount_find_child(struct mount *parent, dirent_t *dent);
|
||||
|
||||
ssize_t vfs_write(inode_t *ino, char *buf, size_t size, size_t offset);
|
||||
ssize_t vfs_read(inode_t *ino, char *buf, size_t size, size_t offset);
|
||||
|
|
|
|||
|
|
@ -5,8 +5,9 @@
|
|||
#include <kernel/common.h>
|
||||
|
||||
#define DEV_CLASS_TTY 1
|
||||
#define DEV_CLASS_BLK 2
|
||||
|
||||
typedef u64 dev_t;
|
||||
typedef i64 dev_t;
|
||||
typedef u64 dev_class_t;
|
||||
|
||||
|
||||
|
|
|
|||
39
init/main.c
39
init/main.c
|
|
@ -25,6 +25,8 @@
|
|||
#include <fs/vfs.h>
|
||||
#include <fs/inode.h>
|
||||
#include <fs/cpio.h>
|
||||
#include <fs/ramfs.h>
|
||||
#include <fs/devfs.h>
|
||||
|
||||
#include <lib/stdio.h>
|
||||
#include <lib/list.h>
|
||||
|
|
@ -43,13 +45,6 @@ static void do_initcalls()
|
|||
(*fn)();
|
||||
}
|
||||
|
||||
static void indent(int indent)
|
||||
{
|
||||
for (int i = 0; i < indent; i++) {
|
||||
kprintf(" ");
|
||||
}
|
||||
}
|
||||
|
||||
static void kmain()
|
||||
{
|
||||
/* data types */
|
||||
|
|
@ -58,24 +53,9 @@ static void kmain()
|
|||
kalloc_init();
|
||||
vfs_init();
|
||||
arch_init();
|
||||
device_layer_init();
|
||||
tty_layer_init();
|
||||
|
||||
void *serial_port = arch_serial_init();
|
||||
tty_printf = tty_create("ttyS0", serial_port);
|
||||
|
||||
int rows;
|
||||
int cols;
|
||||
|
||||
tty_clear_screen(tty_printf);
|
||||
tty_get_size(tty_printf, &rows, &cols);
|
||||
tty_setlf(tty_printf);
|
||||
tty_set_cursor_pos(tty_printf, 0, 0);
|
||||
|
||||
/* start the kernel */
|
||||
page_init();
|
||||
do_initcalls();
|
||||
|
||||
ramfs_init();
|
||||
struct limine_file *file = module_res->modules[0];
|
||||
|
||||
fs_t *initramfs;
|
||||
|
|
@ -85,6 +65,19 @@ static void kmain()
|
|||
if (res < 0)
|
||||
panic("failed to load initramfs: %s\n", strerror(-res));
|
||||
|
||||
/* start the kernel */
|
||||
devfs_init();
|
||||
device_layer_init();
|
||||
tty_layer_init();
|
||||
|
||||
/* init main fallback tty */
|
||||
struct tty *main_tty = NULL;
|
||||
serial_port_tty_init(&main_tty);
|
||||
|
||||
tty_printf = main_tty;
|
||||
|
||||
do_initcalls();
|
||||
|
||||
/* do nothing, for now :) */
|
||||
yield();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,2 +1,3 @@
|
|||
# SPDX-License-Identifier: GPL-2.0-only
|
||||
obj-y += device.o
|
||||
obj-y += block.o
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/common.h>
|
||||
|
||||
#include <drivers/block.h>
|
||||
#include <kernel/block.h>
|
||||
|
||||
ssize_t block_read(struct block_device *dev, char *buf, size_t offset, size_t n)
|
||||
{
|
||||
|
|
@ -1,6 +1,12 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <drivers/tty.h>
|
||||
#include <kernel/slab.h>
|
||||
#include <kernel/block.h>
|
||||
#include <kernel/device.h>
|
||||
#include <kernel/atomic.h>
|
||||
|
||||
#include <fs/devfs.h>
|
||||
|
||||
#include <lib/rbtree.h>
|
||||
#include <lib/string.h>
|
||||
#include <lib/math.h>
|
||||
|
|
@ -16,9 +22,11 @@ static mem_cache_t *dev_info_cache = NULL;
|
|||
|
||||
static rbtree_t device_tree = {0};
|
||||
|
||||
static u64 device_ticker = 0;
|
||||
|
||||
dev_t device_create(void *private, dev_class_t class, const char *name)
|
||||
{
|
||||
dev_t key = rbt_next_key(&device_tree);
|
||||
dev_t key = atomic_xadd(&device_ticker, 1);
|
||||
rbnode_t *node = rbt_insert(&device_tree, key);
|
||||
|
||||
size_t name_len = strlen(name);
|
||||
|
|
@ -31,6 +39,26 @@ dev_t device_create(void *private, dev_class_t class, const char *name)
|
|||
|
||||
node->value = (vaddr_t)info;
|
||||
|
||||
dirent_t *devfile = NULL;
|
||||
|
||||
if (class == DEV_CLASS_BLK) {
|
||||
int res = devfs_insert_device_blk(name, private, &devfile);
|
||||
|
||||
if (res < 0) {
|
||||
kerr("failed to insert blk device node: %s\n", strerror(-res));
|
||||
return res;
|
||||
}
|
||||
} else if (class == DEV_CLASS_TTY) {
|
||||
int res = devfs_insert_device_chr(name, private, &devfile);
|
||||
|
||||
if (res < 0) {
|
||||
kerr("failed to insert chr device node: %s\n", strerror(-res));
|
||||
return res;
|
||||
} else {
|
||||
krdy("inserted chrdev node %s: value %d\n", name, res);
|
||||
}
|
||||
}
|
||||
|
||||
return key;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -225,6 +225,8 @@ char *dirname(const char *path)
|
|||
if (!path)
|
||||
return strdup(".");
|
||||
char *copy = strdup(path);
|
||||
if (!copy)
|
||||
return NULL;
|
||||
char *res = udirname(copy);
|
||||
char *ret = strdup(res);
|
||||
kfree(copy);
|
||||
|
|
@ -236,6 +238,8 @@ char *basename(const char *path)
|
|||
if (!path)
|
||||
return strdup(".");
|
||||
char *copy = strdup(path);
|
||||
if (!copy)
|
||||
return NULL;
|
||||
char *res = ubasename(copy);
|
||||
char *ret = strdup(res);
|
||||
kfree(copy);
|
||||
|
|
|
|||
|
|
@ -88,6 +88,9 @@ void *kzalloc(size_t size, u64 flags)
|
|||
|
||||
void kfree(void *ptr)
|
||||
{
|
||||
if (!ptr)
|
||||
return;
|
||||
|
||||
rbnode_t *node = rbt_search(&kalloc_tree, (u64)ptr);
|
||||
|
||||
if (!node)
|
||||
|
|
|
|||
|
|
@ -138,6 +138,8 @@ static void init_freelist(struct buddy_zone *bzone)
|
|||
|
||||
while (remaining >= PAGE_SIZE) {
|
||||
size_t order = buddy_order(remaining);
|
||||
if (order > bzone->max_order)
|
||||
order = bzone->max_order;
|
||||
|
||||
cur->order = order;
|
||||
cur->zone = bzone;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
# SPDX-License-Identifier: GPL-2.0-only
|
||||
# Makefile stuff for all the booting and bootloader related stuff
|
||||
#
|
||||
INITRAMFS := initramfs.cpio
|
||||
|
||||
$(IMG): limine/limine $(KERNEL)
|
||||
rm -rf $(IMG)
|
||||
|
|
@ -32,11 +34,17 @@ limine/limine:
|
|||
LDFLAGS="$(HOST_LDFLAGS)" \
|
||||
LIBS="$(HOST_LIBS)"
|
||||
|
||||
$(INITRAMFS): initramfs/
|
||||
$(Q)echo " CPIO $@"
|
||||
$(Q)mkdir -p initramfs/
|
||||
$(Q)cd initramfs/ && find . | cpio -oH newc > ../$(INITRAMFS)
|
||||
|
||||
.PHONY += run
|
||||
run: edk2-ovmf $(IMG)
|
||||
run: edk2-ovmf $(INITRAMFS) $(IMG)
|
||||
qemu-system-$(ARCH) \
|
||||
-M q35 \
|
||||
-drive if=pflash,unit=0,format=raw,file=edk2-ovmf/ovmf-code-$(ARCH).fd,readonly=on \
|
||||
-accel kvm \
|
||||
-nographic \
|
||||
-hda $(IMG) \
|
||||
$(QEMUFLAGS)
|
||||
|
|
|
|||
Loading…
Reference in a new issue