From 50bf952a49b88290075738a5c14ba0362c92551d Mon Sep 17 00:00:00 2001 From: Nigel Date: Tue, 21 Feb 2023 21:43:14 +0100 Subject: [PATCH] Basic idea's are created for the storage solution - Added boot device info parsing to the kernel - Added a pointer in the kernel to our pre-kernel BootInfo structure - Created a layout for the FAT driver - Created a layout for the virtual filesystem - Separated IDE driver from the basic atapio driver. This will ensure we are not using one or the other - The create_harddrive shell script will now actually build a harddrive image of the kernel - The virtual filesystem initializes and creates a filesystem structure for every FAT16 partition in the master boot record --- kernel/Makefile | 10 +- kernel/kernel.cpp | 28 +++- kernel/prekernel/prekernel.cpp | 6 +- .../ata pio/{ataDevice.cpp => ATAPIO.cpp} | 19 ++- .../storage/ata pio/{ataDevice.h => ATAPIO.h} | 8 +- .../filesystems}/EXT2/SuperBlock.h | 0 .../filesystems}/FAT/BiosParameterBlock.h | 0 .../filesystems}/FAT/DirectoryEntry.h | 0 .../filesystems}/FAT/ExtendBootRecord.h | 0 .../filesystems}/FAT/FAT.cpp | 77 +++++++++-- .../filesystems}/FAT/FAT.h | 16 ++- .../partitiontables}/mbr/MasterBootRecord.h | 13 +- .../mbr/PartitionTableEntry.h | 0 kernel/storage/vfs/StorageTypes.h | 12 +- kernel/storage/vfs/VFS.cpp | 54 -------- kernel/storage/vfs/vfs.cpp | 130 ++++++++++++++++++ kernel/storage/vfs/{VFS.h => vfs.h} | 7 +- .../supervisorterminal/superVisorTerminal.cpp | 8 +- scripts/create_harddrive.sh | 25 ++-- 19 files changed, 290 insertions(+), 123 deletions(-) rename kernel/storage/ata pio/{ataDevice.cpp => ATAPIO.cpp} (95%) rename kernel/storage/ata pio/{ataDevice.h => ATAPIO.h} (81%) rename kernel/{filesystem => storage/filesystems}/EXT2/SuperBlock.h (100%) rename kernel/{filesystem => storage/filesystems}/FAT/BiosParameterBlock.h (100%) rename kernel/{filesystem => storage/filesystems}/FAT/DirectoryEntry.h (100%) rename kernel/{filesystem => storage/filesystems}/FAT/ExtendBootRecord.h (100%) rename kernel/{filesystem => storage/filesystems}/FAT/FAT.cpp (80%) rename kernel/{filesystem => storage/filesystems}/FAT/FAT.h (63%) rename kernel/{partitiontable => storage/partitiontables}/mbr/MasterBootRecord.h (71%) rename kernel/{partitiontable => storage/partitiontables}/mbr/PartitionTableEntry.h (100%) delete mode 100644 kernel/storage/vfs/VFS.cpp create mode 100644 kernel/storage/vfs/vfs.cpp rename kernel/storage/vfs/{VFS.h => vfs.h} (58%) mode change 100644 => 100755 scripts/create_harddrive.sh diff --git a/kernel/Makefile b/kernel/Makefile index 923680f..4d333d1 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -33,7 +33,8 @@ OFILES = $(OBJ_DIR)/boot.o \ $(OBJ_DIR)/atapiDevice.o \ $(OBJ_DIR)/ataDevice.o \ $(OBJ_DIR)/rsdp.o \ - $(OBJ_DIR)/acpi.o + $(OBJ_DIR)/acpi.o \ + $(OBJ_DIR)/fat.o OBJ_LINK_LIST = $(CRTI_OBJ) $(CRTBEGIN_OBJ) $(OFILES) $(CRTEND_OBJ) $(CRTN_OBJ) INTERNAL_OBJS = $(CRTI_OBJ) $(OFILES) $(CRTN_OBJ) @@ -79,7 +80,7 @@ $(OBJ_DIR)/atapiDevice.o: $(CPP) -c storage/atapi/atapiDevice.cpp -o $(OBJ_DIR)/atapiDevice.o $(CFLAGS) -fno-exceptions -fno-rtti $(OBJ_DIR)/ataDevice.o: - $(CPP) -c "storage/ata pio/ataDevice.cpp" -o $(OBJ_DIR)/ataDevice.o $(CFLAGS) -fno-exceptions -fno-rtti + $(CPP) -c "storage/ata pio/ATAPIO.cpp" -o $(OBJ_DIR)/ataDevice.o $(CFLAGS) -fno-exceptions -fno-rtti $(OBJ_DIR)/rsdp.o: $(CPP) -c acpi/rsdp.cpp -o $(OBJ_DIR)/rsdp.o $(CFLAGS) -fno-exceptions -fno-rtti @@ -91,7 +92,7 @@ $(OBJ_DIR)/pit.o: $(CPP) -c drivers/pit/pit.cpp -o $(OBJ_DIR)/pit.o $(CFLAGS) -fno-exceptions -fno-rtti $(OBJ_DIR)/VFS.o: - $(CPP) -c storage/vfs/VFS.cpp -o $(OBJ_DIR)/VFS.o $(CFLAGS) -fno-exceptions -fno-rtti + $(CPP) -c storage/vfs/vfs.cpp -o $(OBJ_DIR)/VFS.o $(CFLAGS) -fno-exceptions -fno-rtti $(OBJ_DIR)/keyboard.o: $(CPP) -c drivers/ps-2/keyboard.cpp -o $(OBJ_DIR)/keyboard.o $(CFLAGS) -fno-exceptions -fno-rtti @@ -117,6 +118,9 @@ $(OBJ_DIR)/prekernel.o: $(OBJ_DIR)/processor.o: $(CPP) -c i386/processor.cpp -o $(OBJ_DIR)/processor.o $(CFLAGS) -fno-exceptions -fno-rtti +$(OBJ_DIR)/fat.o: + $(CPP) -c storage/filesystems/FAT/FAT.cpp -o $(OBJ_DIR)/fat.o $(CFLAGS) -fno-exceptions -fno-rtti + # Assembly -> Object files $(OBJ_DIR)/boot.o: $(AS) boot/boot.s -o $(OBJ_DIR)/boot.o diff --git a/kernel/kernel.cpp b/kernel/kernel.cpp index b91b517..7609e71 100644 --- a/kernel/kernel.cpp +++ b/kernel/kernel.cpp @@ -14,11 +14,11 @@ #include "terminal/kterm.h" #include "interrupts/idt.h" #include "serial.h" -#include "storage/vfs/VFS.h" -#include "../CoreLib/Memory.h" +#include "storage/vfs/vfs.h" extern "C" void LoadGlobalDescriptorTable(); extern "C" void jump_usermode(); +extern BootInfoBlock* BIB; extern "C" void kernel () { @@ -26,7 +26,6 @@ extern "C" void kernel () init_serial(); kterm_init(); - setup_tss(); initGDT(); initidt(); @@ -41,12 +40,33 @@ extern "C" void kernel () pit_initialise(); ACPI::initialize(); - //PCI::Scan(); + PCI::Scan(); processor::initialize(); processor::enable_protectedMode(); + printf("Boot device: 0x%x\n", BIB->bootDeviceID); + unsigned int part3 = BIB->bootDeviceID & 0xFF; + unsigned int part2 = (BIB->bootDeviceID & 0xFF00) >> 8; + unsigned int part1 = (BIB->bootDeviceID & 0xFF0000) >> 16; + unsigned int drive = (BIB->bootDeviceID & 0xFF000000) >> 24; + if (drive == 0x80 ) + printf("booted from disk!\n"); + if(drive == 0x00) + printf("booted from floppy disk\n"); + + printf("Part1: %d, Part2: %d, Part3: %d\n", part1, part2 , part3); + VirtualFileSystem::initialize(); + // Try and open hello.txt file + VirtualFileSystem::OpenFile("a:hello.txt"); + + + + // Try and open grub.cfg file + VirtualFileSystem::OpenFile("a:boot/grub/grub.cfg"); + + #ifdef USERMODE_RELEASE // Lets jump into user mode diff --git a/kernel/prekernel/prekernel.cpp b/kernel/prekernel/prekernel.cpp index 0934212..e12bcf0 100644 --- a/kernel/prekernel/prekernel.cpp +++ b/kernel/prekernel/prekernel.cpp @@ -6,7 +6,7 @@ #define CHECK_FLAG(flags, bit) ((flags) & (1 <<(bit))) #define VADDR_TO_PADDR(vaddr) (vaddr - 0xC0000000) #define PADDR_TO_VADDR(paddr) (paddr + 0xC0000000) -multiboot_info_t* global_mbi; +BootInfoBlock* BIB; extern "C" void prekernelSetup ( unsigned long magic, multiboot_info_t* mbi) { @@ -21,7 +21,6 @@ extern "C" void prekernelSetup ( unsigned long magic, multiboot_info_t* mbi) } mbi = PADDR_TO_VADDR(mbi); - global_mbi = mbi; // Setup the physical memory manager immmediatly // Doing so saves the complications of doing it later when @@ -82,13 +81,14 @@ extern "C" void prekernelSetup ( unsigned long magic, multiboot_info_t* mbi) } // allocate a full block for the other boot info! - BootInfoBlock* BIB = (BootInfoBlock*) allocate_block(); + BIB = (BootInfoBlock*) allocate_block(); /* is boot device valid ? */ if (CHECK_FLAG (mbi->flags, 1)) { BIB->bootDeviceID = mbi->boot_device; + } else{ BIB->bootDeviceID = 0x11111111; } diff --git a/kernel/storage/ata pio/ataDevice.cpp b/kernel/storage/ata pio/ATAPIO.cpp similarity index 95% rename from kernel/storage/ata pio/ataDevice.cpp rename to kernel/storage/ata pio/ATAPIO.cpp index b38773c..57647f5 100644 --- a/kernel/storage/ata pio/ataDevice.cpp +++ b/kernel/storage/ata pio/ATAPIO.cpp @@ -1,4 +1,4 @@ -#include "ataDevice.h" +#include "ATAPIO.h" #include "../../io/io.h" #define IS_BIT_SET(x, bit) ((x >> bit & 0x1) == 1) @@ -91,7 +91,7 @@ void ATAPIO::Write(uint16_t data, DEVICE_DRIVE dev){ } -void ATAPIO::Identify(uint16_t DEVICE_CHANNEL,DEVICE_DRIVE drive ){ +bool ATAPIO::Identify(ATAPIO_PORT DEVICE_CHANNEL, DEVICE_DRIVE drive ){ // lets ignore which port we actually want to check for now ! /* @@ -125,12 +125,11 @@ void ATAPIO::Identify(uint16_t DEVICE_CHANNEL,DEVICE_DRIVE drive ){ // Select the target drive outb(DEVICE_CHANNEL | 6, drive); // on the primary bus select the master drive outb(DEVICE_CHANNEL | 6 , 0x0); // write 0 to the controlport for some reason - outb(DEVICE_CHANNEL | 6, drive); uint8_t status = inb(DEVICE_CHANNEL | 7 ); if(status == 0x00){ printf("No drive\n"); - return; + return false; } // send the identify command; outb(DEVICE_CHANNEL | 7, 0xEC); @@ -153,7 +152,7 @@ void ATAPIO::Identify(uint16_t DEVICE_CHANNEL,DEVICE_DRIVE drive ){ uint8_t status2 = inb(DEVICE_CHANNEL | 7); if( status2 == 0x00){ printf("No drive\n"); - return; + return false; } //printf("Waiting until ready...\n"); @@ -163,7 +162,7 @@ void ATAPIO::Identify(uint16_t DEVICE_CHANNEL,DEVICE_DRIVE drive ){ if( status2 & 0x01){ printf("Error!\n"); - return ; + return false; } uint16_t deviceIdentify [256] = {0}; @@ -182,17 +181,17 @@ void ATAPIO::Identify(uint16_t DEVICE_CHANNEL,DEVICE_DRIVE drive ){ kterm_put((char)(deviceIdentify[i] & 0x00FF)); } kterm_put('\n'); - + return true; } -void ATAPIO::Soft_Reset(uint8_t DEVICE_CHANNEL,DEVICE_DRIVE drive){ +void ATAPIO::Soft_Reset(ATAPIO_PORT DEVICE_CHANNEL, DEVICE_DRIVE drive){ printf("Soft reseting drive...\n"); - // outb(channels[DEVICE_CHANNEL].base + 7 , 0x4); + outb(DEVICE_CHANNEL + 7 , 0x4); // wait a bit.. for(int i = 0 ; i < 1000000; i++){ asm volatile("NOP"); } - //outb(channels[DEVICE_CHANNEL].base + 7 , 0x0); + outb(DEVICE_CHANNEL + 7 , 0x0); } diff --git a/kernel/storage/ata pio/ataDevice.h b/kernel/storage/ata pio/ATAPIO.h similarity index 81% rename from kernel/storage/ata pio/ataDevice.h rename to kernel/storage/ata pio/ATAPIO.h index f5fb255..5f3dab8 100644 --- a/kernel/storage/ata pio/ataDevice.h +++ b/kernel/storage/ata pio/ATAPIO.h @@ -3,8 +3,6 @@ #include "../ide/ideCommands.h" #include "../ide/sampleIDE.definitions.h" #include "../../devices/BlockDevice.h" - - #include "../../terminal/kterm.h" /* @@ -19,7 +17,7 @@ enum DEVICE_DRIVE{ }; -enum BUS_PORT { +enum ATAPIO_PORT { Primary = 0x1f0, Secondary = 0x170 }; @@ -29,10 +27,10 @@ enum BUS_PORT { class ATAPIO { public: - static void Identify(uint16_t, DEVICE_DRIVE); + static bool Identify(ATAPIO_PORT, DEVICE_DRIVE); static void Read (uint16_t, DEVICE_DRIVE, uint32_t, uint16_t*); static void Write(uint16_t, DEVICE_DRIVE); - static void Soft_Reset(uint8_t ,DEVICE_DRIVE ); + static void Soft_Reset(ATAPIO_PORT , DEVICE_DRIVE ); }; diff --git a/kernel/filesystem/EXT2/SuperBlock.h b/kernel/storage/filesystems/EXT2/SuperBlock.h similarity index 100% rename from kernel/filesystem/EXT2/SuperBlock.h rename to kernel/storage/filesystems/EXT2/SuperBlock.h diff --git a/kernel/filesystem/FAT/BiosParameterBlock.h b/kernel/storage/filesystems/FAT/BiosParameterBlock.h similarity index 100% rename from kernel/filesystem/FAT/BiosParameterBlock.h rename to kernel/storage/filesystems/FAT/BiosParameterBlock.h diff --git a/kernel/filesystem/FAT/DirectoryEntry.h b/kernel/storage/filesystems/FAT/DirectoryEntry.h similarity index 100% rename from kernel/filesystem/FAT/DirectoryEntry.h rename to kernel/storage/filesystems/FAT/DirectoryEntry.h diff --git a/kernel/filesystem/FAT/ExtendBootRecord.h b/kernel/storage/filesystems/FAT/ExtendBootRecord.h similarity index 100% rename from kernel/filesystem/FAT/ExtendBootRecord.h rename to kernel/storage/filesystems/FAT/ExtendBootRecord.h diff --git a/kernel/filesystem/FAT/FAT.cpp b/kernel/storage/filesystems/FAT/FAT.cpp similarity index 80% rename from kernel/filesystem/FAT/FAT.cpp rename to kernel/storage/filesystems/FAT/FAT.cpp index 3f48492..9161c6b 100644 --- a/kernel/filesystem/FAT/FAT.cpp +++ b/kernel/storage/filesystems/FAT/FAT.cpp @@ -1,20 +1,40 @@ // // Created by nigel on 21/02/23. // - #include "FAT.h" +#include "../../ata pio/ATAPIO.h" +#include "../../../memory/KernelHeap.h" +#include -void FAT::Read() +void FAT::Read(PFILE file, unsigned char* buffer , unsigned int length) { } -void FAT::Open() +FILE FAT::Open(char* filename) { + char* tokstate = NULL; + char* nextdir = strtok(filename, "/", &tokstate); + + while (nextdir) + { + + // Read the root directory + printf("First entry to look for: %s\n", nextdir); + + + + nextdir = strtok(NULL, "/", &tokstate); + } + + FILE file; + file.flags = FS_INVALID; + return file; + } -void FAT::Write() +void FAT::Write(PFILE file, unsigned char* buffer, unsigned int length) { } @@ -28,9 +48,17 @@ void ParseDateInteger(unsigned int date){ } -BiosParameterBlock* getBPB(MBR* mbr, bool DEBUG =false ){ + +BiosParameterBlock* getBPB(PTR_PARTITION partition, bool DEBUG =false ){ BiosParameterBlock* bpb = (BiosParameterBlock*) malloc(sizeof(BiosParameterBlock)); - ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, mbr->TableEntries[0].LBA_partition_start, (uint16_t*) bpb); + + ATAPIO_PORT port = (ATAPIO_PORT)(partition->Disk & 0x01FF); + DEVICE_DRIVE drive = (DEVICE_DRIVE)(partition->Disk >> 16); + + printf("ATAPIO_PORT: 0x%x DEVICE_DRIVE: 0x%x\n",port, drive); + printf("Partition Start Address (LBA): 0x%x\n", partition->StartAddress); + + ATAPIO::Read(ATAPIO_PORT::Primary, DEVICE_DRIVE::MASTER, partition->StartAddress, (uint16_t*) bpb); if(DEBUG) { @@ -44,15 +72,37 @@ BiosParameterBlock* getBPB(MBR* mbr, bool DEBUG =false ){ printf("Sectors per FAT: %d\n", bpb->NumberOfSectorsPerFAT); } - - return bpb; } -uint16_t* ReadFAT (BiosParameterBlock& bpb , MBR& mbr, bool DEBUG = false ) { - uint32_t FATAddress = mbr.TableEntries[0].LBA_partition_start + bpb.ReservedSectors ; +bool FAT::Validate(PTR_PARTITION partition ) +{ + + auto* bootParams = getBPB(partition, true); + + if(bootParams->OEM_id) { + return true; + } + + return false; + +} + +void FAT::Info(_PARTITION *pPartition, PFS pSystem) { + pSystem->Read = FAT::Read; + pSystem->Write = FAT::Write; + pSystem->Open = FAT::Open; + + +} + +uint16_t* ReadFAT (BiosParameterBlock& bpb , PTR_PARTITION partition, bool DEBUG = false ) { + uint32_t FATAddress = partition->StartAddress + bpb.ReservedSectors ; uint16_t* FAT = (uint16_t*)malloc(sizeof (uint16_t) * 256); - ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, FATAddress, FAT ); + + ATAPIO_PORT port = (ATAPIO_PORT)(partition->Disk & 0x01FF); + DEVICE_DRIVE drive = (DEVICE_DRIVE)(partition->Disk >> 16); + ATAPIO::Read(port, drive, FATAddress, FAT ); // Show data in terminal if(DEBUG){ @@ -76,7 +126,7 @@ void readFile(uint32_t DataRegion, DirectoryEntry* entry, uint16_t FATentry, Bio uint16_t dataBlob[256]; - ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, sector, dataBlob); + ATAPIO::Read(ATAPIO_PORT::Primary, DEVICE_DRIVE::MASTER, sector, dataBlob); for (unsigned short n: dataBlob) { kterm_put(n & 0x00ff); @@ -84,7 +134,7 @@ void readFile(uint32_t DataRegion, DirectoryEntry* entry, uint16_t FATentry, Bio } kterm_put('\n'); } - +/* void listFilesInRoot(MBR& mbr, BiosParameterBlock& bpb ){ auto FATAddress = mbr.TableEntries[0].LBA_partition_start + bpb.ReservedSectors; uint32_t RootDirectoryRegion = FATAddress + ( bpb.NumberOfFileAllocationTables * bpb.NumberOfSectorsPerFAT ); @@ -265,3 +315,4 @@ FILE fsysFatOpenSubDir(FILE kFile, const char* filename){ file.flags = FS_INVALID; return file; } + */ diff --git a/kernel/filesystem/FAT/FAT.h b/kernel/storage/filesystems/FAT/FAT.h similarity index 63% rename from kernel/filesystem/FAT/FAT.h rename to kernel/storage/filesystems/FAT/FAT.h index 5aeb10b..bf8cce9 100644 --- a/kernel/filesystem/FAT/FAT.h +++ b/kernel/storage/filesystems/FAT/FAT.h @@ -5,7 +5,7 @@ #include "ExtendBootRecord.h" #include "BiosParameterBlock.h" #include "DirectoryEntry.h" - +#include "../../vfs/StorageTypes.h" // Date Format // [0..4] Day @@ -20,9 +20,17 @@ class FAT { public: - void Open(); - void Read(); - void Write(); + + + + static bool Validate(PTR_PARTITION partition ); + + static FILE Open(char* filename); + static void Read(PFILE file, unsigned char* buffer , unsigned int length); + static void Write(PFILE file, unsigned char* buffer, unsigned int length); + + static void Info(_PARTITION *pPartition, PFS pSystem); + private: enum struct TYPE{ FAT, diff --git a/kernel/partitiontable/mbr/MasterBootRecord.h b/kernel/storage/partitiontables/mbr/MasterBootRecord.h similarity index 71% rename from kernel/partitiontable/mbr/MasterBootRecord.h rename to kernel/storage/partitiontables/mbr/MasterBootRecord.h index 76f8c80..1cb207c 100644 --- a/kernel/partitiontable/mbr/MasterBootRecord.h +++ b/kernel/storage/partitiontables/mbr/MasterBootRecord.h @@ -1,19 +1,19 @@ #pragma once #include #include "PartitionTableEntry.h" -#include "../../memory/KernelHeap.h" -#include "../../storage/ata pio/ataDevice.h" +#include "../../../memory/KernelHeap.h" +#include "../../ata pio/ATAPIO.h" struct MBR { uint8_t code [440]; uint32_t uniqueID; uint16_t Reserved; - PartitionTableEntry TableEntries[4]; + struct PartitionTableEntry TableEntries[4]; uint16_t ValidBootsector; }__attribute__((packed)); -MBR* getPartitions( bool DEBUG = false){ +inline MBR* GetPartitions(bool DEBUG = false){ const int C = 0; const int H = 0; const int HPC = 16; @@ -21,10 +21,13 @@ MBR* getPartitions( bool DEBUG = false){ int S =1; uint32_t LBA = (C*HPC+H) * SPT + (S-1); + MBR* mbr =(MBR*) malloc(sizeof (MBR)); - ATAPIO::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, LBA, (uint16_t*)mbr); + ATAPIO::Read(ATAPIO_PORT::Primary, DEVICE_DRIVE::MASTER, LBA, (uint16_t*)mbr); + + printf("MBR (In Memory) Address 0x%x, Size = %d\n", mbr, sizeof (MBR)); if(DEBUG){ printf("BootSector: 0x%x\n", mbr->ValidBootsector ); for( int i = 0 ; i < 4 ; i ++){ diff --git a/kernel/partitiontable/mbr/PartitionTableEntry.h b/kernel/storage/partitiontables/mbr/PartitionTableEntry.h similarity index 100% rename from kernel/partitiontable/mbr/PartitionTableEntry.h rename to kernel/storage/partitiontables/mbr/PartitionTableEntry.h diff --git a/kernel/storage/vfs/StorageTypes.h b/kernel/storage/vfs/StorageTypes.h index 91e1e07..5acdc27 100644 --- a/kernel/storage/vfs/StorageTypes.h +++ b/kernel/storage/vfs/StorageTypes.h @@ -26,7 +26,15 @@ typedef struct _FILE_SYSTEM{ 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); + void (*Write)(PFILE file, unsigned char* buffer, unsigned int length); + void (*Close) (PFILE); + FILE (*Open) (char* filename); }FILESYSTEM, *PFS; +typedef struct _PARTITION { + uint32_t Disk; + uint32_t StartAddress; + uint32_t Sectors; + uint8_t Fs_hint; + uint8_t Attributes; +}PARTITION, *PTR_PARTITION; diff --git a/kernel/storage/vfs/VFS.cpp b/kernel/storage/vfs/VFS.cpp deleted file mode 100644 index 6cdcf4d..0000000 --- a/kernel/storage/vfs/VFS.cpp +++ /dev/null @@ -1,54 +0,0 @@ -#include "VFS.h" -#include -#include "../../memory/KernelHeap.h" -#include - -PFS VirtualFileSystem::_filesystems[VirtualFileSystem::DEVICE_MAX]; - -void VirtualFileSystem::initialize() -{ - -} - -void VirtualFileSystem::ResolvePath(Path &path) -{ - // See reference material (1) https://man7.org/linux/man-pages/man7/path_resolution.7.html - - char* string_path = path.str(); - void* cpy = string_path; - - bool isAbsolutePath = string_path[0] == '/'; - if(isAbsolutePath) - { - // strip the first slash - string_path++; - } - - char* tokstate = NULL; - char* nextdir = strtok(string_path, "/", &tokstate); - while (nextdir) - { - printf("First entry to look for: %s\n", nextdir); - nextdir = strtok(NULL, "/", &tokstate); - } - - free(cpy); -} - -void VirtualFileSystem::Mount(PFS filesystemDescriptor, unsigned int DeviceID) -{ - if(DeviceID < DEVICE_MAX) - if(filesystemDescriptor) - _filesystems[DeviceID] = filesystemDescriptor; -} - - -void VirtualFileSystem::Unmount(unsigned int DeviceID) { - if(DeviceID < DEVICE_MAX) - _filesystems[DeviceID] = nullptr; -} - - - - - diff --git a/kernel/storage/vfs/vfs.cpp b/kernel/storage/vfs/vfs.cpp new file mode 100644 index 0000000..5597ebc --- /dev/null +++ b/kernel/storage/vfs/vfs.cpp @@ -0,0 +1,130 @@ +#include "vfs.h" +#include +#include "../../memory/KernelHeap.h" +#include "../ata pio/ATAPIO.h" +#include "../partitiontables/mbr/MasterBootRecord.h" +#include "../filesystems/FAT/FAT.h" +#include "StorageTypes.h" + +#include + +PFS VirtualFileSystem::_filesystems[VirtualFileSystem::DEVICE_MAX]; +PTR_PARTITION VirtualFileSystem::_partitions [VirtualFileSystem::PARTITION_MAX]; +unsigned int VirtualFileSystem::num_partitions = 0; + +void VirtualFileSystem::initialize() +{ + + // Mount the boot disk + // NOTE: we assume for now that it is the only disk in the system + // This information could possibly be had from the bootloader (GRUB) + // We als assume it is the primary device on the Master port. + ATAPIO::Soft_Reset(ATAPIO_PORT::Primary, DEVICE_DRIVE::MASTER); + bool isAvailable = ATAPIO::Identify(ATAPIO_PORT::Primary, DEVICE_DRIVE::MASTER); + if(!isAvailable){ + // PANIC!!! + printf("Failed to mount root filesystem!\n"); + return; + } + + auto masterbootrecord = GetPartitions(false); + + for (auto partition : masterbootrecord->TableEntries) + { + if(partition.PartitionType == 0x0) continue; // Consider marked as free + + PTR_PARTITION found_partition = (PARTITION*) malloc(sizeof(PARTITION)); + found_partition->Disk = ATAPIO_PORT::Primary | ( DEVICE_DRIVE::MASTER << 16); + printf("Disk Identifier: 0x%x\n", found_partition->Disk); + found_partition->Attributes = partition.driveAttribute; + found_partition->StartAddress = partition.LBA_partition_start; + found_partition->Sectors = partition.Number_sectors_inPartition; + found_partition->Fs_hint = partition.PartitionType; + + VirtualFileSystem::RegisterPartition(found_partition); + } + + printf("Found %d partitions on disk!\n", num_partitions); + + for (int i = 0; i < num_partitions; i++) + { + auto* partition = _partitions[i]; + // Check the fs_hint for a proper driver + if ( partition->Fs_hint != 0x06){ + printf("Assumed Unkown filesystem!\n"); + continue; + } + // Check if filesystem OK + + printf("Partition Start Address (LBA): 0x%x\n", partition->StartAddress); + bool valid = FAT::Validate(partition); + if(!valid) + { + printf("Not a valid FAT fs!\n"); + continue; + } + + // setup FileSystem Description before mounting + PFS FS_FAT = (PFS)malloc(sizeof(FILESYSTEM)); + + FAT::Info(partition, FS_FAT); + + // Mount the partition/filesystem + Mount(FS_FAT , i); + } + + + + +}; + +void VirtualFileSystem::RegisterPartition(PTR_PARTITION partition) { + _partitions[num_partitions] = partition; + num_partitions++; +} + +FILE VirtualFileSystem::OpenFile(const char* path) +{ + // See reference material (1) https://man7.org/linux/man-pages/man7/path_resolution.7.html + unsigned char device = 'a'; + char* filename = (char*)path; + char* cpy = filename; + + + if(filename[1] == ':'){ + device = filename[0]; + filename += 2; + } + + + if ( _filesystems[device - 'a']){ + // Unfortunately this way the FAT Driver doesn't know which device and which partition to read from + // leaving us hopeless of finding the file. + FILE file = _filesystems[device-'a']->Open(filename); + file.device = device; + free(cpy); + return file; + } + + free(cpy); + FILE file; + file.flags = FS_INVALID; + return file; +} + +void VirtualFileSystem::Mount(PFS filesystemDescriptor, unsigned int DeviceID) +{ + if(DeviceID < DEVICE_MAX) + if(filesystemDescriptor) + _filesystems[DeviceID] = filesystemDescriptor; +} + +void VirtualFileSystem::Unmount(unsigned int DeviceID) { + if(DeviceID < DEVICE_MAX) + _filesystems[DeviceID] = nullptr; +} + + + + + diff --git a/kernel/storage/vfs/VFS.h b/kernel/storage/vfs/vfs.h similarity index 58% rename from kernel/storage/vfs/VFS.h rename to kernel/storage/vfs/vfs.h index 095d7de..4c91792 100644 --- a/kernel/storage/vfs/VFS.h +++ b/kernel/storage/vfs/vfs.h @@ -8,10 +8,13 @@ static void initialize(); static void Mount(PFS fs, unsigned int DeviceID); static void Unmount(unsigned int DeviceID); - static void ResolvePath(Path& path); + static FILE OpenFile(const char* path); + static void RegisterPartition(PTR_PARTITION partition); private: static const unsigned int DEVICE_MAX = 26; + static const unsigned int PARTITION_MAX = 4 * DEVICE_MAX; static PFS _filesystems[DEVICE_MAX]; - + static unsigned int num_partitions; + static PTR_PARTITION _partitions [PARTITION_MAX]; }; \ No newline at end of file diff --git a/kernel/supervisorterminal/superVisorTerminal.cpp b/kernel/supervisorterminal/superVisorTerminal.cpp index f239593..bee1a3e 100644 --- a/kernel/supervisorterminal/superVisorTerminal.cpp +++ b/kernel/supervisorterminal/superVisorTerminal.cpp @@ -1,8 +1,8 @@ #include "superVisorTerminal.h" -#include "../storage/ata pio/ataDevice.h" -#include "../partitiontable/mbr/MasterBootRecord.h" -#include "../filesystem/FAT/BiosParameterBlock.h" -#include "../filesystem/FAT/DirectoryEntry.h" +#include "../storage/ata pio/ATAPIO.h" +#include "../storage/partitiontables/mbr/MasterBootRecord.h" +#include "../storage/filesystems/FAT/BiosParameterBlock.h" +#include "../storage/filesystems/FAT/DirectoryEntry.h" bool isRunning = true; extern "C" void startSuperVisorTerminal() diff --git a/scripts/create_harddrive.sh b/scripts/create_harddrive.sh old mode 100644 new mode 100755 index 45dc117..4e0926d --- a/scripts/create_harddrive.sh +++ b/scripts/create_harddrive.sh @@ -9,11 +9,8 @@ echo "Building a FAT16 filesystem" - -su - -# dd if=/dev/zero of=diks.img bs=512 count=131072 -# fdisk disk.img +dd if=/dev/zero of=disk.img bs=512 count=131072 +fdisk disk.img # Use the following options in fdisk (Format Disk Tool) # We want to create a MBR (NOT GPT) Partition table containing 1 logical disk # with a primary FAT16 partition marked bootable @@ -42,23 +39,23 @@ su # w # Create a "block" device from the disk.img -# losetup /dev/loop9 disk.img +losetup /dev/loop9 disk.img # Format the partition on the disk as FAT16 -# mkdosfs -F16 /dev/loop9 +mkdosfs -F16 /dev/loop9 # Mount the disk to a folder on our dev machine -# mount /dev/loop9 /mnt +mount /dev/loop9 /mnt # Install the grub bootloader onto the disk -# grub-install --no-floppy --modules="normal multiboot" /dev/loop9 --target=i386-pc --boot-directory=/mnt/boot --force +grub-install --no-floppy --modules="normal multiboot" /dev/loop9 --target=i386-pc --boot-directory=/mnt/boot --force # copy the necessary OS files -# cp root/boot/myos.bin /mnt/boot/myos.bin -# cp root/boot/grub/grub.cfg /mnt/boot/grub/grub.cfg +cp root/boot/myos.bin /mnt/boot/myos.bin +cp root/boot/grub/grub.cfg /mnt/boot/grub/grub.cfg # Unmount the device -# umount /mnt +umount /mnt -# Destroy the loop device -# losetup -d /dev/loop9 +# Destroy the loop device +losetup -d /dev/loop9