Skip to content

Commit

Permalink
Updated VFS to handle mounting and unmounting.
Browse files Browse the repository at this point in the history
Is now very basic and partition id's change when drives are removed.
This needs to be changed in the future
  • Loading branch information
Remco123 committed Dec 24, 2019
1 parent fc743a8 commit 34ea7b8
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 47 deletions.
5 changes: 3 additions & 2 deletions kernel/include/system/disks/disk.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ namespace CactusOS
class Disk
{
public:
DiskController* controller;
common::uint32_t controllerIndex; //The real number for the disk
DiskController* controller; //Which controller is controling this disk device
common::uint32_t controllerIndex; //The real number for the disk on the controller
char* identifier = 0; //Disk Identifier

Disk(common::uint32_t controllerIndex, DiskController* controller);

Expand Down
3 changes: 3 additions & 0 deletions kernel/include/system/disks/diskmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ namespace CactusOS

DiskManager();

//Add disk to system and check for filesystems
void AddDisk(Disk* disk);
//Remove disk from system and unmount filesystems
void RemoveDisk(Disk* disk);

char ReadSector(common::uint16_t drive, common::uint32_t lba, common::uint8_t* buf);
char WriteSector(common::uint16_t drive, common::uint32_t lba, common::uint8_t* buf);
Expand Down
6 changes: 4 additions & 2 deletions kernel/include/system/disks/partitionmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ namespace CactusOS
class PartitionManager
{
private:
static void AssignVFS(PartitionTableEntry partition, Disk* disk, VFSManager* vfs);
//Check partition type and assign filesystem driver if available
static void AssignVFS(PartitionTableEntry partition, Disk* disk);
public:
static void DetectAndLoadFilesystems(DiskManager* disks, VFSManager* vfs);
//Read partition descriptor of disk and assign fileysysem if possible
static void DetectAndLoadFilesystem(Disk* disk);
};
}
}
Expand Down
2 changes: 2 additions & 0 deletions kernel/include/system/vfs/vfsmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ namespace CactusOS

VFSManager();
void Mount(VirtualFileSystem* vfs);
void Unmount(VirtualFileSystem* vfs);
void UnmountByDisk(Disk* disk);

int ExtractDiskNumber(char* path, common::uint8_t* idSizeReturn);
bool SearchBootPartition();
Expand Down
9 changes: 9 additions & 0 deletions kernel/src/system/disks/diskmanager.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <system/disks/diskmanager.h>

#include <system/drivers/disk/ide.h>
#include <system/system.h>

using namespace CactusOS;
using namespace CactusOS::common;
Expand Down Expand Up @@ -28,5 +29,13 @@ char DiskManager::WriteSector(uint16_t drive, uint32_t lba, uint8_t* buf)

void DiskManager::AddDisk(Disk* disk)
{
//Add Disk to list of all disk
allDisks.push_back(disk);
//Try to detect filesystems present on disk
PartitionManager::DetectAndLoadFilesystem(disk);
}
void DiskManager::RemoveDisk(Disk* disk)
{
allDisks.Remove(disk); //Remove from list
System::vfs->UnmountByDisk(disk); //And unmount all filesystems using that disk
}
72 changes: 35 additions & 37 deletions kernel/src/system/disks/partitionmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,66 @@

#include <system/vfs/iso9660.h>
#include <system/vfs/fat32.h>
#include <system/system.h>

using namespace CactusOS;
using namespace CactusOS::common;
using namespace CactusOS::core;
using namespace CactusOS::system;

