From 87cf731f0b0b129c3b322d6db0e0426ef55ed01d Mon Sep 17 00:00:00 2001 From: Pdawg11239 <83825746+Pdawg-bytes@users.noreply.github.com> Date: Sun, 16 Mar 2025 13:51:50 -0400 Subject: [PATCH] Start working on FAT formatter --- .../BadBuilder.Formatter.csproj | 9 ++++ BadBuilder.Formatter/Class1.cs | 9 ++++ BadBuilder.Formatter/FAT32BootSector.cs | 49 +++++++++++++++++++ BadBuilder.Formatter/FAT32FsInfo.cs | 22 +++++++++ BadBuilder.Formatter/FAT32Utilities.cs | 28 +++++++++++ BadBuilder.sln | 11 ++++- 6 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 BadBuilder.Formatter/BadBuilder.Formatter.csproj create mode 100644 BadBuilder.Formatter/Class1.cs create mode 100644 BadBuilder.Formatter/FAT32BootSector.cs create mode 100644 BadBuilder.Formatter/FAT32FsInfo.cs create mode 100644 BadBuilder.Formatter/FAT32Utilities.cs diff --git a/BadBuilder.Formatter/BadBuilder.Formatter.csproj b/BadBuilder.Formatter/BadBuilder.Formatter.csproj new file mode 100644 index 0000000..fa71b7a --- /dev/null +++ b/BadBuilder.Formatter/BadBuilder.Formatter.csproj @@ -0,0 +1,9 @@ + + + + net8.0 + enable + enable + + + diff --git a/BadBuilder.Formatter/Class1.cs b/BadBuilder.Formatter/Class1.cs new file mode 100644 index 0000000..7ba8ce6 --- /dev/null +++ b/BadBuilder.Formatter/Class1.cs @@ -0,0 +1,9 @@ +using System.Runtime.InteropServices; + +namespace BadBuilder.Formatter +{ + public class DiskFormatter + { + + } +} diff --git a/BadBuilder.Formatter/FAT32BootSector.cs b/BadBuilder.Formatter/FAT32BootSector.cs new file mode 100644 index 0000000..0634994 --- /dev/null +++ b/BadBuilder.Formatter/FAT32BootSector.cs @@ -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; + } +} \ No newline at end of file diff --git a/BadBuilder.Formatter/FAT32FsInfo.cs b/BadBuilder.Formatter/FAT32FsInfo.cs new file mode 100644 index 0000000..461fe37 --- /dev/null +++ b/BadBuilder.Formatter/FAT32FsInfo.cs @@ -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 + } +} \ No newline at end of file diff --git a/BadBuilder.Formatter/FAT32Utilities.cs b/BadBuilder.Formatter/FAT32Utilities.cs new file mode 100644 index 0000000..af7b54b --- /dev/null +++ b/BadBuilder.Formatter/FAT32Utilities.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/BadBuilder.sln b/BadBuilder.sln index ea1b940..fc820d2 100644 --- a/BadBuilder.sln +++ b/BadBuilder.sln @@ -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