Tue, 25 Apr 2023 18:23:17 +0200
6 files changed,
114 insertions(+),
55 deletions(-)
A
include/cedos/drivers/tty.h
@@ -0,0 +1,15 @@
+#ifndef TTY_H +#define TTY_H + +#include "cedos/file.h" +#include "cedos/drivers/keyboard.h" +#include "cedos/drivers/console.h" + +int tty_open(const char *pathname, int flags); +int tty_openat(int fd, const char *fname, int flags); +int tty_read(int fd, char *buffer, uint32_t size); +int tty_write(int fd, char *buffer, uint32_t size); + +extern file_operations_t tty_fops; + +#endif
M
include/cedos/fat.h
→
include/cedos/fat.h
@@ -7,8 +7,10 @@ #include "cedos/file.h"
void FAT_init(); int FAT_dir_next(int fd, int index, char *fname_buffer); -int FAT_openat(FILE *root, FILE *handle, const char *fname, int flags); -uint32_t FAT_read(FILE *file, void *buffer, int count); +int FAT_openat(file_t *root, file_t *handle, const char *fname, int flags); +uint32_t FAT_read(file_t *file, void *buffer, int count); + +extern file_operations_t FAT_fops; #endif
M
include/cedos/file.h
→
include/cedos/file.h
@@ -7,15 +7,26 @@ #include "cedos/mm/paging.h"
typedef uint32_t fpos_t; -typedef struct { - enum { - FILE_STDIO = 0, - FILE_FAT = 1 - } type; +struct file; +struct file_operations; + +typedef struct file file_t; +typedef struct file_operations file_operations_t; + +struct file { + file_operations_t *fops; uint32_t stdio_id; uint32_t fat_cluster; fpos_t pos; -} FILE; +}; + +struct file_operations { + int (*open)(const char *pathname, int flags); + int (*openat)(file_t *root, file_t *handle, const char *fname, int flags); + int (*read)(file_t *file, char *buffer, uint32_t size); + int (*write)(file_t *file, char *buffer, uint32_t size); + int (*dir_next)(int fd, int index, char *fname_buffer); +}; int file_init(); int file_open(const char *pathname, int flags);
A
src/kernel/drivers/tty.c
@@ -0,0 +1,44 @@
+#include "cedos/file.h" +#include "cedos/drivers/tty.h" + +#include "cedos/drivers/console.h" +#include "cedos/drivers/keyboard.h" + +file_operations_t tty_fops = { + tty_open, /* open */ + tty_openat, /* openat */ + tty_read, /* read */ + tty_write, /* write */ + NULL /* dir_next */ +}; + +int tty_open(const char *pathname, int flags) { + +} + +int tty_openat(int fd, const char *fname, int flags) { + +} + +int tty_read(int fd, char *buffer, uint32_t size) { + uint32_t i = 0; + while (i < size) { + char table[] = { + '^', 0x1B, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '\\', '`', 0x08, + '\t', 'q', 'w', 'e', 'r', 't', 'z', 'u', 'i', 'o', 'p', '?', '+', '\n', + 0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', '?', '?', '#', + 0, '<', 'y', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '-', 0, 0, 0, ' ' }; + uint32_t scancode = std_kb->read(); + char c = scancode <= 60 ? table[scancode] : 0; + if (c == 0) { continue; } + buffer[i++] = c; + } + return size; +} + +int tty_write(int fd, char *buffer, uint32_t size) { + for (uint32_t i = 0; i < size; i++) { + std_con->write_c(buffer[i]); + } + return size; +}
M
src/kernel/fat.c
→
src/kernel/fat.c
@@ -1,9 +1,17 @@
-#include "cedos/fat.h" #include "cedos/file.h" +#include "cedos/fat.h" #include "string.h" #include "assert.h" #include <stdint.h> + +file_operations_t FAT_fops = { + NULL, /* open */ + FAT_openat, /* openat */ + FAT_read, /* read */ + NULL, /* write */ + FAT_dir_next /* dir_next */ +}; typedef struct { char jmp[3];@@ -199,12 +207,10 @@ return low | high;
} } -int FAT_openat(FILE *root, FILE *handle, const char *fname, int flags) { +int FAT_openat(file_t *root, file_t *handle, const char *fname, int flags) { int i = 0; // TODO: take fd into consideration (open file in that subdirectory) - assert(root->type == FILE_FAT); - uint16_t first_cluster; while (1) { char buffer[832];@@ -215,16 +221,14 @@ if (i <= 0) { return -1; }
if (strcmp(buffer, fname) == 0) { // file found - handle->type = FILE_FAT; + handle->fops = &FAT_fops; handle->fat_cluster = first_cluster; return 0; } } } -uint32_t FAT_read(FILE *file, void *buffer, int count) { - assert(file->type == FILE_FAT); - +uint32_t FAT_read(file_t *file, void *buffer, int count) { uint16_t cluster = file->fat_cluster; uint32_t size = 0;
M
src/kernel/file.c
→
src/kernel/file.c
@@ -1,8 +1,8 @@
#include "cedos/file.h" #include "cedos/fat.h" -#include "cedos/drivers/console.h" -#include "cedos/drivers/keyboard.h" +#include "cedos/drivers/tty.h" +#include "cedos/core.h" #ifdef DEBUG #define PRINT_DBG(...) printk("[" __FILE__ "] " __VA_ARGS__)@@ -12,24 +12,24 @@ #endif
//const int root_fd = 0x1000; -FILE file_table[256]; +file_t 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].fops = &tty_fops; 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].fops = &tty_fops; 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].fops = &FAT_fops; file_table[next_free].stdio_id = 0; file_table[next_free].fat_cluster = 0; fat_root = next_free++;@@ -51,47 +51,30 @@
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]; + file_t *root = &file_table[fd]; + file_t *handle = &file_table[new_fd]; + + if (root->fops->openat == NULL) { return -1; } - 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; - } + if (root->fops->openat(root, handle, fname, flags)) { return -1; } + + return new_fd; } int file_read(int fd, char *buffer, uint32_t size) { - FILE *file = &file_table[fd]; + file_t *file = &file_table[fd]; - if (file->type == FILE_FAT) { - return FAT_read(file, buffer, size); - } else { - uint32_t i = 0; - while (i < size) { - char table[] = { - '^', 0x1B, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '\\', '`', 0x08, - '\t', 'q', 'w', 'e', 'r', 't', 'z', 'u', 'i', 'o', 'p', '?', '+', '\n', - 0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', '?', '?', '#', - 0, '<', 'y', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '-', 0, 0, 0, ' ' }; - uint32_t scancode = std_kb->read(); - char c = scancode <= 60 ? table[scancode] : 0; - if (c == 0) { continue; } - buffer[i++] = c; - } - return size; - } + if (file->fops->read == NULL) { return -1; } + + file->fops->read(file, buffer, size); } int file_write(int fd, char *buffer, uint32_t size) { - for (uint32_t i = 0; i < size; i++) { - std_con->write_c(buffer[i]); - } - return size; + file_t *file = &file_table[fd]; + + if (file->fops->write == NULL) { return -1; } + + file->fops->write(file, buffer, size); } int file_dir_next(int fd, int index, char *fname_buffer) {