ReactOS 0.4.16-dev-1972-gf20c09f
clean.c File Reference
#include "diskpart.h"
#include <debug.h>
Include dependency graph for clean.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

BOOL clean_main (_In_ INT argc, _In_ PWSTR *argv)
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 11 of file clean.c.

Function Documentation

◆ clean_main()

BOOL clean_main ( _In_ INT  argc,
_In_ PWSTR argv 
)

Definition at line 16 of file clean.c.

19{
21 PPARTENTRY PartEntry;
22 PVOLENTRY VolumeEntry;
23 BOOL bAll = FALSE;
24 PUCHAR SectorsBuffer = NULL;
25 ULONG LayoutBufferSize, Size;
26 INT i;
32 LARGE_INTEGER Offset, Count, MaxCount;
34
35 DPRINT("Clean()\n");
36
37 if (CurrentDisk == NULL)
38 {
40 return TRUE;
41 }
42
43 /* Do not allow to clean the boot disk */
44 if ((CurrentDisk->BiosFound == TRUE) &&
46 {
48 return TRUE;
49 }
50
51 for (i = 1; i < argc; i++)
52 {
53 if (_wcsicmp(argv[1], L"all") == 0)
54 {
55 bAll = TRUE;
56 }
57 }
58
59 /* Dismount and remove all logical partitions */
61 {
63 PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
64
65 /* Dismount the logical partition */
66 if (PartEntry->PartitionType != 0)
67 {
68 DismountVolume(PartEntry);
69 VolumeEntry = GetVolumeFromPartition(PartEntry);
70 if (VolumeEntry)
71 RemoveVolume(VolumeEntry);
72 }
73
74 /* Delete it */
75 RtlFreeHeap(RtlGetProcessHeap(), 0, PartEntry);
76 }
77
78 /* Dismount and remove all primary partitions */
80 {
82 PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
83
84 /* Dismount the primary partition */
85 if ((PartEntry->PartitionType != 0) &&
87 {
88 DismountVolume(PartEntry);
89 VolumeEntry = GetVolumeFromPartition(PartEntry);
90 if (VolumeEntry)
91 RemoveVolume(VolumeEntry);
92 }
93
94 /* Delete it */
95 RtlFreeHeap(RtlGetProcessHeap(), 0, PartEntry);
96 }
97
98 /* Initialize the disk entry */
102 CurrentDisk->PartitionStyle = PARTITION_STYLE_RAW;
103
104 /* Wipe the layout buffer */
105 RtlFreeHeap(RtlGetProcessHeap(), 0, CurrentDisk->LayoutBuffer);
106
107 LayoutBufferSize = sizeof(DRIVE_LAYOUT_INFORMATION_EX) +
108 ((4 - ANYSIZE_ARRAY) * sizeof(PARTITION_INFORMATION_EX));
109 CurrentDisk->LayoutBuffer = RtlAllocateHeap(RtlGetProcessHeap(),
111 LayoutBufferSize);
113 {
114 DPRINT1("Failed to allocate the disk layout buffer!\n");
115 return TRUE;
116 }
117
118 CurrentDisk->LayoutBuffer->PartitionStyle = PARTITION_STYLE_RAW;
119
120 /* Allocate a 1MB sectors buffer */
121 SectorsBuffer = RtlAllocateHeap(RtlGetProcessHeap(),
123 1024 * 1024);
124 if (SectorsBuffer == NULL)
125 {
126 DPRINT1("Failed to allocate the sectors buffer!\n");
127 goto done;
128 }
129
130 /* Open the disk for writing */
132 L"\\Device\\Harddisk%d\\Partition0",
134
136
138 &Name,
140 NULL,
141 NULL);
142
147 0,
149 if (!NT_SUCCESS(Status))
150 {
151 DPRINT1("Failed to open the disk! (Status 0x%08lx)\n", Status);
153 goto done;
154 }
155
156 /* Clean sectors */
157 if (bAll)
158 {
159 MaxCount.QuadPart = (CurrentDisk->SectorCount.QuadPart * CurrentDisk->BytesPerSector) / (1024 * 1024);
160 for (Count.QuadPart = 0; Count.QuadPart < MaxCount.QuadPart; Count.QuadPart++)
161 {
162 Offset.QuadPart = Count.QuadPart * (1024 * 1024);
164 NULL,
165 NULL,
166 NULL,
168 SectorsBuffer,
169 1024 * 1024,
170 &Offset,
171 NULL);
172 if (!NT_SUCCESS(Status))
173 {
174 DPRINT1("Failed to write MB! (Status 0x%08lx)\n", Status);
176 goto done;
177 }
178 }
179
181 if (Size != 0)
182 {
183 Offset.QuadPart += (1024 * 1024);
185 NULL,
186 NULL,
187 NULL,
189 SectorsBuffer,
190 Size,
191 &Offset,
192 NULL);
193 if (!NT_SUCCESS(Status))
194 {
195 DPRINT1("Failed to write the last part! (Status 0x%08lx)\n", Status);
197 goto done;
198 }
199 }
200 }
201 else
202 {
203 /* Clean the first MB */
204 Offset.QuadPart = 0;
206 NULL,
207 NULL,
208 NULL,
210 SectorsBuffer,
211 1024 * 1024,
212 &Offset,
213 NULL);
214 if (!NT_SUCCESS(Status))
215 {
216 DPRINT1("Failed to write the first MB! (Status 0x%08lx)\n", Status);
218 goto done;
219 }
220
221 /* Clean the last MB */
222 Offset.QuadPart = (CurrentDisk->SectorCount.QuadPart * CurrentDisk->BytesPerSector) - (1024 * 1024);
224 NULL,
225 NULL,
226 NULL,
228 SectorsBuffer,
229 1024 * 1024,
230 &Offset,
231 NULL);
232 if (!NT_SUCCESS(Status))
233 {
234 DPRINT1("Failed to write the last MB! (Status 0x%08lx)\n", Status);
236 goto done;
237 }
238 }
239
241
242done:
243 if (FileHandle != NULL)
245
246 if (SectorsBuffer != NULL)
247 RtlFreeHeap(RtlGetProcessHeap(), 0, SectorsBuffer);
248
249 return TRUE;
250}
static int argc
Definition: ServiceArgs.c:12
#define StdOut
Definition: fc.c:14
void ConResPuts(FILE *fp, UINT nID)
Definition: fc.c:27
LONG NTSTATUS
Definition: precomp.h:26
#define DPRINT1
Definition: precomp.h:8
#define IDS_CLEAN_FAIL
Definition: resource.h:26
#define IDS_SELECT_NO_DISK
Definition: resource.h:94
#define IDS_CLEAN_SUCCESS
Definition: resource.h:27
#define IDS_CLEAN_SYSTEM
Definition: resource.h:28
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:616
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:634
Definition: bufpool.h:45
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:36
LPWSTR Name
Definition: desk.c:124
VOID RemoveVolume(_In_ PVOLENTRY VolumeEntry)
Definition: partlist.c:2190
PDISKENTRY CurrentDisk
Definition: partlist.c:76
PVOLENTRY GetVolumeFromPartition(_In_ PPARTENTRY PartEntry)
Definition: partlist.c:2153
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
#define GENERIC_READ
Definition: compat.h:135
#define MAX_PATH
Definition: compat.h:34
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
#define L(x)
Definition: resources.c:13
#define IsListEmpty(ListHead)
Definition: env_spec_w32.h:954
#define RemoveHeadList(ListHead)
Definition: env_spec_w32.h:964
unsigned int BOOL
Definition: ntddk_ex.h:94
_Must_inspect_result_ _In_opt_ PFLT_INSTANCE _Out_ PHANDLE FileHandle
Definition: fltkernel.h:1231
#define FILE_SYNCHRONOUS_IO_NONALERT
Definition: from_kernel.h:31
Status
Definition: gdiplustypes.h:25
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
struct _PARTITION_INFORMATION_EX PARTITION_INFORMATION_EX
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define argv
Definition: mplay32.c:18
int Count
Definition: noreturn.cpp:7
NTSYSAPI NTSTATUS NTAPI NtOpenFile(OUT PHANDLE phFile, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes, OUT PIO_STATUS_BLOCK pIoStatusBlock, IN ULONG ShareMode, IN ULONG OpenMode)
Definition: file.c:3953
#define SYNCHRONIZE
Definition: nt_native.h:61
NTSYSAPI NTSTATUS NTAPI NtWriteFile(IN HANDLE hFile, IN HANDLE hEvent OPTIONAL, IN PIO_APC_ROUTINE IoApcRoutine OPTIONAL, IN PVOID IoApcContext OPTIONAL, OUT PIO_STATUS_BLOCK pIoStatusBlock, IN PVOID WriteBuffer, IN ULONG WriteBufferLength, IN PLARGE_INTEGER FileOffset OPTIONAL, IN PULONG LockOperationKey OPTIONAL)
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3402
#define GENERIC_WRITE
Definition: nt_native.h:90
#define IsContainerPartition(PartitionType)
Definition: ntdddisk.h:321
_In_ ULONG _In_ ULONG Offset
Definition: ntddpcm.h:101
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:75
_Check_return_ _CRTIMP int __cdecl _wcsicmp(_In_z_ const wchar_t *_Str1, _In_z_ const wchar_t *_Str2)
Definition: _wcsicmp_nt.c:13
#define DPRINT
Definition: sndvol32.h:73
STRSAFEAPI StringCchPrintfW(STRSAFE_LPWSTR pszDest, size_t cchDest, STRSAFE_LPCWSTR pszFormat,...)
Definition: strsafe.h:530
base of all file and directory entries
Definition: entries.h:83
ULONG BiosDiskNumber
Definition: diskpart.h:165
ULARGE_INTEGER SectorCount
Definition: partlist.h:115
PPARTENTRY ExtendedPartition
Definition: partlist.h:153
LIST_ENTRY LogicalPartListHead
Definition: partlist.h:150
BOOLEAN NewDisk
Definition: partlist.h:138
ULONG DiskNumber
Definition: partlist.h:129
ULONG BytesPerSector
Definition: partlist.h:113
BOOLEAN BiosFound
Definition: partlist.h:120
BOOLEAN Dirty
Definition: partlist.h:136
DWORD PartitionStyle
Definition: diskpart.h:179
LIST_ENTRY PrimaryPartListHead
Definition: partlist.h:149
PDRIVE_LAYOUT_INFORMATION LayoutBuffer
Definition: partlist.h:143
Definition: typedefs.h:120
UCHAR PartitionType
Definition: partlist.h:73
ULONGLONG QuadPart
Definition: ms-dtyp.idl:185
#define ANYSIZE_ARRAY
Definition: typedefs.h:46
int32_t INT
Definition: typedefs.h:58
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260
unsigned char * PUCHAR
Definition: typedefs.h:53
uint32_t ULONG
Definition: typedefs.h:59
LONGLONG QuadPart
Definition: typedefs.h:114
NTSTATUS DismountVolume(_Inout_ PVOLINFO Volume, _In_ BOOLEAN Force)
Attempts to dismount the designated volume.
Definition: volutil.c:152
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG Size
Definition: wdfdevice.h:4539
__wchar_t WCHAR
Definition: xmlstorage.h:180