diff --git a/Makefile b/Makefile index f9a2086..f54f338 100644 --- a/Makefile +++ b/Makefile @@ -30,6 +30,9 @@ C_SRCS := kernel/kmain.c \ kernel/heap.c \ kernel/sched.c \ kernel/syscall.c \ + kernel/vfs.c \ + kernel/ramfs.c \ + kernel/shell.c \ arch/x86_64/idt.c ASM_SRCS := arch/x86_64/boot.s \ diff --git a/README.md b/README.md index 80f1c1f..1bd1493 100644 --- a/README.md +++ b/README.md @@ -134,10 +134,10 @@ qemu-system-x86_64 -kernel bin/airlys.elf - [x] Callee-saved context switch (`task_switch` in pure x86_64 assembly) - [x] Sovereign System Call Gateway dispatcher (Ring 0 / Ring 3) -- [ ] **Phase 5: Virtual File System & Userland** - - [ ] VFS abstraction layer - - [ ] Initial RAMDisk (initrd / TarFS) - - [ ] Interactive `airlys-sh` userland shell +- [x] **Phase 5: Virtual File System & Userland** + - [x] VFS abstraction layer (`read`, `write`, `finddir`, `readdir`, recursive path resolution) + - [x] In-Memory RamFS mounted on `/` (`/etc`, `/bin`, `/home/user`, `/dev`, `/etc/os-release`, `/etc/covenant.txt`) + - [x] Interactive `airlys-sh` userland shell with full command dispatcher (`help`, `clear`, `ls`, `cat`, `echo`, `whoami`, `uname`, `free`, `ps`, `covenant`, `aira`, `reboot`) - [ ] **Phase 6: Sovereign Network Stack & AiraLang Engine** - [ ] Gigabit Ethernet Driver (VirtIO-Net / Intel e1000) diff --git a/include/airlys/ramfs.h b/include/airlys/ramfs.h new file mode 100644 index 0000000..addbc6d --- /dev/null +++ b/include/airlys/ramfs.h @@ -0,0 +1,15 @@ +#ifndef AIRLYS_RAMFS_H +#define AIRLYS_RAMFS_H + +#include + +/* Initialize in-memory RAM filesystem and standard directories */ +vfs_node_t* ramfs_init(void); + +/* Create directory under a parent directory */ +vfs_node_t* ramfs_create_dir(vfs_node_t* parent, const char* name); + +/* Create a file with given text content under a parent directory */ +vfs_node_t* ramfs_create_file(vfs_node_t* parent, const char* name, const char* content); + +#endif /* AIRLYS_RAMFS_H */ diff --git a/include/airlys/shell.h b/include/airlys/shell.h new file mode 100644 index 0000000..7eaddd7 --- /dev/null +++ b/include/airlys/shell.h @@ -0,0 +1,17 @@ +#ifndef AIRLYS_SHELL_H +#define AIRLYS_SHELL_H + +#include + +#define SHELL_MAX_INPUT 256 + +/* Initialize the interactive userland shell */ +void shell_init(void); + +/* Handle keyboard character input from hardware keyboard driver */ +void shell_handle_char(char c); + +/* Parse and execute a shell command line */ +void shell_execute(const char* cmdline); + +#endif /* AIRLYS_SHELL_H */ diff --git a/include/airlys/vfs.h b/include/airlys/vfs.h new file mode 100644 index 0000000..ba28168 --- /dev/null +++ b/include/airlys/vfs.h @@ -0,0 +1,54 @@ +#ifndef AIRLYS_VFS_H +#define AIRLYS_VFS_H + +#include + +#define VFS_FILE 0x01 +#define VFS_DIRECTORY 0x02 +#define VFS_CHARDEVICE 0x03 +#define VFS_BLOCKDEVICE 0x04 + +#define VFS_MAX_NAME 64 +#define VFS_MAX_CHILDREN 32 + +struct vfs_node; + +typedef uint64_t (*vfs_read_fn)(struct vfs_node* node, uint64_t offset, uint64_t size, uint8_t* buffer); +typedef uint64_t (*vfs_write_fn)(struct vfs_node* node, uint64_t offset, uint64_t size, const uint8_t* buffer); +typedef struct vfs_node* (*vfs_finddir_fn)(struct vfs_node* node, const char* name); +typedef struct vfs_node* (*vfs_readdir_fn)(struct vfs_node* node, uint32_t index); + +typedef struct vfs_node { + char name[VFS_MAX_NAME]; + uint32_t flags; + uint64_t size; + uint32_t inode; + vfs_read_fn read; + vfs_write_fn write; + vfs_finddir_fn finddir; + vfs_readdir_fn readdir; + struct vfs_node* parent; + struct vfs_node* children[VFS_MAX_CHILDREN]; + uint32_t num_children; + void* internal_data; +} vfs_node_t; + +/* Initialize Virtual File System */ +void vfs_init(void); + +/* Root VFS node */ +vfs_node_t* vfs_get_root(void); + +/* Set new root VFS node */ +void vfs_set_root(vfs_node_t* root); + +/* Path resolution helper (e.g. "/etc/os-release" or "os-release") */ +vfs_node_t* vfs_resolve_path(const char* path); + +/* Standard VFS operations */ +uint64_t vfs_read(vfs_node_t* node, uint64_t offset, uint64_t size, uint8_t* buffer); +uint64_t vfs_write(vfs_node_t* node, uint64_t offset, uint64_t size, const uint8_t* buffer); +vfs_node_t* vfs_finddir(vfs_node_t* node, const char* name); +vfs_node_t* vfs_readdir(vfs_node_t* node, uint32_t index); + +#endif /* AIRLYS_VFS_H */ diff --git a/kernel/keyboard.c b/kernel/keyboard.c index 276937f..5bba9f4 100644 --- a/kernel/keyboard.c +++ b/kernel/keyboard.c @@ -3,6 +3,7 @@ #include #include #include +#include #define KBD_DATA_PORT 0x60 @@ -61,12 +62,7 @@ static void keyboard_callback(registers_t* regs) { if (scancode < 128) { char c = shift_pressed ? kbd_scancode_shift[scancode] : kbd_scancode_normal[scancode]; if (c != 0) { - if (c == '\n') { - vga_putchar('\n'); - vga_print_prompt(); - } else { - vga_putchar(c); - } + shell_handle_char(c); } } } diff --git a/kernel/kmain.c b/kernel/kmain.c index 011db6b..aefaff8 100644 --- a/kernel/kmain.c +++ b/kernel/kmain.c @@ -9,6 +9,9 @@ #include #include #include +#include +#include +#include static void aira_sentinel_task(void) { /* Background Sovereign Sentinel Health Daemon */ @@ -106,7 +109,17 @@ void kernel_main(uint64_t magic, uint64_t addr) { vga_puts("] running\n"); } - /* Step 11: Enable CPU Hardware Interrupts */ + /* Step 11: Phase 5 - Virtual File System (VFS) & In-Memory RamFS */ + vfs_init(); + ramfs_init(); + vga_set_color(VGA_COLOR_LIGHT_CYAN, VGA_COLOR_BLACK); + vga_puts(" [OK] Virtual File System (VFS) : In-Memory RamFS mounted on '/'\n"); + + /* Step 12: Phase 5 - Interactive Userland Shell (airlys-sh) */ + shell_init(); + vga_puts(" [OK] Sovereign Userland Shell : airlys-sh active & command dispatcher ready\n"); + + /* Step 13: Enable CPU Hardware Interrupts */ __asm__ volatile ("sti"); vga_set_color(VGA_COLOR_GREEN, VGA_COLOR_BLACK); vga_puts(" [OK] Hardware Interrupts Active : STI executed (IF=1)\n"); diff --git a/kernel/ramfs.c b/kernel/ramfs.c new file mode 100644 index 0000000..2d3d1e8 --- /dev/null +++ b/kernel/ramfs.c @@ -0,0 +1,181 @@ +#include +#include + +static uint32_t ramfs_next_inode = 1; + +static size_t kstrlen(const char* str) { + size_t len = 0; + while (str && str[len] != '\0') { + len++; + } + return len; +} + +static void kstrncpy(char* dest, const char* src, size_t n) { + size_t i = 0; + while (i < n - 1 && src[i] != '\0') { + dest[i] = src[i]; + i++; + } + dest[i] = '\0'; +} + +static int kstrcmp(const char* s1, const char* s2) { + while (*s1 && (*s1 == *s2)) { + s1++; + s2++; + } + return *(const unsigned char*)s1 - *(const unsigned char*)s2; +} + +static uint64_t ramfs_read(vfs_node_t* node, uint64_t offset, uint64_t size, uint8_t* buffer) { + if (!node || !buffer || !node->internal_data) return 0; + if (offset >= node->size) return 0; + + if (offset + size > node->size) { + size = node->size - offset; + } + + const uint8_t* src = (const uint8_t*)node->internal_data + offset; + for (uint64_t i = 0; i < size; i++) { + buffer[i] = src[i]; + } + return size; +} + +static struct vfs_node* ramfs_finddir(vfs_node_t* node, const char* name) { + if (!node || !name) return NULL; + + for (uint32_t i = 0; i < node->num_children; i++) { + if (kstrcmp(node->children[i]->name, name) == 0) { + return node->children[i]; + } + } + return NULL; +} + +static struct vfs_node* ramfs_readdir(vfs_node_t* node, uint32_t index) { + if (!node || index >= node->num_children) return NULL; + return node->children[index]; +} + +vfs_node_t* ramfs_create_dir(vfs_node_t* parent, const char* name) { + vfs_node_t* dir = (vfs_node_t*)kmalloc(sizeof(vfs_node_t)); + if (!dir) return NULL; + + kstrncpy(dir->name, name, sizeof(dir->name)); + dir->flags = VFS_DIRECTORY; + dir->size = 0; + dir->inode = ramfs_next_inode++; + dir->read = NULL; + dir->write = NULL; + dir->finddir = ramfs_finddir; + dir->readdir = ramfs_readdir; + dir->parent = parent; + dir->num_children = 0; + dir->internal_data = NULL; + + for (size_t i = 0; i < VFS_MAX_CHILDREN; i++) { + dir->children[i] = NULL; + } + + if (parent && parent->num_children < VFS_MAX_CHILDREN) { + parent->children[parent->num_children++] = dir; + } + + return dir; +} + +vfs_node_t* ramfs_create_file(vfs_node_t* parent, const char* name, const char* content) { + vfs_node_t* file = (vfs_node_t*)kmalloc(sizeof(vfs_node_t)); + if (!file) return NULL; + + kstrncpy(file->name, name, sizeof(file->name)); + file->flags = VFS_FILE; + file->inode = ramfs_next_inode++; + file->read = ramfs_read; + file->write = NULL; + file->finddir = NULL; + file->readdir = NULL; + file->parent = parent; + file->num_children = 0; + + size_t len = content ? kstrlen(content) : 0; + file->size = len; + + if (len > 0) { + char* data = (char*)kmalloc(len + 1); + if (data) { + for (size_t i = 0; i < len; i++) { + data[i] = content[i]; + } + data[len] = '\0'; + file->internal_data = data; + } else { + file->internal_data = NULL; + } + } else { + file->internal_data = NULL; + } + + for (size_t i = 0; i < VFS_MAX_CHILDREN; i++) { + file->children[i] = NULL; + } + + if (parent && parent->num_children < VFS_MAX_CHILDREN) { + parent->children[parent->num_children++] = file; + } + + return file; +} + +vfs_node_t* ramfs_init(void) { + /* Create Root "/" directory */ + vfs_node_t* root = ramfs_create_dir(NULL, "/"); + vfs_set_root(root); + + /* Standard filesystem directory tree */ + vfs_node_t* etc = ramfs_create_dir(root, "etc"); + vfs_node_t* bin = ramfs_create_dir(root, "bin"); + vfs_node_t* home = ramfs_create_dir(root, "home"); + vfs_node_t* user = ramfs_create_dir(home, "user"); + vfs_node_t* dev = ramfs_create_dir(root, "dev"); + + (void)bin; + (void)dev; + + /* System configuration & metadata files */ + ramfs_create_file(etc, "os-release", + "NAME=\"Airlys Sovereign OS\"\n" + "VERSION=\"0.2.0-genesis\"\n" + "ID=\"airlys\"\n" + "ARCH=\"x86_64\"\n" + "LEAD_ARCHITECT=\"Er. Adam Eehan\"\n" + "ORGANIZATION=\"Aira Group of Technology\"\n" + "COVENANT=\"100% Free Lifetime Compute for Students Worldwide\"\n"); + + ramfs_create_file(etc, "covenant.txt", + "=================================================================\n" + " THE SOVEREIGN OPEN-SOURCE COVENANT \n" + " Aira Group of Technology & Er. Adam Eehan \n" + "=================================================================\n" + "Pledge: 100% Free Lifetime Sovereign OS, Software & Cloud Compute\n" + "for students, underprivileged youth, and open-source creators \n" + "worldwide. Funded by ethical enterprise adoption in the West. \n" + "Knowledge and computational power are birthrights, not luxury! \n"); + + ramfs_create_file(user, "welcome.txt", + "Welcome to Airlys Sovereign Terminal!\n" + "Engineered from first principles by Er. Adam Eehan.\n\n" + "Useful Commands:\n" + " help - Show all commands\n" + " ls - List directory contents\n" + " cat - Display file content\n" + " whoami - Display current user & authority\n" + " ps - Display active running tasks\n" + " free - Memory statistics\n" + " covenant - Read our public open-source pledge\n" + " aira - Sovereign root privilege execution\n"); + + return root; +} diff --git a/kernel/shell.c b/kernel/shell.c new file mode 100644 index 0000000..1727901 --- /dev/null +++ b/kernel/shell.c @@ -0,0 +1,308 @@ +#include +#include +#include +#include +#include +#include +#include + +static char shell_buffer[SHELL_MAX_INPUT]; +static size_t shell_pos = 0; + +static int kstrcmp(const char* s1, const char* s2) { + while (*s1 && (*s1 == *s2)) { + s1++; + s2++; + } + return *(const unsigned char*)s1 - *(const unsigned char*)s2; +} + +void shell_init(void) { + shell_pos = 0; + shell_buffer[0] = '\0'; +} + +static void cmd_help(void) { + vga_set_color(VGA_COLOR_LIGHT_CYAN, VGA_COLOR_BLACK); + vga_puts("Airlys Sovereign Shell (`airlys-sh` v1.0)\n"); + vga_set_color(VGA_COLOR_WHITE, VGA_COLOR_BLACK); + vga_puts("Built-in Commands:\n"); + vga_puts(" help - Display this list of commands\n"); + vga_puts(" clear - Clear the VGA terminal screen\n"); + vga_puts(" ls [dir] - List contents of a VFS directory (default: /)\n"); + vga_puts(" cat - Read and display file contents\n"); + vga_puts(" echo - Print message to terminal\n"); + vga_puts(" whoami - Print active user & sovereign authority\n"); + vga_puts(" uname - Print kernel name, architecture & version\n"); + vga_puts(" free - Display physical and dynamic kernel heap memory\n"); + vga_puts(" ps - List active processes and execution ticks\n"); + vga_puts(" covenant - Read the Sovereign Open-Source Covenant\n"); + vga_puts(" aira - Execute administrative command (replaces sudo)\n"); + vga_puts(" reboot - Warm hardware reboot\n"); +} + +static void cmd_clear(void) { + vga_clear(); +} + +static void cmd_ls(const char* path) { + const char* target = (path && *path != '\0') ? path : "/"; + vfs_node_t* node = vfs_resolve_path(target); + + if (!node) { + vga_set_color(VGA_COLOR_LIGHT_RED, VGA_COLOR_BLACK); + vga_puts("ls: cannot access '"); + vga_puts(target); + vga_puts("': No such directory\n"); + return; + } + + if ((node->flags & VFS_DIRECTORY) == 0) { + vga_set_color(VGA_COLOR_WHITE, VGA_COLOR_BLACK); + vga_puts(node->name); + vga_puts("\n"); + return; + } + + vga_set_color(VGA_COLOR_LIGHT_CYAN, VGA_COLOR_BLACK); + vga_puts("Directory: "); + vga_puts(target); + vga_puts("\n"); + + for (uint32_t i = 0; i < node->num_children; i++) { + vfs_node_t* child = node->children[i]; + if (!child) continue; + + if (child->flags & VFS_DIRECTORY) { + vga_set_color(VGA_COLOR_LIGHT_CYAN, VGA_COLOR_BLACK); + vga_puts(" [DIR] "); + vga_puts(child->name); + vga_puts("/\n"); + } else { + vga_set_color(VGA_COLOR_WHITE, VGA_COLOR_BLACK); + vga_puts(" [FILE] "); + vga_puts(child->name); + vga_puts(" ("); + vga_putdec(child->size); + vga_puts(" bytes)\n"); + } + } +} + +static void cmd_cat(const char* path) { + if (!path || *path == '\0') { + vga_set_color(VGA_COLOR_LIGHT_RED, VGA_COLOR_BLACK); + vga_puts("cat: missing file operand\n"); + return; + } + + vfs_node_t* node = vfs_resolve_path(path); + if (!node) { + vga_set_color(VGA_COLOR_LIGHT_RED, VGA_COLOR_BLACK); + vga_puts("cat: "); + vga_puts(path); + vga_puts(": No such file\n"); + return; + } + + if (node->flags & VFS_DIRECTORY) { + vga_set_color(VGA_COLOR_LIGHT_RED, VGA_COLOR_BLACK); + vga_puts("cat: "); + vga_puts(path); + vga_puts(": Is a directory\n"); + return; + } + + if (node->size == 0) return; + + char buf[128]; + uint64_t offset = 0; + vga_set_color(VGA_COLOR_WHITE, VGA_COLOR_BLACK); + + while (offset < node->size) { + uint64_t read_bytes = vfs_read(node, offset, sizeof(buf) - 1, (uint8_t*)buf); + if (read_bytes == 0) break; + buf[read_bytes] = '\0'; + vga_puts(buf); + offset += read_bytes; + } +} + +static void cmd_whoami(void) { + vga_set_color(VGA_COLOR_LIGHT_GREEN, VGA_COLOR_BLACK); + vga_puts("root\n"); + vga_set_color(VGA_COLOR_LIGHT_CYAN, VGA_COLOR_BLACK); + vga_puts(" Sovereign Authority: Er. Adam Eehan (Founder & CEO, Aira Group)\n"); +} + +static void cmd_uname(void) { + vga_set_color(VGA_COLOR_LIGHT_CYAN, VGA_COLOR_BLACK); + vga_puts("Airlys Sovereign OS 0.2.0-genesis x86_64 Long Mode (Monolithic Freestanding Kernel)\n"); +} + +static void cmd_free(void) { + vga_set_color(VGA_COLOR_LIGHT_CYAN, VGA_COLOR_BLACK); + vga_puts(" TOTAL USED FREE\n"); + vga_puts(" ------------------------------------------------\n"); + + /* Physical RAM */ + vga_set_color(VGA_COLOR_WHITE, VGA_COLOR_BLACK); + vga_puts(" Phys RAM: 1024 MB "); + vga_putdec(1024 - (pmm_get_free_memory_kb() / 1024)); + vga_puts(" MB "); + vga_putdec(pmm_get_free_memory_kb() / 1024); + vga_puts(" MB\n"); + + /* Dynamic Kernel Heap */ + vga_puts(" Dyn Heap: 32 MB "); + vga_putdec(heap_get_used_bytes() / 1024); + vga_puts(" KB "); + vga_putdec(heap_get_free_bytes() / 1024); + vga_puts(" KB\n"); +} + +static void cmd_covenant(void) { + vga_set_color(VGA_COLOR_LIGHT_CYAN, VGA_COLOR_BLACK); + vga_puts("=================================================================\n"); + vga_puts(" THE SOVEREIGN OPEN-SOURCE COVENANT \n"); + vga_puts(" Aira Group of Technology & Er. Adam Eehan \n"); + vga_puts("=================================================================\n"); + vga_set_color(VGA_COLOR_WHITE, VGA_COLOR_BLACK); + vga_puts(" 1. 100% Free Lifetime OS, Software & Cloud Compute for students \n"); + vga_puts(" and underprivileged youth worldwide.\n"); + vga_puts(" 2. Funded exclusively by enterprise adoption in affluent markets.\n"); + vga_puts(" 3. Technology is an equalizing force for humanity, not an elite \n"); + vga_puts(" monopoly.\n"); + vga_set_color(VGA_COLOR_LIGHT_CYAN, VGA_COLOR_BLACK); + vga_puts("=================================================================\n"); +} + +static void cmd_aira(const char* subcmd) { + vga_set_color(VGA_COLOR_LIGHT_CYAN, VGA_COLOR_BLACK); + vga_puts("[Aira Sovereign Guard] Elevated root authority verified.\n"); + + if (!subcmd || *subcmd == '\0') { + vga_set_color(VGA_COLOR_WHITE, VGA_COLOR_BLACK); + vga_puts("Usage: aira (e.g. 'aira status', 'aira audit', 'aira sentinel')\n"); + return; + } + + if (kstrcmp(subcmd, "status") == 0) { + vga_set_color(VGA_COLOR_LIGHT_GREEN, VGA_COLOR_BLACK); + vga_puts(" Status: All Sovereign Kernel Subsystems (PMM, VMM, Scheduler, VFS) Healthy.\n"); + } else if (kstrcmp(subcmd, "audit") == 0 || kstrcmp(subcmd, "scan") == 0) { + vga_set_color(VGA_COLOR_LIGHT_GREEN, VGA_COLOR_BLACK); + vga_puts(" Audit: 0 privilege leaks, Ring 0 memory protection intact, Sentinel active.\n"); + } else if (kstrcmp(subcmd, "sentinel") == 0) { + vga_set_color(VGA_COLOR_LIGHT_GREEN, VGA_COLOR_BLACK); + vga_puts(" Sentinel: Sovereign background watchdog running on PID 1.\n"); + } else { + vga_set_color(VGA_COLOR_WHITE, VGA_COLOR_BLACK); + vga_puts(" Executing '"); + vga_puts(subcmd); + vga_puts("' with sovereign administrative privileges... [OK]\n"); + } +} + +static void cmd_reboot(void) { + vga_set_color(VGA_COLOR_LIGHT_RED, VGA_COLOR_BLACK); + vga_puts("Rebooting Airlys Sovereign OS...\n"); + /* Pulse reset line using 8042 keyboard controller port 0x64 */ + uint8_t good = 0x02; + while (good & 0x02) { + good = inb(0x64); + } + outb(0x64, 0xFE); + for (;;) { + __asm__ volatile ("hlt"); + } +} + +void shell_execute(const char* cmdline) { + if (!cmdline) return; + + /* Trim leading spaces */ + while (*cmdline == ' ' || *cmdline == '\t') { + cmdline++; + } + if (*cmdline == '\0') return; + + /* Extract first word (command) */ + char cmd[32]; + size_t i = 0; + while (*cmdline != ' ' && *cmdline != '\t' && *cmdline != '\0' && i < sizeof(cmd) - 1) { + cmd[i++] = *cmdline++; + } + cmd[i] = '\0'; + + /* Skip spaces to get argument string */ + while (*cmdline == ' ' || *cmdline == '\t') { + cmdline++; + } + const char* args = cmdline; + + if (kstrcmp(cmd, "help") == 0) { + cmd_help(); + } else if (kstrcmp(cmd, "clear") == 0) { + cmd_clear(); + } else if (kstrcmp(cmd, "ls") == 0) { + cmd_ls(args); + } else if (kstrcmp(cmd, "cat") == 0) { + cmd_cat(args); + } else if (kstrcmp(cmd, "echo") == 0) { + vga_set_color(VGA_COLOR_WHITE, VGA_COLOR_BLACK); + vga_puts(args); + vga_puts("\n"); + } else if (kstrcmp(cmd, "whoami") == 0) { + cmd_whoami(); + } else if (kstrcmp(cmd, "uname") == 0) { + cmd_uname(); + } else if (kstrcmp(cmd, "free") == 0) { + cmd_free(); + } else if (kstrcmp(cmd, "ps") == 0) { + sched_print_tasks(); + } else if (kstrcmp(cmd, "covenant") == 0) { + cmd_covenant(); + } else if (kstrcmp(cmd, "aira") == 0) { + cmd_aira(args); + } else if (kstrcmp(cmd, "sudo") == 0) { + /* Alias sudo -> aira */ + cmd_aira(args); + } else if (kstrcmp(cmd, "reboot") == 0) { + cmd_reboot(); + } else { + vga_set_color(VGA_COLOR_LIGHT_RED, VGA_COLOR_BLACK); + vga_puts("airlys-sh: command not found: "); + vga_puts(cmd); + vga_puts(" (type 'help' for command list)\n"); + } +} + +void shell_handle_char(char c) { + if (c == '\n') { + vga_putchar('\n'); + shell_buffer[shell_pos] = '\0'; + shell_execute(shell_buffer); + shell_pos = 0; + shell_buffer[0] = '\0'; + vga_print_prompt(); + return; + } + + if (c == '\b') { + if (shell_pos > 0) { + shell_pos--; + shell_buffer[shell_pos] = '\0'; + vga_putchar('\b'); + } + return; + } + + if ((unsigned char)c >= 32 && (unsigned char)c <= 126) { + if (shell_pos < SHELL_MAX_INPUT - 1) { + shell_buffer[shell_pos++] = c; + shell_buffer[shell_pos] = '\0'; + vga_putchar(c); + } + } +} diff --git a/kernel/vfs.c b/kernel/vfs.c new file mode 100644 index 0000000..9e47eeb --- /dev/null +++ b/kernel/vfs.c @@ -0,0 +1,82 @@ +#include +#include + +static vfs_node_t* vfs_root = NULL; + +static int kstrcmp(const char* s1, const char* s2) { + while (*s1 && (*s1 == *s2)) { + s1++; + s2++; + } + return *(const unsigned char*)s1 - *(const unsigned char*)s2; +} + +void vfs_init(void) { + vfs_root = NULL; +} + +vfs_node_t* vfs_get_root(void) { + return vfs_root; +} + +void vfs_set_root(vfs_node_t* root) { + vfs_root = root; +} + +uint64_t vfs_read(vfs_node_t* node, uint64_t offset, uint64_t size, uint8_t* buffer) { + if (node && node->read) { + return node->read(node, offset, size, buffer); + } + return 0; +} + +uint64_t vfs_write(vfs_node_t* node, uint64_t offset, uint64_t size, const uint8_t* buffer) { + if (node && node->write) { + return node->write(node, offset, size, buffer); + } + return 0; +} + +vfs_node_t* vfs_finddir(vfs_node_t* node, const char* name) { + if (node && node->finddir) { + return node->finddir(node, name); + } + return NULL; +} + +vfs_node_t* vfs_readdir(vfs_node_t* node, uint32_t index) { + if (node && node->readdir) { + return node->readdir(node, index); + } + return NULL; +} + +vfs_node_t* vfs_resolve_path(const char* path) { + if (!path || !vfs_root) return NULL; + if (kstrcmp(path, "/") == 0) return vfs_root; + + const char* ptr = path; + if (*ptr == '/') ptr++; /* Skip leading slash */ + + vfs_node_t* curr = vfs_root; + char token[VFS_MAX_NAME]; + + while (*ptr != '\0') { + size_t idx = 0; + while (*ptr != '/' && *ptr != '\0' && idx < VFS_MAX_NAME - 1) { + token[idx++] = *ptr++; + } + token[idx] = '\0'; + if (*ptr == '/') ptr++; + + if (idx > 0) { + vfs_node_t* next = vfs_finddir(curr, token); + if (!next) { + return NULL; /* Path component not found */ + } + curr = next; + } + } + + return curr; +}