diff --git a/fs/block.c b/fs/block.c index e387a44..8c7b153 100644 --- a/fs/block.c +++ b/fs/block.c @@ -35,6 +35,16 @@ struct block_device *block_register(char *name, struct block_device_ops *ops, vo return bdev; } +struct block_device *block_get_device(const char *name) +{ + for (size_t i = 0; i < block_devices_count; i++) { + if (!strcmp(block_devices[i]->name, name)) + return block_devices[i]; + } + + return NULL; +} + inline int block_read(struct block_device *bdev, void *buf, size_t offset, size_t size) { return bdev->ops.read(bdev, buf, offset + bdev->lba_start, size); diff --git a/fs/ext2.c b/fs/ext2.c index 42622e3..c622ee9 100644 --- a/fs/ext2.c +++ b/fs/ext2.c @@ -114,9 +114,10 @@ int ext2_write_inode(struct ext2fs *fs, struct ext2_inode *in, uint32_t inode) return 0; } -int ext2_open_file(struct ext2fs *fs, struct file *file, const char *path) +int ext2_open_file(void *extfs, struct file *file, const char *path) { /* find the inode of the file */ + struct ext2fs *fs = (struct ext2fs *)extfs; struct ext2_inode *inode = kmalloc(sizeof(struct ext2_inode), ALLOC_KERN); if (!inode) return -ENOMEM; @@ -199,17 +200,22 @@ int ext2_open_file(struct ext2fs *fs, struct file *file, const char *path) return 0; } -int ext2_close_file(struct ext2fs *fs, struct file *file) +int ext2_close_file(void *extfs, struct file *file) { return 0; } -int ext2_write_file(struct ext2fs *fs, struct file *file, const void *buf, size_t count) +int ext2_write_file(void *extfs, struct file *file, void *buf, size_t count) { return 0; } -int ext2_read_file(struct ext2fs *fs, struct file *file, const void *buf, size_t count) +int ext2_read_file(void *extfs, struct file *file, void *buf, size_t count) +{ + return 0; +} + +int ext2_seek_file(void *extfs, struct file *file, size_t offset) { return 0; } @@ -290,45 +296,59 @@ next: } #endif /* KDEBUG */ -struct ext2fs *ext2_init_fs(struct block_device *bdev) +struct fs *ext2_init_fs(struct block_device *bdev) { - struct ext2fs *ret = kmalloc(sizeof(*ret), ALLOC_KERN); + struct ext2fs *extfs = kmalloc(sizeof(*extfs), ALLOC_KERN); - if (!ret) + if (!extfs) return NULL; - if (ext2_read_super(bdev, &ret->sb) < 0) { - kfree(ret); + if (ext2_read_super(bdev, &extfs->sb) < 0) { + kfree(extfs); return NULL; } - ret->bdev = bdev; - ret->block_size = 1024 << ret->sb.log_block_size; + extfs->bdev = bdev; + extfs->block_size = 1024 << extfs->sb.log_block_size; - bdev->fs = ret; + bdev->fs = extfs; /* read block group descriptor table */ - uint32_t num_groups = ret->sb.blocks_count / ret->sb.blocks_per_group; - if (ret->sb.blocks_count % ret->sb.blocks_per_group) + uint32_t num_groups = extfs->sb.blocks_count / extfs->sb.blocks_per_group; + if (extfs->sb.blocks_count % extfs->sb.blocks_per_group) num_groups++; - uint32_t bgdt_block = 2 + (ret->sb.first_data_block / ret->sb.blocks_per_group); + uint32_t bgdt_block = 2 + (extfs->sb.first_data_block / extfs->sb.blocks_per_group); uint32_t bgdt_size = num_groups * sizeof(struct ext2_group_desc); char *buf = kmalloc(bgdt_size, ALLOC_DMA); ext2_read_block(bdev->fs, buf, bgdt_block); - ret->bgdt = (struct ext2_group_desc *)kmalloc(bgdt_size, ALLOC_KERN); - memcpy(ret->bgdt, buf, bgdt_size); + extfs->bgdt = (struct ext2_group_desc *)kmalloc(bgdt_size, ALLOC_KERN); + memcpy(extfs->bgdt, buf, bgdt_size); kfree(buf); #ifdef KDEBUG struct ext2_inode root; - ext2_read_inode(ret, &root, 2); - ext2_print_dir(ret, &root, 0); + ext2_read_inode(extfs, &root, 2); + ext2_print_dir(extfs, &root, 0); #endif /* KDEBUG */ + struct fs *ret = kmalloc(sizeof(struct fs), ALLOC_KERN); + ret->fs = extfs; + ret->type = VFS_TYPE_EXT2; + + struct fs_ops ops = { + .open = ext2_open_file, + .close = ext2_close_file, + .read = ext2_read_file, + .write = ext2_write_file, + .seek = ext2_seek_file, + }; + + ret->ops = ops; + return ret; } diff --git a/fs/gpt.c b/fs/gpt.c index 7cddfa2..f759346 100644 --- a/fs/gpt.c +++ b/fs/gpt.c @@ -73,11 +73,6 @@ void block_gpt_init(struct block_device *dev) bdev->lba_start = entry->lba_first; bdev->block_count = entry->lba_last - entry->lba_first + 1; - - struct ext2fs *fs = ext2_init_fs(bdev); - if (fs) { - kprintf(LOG_INFO "Found ext2 filesystem on device %s\n", buf); - } } dev->partition_count = used_count; diff --git a/fs/vfs.c b/fs/vfs.c index c6f9d98..423109c 100644 --- a/fs/vfs.c +++ b/fs/vfs.c @@ -1,12 +1,38 @@ /* SDPX-License-Identifier: GPL-2.0-only */ - -#include #include #include +#include -void vfs_init(struct block_device *root) +#include + +#include + +struct rbtree *kfd; + +typedef struct fs *(*vfs_init_t)(struct block_device *); + +void vfs_init(const char *rootfs) { + kfd = kzalloc(sizeof(struct rbtree), ALLOC_DMA); + /* mount root */ + struct block_device *rootdev = block_get_device(rootfs); + if (!rootdev) { + kprintf(LOG_ERROR "Failed to find root device %s\n", rootfs); + return; + } else { + kprintf(LOG_DEBUG "Found root device %s\n", rootfs); + + /* TODO: detect filesystem type */ + /* for now, assume ext2 */ + struct fs *fs = ext2_init_fs(rootdev); + if (!fs) { + kprintf(LOG_ERROR "Failed to mount root device %s\n", rootfs); + return; + } else { + kprintf(LOG_DEBUG "Mounted root device %s\n", rootfs); + } + } } int write(int fd, const void *buf, size_t count) diff --git a/include/fs/ext2.h b/include/fs/ext2.h index 5368214..b79b2b9 100644 --- a/include/fs/ext2.h +++ b/include/fs/ext2.h @@ -141,7 +141,7 @@ struct ext2fs { uint32_t block_size; }; -struct ext2fs *ext2_init_fs(struct block_device *bdev); +struct fs *ext2_init_fs(struct block_device *bdev); /* Filesystem block sizes are not equal to block device block sizes */ int ext2_read_block(struct ext2fs *fs, void *buf, size_t block); diff --git a/include/fs/vfs.h b/include/fs/vfs.h index d4559b0..cd5e1e2 100644 --- a/include/fs/vfs.h +++ b/include/fs/vfs.h @@ -40,14 +40,15 @@ struct file { uint64_t size; /* size of the file */ uint64_t pos; /* current position in the file */ - void *fs_data; + void *fs_file; }; struct fs_ops { - int (*read)(void *fs, struct file *file, void *buf, size_t offset, size_t size); - int (*write)(void *fs, struct file *file, void *buf, size_t offset, size_t size); + int (*read)(void *fs, struct file *file, void *buf, size_t size); + int (*write)(void *fs, struct file *file, void *buf, size_t size); int (*open)(void *fs, struct file *file, const char *path); int (*close)(void *fs, struct file *file); + int (*seek)(void *fs, struct file *file, size_t offset); }; struct fs { @@ -56,4 +57,6 @@ struct fs { struct fs_ops ops; }; +void vfs_init(const char *rootfs); + #endif /* _VFS_H_ */ diff --git a/include/kernel/block.h b/include/kernel/block.h index a141094..11ba206 100644 --- a/include/kernel/block.h +++ b/include/kernel/block.h @@ -28,6 +28,7 @@ struct block_device { }; struct block_device *block_register(char *name, struct block_device_ops *ops, void *data, size_t block_count, size_t block_size); +struct block_device *block_get_device(const char *name); int block_read(struct block_device *bdev, void *buf, size_t offset, size_t size); int block_write(struct block_device *bdev, void *buf, size_t offset, size_t size); diff --git a/include/string.h b/include/string.h index ebeb77a..0d2fbd7 100644 --- a/include/string.h +++ b/include/string.h @@ -12,6 +12,7 @@ void *memcpy(void *dest, const void *src, size_t num); int memcmp(const void *aa, const void *bb, size_t num); int strcmp(const char *a, const char *b); +int strncmp(const char *a, const char *b, size_t n); void *memset(void *str, int c, size_t n); char *strcpy(char *dest, const char *src); char *strncpy(char *dest, const char *src, size_t n); diff --git a/init/main.c b/init/main.c index a458ec9..45f877e 100644 --- a/init/main.c +++ b/init/main.c @@ -14,6 +14,10 @@ #include #include +#include + +#include + #define LIMINE_INTERNAL_MODULE_REQUIRED (1 << 0) #include @@ -53,6 +57,48 @@ static inline void _pic_init() ; } +char *kcmdline_get_symbol(const char *sym) +{ + /* symbol=value */ + /* only parse until the space after the value */ + size_t sym_len = strlen(sym); + size_t cmd_len = strlen(kcmdline); + + spinlock_acquire(&strtok_lock); + + char *cmdline_copy = kmalloc(cmd_len + 1, ALLOC_KERN); + if (!cmdline_copy) { + spinlock_release(&strtok_lock); + return NULL; + } + + strcpy(cmdline_copy, kcmdline); + + char *token = strtok(cmdline_copy, " "); + while (token) { + if (!strncmp(token, sym, sym_len)) { + size_t ret_len = strlen(token + sym_len + 1); + char *ret = kzalloc(ret_len + 1, ALLOC_KERN); + if (!ret) { + kfree(cmdline_copy); + spinlock_release(&strtok_lock); + return NULL; + } + + strcpy(ret, token + sym_len + 1); + + kfree(cmdline_copy); + spinlock_release(&strtok_lock); + return ret; + } + token = strtok(NULL, " "); + } + + spinlock_release(&strtok_lock); + + return NULL; +} + void kmain(void) { hhdm_start = hhdm_req.response->offset; @@ -101,6 +147,12 @@ void kmain_post_stack_init() { ahci_init(); + char *root_path = kcmdline_get_symbol("root"); + if (!root_path) + kprintf(LOG_ERROR "No root= parameter specified\n"); + else + vfs_init(root_path); + _pic_init(); kprintf("\n\n# "); diff --git a/lib/string.c b/lib/string.c index 035fdd4..b7ec2d8 100644 --- a/lib/string.c +++ b/lib/string.c @@ -1,5 +1,4 @@ /* SPDX-License-Identifier: GPL-2.0-only */ -#include "kernel/pio.h" #include #include @@ -28,6 +27,17 @@ int strcmp(const char *a, const char *b) return *a - *b; } +int strncmp(const char *a, const char *b, size_t n) +{ + for (size_t i = 0; i < n; i++) { + if (a[i] != b[i]) + return a[i] - b[i]; + if (!a[i]) + break; + } + return 0; +} + char *strcpy(char *dest, const char *src) { while ((*dest++ = *src++))