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:
@ -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;
|
||||
|
@ -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
130
kernel/storage/vfs/vfs.cpp
Normal 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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -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];
|
||||
};
|
Reference in New Issue
Block a user