forked from PoliEcho-holding-org/kern
Made Open() syscall work (badly)
This commit is contained in:
+19
-13
@@ -7,36 +7,42 @@
|
||||
#include <stdint.h>
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
stream->_ptr = NULL; // Idk
|
||||
stream->_file = fd;
|
||||
stream->_oflag = oflag;
|
||||
// TODO: Somehow get the open file table
|
||||
FILE* fp = malloc(sizeof(FILE));
|
||||
fp->_file = fd;
|
||||
|
||||
return stream;
|
||||
return fp;
|
||||
}
|
||||
|
||||
int fclose(FILE* stream) {
|
||||
int res = close(stream->_file);
|
||||
free(stream);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
+3
-2
@@ -6,6 +6,8 @@
|
||||
#include <disk/disk.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#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;
|
||||
|
||||
|
||||
@@ -115,37 +115,20 @@ 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;
|
||||
|
||||
// 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
|
||||
}*/
|
||||
loadFile(inode, fp->_ptr);
|
||||
|
||||
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;
|
||||
|
||||
@@ -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
|
||||
|
||||
+26
-6
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user