fs: move fs init into kernel cmdline
Previously, any partition with an ext2 filesystem would be mounted by the gpt header parser. With this patch, the kernel cmdline `root=val` is responsible for mounting the root filesystem.
This commit is contained in:
parent
5ebafca2e6
commit
25a828437a
10 changed files with 150 additions and 32 deletions
10
fs/block.c
10
fs/block.c
|
|
@ -35,6 +35,16 @@ struct block_device *block_register(char *name, struct block_device_ops *ops, vo
|
||||||
return bdev;
|
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)
|
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);
|
return bdev->ops.read(bdev, buf, offset + bdev->lba_start, size);
|
||||||
|
|
|
||||||
58
fs/ext2.c
58
fs/ext2.c
|
|
@ -114,9 +114,10 @@ int ext2_write_inode(struct ext2fs *fs, struct ext2_inode *in, uint32_t inode)
|
||||||
return 0;
|
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 */
|
/* find the inode of the file */
|
||||||
|
struct ext2fs *fs = (struct ext2fs *)extfs;
|
||||||
struct ext2_inode *inode = kmalloc(sizeof(struct ext2_inode), ALLOC_KERN);
|
struct ext2_inode *inode = kmalloc(sizeof(struct ext2_inode), ALLOC_KERN);
|
||||||
if (!inode)
|
if (!inode)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
|
@ -199,17 +200,22 @@ int ext2_open_file(struct ext2fs *fs, struct file *file, const char *path)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ext2_close_file(struct ext2fs *fs, struct file *file)
|
int ext2_close_file(void *extfs, struct file *file)
|
||||||
{
|
{
|
||||||
return 0;
|
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;
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
@ -290,45 +296,59 @@ next:
|
||||||
}
|
}
|
||||||
#endif /* KDEBUG */
|
#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;
|
return NULL;
|
||||||
|
|
||||||
if (ext2_read_super(bdev, &ret->sb) < 0) {
|
if (ext2_read_super(bdev, &extfs->sb) < 0) {
|
||||||
kfree(ret);
|
kfree(extfs);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret->bdev = bdev;
|
extfs->bdev = bdev;
|
||||||
ret->block_size = 1024 << ret->sb.log_block_size;
|
extfs->block_size = 1024 << extfs->sb.log_block_size;
|
||||||
|
|
||||||
bdev->fs = ret;
|
bdev->fs = extfs;
|
||||||
|
|
||||||
/* read block group descriptor table */
|
/* read block group descriptor table */
|
||||||
uint32_t num_groups = ret->sb.blocks_count / ret->sb.blocks_per_group;
|
uint32_t num_groups = extfs->sb.blocks_count / extfs->sb.blocks_per_group;
|
||||||
if (ret->sb.blocks_count % ret->sb.blocks_per_group)
|
if (extfs->sb.blocks_count % extfs->sb.blocks_per_group)
|
||||||
num_groups++;
|
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);
|
uint32_t bgdt_size = num_groups * sizeof(struct ext2_group_desc);
|
||||||
|
|
||||||
char *buf = kmalloc(bgdt_size, ALLOC_DMA);
|
char *buf = kmalloc(bgdt_size, ALLOC_DMA);
|
||||||
|
|
||||||
ext2_read_block(bdev->fs, buf, bgdt_block);
|
ext2_read_block(bdev->fs, buf, bgdt_block);
|
||||||
|
|
||||||
ret->bgdt = (struct ext2_group_desc *)kmalloc(bgdt_size, ALLOC_KERN);
|
extfs->bgdt = (struct ext2_group_desc *)kmalloc(bgdt_size, ALLOC_KERN);
|
||||||
memcpy(ret->bgdt, buf, bgdt_size);
|
memcpy(extfs->bgdt, buf, bgdt_size);
|
||||||
|
|
||||||
kfree(buf);
|
kfree(buf);
|
||||||
|
|
||||||
#ifdef KDEBUG
|
#ifdef KDEBUG
|
||||||
struct ext2_inode root;
|
struct ext2_inode root;
|
||||||
ext2_read_inode(ret, &root, 2);
|
ext2_read_inode(extfs, &root, 2);
|
||||||
ext2_print_dir(ret, &root, 0);
|
ext2_print_dir(extfs, &root, 0);
|
||||||
#endif /* KDEBUG */
|
#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;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
5
fs/gpt.c
5
fs/gpt.c
|
|
@ -73,11 +73,6 @@ void block_gpt_init(struct block_device *dev)
|
||||||
|
|
||||||
bdev->lba_start = entry->lba_first;
|
bdev->lba_start = entry->lba_first;
|
||||||
bdev->block_count = entry->lba_last - entry->lba_first + 1;
|
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;
|
dev->partition_count = used_count;
|
||||||
|
|
||||||
|
|
|
||||||
32
fs/vfs.c
32
fs/vfs.c
|
|
@ -1,12 +1,38 @@
|
||||||
/* SDPX-License-Identifier: GPL-2.0-only */
|
/* SDPX-License-Identifier: GPL-2.0-only */
|
||||||
|
|
||||||
#include <fs/vfs.h>
|
|
||||||
#include <kernel/slab.h>
|
#include <kernel/slab.h>
|
||||||
#include <kernel/block.h>
|
#include <kernel/block.h>
|
||||||
|
#include <kernel/rbtree.h>
|
||||||
|
|
||||||
void vfs_init(struct block_device *root)
|
#include <fs/ext2.h>
|
||||||
|
|
||||||
|
#include <fs/vfs.h>
|
||||||
|
|
||||||
|
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)
|
int write(int fd, const void *buf, size_t count)
|
||||||
|
|
|
||||||
|
|
@ -141,7 +141,7 @@ struct ext2fs {
|
||||||
uint32_t block_size;
|
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 */
|
/* Filesystem block sizes are not equal to block device block sizes */
|
||||||
int ext2_read_block(struct ext2fs *fs, void *buf, size_t block);
|
int ext2_read_block(struct ext2fs *fs, void *buf, size_t block);
|
||||||
|
|
|
||||||
|
|
@ -40,14 +40,15 @@ struct file {
|
||||||
uint64_t size; /* size of the file */
|
uint64_t size; /* size of the file */
|
||||||
uint64_t pos; /* current position in the file */
|
uint64_t pos; /* current position in the file */
|
||||||
|
|
||||||
void *fs_data;
|
void *fs_file;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct fs_ops {
|
struct fs_ops {
|
||||||
int (*read)(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 offset, 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 (*open)(void *fs, struct file *file, const char *path);
|
||||||
int (*close)(void *fs, struct file *file);
|
int (*close)(void *fs, struct file *file);
|
||||||
|
int (*seek)(void *fs, struct file *file, size_t offset);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct fs {
|
struct fs {
|
||||||
|
|
@ -56,4 +57,6 @@ struct fs {
|
||||||
struct fs_ops ops;
|
struct fs_ops ops;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void vfs_init(const char *rootfs);
|
||||||
|
|
||||||
#endif /* _VFS_H_ */
|
#endif /* _VFS_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_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_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);
|
int block_write(struct block_device *bdev, void *buf, size_t offset, size_t size);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@
|
||||||
void *memcpy(void *dest, const void *src, size_t num);
|
void *memcpy(void *dest, const void *src, size_t num);
|
||||||
int memcmp(const void *aa, const void *bb, size_t num);
|
int memcmp(const void *aa, const void *bb, size_t num);
|
||||||
int strcmp(const char *a, const char *b);
|
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);
|
void *memset(void *str, int c, size_t n);
|
||||||
char *strcpy(char *dest, const char *src);
|
char *strcpy(char *dest, const char *src);
|
||||||
char *strncpy(char *dest, const char *src, size_t n);
|
char *strncpy(char *dest, const char *src, size_t n);
|
||||||
|
|
|
||||||
52
init/main.c
52
init/main.c
|
|
@ -14,6 +14,10 @@
|
||||||
#include <dev/pci.h>
|
#include <dev/pci.h>
|
||||||
#include <dev/ahci.h>
|
#include <dev/ahci.h>
|
||||||
|
|
||||||
|
#include <fs/vfs.h>
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#define LIMINE_INTERNAL_MODULE_REQUIRED (1 << 0)
|
#define LIMINE_INTERNAL_MODULE_REQUIRED (1 << 0)
|
||||||
#include <limine/limine.h>
|
#include <limine/limine.h>
|
||||||
|
|
||||||
|
|
@ -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)
|
void kmain(void)
|
||||||
{
|
{
|
||||||
hhdm_start = hhdm_req.response->offset;
|
hhdm_start = hhdm_req.response->offset;
|
||||||
|
|
@ -101,6 +147,12 @@ void kmain_post_stack_init()
|
||||||
{
|
{
|
||||||
ahci_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();
|
_pic_init();
|
||||||
|
|
||||||
kprintf("\n\n# ");
|
kprintf("\n\n# ");
|
||||||
|
|
|
||||||
12
lib/string.c
12
lib/string.c
|
|
@ -1,5 +1,4 @@
|
||||||
/* SPDX-License-Identifier: GPL-2.0-only */
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||||
#include "kernel/pio.h"
|
|
||||||
#include <kernel/slab.h>
|
#include <kernel/slab.h>
|
||||||
#include <kernel/lock.h>
|
#include <kernel/lock.h>
|
||||||
|
|
||||||
|
|
@ -28,6 +27,17 @@ int strcmp(const char *a, const char *b)
|
||||||
return *a - *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)
|
char *strcpy(char *dest, const char *src)
|
||||||
{
|
{
|
||||||
while ((*dest++ = *src++))
|
while ((*dest++ = *src++))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue