ReactOS  0.4.15-dev-4927-gfe8f806
clean.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS DiskPart
3  * LICENSE: GPL - See COPYING in the top level directory
4  * FILE: base/system/diskpart/clean.c
5  * PURPOSE: Manages all the partitions of the OS in an interactive way.
6  * PROGRAMMERS: Lee Schroeder
7  */
8 
9 #include "diskpart.h"
10 
11 #define NDEBUG
12 #include <debug.h>
13 
14 
15 BOOL
17  _In_ INT argc,
18  _In_ PWSTR *argv)
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) &&
86  (IsContainerPartition(PartEntry->PartitionType) == FALSE))
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 */
103 
104  /* Wipe the layout buffer */
105  RtlFreeHeap(RtlGetProcessHeap(), 0, CurrentDisk->LayoutBuffer);
106 
107  LayoutBufferSize = sizeof(DRIVE_LAYOUT_INFORMATION) +
108  ((4 - ANYSIZE_ARRAY) * sizeof(PARTITION_INFORMATION));
109  CurrentDisk->LayoutBuffer = RtlAllocateHeap(RtlGetProcessHeap(),
111  LayoutBufferSize);
112  if (CurrentDisk->LayoutBuffer == NULL)
113  {
114  DPRINT1("Failed to allocate the disk layout buffer!\n");
115  return TRUE;
116  }
117 
118  /* Allocate a 1MB sectors buffer */
119  SectorsBuffer = RtlAllocateHeap(RtlGetProcessHeap(),
121  1024 * 1024);
122  if (SectorsBuffer == NULL)
123  {
124  DPRINT1("Failed to allocate the sectors buffer!\n");
125  goto done;
126  }
127 
128  /* Open the disk for writing */
130  L"\\Device\\Harddisk%d\\Partition0",
132 
134 
136  &Name,
138  NULL,
139  NULL);
140 
144  &IoStatusBlock,
145  0,
147  if (!NT_SUCCESS(Status))
148  {
149  DPRINT1("Failed to open the disk! (Status 0x%08lx)\n", Status);
151  goto done;
152  }
153 
154  /* Clean sectors */
155  if (bAll)
156  {
157  MaxCount.QuadPart = (CurrentDisk->SectorCount.QuadPart * CurrentDisk->BytesPerSector) / (1024 * 1024);
158  for (Count.QuadPart = 0; Count.QuadPart < MaxCount.QuadPart; Count.QuadPart++)
159  {
160  Offset.QuadPart = Count.QuadPart * (1024 * 1024);
162  NULL,
163  NULL,
164  NULL,
165  &IoStatusBlock,
166  SectorsBuffer,
167  1024 * 1024,
168  &Offset,
169  NULL);
170  if (!NT_SUCCESS(Status))
171  {
172  DPRINT1("Failed to write MB! (Status 0x%08lx)\n", Status);
174  goto done;
175  }
176  }
177 
179  if (Size != 0)
180  {
181  Offset.QuadPart += (1024 * 1024);
183  NULL,
184  NULL,
185  NULL,
186  &IoStatusBlock,
187  SectorsBuffer,
188  Size,
189  &Offset,
190  NULL);
191  if (!NT_SUCCESS(Status))
192  {
193  DPRINT1("Failed to write the last part! (Status 0x%08lx)\n", Status);
195  goto done;
196  }
197  }
198  }
199  else
200  {
201  /* Clean the first MB */
202  Offset.QuadPart = 0;
204  NULL,
205  NULL,
206  NULL,
207  &IoStatusBlock,
208  SectorsBuffer,
209  1024 * 1024,
210  &Offset,
211  NULL);
212  if (!NT_SUCCESS(Status))
213  {
214  DPRINT1("Failed to write the first MB! (Status 0x%08lx)\n", Status);
216  goto done;
217  }
218 
219  /* Clean the last MB */
220  Offset.QuadPart = (CurrentDisk->SectorCount.QuadPart * CurrentDisk->BytesPerSector) - (1024 * 1024);
222  NULL,
223  NULL,
224  NULL,
225  &IoStatusBlock,
226  SectorsBuffer,
227  1024 * 1024,
228  &Offset,
229  NULL);
230  if (!NT_SUCCESS(Status))
231  {
232  DPRINT1("Failed to write the last MB! (Status 0x%08lx)\n", Status);
234  goto done;
235  }
236  }
237 
239 
240 done:
241  if (FileHandle != NULL)
243 
244  if (SectorsBuffer != NULL)
245  RtlFreeHeap(RtlGetProcessHeap(), 0, SectorsBuffer);
246 
247  return TRUE;
248 }
LIST_ENTRY PrimaryPartListHead
Definition: partlist.h:131
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
static int argc
Definition: ServiceArgs.c:12
struct _Entry Entry
Definition: kefuncs.h:629
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
#define ANYSIZE_ARRAY
Definition: typedefs.h:46
#define TRUE
Definition: types.h:120
uint16_t * PWSTR
Definition: typedefs.h:56
unsigned char * PUCHAR
Definition: retypes.h:3
LONG NTSTATUS
Definition: precomp.h:26
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:606
#define IDS_CLEAN_FAIL
Definition: resource.h:25
VOID RemoveVolume(_In_ PVOLENTRY VolumeEntry)
Definition: partlist.c:2103
ULONG DiskNumber
Definition: partlist.h:111
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:361
PDISKENTRY CurrentDisk
Definition: partlist.c:74
#define IsContainerPartition(PartitionType)
Definition: ntdddisk.h:316
BOOL clean_main(_In_ INT argc, _In_ PWSTR *argv)
Definition: clean.c:16
#define argv
Definition: mplay32.c:18
struct _PARTITION_INFORMATION PARTITION_INFORMATION
int32_t INT
Definition: typedefs.h:58
_Must_inspect_result_ FORCEINLINE BOOLEAN IsListEmpty(_In_ const LIST_ENTRY *ListHead)
Definition: rtlfuncs.h:57
PPARTENTRY ExtendedPartition
Definition: partlist.h:135
NTSTATUS DismountVolume(IN PPARTENTRY PartEntry)
Definition: partlist.c:2940
#define L(x)
Definition: ntvdm.h:50
ULONGLONG QuadPart
Definition: ms-dtyp.idl:185
#define FALSE
Definition: types.h:117
unsigned int BOOL
Definition: ntddk_ex.h:94
#define IDS_CLEAN_SYSTEM
Definition: resource.h:27
PVOLENTRY GetVolumeFromPartition(_In_ PPARTENTRY PartEntry)
Definition: partlist.c:2066
#define GENERIC_WRITE
Definition: nt_native.h:90
struct _DRIVE_LAYOUT_INFORMATION DRIVE_LAYOUT_INFORMATION
BOOLEAN Dirty
Definition: partlist.h:118
struct NameRec_ * Name
Definition: cdprocs.h:459
#define _In_
Definition: ms_sal.h:308
FORCEINLINE PLIST_ENTRY RemoveHeadList(_Inout_ PLIST_ENTRY ListHead)
Definition: rtlfuncs.h:128
Definition: bufpool.h:45
void ConResPuts(FILE *fp, UINT nID)
Definition: fc.c:27
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
ULARGE_INTEGER SectorCount
Definition: partlist.h:97
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:588
Status
Definition: gdiplustypes.h:24
#define IDS_CLEAN_SUCCESS
Definition: resource.h:26
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:3951
int Count
Definition: noreturn.cpp:7
UCHAR PartitionType
Definition: partlist.h:53
BOOLEAN BiosFound
Definition: partlist.h:102
ULONG BytesPerSector
Definition: partlist.h:95
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define MAX_PATH
Definition: compat.h:34
#define IDS_SELECT_NO_DISK
Definition: resource.h:85
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3402
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)
ULONG BiosDiskNumber
Definition: diskpart.h:160
#define GENERIC_READ
Definition: compat.h:135
Definition: typedefs.h:119
#define SYNCHRONIZE
Definition: nt_native.h:61
_In_ ULONG _In_ ULONG Offset
Definition: ntddpcm.h:101
_Must_inspect_result_ _In_opt_ PFLT_INSTANCE _Out_ PHANDLE FileHandle
Definition: fltkernel.h:1230
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
STRSAFEAPI StringCchPrintfW(STRSAFE_LPWSTR pszDest, size_t cchDest, STRSAFE_LPCWSTR pszFormat,...)
Definition: strsafe.h:530
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:75
#define NULL
Definition: types.h:112
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
#define DPRINT1
Definition: precomp.h:8
LIST_ENTRY LogicalPartListHead
Definition: partlist.h:132
#define FILE_SYNCHRONOUS_IO_NONALERT
Definition: from_kernel.h:31
#define StdOut
Definition: fc.c:14
unsigned int ULONG
Definition: retypes.h:1
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define DPRINT
Definition: sndvol32.h:71
PDRIVE_LAYOUT_INFORMATION LayoutBuffer
Definition: partlist.h:125
BOOLEAN NewDisk
Definition: partlist.h:120
base of all file and directory entries
Definition: entries.h:82
_Check_return_ _CRTIMP int __cdecl _wcsicmp(_In_z_ const wchar_t *_Str1, _In_z_ const wchar_t *_Str2)
BOOLEAN NoMbr
Definition: diskpart.h:174
LONGLONG QuadPart
Definition: typedefs.h:114