Thu, 20 Apr 2023 22:15:04 +0200
7 files changed,
83 insertions(+),
19 deletions(-)
M
include/cedos/file.h
→
include/cedos/file.h
@@ -5,6 +5,18 @@ #include <stdint.h>
#include "cedos/mm/paging.h" +typedef uint32_t fpos_t; + +typedef struct { + enum { + FILE_STDIO = 0, + FILE_FAT = 1 + } type; + uint32_t stdio_id; + uint32_t fat_cluster; + fpos_t pos; +} FILE; + int file_open(const char *pathname, int flags); int file_openat(int fd, const char *fname, int flags); int file_read(int fd, char *buffer, uint32_t size);
M
run.sh
→
run.sh
@@ -1,1 +1,1 @@
-qemu-system-i386 -drive index=0,if=floppy,format=raw,file=./build/release/cedos.img -m 64 -monitor stdio -no-reboot -d int,cpu_reset,exec,in_asm -vga std -vnc :0 2> log/run_err.log +qemu-system-i386 -drive index=0,if=floppy,format=raw,file=${1} -m 64 -monitor stdio -no-reboot -d int,cpu_reset,exec,in_asm -vga std -vnc :0 2> log/run_err.log
M
src/kernel/elf.c
→
src/kernel/elf.c
@@ -129,6 +129,8 @@ PRINT_DBG("Loading ELF executable \"%s\".\n", fname);
VIRT_ADDR elf_addr = (VIRT_ADDR*)(0xA0000000); // TODO: needs to change when we have other file systems int fd = file_open(fname, 0); + PRINT_DBG("File handle: %i\n", fd); + if (fd == -1) { printk("Executable file not found: %s\n", fname); return -1;
M
src/kernel/fat.c
→
src/kernel/fat.c
@@ -1,5 +1,8 @@
#include "cedos/fat.h" +#include "cedos/file.h" #include "string.h" +#include "assert.h" + #include <stdint.h> typedef struct {@@ -196,15 +199,11 @@ return low | high;
} } -int FAT_openat(int fd, const char *fname, int flags) { +int FAT_openat(FILE *root, FILE *handle, const char *fname, int flags) { int i = 0; // TODO: take fd into consideration (open file in that subdirectory) - - - if (!(fd & 0x1000)) { return -1; } - - fd &= 0x0FFF; + assert(root->type == FILE_FAT); uint16_t first_cluster; while (1) {@@ -216,15 +215,17 @@ if (i <= 0) { return -1; }
if (strcmp(buffer, fname) == 0) { // file found - return first_cluster | 0x1000; + handle->type = FILE_FAT; + handle->fat_cluster = first_cluster; + return 0; } } } -uint32_t FAT_read(int fd, void *buffer, int count) { - if (!(fd & 0x1000)) { return -1; } +uint32_t FAT_read(FILE *file, void *buffer, int count) { + assert(file->type == FILE_FAT); - uint16_t cluster = fd & 0xFFF; + uint16_t cluster = file->fat_cluster; uint32_t size = 0; while (1) {
M
src/kernel/file.c
→
src/kernel/file.c
@@ -4,28 +4,72 @@
#include "cedos/drivers/console.h" #include "cedos/drivers/keyboard.h" -const int root_fd = 0x1000; +#ifdef DEBUG +#define PRINT_DBG(...) printk("[" __FILE__ "] " __VA_ARGS__) +#else +#define PRINT_DBG(...) {} +#endif + +//const int root_fd = 0x1000; + +FILE file_table[256]; +int next_free = 0; + +int stdin, stdout, fat_root; + +int file_init() { + file_table[next_free].type = FILE_STDIO; + file_table[next_free].stdio_id = 0; + file_table[next_free].fat_cluster = 0; + stdin = next_free++; + + file_table[next_free].type = FILE_STDIO; + file_table[next_free].stdio_id = 1; + file_table[next_free].fat_cluster = 0; + stdout = next_free++; + + // FAT root + file_table[next_free].type = FILE_FAT; + file_table[next_free].stdio_id = 0; + file_table[next_free].fat_cluster = 0; + fat_root = next_free++; + + return 0; +} int file_open(const char *pathname, int flags) { while (*pathname == '/') { pathname++; } - return file_openat(0x1000, pathname, flags); + return file_openat(fat_root, pathname, flags); } int file_openat(int fd, const char *fname, int flags) { - if (fd & 0x1000) { - return FAT_openat(fd, fname, flags); + int new_fd = next_free++; + + PRINT_DBG("Given fd: %i\n", fd); + PRINT_DBG("New fd: %i\n", new_fd); + + FILE *root = &file_table[fd]; + FILE *handle = &file_table[new_fd]; + + if (root->type == FILE_FAT) { + PRINT_DBG("fd is FAT\n"); + FAT_openat(root, handle, fname, flags); + return new_fd; } else { + PRINT_DBG("fd is NOT FAT (%i)\n", (int)(root->type)); // open other files return 0; } } int file_read(int fd, char *buffer, uint32_t size) { - if (fd & 0x1000) { - return FAT_read(fd, buffer, size); + FILE *file = &file_table[fd]; + + if (file->type == FILE_FAT) { + return FAT_read(file, buffer, size); } else { uint32_t i = 0; while (i < size) {
M
src/kernel/main.c
→
src/kernel/main.c
@@ -16,6 +16,7 @@ #include "cedos/core.h"
#include "cedos/elf.h" +#include "cedos/file.h" #include "cedos/fat.h" #include "linker.h"@@ -65,7 +66,11 @@ printk("Initializing keyboard...");
ps2_kb.init(); printk("done.\n"); - printk("Initializing root file system..."); + printk("Initializing files..."); + file_init(); + printk("done.\n"); + + printk("Initializing FAT file system..."); FAT_init(); printk("done.\n");