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
This commit is contained in:
2023-02-21 21:43:14 +01:00
parent ef2bba5c1c
commit 50bf952a49
19 changed files with 290 additions and 123 deletions

View File

@ -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);
}

View File

@ -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 );
};

View File

@ -0,0 +1,29 @@
#pragma once
#include <stdint.h>
struct SuperBlock {
uint32_t NumberOfInodes;
uint32_t NumberOfBlocks;
uint32_t NumberOfReservedBlocks;
uint32_t NumberOfUnallocatedBlocks;
uint32_t NumberOfUnallocatedInodes;
uint32_t BlockNumberOfSuperBlock;
uint32_t BlockSize;// Something about a shift left
uint32_t FragmentSize;
uint32_t NumberOfBlocksInGroup;
uint32_t NumberOfFragmentsInBlockGroup;
uint32_t NumberOfInodesInBlockGroup;
uint32_t LastMountTime; // POSIX
uint32_t LastWrittenTime; // POSIX
uint16_t TimesMountedSinceCheck;
uint16_t TimesMountedUntilCheck;
uint16_t EXT_SIG ; // 0xef53
uint16_t FS_STATE;
uint16_t ON_ERR;
uint16_t VERSION_MINOR;
uint32_t TimeLastCheck; // POSIX
uint32_t CheckInterval; //POSIX
uint32_t OS_ID; // OS the FS was created with
uint32_t VERSION_MAJOR;
uint16_t UIDReservedBlocks;
uint16_t GIDReservedBlocks;
}__attribute__((packed));

View File

@ -0,0 +1,21 @@
#pragma once
#include <stdint.h>
#include "./ExtendBootRecord.h"
struct BiosParameterBlock {
uint8_t BootLoaderCodeSection [3];
uint8_t OEM_id [8];
uint16_t BytesPerSector ; // I suspect would be 512
uint8_t SectorsPerCluster ;
uint16_t ReservedSectors;
uint8_t NumberOfFileAllocationTables; // Probably equals 2
uint16_t NumberOfDirectoryEntries; // Root directory must contain entire sectors
uint16_t TotalSectorsInLogicalVolume ; // 0 means >65535 sectors in volume , actual count can be found in LargeSectorCount
uint8_t MediaDescriptor ; // Indication the media descriptor type
uint16_t NumberOfSectorsPerFAT;// only in FAT12 / FAT 16
uint16_t NumberOfSectorsPerTrack;
uint16_t NumberOfHeadsOnMedia;
uint32_t NumberOfHiddenSectors;
uint32_t LargeSectorCount;
ExtendedBootRecord_FAT16 ebpb;
}__attribute__((packed));

View File

@ -0,0 +1,37 @@
#pragma once
#include <stdint-gcc.h>
struct DirectoryEntry {
uint8_t filename [8];
uint8_t Extension [3];
uint8_t attribute;
uint8_t Reserved;
uint8_t creation; // Creation in tenths of a second
uint16_t CreationTime; // Time Created NOTE: Multiply the seconds by 2
uint16_t CreationDate; // Date Created
uint16_t LastAccessDate;
uint16_t ReservedFAT32;
uint16_t LastWriteTime;
uint16_t LastWriteDate;
uint16_t StartingCluster;
uint32_t FilesizeInBytes;
}__attribute__((packed));
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;
}__attribute__((packed)) DIRECTORY, *PDIRECTORY;

View File

@ -0,0 +1,32 @@
#pragma once
#include <stdint.h>
struct ExtendedBootRecord_FAT16{
uint8_t DriveNumber;
uint8_t Reserved;
uint8_t Signature;
const uint32_t VOLUME_ID_SERIAL_NUMBER;
uint8_t volume_label [11];
uint8_t Identifier_string [8];
uint8_t bootCode [448];
uint16_t partitionSignature;
}__attribute__((packed));
struct ExtendedBootRecord_FAT32{
uint32_t SectorsPerFAT;
uint16_t Flags;
const uint16_t FAT_VERSION_NUMBER;
uint32_t rootDirectory_clusterNumber;// Often set to 2;
uint16_t FSInfo_SectorNumber;
uint16_t backup_bpb_sectorNumber;
uint8_t Reserved [12];
uint8_t DriveNumber;
uint8_t Reserved2;
uint8_t Signature; // must be 0x28 or 0x29
uint32_t VOLUME_ID_SERIAL;
uint8_t volume_label[11];
uint8_t SystemIdentifierString [8]; // ALWAYS "FAT32 " but spec says do not trust
uint8_t BootCode [420]; // NICE
uint16_t PartitionSignature; // 0xAA55
}__attribute__((packed));

View File

