-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.pas
More file actions
367 lines (324 loc) · 11 KB
/
Copy pathMainForm.pas
File metadata and controls
367 lines (324 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
unit MainForm;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, ExtCtrls, ShellAPI;
type
TFrmMain = class(TForm)
PanelTop: TPanel;
btnRefresh: TButton;
btnWipe: TButton;
btnStop: TButton;
chkForceOffline: TCheckBox;
grpMode: TGroupBox;
rbDisk: TRadioButton;
rbSSD: TRadioButton;
rbNVMe: TRadioButton;
ProgressBar: TProgressBar;
lblProgressGB: TLabel;
lblProgressBytes: TLabel;
lblSpeedEta: TLabel;
lvDisks: TListView;
Splitter1: TSplitter;
MemoLog: TMemo;
procedure FormCreate(Sender: TObject);
procedure btnRefreshClick(Sender: TObject);
procedure btnWipeClick(Sender: TObject);
procedure btnStopClick(Sender: TObject);
private
FCancel: Boolean;
FStartTick: DWORD;
FWinMajor: DWORD;
procedure Log(const S: string);
function SelectedDiskNo: Integer;
procedure UpdateProgress(BytesWritten, Total: Int64);
function CanContinue: Boolean;
function Floor1Dp(Value: Double): Double;
function FormatIntWithSep(Value: Int64): string;
function FormatHMS(Seconds: Int64): string;
function IsWin7Plus: Boolean;
function IsWin10Plus: Boolean;
public
end;
var
FrmMain: TFrmMain;
implementation
{$R *.dfm}
uses
DDWrap, SSDErase, NVMeErase;
type
RTL_OSVERSIONINFOW = packed record
dwOSVersionInfoSize: DWORD;
dwMajorVersion: DWORD;
dwMinorVersion: DWORD;
dwBuildNumber: DWORD;
dwPlatformId: DWORD;
szCSDVersion: array[0..127] of WideChar;
end;
TRtlGetVersion = function(var info: RTL_OSVERSIONINFOW): LongInt; stdcall;
function IsUserAnAdmin: BOOL; stdcall; external 'shell32.dll';
function GetRealWinVersion(out Major, Minor: DWORD): Boolean;
var
h: HMODULE;
fn: TRtlGetVersion;
info: RTL_OSVERSIONINFOW;
p: Pointer;
begin
Result := False; Major := 0; Minor := 0;
h := GetModuleHandle('ntdll.dll');
if h <> 0 then
begin
p := GetProcAddress(h, 'RtlGetVersion');
if p <> nil then
begin
fn := TRtlGetVersion(p);
ZeroMemory(@info, SizeOf(info));
info.dwOSVersionInfoSize := SizeOf(info);
if fn(info) = 0 then
begin
Major := info.dwMajorVersion;
Minor := info.dwMinorVersion;
Result := True;
end;
end;
end;
end;
function IsProcessAdmin: Boolean;
begin
Result := IsUserAnAdmin();
end;
function TFrmMain.IsWin7Plus: Boolean;
begin
Result := (FWinMajor > 6) or (FWinMajor = 6);
end;
function TFrmMain.IsWin10Plus: Boolean;
begin
Result := (FWinMajor >= 10);
end;
procedure TFrmMain.FormCreate(Sender: TObject);
var
realMj, realMn: DWORD;
begin
Caption := 'EasyDiskWiper';
lvDisks.ViewStyle := vsReport;
lvDisks.Columns.Add.Caption := 'Disk #';
lvDisks.Columns.Add.Caption := 'Path';
lvDisks.Columns.Add.Caption := 'Model';
lvDisks.Columns.Add.Caption := 'Bus';
lvDisks.Columns.Add.Caption := 'Size (GB)';
lvDisks.Columns.Add.Caption := 'Layout';
lvDisks.Columns.Add.Caption := 'Partitions';
lvDisks.Columns.Add.Caption := 'Volumes';
// Widths (existing + Bus=60)
lvDisks.Columns[0].Width := 45;
lvDisks.Columns[1].Width := 110;
lvDisks.Columns[2].Width := 260;
lvDisks.Columns[3].Width := 60; // Bus
lvDisks.Columns[4].Width := 70;
lvDisks.Columns[5].Width := 50;
lvDisks.Columns[6].Width := 250;
lvDisks.Columns[7].Width := 80;
ProgressBar.Min := 0;
ProgressBar.Max := 1000;
rbDisk.Checked := True;
Application.ShowHint := True;
if not GetRealWinVersion(realMj, realMn) then realMj := 5;
FWinMajor := realMj;
if IsWin7Plus and (not IsProcessAdmin) then
begin
if ShellExecute(0, 'runas', PChar(ParamStr(0)), nil, nil, SW_SHOWNORMAL) > 32 then
Halt(0);
end;
rbNVMe.Enabled := IsWin10Plus;
rbNVMe.ShowHint := True;
if rbNVMe.Enabled then rbNVMe.Hint := 'NVMe (Win10+)' else rbNVMe.Hint := 'Requires Windows 10 or later';
if (not rbNVMe.Enabled) and rbNVMe.Checked then rbDisk.Checked := True;
chkForceOffline.Enabled := IsWin7Plus;
chkForceOffline.ShowHint := True;
if chkForceOffline.Enabled then
chkForceOffline.Hint := 'Takes disk offline temporarily (Win7+)'
else
chkForceOffline.Hint := 'Requires Windows 7 or later';
btnRefreshClick(Self);
end;
procedure TFrmMain.Log(const S: string);
begin
MemoLog.Lines.Add(FormatDateTime('hh:nn:ss', Now) + ' ' + S);
MemoLog.SelStart := Length(MemoLog.Text);
MemoLog.Perform(WM_VSCROLL, SB_BOTTOM, 0);
end;
function TFrmMain.Floor1Dp(Value: Double): Double;
begin
if Value < 0 then Result := 0 else Result := Int(Value*10.0)/10.0;
end;
function TFrmMain.FormatIntWithSep(Value: Int64): string;
var s: string; i, j: Integer;
begin
s := IntToStr(Value);
Result := ''; j := 0;
for i := Length(s) downto 1 do
begin
Inc(j);
Result := s[i] + Result;
if (j mod 3 = 0) and (i > 1) then Result := ',' + Result;
end;
end;
function TFrmMain.FormatHMS(Seconds: Int64): string;
var h, m, s: Int64;
begin
if Seconds < 0 then Seconds := 0;
h := Seconds div 3600;
m := (Seconds mod 3600) div 60;
s := Seconds mod 60;
if h > 0 then Result := Format('%d:%2.2d:%2.2d', [h, m, s])
else Result := Format('%d:%2.2d', [m, s]);
end;
procedure TFrmMain.UpdateProgress(BytesWritten, Total: Int64);
var
pct: Integer; gbWritten, gbTotal: Double; nowTick: DWORD;
elapsedMs: Int64; avgMBs: Double; etaSec: Int64;
begin
if Total > 0 then pct := Trunc(BytesWritten * 1000.0 / Total) else pct := 0;
if pct < 0 then pct := 0; if pct > 1000 then pct := 1000;
ProgressBar.Position := pct;
gbWritten := Floor1Dp(BytesWritten / 1073741824.0);
gbTotal := Total / 1073741824.0;
lblProgressGB.Caption := Format('%.1f / %.1f GB', [gbWritten, gbTotal]);
lblProgressBytes.Caption := '(' + FormatIntWithSep(BytesWritten) + ' bytes written)';
Caption := 'EasyDiskWiper - ' + lblProgressGB.Caption;
nowTick := GetTickCount;
if FStartTick = 0 then FStartTick := nowTick;
elapsedMs := (nowTick - FStartTick) and $FFFFFFFF;
if elapsedMs > 0 then
begin
avgMBs := (BytesWritten / 1048576.0) / (elapsedMs / 1000.0);
if avgMBs < 0 then avgMBs := 0;
if (Total > 0) and (BytesWritten < Total) and (avgMBs > 0) then
etaSec := Round((Total - BytesWritten) / (avgMBs * 1048576.0))
else
etaSec := 0;
lblSpeedEta.Caption := Format('%.1f MB/s | ETA %s', [avgMBs, FormatHMS(etaSec)]);
end
else
lblSpeedEta.Caption := '0.0 MB/s | ETA 0:00';
Application.ProcessMessages;
end;
function TFrmMain.SelectedDiskNo: Integer;
begin
if (lvDisks.Selected <> nil) and (lvDisks.Selected.Caption <> '') then
Result := StrToIntDef(lvDisks.Selected.Caption, -1)
else
Result := -1;
end;
procedure TFrmMain.btnRefreshClick(Sender: TObject);
var
disks: TDDDiskList; i: Integer; item: TListItem;
begin
lvDisks.Items.BeginUpdate;
try
lvDisks.Items.Clear;
disks := DDEnumerateDisks;
try
for i := 0 to disks.Count - 1 do
begin
item := lvDisks.Items.Add;
item.Caption := IntToStr(disks[i].Number);
item.SubItems.Add(disks[i].DevicePath);
item.SubItems.Add(disks[i].Model);
item.SubItems.Add(disks[i].BusName);
item.SubItems.Add(FormatFloat('0.0', disks[i].SizeBytes / 1073741824.0));
item.SubItems.Add(disks[i].Layout);
item.SubItems.Add(disks[i].PartitionsSummary);
item.SubItems.Add(disks[i].Volumes);
end;
finally
disks.Free;
end;
finally
lvDisks.Items.EndUpdate;
end;
Log('Refreshed disk list.');
end;
function TFrmMain.CanContinue: Boolean;
begin
Application.ProcessMessages;
Result := not FCancel;
end;
procedure TFrmMain.btnWipeClick(Sender: TObject);
var
d: Integer; total: Int64; info: TSSDSecurityInfo;
bus: DWORD;
begin
d := SelectedDiskNo;
if d < 0 then begin ShowMessage('Select a disk first.'); Exit; end;
if rbDisk.Checked then
begin
if not DDGetDiskLength(DDPhysicalPath(d), total) then
begin ShowMessage('Could not query disk size.'); Exit; end;
if MessageDlg(Format('This will ERASE the ENTIRE disk %d. Continue?',[d]), mtWarning, mbOKCancel, 0) <> mrOk then Exit;
FCancel := False; FStartTick := 0;
btnWipe.Enabled := False; btnStop.Enabled := True; btnRefresh.Enabled := False;
Log(Format('Wiping disk %d (%.1f GB) ...', [d, total / 1073741824.0]));
ProgressBar.Position := 0; lblProgressGB.Caption := ''; lblProgressBytes.Caption := ''; lblSpeedEta.Caption := '';
if DDWipeSinglePass(d, UpdateProgress, Log, CanContinue, chkForceOffline.Checked) then
Log('Wipe completed successfully.')
else
Log('Wipe failed.');
btnWipe.Enabled := True; btnStop.Enabled := False; btnRefresh.Enabled := True;
Exit;
end;
bus := DDGetBusType(d);
if rbSSD.Checked then
begin
if bus = 7 {USB} then
begin
ShowMessage('ATA Secure Erase is not available through most USB-SATA bridges. Connect the SSD to a native SATA port and try again.');
Exit;
end;
if not SSDQuerySecurityInfo(d, info) then
begin ShowMessage('Device does not report ATA security info. It may not be a native ATA/SATA disk.'); Exit; end;
if info.Frozen then begin ShowMessage('Drive is FROZEN by BIOS/OS. Power cycle or suspend/resume to unfreeze, then try again.'); Exit; end;
if MessageDlg(Format('Perform ATA Secure Erase on disk %d?'#13#10#13#10 +
'Estimated time: %d-%d minutes (from IDENTIFY).',
[d, info.EraseTimeMin, info.EnhEraseTimeMin]), mtWarning, mbOKCancel, 0) <> mrOk then Exit;
btnWipe.Enabled := False; btnStop.Enabled := False; btnRefresh.Enabled := False;
try
Log('Starting ATA Secure Erase (standard)...');
if SSDAtaSecureErase(d, False, Log) then
Log('Secure Erase command issued; device will complete internally.')
else
Log('Secure Erase failed to issue.');
finally
btnWipe.Enabled := True; btnStop.Enabled := False; btnRefresh.Enabled := True;
end;
Exit;
end;
if rbNVMe.Checked then
begin
if not IsWin10Plus then begin ShowMessage('NVMe Format/Sanitize requires Windows 10 or later.'); Exit; end;
if bus = 7 {USB} then
begin
ShowMessage('NVMe admin commands are not exposed over typical USB NVMe enclosures (UASP). Connect the NVMe drive directly to an M.2/PCIe slot.');
Exit;
end;
if not DDIsNVMe(d) then begin ShowMessage('Selected device is not NVMe.'); Exit; end;
if MessageDlg(Format('Perform NVMe Format NVM (crypto erase if supported) on disk %d?',[d]), mtWarning, mbOKCancel, 0) <> mrOk then Exit;
btnWipe.Enabled := False; btnStop.Enabled := False; btnRefresh.Enabled := False;
try
Log('Issuing NVMe Format NVM (with crypto-erase flag if supported)...');
if NVMeFormatNVM(d, True, Log) then
Log('NVMe format/sanitize issued successfully (drive completes internally).')
else
Log('NVMe format/sanitize failed to issue.');
finally
btnWipe.Enabled := True; btnStop.Enabled := False; btnRefresh.Enabled := True;
end;
Exit;
end;
end;
procedure TFrmMain.btnStopClick(Sender: TObject);
begin
FCancel := True;
Log('Cancel requested...');
end;
end.