Clean up formatter
This commit is contained in:
@@ -22,16 +22,16 @@ namespace BadBuilder.Formatter
|
|||||||
if (driveHandle.IsInvalid) return Error("Unable to open device. GetLastError: " + Marshal.GetLastWin32Error());
|
if (driveHandle.IsInvalid) return Error("Unable to open device. GetLastError: " + Marshal.GetLastWin32Error());
|
||||||
|
|
||||||
if (!EnableExtendedDASDIO(driveHandle) || !LockDevice(driveHandle))
|
if (!EnableExtendedDASDIO(driveHandle) || !LockDevice(driveHandle))
|
||||||
return Error("Failed to initialize device access.");
|
return Error($"Failed to initialize device access. GetLastError: {Marshal.GetLastWin32Error()}");
|
||||||
|
|
||||||
DISK_GEOMETRY diskGeometry;
|
DISK_GEOMETRY diskGeometry;
|
||||||
if (!TryGetDiskGeometry(driveHandle, out diskGeometry))
|
if (!TryGetDiskGeometry(driveHandle, out diskGeometry))
|
||||||
return Error("Failed to get drive geometry.");
|
return Error($"Failed to get disk geometry. GetLastError: {Marshal.GetLastWin32Error()}");
|
||||||
|
|
||||||
PARTITION_INFORMATION partitionInfo = new();
|
PARTITION_INFORMATION partitionInfo;
|
||||||
bool isGPT = false;
|
bool isGPT = false;
|
||||||
if (!TryGetPartitionInfo(driveHandle, ref diskGeometry, out partitionInfo, out isGPT))
|
if (!TryGetPartitionInfo(driveHandle, ref diskGeometry, out partitionInfo, out isGPT))
|
||||||
return Error("Failed to get partition information.");
|
return Error($"Failed to get partition information. GetLastError: {Marshal.GetLastWin32Error()}");
|
||||||
|
|
||||||
uint totalSectors = (uint)(partitionInfo.PartitionLength / diskGeometry.BytesPerSector);
|
uint totalSectors = (uint)(partitionInfo.PartitionLength / diskGeometry.BytesPerSector);
|
||||||
if (!IsValidFAT32Size(totalSectors))
|
if (!IsValidFAT32Size(totalSectors))
|
||||||
@@ -175,18 +175,25 @@ namespace BadBuilder.Formatter
|
|||||||
return sector;
|
return sector;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static unsafe string FormatVolumeData(SafeFileHandle driveHandle, DISK_GEOMETRY diskGeometry, FAT32BootSector bootSector, FAT32FsInfoSector fsInfo, uint[] firstFATSector, bool isGPT, PARTITION_INFORMATION partitionInfo)
|
private static unsafe string FormatVolumeData(
|
||||||
|
SafeFileHandle driveHandle,
|
||||||
|
DISK_GEOMETRY diskGeometry,
|
||||||
|
FAT32BootSector bootSector,
|
||||||
|
FAT32FsInfoSector fsInfo,
|
||||||
|
uint[] firstFATSector,
|
||||||
|
bool isGPT,
|
||||||
|
PARTITION_INFORMATION partitionInfo)
|
||||||
{
|
{
|
||||||
uint bytesPerSector = diskGeometry.BytesPerSector;
|
uint bytesPerSector = diskGeometry.BytesPerSector;
|
||||||
uint totalSectors = (uint)(partitionInfo.PartitionLength / diskGeometry.BytesPerSector);
|
uint totalSectors = (uint)(partitionInfo.PartitionLength / bytesPerSector);
|
||||||
uint userAreaSize = totalSectors - bootSector.ReservedSectorCount - (bootSector.NumberOfFATs * bootSector.SectorsPerFAT);
|
|
||||||
uint systemAreaSize = bootSector.ReservedSectorCount + (bootSector.NumberOfFATs * bootSector.SectorsPerFAT) + bootSector.SectorsPerCluster;
|
uint systemAreaSize = bootSector.ReservedSectorCount + (bootSector.NumberOfFATs * bootSector.SectorsPerFAT) + bootSector.SectorsPerCluster;
|
||||||
|
uint userAreaSize = totalSectors - systemAreaSize;
|
||||||
uint clusterCount = userAreaSize / bootSector.SectorsPerCluster;
|
uint clusterCount = userAreaSize / bootSector.SectorsPerCluster;
|
||||||
|
|
||||||
if (clusterCount < 65536 || clusterCount > 0x0FFFFFFF)
|
if (clusterCount < 65536 || clusterCount > 0x0FFFFFFF)
|
||||||
return Error("The drives cluster count is out of range (65536 < clusterCount < 0x0FFFFFFF)");
|
return Error("The drive's cluster count is out of range (65536 < clusterCount < 0x0FFFFFFF)");
|
||||||
|
|
||||||
fsInfo.FreeClusterCount = (userAreaSize / bootSector.SectorsPerCluster) - 1;
|
fsInfo.FreeClusterCount = clusterCount - 1;
|
||||||
|
|
||||||
ZeroOutSectors(driveHandle, 0, systemAreaSize, bytesPerSector);
|
ZeroOutSectors(driveHandle, 0, systemAreaSize, bytesPerSector);
|
||||||
|
|
||||||
@@ -199,16 +206,13 @@ namespace BadBuilder.Formatter
|
|||||||
|
|
||||||
for (int i = 0; i < bootSector.NumberOfFATs; i++)
|
for (int i = 0; i < bootSector.NumberOfFATs; i++)
|
||||||
{
|
{
|
||||||
uint sectorStart = bootSector.ReservedSectorCount + ((uint)i * bootSector.SectorsPerFAT);
|
uint sectorStart = (uint)(bootSector.ReservedSectorCount + (i * bootSector.SectorsPerFAT));
|
||||||
WriteSector(driveHandle, sectorStart, 1, bytesPerSector, UintArrayToBytes(firstFATSector));
|
WriteSector(driveHandle, sectorStart, 1, bytesPerSector, UintArrayToBytes(firstFATSector));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isGPT)
|
if (!isGPT)
|
||||||
{
|
{
|
||||||
SET_PARTITION_INFORMATION setPartInfo = new SET_PARTITION_INFORMATION
|
SET_PARTITION_INFORMATION setPartInfo = new() { PartitionType = 0x0C };
|
||||||
{
|
|
||||||
PartitionType = 0x0C
|
|
||||||
};
|
|
||||||
|
|
||||||
if (!DeviceIoControl(driveHandle, IOCTL_DISK_SET_PARTITION_INFO, &setPartInfo, (uint)sizeof(SET_PARTITION_INFORMATION), null, 0, null, null))
|
if (!DeviceIoControl(driveHandle, IOCTL_DISK_SET_PARTITION_INFO, &setPartInfo, (uint)sizeof(SET_PARTITION_INFORMATION), null, 0, null, null))
|
||||||
return Error($"Failed to set the drive partition information. GetLastError: {Marshal.GetLastWin32Error()}");
|
return Error($"Failed to set the drive partition information. GetLastError: {Marshal.GetLastWin32Error()}");
|
||||||
|
|||||||
@@ -71,13 +71,11 @@ namespace BadBuilder.Formatter
|
|||||||
|
|
||||||
internal static unsafe void WriteSector(SafeHandle hDevice, uint sector, uint numberOfSectors, uint bytesPerSector, byte[] data)
|
internal static unsafe void WriteSector(SafeHandle hDevice, uint sector, uint numberOfSectors, uint bytesPerSector, byte[] data)
|
||||||
{
|
{
|
||||||
uint bytesWritten;
|
|
||||||
|
|
||||||
SeekTo(hDevice, sector, bytesPerSector);
|
SeekTo(hDevice, sector, bytesPerSector);
|
||||||
|
|
||||||
fixed (byte* pData = &data[0])
|
fixed (byte* pData = &data[0])
|
||||||
{
|
{
|
||||||
if (!WriteFile(new HANDLE(hDevice.DangerousGetHandle()), pData, numberOfSectors * bytesPerSector, &bytesWritten, null))
|
if (!WriteFile(new HANDLE(hDevice.DangerousGetHandle()), pData, numberOfSectors * bytesPerSector, null, null))
|
||||||
ExitWithError($"Unable to write sectors to FAT32 device, exiting. GetLastError: {Marshal.GetLastWin32Error()}");
|
ExitWithError($"Unable to write sectors to FAT32 device, exiting. GetLastError: {Marshal.GetLastWin32Error()}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -86,7 +84,6 @@ namespace BadBuilder.Formatter
|
|||||||
{
|
{
|
||||||
const uint burstSize = 128;
|
const uint burstSize = 128;
|
||||||
uint writeSize;
|
uint writeSize;
|
||||||
uint bytesWritten;
|
|
||||||
|
|
||||||
byte* pZeroSector = (byte*)VirtualAlloc(null, bytesPerSector * burstSize, VIRTUAL_ALLOCATION_TYPE.MEM_COMMIT | VIRTUAL_ALLOCATION_TYPE.MEM_RESERVE, PAGE_PROTECTION_FLAGS.PAGE_READWRITE);
|
byte* pZeroSector = (byte*)VirtualAlloc(null, bytesPerSector * burstSize, VIRTUAL_ALLOCATION_TYPE.MEM_COMMIT | VIRTUAL_ALLOCATION_TYPE.MEM_RESERVE, PAGE_PROTECTION_FLAGS.PAGE_READWRITE);
|
||||||
|
|
||||||
@@ -98,7 +95,7 @@ namespace BadBuilder.Formatter
|
|||||||
{
|
{
|
||||||
writeSize = (numberOfSectors > burstSize) ? burstSize : numberOfSectors;
|
writeSize = (numberOfSectors > burstSize) ? burstSize : numberOfSectors;
|
||||||
|
|
||||||
if (!WriteFile(new HANDLE(hDevice.DangerousGetHandle()), pZeroSector, writeSize * bytesPerSector, &bytesWritten, null))
|
if (!WriteFile(new HANDLE(hDevice.DangerousGetHandle()), pZeroSector, writeSize * bytesPerSector, null, null))
|
||||||
ExitWithError($"Unable to write sectors to FAT32 device, exiting. GetLastError: {Marshal.GetLastWin32Error()}");
|
ExitWithError($"Unable to write sectors to FAT32 device, exiting. GetLastError: {Marshal.GetLastWin32Error()}");
|
||||||
|
|
||||||
numberOfSectors -= writeSize;
|
numberOfSectors -= writeSize;
|
||||||
|
|||||||
@@ -39,10 +39,11 @@ namespace BadBuilder
|
|||||||
bool ret = true;
|
bool ret = true;
|
||||||
string output = string.Empty;
|
string output = string.Empty;
|
||||||
|
|
||||||
AnsiConsole.Status().SpinnerStyle(LightOrangeStyle).Start($"[#76B900]Formatting disk[/] {selectedDisk}", ctx =>
|
AnsiConsole.Status().SpinnerStyle(LightOrangeStyle).Start($"[#76B900]Formatting disk[/] {selectedDisk}", async ctx =>
|
||||||
{
|
{
|
||||||
if (diskIndex == -1) return;
|
if (diskIndex == -1) return;
|
||||||
|
|
||||||
|
ClearConsole();
|
||||||
output = DiskHelper.FormatDisk(disks[diskIndex]);
|
output = DiskHelper.FormatDisk(disks[diskIndex]);
|
||||||
if (output != string.Empty) ret = false;
|
if (output != string.Empty) ret = false;
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -13,8 +13,7 @@ namespace BadBuilder
|
|||||||
.Columns(
|
.Columns(
|
||||||
new TaskDescriptionColumn(),
|
new TaskDescriptionColumn(),
|
||||||
new ProgressBarColumn().FinishedStyle(GreenStyle).CompletedStyle(LightOrangeStyle),
|
new ProgressBarColumn().FinishedStyle(GreenStyle).CompletedStyle(LightOrangeStyle),
|
||||||
new PercentageColumn().CompletedStyle(GreenStyle),
|
new PercentageColumn().CompletedStyle(GreenStyle)
|
||||||
new RemainingTimeColumn().Style(GrayStyle)
|
|
||||||
)
|
)
|
||||||
.StartAsync(async ctx =>
|
.StartAsync(async ctx =>
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
using BadBuilder.Models;
|
using BadBuilder.Models;
|
||||||
using BadBuilder.Formatter;
|
using BadBuilder.Formatter;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
using Spectre.Console;
|
||||||
|
|
||||||
namespace BadBuilder.Helpers
|
namespace BadBuilder.Helpers
|
||||||
{
|
{
|
||||||
@@ -27,6 +29,12 @@ namespace BadBuilder.Helpers
|
|||||||
return disks;
|
return disks;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static string FormatDisk(DiskInfo disk) => DiskFormatter.FormatVolume(disk.DriveLetter.ToCharArray()[0]);
|
internal static string FormatDisk(DiskInfo disk)
|
||||||
|
{
|
||||||
|
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||||
|
return "\u001b[38;2;255;114;0m[-]\u001b[0m Formatting is currently only supported on Windows. Please format your drive manually and try again.";
|
||||||
|
|
||||||
|
return DiskFormatter.FormatVolume(disk.DriveLetter[0]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -48,8 +48,6 @@ namespace BadBuilder
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ClearConsole();
|
|
||||||
|
|
||||||
List<ArchiveItem> downloadedFiles = DownloadRequiredFiles().Result;
|
List<ArchiveItem> downloadedFiles = DownloadRequiredFiles().Result;
|
||||||
ExtractFiles(downloadedFiles).Wait();
|
ExtractFiles(downloadedFiles).Wait();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user