diff --git a/filesystem/Makefile b/filesystem/Makefile index 68e494a..1bea9fd 100644 --- a/filesystem/Makefile +++ b/filesystem/Makefile @@ -25,11 +25,6 @@ vfs/%.o: vfs/%.c initrd/%.o: initrd/%.c $(CC) $(FLAGS) -c $^ -o $@ -generator: filesystem_generator.c - gcc filesystem_generator.c -o filesystem_generator - ./filesystem_generator - rm -rf filesystem_generator - clean: rm -rf $(VFS_OBJ) rm -rf $(IRD_OBJ) diff --git a/filesystem/filesystem_generator.c b/filesystem/filesystem_generator.c deleted file mode 100644 index 85cebb5..0000000 --- a/filesystem/filesystem_generator.c +++ /dev/null @@ -1,52 +0,0 @@ -#include -#include -#include -#include - -struct initrd_header { - uint8_t magic; - char name[64]; - uint32_t offset; - uint32_t len; -}; - -int main(int argc, char **argv) { - int nheaders = (argc - 1) / 2; - struct initrd_header headers[64]; - printf("size of headers: %ld\n", sizeof(struct initrd_header)); - uint32_t off = sizeof(struct initrd_header) * 64 + sizeof(int); - size_t i; - for (i = 0; i < nheaders; i++) { - printf("writing file %s->%s at 0x%x\n", argv[i * 2 + 1], argv[i * 2 + 2], off); - strcpy(headers[i].name, argv[i * 2 + 2]); - headers[i].offset = off; - FILE* stream =fopen(argv[i * 2 + 1], "r"); - if (!stream) { - printf("Error: file not found: %s\n", argv[i * 2 + 1]); - return 1; - } - fseek(stream, 0, SEEK_END); - headers[i].len = ftell(stream); - off += headers[i].len; - fclose(stream); - headers[i].magic = 0xBF; - } - - FILE* wstream = fopen("./initrd.img", "w"); - uint8_t* data = (uint8_t*)malloc(off); - fwrite(&nheaders, sizeof(uint32_t), 1, wstream); - fwrite(headers, sizeof(struct initrd_header), 64, wstream); - - for (i = 0; i < nheaders; i++) { - FILE* stream = fopen(argv[i * 2 + 1], "r"); - uint8_t* buf = (uint8_t*)malloc(headers[i].len); - fread(buf, 1, headers[i].len, stream); - fwrite(buf, 1, headers[i].len, wstream); - fclose(stream); - free(buf); - } - - fclose(wstream); - free(data); - return 0; -} diff --git a/filesystem/initrd/initrd.c b/filesystem/initrd/initrd.c index 7b39cb4..4f02dc8 100644 --- a/filesystem/initrd/initrd.c +++ b/filesystem/initrd/initrd.c @@ -1,13 +1,13 @@ #include "initrd.h" -initrd_header_t *initrd_header; // The header. -initrd_file_header_t *file_headers; // The list of file headers. -fs_node_t *initrd_root; // Our root directory node. -fs_node_t *initrd_dev; // We also add a directory node for /dev, so we can mount devfs later on. -fs_node_t *root_nodes; // List of file nodes. -int nroot_nodes; // Number of file nodes. +initrd_header_t *initrd_header; // The header. +initrd_file_header_t *file_headers; // The list of file headers. +PFILE initrd_root; // Our root directory node. +PFILE initrd_dev; // We also add a directory node for /dev, so we can mount devfs later on. +PFILE root_nodes; // List of file nodes. +uint32_t nroot_nodes; // Number of file nodes. -dir_entry_t dirent; +dentry dir_entry; static uint32_t initrd_read(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffer) { initrd_file_header_t header = file_headers[node->inode]; @@ -17,7 +17,7 @@ static uint32_t initrd_read(fs_node_t *node, uint32_t offset, uint32_t size, return size; } -static dir_entry_t *initrd_readdir(fs_node_t *node, uint32_t index) { +static dentry *initrd_readdir(fs_node_t *node, uint32_t index) { if (node == initrd_root && index == 0) { strcpy(dirent.name, "dev"); diff --git a/filesystem/vfs/vfs.c b/filesystem/vfs/vfs.c index 2641ad1..435f7a5 100644 --- a/filesystem/vfs/vfs.c +++ b/filesystem/vfs/vfs.c @@ -1,31 +1,28 @@ #include "vfs.h" -struct fs_node_s *fs_root = NULL; +PFILE root; -uint32_t fs_write(struct fs_node_s* node, uint32_t offset, uint32_t size, uint8_t* buffer) { - if (!node->write) return node->write(node, offset, size, buffer); +uint32_t fs_read(PFILE node, uint32_t offset, uint32_t size, char* buffer) { + if (node->node_operations.read) return node->node_operations.read(node, offset, size, buffer); return 0; } -uint32_t fs_read(struct fs_node_s* node, uint32_t offset, uint32_t size, uint8_t* buffer) { - if (!node->read) return node->read(node, offset, size, buffer); +uint32_t fs_write(PFILE node,uint32_t offset, uint32_t size, char* buffer) { + if (node->node_operations.write) return node->node_operations.write(node, offset, size, buffer); return 0; } -void fs_open(struct fs_node_s* node, uint8_t read, uint8_t write) { - if (!node->open) (node->open(node)); -} - -void fs_close(struct fs_node_s* node) { - if (!node->close) (node->close(node)); +uint32_t fs_open(PFILE node, enum open_flags flags) { + if (node->node_operations.open) return node->node_operations.open(node, flags); + return 0; } -dir_entry_t* readdir_fs(struct fs_node_s* node, uint32_t index) { - if (!node->readdir && (node->flags & 7 == FS_DIRECTORY)) return node->readdir(node, index); - return NULL; +uint32_t fs_close(PFILE node) { + if (node->node_operations.close) return node->node_operations.close(node); + return 0; } -struct fs_node_s* finddir_fs(struct fs_node_s* node, char* name) { - if (!node->finddir && (node->flags & 7 == FS_DIRECTORY)) return node->finddir(node, name); +dentry* fs_readdir(PFILE node, uint32_t index) { + if ((node->type & FS_DIR) && node->node_operations.readdir) return node->readdir(node, index); return NULL; } diff --git a/filesystem/vfs/vfs.h b/filesystem/vfs/vfs.h index 0d72abb..3efd1a6 100644 --- a/filesystem/vfs/vfs.h +++ b/filesystem/vfs/vfs.h @@ -1,58 +1,52 @@ #ifndef VFS_H #define VFS_H -#include +#include #include #include "../../lib/string/string.h" #include "../../memory/vmm/vmm.h" -enum fs_flags { - FS_FILE = 1, - FS_DIRECTORY = 2, - FS_CHARDEVICE = 3, - FS_BLOCKDEVICE = 4, - FS_PIPE = 5, - FS_SYMLINK = 6, - FS_MOUNTPOINT = 8 +typedef enum node_type { + FS_FILE, + FS_DIR, + FS_CHARDEVICE, + FS_BLOCKDEVICE, + FS_PIPE, + FS_SYMLINK, + FS_MOUNTPOINT }; -typedef uint32_t (*rw_t)(struct fs_node_s*, uint32_t, uint32_t, uint8_t*); -typedef void (*oc_t)(struct fs_node_s*); -typedef struct dir_entry_s * (*readdir_t)(struct fs_node_s*, uint32_t); -typedef struct fs_node_s * (*finddir_t)(struct fs_node_s*, char* name); - -typedef struct fs_node_s { - char name[128]; // Should move it to directory node - uint32_t permissions; - uint32_t uid; - uint32_t gid; - uint32_t flags; - uint32_t inode; // Provide to a filesystem a way to identify nodes - uint32_t len; - uint32_t impl; // Implementation-defined number - // These are pointers to callbacks - rw_t read; - rw_t write; - oc_t open; - oc_t close; - readdir_t readdir; - finddir_t finddir; - struct fs_node_s *ptr; // For symlink and mountpoint -} fs_node_t; - -typedef struct dir_entry_s { - char name[128]; - uint32_t inode; -} dir_entry_t; - - -uint32_t fs_write(struct fs_node_s* node, uint32_t offset, uint32_t size, uint8_t* buffer); -uint32_t fs_read(struct fs_node_s* node, uint32_t offset, uint32_t size, uint8_t* buffer); -void fs_open(struct fs_node_s* node, uint8_t read, uint8_t write); -void fs_close(struct fs_node_s* node); -dir_entry_t* readdir_fs(struct fs_node_s* node, uint32_t index); -struct fs_node_s* finddir_fs(struct fs_node_s*, char* name); +typedef enum open_flags { + READ, + WRITE +}; -extern fs_node_t* fs_root; +typedef struct _dentry { + char name[32]; + uint32_t inode; +} dentry; + +typedef struct _FILE { + char name[32]; + uint32_t permissions; + enum node_type type; + uint32_t uid; + uint32_t sid; + uint32_t inode; + uint32_t length; + struct node_operations { + uint32_t (*read)(struct FILE*,uint32_t,uint32_t,char*buffer); + uint32_t (*write)(struct FILE*,uint32_t,uint32_t,char*buffer); + uint32_t (*open)(struct FILE*, enum open_flags); + uint32_t (*close)(struct FILE*); + dentry* (*readdir)(struct FILE*,uint32_t); + }; +} FILE, *PFILE; + +uint32_t fs_close(PFILE); +uint32_t fs_write(PFILE,uint32_t,uint32_t,char*buffer); +uint32_t fs_read(PFILE,uint32_t,uint32_t,char*buffer); +uint32_t fs_open(PFILE, enum open_flags); +dentry* fs_readdir(PFILE,uint32_t); #endif diff --git a/kernel/kernel.c b/kernel/kernel.c index 70ad409..cd12ab3 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -31,8 +31,8 @@ void kernel_main(uint32_t magic, uint32_t addr) { multiboot_info_t* mbi = (multiboot_info_t*)addr; struct multiboot_mmap_entry* region = (struct multiboot_mmap_entry*) mbi->mmap_addr; uint32_t mem_size = MAX_MEMORY_SIZE; - uint32_t initrd_location = *(uint32_t*)(mbi->mods_addr); - uint32_t initrd_end = *(uint32_t*)(mbi->mods_addr + 4); +// uint32_t initrd_location = *(uint32_t*)(mbi->mods_addr); +// uint32_t initrd_end = *(uint32_t*)(mbi->mods_addr + 4); init_current_screen(BLUE, WHITE); term_clear(); @@ -54,6 +54,6 @@ void kernel_main(uint32_t magic, uint32_t addr) { asm volatile("mov %%cr0, %0" : "=r" (cr0)); if (cr0 & 0x80000000) printf("Paging enabled: cr0 == %p\n", cr0); else printf("Paging disabled: cr0 == %p\n", cr0); - if (!mbi->mods_count) printf("no mods added\n"); - fs_root = initialise_initrd(initrd_location); +// if (!mbi->mods_count) printf("no mods added\n"); +// fs_root = initialise_initrd(initrd_location); }