mirror of
https://github.com/Tom-on64/kern.git
synced 2026-11-03 18:22:22 +00:00
Commited some old changes, idk what they were
This commit is contained in:
+31
-13
@@ -1,23 +1,41 @@
|
||||
#ifndef SYSCALLNUMS_H
|
||||
#define SYSCALLNUMS_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define MAX_SYSCALLS 5
|
||||
|
||||
typedef enum {
|
||||
SYS_SLEEP,
|
||||
SYS_WRITE,
|
||||
SYS_GETS,
|
||||
SYS_MALLOC,
|
||||
SYS_FREE,
|
||||
} SYSCALL_NUMBERS;
|
||||
#define MAX_SYSCALLS 9
|
||||
|
||||
// TODO: Add malloc() and free() syscall wrappers??
|
||||
int32_t write(int32_t fd, const void* buf, uint32_t len) {
|
||||
int32_t result = -1;
|
||||
|
||||
typedef enum {
|
||||
SYS_RESERVED,
|
||||
SYS_SLEEP,
|
||||
SYS_GETS,
|
||||
SYS_WRITE,
|
||||
SYS_OPEN,
|
||||
SYS_CLOSE,
|
||||
SYS_MALLOC,
|
||||
SYS_FREE,
|
||||
SYS_SEEK,
|
||||
} SYSCALL_NUMBERS;
|
||||
|
||||
typedef enum {
|
||||
O_CREAT = 0x1, // Create if does not exist
|
||||
O_RDONLY = 0x2, // Read only
|
||||
O_WRONLY = 0x4, // Write only
|
||||
O_RDWR = 0x8, // Read and write
|
||||
O_APPEND = 0x10, // Always writes at the end (appends)
|
||||
O_TRUNC = 0x20, // Truncate file size and position to 0 on Open
|
||||
} OFLAGS;
|
||||
|
||||
int write(int fd, const void* buf, unsigned int len) {
|
||||
int result = -1;
|
||||
__asm__ volatile ("int $0x80" : "=a"(result) : "a"(SYS_WRITE), "b"(fd), "c"(buf), "d"(len));
|
||||
return result;
|
||||
}
|
||||
|
||||
int open(const char* path, int oflag) {
|
||||
int result = -1;
|
||||
__asm__ volatile ("int $0x80" : "=a"(result) : "a"(SYS_OPEN), "b"(path), "c"(oflag));
|
||||
return result;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,80 +0,0 @@
|
||||
#ifndef FILESYS_H
|
||||
#define FILESYS_H
|
||||
|
||||
#include <disk/disk.h>
|
||||
#include <memory/addresses.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct {
|
||||
char filename[10];
|
||||
char filetype[3];
|
||||
uint8_t reserved;
|
||||
uint8_t startAddress;
|
||||
uint8_t length;
|
||||
} __attribute__ ((packed)) fileEntry_t;
|
||||
|
||||
char* filetable = (char*)FILETABLE_LOC; // Pretty sure we don't have to care about the bootloader at 0x7c00
|
||||
|
||||
fileEntry_t* findFile(const char* filename) {
|
||||
if (strlen(filename) > 14) { return NULL; } // Filename too long
|
||||
static char filenameBuf[16];
|
||||
strcpy(filenameBuf, filename);
|
||||
|
||||
char* filetype = strchr(filenameBuf, '.');
|
||||
*filetype++ = '\0'; // Split filename and filetype by a NULL
|
||||
|
||||
char* ft = filetable;
|
||||
while (*ft != '\0') {
|
||||
if (strcmp(filenameBuf, ft) != 0) { // Filename doesn't match
|
||||
ft += 16; // Goto next entry
|
||||
continue;
|
||||
}
|
||||
ft += 10; // Goto filetype
|
||||
|
||||
if (strcmp(filetype, ft) != 0) {
|
||||
ft += 6; // Goto next entry
|
||||
continue;
|
||||
}
|
||||
ft -= 10; // Go back to start of entry
|
||||
|
||||
return (fileEntry_t*)ft;
|
||||
}
|
||||
|
||||
return NULL; // No such file
|
||||
}
|
||||
|
||||
void updateFiletable() {
|
||||
// TODO: Update the filetable by writing data from memory to disk!
|
||||
}
|
||||
|
||||
uint8_t readFile(char* filename, void* buffer) { // Hopefully the buffer is large enought :/
|
||||
fileEntry_t* file = findFile(filename);
|
||||
if (file == NULL) { return 1; } // File doesn't exist!
|
||||
|
||||
diskRead(file->startAddress, file->length, buffer);
|
||||
return 0; // Success
|
||||
}
|
||||
|
||||
uint8_t writeFile(char* filename, void* buffer) { // TODO: Add length so we don't write too much/too little
|
||||
fileEntry_t* file = findFile(filetable);
|
||||
if (file == NULL) { // File exists -> override
|
||||
// File doesn't exist -> create new file
|
||||
file = (fileEntry_t*)filetable;
|
||||
while (*(char*)file != '\0') { file++; } // Find end of the filetable
|
||||
|
||||
*strchr(filename, '.') = '\0';
|
||||
|
||||
char* filetype = filename + strlen(filename); // + 1?
|
||||
fileEntry_t* previous = file - 1;
|
||||
|
||||
strcpy(file->filename, filename);
|
||||
strcpy(file->filetype, filetype);
|
||||
file->startAddress = previous->startAddress + previous->length;
|
||||
file->length = 1; // TODO: Add lenght! Currently will just assume 1 sector :/
|
||||
}
|
||||
|
||||
diskWrite(file->startAddress, file->length, buffer); // Write the file
|
||||
return 0; // Success
|
||||
}
|
||||
|
||||
#endif
|
||||
+6
-7
@@ -27,11 +27,11 @@ typedef struct {
|
||||
typedef struct {
|
||||
uint32_t inodeCount;
|
||||
|
||||
uint16_t firstInodeBitmapBlock;
|
||||
uint16_t firstINodeBitmapBlock;
|
||||
uint16_t firstDataBitmapBlock;
|
||||
uint16_t inodeBitmapBlockCount;
|
||||
uint16_t dataBitmapBlockCount;
|
||||
uint32_t firstInodeBlock;
|
||||
uint32_t firstINodeBlock;
|
||||
uint32_t firstDataBlock;
|
||||
uint16_t inodeBlockCount;
|
||||
uint16_t dataBlockCount;
|
||||
@@ -40,14 +40,14 @@ typedef struct {
|
||||
uint16_t blockSize; // in bytes
|
||||
uint8_t inodeSize; // in bytes
|
||||
|
||||
uint32_t rootInodePtr;
|
||||
uint32_t rootINodePtr;
|
||||
uint8_t inodesPerBlock;
|
||||
uint8_t directExtentsPerInode;
|
||||
uint8_t directExtentsPerINode;
|
||||
uint16_t extentsPerIndirectBlock;
|
||||
uint32_t firstFreeInodeBit;
|
||||
uint32_t firstFreeINodeBit;
|
||||
uint32_t firstFreeDataBit;
|
||||
uint16_t deviceNumber;
|
||||
uint8_t firstUnreservedInode;
|
||||
uint8_t firstUnreservedINode;
|
||||
|
||||
uint8_t reserved[14];
|
||||
} __attribute__ ((packed)) superBlock_t;
|
||||
@@ -87,5 +87,4 @@ uint32_t bytesToSectors(uint32_t bytes) {
|
||||
if (bytes <= FS_SECTOR_SIZE) return 1;
|
||||
return (bytes / FS_SECTOR_SIZE) + ((bytes % FS_SECTOR_SIZE) > 0 ? 1 : 0);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
#ifndef IMPL_H
|
||||
#define IMPL_H
|
||||
|
||||
#include <disk/disk.h>
|
||||
#include <fs/fs.h>
|
||||
#include <memory/addresses.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
char* currentDir;
|
||||
inode_t currentDirINode;
|
||||
|
||||
char tempSector[FS_SECTOR_SIZE];
|
||||
char tempBlock[FS_BLOCK_SIZE];
|
||||
|
||||
inode_t rootINode;
|
||||
|
||||
inode_t* getINode(char* path) {
|
||||
dirEntry_t* dirEntry = (dirEntry_t*)SCRATCH_BLOCK_LOC;
|
||||
while (dirEntry->name[0] != '\0') {
|
||||
if (strcmp(dirEntry->name, path) == 0) break;
|
||||
dirEntry++;
|
||||
}
|
||||
|
||||
if (dirEntry->name[0] == '\0') return NULL;
|
||||
|
||||
inode_t* inode = (inode_t*)BOOT_FIRST_INODE_LOC;
|
||||
while (inode->id != dirEntry->id) {
|
||||
inode++;
|
||||
}
|
||||
|
||||
return inode;
|
||||
}
|
||||
|
||||
uint8_t loadFile(inode_t* inode, void* dest) {
|
||||
uint32_t totalBlocks = bytesToBlocks(inode->sizeBytes);
|
||||
uint32_t addressOffset = 0;
|
||||
|
||||
for (uint8_t i = 0; i < 4 && totalBlocks > 0; i++) {
|
||||
diskRead(inode->extent[i].firstBlock * 8, inode->extent[i].length * 8, dest + addressOffset);
|
||||
addressOffset += inode->extent[i].length * FS_BLOCK_SIZE;
|
||||
totalBlocks -= inode->extent[i].length;
|
||||
}
|
||||
|
||||
if (totalBlocks > 0) {
|
||||
// TODO: Indirect extents
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -9,8 +9,12 @@
|
||||
#include <memory/addresses.h>
|
||||
#include <syscall.h>
|
||||
#include <stdint.h>
|
||||
#include <fs/fs.h>
|
||||
|
||||
/*
|
||||
* TODO WARNING - Update MAX_SYSCALLS in the handler TODO
|
||||
*/
|
||||
|
||||
// Syscall(0) - Sleep()
|
||||
// Args: ebx - number of miliseconds
|
||||
void sys_sleep() {
|
||||
__asm__ volatile ("movl %%ebx, %0" : "=r"(*sleepTimerTicks));
|
||||
@@ -20,7 +24,6 @@ void sys_sleep() {
|
||||
}
|
||||
}
|
||||
|
||||
// Syscall(1) - Write()
|
||||
// Args: ebx - int fd
|
||||
// ecx - void* buf
|
||||
// edx - uint32_t len
|
||||
@@ -48,7 +51,6 @@ void sys_gets() {
|
||||
read(s);
|
||||
}
|
||||
|
||||
// Syscall(3) - Malloc()
|
||||
// Args: ebx - amount of bytes to malloc
|
||||
// Returns: eax - Pointer to allocated memory
|
||||
void sys_malloc() {
|
||||
@@ -67,7 +69,6 @@ void sys_malloc() {
|
||||
__asm__ volatile ("movl %0, %%eax" : : "r"(ptr));
|
||||
}
|
||||
|
||||
// Syscall(4) - Free()
|
||||
// Args: ebx - allocated pointer
|
||||
void sys_free() {
|
||||
void* ptr = NULL;
|
||||
@@ -75,13 +76,39 @@ void sys_free() {
|
||||
mallocFree(ptr);
|
||||
}
|
||||
|
||||
// Args: ebx - path, ecx - oflag
|
||||
void sys_open() {
|
||||
int32_t fd = -1;
|
||||
char* path = NULL;
|
||||
uint32_t oflag = 0;
|
||||
|
||||
// From kernel.c
|
||||
//extern openFileTable_t* openFileTable;
|
||||
//extern inode_t* openINodeTable;
|
||||
//extern uint32_t maxOpenFiles;
|
||||
//extern uint32_t openFileCount;
|
||||
//extern uint32_t maxOpenInodes;
|
||||
//extern uint32_t openInodeCount;
|
||||
|
||||
__asm__ volatile ("nop" : "=b"(path), "=c"(oflag));
|
||||
|
||||
__asm__ volatile ("movl %0, %%eax" : : "a"(fd));
|
||||
}
|
||||
|
||||
void sys_close() {}
|
||||
|
||||
void sys_seek() {}
|
||||
|
||||
// System call table
|
||||
void (*syscalls[MAX_SYSCALLS])(void) = {
|
||||
[SYS_SLEEP] = sys_sleep,
|
||||
[SYS_WRITE] = sys_write,
|
||||
[SYS_GETS] = sys_gets,
|
||||
[SYS_MALLOC] = sys_malloc,
|
||||
[SYS_FREE] = sys_free,
|
||||
[SYS_FREE] = sys_free,
|
||||
[SYS_OPEN] = sys_open,
|
||||
[SYS_CLOSE] = sys_close,
|
||||
[SYS_SEEK] = sys_seek,
|
||||
};
|
||||
|
||||
// int 0x80 - Syscall interrupt, handled by this function
|
||||
@@ -92,7 +119,7 @@ void syscallHandler(intFrame_t* iframe) {
|
||||
// We should also push these regs: ax, gs, fs, es, ds, bp, di, si, dx, cx, bx
|
||||
__asm__ volatile (
|
||||
".intel_syntax noprefix\n" // We use intel syntax
|
||||
".equ MAX_SYSCALLS, 5\n" // Define MAX_SYSCALLS again TODO: I don't want to have to do this
|
||||
".equ MAX_SYSCALLS, 9\n" // Define MAX_SYSCALLS again TODO: I don't want to have to do this
|
||||
|
||||
// Check if syscall exists
|
||||
"cmp eax, MAX_SYSCALLS - 1\n"
|
||||
@@ -127,7 +154,7 @@ void syscallHandler(intFrame_t* iframe) {
|
||||
"iretd\n" // Return from interrupt (32 bit)
|
||||
|
||||
// TODO: Send an error or something here
|
||||
"invalidSyscall:\n"
|
||||
"invalidSyscall:\n"
|
||||
"mov eax, -1\n" // Current error thing
|
||||
"iretd\n"
|
||||
|
||||
|
||||
@@ -4,19 +4,9 @@
|
||||
#include <terminal/terminal.h>
|
||||
#include <memory/addresses.h>
|
||||
#include <screen/gfxmode.h>
|
||||
#include <disk/filesys.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
// TODO: Make this check if it's a valid font
|
||||
// TODO: Make it take a fileEntry_t?
|
||||
uint8_t loadFont(char* filename) {
|
||||
if (readFile(filename, (void*)FONT_LOC) != 0) { return 1; } // File not found
|
||||
if (*(uint8_t*)FONT_WIDTH == 0 || *(uint8_t*)FONT_HEIGHT == 0) { return 1; } // Not a font
|
||||
|
||||
return 0; // Success
|
||||
}
|
||||
|
||||
// TODO: Reimplement larger text
|
||||
void drawChar(char c, uint32_t x, uint32_t y, uint32_t fgColor, uint32_t bgColor) {
|
||||
// Font info
|
||||
|
||||
Reference in New Issue
Block a user