@ -0,0 +1,318 @@
//
// Created by nigel on 21/02/23.
//
#include "FAT.h"
#include "../../ata pio/ATAPIO.h"
#include "../../../memory/KernelHeap.h"
#include <CoreLib/Memory.h>
void FAT::Read(PFILE file, unsigned char* buffer , unsigned int length)
{
}
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(PFILE file, unsigned char* buffer, unsigned int length)
{
}
void ParseDateInteger(unsigned int date){
printf("Date (hex) 0x%x\n", date);
unsigned int year = (date >> 9 )+ 1980;
unsigned int month = (date & 0xf0 ) >> 4;
unsigned int day = date & 0xf ;
printf("Date: (D,M,Y) %d, %d ,%d\n", day , month, year );
}
BiosParameterBlock* getBPB(PTR_PARTITION partition, bool DEBUG =false ){
BiosParameterBlock* bpb = (BiosParameterBlock*) malloc(sizeof(BiosParameterBlock));
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)
{
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;
}
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);
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){
for( unsigned int i =0 ; i < 256 ; i++) {
printf("0x%x ", (unsigned short)FAT[i]);
}
kterm_put('\n');
}
return FAT;
}
void readFile(uint32_t DataRegion, DirectoryEntry* entry, uint16_t FATentry, BiosParameterBlock& bpb ){
printf("Show contents");
printf("Start cluster of the file: 0x%x\n", entry->StartingCluster);
printf("IS it only 1 cluster? %s\n", FATentry == 0xFFFF ? "Yes" : "No");
uint32_t sector = DataRegion + ((entry->StartingCluster - 0x02) * bpb.SectorsPerCluster);
uint16_t dataBlob[256];
ATAPIO::Read(ATAPIO_PORT::Primary, DEVICE_DRIVE::MASTER, sector, dataBlob);
for (unsigned short n: dataBlob) {
kterm_put(n & 0x00ff);
kterm_put(n >> 8);
}
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 );
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 < sizeof (data2) / sizeof (DirectoryEntry); i++ ) {
auto *entry = (DirectoryEntry * )((uint32_t) RootDirectory + (i * sizeof(DirectoryEntry)));
if (entry->filename[0] == (uint8_t) 0x00)
continue; // There are no more entries in this directory or the entry is free
if (entry->attribute & ATTRIBUTES::ATT_HIDDEN)
continue;
if(entry->attribute & ATTRIBUTES::ATTR_SYSTEM)
continue;
if(entry->attribute & ATTRIBUTES::ATTR_VOLUME_ID)
continue;
// Print the filename;
for (char n: entry->filename) {
if (n == 0x20)
break;
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 != 0x10)) {
readFile(DataRegion,entry, FAT[i], bpb);
}
}
free(FAT);
}
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;
}
}
FILE fsysFatOpenSubDir(FILE kFile, const char* filename){
FILE file;
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;
}
*/

View File

@ -0,0 +1,49 @@
//
// Created by nigel on 21/02/23.
//
#pragma once
#include "ExtendBootRecord.h"
#include "BiosParameterBlock.h"
#include "DirectoryEntry.h"
#include "../../vfs/StorageTypes.h"
// Date Format
// [0..4] Day
// [5..8] Month
// [9..15] Year
// Time Format
// [0..4] Seconds
// [5..10] Minute
// [11..15] Hour
class FAT {
public:
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,
FAT16,
FAT32,
VFAT
};
enum ATTRIBUTES {
ATTR_READ_ONLY = 0x01,
ATT_HIDDEN = 0x02,
ATTR_SYSTEM = 0x04,
ATTR_VOLUME_ID = 0x08,
ATTR_DIRECTORY = 0x10,
ATTR_ARCHIVE = 0x20
};
};

View File

@ -0,0 +1,43 @@
#pragma once
#include <stdint-gcc.h>
#include "PartitionTableEntry.h"
#include "../../../memory/KernelHeap.h"
#include "../../ata pio/ATAPIO.h"
struct MBR {
uint8_t code [440];
uint32_t uniqueID;
uint16_t Reserved;
struct PartitionTableEntry TableEntries[4];
uint16_t ValidBootsector;
}__attribute__((packed));
inline MBR* GetPartitions(bool DEBUG = false){
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));
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 ++){
PartitionTableEntry PT = mbr->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 );
}
}
return mbr;
}

View File

@ -0,0 +1,11 @@
#pragma once
#include <stdint.h>
struct PartitionTableEntry{
uint8_t driveAttribute;
uint8_t CHS_start_address [3];
uint8_t PartitionType;
uint8_t CHS_lastSector_Address[3];
uint32_t LBA_partition_start;
uint32_t Number_sectors_inPartition;
}__attribute__((packed));

View File

@ -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;

View File

@ -1,54 +0,0 @@
#include "VFS.h"
#include <stdint-gcc.h>
#include "../../memory/KernelHeap.h"
#include <CoreLib/Memory.h>
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;
}

130
kernel/storage/vfs/vfs.cpp Normal file
View File

@ -0,0 +1,130 @@
#include "vfs.h"
#include <stdint-gcc.h>
#include "../../memory/KernelHeap.h"
#include "../ata pio/ATAPIO.h"
#include "../partitiontables/mbr/MasterBootRecord.h"
#include "../filesystems/FAT/FAT.h"
#include "StorageTypes.h"
#include <CoreLib/Memory.h>
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;
}

View File

@ -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];
};