From 29478baf24532391cd94bd08d94cf867dee976e6 Mon Sep 17 00:00:00 2001 From: James Barnett Date: Sun, 6 Oct 2019 18:39:36 +0100 Subject: Start on ram backed filesystem --- Makefile | 4 +++- os/commands/commands.h | 3 +++ os/commands/echo.c | 2 +- os/commands/echo.h | 1 - os/commands/files.c | 20 ++++++++++++++++++++ os/filesystem/ramfs.c | 27 +++++++++++++++++++++++++++ os/filesystem/ramfs.h | 10 ++++++++++ os/shell/shell.c | 10 ++++++++-- os/stdlib/sdtlib.h | 3 ++- os/stdlib/stdlib.c | 14 +++++++++++++- 10 files changed, 87 insertions(+), 7 deletions(-) create mode 100644 os/commands/commands.h delete mode 100644 os/commands/echo.h create mode 100644 os/commands/files.c create mode 100644 os/filesystem/ramfs.c create mode 100644 os/filesystem/ramfs.h diff --git a/Makefile b/Makefile index a0ae7a6..a62e0cd 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CC = gcc -CFLAGS = -m32 -std=c99 -fno-stack-protector +CFLAGS = -m32 -std=c99 -fno-stack-protector -Wno-builtin-declaration-mismatch default: build @@ -17,6 +17,8 @@ build: setup kernel/kernel.c kernel/global_descriptor_table.c kernel/gpu/text_mo $(CC) $(CFLAGS) -c os/shell/shell.c -o bin/shell.o $(CC) $(CFLAGS) -c os/stdlib/stdlib.c -o bin/stdlib.o $(CC) $(CFLAGS) -c os/commands/echo.c -o bin/echo.o + $(CC) $(CFLAGS) -c os/commands/files.c -o bin/files.o + $(CC) $(CFLAGS) -c os/filesystem/ramfs.c -o bin/ramfs.o ld -m elf_i386 -T link.ld -o bin/kernel.bin bin/*.o clean: diff --git a/os/commands/commands.h b/os/commands/commands.h new file mode 100644 index 0000000..6bc0e7d --- /dev/null +++ b/os/commands/commands.h @@ -0,0 +1,3 @@ +void cmd_cf(char *args); +void cmd_lf(char *args); +void cmd_echo(char *args); \ No newline at end of file diff --git a/os/commands/echo.c b/os/commands/echo.c index 669003e..4a0bcd3 100644 --- a/os/commands/echo.c +++ b/os/commands/echo.c @@ -1,5 +1,5 @@ #include "../../kernel/gpu/text_mode/display.h" -void echo(char *args) { +void cmd_echo(char *args) { scrn_println(args); } \ No newline at end of file diff --git a/os/commands/echo.h b/os/commands/echo.h deleted file mode 100644 index db62a4f..0000000 --- a/os/commands/echo.h +++ /dev/null @@ -1 +0,0 @@ -void echo(char *args); \ No newline at end of file diff --git a/os/commands/files.c b/os/commands/files.c new file mode 100644 index 0000000..6f23e34 --- /dev/null +++ b/os/commands/files.c @@ -0,0 +1,20 @@ +#include "../../kernel/gpu/text_mode/display.h" +#include "../filesystem/ramfs.h" + +void cmd_cf(char *args) { + // todo check args, filname length etc + ramfs_create_file(args); + scrn_print("created file: "); + scrn_println(args); +} + +void cmd_lf(char *args) { + scrn_println("files:"); + File_t* files = ramfs_list_files(); + + for(int i = 0; i <= ramfs_get_tip_file_idx(); i++) { + scrn_print(files[i].name); + scrn_print(" "); + } + scrn_newline(); +} diff --git a/os/filesystem/ramfs.c b/os/filesystem/ramfs.c new file mode 100644 index 0000000..0c0fdff --- /dev/null +++ b/os/filesystem/ramfs.c @@ -0,0 +1,27 @@ +#include "ramfs.h" +#include "../../kernel/gpu/text_mode/display.h" + +#include "../stdlib/sdtlib.h" + +#define RAMDISK_SIZE 102400 // 100 blocks +#define BLOCK_SIZE 1024 +#define MAX_FILES 128 // fixed size until mem management done + +unsigned char ramdisk[RAMDISK_SIZE]; + +File_t files[MAX_FILES]; +int tip_file_idx = -1; + +File_t* ramfs_list_files() { + return files; +} + +int ramfs_get_tip_file_idx() { + return tip_file_idx; +} + +File_t ramfs_create_file(unsigned char *filename) { + int i = ++tip_file_idx; + strcpy(filename, files[i].name); + return files[i]; +} diff --git a/os/filesystem/ramfs.h b/os/filesystem/ramfs.h new file mode 100644 index 0000000..c0dd147 --- /dev/null +++ b/os/filesystem/ramfs.h @@ -0,0 +1,10 @@ +typedef struct +{ + char name[128]; // 128 max filename lentgh + int start_addr; + int end_addr; +} File_t; + +File_t ramfs_create_file(unsigned char * filename); +File_t* ramfs_list_files(); +int ramfs_get_tip_file_idx(); \ No newline at end of file diff --git a/os/shell/shell.c b/os/shell/shell.c index 4fb5488..615c824 100644 --- a/os/shell/shell.c +++ b/os/shell/shell.c @@ -1,7 +1,7 @@ #include "../../os/main.h" #include "../../kernel/gpu/text_mode/display.h" #include "../stdlib/sdtlib.h" -#include "../commands/echo.h" +#include "../commands/commands.h" #define CMD_BUFFER_SIZE 80 @@ -71,7 +71,13 @@ unsigned char* shell_parse_args() { void shell_exec_cmd(unsigned char* cmd_name, unsigned char* args) { // TODO lookup string to funciton pointer if (strcmp(cmd_name, "echo") == 0) { - echo(args); + cmd_echo(args); + } + else if(strcmp(cmd_name, "cf") == 0) { + cmd_cf(args); + } + else if(strcmp(cmd_name, "lf") == 0) { + cmd_lf(args); } else if (strcmp(cmd_name, "") != 0) { scrn_print(cmd_name); diff --git a/os/stdlib/sdtlib.h b/os/stdlib/sdtlib.h index 85073d2..cdb1aff 100644 --- a/os/stdlib/sdtlib.h +++ b/os/stdlib/sdtlib.h @@ -1,2 +1,3 @@ char* int_to_str(int i); -int strcmp(const char *str1, const char *str2); \ No newline at end of file +int strcmp(const char *str1, const char *str2); +void strcpy(const char* src, char* dest); \ No newline at end of file diff --git a/os/stdlib/stdlib.c b/os/stdlib/stdlib.c index bdb2242..250e9b4 100644 --- a/os/stdlib/stdlib.c +++ b/os/stdlib/stdlib.c @@ -23,7 +23,7 @@ char* int_to_str(int i) int strcmp(const char *str1, const char *str2) { - while(*str1){ + while(*str1) { if (*str1 != *str2) { break; } @@ -32,4 +32,16 @@ int strcmp(const char *str1, const char *str2) } return *(const unsigned char*)str1 - *(const unsigned char*)str2; +} + +void strcpy(const char *src, char * dest) { + char *ptr = dest; + + while (*src != '\0') { + *dest = *src; + dest++; + src++; + } + + *dest = '\0'; } \ No newline at end of file -- cgit v1.2.3