This commit is contained in:
kudzu_cp 2011-02-24 03:24:08 +00:00
parent bad03a31ae
commit b3a7a30678
4 changed files with 79 additions and 1 deletions

View file

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

View file

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

View file

@ -59,7 +59,6 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="Cosmos.Debug.Kernel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5ae71220097cb983, processorArchitecture=MSIL" />
<Reference Include="Cosmos.System, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5ae71220097cb983, processorArchitecture=MSIL" />
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
@ -74,6 +73,14 @@
<Project>{1FAC100C-D732-4EA4-B518-5AF4BAF64F2E}</Project>
<Name>Cosmos.Common.Extensions</Name>
</ProjectReference>
<ProjectReference Include="..\..\..\Kernel\Common\Cosmos.Common\Cosmos.Common.csproj">
<Project>{04602B68-B877-4CA0-B17B-09C309732943}</Project>
<Name>Cosmos.Common</Name>
</ProjectReference>
<ProjectReference Include="..\..\..\Kernel\System\Cosmos.System\Cosmos.System.csproj">
<Project>{DA50B9B2-0E95-4F0D-A3C8-79FC549301B5}</Project>
<Name>Cosmos.System</Name>
</ProjectReference>
<ProjectReference Include="..\..\..\Kernel\System\Hardware\Core\Cosmos.Core\Cosmos.Core.csproj">
<Project>{5AC4773C-CB4E-4CD9-8D50-02E10A07DEE6}</Project>
<Name>Cosmos.Core</Name>

View file

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