From 490529099b0f378c4179bcbe8bf815a8b0bca1fc Mon Sep 17 00:00:00 2001 From: Nigel Date: Fri, 17 Feb 2023 21:52:03 +0100 Subject: [PATCH] Started implementing the virtual file system - The FAT command is no longer available - At Startup the FileSystem initialise funciton is called, it should execute the same code as the FAT command did. - ACPI::initialise is commented out because it causes a Exception --- Makefile | 12 +- source/kernel/drivers/acpi/acpi.cpp | 3 +- source/kernel/drivers/acpi/rsdp.cpp | 5 +- source/kernel/filesystem/FAT/FAT16.cpp | 0 source/kernel/filesystem/FAT/FAT16.h | 8 - source/kernel/kernel.cpp | 12 +- .../supervisorterminal/superVisorTerminal.cpp | 150 +------ source/kernel/vfs/File.h | 9 - source/kernel/vfs/VFS.cpp | 376 ++++++++++++++++-- source/kernel/vfs/VFS.h | 91 ++++- 10 files changed, 429 insertions(+), 237 deletions(-) delete mode 100644 source/kernel/filesystem/FAT/FAT16.cpp delete mode 100644 source/kernel/filesystem/FAT/FAT16.h delete mode 100644 source/kernel/vfs/File.h diff --git a/Makefile b/Makefile index 95e595f..ae3954c 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ CC = ${HOME}/opt/cross/bin/i686-elf-gcc CPP = ${HOME}/opt/cross/bin/i686-elf-g++ CFLAGS = -ffreestanding -Og -ggdb -Wall -Wextra -OFILES =$(BUILD_DIR)/boot.o $(BUILD_DIR)/kterm.o $(BUILD_DIR)/kernel.o $(BUILD_DIR)/memory.o $(BUILD_DIR)/paging.o $(BUILD_DIR)/pit.o $(BUILD_DIR)/time.o $(BUILD_DIR)/keyboard.o $(BUILD_DIR)/io.o $(BUILD_DIR)/processor.o $(BUILD_DIR)/gdtc.o $(BUILD_DIR)/idt.o $(BUILD_DIR)/pic.o $(BUILD_DIR)/sv-terminal.o $(BUILD_DIR)/string.o $(BUILD_DIR)/prekernel.o $(BUILD_DIR)/cpu.o $(BUILD_DIR)/KHeap.o $(BUILD_DIR)/pci.o $(BUILD_DIR)/pcidevice.o $(BUILD_DIR)/atapiDevice.o $(BUILD_DIR)/ataDevice.o $(BUILD_DIR)/rsdp.o $(BUILD_DIR)/acpi.o +OFILES =$(BUILD_DIR)/boot.o $(BUILD_DIR)/kterm.o $(BUILD_DIR)/kernel.o $(BUILD_DIR)/memory.o $(BUILD_DIR)/paging.o $(BUILD_DIR)/VFS.o $(BUILD_DIR)/pit.o $(BUILD_DIR)/time.o $(BUILD_DIR)/keyboard.o $(BUILD_DIR)/io.o $(BUILD_DIR)/processor.o $(BUILD_DIR)/gdtc.o $(BUILD_DIR)/idt.o $(BUILD_DIR)/pic.o $(BUILD_DIR)/sv-terminal.o $(BUILD_DIR)/string.o $(BUILD_DIR)/prekernel.o $(BUILD_DIR)/cpu.o $(BUILD_DIR)/KHeap.o $(BUILD_DIR)/pci.o $(BUILD_DIR)/pcidevice.o $(BUILD_DIR)/atapiDevice.o $(BUILD_DIR)/ataDevice.o $(BUILD_DIR)/rsdp.o $(BUILD_DIR)/acpi.o SRC_DIR = source BUILD_DIR = build @@ -42,7 +42,13 @@ test: test_iso: $(EMULATOR) -boot d -cdrom $(BUILD_DIR)/barinkOS.iso -serial stdio -vga std -display gtk -m 2G -cpu core2duo -d int -no-reboot -no-shutdown -test_disk: +test_disk: all + sudo losetup /dev/loop9 build/disk.img + sudo mount /dev/loop9 /mnt + sudo cp build/myos.bin /mnt/boot/myos.bin + sudo umount /mnt + sudo losetup -d /dev/loop9 + $(EMULATOR) -boot d -drive format=raw,file=build/disk.img -serial stdio -vga std -display gtk -m 2G -cpu core2duo @@ -103,6 +109,8 @@ $(BUILD_DIR)/acpi.o: $(BUILD_DIR)/pit.o: $(CPP) -c $(SRC_DIR)/kernel/drivers/pit/pit.cpp -o $(BUILD_DIR)/pit.o $(CFLAGS) -fno-exceptions -fno-rtti +$(BUILD_DIR)/VFS.o: + $(CPP) -c $(SRC_DIR)/kernel/vfs/VFS.cpp -o $(BUILD_DIR)/VFS.o $(CFLAGS) -fno-exceptions -fno-rtti $(BUILD_DIR)/keyboard.o: $(CPP) -c $(SRC_DIR)/kernel/drivers/ps-2/keyboard.cpp -o $(BUILD_DIR)/keyboard.o $(CFLAGS) -fno-exceptions -fno-rtti diff --git a/source/kernel/drivers/acpi/acpi.cpp b/source/kernel/drivers/acpi/acpi.cpp index 6366d6f..cf73c27 100644 --- a/source/kernel/drivers/acpi/acpi.cpp +++ b/source/kernel/drivers/acpi/acpi.cpp @@ -9,6 +9,7 @@ void ACPI::initialize(){ // Find the Root System Description Pointer ACPI::rsd_ptr = FindRSD(); printRSD(rsd_ptr); + // Get the Root System Description Table - ACPI::rsd_table = getRSDT(rsd_ptr); + ACPI::rsd_table = getRSDT((RSDPTR*)((uint32_t)rsd_ptr + 0xC00000000)); } \ No newline at end of file diff --git a/source/kernel/drivers/acpi/rsdp.cpp b/source/kernel/drivers/acpi/rsdp.cpp index 5f4991f..910fadb 100644 --- a/source/kernel/drivers/acpi/rsdp.cpp +++ b/source/kernel/drivers/acpi/rsdp.cpp @@ -18,17 +18,16 @@ void printRSD(RSDPTR* rsd){ } RSDPTR* FindRSD(){ - char* memory_byte = (char*) 0x000f2e14; + char* memory_byte = (char*) 0xC00f2e14; const void* string = "RSD PTR "; - for( ; (uint32_t) memory_byte < 0x0100000; memory_byte+=10){ + for( ; (uint32_t) memory_byte < 0xC0100000; memory_byte+=10){ if( memcmp(memory_byte , string , 8 ) == 0 ) { printf("RSD PTR found at 0x%x !\n", memory_byte); break; } } - printRSD((RSDPTR*) memory_byte); return (RSDPTR*) memory_byte; } diff --git a/source/kernel/filesystem/FAT/FAT16.cpp b/source/kernel/filesystem/FAT/FAT16.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/source/kernel/filesystem/FAT/FAT16.h b/source/kernel/filesystem/FAT/FAT16.h deleted file mode 100644 index bc24274..0000000 --- a/source/kernel/filesystem/FAT/FAT16.h +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once -#include "../../vfs/File.h" - - -class FAT16 : File { -public: - -}; \ No newline at end of file diff --git a/source/kernel/kernel.cpp b/source/kernel/kernel.cpp index 3b10a2b..f0f9bc9 100644 --- a/source/kernel/kernel.cpp +++ b/source/kernel/kernel.cpp @@ -14,11 +14,11 @@ extern "C"{ #include "drivers/pci/pci.h" #include "drivers/pit/pit.h" #include "drivers/acpi/acpi.h" -#include "drivers/ide/ide.h" #include "i386/processor.h" #include "terminal/kterm.h" #include "interrupts/idt.h" #include "serial.h" +#include "vfs/VFS.h" extern "C" void LoadGlobalDescriptorTable(); extern "C" void jump_usermode(); @@ -54,17 +54,13 @@ extern "C" void kernel () // Enable interrupts asm volatile("STI"); + initHeap(); - pit_initialise(); - - // ACPI::initialize(); + // ACPI::initialize(); // FIXME: improper reading of bios memory PCI::Scan(); - - //TestIDEController(); - - processor::initialize(); + FileSystem::initialize(); printf("Enable Protected mode and jump to kernel main\n"); diff --git a/source/kernel/supervisorterminal/superVisorTerminal.cpp b/source/kernel/supervisorterminal/superVisorTerminal.cpp index ea2cc32..f2790e8 100644 --- a/source/kernel/supervisorterminal/superVisorTerminal.cpp +++ b/source/kernel/supervisorterminal/superVisorTerminal.cpp @@ -70,160 +70,14 @@ extern "C" void startSuperVisorTerminal() kterm_init(); printf("|=== BarinkOS ===|\n"); } - if(strncmp("FAT", command, characterCount) == 0) + if(strncmp("LIST", command, characterCount) == 0) { - - int devNumber = 0 ; - for ( auto device : ide_devices){ - if (!device.Reserved) - continue; - printf("Device %d\n" , devNumber); - printf (" Device on Channel: (0x%x) %s\n" ,device.Channel, device.Channel == 0 ? "Primary" : "Secondary"); - printf (" Device drive:(0x%x) %s\n" , device.Drive, device.Drive? "Slave" : "Master"); - printf (" Device Type:(0x%x) %s\n" , device.Type, device.Type ? "ATAPI" : "ATA"); - devNumber ++; - - } - - enum BUS_PORT { - Primary= 0x1f0, - Secondary = 0x170 - }; - - - // FIXME: If no drive is connected we continue trying to read from - // a not connected drive! - ATA_DEVICE::Identify((uint16_t) BUS_PORT::Primary, DEVICE_DRIVE::MASTER); - - const int C = 0; - const int H = 0; - const int HPC = 16; - const int SPT = 63; - - int S = 1; - uint32_t LBA = (C*HPC+H) * SPT + (S-1); - printf("LBA: %d\n" , LBA); - uint16_t buffer [256]; - - - ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, LBA, buffer); - - MBR* mbr = (MBR*) buffer; - - printf("BootSector: 0x%x\n", mbr->ValidBootsector ); - for( int i = 0 ; i < 4 ; i ++){ - PartitionTableEntry PT = mbr->TableEntries[i]; - - printf("Partition %d [ %d sectors, PartitionType: %x, 0x%x, \nLBA Start: 0x%x ]\n" , - i, PT.Number_sectors_inPartition, PT.PartitionType, mbr->uniqueID, PT.LBA_partition_start ); - } - - // Find the BiosParameter block - uint16_t biosparameterblock[256]; - ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, mbr->TableEntries[0].LBA_partition_start, biosparameterblock); - - auto* bpb = (BiosParameterBlock*) biosparameterblock; - - - printf("\nBPB: Bytes per Sector %d\n", bpb->BytesPerSector ); - printf("OEM ID: %s\n", bpb->OEM_id); - printf("Bytes per sector: %d\n", bpb->BytesPerSector); - printf("Sectors per cluster: %d\n", bpb->SectorsPerCluster); - printf("Reserved sectors: %d\n", bpb->ReservedSectors); - printf("Number of FAT: %d\n", bpb->NumberOfFileAllocationTables); - printf("Number of Dir entries: %d\n", bpb->NumberOfDirectoryEntries); - printf("Total Sectors in volume: %d\n", bpb->TotalSectorsInLogicalVolume); - printf("Sectors per FAT: %d\n", bpb->NumberOfSectorsPerFAT); - - /** - * @brief File Allocation Table - */ - - - uint32_t FATAddress = mbr->TableEntries[0].LBA_partition_start + bpb->ReservedSectors ; - uint16_t FAT[256]; - ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, FATAddress, FAT ); - - // Show data in terminal - for(unsigned short i : FAT) { - printf("%x ", i); - } - kterm_put('\n'); - - - uint32_t RootDirectoryRegion = FATAddress + ( bpb->NumberOfFileAllocationTables * bpb->NumberOfSectorsPerFAT ); - uint32_t DataRegion = RootDirectoryRegion + ((bpb->NumberOfDirectoryEntries * 32) / bpb->BytesPerSector ); - - uint16_t data2 [256]; - ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, RootDirectoryRegion, data2 ); - auto* RootDirectory = (DirectoryEntry*) data2; - // List files in root - for(int i= 0; i < bpb->NumberOfDirectoryEntries ; i++ ) - { - auto* entry = (DirectoryEntry*)((uint32_t) RootDirectory + (i * sizeof(DirectoryEntry))); - - if( entry->filename[0] == (uint8_t) 0x00 ) - break; // There are no more entries in this directory or the entry is free - - if( (entry->attribute & 0x01) == 0x01 || (entry->attribute & 0x20) == 0x20) - continue; // Skip listing if hidden or Achieve flag is set - - // Print the filename; - for(char n : entry->filename){ - if(n == 0x20) - break; - kterm_put(n); - } - kterm_put('\n'); - - for(unsigned char n : entry->Extension){ - kterm_put(n); - } - kterm_put('\n'); - - printf("Attribute: %x \n" , entry->attribute); - printf("FileSize: %d Bytes\n", entry->FilesizeInBytes); - - if( entry->FilesizeInBytes != 0x0 || (entry->attribute & 0x8) == 0x0){ - printf("Show contents"); - - printf( "Start cluster of the file: 0x%x\n" , entry->StartingCluster); - - printf("IS it only 1 cluster? %s\n" , FAT[i] == 0xFFFF? "Yes": "No" ); - - uint32_t sector = DataRegion + ((entry->StartingCluster - 0x02 ) * bpb->SectorsPerCluster); - - - uint16_t dataBlob [256]; - ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, sector, dataBlob ); - for(unsigned short n : dataBlob) - { - kterm_put(n & 0x00ff); - - kterm_put(n >> 8); - }kterm_put('\n'); - - - } - - printf("======================\n"); - - - } - continue; + printf("=============== DIRECTORY LISTING =================\n"); } - - if(strncmp("DEVICES", command, characterCount) == 0){ printf("================ CONNECTED DEVICES ===============\n"); - - - - - - } diff --git a/source/kernel/vfs/File.h b/source/kernel/vfs/File.h deleted file mode 100644 index 5a76c48..0000000 --- a/source/kernel/vfs/File.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -class File { - -public: - virtual const File* Open () const ; // TODO: figure out a proper return value - virtual const char* Read() const; - virtual void Write(); -}; diff --git a/source/kernel/vfs/VFS.cpp b/source/kernel/vfs/VFS.cpp index 8eca77a..1c5cc17 100644 --- a/source/kernel/vfs/VFS.cpp +++ b/source/kernel/vfs/VFS.cpp @@ -1,50 +1,354 @@ #include "VFS.h" +#include "../filesystem/FAT/BiosParameterBlock.h" +#include "../drivers/ide/ide.h" +#include "../drivers/ata/ataDevice.h" +#include "../partitiontable/mbr/MasterBootRecord.h" +#include "../memory/KernelHeap.h" +#include "../../lib/include/string.h" +#include "../filesystem/FAT/DirectoryEntry.h" + +MOUNT_INFO mountInfo; + +PFILESYSTEM _filesystems[DEVICE_MAX]; + +FILE volOpenFile(const char* fname) +{ + if(fname){ + unsigned char device = 'a'; + + char* filename = (char*) fname; + + if(fname[1]== ':'){ + device = fname[0]; + filename += 2; // strip the volume component from the path + } + + if(_filesystems[device - 'a']){ + FILE file = _filesystems[device-'a']->Open(filename); + file.device = device; + return file; + } + } + + FILE file; + file.flags = FS_INVALID; + return file; +} + +extern void volReadFile(PFILE file, unsigned char* Buffer, unsigned int length) +{ + +} +extern void volCloseFile(PFILE file) +{ + if( file->device < DEVICE_MAX){ + // _filesystems[file->device]->Close(file); + } +} + +extern void volRegisterFilesystem(PFILESYSTEM fsys , unsigned int deviceID){ + if(deviceID < DEVICE_MAX) + if(fsys) + _filesystems[deviceID] = fsys; +} + +extern void volUnregisterFilesystem(PFILESYSTEM){ + +} + +extern void volUnregisterFileSystemByID(unsigned int deviceID){ + +} + +enum BUS_PORT { + Primary = 0x1f0, + Secondary = 0x170 +}; + +bool driveAvailable(){ + int devNumber = 0; + for ( auto device : ide_devices){ + if(!device.Reserved) + continue; + devNumber++; + } + + + // FIXME: If no drive is connected we continue trying to read from + // a not connected drive! + ATA_DEVICE::Identify((uint16_t) BUS_PORT::Primary, DEVICE_DRIVE::MASTER); + return true; +} + +MBR* getPartitions(){ + const int C = 0; + const int H = 0; + const int HPC = 16; + const int SPT = 63; + + int S =1; + uint32_t LBA = (C*HPC+H) * SPT + (S-1); + MBR* mbr =(MBR*) malloc(sizeof (MBR)); + + ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, LBA, (uint16_t*)mbr); + + printf("BootSector: 0x%x\n", mbr->ValidBootsector ); + for( int i = 0 ; i < 4 ; i ++){ + PartitionTableEntry PT = mbr->TableEntries[i]; + + printf("Partition %d [ %d sectors, PartitionType: %x, 0x%x, \nLBA Start: 0x%x ]\n" , + i, PT.Number_sectors_inPartition, PT.PartitionType, mbr->uniqueID, PT.LBA_partition_start ); + } + + return mbr; +} + +BiosParameterBlock* getBPB(MBR* mbr){ + BiosParameterBlock* bpb = (BiosParameterBlock*) malloc(sizeof(BiosParameterBlock)); + + ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, mbr->TableEntries[0].LBA_partition_start, (uint16_t*) bpb); + + printf("OEM ID: %s\n", bpb->OEM_id); + printf("Bytes per sector: %d\n", bpb->BytesPerSector); + printf("Sectors per cluster: %d\n", bpb->SectorsPerCluster); + printf("Reserved sectors: %d\n", bpb->ReservedSectors); + printf("Number of FAT: %d\n", bpb->NumberOfFileAllocationTables); + printf("Number of Dir entries: %d\n", bpb->NumberOfDirectoryEntries); + printf("Total Sectors in volume: %d\n", bpb->TotalSectorsInLogicalVolume); + printf("Sectors per FAT: %d\n", bpb->NumberOfSectorsPerFAT); + + + return bpb; +} + +uint16_t* ReadFAT (BiosParameterBlock& bpb , MBR& mbr) { + uint32_t FATAddress = mbr.TableEntries[0].LBA_partition_start + bpb.ReservedSectors ; + uint16_t* FAT = (uint16_t*)malloc(sizeof (uint16_t) * 256); + ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, FATAddress, FAT ); + + // Show data in terminal + /* + for(unsigned short i : FAT) { + printf("%x ", i); + } + kterm_put('\n');*/ + + return FAT; + +} + +void listFilesInRoot(MBR& mbr, BiosParameterBlock& bpb ){ + auto FATAddress = mbr.TableEntries[0].LBA_partition_start + bpb.ReservedSectors; + uint32_t RootDirectoryRegion = FATAddress + ( bpb.NumberOfFileAllocationTables * bpb.NumberOfSectorsPerFAT ); + uint32_t DataRegion = RootDirectoryRegion + ((bpb.NumberOfDirectoryEntries * 32) / bpb.BytesPerSector ); + uint16_t* FAT = ReadFAT(bpb, mbr); + + + uint16_t data2 [256]; + ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, RootDirectoryRegion, data2 ); + auto* RootDirectory = (DirectoryEntry*) data2; + // List files in root + for(int i= 0; i < bpb.NumberOfDirectoryEntries ; i++ ) { + auto *entry = (DirectoryEntry * )((uint32_t) RootDirectory + (i * sizeof(DirectoryEntry))); + + if (entry->filename[0] == (uint8_t) 0x00) + break; // There are no more entries in this directory or the entry is free + + if ((entry->attribute & 0x01) == 0x01 || (entry->attribute & 0x20) == 0x20) + continue; // Skip listing if hidden or Achieve flag is set + + // Print the filename; + for (char n: entry->filename) { + if (n == 0x20) + break; + kterm_put(n); + } + kterm_put('\n'); + + for (unsigned char n: entry->Extension) { + kterm_put(n); + } + kterm_put('\n'); + + printf("Attribute: %x \n", entry->attribute); + printf("FileSize: %d Bytes\n", entry->FilesizeInBytes); + + if (entry->FilesizeInBytes != 0x0 || (entry->attribute & 0x8) == 0x0) { + printf("Show contents"); + + printf("Start cluster of the file: 0x%x\n", entry->StartingCluster); + + printf("IS it only 1 cluster? %s\n", FAT[i] == 0xFFFF ? "Yes" : "No"); + + uint32_t sector = DataRegion + ((entry->StartingCluster - 0x02) * bpb.SectorsPerCluster); + + + uint16_t dataBlob[256]; + ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, sector, dataBlob); + for (unsigned short n: dataBlob) { + kterm_put(n & 0x00ff); + + kterm_put(n >> 8); + } + kterm_put('\n'); + + + } + printf("======================\n"); + + } + +} + + /* - * TODO: Implement this!! - * +FILE fsysFatDirectory (const char* DirectoryName){ + FILE file; + unsigned char* buf; + PDIRECTORY directory; + + char DosFileName[11]; + ToDosFileName(DirectoryName, DosFileName, 11); + DosFileName[11] =0; + + for (int sector=0; sector <14 ; sector++){ + ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, mountInfo.rootOffset + sector, (uint16_t*)buf); + directory = (PDIRECTORY) buf; + + for (int i =0; i < 16; i++){ + char name[11]; + memcpy(name, directory->Filename, 11); + name[11]=0; + + if(strncmp(DosFileName, name, 11) == 0){ + strcpy(file.name, DirectoryName); + file.id = 0; + file.currentCluster = directory->FirstCluster; + file.eof = 0; + file.filelength = directory->FileSize; + + if(directory->Attrib == 0x10){ + file.flags = FS_DIRECTORY; + } else { + file.flags = FS_FILE; + } + return file; + } + directory++; + } + } + + // Can't find file + file.flags = FS_INVALID; + return file; + +} */ +void fsysFATRead(PFILE file, unsigned char* buffer, unsigned int length){ + if(file){ + unsigned int physSector = 32 + (file->currentCluster - 1); + const unsigned int SECTOR_SIZE = 512; + // read sector + ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, physSector, (uint16_t*) buffer ); + + unsigned int FAT_Offset = file->currentCluster + (file->currentCluster /2); + unsigned int FAT_Sector = 1 + (FAT_Offset / SECTOR_SIZE); + unsigned int entryOffset =FAT_Offset % SECTOR_SIZE; + + uint8_t FAT[SECTOR_SIZE*2]; + ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, FAT_Sector,(uint16_t*) FAT); // Read 1st FAT sector + + ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, FAT_Sector +1, (uint16_t*)FAT+SECTOR_SIZE); + + // read entry for next cluster + uint16_t nextCluster = *(uint16_t*) &FAT[entryOffset]; + + // test if entry is odd or even + if(file->currentCluster & 0x0001){ + nextCluster>>= 4; // grab the high 12 bits + }else{ + nextCluster &= 0x0FFF; // grab the low 12 bits + } + + // test for end of file + if(nextCluster >= 0xff8){ + file->eof -1; + return; + } + + // test for file corruption + if(nextCluster == 0){ + file->eof =1; + return; + } + + // set next cluster + file->currentCluster = nextCluster; -void VirtualFileSystem::Initialize(FS* root) -{ - root = root; + } } +/* +FILE fsysFatOpenSubDir(FILE kFile, const char* filename){ + FILE file; -void VirtualFileSystem::Open(const char* path) -{ - /* - What does this mean? - 1. Parse the path string - 2. Traverse the graph (Finding the correct Node) - 3. Create some kind of open file pointer thingy - */ + char DosFileName[11]; + ToDosFileName(filename, DosFileName, 11); + DosFileName[11] = 0; + + while(!kFile.eof){ + //read directory + unsigned char buf[512]; + fsysFATRead(&file, buf, 512); + + PDIRECTORY pkDir = (PDIRECTORY) buf; + + for (unsigned int i = 0; i < 16; i++){ + // get current filename + char name[11]; + memcpy(name, pkDir->Filename, 11); + name[11] = 0; + + if(strncmp(name, DosFileName, 11) == 0){ + strcpy(file.name, filename); + file.id = 0; + file.currentCluster = pkDir->FirstCluster; + file.filelength = pkDir->FileSize; + file.eof = 0; + + // set file type; + if(pkDir->Attrib == 0x10){ + file.flags = FS_DIRECTORY; + } else{ + file.flags = FS_FILE; + } + + return file; + } + // go to next entry + pkDir++; + } + } + // unable to find file + file.flags = FS_INVALID; + return file; } +*/ -void VirtualFileSystem::Read() +void FileSystem::initialize() { - // NOTE: we need some way to know what file we wish to read from -} + MBR* mbr = getPartitions(); + BiosParameterBlock* bootsector = getBPB(mbr); + listFilesInRoot(*mbr, *bootsector); +/* + mountInfo.numSectors = bootsector->NumberOfSectorsPerFAT; + mountInfo.fatOffset = 1; + mountInfo.fatSize = bootsector->NumberOfSectorsPerFAT; + mountInfo.fatEntrySize = 8; + mountInfo.numRootEntries = bootsector->NumberOfDirectoryEntries; + mountInfo.rootOffset = (bootsector->NumberOfFileAllocationTables * bootsector->NumberOfSectorsPerFAT) + 1; + mountInfo.rootSize = (bootsector->NumberOfDirectoryEntries * 32) / bootsector->BytesPerSector;*/ -void VirtualFileSystem::Write() -{ - // NOTE: we need some way to know what file we wish to write to -} -void VirtualFileSystem::Mount(const char* path, FS* FileSystem) -{ - /* - What does this mean? - 1. Parse the path string - 2. Add a node to our internal graph - */ -} - -void VirtualFileSystem::UnMount(FS* FileSystem) -{ - /* - What does this mean? - 1. Parse the path string - 2. Remve a node to our internal graph - */ } \ No newline at end of file diff --git a/source/kernel/vfs/VFS.h b/source/kernel/vfs/VFS.h index b525cc2..f7ec66d 100644 --- a/source/kernel/vfs/VFS.h +++ b/source/kernel/vfs/VFS.h @@ -1,29 +1,76 @@ #pragma once +#include +#define FS_FILE 0 +#define FS_DIRECTORY 1 +#define FS_INVALID 2 +#define DEVICE_MAX 26 -class VirtualFileSystem{ -public: - void Initialize( FS* root); - void Open (const char* path); - void Read(); - void Write(); +typedef struct _FILE { + char name [32]; + uint32_t flags; + uint32_t filelength; + uint32_t id; + uint32_t eof; + uint32_t position; + uint32_t currentCluster; + uint32_t device; +}FILE, *PFILE; - void Mount(const char* path,FS* FileSystem); - void UnMount(FS* FileSystem); +typedef struct _FILE_SYSTEM{ + char name[8]; + FILE (*Directory) (const char* Directoryname); + void (*Mount) (); + void (*Read) (PFILE file, unsigned char* buffer, unsigned int length); + void (Close) (PFILE); + FILE (*Open) (const char* filename); +}FILESYSTEM, *PFILESYSTEM; -private: - FS* root; +typedef struct _MOUNT_INFO{ + uint32_t numSectors; + uint32_t fatOffset; + uint32_t numRootEntries; + uint32_t rootOffset; + uint32_t rootSize; + uint32_t fatSize; + uint32_t fatEntrySize; +}MOUNT_INFO, *PMOUNT_INFO; + +typedef struct _DIRECTORY{ + uint8_t Filename[8]; + uint8_t Ext[3]; + uint8_t Attrib; + uint8_t Reserved; + uint8_t TimeCreatedMs; + uint16_t TimeCreated; + uint16_t DateCreated; + uint16_t DateLastAccessed; + uint16_t FirstClusterHiBytes; + uint16_t LastModTime; + uint16_t LastModDate; + uint16_t FirstCluster; + uint32_t FileSize; +}DIRECTORY, *PDIRECTORY; +// Date Format +// [0..4] Day +// [5..8] Month +// [9..15] Year + +// Time Format +// [0..4] Seconds +// [5..10] Minute +// [11..15] Hour +extern PFILESYSTEM _filesystems[DEVICE_MAX]; + + FILE volOpenFile(const char* fname); + + void volCloseFile(PFILE file); + void volRegisterFilesystem(PFILESYSTEM, unsigned int deviceID); + void volUnregisterFilesystem(PFILESYSTEM); + void volUnregisterFileSystemByID(unsigned int deviceID); -}; - -struct FS -{ - const char* name ; - int DeviceID; - int ManufacturerID; - FS* next; - char**(Read)(); - void*(Write)(); - void*(Open)(); -}; + class FileSystem{ + public: + static void initialize(); + }; \ No newline at end of file