diff --git a/include/c/stdio.h b/include/c/stdio.h index c7f1aaf..1a864aa 100644 --- a/include/c/stdio.h +++ b/include/c/stdio.h @@ -7,36 +7,42 @@ #include // Nobody should have to interface with the internals of this structure -typedef struct _iobuf { // 16B - char* _ptr; // Virtual address of file base - int _offset; // Offset into file; Seek() - int _file; // File descriptor - int _oflag; // Flags used to open file +typedef struct _iobuf { // 24B + unsigned char* _ptr; // File buffer base + unsigned int _size; // Size of buffer + int _offset; // Offset into file + unsigned short _flag; // Open flags + int _file; // File descriptor + + char __reserved[6]; } FILE; FILE* stdin; FILE* stdout; FILE* stderr; +/* Declarations */ +int printf(const char*, ...); + /* --- file I/O --- */ FILE* fopen(const char* filename, const char* mode) { - FILE* stream = malloc(sizeof(FILE)); - int oflag = O_RDWR | O_CREAT | O_BIN; // TODO! int fd = open(filename, oflag); + if (fd < 0) { + printf(""); // TODO: i'd prefer if i didn't need this + return NULL; + } + + // TODO: Somehow get the open file table + FILE* fp = malloc(sizeof(FILE)); + fp->_file = fd; - stream->_ptr = NULL; // Idk - stream->_file = fd; - stream->_oflag = oflag; - - return stream; + return fp; } int fclose(FILE* stream) { int res = close(stream->_file); - free(stream); - return res; } @@ -148,7 +154,7 @@ int printf(const char* fmt, ...) { case '%': // Just print '%' buffer[len++] = c; break; - } + } state = 0; } } diff --git a/include/fs/impl.h b/include/fs/impl.h index a180369..48adabf 100644 --- a/include/fs/impl.h +++ b/include/fs/impl.h @@ -6,6 +6,8 @@ #include #include +#define MAX_OPEN_FILES 256 + superblock_t* impl_superblock = (superblock_t*)SUPERBLOCK_LOC; inode_t* getInode(char* path, dirEntry_t* dentries) { @@ -13,8 +15,7 @@ inode_t* getInode(char* path, dirEntry_t* dentries) { if (strcmp(dentries->name, path) == 0) break; dentries++; } - - if (dentries->name[0] == '\0') return NULL; + if (dentries->name[0] == '\0') { return NULL; } inode_t* inode = (inode_t*)BOOT_FIRST_INODE_LOC + dentries->id; diff --git a/include/interrupt/syscalls.h b/include/interrupt/syscalls.h index 55cc67a..04a372e 100644 --- a/include/interrupt/syscalls.h +++ b/include/interrupt/syscalls.h @@ -115,38 +115,21 @@ int sys_open(sys_regs_t regs) { loadFile(root, (void*)SCRATCH_BLOCK_LOC); inode_t* inode = getInode(path, (dirEntry_t*)SCRATCH_BLOCK_LOC); + if (inode == NULL) { + return -1; + } // TODO: check if max open files have'nt been reached fd = openFiles++; FILE* fp = openFileTable + fd; - fp->_ptr = 0; + fp->_ptr = (void*)0xd000; fp->_offset = 0; fp->_size = inode->sizeBytes; fp->_flag = oflag; fp->_file = fd; - /*fp->_ptr = (unsigned char*)nextAvailableVirtualAddress; + loadFile(inode, fp->_ptr); - // Read file from disk - uint32_t pageCount = BYTES2BLOCKS(inode->sizeBytes); - if (pageCount == 0) { pageCount++; } - - for (size_t i = 0; i < pageCount; i++) { - ptEntry_t page = 0; - uint32_t physAddr = (uint32_t)allocPage(&page); - - if (!mapPage((void*)physAddr, (void*)nextAvailableVirtualAddress)) { - fd = -1; // TODO errno - break; - } - - nextAvailableVirtualAddress += PAGE_SIZE; - } - - if (!loadFile(inode, fp->_ptr)) { - fd = -1; // TODO errno - }*/ - return fd; } @@ -159,7 +142,7 @@ int sys_close(sys_regs_t regs) { extern uint32_t openFiles; FILE* fp = openFileTable + fd; - //free(fp->_ptr); + free(fp->_ptr); openFiles--; return 0; diff --git a/include/memory/addresses.h b/include/memory/addresses.h index a8a87e0..f5ba23b 100644 --- a/include/memory/addresses.h +++ b/include/memory/addresses.h @@ -30,7 +30,7 @@ #define DATETIME_LOC 0x2200 // Filesystem Area 0x3000 - 0x3fff -#define FILETABLE_LOC 0x3000 +#define OPEN_FILE_TABLE_LOC 0x3000 // Font area 0x4000 - 0x4fff #define FONT_LOC 0x4000 @@ -58,5 +58,6 @@ #define KERNEL_LOC 0x100000 #define KERNEL_MALLOC_AREA 0x300000 #define MEMMAP_AREA 0x30000 +#define OPEN_FILE_LOC 0xd000 #endif diff --git a/kern.iso b/kern.iso index 23932b5..7948b03 100644 Binary files a/kern.iso and b/kern.iso differ diff --git a/src/kernel.c b/src/kernel.c index 2f78afe..dc7b9fc 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -62,7 +62,7 @@ void main() { // Setup kernel malloc variables mallocNode_t* kernelMallocListHead = 0; uint32_t kernelMallocVirtualAddr = KERNEL_MALLOC_AREA; - uint32_t kernelMallocPhysicalAddr = (uint32_t)allocBlocks(1); // TODO: Kernel may use more than 4kB? + uint32_t kernelMallocPhysicalAddr = (uint32_t)allocBlocks(16); // TODO: Kernel may use more than 64kB? uint32_t kernelMallocPages = 1; mapPage((void*)kernelMallocPhysicalAddr, (void*)kernelMallocVirtualAddr); @@ -176,7 +176,7 @@ void main() { drawCircle(1500, 600, 300, WHITE); fillCircle(950, 750, 75, rgb(0xa2, 0xd6, 0xf9)); - getc(); // Wait until we get a 'q' + getc(); // Wait until we get a keystroke clear(); } else if (strcmp(argv[0], "help") == 0) { printf("Available Commands:\n"); @@ -233,13 +233,33 @@ void main() { disableSpeaker(); } else { // TODO: Use cwd for this char* fileName = argv[0]; - //FILE* fp = fopen(fileName, "rb"); - - //if (fp == NULL) { + + FILE* _f = fopen(fileName, "rb"); + if (_f == NULL) { printf("Command not found: %s\n", fileName); - // continue; - //} + continue; + } + FILE* fp = openFileTable + _f->_file; + printf("%d: 0x%x Bytes", fp->_file, fp->_size); + for (size_t i = 0; i < fp->_size; i++) { + if (i % 32 == 0) { + if (i != 0) { + size_t oldi = i; + i -= 32; + while (i < oldi) { + char c = fp->_ptr[i++]; + printf("%c", c < 32 ? '.' : c); + } + } + printf("\n%x: ", i); + } + printf("%x ", fp->_ptr[i]); + } + printf("\n"); + + free(_f); + fclose(fp); } }