void PartitionManager::DetectAndLoadFilesystems(DiskManager* disks, VFSManager* vfs)
void PartitionManager::DetectAndLoadFilesystem(Disk* disk)
{
BootConsole::WriteLine("Detecting partitions on disks");
uint8_t* Readbuf = new uint8_t[2048];
MemoryOperations::memset(Readbuf, 0, 2048);
char* diskIdentifier = (char*)(disk->identifier == 0 ? "" : disk->identifier);
Log(Info, "Detecting partitions on disk %s", diskIdentifier);

uint8_t* Readbuf = new uint8_t[CDROM_SECTOR_SIZE];
MemoryOperations::memset(Readbuf, 0, CDROM_SECTOR_SIZE);

for(int i = 0; i < disks->allDisks.size(); i++)
char ret = disk->ReadSector(0, Readbuf);
if(ret == 0)
{
char ret = disks->allDisks[i]->ReadSector(0, Readbuf);
if(ret == 0)
MasterBootRecord* mbr = (MasterBootRecord*)Readbuf;
if(mbr->magicnumber != 0xAA55) {
Log(Warning, "MBR magic number is not 0xAA55 instead %w", mbr->magicnumber);
delete Readbuf;
return;
}
//Loop trough partitions
for(int p = 0; p < 4; p++)
{
MasterBootRecord* mbr = (MasterBootRecord*)Readbuf;
if(mbr->magicnumber != 0xAA55)
{
BootConsole::WriteLine("MBR Magic Number is not correct");
BootConsole::Write("Instead it was: "); Print::printfHex16(mbr->magicnumber); BootConsole::WriteLine();
if(mbr->primaryPartitions[p].partition_id == 0x00)
continue;
}
//Loop trough partitions
for(int p = 0; p < 4; p++)
{
if(mbr->primaryPartitions[p].partition_id == 0x00)
continue;

BootConsole::Write("- Disk "); BootConsole::Write(Convert::IntToString(i));
BootConsole::Write(" Partition: "); BootConsole::Write(Convert::IntToString(p));
BootConsole::Write("- Disk "); BootConsole::Write(diskIdentifier);
BootConsole::Write(" Partition: "); BootConsole::Write(Convert::IntToString(p));

if(mbr->primaryPartitions[p].bootable == 0x80)
BootConsole::Write(" Bootable ID: 0x");
else
BootConsole::Write(" ID: 0x");
Print::printfHex(mbr->primaryPartitions[p].partition_id);
BootConsole::Write(" Sectors: "); BootConsole::Write(Convert::IntToString(mbr->primaryPartitions[p].length));
if(mbr->primaryPartitions[p].bootable == 0x80)
BootConsole::Write(" Bootable ID: 0x");
else
BootConsole::Write(" ID: 0x");

Print::printfHex(mbr->primaryPartitions[p].partition_id);
BootConsole::Write(" Sectors: "); BootConsole::Write(Convert::IntToString(mbr->primaryPartitions[p].length));

AssignVFS(mbr->primaryPartitions[p], disks->allDisks[i], vfs);
AssignVFS(mbr->primaryPartitions[p], disk);

BootConsole::WriteLine();
}
}
else
{
BootConsole::Write("Error reading disk: "); BootConsole::Write(Convert::IntToString(i)); BootConsole::Write(" Code: 0x"); Print::printfHex(ret); BootConsole::WriteLine();
BootConsole::WriteLine();
}
}
else {
BootConsole::Write("Error reading disk "); BootConsole::Write(diskIdentifier); BootConsole::Write(" Code: 0x"); Print::printfHex(ret); BootConsole::WriteLine();
}
delete Readbuf;
}

