From e82392e9d98eab07cd5dd525043742d1e305489b Mon Sep 17 00:00:00 2001 From: Nigel Date: Fri, 27 Oct 2023 18:07:11 +0200 Subject: [PATCH] FAT Filesystem implementation additions --- CoreLib/Makefile | 2 - kernel/kernel.cpp | 33 ++++------- kernel/memory/VirtualMemoryManager.cpp | 56 ++++++++++--------- kernel/storage/filesystems/FAT/FAT.cpp | 18 +++++- .../partitiontables/mbr/MasterBootRecord.h | 16 +++--- kernel/storage/vfs/vfs.cpp | 21 ++++++- kernel/terminal/kterm.cpp | 29 +++++----- 7 files changed, 101 insertions(+), 74 deletions(-) diff --git a/CoreLib/Makefile b/CoreLib/Makefile index cb74d68..19b06ef 100644 --- a/CoreLib/Makefile +++ b/CoreLib/Makefile @@ -15,8 +15,6 @@ $(OUTPUTFILE): $(OFILES) pwd ar -rc $(OUTPUTFILE) $(OFILES) - - $(OBJ_FOLDER)/ctype.o: ctype.cpp $(CPP) -c ctype.cpp -o $(OBJ_FOLDER)/ctype.o $(CFLAGS) diff --git a/kernel/kernel.cpp b/kernel/kernel.cpp index 5551f25..4375be3 100644 --- a/kernel/kernel.cpp +++ b/kernel/kernel.cpp @@ -42,43 +42,33 @@ extern "C" void kernel () { init_serial(); kterm_init(); - + print_serial("kterm initialized...\n"); setup_tss(); initGDT(); initidt(); LoadGlobalDescriptorTable(); flush_tss(); printf("Memory setup complete!\n"); - + print_serial("Memory initialized....\n"); // Enable interrupts asm volatile("STI"); - initHeap(); - pit_initialise(); - ACPI::initialize(); - PCI::Scan(); + print_serial("Heap initialized...\n"); + //pit_initialise(); + + + //ACPI::initialize(); + //PCI::Scan(); processor::initialize(); processor::enable_protectedMode(); initBootDrive(); VirtualFileSystem::initialize(); - - + print_serial("Run test!"); +#define VFS_EXAMPLE #ifdef VFS_EXAMPLE auto fontFile = VirtualFileSystem::open("/FONT PSF", 0); - if(grubcfg->flags == 0){ - uint16_t* contents = (uint16_t*) malloc(sizeof(uint16_t) * 256); - grubcfg->read(grubcfg, contents, 512); - - for(int i =0 ; i < grubcfg->root->size; i++ ){ - kterm_put(contents[i] & 0x00ff); - kterm_put(contents[i] >> 8); - } - kterm_put('\n'); - free((void*)contents); - }else{ - printf("Could not find file\n"); - } + printf("Size of font file: %d bytes", fontFile->root->size); // COOL This Works like a charm #endif #ifdef USERMODE_RELEASE @@ -86,6 +76,7 @@ extern "C" void kernel () jump_usermode(); #else startSuperVisorTerminal(); + #endif } \ No newline at end of file diff --git a/kernel/memory/VirtualMemoryManager.cpp b/kernel/memory/VirtualMemoryManager.cpp index e866b28..a8ec187 100644 --- a/kernel/memory/VirtualMemoryManager.cpp +++ b/kernel/memory/VirtualMemoryManager.cpp @@ -1,4 +1,6 @@ #include "VirtualMemoryManager.h" +#include "../../CoreLib/Memory.h" + #define ALIGN(addr, align) (((addr) & ~((align) - 1 )) + (align)) extern uint32_t boot_page_directory[1024] ; // points to the wrong location @@ -14,7 +16,7 @@ void flush_cr3(){ void AllocatePage(uint32_t vaddr) { - uint32_t page_aligned_address = ALIGN(vaddr, 4096); + //uint32_t page_aligned_address = ALIGN(vaddr, 4096); // allocate a page at virtual address int PageDirectoryEntryIndex = vaddr >> 22; @@ -54,16 +56,16 @@ void AllocatePage(uint32_t vaddr) void FreePage(uint32_t vaddr ) { - uint32_t page_aligned_address = ALIGN(vaddr, 4096); + // uint32_t page_aligned_address = ALIGN(vaddr, 4096); // allocate a page at virtual address int PageDirectoryEntryIndex = vaddr >> 22; int PageTableEntryIndex = (vaddr >> 12) & 0x1FFF; - uint32_t* pageTable = (uint32_t*)(boot_page_directory[PageDirectoryEntryIndex] & 0xFFFFE000 + 0xC0000000); + uint32_t* pageTable = (uint32_t*)(boot_page_directory[PageDirectoryEntryIndex] & (0xFFFFE000 + 0xC0000000)); - void* physicalAddressToFree = (void*)(pageTable[PageTableEntryIndex] & 0xFFFFE000 + 0xC0000000); + void* physicalAddressToFree = (void*)(pageTable[PageTableEntryIndex] & (0xFFFFE000 + 0xC0000000)); free_block(physicalAddressToFree); pageTable[PageTableEntryIndex] = 0x0; @@ -80,9 +82,9 @@ void Immediate_Map ( uint32_t vaddr, uint32_t paddr) int PageTableEntryIndex = (vaddr >> 12) & 0x1FFF; printf("Map address at PDE 0x%x PTE 0x%x\n", PageDirectoryEntryIndex, PageTableEntryIndex); - - if ((boot_page_directory - 0xC0000000)[PageDirectoryEntryIndex] & 0x1 ) - { + printf("boot pagedirectoy address: 0x%x\n", &boot_page_directory); + printf("PDE : 0x%x\n", boot_page_directory[PageTableEntryIndex]); + if (boot_page_directory[PageDirectoryEntryIndex] & 0x1 ) { printf("Directory entry is marked as present\n"); } else { @@ -90,37 +92,39 @@ void Immediate_Map ( uint32_t vaddr, uint32_t paddr) // mark the page table as present and allocate a physical block for it void* new_page_dir = allocate_block(); - printf("New page directory address 0x%x\n", new_page_dir); + memset(new_page_dir, 0 , 1024 * sizeof (uint32_t)); + printf("New page directory address 0x%x\n", &new_page_dir); boot_page_directory[PageDirectoryEntryIndex] = (uint32_t)new_page_dir | 0x3; - } printf("PDE found at : 0x%x\n", (uint32_t) &boot_page_directory[PageDirectoryEntryIndex]); - uint32_t* page_table = (uint32_t*)(boot_page_directory[PageDirectoryEntryIndex] & 0xFFFFE000) ; - //page_table = (uint32_t*) ((uint32_t)page_table - 0xC0000000); // remove kernel offset - printf("Page table address: 0x%x\n", (uint32_t)page_table); + uint32_t* page_table = (uint32_t*)(boot_page_directory[PageDirectoryEntryIndex] & 0xFFFFE000) ; + printf("Page table address: 0x%x\n", (uint32_t)page_table); - // check if the page table entry is marked as present - if ( page_table[PageTableEntryIndex] & 0x1 ) - { - printf("page already present!\n"); - printf("Entry found at addr: 0x%x\n", &(page_table[PageTableEntryIndex])); - } else{ - printf("Mapping a physical page.\n"); - // Map the entry to a physical page - page_table[PageTableEntryIndex] = (uint32_t)(paddr | 0x3); - } + // check if the page table entry is marked as present + if ( page_table[PageTableEntryIndex] & 0x1 ) + { + printf("page already present!\n"); + printf("Entry found at addr: 0x%x\n", &(page_table[PageTableEntryIndex])); + } else{ + printf("Mapping a physical page.\n"); + // Map the entry to a physical page + page_table[PageTableEntryIndex] = (uint32_t)(paddr | 0x3); + } - asm ("cli; invlpg (%0); sti" :: "r" (vaddr) : "memory" ); + asm ("invlpg (%0)" :: "r" (vaddr) : "memory" ); } + +// NOT IMPLEMENTED void Immediate_Unmap(uint32_t vaddr) { // NOTE: I will implement lazy unmapping for now - uint32_t page_aligned_address = ALIGN(vaddr, 4096); + //uint32_t page_aligned_address = ALIGN(vaddr, 4096); // allocate a page at virtual address - int PageDirectoryEntryIndex = vaddr >> 22; - int PageTableEntryIndex = (vaddr >> 12) & 0x1FFF; + //int PageDirectoryEntryIndex = vaddr >> 22; + //int PageTableEntryIndex = (vaddr >> 12) & 0x1FFF; + } diff --git a/kernel/storage/filesystems/FAT/FAT.cpp b/kernel/storage/filesystems/FAT/FAT.cpp index 80465b9..e181f7e 100644 --- a/kernel/storage/filesystems/FAT/FAT.cpp +++ b/kernel/storage/filesystems/FAT/FAT.cpp @@ -107,10 +107,10 @@ DirectoryNode* FAT::Lookup (inode* currentDir , DirectoryNode* dir) for(int i = 0; i < sizeof(data) / sizeof (DIR); i++) { DIR* entry = (DIR*)((uint32_t)directory + (i * sizeof(DIR))); + + if( entry->Name[0] == FAT::FREE_DIR || - entry->Name[0] == FAT::FREE_DIR_2 || - entry->Name[0] == 0xE5 || entry->ATTR & FAT::ATTRIBUTES::ATTR_VOLUME_ID || entry->ATTR & FAT::ATTRIBUTES::ATTR_SYSTEM || entry->ATTR & FAT::ATTRIBUTES::ATTR_HIDDEN @@ -118,6 +118,20 @@ DirectoryNode* FAT::Lookup (inode* currentDir , DirectoryNode* dir) continue; } + + if( entry->ATTR & FAT::ATTRIBUTES::ATTR_DIRECTORY){ + printf("entry in directory\n"); + for(int i = 0; i < 11 ;i ++) + kterm_put(entry->Name[i]); + kterm_put('\n'); + } + + + + + if( entry->Name[0] == FAT::FREE_DIR_2 ) + break; + auto* dirNode = (DirectoryNode*) malloc(sizeof (DirectoryNode)); char* name = (char*)malloc(sizeof(char[11])); diff --git a/kernel/storage/partitiontables/mbr/MasterBootRecord.h b/kernel/storage/partitiontables/mbr/MasterBootRecord.h index 1cb207c..4f83ceb 100644 --- a/kernel/storage/partitiontables/mbr/MasterBootRecord.h +++ b/kernel/storage/partitiontables/mbr/MasterBootRecord.h @@ -22,22 +22,22 @@ inline MBR* GetPartitions(bool DEBUG = false){ int S =1; uint32_t LBA = (C*HPC+H) * SPT + (S-1); - MBR* mbr =(MBR*) malloc(sizeof (MBR)); + uint16_t* mbr =(uint16_t*) malloc(sizeof (MBR)); - ATAPIO::Read(ATAPIO_PORT::Primary, DEVICE_DRIVE::MASTER, LBA, (uint16_t*)mbr); + ATAPIO::Read(ATAPIO_PORT::Primary, DEVICE_DRIVE::MASTER, LBA, mbr ); + auto bootRecord = (MBR*)(mbr); - - printf("MBR (In Memory) Address 0x%x, Size = %d\n", mbr, sizeof (MBR)); + printf("MBR (In Memory) Address 0x%x, Size = %d\n", bootRecord, sizeof (MBR)); if(DEBUG){ - printf("BootSector: 0x%x\n", mbr->ValidBootsector ); + printf("BootSector: 0x%x\n", bootRecord->ValidBootsector ); for( int i = 0 ; i < 4 ; i ++){ - PartitionTableEntry PT = mbr->TableEntries[i]; + PartitionTableEntry PT = bootRecord->TableEntries[i]; printf("Partition %d [ %d sectors, PartitionType: 0x%x, 0x%x, \nLBA Start: 0x%x ]\n" , - i, PT.Number_sectors_inPartition, PT.PartitionType, mbr->uniqueID, PT.LBA_partition_start ); + i, PT.Number_sectors_inPartition, PT.PartitionType, bootRecord->uniqueID, PT.LBA_partition_start ); } } - return mbr; + return bootRecord; } diff --git a/kernel/storage/vfs/vfs.cpp b/kernel/storage/vfs/vfs.cpp index d432632..0eb140e 100644 --- a/kernel/storage/vfs/vfs.cpp +++ b/kernel/storage/vfs/vfs.cpp @@ -80,20 +80,37 @@ FILE* VirtualFileSystem::open(const char* pathname, int flags){ } // let's just loop through the folder first + printf("looking for: "); + for(int i = 0; i < 5; i++) + kterm_put(nextdir[i]); + kterm_put('\n'); auto* child = rootentry->children; while(child->next != nullptr){ - auto* directory = (DirectoryNode*)child->data; + + for(int i = 0; i < 11 ; i++) + kterm_put(directory->name[i]); + kterm_put('\n'); + if( directory->compare(directory, directory->name, nextdir) == 0){ nextdir = strtok(nullptr, "/", &tokstate); + printf("Found dir!\n"); if(nextdir == NULL){ file->root = directory->node; file->flags =0; file->read = FAT::Read; return file; } + printf("continue searching next directory!\n"); + if(directory->children == nullptr) + directory->node->lookup(directory->node, directory); + + + child = directory->children; + + }else{ + child = child->next; } - child = child->next; } diff --git a/kernel/terminal/kterm.cpp b/kernel/terminal/kterm.cpp index ede8bbd..263d799 100644 --- a/kernel/terminal/kterm.cpp +++ b/kernel/terminal/kterm.cpp @@ -21,7 +21,14 @@ void kterm_init () { kterm_row = 0; kterm_column = 0; kterm_color = vga_entry_color ( VGA_COLOR_LIGHT_GREY , VGA_COLOR_BLACK); - kterm_buffer = (uint16_t*) 0xC03FF000; + + //Physical address + // 0xB8000 + // Virtual address + // 0xC03FF000 + + //kterm_buffer = ((uint16_t*) 0xB8000); + kterm_buffer = ((uint16_t*) 0xC03FF000 ); for (size_t y = 0; y < VGA_HEIGHT; y++ ){ for( size_t x = 0; x < VGA_WIDTH; x++){ @@ -59,7 +66,6 @@ void disable_cursor() outb(0x3D5, 0x20); } - void update_cursor(int x, int y){ uint16_t pos = y * VGA_WIDTH + x; @@ -86,8 +92,6 @@ int get_cursor_y (uint16_t cursor_pos ) { return cursor_pos / VGA_WIDTH; } - - /** * With the help from: * https://whiteheadsoftware.dev/operating-systems-development-for-dummies/ @@ -134,7 +138,6 @@ void kterm_writestring(const char* data ){ kterm_write(data, strlen(data)); } - static void itoa (char *buf, int base, int d) { char *p = buf; char *p1, *p2; @@ -173,28 +176,28 @@ static void itoa (char *buf, int base, int d) { void printf ( const char *format, ...) { - char **arg = (char **)&format; + auto **arg = (unsigned char **)&format; int c; char buf[20]; arg++; - while ((c = *format++) != 0){ + while ((c = *(const unsigned char*)format++) != 0){ if( c != '%') kterm_put(c); else{ - char *p, *p2; + char const *p, *p2; int pad0 = 0, pad = 0; - c = *format++; + c = *(const unsigned char*)format++; if(c =='0'){ pad0 = 1; - c = *format++; + c = *(const unsigned char*)format++; } if ( c >= '0' && c <= '9'){ pad = c - '0'; - c = *format++; + c = *(const unsigned char*)format++; } switch (c) @@ -210,7 +213,7 @@ void printf ( const char *format, ...) { break; case 's': - p = *arg++; + p = (char const *)*arg++; if(!p) p = "(null)"; @@ -224,7 +227,7 @@ void printf ( const char *format, ...) { default: - kterm_put(*((int *)arg++)); + kterm_put(*(int *) arg++); // NOLINT(cppcoreguidelines-narrowing-conversions) break; } }