diff --git a/source2/Kernel/System/Cosmos.System/Filesystem/FAT.cs b/source2/Kernel/System/Cosmos.System/Filesystem/FAT.cs index 077ac8029..dd3a7e2ec 100644 --- a/source2/Kernel/System/Cosmos.System/Filesystem/FAT.cs +++ b/source2/Kernel/System/Cosmos.System/Filesystem/FAT.cs @@ -5,5 +5,72 @@ using System.Text; namespace Cosmos.System.Filesystem { public class FAT : Filesystem { + readonly public int BytesPerSector; + readonly public int SectorsPerCluster; + readonly public int ReservedSectorCount; + readonly public int NumberOfFATs; + readonly public int FatSectorCount; + readonly public int TotalSectorCount; + readonly public int DataSectorCount; + readonly public int ClusterCount; + readonly public int RootSector; // FAT12/16 + readonly public int RootCluster; // FAT32 + readonly public int RootEntryCount; + + public enum FatTypeEnum {Unknown, Fat12, Fat16, Fat32} + readonly public FatTypeEnum FatType = FatTypeEnum.Unknown; + + Cosmos.Hardware.BlockDevice.BlockDevice mDevice; + + public FAT(Cosmos.Hardware.BlockDevice.BlockDevice aDevice) { + mDevice = aDevice; + + // 0xAA55 + // [510] == 0x55 + // [511] == 0xAA + // This is the FAT signature. We need to check it in the future. + + var xBPB = new byte[512]; + mDevice.ReadBlock(0, xBPB); + BytesPerSector = xBPB[12] << 8 | xBPB[11]; + SectorsPerCluster = xBPB[13]; + ReservedSectorCount = xBPB[15] << 8 | xBPB[14]; + NumberOfFATs = xBPB[16]; + RootEntryCount = xBPB[18] << 8 | xBPB[17]; + + TotalSectorCount = xBPB[20] << 8 | xBPB[19]; + if (TotalSectorCount == 0) { + TotalSectorCount = xBPB[35] << 24 | xBPB[34] << 16 | xBPB[33] << 8 | xBPB[32]; + } + + FatSectorCount = xBPB[23] << 8 | xBPB[22]; + if (FatSectorCount == 0) { + FatSectorCount = xBPB[39] << 24 | xBPB[38] << 16 | xBPB[37] << 8 | xBPB[36]; + } + + DataSectorCount = TotalSectorCount - (ReservedSectorCount + (NumberOfFATs * FatSectorCount) + ReservedSectorCount); + + // Computation rounds down. + ClusterCount = DataSectorCount / SectorsPerCluster; + // Determine the FAT type. Do not use another method - this IS the official and + // proper way to determine FAT type. + // Comparisons are purposefully < and not <= + // FAT16 starts at 4085 + // FAT32 starts at 65525 + if (ClusterCount < 4085) { + FatType = FatTypeEnum.Fat12; + } else if(ClusterCount < 65525) { + FatType = FatTypeEnum.Fat16; + } else { + FatType = FatTypeEnum.Fat32; + } + + if (FatType == FatTypeEnum.Fat32) { + RootCluster = xBPB[47] << 24 | xBPB[46] << 16 | xBPB[45] << 8 | xBPB[44]; + } else { + RootSector = ReservedSectorCount + (NumberOfFATs * FatSectorCount); + } + } + } } diff --git a/source2/Kernel/System/Hardware/Cosmos.Hardware/BlockDevice/Partition.cs b/source2/Kernel/System/Hardware/Cosmos.Hardware/BlockDevice/Partition.cs index 93d59eee5..b141582f3 100644 --- a/source2/Kernel/System/Hardware/Cosmos.Hardware/BlockDevice/Partition.cs +++ b/source2/Kernel/System/Hardware/Cosmos.Hardware/BlockDevice/Partition.cs @@ -20,12 +20,14 @@ namespace Cosmos.Hardware.BlockDevice { // TODO:UInt64 UInt32 xHostBlockNo = mStartingSector + aBlockNo; CheckBlockNo(xHostBlockNo); + mHost.ReadBlock(xHostBlockNo, aData); } public override void WriteBlock(UInt32 aBlockNo, byte[] aData) { // TODO:UInt64 UInt32 xHostBlockNo = mStartingSector + aBlockNo; CheckBlockNo(xHostBlockNo); + mHost.WriteBlock(xHostBlockNo, aData); } } diff --git a/source2/Users/Kudzu/Breakpoints/BreakpointsKernel.csproj b/source2/Users/Kudzu/Breakpoints/BreakpointsKernel.csproj index 906138c15..3e6aa55ed 100644 --- a/source2/Users/Kudzu/Breakpoints/BreakpointsKernel.csproj +++ b/source2/Users/Kudzu/Breakpoints/BreakpointsKernel.csproj @@ -59,7 +59,6 @@ - 3.5 @@ -74,6 +73,14 @@ {1FAC100C-D732-4EA4-B518-5AF4BAF64F2E} Cosmos.Common.Extensions + + {04602B68-B877-4CA0-B17B-09C309732943} + Cosmos.Common + + + {DA50B9B2-0E95-4F0D-A3C8-79FC549301B5} + Cosmos.System + {5AC4773C-CB4E-4CD9-8D50-02E10A07DEE6} Cosmos.Core diff --git a/source2/Users/Kudzu/Breakpoints/BreakpointsOS.cs b/source2/Users/Kudzu/Breakpoints/BreakpointsOS.cs index e234cffe3..2efa501d2 100644 --- a/source2/Users/Kudzu/Breakpoints/BreakpointsOS.cs +++ b/source2/Users/Kudzu/Breakpoints/BreakpointsOS.cs @@ -47,6 +47,8 @@ namespace BreakpointsKernel { Console.WriteLine("Model No: " + xATA.ModelNo); Console.WriteLine("Size: " + xATA.BlockCount * xATA.BlockSize / 1024 / 1024 + " MB"); + var xFS = new Cosmos.System.Filesystem.FAT(BlockDevice.Devices[1]); + //var xWrite = new byte[512]; //for (int i = 0; i < 512; i++) { // xWrite[i] = (byte)i;