Skip to content

Commit

Permalink
1.1.3 03/05/16 AJL Add button to erase MBR (allows us to then reforma…
Browse files Browse the repository at this point in the history
…t the drive to full capacity)
  • Loading branch information
ajlennon committed May 3, 2016
1 parent a3d0438 commit fbd1ab3
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 21 deletions.
6 changes: 3 additions & 3 deletions DiskImager.Installer/DiskImager.Installer.vdproj
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,15 @@
{
"Name" = "8:Microsoft Visual Studio"
"ProductName" = "8:Disk Imager"
"ProductCode" = "8:{7AE7013F-7719-46F7-BA1E-77085FB0AC2A}"
"PackageCode" = "8:{8B82D457-AA37-4EE1-877E-1AE6DDEFC68E}"
"ProductCode" = "8:{775B32DE-D69B-4A1A-9262-EB55FDC5F9DA}"
"PackageCode" = "8:{16CF7871-0DE7-4129-AB80-F3FBD415C4AB}"
"UpgradeCode" = "8:{A2F957D8-23F6-44DB-A22C-CCA8275E1FCE}"
"AspNetVersion" = "8:4.0.30319.0"
"RestartWWWService" = "11:FALSE"
"RemovePreviousVersions" = "11:FALSE"
"DetectNewerInstalledVersion" = "11:TRUE"
"InstallAllUsers" = "11:FALSE"
"ProductVersion" = "8:1.1.1"
"ProductVersion" = "8:1.1.3"
"Manufacturer" = "8:Dynamic Devices"
"ARPHELPTELEPHONE" = "8:"
"ARPHELPLINK" = "8:"
Expand Down
1 change: 0 additions & 1 deletion DiskImager.sln
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ Global
{1B0F140A-DFA6-4693-9C09-FCB9E0B35189}.Debug|x86.ActiveCfg = Debug|x86
{1B0F140A-DFA6-4693-9C09-FCB9E0B35189}.Release|Any CPU.ActiveCfg = Release|x86
{1B0F140A-DFA6-4693-9C09-FCB9E0B35189}.Release|Mixed Platforms.ActiveCfg = Release|x86
{1B0F140A-DFA6-4693-9C09-FCB9E0B35189}.Release|Mixed Platforms.Build.0 = Release|x86
{1B0F140A-DFA6-4693-9C09-FCB9E0B35189}.Release|x86.ActiveCfg = Release|x86
{0E7413FF-EB9E-4714-ACF2-BE3A6A7B2FFD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0E7413FF-EB9E-4714-ACF2-BE3A6A7B2FFD}.Debug|Any CPU.Build.0 = Debug|Any CPU
Expand Down
79 changes: 76 additions & 3 deletions DiskImager/Disk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,14 +287,88 @@ where zipEntry.IsFile
return !errored;
}

/// <summary>
/// Erase MBR of drive (allows us to then reinsert and it will reformat to full capacity)
/// </summary>
/// <param name="driveLetter"></param>
/// <param name="fileName"></param>
/// <param name="eCompType"></param>
/// <returns></returns>
public bool EraseMBR(string driveLetter)
{
var isErrored = false;

IsCancelling = false;

var dtStart = DateTime.Now;

//
// Get physical drive partition for logical partition
//
var physicalDrive = _diskAccess.GetPhysicalPathForLogicalPath(driveLetter);
if (string.IsNullOrEmpty(physicalDrive))
{
LogMsg(@"Error: Couldn't map partition to physical drive");
_diskAccess.UnlockDrive();
return false;
}

//
// Lock logical drive
//
var success = _diskAccess.LockDrive(driveLetter);
if (!success)
{
LogMsg(@"Failed to lock drive");
return false;
}


//
// Get drive size
//
var driveSize = _diskAccess.GetDriveSize(physicalDrive);
if (driveSize <= 0)
{
LogMsg(@"Failed to get device size");
_diskAccess.UnlockDrive();
return false;
}

//
// Open the physical drive
//
var physicalHandle = _diskAccess.Open(physicalDrive);
if (physicalHandle == null)
{
LogMsg(@"Failed to open physical drive");
_diskAccess.UnlockDrive();
return false;
}

var zeroData = new byte[512];
int wroteBytes;
if (_diskAccess.Write(zeroData, zeroData.Length, out wroteBytes) < 0)
{
LogMsg(@"Error writing data to drive: " + Marshal.GetHRForLastWin32Error());
isErrored = true;
}

_diskAccess.Close();

_diskAccess.UnlockDrive();

return !isErrored;
}

