CeDOS - Commit d3848641

FAT functions now integrated into general file functions
Celina Kalus
Mon, 27 Mar 2023 15:02:29 +0200
6 files changed, 52 insertions(+), 47 deletions(-)
M include/cedos/file.hinclude/cedos/file.h

@@ -5,6 +5,8 @@ #include <stdint.h>

#include "cedos/mm/paging.h" +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); int file_write(int fd, char *buffer, uint32_t size);
M src/kernel/elf.csrc/kernel/elf.c

@@ -2,7 +2,7 @@ #include <stdint.h>

#include "cedos/elf.h" #include "cedos/core.h" -#include "cedos/fat.h" +#include "cedos/file.h" #include "cedos/sched/process.h" #include "assert.h"

@@ -128,9 +128,9 @@ PROCESS_ID elf_exec(const char *fname, char *args) {

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 = FAT_openat(0, fname, 0); + int fd = file_open(fname, 0); assert(fd != -1); - int size = FAT_read(fd, elf_addr, 0); + int size = file_read(fd, elf_addr, 0); assert(size != 0); ELF_HEADER *header = (ELF_HEADER*)(elf_addr);
M src/kernel/fat.csrc/kernel/fat.c

@@ -188,6 +188,11 @@ int FAT_openat(int fd, 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; uint16_t first_cluster; while (1) {

@@ -199,13 +204,15 @@ if (i <= 0) { return -1; }

if (strcmp(buffer, fname) == 0) { // file found - return first_cluster; + return first_cluster | 0x1000; } } } uint32_t FAT_read(int fd, void *buffer, int count) { - uint16_t cluster = fd; + if (!(fd & 0x1000)) { return -1; } + + uint16_t cluster = fd & 0xFFF; uint32_t size = 0; while (1) {
M src/kernel/file.csrc/kernel/file.c

@@ -3,18 +3,40 @@

#include "cedos/drivers/console.h" #include "cedos/drivers/keyboard.h" +const int root_fd = 0x1000; + +int file_open(const char *pathname, int flags) { + while (*pathname == '/') { + pathname++; + } + + return file_openat(0x1000, pathname, flags); +} + +int file_openat(int fd, const char *fname, int flags) { + if (fd & 0x1000) { + return FAT_openat(fd, fname, flags); + } else { + // open other files + } +} + int file_read(int fd, char *buffer, uint32_t size) { - int 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', ',', '.', '-'}; - uint32_t scancode = std_kb->read(); - char c = scancode >= 0 && scancode <= 60 ? table[scancode] : 0; - if (c == 0) { continue; } - buffer[i++] = c; + if (fd & 0x1000) { + return FAT_read(fd, buffer, size); + } else { + int 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 >= 0 && scancode <= 60 ? table[scancode] : 0; + if (c == 0) { continue; } + buffer[i++] = c; + } } }
M src/kernel/main.csrc/kernel/main.c

@@ -62,6 +62,10 @@ printk("Initializing keyboard...");

ps2_kb.init(); printk("done.\n"); + printk("Initializing root file system..."); + FAT_init(); + printk("done."); + printk("Initialization finished.\n--------------\n");

@@ -142,36 +146,6 @@ }

int os_main(void) { infodump(); - - FAT_init(); - - int i = 0; - - while (1) { - char buffer[832]; - uint16_t first_cluster; - uint32_t file_size; - - i = FAT_root_dir_next(i, buffer, &first_cluster, &file_size); - if (i <= 0) { break; } - - PRINT_DBG("%s (start: %i, size: %i)\n", buffer, first_cluster, file_size); - - uint16_t cluster = first_cluster; - - if (cluster == 0xFFF || cluster == 0x000) { continue; } - - PRINT_DBG(" clusters: \n"); - while (1) { - PRINT_DBG(" %x\n", cluster); - - //char *sect = FAT_read_cluster(cluster); - //hexdump(sect, 512 * 8); - - cluster = FAT_next_cluster(cluster); - if (cluster == 0xFFF || cluster == 0x000) { break; } - } - } // create test tasks printk("Creating tasks.\n");
M src/kernel/sched/sched.csrc/kernel/sched/sched.c

@@ -55,7 +55,7 @@ PROCESS_ID sched_spawn(const char *name, char *args) {

crit_enter(); if (name != NULL) { - int fd = FAT_openat(0, name, 0); + int fd = file_open(name, 0); if (fd == -1) { return -1; } }