diff --git a/.gitignore b/.gitignore index 8ba6358..8a3d186 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,15 @@ -build -kern.iso +.DS_Store + +# Binaries +bin/ +*.bin makedisk + +# Object files +*.o +*.obj +*.elf + +# Vim swap files +*.swp + diff --git a/Makefile b/Makefile index e1deea1..702920c 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ # Variables -CC = i386-elf-gcc -LD = i386-elf-ld +CC = i686-elf-gcc +LD = i686-elf-ld AS = nasm CFLAGS = -std=c17 -ffreestanding -fno-builtin -fno-stack-protector -fno-pie \ @@ -8,51 +8,51 @@ CFLAGS = -std=c17 -ffreestanding -fno-builtin -fno-stack-protector -fno-pie \ LDFLAGS = -z notext -melf_i386 SRC = ./src -BUILD = ./build +BIN = ./bin C_FILES = calc editor FONTS = testfont term16n .PHONY: os clean run # Main compilation target -os: $(BUILD) bootloader kernel $(C_FILES) $(FONTS) +os: $(BIN) bootloader kernel $(C_FILES) $(FONTS) @cc ./makedisk.c -o ./makedisk @./makedisk # TODO: List of files @echo "\nDone!\n" -$(BUILD): - @[ -d $(BUILD) ] || mkdir $(BUILD) - @rm -rf $(BUILD)/* +$(BIN): + @[ -d $(BIN) ] || mkdir $(BIN) + @rm -rf $(BIN)/* # Bootloader: This target is responsible for creating stage?.bin (1,2,3) bootloader: $(SRC)/stage1.asm $(SRC)/stage2.asm @echo "Assembling $(SRC)/stage1.asm..." - @$(AS) -fbin -o $(BUILD)/stage1.bin $(SRC)/stage1.asm + @$(AS) -fbin -o $(BIN)/stage1.bin $(SRC)/stage1.asm @echo "Assembling $(SRC)/stage2.asm..." - @$(AS) -fbin -o $(BUILD)/stage2.bin $(SRC)/stage2.asm + @$(AS) -fbin -o $(BIN)/stage2.bin $(SRC)/stage2.asm @echo "Compiling $(SRC)/stage3.c..." - @$(CC) $(CFLAGS) -Iinclude -o $(BUILD)/stage3.o -c $(SRC)/stage3.c - @$(LD) $(LDFLAGS) -T$(SRC)/stage3.ld --oformat binary -o $(BUILD)/stage3.bin $(BUILD)/stage3.o - @rm $(BUILD)/stage3.o + @$(CC) $(CFLAGS) -Iinclude -o $(BIN)/stage3.o -c $(SRC)/stage3.c + @$(LD) $(LDFLAGS) -T$(SRC)/stage3.ld --oformat binary -o $(BIN)/stage3.bin $(BIN)/stage3.o + @rm $(BIN)/stage3.o # Kernel: This target is responsible for creating kernel.bin kernel: $(SRC)/kernel.c @echo "Compiling $(SRC)/kernel.c..." - @$(CC) $(CFLAGS) -Iinclude -o $(BUILD)/kernel.o -c $(SRC)/kernel.c - @$(LD) $(LDFLAGS) -T$(SRC)/kernel.ld --oformat binary -o $(BUILD)/kernel.bin $(BUILD)/kernel.o - @rm $(BUILD)/kernel.o + @$(CC) $(CFLAGS) -Iinclude -o $(BIN)/kernel.o -c $(SRC)/kernel.c + @$(LD) $(LDFLAGS) -T$(SRC)/kernel.ld --oformat binary -o $(BIN)/kernel.bin $(BIN)/kernel.o + @rm $(BIN)/kernel.o # This target compiles *.c (except kernel.c) $(C_FILES): @echo "Compiling $(SRC)/$@.c..." - @$(CC) $(CFLAGS) -o $(BUILD)/$@.o -c $(SRC)/$@.c - @$(LD) $(LDFLAGS) -T$(SRC)/$@.ld --oformat binary -o $(BUILD)/$@.bin $(BUILD)/$@.o - @rm $(BUILD)/$@.o + @$(CC) $(CFLAGS) -o $(BIN)/$@.o -c $(SRC)/$@.c + @$(LD) $(LDFLAGS) -T$(SRC)/$@.ld --oformat binary -o $(BIN)/$@.bin $(BIN)/$@.o + @rm $(BIN)/$@.o # This target builds all fonts inside src/fonts $(FONTS): @echo "Assembling $(SRC)/fonts/$@.asm..." - @$(AS) -fbin -o $(BUILD)/$@.fnt $(SRC)/fonts/$@.asm + @$(AS) -fbin -o $(BIN)/$@.fnt $(SRC)/fonts/$@.asm # Run QEMU run: @@ -64,7 +64,7 @@ run: # Clean all build files clean: - @rm -rf $(BUILD) + @rm -rf $(BIN) @rm -f kern.iso @rm makedisk diff --git a/docs/syscalls.txt b/docs/syscalls.txt index fd3ab08..ea54056 100644 --- a/docs/syscalls.txt +++ b/docs/syscalls.txt @@ -4,23 +4,16 @@ Syscall handlers are prefixed by 'sys_' Syscalls can be called using interrupt 0x80 The EAX register should countain a syscall number on calling and it will contain a return value (if applicable) after calling -Here's a list of syscalls: (and their numbers) +--- Syscall Table --- -Syscall(0) : Sleep() - Args: EBX - Number of miliseconds to sleep for - -Syscall(1) : Write() - Args: EBX - File descriptor - ECX - Buffer to write - EDX - Length - Returns: Status - -Syscall(2) : Read() #TBD - -Syscall(3) : Malloc() - Args: EBX - Amount of bytes to malloc - Returns: Address of allocated memory - -Syscall(4) : Free() - Args: EBX - Pointer to free +| NR | SYSC NAME | RETURN (EAX) | ARG0 (EBX) | ARG1 (ECX) | ARG2 (EDX) | +| 00 | reserved | - | - | - | - | +| 01 | sleep | - | uint ms | - | - | +| 02 | read | int count | int fd | char* buf | uint count | +| 03 | write | int count | int fd | char* buf | uint count | +| 04 | open | int fd | char* path | int oflag | - | +| 05 | close | int status | int fd | - | - | +| 06 | malloc | void* buf | int count | - | - | +| 07 | free | - | void* buf | - | - | +| 08 | seek | #TBD | | | | diff --git a/include/c/syscall.h b/include/c/syscall.h index 7a5067f..5b0389a 100644 --- a/include/c/syscall.h +++ b/include/c/syscall.h @@ -1,23 +1,41 @@ #ifndef SYSCALLNUMS_H #define SYSCALLNUMS_H -#include - -#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 diff --git a/include/disk/filesys.h b/include/disk/filesys.h deleted file mode 100644 index 7a7748a..0000000 --- a/include/disk/filesys.h +++ /dev/null @@ -1,80 +0,0 @@ -#ifndef FILESYS_H -#define FILESYS_H - -#include -#include -#include - -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 diff --git a/include/fs/fs.h b/include/fs/fs.h index cee2641..9cca27d 100644 --- a/include/fs/fs.h +++ b/include/fs/fs.h @@ -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 diff --git a/include/fs/impl.h b/include/fs/impl.h new file mode 100644 index 0000000..8af4cd6 --- /dev/null +++ b/include/fs/impl.h @@ -0,0 +1,52 @@ +#ifndef IMPL_H +#define IMPL_H + +#include +#include +#include +#include +#include + +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 diff --git a/include/interrupt/syscalls.h b/include/interrupt/syscalls.h index 6634424..470d6a2 100644 --- a/include/interrupt/syscalls.h +++ b/include/interrupt/syscalls.h @@ -9,8 +9,12 @@ #include #include #include +#include + +/* + * 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" diff --git a/include/screen/text.h b/include/screen/text.h index 9aa76a4..4a30c28 100644 --- a/include/screen/text.h +++ b/include/screen/text.h @@ -4,19 +4,9 @@ #include #include #include -#include #include #include -// 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 diff --git a/kern.iso b/kern.iso new file mode 100644 index 0000000..cdf5b25 Binary files /dev/null and b/kern.iso differ diff --git a/makedisk.c b/makedisk.c index 70bfede..e84780e 100644 --- a/makedisk.c +++ b/makedisk.c @@ -8,6 +8,7 @@ #include "include/fs/fs.h" #define IMAGE_NAME "kern.iso" +#define BIN_DIR "./bin/" #define DISK_SIZE (2880 * 512) // 1.44MB uint8_t nullBlock[FS_BLOCK_SIZE] = { 0 }; @@ -19,15 +20,15 @@ typedef struct { } fileInfo_t; fileInfo_t files[] = { // TODO: Not have to list all of the files, loop through a directory or smth - { "./build/stage1.bin" }, - { "./build/stage2.bin" }, - { "./build/stage3.bin" }, - { "./build/kernel.bin" }, - { "./build/term16n.fnt" }, - { "./build/testfont.fnt" }, + { BIN_DIR"stage1.bin" }, + { BIN_DIR"stage2.bin" }, + { BIN_DIR"stage3.bin" }, + { BIN_DIR"kernel.bin" }, + { BIN_DIR"term16n.fnt" }, + { BIN_DIR"testfont.fnt" }, { "./src/fs/test.txt" }, - { "./build/calc.bin" }, - { "./build/editor.bin" }, + { BIN_DIR"calc.bin" }, + { BIN_DIR"editor.bin" }, }; superBlock_t superblock = { 0 }; @@ -117,29 +118,29 @@ void writeBootblock() { void writeSuperblock() { superblock.inodeCount = fileCount; // 2 reserved INodes (0 - Invalid, 1 - Root) - superblock.firstInodeBitmapBlock = 2; // Block 0 - Boot, Block 1 - Superblock + superblock.firstINodeBitmapBlock = 2; // Block 0 - Boot, Block 1 - Superblock superblock.inodeBitmapBlockCount = superblock.inodeCount / (FS_BLOCK_SIZE * 8) + ((superblock.inodeCount % (FS_BLOCK_SIZE * 8) > 0) ? 1 : 0); - superblock.firstDataBitmapBlock = superblock.firstInodeBitmapBlock + superblock.inodeBitmapBlockCount; + superblock.firstDataBitmapBlock = superblock.firstINodeBitmapBlock + superblock.inodeBitmapBlockCount; superblock.inodeBlockCount = bytesToBlocks(superblock.inodeCount * sizeof(inode_t)); dataBlocks = bytesToBlocks(diskSize); dataBitCount = (dataBlocks - superblock.firstDataBitmapBlock - superblock.inodeBlockCount); superblock.dataBitmapBlockCount = dataBitCount / (FS_BLOCK_SIZE * 8) + ((dataBitCount % (FS_BLOCK_SIZE * 8) > 0) ? 1 : 0); - superblock.firstInodeBlock = superblock.firstDataBitmapBlock + superblock.dataBitmapBlockCount; - superblock.firstDataBlock = superblock.firstInodeBlock + superblock.inodeBlockCount; + superblock.firstINodeBlock = superblock.firstDataBitmapBlock + superblock.dataBitmapBlockCount; + superblock.firstDataBlock = superblock.firstINodeBlock + superblock.inodeBlockCount; superblock.dataBlockCount = (fileBlocks - 2) + 1; // 1 Block for root dir (-1 for boot block) superblock.maxFileSize = 0xffffffff; superblock.blockSize = FS_BLOCK_SIZE; superblock.inodeSize = sizeof(inode_t); - superblock.rootInodePtr = 0; // Gets setup at runtime + superblock.rootINodePtr = 0; // Gets setup at runtime superblock.inodesPerBlock = FS_BLOCK_SIZE / sizeof(inode_t); - superblock.directExtentsPerInode = 4; + superblock.directExtentsPerINode = 4; superblock.extentsPerIndirectBlock = FS_BLOCK_SIZE / sizeof(extent_t); - superblock.firstFreeInodeBit = superblock.inodeCount; + superblock.firstFreeINodeBit = superblock.inodeCount; superblock.firstFreeDataBit = (fileBlocks - 2) + 1; // +1 for root dir superblock.deviceNumber = 0x01; - superblock.firstUnreservedInode = 3; // 0 - Invalid; 1 - Root dir; 2 - Bootloader INode + superblock.firstUnreservedINode = 3; // 0 - Invalid; 1 - Root dir; 2 - Bootloader INode assert(fwrite(&superblock, sizeof(superBlock_t), 1, imagePtr) == 1); assert(fwrite(nullBlock, paddingBytes(sizeof(superBlock_t), FS_BLOCK_SIZE), 1, imagePtr) == 1); // Padding diff --git a/src/kernel.c b/src/kernel.c index 0c0356f..bf9afc4 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -10,8 +10,8 @@ #include #include #include -#include -#include +#include +#include #include #include #include @@ -23,14 +23,22 @@ #include #include #include -#include -#define PROMPT "/ #> " +#define PROMPT "#>" + +//openFileTable_t* openFileTable; +inode_t* openINodeTable; +uint32_t maxOpenFiles = 256; +uint32_t openFileCount = 3; // First 3 reserved for stdin, stdout, stderr +uint32_t maxOpenINodes = 256; +uint32_t openINodeCount = 0; // Function declarations void listFiles(); void printRegs(); void printPhysicalMemmap(); +void initOpenFileTable(); +void initOpenINodeTable(); __attribute__ ((section("entry"))) void main() { @@ -73,6 +81,18 @@ void main() { mallocPhysicalAddr = kernelMallocPhysicalAddr; mallocPages = kernelMallocPages; + // Setup filesystem + currentDir = malloc(1024); + strcpy(currentDir, "/"); // Start at root + + + // Setup kernel open file table and open inode table + initOpenFileTable(); + initOpenINodeTable(); + currentDir = malloc(1024); + strcpy(currentDir, "/"); + + // Terminal setup terminal->fg = FG_COLOR; terminal->bg = BG_COLOR; clear(); @@ -81,7 +101,7 @@ void main() { // Run Interactive Shell Program // TODO: Make it in another file while (1) { - printf(PROMPT); + printf("%s %s ", currentDir, PROMPT); char input[256]; char* inputPtr = input; @@ -223,12 +243,13 @@ void main() { } else if (strcmp(argv[0], "test") == 0) { printf("\x1b[1MError: 'test' is not available.\x1b[8M\n"); } else { // TODO: Use new filesystem! + /* fileEntry_t* file = findFile(argv[0]); - if (file == NULL) { + if (file == NULL) {*/ printf("Command not found: %s\n", argv[0]); continue; - } + /*} uint32_t neededPages = (file->length * 512) / PAGE_SIZE; if ((file->length * 512) % PAGE_SIZE > 0) { neededPages++; } @@ -291,7 +312,7 @@ void main() { unmapPage((void*)virtualAddr); flushTlbEntry(virtualAddr); // Invalidate unused page } - } + }*/ } } @@ -375,3 +396,13 @@ void printPhysicalMemmap() { printf("Free blocks: %d\n\n", maxBlocks - usedBlocks); } +void initOpenFileTable() { + //openFileTable = malloc(sizeof(openFileTable_t) * maxOpenFiles); + //*openFileTable = (openFileTable_t) { 0 }; +} + +void initOpenINodeTable() { + openINodeTable = malloc(sizeof(inode_t) * maxOpenINodes); + *openINodeTable = (inode_t) { 0 }; +} + diff --git a/src/stage2.asm b/src/stage2.asm index 8b70dc0..73b9130 100644 --- a/src/stage2.asm +++ b/src/stage2.asm @@ -369,9 +369,9 @@ DATA_SEG equ GDT.dataDescriptor - GDT.start ;; VBE Stuff ;; !! IF YOU WANT TO SELECT ANY VALUE IN THE SETUP, SET THESE TO ZERO !! -width: dw 0 -height: dw 0 -bpp: db 0 +width: dw 1920 +height: dw 1080 +bpp: db 32 offset: dw 0 _segment: dw 0 ; segment is a keyword diff --git a/src/stage3.c b/src/stage3.c index b10eabc..644a13f 100644 --- a/src/stage3.c +++ b/src/stage3.c @@ -2,12 +2,9 @@ #include #include #include -//#include -#include - +#include #include - -uint8_t loadFile(char* filename, void* dest); +#include __attribute__ ((section("entry"))) void main() { @@ -31,11 +28,17 @@ void main() { // Load root dir, find kernel INode and parse it superBlock_t* superblock = (superBlock_t*)SUPERBLOCK_LOC; - superblock->rootInodePtr = BOOT_FIRST_INODE_LOC + sizeof(inode_t); + superblock->rootINodePtr = BOOT_FIRST_INODE_LOC + sizeof(inode_t); diskRead(superblock->firstDataBlock * 8, 8, (void*)SCRATCH_BLOCK_LOC); - if (loadFile("kernel.bin", (void*)KERNEL_LOC) != 0) __asm__ volatile ("cli; hlt" : : "a"(0xDeadBeef)); - if (loadFile("term16n.fnt", (void*)FONT_LOC) != 0) __asm__ volatile ("cli; hlt" : : "a"(0xDeadBeef)); + inode_t* inode; + if ((inode = getINode("kernel.bin")) == NULL) { + __asm__ volatile ("cli; hlt" : : "a"(0xDeadBeef)); + } else { loadFile(inode, (void*)KERNEL_LOC); } + + if ((inode = getINode("term16n.fnt")) == NULL) { + __asm__ volatile ("cli; hlt" : : "a"(0xDeadBeef)); + } else { loadFile(inode, (void*)FONT_LOC); } // Virtual memory manager setup if (setupVirtualMemoryManager() != 0) { @@ -66,33 +69,3 @@ void main() { ((void(*)(void))0xC0000000)(); } -uint8_t loadFile(char* filename, void* dest) { - dirEntry_t* dirEntry = (dirEntry_t*)SCRATCH_BLOCK_LOC; - while (dirEntry->name[0] != '\0') { - if (strcmp(dirEntry->name, filename) == 0) break; - dirEntry++; - } - - if (dirEntry->name[0] == '\0') return 1; - - inode_t* inode = (inode_t*)BOOT_FIRST_INODE_LOC; - while (inode->id != dirEntry->id) { - inode++; - } - - 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; -} -