void PartitionManager::AssignVFS(PartitionTableEntry partition, Disk* disk, VFSManager* vfs)
void PartitionManager::AssignVFS(PartitionTableEntry partition, Disk* disk)
{
if(partition.partition_id == 0xCD)
{
BootConsole::Write(" [ISO9660]");
ISO9660* isoVFS = new ISO9660(disk, partition.start_lba, partition.length);
if(isoVFS->Initialize())
vfs->Mount(isoVFS); //Mount the filesystem
System::vfs->Mount(isoVFS); //Mount the filesystem
else
delete isoVFS;
}
Expand All @@ -72,7 +70,7 @@ void PartitionManager::AssignVFS(PartitionTableEntry partition, Disk* disk, VFSM
BootConsole::Write(" [FAT32]");
FAT32* isoVFS = new FAT32(disk, partition.start_lba, partition.length);
if(isoVFS->Initialize())
vfs->Mount(isoVFS); //Mount the filesystem
System::vfs->Mount(isoVFS); //Mount the filesystem
else
delete isoVFS;
}
Expand Down
15 changes: 14 additions & 1 deletion kernel/src/system/drivers/disk/ide.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,20 @@ bool IDEController::Initialize()
BootConsole::Write(" Drive "); BootConsole::Write(Convert::IntToString(ideDevices[i].Size / 1024 / 2));
BootConsole::Write("MB - "); BootConsole::WriteLine((char*)ideDevices[i].Model);

System::diskManager->AddDisk(new Disk(i, this));
Disk* disk = new Disk(i, this);

//////////
// Create Identifier
//////////
int strLen = 40;
while(ideDevices[i].Model[strLen - 1] == ' ' && strLen > 1)
strLen--;
disk->identifier = new char[strLen + 1];

MemoryOperations::memcpy(disk->identifier, ideDevices[i].Model, strLen);
disk->identifier[strLen] = '\0';

System::diskManager->AddDisk(disk);
}

return true;
Expand Down
12 changes: 11 additions & 1 deletion kernel/src/system/drivers/usb/mass_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ bool USBMassStorageDriver::Initialize()
char revision[4 + 1]; revision[4] = '\0'; MemoryOperations::memcpy(revision, retBuf.productRevisionLevel, 4);

Log(Info, "MSD Vendor: %s Product: %s Revision: %s", vendorInfo, productInfo, revision);

//////////
// Create Identifier
//////////
int strLen = 16;
while(productInfo[strLen - 1] == ' ' && strLen > 1)
strLen--;
this->identifier = new char[strLen + 1];
MemoryOperations::memcpy(this->identifier, productInfo, strLen);
this->identifier[strLen] = '\0';
}
else {
delete sendBuf;
Expand Down Expand Up @@ -283,7 +293,7 @@ bool USBMassStorageDriver::SCSIRequestOut(CommandBlockWrapper* request, uint8_t*
//Called when mass storage device is unplugged from system
void USBMassStorageDriver::DeInitialize()
{

System::diskManager->RemoveDisk(this);
}

//Read Sector from mass storage device
Expand Down
6 changes: 2 additions & 4 deletions kernel/src/system/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ void System::Start()

BootConsole::WriteLine("Starting USB Manager");
System::usbManager = new USBManager();
BootConsole::WriteLine("Initializing Virtual File System");
System::vfs = new VFSManager();

BootConsole::WriteLine("Setting up random...");
Random::SetSeed(pit->Ticks());
Expand All @@ -112,10 +114,6 @@ void System::Start()
System::usbManager->USBPoll();

BootConsole::Write("Found a total of: "); BootConsole::Write(Convert::IntToString(System::diskManager->allDisks.size())); BootConsole::WriteLine(System::diskManager->allDisks.size() > 1 ? (char*)" disks" : (char*)" disk");
BootConsole::WriteLine("Initializing Virtual File System");
System::vfs = new VFSManager();
PartitionManager::DetectAndLoadFilesystems(System::diskManager, System::vfs);

BootConsole::Write("Searching for boot partition");
if(System::vfs->SearchBootPartition()) {
BootConsole::Write(" [Found] ("); BootConsole::Write(Convert::IntToString(System::vfs->bootPartitionID)); BootConsole::WriteLine(")");
Expand Down
10 changes: 10 additions & 0 deletions kernel/src/system/vfs/vfsmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ void VFSManager::Mount(VirtualFileSystem* vfs)
{
this->Filesystems->push_back(vfs); //Just add it to the list of known filesystems, easy.
}
void VFSManager::Unmount(VirtualFileSystem* vfs)
{
this->Filesystems->Remove(vfs);
}
void VFSManager::UnmountByDisk(Disk* disk)
{
for(VirtualFileSystem* vfs : *Filesystems)
if(vfs->disk == disk)
Unmount(vfs);
}

bool VFSManager::SearchBootPartition()
{
Expand Down

0 comments on commit 34ea7b8

Please sign in to comment.