Merge into main the new state of the operating system/kernel #1
@ -1,6 +1,6 @@
|
|||||||
AS = ${HOME}/opt/cross/bin/i686-elf-as
|
AS = /opt/cross/bin/i686-elf-as
|
||||||
CC = ${HOME}/opt/cross/bin/i686-elf-gcc
|
CC = /opt/cross/bin/i686-elf-gcc
|
||||||
CPP = ${HOME}/opt/cross/bin/i686-elf-g++
|
CPP = /opt/cross/bin/i686-elf-g++
|
||||||
CFLAGS = -ffreestanding -Og -ggdb -Wall -Wextra -I ../build/CoreLib/include
|
CFLAGS = -ffreestanding -Og -ggdb -Wall -Wextra -I ../build/CoreLib/include
|
||||||
BUILD_DIR = ../build/kernel
|
BUILD_DIR = ../build/kernel
|
||||||
OBJ_DIR = ../bin/kernel
|
OBJ_DIR = ../bin/kernel
|
||||||
|
@ -15,6 +15,8 @@
|
|||||||
#include "interrupts/idt.h"
|
#include "interrupts/idt.h"
|
||||||
#include "serial.h"
|
#include "serial.h"
|
||||||
#include "storage/vfs/vfs.h"
|
#include "storage/vfs/vfs.h"
|
||||||
|
#include "storage/filesystems/FAT/FAT.h"
|
||||||
|
|
||||||
|
|
||||||
extern "C" void LoadGlobalDescriptorTable();
|
extern "C" void LoadGlobalDescriptorTable();
|
||||||
extern "C" void jump_usermode();
|
extern "C" void jump_usermode();
|
||||||
@ -54,20 +56,83 @@ extern "C" void kernel ()
|
|||||||
printf("booted from floppy disk\n");
|
printf("booted from floppy disk\n");
|
||||||
|
|
||||||
printf("Part1: %d, Part2: %d, Part3: %d\n", part1, part2 , part3);
|
printf("Part1: %d, Part2: %d, Part3: %d\n", part1, part2 , part3);
|
||||||
|
ATAPIO::Identify(ATAPIO_PORT::Primary, DEVICE_DRIVE::MASTER);
|
||||||
|
auto* bpb = FAT::getBPB(false);
|
||||||
|
auto* mbr = GetPartitions(false);
|
||||||
|
auto fsType = FAT::determineFATType(bpb);
|
||||||
|
switch (fsType) {
|
||||||
|
case FAT_TYPE::FAT12:
|
||||||
|
printf("FAT12 Disk!\n");
|
||||||
|
break;
|
||||||
|
case FAT_TYPE::FAT16:
|
||||||
|
printf("FAT16 Disk!\n");
|
||||||
|
break;
|
||||||
|
case FAT_TYPE::FAT32:
|
||||||
|
printf("FAT32 Disk!\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
VirtualFileSystem::initialize();
|
// list files in root
|
||||||
|
int total_sectors = bpb->TotSec32;
|
||||||
|
int fat_size = bpb->FATSz16;
|
||||||
|
int root_dir_sectors = FAT::RootDirSize(bpb);
|
||||||
|
int first_data_sector = bpb->RsvdSecCnt + (bpb->NumFATs * fat_size) + root_dir_sectors ;
|
||||||
|
int first_fat_sector = bpb->RsvdSecCnt;
|
||||||
|
int data_sectors = bpb->TotSec32 - (bpb->RsvdSecCnt + (bpb->NumFATs * fat_size) + root_dir_sectors);
|
||||||
|
int total_clusters = data_sectors / bpb->SecPerClus;
|
||||||
|
|
||||||
// Try and open hello.txt file
|
|
||||||
VirtualFileSystem::OpenFile("a:hello.txt");
|
int first_root_dir_sector = first_data_sector - root_dir_sectors;
|
||||||
|
//int first_sector_of_cluster = ((cluster - 2) * bpb->SecPerClus) + first_data_sector;
|
||||||
|
uint16_t data[256];
|
||||||
|
ATAPIO::Read(ATAPIO_PORT::Primary, DEVICE_DRIVE::MASTER, first_root_dir_sector, data);
|
||||||
|
|
||||||
|
auto* RootDirectory = (DIR*)data;
|
||||||
|
for(int i = 0; i < sizeof(data) / sizeof (DIR); i++)
|
||||||
|
{
|
||||||
|
DIR* entry = (DIR*)((uint32_t)RootDirectory + (i * sizeof(DIR)));
|
||||||
|
|
||||||
|
|
||||||
|
if(entry->Name[0] == FAT::FREE_DIR || entry->Name[0] == FAT::FREE_DIR_2 || entry->Name[0] == 0xE5){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(entry->ATTR & FAT::ATTRIBUTES::ATTR_HIDDEN){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(entry->ATTR & FAT::ATTRIBUTES::ATTR_SYSTEM)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if(entry->ATTR & FAT::ATTRIBUTES::ATTR_VOLUME_ID){
|
||||||
|
continue;
|
||||||
|
|
||||||
|
}
|
||||||
|
if (!(entry->ATTR & FAT::ATTRIBUTES::ATTR_LONG_NAME)){
|
||||||
|
for(char n : entry->Name){
|
||||||
|
if(n == 0x20)
|
||||||
|
continue;
|
||||||
|
kterm_put(n);
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
printf("Long file name detected!\n");
|
||||||
|
}
|
||||||
|
printf(" [Size: %d bytes, Attributes: %x\n", entry->ATTR, entry->FileSize);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Try and open grub.cfg file
|
|
||||||
VirtualFileSystem::OpenFile("a:boot/grub/grub.cfg");
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// VirtualFileSystem::initialize();
|
||||||
|
|
||||||
|
// VirtualFileSystem::open("/hello.txt", 0);
|
||||||
|
|
||||||
|
|
||||||
#ifdef USERMODE_RELEASE
|
#ifdef USERMODE_RELEASE
|
||||||
// Lets jump into user mode
|
// Lets jump into user mode
|
||||||
jump_usermode();
|
jump_usermode();
|
||||||
|
@ -1,21 +0,0 @@
|
|||||||
#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));
|
|
@ -1,37 +0,0 @@
|
|||||||
#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;
|
|
@ -1,32 +0,0 @@
|
|||||||
#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));
|
|
@ -4,105 +4,198 @@
|
|||||||
#include "FAT.h"
|
#include "FAT.h"
|
||||||
#include "../../ata pio/ATAPIO.h"
|
#include "../../ata pio/ATAPIO.h"
|
||||||
#include "../../../memory/KernelHeap.h"
|
#include "../../../memory/KernelHeap.h"
|
||||||
|
#include "../../../../CoreLib/Memory.h"
|
||||||
|
#include "../../partitiontables/mbr/MasterBootRecord.h"
|
||||||
#include <CoreLib/Memory.h>
|
#include <CoreLib/Memory.h>
|
||||||
|
superblock* FAT::Mount(filesystem *fs, const char* name ,vfsmount *mnt)
|
||||||
void FAT::Read(PFILE file, unsigned char* buffer , unsigned int length)
|
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
if( strncmp (fs->name, "fat", 3 ) != 0 )
|
||||||
|
|
||||||
FILE FAT::Open(char* filename)
|
|
||||||
{
|
{
|
||||||
|
printf("Can't mount filesystem with none fat type!\n");
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
char* tokstate = NULL;
|
superblock* sb = (superblock*) malloc(sizeof(superblock));
|
||||||
char* nextdir = strtok(filename, "/", &tokstate);
|
directoryEntry* root = (directoryEntry*) malloc(sizeof (directoryEntry));
|
||||||
|
root->name = (char*) name;
|
||||||
|
root->node = nullptr;
|
||||||
|
root->parent = nullptr;
|
||||||
|
|
||||||
while (nextdir)
|
dentry_operations* op = (dentry_operations*) malloc(sizeof(dentry_operations));
|
||||||
|
op->compare = FAT::compare;
|
||||||
|
|
||||||
|
|
||||||
|
root->op = op;
|
||||||
|
|
||||||
|
|
||||||
|
mnt->mnt_count =1;
|
||||||
|
mnt->mnt_devname = "QEMU HDD";
|
||||||
|
mnt->mnt_flags = 0;
|
||||||
|
mnt->mnt_parent = nullptr;
|
||||||
|
|
||||||
|
mnt->root = root;
|
||||||
|
mnt->sb = sb;
|
||||||
|
|
||||||
|
sb->type = fs;
|
||||||
|
sb->root = root;
|
||||||
|
//sb->fs_info = getBPB();
|
||||||
|
|
||||||
|
return sb;
|
||||||
|
}
|
||||||
|
int FAT::Read(file* file, void* buffer , int length)
|
||||||
{
|
{
|
||||||
|
return 0;
|
||||||
// Read the root directory
|
|
||||||
printf("First entry to look for: %s\n", nextdir);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
nextdir = strtok(NULL, "/", &tokstate);
|
|
||||||
}
|
}
|
||||||
|
int FAT::Write(file* file, const void* buffer, int length)
|
||||||
FILE file;
|
|
||||||
file.flags = FS_INVALID;
|
|
||||||
return file;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void FAT::Write(PFILE file, unsigned char* buffer, unsigned int length)
|
|
||||||
{
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int FAT::compare (directoryEntry*, char* filename, char* filename2)
|
||||||
|
{
|
||||||
|
// use the size of the smallest string
|
||||||
|
int a = strlen(filename);
|
||||||
|
int b = strlen(filename2);
|
||||||
|
|
||||||
|
if( a == b ){
|
||||||
|
return strncmp(filename, filename2, a);
|
||||||
|
}
|
||||||
|
return a-b;
|
||||||
|
}
|
||||||
|
int FAT::create(inode* dir_node, inode** target, const char* component_name){}
|
||||||
|
int FAT::lookup (inode*, inode**, const char*){}
|
||||||
|
|
||||||
|
|
||||||
|
FAT_TYPE FAT::determineFATType(BiosParameterBlock* bpb){
|
||||||
|
int RootDirSector = ((bpb->RootEntCnt * 32) + (bpb->BytsPerSec -1)) / bpb->BytsPerSec;
|
||||||
|
int FATSz = 0;
|
||||||
|
if(bpb->FATSz16 != 0){
|
||||||
|
FATSz = bpb->FATSz16;
|
||||||
|
} else{
|
||||||
|
// FATSz = bpb->FATSz32;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
int TotSec = 0;
|
||||||
void ParseDateInteger(unsigned int date){
|
if(bpb->TotSec16 != 0){
|
||||||
printf("Date (hex) 0x%x\n", date);
|
TotSec= bpb->TotSec16;
|
||||||
unsigned int year = (date >> 9 )+ 1980;
|
}else{
|
||||||
unsigned int month = (date & 0xf0 ) >> 4;
|
TotSec = bpb->TotSec32;
|
||||||
unsigned int day = date & 0xf ;
|
|
||||||
printf("Date: (D,M,Y) %d, %d ,%d\n", day , month, year );
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int DataSec = TotSec - (bpb->RsvdSecCnt + (bpb->NumFATs * FATSz) + RootDirSector);
|
||||||
|
int CountofClusters = DataSec / bpb->SecPerClus;
|
||||||
|
|
||||||
BiosParameterBlock* getBPB(PTR_PARTITION partition, bool DEBUG =false ){
|
if(CountofClusters < 4085){
|
||||||
|
return FAT_TYPE::FAT12;
|
||||||
|
} else if (CountofClusters < 65525) {
|
||||||
|
return FAT_TYPE::FAT16;
|
||||||
|
} else{
|
||||||
|
return FAT_TYPE::FAT32;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
BiosParameterBlock* FAT::getBPB( bool DEBUG ){
|
||||||
BiosParameterBlock* bpb = (BiosParameterBlock*) malloc(sizeof(BiosParameterBlock));
|
BiosParameterBlock* bpb = (BiosParameterBlock*) malloc(sizeof(BiosParameterBlock));
|
||||||
|
uint16_t StartAddress = 0x00 ;
|
||||||
ATAPIO_PORT port = (ATAPIO_PORT)(partition->Disk & 0x01FF);
|
ATAPIO::Read(ATAPIO_PORT::Primary, DEVICE_DRIVE::MASTER, StartAddress, (uint16_t*) bpb);
|
||||||
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)
|
if(DEBUG)
|
||||||
{
|
{
|
||||||
printf("OEM ID: %s\n", bpb->OEM_id);
|
printf("OEM ID: %s\n", bpb->OEMName);
|
||||||
printf("Bytes per sector: %d\n", bpb->BytesPerSector);
|
printf("Bytes per sector: %d\n", bpb->BytsPerSec);
|
||||||
printf("Sectors per cluster: %d\n", bpb->SectorsPerCluster);
|
printf("Sectors per cluster: %d\n", bpb->SecPerClus);
|
||||||
printf("Reserved sectors: %d\n", bpb->ReservedSectors);
|
printf("Reserved sectors: %d\n", bpb->RsvdSecCnt);
|
||||||
printf("Number of FAT: %d\n", bpb->NumberOfFileAllocationTables);
|
printf("Number of FAT: %d\n", bpb->NumFATs);
|
||||||
printf("Number of Dir entries: %d\n", bpb->NumberOfDirectoryEntries);
|
printf("Number of Dir entries: %d\n", bpb->RootEntCnt);
|
||||||
printf("Total Sectors in volume: %d\n", bpb->TotalSectorsInLogicalVolume);
|
printf("Total Sectors in volume: %d\n", bpb->TotSec16 == 0 ? bpb->TotSec32 : bpb->TotSec16);
|
||||||
printf("Sectors per FAT: %d\n", bpb->NumberOfSectorsPerFAT);
|
printf("Sectors per FAT: %d\n", bpb->FATSz16 );
|
||||||
}
|
}
|
||||||
|
|
||||||
return bpb;
|
return bpb;
|
||||||
}
|
}
|
||||||
|
uint16_t FAT::GetFATEntry (BiosParameterBlock* bpb, unsigned int cluster){
|
||||||
|
int FATSz =0;
|
||||||
|
if(bpb->FATSz16 != 0){
|
||||||
|
FATSz = bpb->FATSz16;
|
||||||
|
} else{
|
||||||
|
//FATSz = bpb->FATSz32;
|
||||||
|
}
|
||||||
|
|
||||||
bool FAT::Validate(PTR_PARTITION partition )
|
int FATOffset = 0;
|
||||||
|
FAT_TYPE type = FAT::determineFATType(bpb);
|
||||||
|
if( type == FAT_TYPE::FAT16){
|
||||||
|
FATOffset = cluster *2;
|
||||||
|
} else if( type == FAT_TYPE::FAT32){
|
||||||
|
FATOffset = cluster * 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
int thisFATSecNum = bpb->RsvdSecCnt + (FATOffset / bpb->BytsPerSec); // Sector number containing the entry for the cluster
|
||||||
|
|
||||||
|
// For any other FAT other then the default
|
||||||
|
// SectorNumber = (FATNumber * FATSz) + ThisFATSecNum
|
||||||
|
|
||||||
|
uint16_t buff[bpb->BytsPerSec];
|
||||||
|
|
||||||
|
ATAPIO::Read(ATAPIO_PORT::Primary, DEVICE_DRIVE::MASTER, thisFATSecNum, buff );
|
||||||
|
|
||||||
|
int thisFATEntOffset = FATOffset % bpb->BytsPerSec; // offset for the entry in the sector containing the entry for the cluster
|
||||||
|
uint16_t ClusterEntryValue = 0;
|
||||||
|
// Get the FATEntry
|
||||||
|
if(type == FAT_TYPE::FAT16){
|
||||||
|
return *((uint16_t*) &buff[thisFATEntOffset]);
|
||||||
|
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
// FAT32 logic
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
uint16_t FAT::DetermineFreeSpace()
|
||||||
{
|
{
|
||||||
|
// Loop through all FAT entries in all FAT's
|
||||||
|
// to construct a list of free/available clusters
|
||||||
|
// Free clusters are recorded with all 0's except on FAT32 where
|
||||||
|
// the highest order 4 bits should be ignored.
|
||||||
|
|
||||||
auto* bootParams = getBPB(partition, true);
|
/*
|
||||||
|
* The number of sectors reserved for each FAT (count of sectors in the BPB_FATSz16 or
|
||||||
if(bootParams->OEM_id) {
|
BPB_FATSz32 fields) may be bigger than the actual number of sectors required for
|
||||||
return true;
|
containing the entire FAT. Therefore, there may be totally unused FAT sectors at the end of
|
||||||
}
|
each FAT in the FAT region of the volume. Each implementation must determine the value
|
||||||
|
for the last valid sector in the FAT using CountOfClusters (the last valid sector in the FAT
|
||||||
return false;
|
is the one containing the FAT entry numbered CountOfClusters + 1).
|
||||||
|
All sectors reserved for the FAT beyond the last valid sector (defined as the one containing
|
||||||
}
|
the FAT entry for the last cluster) must be set to 0x0 during volume initialization/format.
|
||||||
|
*/
|
||||||
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 ) {
|
int FAT::GetSectorOfRootDirectory (BiosParameterBlock* bpb)
|
||||||
uint32_t FATAddress = partition->StartAddress + bpb.ReservedSectors ;
|
{
|
||||||
|
return (bpb->RsvdSecCnt + (bpb->NumFATs * bpb->FATSz16));
|
||||||
|
}
|
||||||
|
|
||||||
|
int FAT::RootDirSize(BiosParameterBlock* bpb)
|
||||||
|
{
|
||||||
|
int size = bpb->RootEntCnt * 32;
|
||||||
|
if((size % bpb->BytsPerSec) != 0){
|
||||||
|
printf("ERR: Root entry count invalid!\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
uint16_t* ReadFAT (BiosParameterBlock& bpb , bool DEBUG = false ) {
|
||||||
|
uint32_t FATAddress = /*StartAddress*/ 0x00 + bpb.RsvdSecCnt ;
|
||||||
uint16_t* FAT = (uint16_t*)malloc(sizeof (uint16_t) * 256);
|
uint16_t* FAT = (uint16_t*)malloc(sizeof (uint16_t) * 256);
|
||||||
|
|
||||||
ATAPIO_PORT port = (ATAPIO_PORT)(partition->Disk & 0x01FF);
|
ATAPIO::Read(ATAPIO_PORT::Primary, DEVICE_DRIVE::MASTER, FATAddress, FAT );
|
||||||
DEVICE_DRIVE drive = (DEVICE_DRIVE)(partition->Disk >> 16);
|
|
||||||
ATAPIO::Read(port, drive, FATAddress, FAT );
|
|
||||||
|
|
||||||
// Show data in terminal
|
// Show data in terminal
|
||||||
if(DEBUG){
|
if(DEBUG){
|
||||||
@ -115,14 +208,15 @@ uint16_t* ReadFAT (BiosParameterBlock& bpb , PTR_PARTITION partition, bool DEBUG
|
|||||||
return FAT;
|
return FAT;
|
||||||
}
|
}
|
||||||
|
|
||||||
void readFile(uint32_t DataRegion, DirectoryEntry* entry, uint16_t FATentry, BiosParameterBlock& bpb ){
|
|
||||||
|
void readFile(uint32_t DataRegion, DIR* entry, uint16_t FATentry, BiosParameterBlock& bpb ){
|
||||||
printf("Show contents");
|
printf("Show contents");
|
||||||
|
|
||||||
printf("Start cluster of the file: 0x%x\n", entry->StartingCluster);
|
printf("Start cluster of the file: 0x%x\n", entry->FileSize);
|
||||||
|
|
||||||
printf("IS it only 1 cluster? %s\n", FATentry == 0xFFFF ? "Yes" : "No");
|
printf("IS it only 1 cluster? %s\n", FATentry == 0xFFFF ? "Yes" : "No");
|
||||||
|
|
||||||
uint32_t sector = DataRegion + ((entry->StartingCluster - 0x02) * bpb.SectorsPerCluster);
|
uint32_t sector = DataRegion + ((entry->FileSize - 0x02) * bpb.SecPerClus);
|
||||||
|
|
||||||
|
|
||||||
uint16_t dataBlob[256];
|
uint16_t dataBlob[256];
|
||||||
@ -134,66 +228,21 @@ void readFile(uint32_t DataRegion, DirectoryEntry* entry, uint16_t FATentry, Bio
|
|||||||
}
|
}
|
||||||
kterm_put('\n');
|
kterm_put('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
void listFilesInRoot(MBR& mbr, BiosParameterBlock& bpb ){
|
file fsysFatDirectory (const char* DirectoryName){
|
||||||
auto FATAddress = mbr.TableEntries[0].LBA_partition_start + bpb.ReservedSectors;
|
file file;
|
||||||
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;
|
unsigned char* buf;
|
||||||
PDIRECTORY directory;
|
PDIRECTORY directory;
|
||||||
|
|
||||||
char DosFileName[11];
|
char DosFileName[11];
|
||||||
ToDosFileName(DirectoryName, DosFileName, 11);
|
//ToDosFileName(DirectoryName, DosFileName, 11);
|
||||||
DosFileName[11] =0;
|
DosFileName[11] =0;
|
||||||
|
|
||||||
for (int sector=0; sector <14 ; sector++){
|
for (int sector=0; sector <14 ; sector++){
|
||||||
ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, mountInfo.rootOffset + sector, (uint16_t*)buf);
|
ATAPIO::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, mountInfo.rootOffset + sector, (uint16_t*)buf);
|
||||||
directory = (PDIRECTORY) buf;
|
directory = (PDIRECTORY) buf;
|
||||||
|
|
||||||
for (int i =0; i < 16; i++){
|
for (int i =0; i < 16; i++){
|
||||||
@ -209,9 +258,9 @@ FILE fsysFatDirectory (const char* DirectoryName){
|
|||||||
file.filelength = directory->FileSize;
|
file.filelength = directory->FileSize;
|
||||||
|
|
||||||
if(directory->Attrib == 0x10){
|
if(directory->Attrib == 0x10){
|
||||||
file.flags = FS_DIRECTORY;
|
file.flags = 2;
|
||||||
} else {
|
} else {
|
||||||
file.flags = FS_FILE;
|
file.flags = 1;
|
||||||
}
|
}
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
@ -220,7 +269,7 @@ FILE fsysFatDirectory (const char* DirectoryName){
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Can't find file
|
// Can't find file
|
||||||
file.flags = FS_INVALID;
|
file.flags = -1;
|
||||||
return file;
|
return file;
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -300,9 +349,9 @@ FILE fsysFatOpenSubDir(FILE kFile, const char* filename){
|
|||||||
|
|
||||||
// set file type;
|
// set file type;
|
||||||
if(pkDir->Attrib == 0x10){
|
if(pkDir->Attrib == 0x10){
|
||||||
file.flags = FS_DIRECTORY;
|
file.flags = 2;
|
||||||
} else{
|
} else{
|
||||||
file.flags = FS_FILE;
|
file.flags = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return file;
|
return file;
|
||||||
@ -312,7 +361,7 @@ FILE fsysFatOpenSubDir(FILE kFile, const char* filename){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// unable to find file
|
// unable to find file
|
||||||
file.flags = FS_INVALID;
|
file.flags = -1;
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
@ -2,48 +2,152 @@
|
|||||||
// Created by nigel on 21/02/23.
|
// Created by nigel on 21/02/23.
|
||||||
//
|
//
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "ExtendBootRecord.h"
|
#include "../../vfs/vfs_types.h"
|
||||||
#include "BiosParameterBlock.h"
|
#include "../../vfs/vfs_types.h"
|
||||||
#include "DirectoryEntry.h"
|
#include "../../partitiontables/mbr/MasterBootRecord.h"
|
||||||
#include "../../vfs/StorageTypes.h"
|
|
||||||
|
|
||||||
// Date Format
|
struct ExtendedBootRecord_FAT16{
|
||||||
// [0..4] Day
|
uint8_t DrvNum;
|
||||||
// [5..8] Month
|
uint8_t Reserved1;
|
||||||
// [9..15] Year
|
uint8_t BootSig;
|
||||||
|
const uint32_t VolID;
|
||||||
|
uint8_t VolLab [11];
|
||||||
|
uint8_t FilSysType [8];
|
||||||
|
uint8_t bootCode [448];
|
||||||
|
uint16_t Signature_word;
|
||||||
|
uint8_t SecRmndr[512];
|
||||||
|
}__attribute__((packed));
|
||||||
|
|
||||||
// Time Format
|
struct ExtendedBootRecord_FAT32{
|
||||||
// [0..4] Seconds
|
uint32_t SectorsPerFAT;
|
||||||
// [5..10] Minute
|
uint16_t Flags;
|
||||||
// [11..15] Hour
|
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));
|
||||||
|
|
||||||
|
struct BiosParameterBlock {
|
||||||
|
uint8_t jmpBoot[3];
|
||||||
|
uint8_t OEMName [8];
|
||||||
|
uint16_t BytsPerSec ; // I suspect would be 512
|
||||||
|
uint8_t SecPerClus ;
|
||||||
|
uint16_t RsvdSecCnt;
|
||||||
|
uint8_t NumFATs; // Probably equals 2
|
||||||
|
uint16_t RootEntCnt; // Root directory must contain entire sectors
|
||||||
|
uint16_t TotSec16 ; // 0 means >65535 sectors in volume , actual count can be found in LargeSectorCount
|
||||||
|
uint8_t Media ; // Indication the media descriptor type
|
||||||
|
uint16_t FATSz16;// only in FAT12 / FAT 16
|
||||||
|
uint16_t SecPerTrk;
|
||||||
|
uint16_t NumHeads;
|
||||||
|
uint32_t HiddSec;
|
||||||
|
uint32_t TotSec32;
|
||||||
|
ExtendedBootRecord_FAT16 ebpb;
|
||||||
|
}__attribute__((packed));
|
||||||
|
|
||||||
|
struct DIR {
|
||||||
|
uint8_t Name [11];
|
||||||
|
uint8_t ATTR ;
|
||||||
|
uint8_t NTRes;
|
||||||
|
uint8_t CrtTimeTenth; // File Creation time component - count of tenths of a second (between 0 and 199)
|
||||||
|
uint16_t CrtTime; // Creation time. Granularity is 2 seconds
|
||||||
|
uint16_t CrtDate; // Creation date.
|
||||||
|
uint16_t LstAccDate; // Last Access Date (Last read or write date)
|
||||||
|
uint16_t FstClusHi; // High Word of first data cluster for file/directory described
|
||||||
|
uint16_t WrtTime; // Last Modification time | Must equal CrtTime
|
||||||
|
uint16_t WrtDate; // Last Modification date | Must equal CrtDate
|
||||||
|
uint16_t FstClusLO; // Low word of first data cluster for file/directory described
|
||||||
|
uint32_t FileSize; // size in bytes of file/directory described
|
||||||
|
}__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;
|
||||||
|
|
||||||
|
enum struct FAT_TYPE{
|
||||||
|
FAT12,
|
||||||
|
FAT16,
|
||||||
|
FAT32,
|
||||||
|
VFAT,
|
||||||
|
UNKOWN
|
||||||
|
};
|
||||||
|
|
||||||
class FAT {
|
class FAT {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
|
||||||
|
// Wanted API for vfs
|
||||||
|
static file Open(char* filename);
|
||||||
|
static int close(file* file);
|
||||||
|
static int Read(file* file, void* buffer , int length);
|
||||||
|
static int Write(file* file, const void* buffer, int length);
|
||||||
|
static int create(inode* dir_node, inode** target, const char* component_name);
|
||||||
|
static int lookup(inode* , inode**, const char*);
|
||||||
|
static int compare(directoryEntry* , char* , char*);
|
||||||
|
static superblock* Mount(filesystem* fs, const char* name ,vfsmount* mount);
|
||||||
|
|
||||||
static bool Validate(PTR_PARTITION partition );
|
// TEMP
|
||||||
|
static void listFilesInRoot(MBR* mbr, BiosParameterBlock* bpb );
|
||||||
|
static BiosParameterBlock* getBPB( bool DEBUG =false );
|
||||||
|
static FAT_TYPE determineFATType(BiosParameterBlock* bpb);
|
||||||
|
static uint16_t GetFATEntry(BiosParameterBlock*, unsigned int);
|
||||||
|
static uint16_t DetermineFreeSpace();
|
||||||
|
static int GetSectorOfRootDirectory(BiosParameterBlock*);
|
||||||
|
static int RootDirSize(BiosParameterBlock*);
|
||||||
|
|
||||||
static FILE Open(char* filename);
|
static const int FREE = 0x0000;
|
||||||
static void Read(PFILE file, unsigned char* buffer , unsigned int length);
|
static const int ALLOCATED = 0x0002;
|
||||||
static void Write(PFILE file, unsigned char* buffer, unsigned int length);
|
static const int BAD = 0xFFF7;
|
||||||
|
static const int EOF = 0xFFFF;
|
||||||
|
|
||||||
static void Info(_PARTITION *pPartition, PFS pSystem);
|
static const int ClnShutBitMask = 0x8000;
|
||||||
|
static const int HrdErrBitMask = 0x4000;
|
||||||
|
|
||||||
|
static const char DOS_TRAILING_SPACE = 0x20;
|
||||||
|
static const char FREE_DIR = 0xE5; // If KANJI charset 0x05
|
||||||
|
static const char FREE_DIR_2 = 0x00; // All directories after this are free including this one
|
||||||
|
|
||||||
private:
|
|
||||||
enum struct TYPE{
|
|
||||||
FAT,
|
|
||||||
FAT16,
|
|
||||||
FAT32,
|
|
||||||
VFAT
|
|
||||||
};
|
|
||||||
enum ATTRIBUTES {
|
enum ATTRIBUTES {
|
||||||
ATTR_READ_ONLY = 0x01,
|
ATTR_READ_ONLY = 0x01,
|
||||||
ATT_HIDDEN = 0x02,
|
ATTR_HIDDEN = 0x02,
|
||||||
ATTR_SYSTEM = 0x04,
|
ATTR_SYSTEM = 0x04,
|
||||||
ATTR_VOLUME_ID = 0x08,
|
ATTR_VOLUME_ID = 0x08,
|
||||||
ATTR_DIRECTORY = 0x10,
|
ATTR_DIRECTORY = 0x10,
|
||||||
ATTR_ARCHIVE = 0x20
|
ATTR_ARCHIVE = 0x20,
|
||||||
|
ATTR_LONG_NAME = (ATTR_READ_ONLY | ATTR_HIDDEN | ATTR_SYSTEM | ATTR_VOLUME_ID)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
|
||||||
|
enum ENTRY_SIZE {
|
||||||
|
FAT12 = 12,
|
||||||
|
FAT16 = 16,
|
||||||
|
FAT32 = 32
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
40
kernel/storage/filesystems/FAT/msdosDate.h
Normal file
40
kernel/storage/filesystems/FAT/msdosDate.h
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
//
|
||||||
|
// Created by nigel on 23/02/23.
|
||||||
|
//
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "../../../terminal/kterm.h"
|
||||||
|
|
||||||
|
// Date Format
|
||||||
|
// [0..4] Day
|
||||||
|
// [5..8] Month
|
||||||
|
// [9..15] Year
|
||||||
|
class MSDOSDATE {
|
||||||
|
static void ParseDate(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 );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Time Format
|
||||||
|
// [0..4] Seconds
|
||||||
|
// [5..10] Minute
|
||||||
|
// [11..15] Hour
|
||||||
|
class MSDOSTIME {
|
||||||
|
static void ParseTime(unsigned int time)
|
||||||
|
{
|
||||||
|
printf("Time (hex) 0x%x\n", time);
|
||||||
|
unsigned int seconds = ( time & 0x0f) * 2;
|
||||||
|
unsigned int minutes = (time & 0xf0);
|
||||||
|
unsigned int hours = (time & 0xf00);
|
||||||
|
printf("Time (H:M:S) %d:%d:%d\n", hours, minutes, seconds);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
18
kernel/storage/partitions/partitionManager.cpp
Normal file
18
kernel/storage/partitions/partitionManager.cpp
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
//
|
||||||
|
// Created by nigel on 23/02/23.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "partitionManager.h"
|
||||||
|
|
||||||
|
bool partitionManager::Validate( )
|
||||||
|
{
|
||||||
|
|
||||||
|
//auto* bootParams = getBPB(this, true);
|
||||||
|
|
||||||
|
//if(bootParams->OEM_id) {
|
||||||
|
// return true;
|
||||||
|
//}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
|
||||||
|
}
|
11
kernel/storage/partitions/partitionManager.h
Normal file
11
kernel/storage/partitions/partitionManager.h
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
//
|
||||||
|
// Created by nigel on 23/02/23.
|
||||||
|
//
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
class partitionManager {
|
||||||
|
public:
|
||||||
|
static bool Validate();
|
||||||
|
|
||||||
|
};
|
||||||
|
|
@ -1,41 +0,0 @@
|
|||||||
//
|
|
||||||
// Created by nigel on 21/02/23.
|
|
||||||
//
|
|
||||||
#include "FileSystem.h"
|
|
||||||
|
|
||||||
void FileSystem::WriteFile(int file, unsigned char* buffer, unsigned int length) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void FileSystem::ReadFile(int file, unsigned char* buffer, unsigned int length) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
FILE FileSystem::OpenFile(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;
|
|
||||||
}
|
|
||||||
|
|
||||||
void FileSystem::CloseFile(PFILE file) {
|
|
||||||
if(file->device < DEVICE_MAX){
|
|
||||||
// _filesystems[file->device]->Close(file);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
@ -1,18 +0,0 @@
|
|||||||
//
|
|
||||||
// Created by nigel on 21/02/23.
|
|
||||||
//
|
|
||||||
#pragma once
|
|
||||||
#include "StorageTypes.h"
|
|
||||||
|
|
||||||
class FileSystem {
|
|
||||||
public:
|
|
||||||
static void WriteFile(PFILE file, unsigned char* beffer, unsigned int length);
|
|
||||||
static void ReadFile(PFILE file, unsigned char* buffer, unsigned int length);
|
|
||||||
static FILE OpenFile(const char* fname);
|
|
||||||
static void CloseFile(PFILE file);
|
|
||||||
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,5 +0,0 @@
|
|||||||
//
|
|
||||||
// Created by nigel on 21/02/23.
|
|
||||||
//
|
|
||||||
|
|
||||||
#include "Inode.h"
|
|
@ -1,24 +0,0 @@
|
|||||||
//
|
|
||||||
// Created by nigel on 21/02/23.
|
|
||||||
//
|
|
||||||
#pragma once
|
|
||||||
enum struct NODE_TYPE {
|
|
||||||
FILESYSTEM,
|
|
||||||
FILE,
|
|
||||||
DIRECTORY
|
|
||||||
};
|
|
||||||
|
|
||||||
enum struct PERMISSIONS {
|
|
||||||
READ,
|
|
||||||
WRITE,
|
|
||||||
EXECUTE
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Inode {
|
|
||||||
NODE_TYPE type;
|
|
||||||
PERMISSIONS permissions;
|
|
||||||
Inode* Parent;
|
|
||||||
Inode* sibling;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
@ -1,40 +0,0 @@
|
|||||||
//
|
|
||||||
// Created by nigel on 21/02/23.
|
|
||||||
//
|
|
||||||
#pragma once
|
|
||||||
#include <stdint-gcc.h>
|
|
||||||
|
|
||||||
enum FS_TYPES {
|
|
||||||
FS_FILE =0,
|
|
||||||
FS_DIRECTORY =1,
|
|
||||||
FS_INVALID=2
|
|
||||||
};
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
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 (*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;
|
|
@ -4,16 +4,122 @@
|
|||||||
#include "../ata pio/ATAPIO.h"
|
#include "../ata pio/ATAPIO.h"
|
||||||
#include "../partitiontables/mbr/MasterBootRecord.h"
|
#include "../partitiontables/mbr/MasterBootRecord.h"
|
||||||
#include "../filesystems/FAT/FAT.h"
|
#include "../filesystems/FAT/FAT.h"
|
||||||
#include "StorageTypes.h"
|
#include "vfs_types.h"
|
||||||
|
#include "../../../CoreLib/Memory.h"
|
||||||
#include <CoreLib/Memory.h>
|
#include <CoreLib/Memory.h>
|
||||||
|
|
||||||
PFS VirtualFileSystem::_filesystems[VirtualFileSystem::DEVICE_MAX];
|
vfsmount* VirtualFileSystem::rootfs;
|
||||||
PTR_PARTITION VirtualFileSystem::_partitions [VirtualFileSystem::PARTITION_MAX];
|
int VirtualFileSystem::mount_number = 0;
|
||||||
unsigned int VirtualFileSystem::num_partitions = 0;
|
int VirtualFileSystem::superblock_number =0;
|
||||||
|
int VirtualFileSystem::filesystem_number =0;
|
||||||
|
|
||||||
|
filesystem* VirtualFileSystem::filesystems[4];
|
||||||
|
superblock* VirtualFileSystem::superblocks[8];
|
||||||
|
vfsmount* VirtualFileSystem::mounts[12];
|
||||||
|
|
||||||
|
void VirtualFileSystem::Mount(filesystem* fs, const char* name)
|
||||||
|
{
|
||||||
|
vfsmount* mnt_point = (vfsmount*) malloc(sizeof(vfsmount));
|
||||||
|
superblock* sb = fs->mount(fs, name, mnt_point);
|
||||||
|
|
||||||
|
mounts[mount_number++] = mnt_point;
|
||||||
|
superblocks[superblock_number++] = sb;
|
||||||
|
|
||||||
|
rootfs = mnt_point;
|
||||||
|
}
|
||||||
|
|
||||||
void VirtualFileSystem::initialize()
|
void VirtualFileSystem::initialize()
|
||||||
{
|
{
|
||||||
|
// TODO: setup memory pools etc to speed things up
|
||||||
|
// a bit
|
||||||
|
// TODO: Add a devfs, procfs etc...
|
||||||
|
|
||||||
|
filesystem* fat_fs = (filesystem*) malloc(sizeof (filesystem));
|
||||||
|
fat_fs->name = "fat";
|
||||||
|
fat_fs->mount = FAT::Mount;
|
||||||
|
//register_filesystem(fat_fs);
|
||||||
|
|
||||||
|
|
||||||
|
// Mount the bootdrive
|
||||||
|
// NOTE: for now will hardcode this
|
||||||
|
Mount(fat_fs, "/");
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
int VirtualFileSystem::register_filesystem(struct filesystem* fs) {
|
||||||
|
// register the filesystem to the kernel.
|
||||||
|
filesystems[filesystem_number] = fs;
|
||||||
|
filesystem_number++;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
struct file* VirtualFileSystem::open(const char* pathname, int flags){
|
||||||
|
// 1. Lookup pathname from the root node
|
||||||
|
// 2. Create a new file descriptor for this v_node if found.
|
||||||
|
// 3. Create a new file if O_CREATE is specified in the flags.
|
||||||
|
|
||||||
|
// See reference material (1) https://man7.org/linux/man-pages/man7/path_resolution.7.html
|
||||||
|
|
||||||
|
// FILE file = ->Open(filename);
|
||||||
|
if(pathname[0] != '/'){
|
||||||
|
printf("We won't handle relative paths yet!");
|
||||||
|
file file;
|
||||||
|
file.flags = 1;
|
||||||
|
return &file;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
auto* dentry = rootfs->root;
|
||||||
|
|
||||||
|
int result = dentry->op->compare(dentry, "/", dentry->name);
|
||||||
|
if(result != 0 ){
|
||||||
|
printf("rootfs not called / \n");
|
||||||
|
file file;
|
||||||
|
file.flags = 1;
|
||||||
|
return &file;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* tokstate = nullptr;
|
||||||
|
auto nextdir = strtok ((char*)pathname, "/", &tokstate );
|
||||||
|
while (nextdir)
|
||||||
|
{
|
||||||
|
printf("Look for dentry: %s\n", nextdir);
|
||||||
|
// look to its children
|
||||||
|
if (dentry->children ) {
|
||||||
|
printf("No children | children unknown!\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (dentry->op->compare(dentry, nextdir, dentry->name))
|
||||||
|
{
|
||||||
|
// file found
|
||||||
|
nextdir = strtok(nullptr, "/", &tokstate);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
file file;
|
||||||
|
file.flags = 1;
|
||||||
|
return &file;
|
||||||
|
}
|
||||||
|
|
||||||
|
int VirtualFileSystem::close (struct file* file){
|
||||||
|
// 1. release the file descriptor
|
||||||
|
}
|
||||||
|
int VirtualFileSystem::write(struct file* file, const void* buf, size_t len){
|
||||||
|
// 1. Write len bytes from buf to the opened file.
|
||||||
|
// 2. return written size or error code if an error occurs
|
||||||
|
}
|
||||||
|
int VirtualFileSystem::read (struct file* file, void* buf, size_t len){
|
||||||
|
// 1. read min(len, readable file data size) bytes ro buf from the opened file.
|
||||||
|
// 2. return read size or error code if an error occurs
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
void fs_discovery(){
|
||||||
|
|
||||||
|
|
||||||
// Mount the boot disk
|
// Mount the boot disk
|
||||||
// NOTE: we assume for now that it is the only disk in the system
|
// NOTE: we assume for now that it is the only disk in the system
|
||||||
@ -41,7 +147,7 @@ void VirtualFileSystem::initialize()
|
|||||||
found_partition->Sectors = partition.Number_sectors_inPartition;
|
found_partition->Sectors = partition.Number_sectors_inPartition;
|
||||||
found_partition->Fs_hint = partition.PartitionType;
|
found_partition->Fs_hint = partition.PartitionType;
|
||||||
|
|
||||||
VirtualFileSystem::RegisterPartition(found_partition);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Found %d partitions on disk!\n", num_partitions);
|
printf("Found %d partitions on disk!\n", num_partitions);
|
||||||
@ -70,61 +176,11 @@ void VirtualFileSystem::initialize()
|
|||||||
FAT::Info(partition, FS_FAT);
|
FAT::Info(partition, FS_FAT);
|
||||||
|
|
||||||
// Mount the partition/filesystem
|
// 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,20 +1,32 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <stdint-gcc.h>
|
#include <stdint-gcc.h>
|
||||||
#include "../../../CoreLib/Path.h"
|
#include "../../../CoreLib/Path.h"
|
||||||
#include "StorageTypes.h"
|
#include "vfs_types.h"
|
||||||
|
#include "vfs_types.h"
|
||||||
class VirtualFileSystem
|
class VirtualFileSystem
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static void initialize();
|
static void initialize();
|
||||||
static void Mount(PFS fs, unsigned int DeviceID);
|
static void Mount(filesystem* fs, const char* name);
|
||||||
static void Unmount(unsigned int DeviceID);
|
|
||||||
static FILE OpenFile(const char* path);
|
static int register_filesystem(struct filesystem* fs);
|
||||||
static void RegisterPartition(PTR_PARTITION partition);
|
|
||||||
|
static struct file* open(const char* pathname, int flags);
|
||||||
|
static int close(struct file* file);
|
||||||
|
static int write(struct file* file, const void* buf, size_t len);
|
||||||
|
static int read(struct file* file, void* buf, size_t len);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static const unsigned int DEVICE_MAX = 26;
|
static vfsmount* rootfs;
|
||||||
static const unsigned int PARTITION_MAX = 4 * DEVICE_MAX;
|
|
||||||
static PFS _filesystems[DEVICE_MAX];
|
|
||||||
static unsigned int num_partitions;
|
static int mount_number;
|
||||||
static PTR_PARTITION _partitions [PARTITION_MAX];
|
static int superblock_number;
|
||||||
|
static int filesystem_number;
|
||||||
|
|
||||||
|
static filesystem* filesystems[];
|
||||||
|
static superblock* superblocks[];
|
||||||
|
static vfsmount* mounts[];
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
116
kernel/storage/vfs/vfs_types.h
Normal file
116
kernel/storage/vfs/vfs_types.h
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
//
|
||||||
|
// Created by nigel on 21/02/23.
|
||||||
|
//
|
||||||
|
#pragma once
|
||||||
|
// grasslab.github.io/osdi/en/labs/lab7.html
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
struct inode_operations;
|
||||||
|
struct vfsmount;
|
||||||
|
struct superblock;
|
||||||
|
struct inode;
|
||||||
|
struct dentry_operations;
|
||||||
|
struct directoryEntry;
|
||||||
|
struct filesystem;
|
||||||
|
struct superblock;
|
||||||
|
struct file;
|
||||||
|
|
||||||
|
struct file_operations{
|
||||||
|
int(*write) (file* file, const void* buf, size_t len);
|
||||||
|
int(*read) (file* file, void* buf, size_t len);
|
||||||
|
};
|
||||||
|
struct inode_operations {
|
||||||
|
int (*create)(inode*, directoryEntry*, const char* );
|
||||||
|
directoryEntry* (*lookup)(inode* , directoryEntry*);
|
||||||
|
int (*link)(directoryEntry*, inode* , directoryEntry*);
|
||||||
|
int (*unlink)(inode*, directoryEntry*);
|
||||||
|
int (*symlink)(inode*, directoryEntry*);
|
||||||
|
int (*mkdir)(inode*, directoryEntry*, const char*);
|
||||||
|
int (*rmdir)(inode*, directoryEntry*);
|
||||||
|
int (*rename)(inode*, directoryEntry*, inode*, directoryEntry*);
|
||||||
|
void (*truncate)(inode*);
|
||||||
|
int (*permission)(inode*, int);
|
||||||
|
int (*setattr)(directoryEntry, unsigned long*);
|
||||||
|
int (*getattr)(vfsmount* mnt, directoryEntry*, unsigned long*);
|
||||||
|
};
|
||||||
|
// Describes a mount
|
||||||
|
struct vfsmount {
|
||||||
|
vfsmount* mnt_parent; // fs we are mounted on
|
||||||
|
directoryEntry* mountpoint; // dentry of mount point
|
||||||
|
directoryEntry* root; // root of the mounted tree
|
||||||
|
superblock* sb; // pointer to the superblock
|
||||||
|
unsigned int mnt_count; // keep track of users of this structure
|
||||||
|
int mnt_flags;
|
||||||
|
char* mnt_devname; // name of device eg /dev/dsk/hda1
|
||||||
|
|
||||||
|
};
|
||||||
|
struct superblock;
|
||||||
|
// Represents a filesystem object (i.e. a file or directory or device)
|
||||||
|
struct inode {
|
||||||
|
unsigned long mode; // access permissions
|
||||||
|
unsigned long uid; // user id owner
|
||||||
|
unsigned long gid; // group id owner
|
||||||
|
unsigned int flags;
|
||||||
|
inode_operations* i_op; // operations possible on inode
|
||||||
|
superblock* sb;
|
||||||
|
unsigned long ino; // unique number of this inode
|
||||||
|
unsigned int links; // number of hard links
|
||||||
|
void* device;
|
||||||
|
unsigned long size; // size of inode contents in bytes
|
||||||
|
unsigned long atime; // Access time
|
||||||
|
unsigned long mtime; // Modify time
|
||||||
|
unsigned long ctime; // Creation time
|
||||||
|
unsigned short bytes; // bytes consumed
|
||||||
|
file_operations* fop;
|
||||||
|
void* internal; // point to underlying representation of this virtual node (e.g. FAT entry or Directory Entry)
|
||||||
|
};
|
||||||
|
// Represents the possible operations on a directory entry
|
||||||
|
struct dentry_operations
|
||||||
|
{
|
||||||
|
int (*compare)(directoryEntry*, char*, char* );
|
||||||
|
int (*o_delete)(directoryEntry*);
|
||||||
|
void (*release)(directoryEntry*);
|
||||||
|
void (*iput)(directoryEntry*, inode* );
|
||||||
|
|
||||||
|
};
|
||||||
|
// Represents the name of files in the filesystem
|
||||||
|
// it identifies the file that an inode represents
|
||||||
|
struct directoryEntry {
|
||||||
|
char* name; // name of the file on the disk
|
||||||
|
directoryEntry* parent; // parent of the file
|
||||||
|
inode* node; // node belongs to...
|
||||||
|
dentry_operations* op;
|
||||||
|
directoryEntry* children[];
|
||||||
|
|
||||||
|
};
|
||||||
|
// Represents a filesystem type
|
||||||
|
struct filesystem {
|
||||||
|
const char* name;
|
||||||
|
superblock* (*mount)(filesystem* self, const char* name, vfsmount* mnt);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Represents a mounted filesystem
|
||||||
|
struct superblock {
|
||||||
|
|
||||||
|
void* device; // device associated with the filesystem
|
||||||
|
unsigned long blocksize;
|
||||||
|
unsigned long maxbytes;
|
||||||
|
filesystem* type;
|
||||||
|
unsigned long magic; // IDK
|
||||||
|
directoryEntry* root; // Root dentry
|
||||||
|
int count; // IDK
|
||||||
|
void* fs_info; // pointer to raw filesystem info
|
||||||
|
dentry_operations* d_op;
|
||||||
|
inode* inodes[];
|
||||||
|
};
|
||||||
|
|
||||||
|
// Represents an opened file
|
||||||
|
struct file {
|
||||||
|
inode* root;
|
||||||
|
size_t f_pos; // The next read/write position of this file descriptor;
|
||||||
|
file_operations* f_ops;
|
||||||
|
int flags;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
@ -1,8 +1,7 @@
|
|||||||
#include "superVisorTerminal.h"
|
#include "superVisorTerminal.h"
|
||||||
#include "../storage/ata pio/ATAPIO.h"
|
#include "../storage/ata pio/ATAPIO.h"
|
||||||
#include "../storage/partitiontables/mbr/MasterBootRecord.h"
|
#include "../storage/partitiontables/mbr/MasterBootRecord.h"
|
||||||
#include "../storage/filesystems/FAT/BiosParameterBlock.h"
|
#include "../storage/filesystems/FAT/FAT.h"
|
||||||
#include "../storage/filesystems/FAT/DirectoryEntry.h"
|
|
||||||
|
|
||||||
bool isRunning = true;
|
bool isRunning = true;
|
||||||
extern "C" void startSuperVisorTerminal()
|
extern "C" void startSuperVisorTerminal()
|
||||||
|
21
run.sh
21
run.sh
@ -1,12 +1,25 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
PROC=$$
|
||||||
|
|
||||||
|
# Build the Corelib static library
|
||||||
|
(cd CoreLib
|
||||||
|
if ! make; then
|
||||||
|
echo "Build failed!"
|
||||||
|
kill -10 $PROC
|
||||||
|
fi)
|
||||||
|
|
||||||
cd CoreLib
|
# Build the kernel image
|
||||||
make
|
(cd kernel
|
||||||
cd ../kernel
|
|
||||||
make clean
|
make clean
|
||||||
make
|
make
|
||||||
cd ..
|
if ! make; then
|
||||||
|
echo "Build failed!"
|
||||||
|
kill -10 $PROC
|
||||||
|
fi)
|
||||||
|
|
||||||
./scripts/update_harddrive.sh
|
./scripts/update_harddrive.sh
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
./scripts/run_qemu.sh
|
./scripts/run_qemu.sh
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user