/// <summary>
/// Read data direct from drive to file
/// </summary>
/// <param name="driveLetter"></param>
/// <param name="fileName"></param>
/// <param name="eCompType"></param>
/// <returns></returns>
public bool ReadDrive(string driveLetter, string fileName, EnumCompressionType eCompType, bool bUseMBR)
public bool ReadDrive(string driveLetter, string fileName, EnumCompressionType eCompType, bool bUseMBR, long start, long length)
{
IsCancelling = false;

Expand Down Expand Up @@ -351,8 +425,7 @@ public bool ReadDrive(string driveLetter, string fileName, EnumCompressionType e

var buffer = new byte[Globals.MaxBufferSize];
var offset = 0L;



using(var basefs = (Stream)new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
Stream fs;
Expand Down
88 changes: 80 additions & 8 deletions DiskImager/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 54 additions & 3 deletions DiskImager/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public MainForm()

MessageBoxEx.Owner = this.Handle;

toolStripStatusLabel1.Text = @"Initialised. Licensed under GPLv3. Use at own risk!";
toolStripStatusLabel1.Text = @"Initialised. Licensed under GPLv3.";

saveFileDialog1.OverwritePrompt = false;
saveFileDialog1.Filter = @"Image Files (*.img,*.bin,*.sdcard)|*.img;*.bin;*.sdcard|Compressed Files (*.zip,*.gz,*tgz)|*.zip;*.gz;*.tgz|All files (*.*)|*.*";
Expand Down Expand Up @@ -169,7 +169,30 @@ private void ButtonReadClick(object sender, EventArgs e)

try
{
_disk.ReadDrive(drive, textBoxFileName.Text, _eCompType, checkBoxUseMBR.Checked);
var start = 0l;

if(!string.IsNullOrEmpty(textBoxStart.Text))
{
try
{
start = long.Parse(textBoxStart.Text);
} catch
{
}
}

var length = 0l;
if(!string.IsNullOrEmpty(textBoxLength.Text))
{
try
{
length = long.Parse(textBoxLength.Text);
} catch
{
}
}

_disk.ReadDrive(drive, textBoxFileName.Text, _eCompType, checkBoxUseMBR.Checked, start, length);
} catch(Exception ex)
{
toolStripStatusLabel1.Text = ex.Message;
Expand Down Expand Up @@ -229,6 +252,31 @@ private void ButtonWriteClick(object sender, EventArgs e)
EnableButtons();
}

private void ButtonEraseMBRClick(object sender, EventArgs e)
{
if (comboBoxDrives.SelectedIndex < 0)
return;

var drive = (string)comboBoxDrives.SelectedItem;

var success = false;
try
{
success = _disk.EraseMBR(drive);
}
catch (Exception ex)
{
success = false;
toolStripStatusLabel1.Text = ex.Message;
}

if (!success && !_disk.IsCancelling)
MessageBoxEx.Show("Problem writing to disk. Is it write-protected?", "Write Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
else
MessageBoxEx.Show("MBR erased. Please remove and reinsert to format", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}


/// <summary>
/// Called to persist registry values on closure so we can remember things like last file used
/// </summary>
Expand Down Expand Up @@ -325,7 +373,8 @@ private void ChooseFile()
return;

textBoxFileName.Text = saveFileDialog1.FileName;
TextBoxFileNameTextChanged(this, null);

TextBoxFileNameTextChanged(this, null);
}

private void TextBoxFileNameTextChanged(object sender, EventArgs e)
Expand Down Expand Up @@ -426,6 +475,7 @@ private void DisableButtons(bool running)
{
buttonRead.Enabled = false;
buttonWrite.Enabled = false;
buttonEraseMBR.Enabled = false;
buttonExit.Enabled = !running;
buttonCancel.Enabled = running;
comboBoxDrives.Enabled = false;
Expand All @@ -442,6 +492,7 @@ private void EnableButtons()
{
buttonRead.Enabled = true;
buttonWrite.Enabled = true;
buttonEraseMBR.Enabled = true;
buttonExit.Enabled = true;
buttonCancel.Enabled = false;
comboBoxDrives.Enabled = true;
Expand Down
4 changes: 2 additions & 2 deletions DiskImager/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.1.1.*")]
[assembly: AssemblyFileVersion("1.1.1.0")]
[assembly: AssemblyVersion("1.1.3.*")]
[assembly: AssemblyFileVersion("1.1.3.0")]
Loading

0 comments on commit fbd1ab3

Please sign in to comment.