Start working on FAT formatter

This commit is contained in:
Pdawg11239
2025-03-16 13:51:50 -04:00
parent 642fd5e1ed
commit 87cf731f0b
6 changed files with 127 additions and 1 deletions

View File

@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,9 @@
using System.Runtime.InteropServices;
namespace BadBuilder.Formatter
{
public class DiskFormatter
{
}
}

View File

@@ -0,0 +1,49 @@
using System.Runtime.InteropServices;
namespace BadBuilder.Formatter
{
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct FAT32BootSector
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public byte[] JumpCode;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] OEMName;
public ushort BytesPerSector;
public byte SectorsPerCluster;
public ushort ReservedSectorCount;
public byte NumberOfFATs;
public ushort MaxRootEntries; // Unused in FAT32
public ushort TotalSectors16; // If 0, use TotalSectors
public byte MediaDescriptor;
public ushort SectorsPerFAT16; // Unused in FAT32
public ushort SectorsPerTrack;
public ushort NumberOfHeads;
public uint HiddenSectors;
public uint TotalSectors; // Total sectors (if TotalSectors16 is 0)
// FAT32-specific fields
public uint SectorsPerFAT;
public ushort FATFlags;
public ushort FileSystemVersion;
public uint RootCluster;
public ushort FSInfoSector;
public ushort BackupBootSector;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)]
public byte[] Reserved;
public byte DriveNumber;
public byte Reserved1;
public byte BootSignature;
public uint VolumeID;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 11)]
public byte[] VolumeLabel;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] FileSystemType;
}
}

View File

@@ -0,0 +1,22 @@
using System.Runtime.InteropServices;
namespace BadBuilder.Formatter
{
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct FAT32FsInfo
{
public uint LeadSignature; // Should be 0x41615252
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 480)]
public byte[] Reserved1; // Zeros
public uint StructureSignature; // Should be 0x61417272
public uint FreeClusterCount; // Number of free clusters (or 0xFFFFFFFF if unknown)
public uint NextFreeCluster; // Next free cluster (or 0xFFFFFFFF if unknown)
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)]
public byte[] Reserved2; // Zeros
public uint TrailSignature; // Should be 0xAA550000
}
}

View File

@@ -0,0 +1,28 @@
namespace BadBuilder.Formatter
{
static class FAT32Utilities
{
internal static uint GetVolumeID()
{
DateTime now = DateTime.Now;
ushort low = (ushort)(now.Day + (now.Month << 8));
low += (ushort)((now.Millisecond / 10) + (now.Second << 8));
ushort hi = (ushort)(now.Minute + (now.Hour << 8));
hi += (ushort)now.Year;
return (uint)(low | (hi << 16));
}
internal static uint CalculateFATSize(uint diskSize, uint reservedSectorCount, uint sectorsPerCluster, uint numberOfFATs, uint bytesPerSector)
{
const ulong fatElementSize = 4;
ulong numerator = fatElementSize * (diskSize - reservedSectorCount);
ulong denominator = (sectorsPerCluster * bytesPerSector) + (fatElementSize * numberOfFATs);
return (uint)((numerator / denominator) + 1);
}
}
}

View File

@@ -1,10 +1,12 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35707.178 d17.12
VisualStudioVersion = 17.12.35707.178
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BadBuilder", "BadBuilder\BadBuilder.csproj", "{654D211A-668A-4BA4-8BD4-174A11666A3B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BadBuilder.Formatter", "BadBuilder.Formatter\BadBuilder.Formatter.csproj", "{30B03E55-670C-47F3-A9C6-7533C9095669}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -15,8 +17,15 @@ Global
{654D211A-668A-4BA4-8BD4-174A11666A3B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{654D211A-668A-4BA4-8BD4-174A11666A3B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{654D211A-668A-4BA4-8BD4-174A11666A3B}.Release|Any CPU.Build.0 = Release|Any CPU
{30B03E55-670C-47F3-A9C6-7533C9095669}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{30B03E55-670C-47F3-A9C6-7533C9095669}.Debug|Any CPU.Build.0 = Debug|Any CPU
{30B03E55-670C-47F3-A9C6-7533C9095669}.Release|Any CPU.ActiveCfg = Release|Any CPU
{30B03E55-670C-47F3-A9C6-7533C9095669}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {91C4262D-5321-41BC-BF21-B7897AFE8F3C}
EndGlobalSection
EndGlobal