diff options
author | Marvin Borner | 2021-01-07 20:11:48 +0100 |
---|---|---|
committer | Marvin Borner | 2021-01-07 20:11:48 +0100 |
commit | 9fad5e3bb718c5892ec72de42977886233721cb9 (patch) | |
tree | 135ac142e1b3d479d303e3aba4dcfa142749cb2a /kernel | |
parent | 8fb2349d12e21868c77f2c975dbd958eb2536499 (diff) |
Started conversion to VFS
Diffstat (limited to 'kernel')
-rw-r--r-- | kernel/features/fs.c | 77 | ||||
-rw-r--r-- | kernel/features/load.c | 4 | ||||
-rw-r--r-- | kernel/features/syscall.c | 4 | ||||
-rw-r--r-- | kernel/inc/fs.h | 26 |
4 files changed, 77 insertions, 34 deletions
diff --git a/kernel/features/fs.c b/kernel/features/fs.c index 670a195..a6c233b 100644 --- a/kernel/features/fs.c +++ b/kernel/features/fs.c @@ -9,6 +9,8 @@ #include <random.h> #include <str.h> +#include <cpu.h> // TODO: Remove later + /** * VFS */ @@ -40,16 +42,49 @@ u32 vfs_mount(struct device *dev, const char *path) return 1; } +const char *vfs_resolve_type(enum vfs_type type) +{ + switch (type) { + case VFS_DEVFS: + return "devfs"; + case VFS_TMPFS: + return "tmpfs"; + case VFS_PROCFS: + return "procfs"; + case VFS_EXT2: + return "ext2"; + default: + return "unknown"; + } +} + void vfs_list_mounts() { struct node *iterator = mount_points->head; while (iterator) { struct mount_info *m = iterator->data; - printf("%s on %s type: %s\n", m->dev->name, m->path, m->dev->vfs->name); + printf("%s on %s type %s\n", m->dev->name, m->path, + vfs_resolve_type(m->dev->vfs->type)); iterator = iterator->next; } } +void *vfs_read(char *path) +{ + (void)path; + printf("NOT IMPLEMENTED!\n"); + loop(); + return NULL; +} + +u32 vfs_stat(char *path) +{ + (void)path; + printf("NOT IMPLEMENTED!\n"); + loop(); + return 0; +} + void vfs_install(void) { mount_points = list_new(); @@ -83,12 +118,13 @@ void device_install(void) devices = list_new(); struct vfs *vfs = malloc(sizeof(*vfs)); - vfs->name = strdup("devfs"); + vfs->type = VFS_DEVFS; struct device *dev = malloc(sizeof(*dev)); dev->name = "dev"; dev->vfs = vfs; device_add(dev); vfs_mount(dev, "/dev/"); + printf("%s\n", vfs_mounted("/dev/test")->name); vfs_list_mounts(); } @@ -101,34 +137,35 @@ void *buffer_read(u32 block) return ide_read(malloc(BLOCK_SIZE), block); } -struct superblock *get_superblock(void) +struct ext2_superblock *get_superblock(void) { - struct superblock *sb = buffer_read(EXT2_SUPER); + struct ext2_superblock *sb = buffer_read(EXT2_SUPER); if (sb->magic != EXT2_MAGIC) return NULL; return sb; } -struct bgd *get_bgd(void) +struct ext2_bgd *get_bgd(void) { return buffer_read(EXT2_SUPER + 1); } -struct inode *get_inode(u32 i) +struct ext2_inode *get_inode(u32 i) { - struct superblock *s = get_superblock(); + struct ext2_superblock *s = get_superblock(); assert(s); - struct bgd *b = get_bgd(); + struct ext2_bgd *b = get_bgd(); assert(b); u32 block_group = (i - 1) / s->inodes_per_group; u32 index = (i - 1) % s->inodes_per_group; - u32 block = (index * INODE_SIZE) / BLOCK_SIZE; + u32 block = (index * EXT2_INODE_SIZE) / BLOCK_SIZE; b += block_group; u32 *data = buffer_read(b->inode_table + block); - struct inode *in = - (struct inode *)((u32)data + (index % (BLOCK_SIZE / INODE_SIZE)) * INODE_SIZE); + struct ext2_inode *in = + (struct ext2_inode *)((u32)data + + (index % (BLOCK_SIZE / EXT2_INODE_SIZE)) * EXT2_INODE_SIZE); return in; } @@ -138,7 +175,7 @@ u32 read_indirect(u32 indirect, u32 block_num) return *(u32 *)((u32)data + block_num * sizeof(u32)); } -void *read_inode(struct inode *in) +void *read_inode(struct ext2_inode *in) { assert(in); if (!in) @@ -189,7 +226,7 @@ u32 find_inode(const char *name, u32 dir_inode) if (!dir_inode) return (unsigned)-1; - struct inode *i = get_inode(dir_inode); + struct ext2_inode *i = get_inode(dir_inode); char *buf = malloc(BLOCK_SIZE * i->blocks / 2); memset(buf, 0, BLOCK_SIZE * i->blocks / 2); @@ -199,7 +236,7 @@ u32 find_inode(const char *name, u32 dir_inode) memcpy((u32 *)((u32)buf + q * BLOCK_SIZE), data, BLOCK_SIZE); } - struct dirent *d = (struct dirent *)buf; + struct ext2_dirent *d = (struct ext2_dirent *)buf; u32 sum = 0; do { @@ -210,14 +247,14 @@ u32 find_inode(const char *name, u32 dir_inode) free(buf); return d->inode_num; } - d = (struct dirent *)((u32)d + d->total_len); + d = (struct ext2_dirent *)((u32)d + d->total_len); } while (sum < (1024 * i->blocks / 2)); free(buf); return (unsigned)-1; } -struct inode *find_inode_by_path(char *path) +struct ext2_inode *find_inode_by_path(char *path) { if (path[0] != '/') return 0; @@ -250,18 +287,18 @@ struct inode *find_inode_by_path(char *path) return get_inode(inode); } -void *file_read(char *path) +void *ext2_read(char *path) { - struct inode *in = find_inode_by_path(path); + struct ext2_inode *in = find_inode_by_path(path); if (in) return read_inode(in); else return NULL; } -u32 file_stat(char *path) +u32 ext2_stat(char *path) { - struct inode *in = find_inode_by_path(path); + struct ext2_inode *in = find_inode_by_path(path); if (!in) return 0; diff --git a/kernel/features/load.c b/kernel/features/load.c index d2f1961..b3d5828 100644 --- a/kernel/features/load.c +++ b/kernel/features/load.c @@ -11,7 +11,7 @@ int bin_load(char *path, struct proc *proc) { - char *data = file_read(path); + char *data = vfs_read(path); u32 stack = (u32)malloc(0x2000) + 0x1000; @@ -33,7 +33,7 @@ int elf_verify(struct elf_header *h) // TODO: Fix elf loading void elf_load(char *path, struct proc *proc) { - char *data = file_read(path); + char *data = vfs_read(path); struct elf_header *h = (struct elf_header *)data; if (!elf_verify(h)) { diff --git a/kernel/features/syscall.c b/kernel/features/syscall.c index 9eb9f4b..8ede015 100644 --- a/kernel/features/syscall.c +++ b/kernel/features/syscall.c @@ -34,11 +34,11 @@ void syscall_handler(struct regs *r) break; } case SYS_STAT: { - r->eax = (u32)file_stat((char *)r->ebx); + r->eax = (u32)vfs_stat((char *)r->ebx); break; } case SYS_READ: { - r->eax = (u32)file_read((char *)r->ebx); + r->eax = (u32)vfs_read((char *)r->ebx); break; } case SYS_WRITE: { diff --git a/kernel/inc/fs.h b/kernel/inc/fs.h index e2a3aad..8af28c4 100644 --- a/kernel/inc/fs.h +++ b/kernel/inc/fs.h @@ -24,8 +24,11 @@ void device_install(void); * VFS */ +enum vfs_type { VFS_DEVFS, VFS_TMPFS, VFS_PROCFS, VFS_EXT2 }; + struct vfs { - const char *name; + enum vfs_type type; + int flags; //u8 (*read)(char *, char *, struct device *, void *); //u8 (*mount)(struct device *, void *); }; @@ -37,6 +40,9 @@ struct mount_info { void vfs_install(void); +void *vfs_read(char *path); +u32 vfs_stat(char *path); + /** * EXT2 */ @@ -46,7 +52,7 @@ void vfs_install(void); #define EXT2_ROOT 2 #define EXT2_MAGIC 0x0000EF53 -struct superblock { +struct ext2_superblock { u32 total_inodes; u32 total_blocks; u32 su_res_blocks; // Superuser reserved @@ -74,7 +80,7 @@ struct superblock { u16 res_block_gid; }; -struct bgd { +struct ext2_bgd { u32 block_bitmap; u32 inode_bitmap; u32 inode_table; @@ -85,7 +91,7 @@ struct bgd { u8 bg_reserved[12]; }; -struct inode { +struct ext2_inode { u16 mode; u16 uid; u32 size; @@ -110,9 +116,9 @@ struct inode { u8 os_specific_val2[12]; }; -#define INODE_SIZE (sizeof(struct inode)) +#define EXT2_INODE_SIZE (sizeof(struct ext2_inode)) -struct dirent { +struct ext2_dirent { u32 inode_num; u16 total_len; u8 name_len; @@ -120,15 +126,15 @@ struct dirent { u8 name[]; }; -struct file { - struct inode inode; +struct ext2_file { + struct ext2_inode inode; u32 pos; u8 block_index; u8 *buf; u32 curr_block_pos; }; -void *file_read(char *path); -u32 file_stat(char *path); +void *ext2_read(char *path); +u32 ext2_stat(char *path); #endif |