Made Open() syscall work (badly)

This commit is contained in:
2024-08-26 14:43:12 +02:00
parent 105b58a684
commit 9388457774
6 changed files with 59 additions and 48 deletions
+21 -15
View File
@@ -7,36 +7,42 @@
#include <stdint.h> #include <stdint.h>
// Nobody should have to interface with the internals of this structure // Nobody should have to interface with the internals of this structure
typedef struct _iobuf { // 16B typedef struct _iobuf { // 24B
char* _ptr; // Virtual address of file base unsigned char* _ptr; // File buffer base
int _offset; // Offset into file; Seek() unsigned int _size; // Size of buffer
int _file; // File descriptor int _offset; // Offset into file
int _oflag; // Flags used to open file unsigned short _flag; // Open flags
int _file; // File descriptor
char __reserved[6];
} FILE; } FILE;
FILE* stdin; FILE* stdin;
FILE* stdout; FILE* stdout;
FILE* stderr; FILE* stderr;
/* Declarations */
int printf(const char*, ...);
/* --- file I/O --- */ /* --- file I/O --- */
FILE* fopen(const char* filename, const char* mode) { FILE* fopen(const char* filename, const char* mode) {
FILE* stream = malloc(sizeof(FILE));
int oflag = O_RDWR | O_CREAT | O_BIN; // TODO! int oflag = O_RDWR | O_CREAT | O_BIN; // TODO!
int fd = open(filename, oflag); 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 return fp;
stream->_file = fd;
stream->_oflag = oflag;
return stream;
} }
int fclose(FILE* stream) { int fclose(FILE* stream) {
int res = close(stream->_file); int res = close(stream->_file);
free(stream);
return res; return res;
} }
@@ -148,7 +154,7 @@ int printf(const char* fmt, ...) {
case '%': // Just print '%' case '%': // Just print '%'
buffer[len++] = c; buffer[len++] = c;
break; break;
} }
state = 0; state = 0;
} }
} }
+3 -2
View File
@@ -6,6 +6,8 @@
#include <disk/disk.h> #include <disk/disk.h>
#include <stdint.h> #include <stdint.h>
#define MAX_OPEN_FILES 256
superblock_t* impl_superblock = (superblock_t*)SUPERBLOCK_LOC; superblock_t* impl_superblock = (superblock_t*)SUPERBLOCK_LOC;
inode_t* getInode(char* path, dirEntry_t* dentries) { 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; if (strcmp(dentries->name, path) == 0) break;
dentries++; 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; inode_t* inode = (inode_t*)BOOT_FIRST_INODE_LOC + dentries->id;
+6 -23
View File
@@ -115,38 +115,21 @@ int sys_open(sys_regs_t regs) {
loadFile(root, (void*)SCRATCH_BLOCK_LOC); loadFile(root, (void*)SCRATCH_BLOCK_LOC);
inode_t* inode = getInode(path, (dirEntry_t*)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 // TODO: check if max open files have'nt been reached
fd = openFiles++; fd = openFiles++;
FILE* fp = openFileTable + fd; FILE* fp = openFileTable + fd;
fp->_ptr = 0; fp->_ptr = (void*)0xd000;
fp->_offset = 0; fp->_offset = 0;
fp->_size = inode->sizeBytes; fp->_size = inode->sizeBytes;
fp->_flag = oflag; fp->_flag = oflag;
fp->_file = fd; 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; return fd;
} }
@@ -159,7 +142,7 @@ int sys_close(sys_regs_t regs) {
extern uint32_t openFiles; extern uint32_t openFiles;
FILE* fp = openFileTable + fd; FILE* fp = openFileTable + fd;
//free(fp->_ptr); free(fp->_ptr);
openFiles--; openFiles--;
return 0; return 0;
+2 -1
View File
@@ -30,7 +30,7 @@
#define DATETIME_LOC 0x2200 #define DATETIME_LOC 0x2200
// Filesystem Area 0x3000 - 0x3fff // Filesystem Area 0x3000 - 0x3fff
#define FILETABLE_LOC 0x3000 #define OPEN_FILE_TABLE_LOC 0x3000
// Font area 0x4000 - 0x4fff // Font area 0x4000 - 0x4fff
#define FONT_LOC 0x4000 #define FONT_LOC 0x4000
@@ -58,5 +58,6 @@
#define KERNEL_LOC 0x100000 #define KERNEL_LOC 0x100000
#define KERNEL_MALLOC_AREA 0x300000 #define KERNEL_MALLOC_AREA 0x300000
#define MEMMAP_AREA 0x30000 #define MEMMAP_AREA 0x30000
#define OPEN_FILE_LOC 0xd000
#endif #endif
BIN
View File
Binary file not shown.
+27 -7
View File
@@ -62,7 +62,7 @@ void main() {
// Setup kernel malloc variables // Setup kernel malloc variables
mallocNode_t* kernelMallocListHead = 0; mallocNode_t* kernelMallocListHead = 0;
uint32_t kernelMallocVirtualAddr = KERNEL_MALLOC_AREA; 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; uint32_t kernelMallocPages = 1;
mapPage((void*)kernelMallocPhysicalAddr, (void*)kernelMallocVirtualAddr); mapPage((void*)kernelMallocPhysicalAddr, (void*)kernelMallocVirtualAddr);
@@ -176,7 +176,7 @@ void main() {
drawCircle(1500, 600, 300, WHITE); drawCircle(1500, 600, 300, WHITE);
fillCircle(950, 750, 75, rgb(0xa2, 0xd6, 0xf9)); fillCircle(950, 750, 75, rgb(0xa2, 0xd6, 0xf9));
getc(); // Wait until we get a 'q' getc(); // Wait until we get a keystroke
clear(); clear();
} else if (strcmp(argv[0], "help") == 0) { } else if (strcmp(argv[0], "help") == 0) {
printf("Available Commands:\n"); printf("Available Commands:\n");
@@ -233,13 +233,33 @@ void main() {
disableSpeaker(); disableSpeaker();
} else { // TODO: Use cwd for this } else { // TODO: Use cwd for this
char* fileName = argv[0]; char* fileName = argv[0];
//FILE* fp = fopen(fileName, "rb");
FILE* _f = fopen(fileName, "rb");
//if (fp == NULL) { if (_f == NULL) {
printf("Command not found: %s\n", fileName); 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);
} }
} }