1a2aaa284SEric Kohl /*
2a2aaa284SEric Kohl * PROJECT: ReactOS DiskPart
3a2aaa284SEric Kohl * LICENSE: GPL - See COPYING in the top level directory
4a2aaa284SEric Kohl * FILE: base/system/diskpart/dump.c
5a2aaa284SEric Kohl * PURPOSE: Manages all the partitions of the OS in an interactive way.
6a2aaa284SEric Kohl * PROGRAMMERS: Eric Kohl
7a2aaa284SEric Kohl */
8a2aaa284SEric Kohl
9a2aaa284SEric Kohl #include "diskpart.h"
10a2aaa284SEric Kohl
11a2aaa284SEric Kohl #define NDEBUG
12a2aaa284SEric Kohl #include <debug.h>
13a2aaa284SEric Kohl
14a2aaa284SEric Kohl /* FUNCTIONS ******************************************************************/
15a2aaa284SEric Kohl
16a2aaa284SEric Kohl static
17a2aaa284SEric Kohl VOID
HexDump(_In_ UCHAR * addr,_In_ int len)18a2aaa284SEric Kohl HexDump(
19a2aaa284SEric Kohl _In_ UCHAR *addr,
20a2aaa284SEric Kohl _In_ int len)
21a2aaa284SEric Kohl {
22a2aaa284SEric Kohl WCHAR Buffer[17];
23a2aaa284SEric Kohl UCHAR *pc;
24a2aaa284SEric Kohl int i;
25a2aaa284SEric Kohl
26a2aaa284SEric Kohl Buffer[16] = L'\0';
27a2aaa284SEric Kohl
28a2aaa284SEric Kohl pc = addr;
29a2aaa284SEric Kohl for (i = 0; i < len; i++)
30a2aaa284SEric Kohl {
31a2aaa284SEric Kohl if ((i % 16) == 0)
32a2aaa284SEric Kohl ConPrintf(StdOut, L" %04x ", i);
33a2aaa284SEric Kohl
34a2aaa284SEric Kohl ConPrintf(StdOut, L" %02x", pc[i]);
35a2aaa284SEric Kohl
36a2aaa284SEric Kohl if ((pc[i] < 0x20) || (pc[i] > 0x7e))
37a2aaa284SEric Kohl Buffer[i % 16] = L'.';
38a2aaa284SEric Kohl else
39a2aaa284SEric Kohl Buffer[i % 16] = (WCHAR)(USHORT)pc[i];
40a2aaa284SEric Kohl
41a2aaa284SEric Kohl if ((i % 16) == (16 - 1))
42a2aaa284SEric Kohl ConPrintf(StdOut, L" %s\n", Buffer);
43a2aaa284SEric Kohl }
44a2aaa284SEric Kohl }
45a2aaa284SEric Kohl
46a2aaa284SEric Kohl
47*417687e9SEric Kohl BOOL
DumpDisk(_In_ INT argc,_In_ LPWSTR * argv)48a2aaa284SEric Kohl DumpDisk(
49a2aaa284SEric Kohl _In_ INT argc,
50a2aaa284SEric Kohl _In_ LPWSTR *argv)
51a2aaa284SEric Kohl {
52a2aaa284SEric Kohl OBJECT_ATTRIBUTES ObjectAttributes;
53a2aaa284SEric Kohl IO_STATUS_BLOCK Iosb;
54a2aaa284SEric Kohl NTSTATUS Status;
55a2aaa284SEric Kohl WCHAR Buffer[MAX_PATH];
56a2aaa284SEric Kohl UNICODE_STRING Name;
57a2aaa284SEric Kohl HANDLE FileHandle = NULL;
58a2aaa284SEric Kohl PUCHAR pSectorBuffer = NULL;
59a2aaa284SEric Kohl LARGE_INTEGER FileOffset;
60a2aaa284SEric Kohl LONGLONG Sector;
61a2aaa284SEric Kohl LPWSTR endptr = NULL;
62a2aaa284SEric Kohl
63a2aaa284SEric Kohl #if 0
64a2aaa284SEric Kohl if (argc == 2)
65a2aaa284SEric Kohl {
66a2aaa284SEric Kohl ConResPuts(StdOut, IDS_HELP_CMD_DUMP_DISK);
67a2aaa284SEric Kohl return TRUE;
68a2aaa284SEric Kohl }
69a2aaa284SEric Kohl #endif
70a2aaa284SEric Kohl
71a2aaa284SEric Kohl if (CurrentDisk == NULL)
72a2aaa284SEric Kohl {
73a2aaa284SEric Kohl ConResPuts(StdOut, IDS_SELECT_NO_DISK);
74*417687e9SEric Kohl return TRUE;
75a2aaa284SEric Kohl }
76a2aaa284SEric Kohl
77a2aaa284SEric Kohl Sector = _wcstoi64(argv[2], &endptr, 0);
78a2aaa284SEric Kohl if (((Sector == 0) && (endptr == argv[2])) ||
79a2aaa284SEric Kohl (Sector < 0))
80a2aaa284SEric Kohl {
81a2aaa284SEric Kohl ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
82*417687e9SEric Kohl return TRUE;
83a2aaa284SEric Kohl }
84a2aaa284SEric Kohl
85a2aaa284SEric Kohl pSectorBuffer = RtlAllocateHeap(RtlGetProcessHeap(), HEAP_ZERO_MEMORY, CurrentDisk->BytesPerSector);
86a2aaa284SEric Kohl if (pSectorBuffer == NULL)
87a2aaa284SEric Kohl {
88a2aaa284SEric Kohl DPRINT1("\n");
89a2aaa284SEric Kohl /* Error message */
90a2aaa284SEric Kohl goto done;
91a2aaa284SEric Kohl }
92a2aaa284SEric Kohl
93a2aaa284SEric Kohl swprintf(Buffer,
94a2aaa284SEric Kohl L"\\Device\\Harddisk%d\\Partition0",
95a2aaa284SEric Kohl CurrentDisk->DiskNumber);
96a2aaa284SEric Kohl RtlInitUnicodeString(&Name,
97a2aaa284SEric Kohl Buffer);
98a2aaa284SEric Kohl
99a2aaa284SEric Kohl InitializeObjectAttributes(&ObjectAttributes,
100a2aaa284SEric Kohl &Name,
101a2aaa284SEric Kohl 0,
102a2aaa284SEric Kohl NULL,
103a2aaa284SEric Kohl NULL);
104a2aaa284SEric Kohl
105a2aaa284SEric Kohl Status = NtOpenFile(&FileHandle,
106a2aaa284SEric Kohl FILE_READ_DATA | FILE_READ_ATTRIBUTES | SYNCHRONIZE,
107a2aaa284SEric Kohl &ObjectAttributes,
108a2aaa284SEric Kohl &Iosb,
109a2aaa284SEric Kohl FILE_SHARE_READ,
110a2aaa284SEric Kohl FILE_SYNCHRONOUS_IO_NONALERT);
111a2aaa284SEric Kohl if (!NT_SUCCESS(Status))
112a2aaa284SEric Kohl {
113a2aaa284SEric Kohl DPRINT1("\n");
114a2aaa284SEric Kohl goto done;
115a2aaa284SEric Kohl }
116a2aaa284SEric Kohl
117a2aaa284SEric Kohl FileOffset.QuadPart = Sector * CurrentDisk->BytesPerSector;
118a2aaa284SEric Kohl Status = NtReadFile(FileHandle,
119a2aaa284SEric Kohl NULL,
120a2aaa284SEric Kohl NULL,
121a2aaa284SEric Kohl NULL,
122a2aaa284SEric Kohl &Iosb,
123a2aaa284SEric Kohl (PVOID)pSectorBuffer,
124a2aaa284SEric Kohl CurrentDisk->BytesPerSector,
125a2aaa284SEric Kohl &FileOffset,
126a2aaa284SEric Kohl NULL);
127a2aaa284SEric Kohl if (!NT_SUCCESS(Status))
128a2aaa284SEric Kohl {
129a2aaa284SEric Kohl DPRINT1("NtReadFile failed, status=%x\n", Status);
130a2aaa284SEric Kohl goto done;
131a2aaa284SEric Kohl }
132a2aaa284SEric Kohl
133a2aaa284SEric Kohl HexDump(pSectorBuffer, CurrentDisk->BytesPerSector);
134a2aaa284SEric Kohl
135a2aaa284SEric Kohl done:
136a2aaa284SEric Kohl if (FileHandle)
137a2aaa284SEric Kohl NtClose(FileHandle);
138a2aaa284SEric Kohl
139a2aaa284SEric Kohl RtlFreeHeap(RtlGetProcessHeap(), 0, pSectorBuffer);
140a2aaa284SEric Kohl
141*417687e9SEric Kohl return TRUE;
142a2aaa284SEric Kohl }
143a2aaa284SEric Kohl
144a2aaa284SEric Kohl
145*417687e9SEric Kohl BOOL
DumpPartition(_In_ INT argc,_In_ LPWSTR * argv)146a2aaa284SEric Kohl DumpPartition(
147a2aaa284SEric Kohl _In_ INT argc,
148a2aaa284SEric Kohl _In_ LPWSTR *argv)
149a2aaa284SEric Kohl {
150a2aaa284SEric Kohl OBJECT_ATTRIBUTES ObjectAttributes;
151a2aaa284SEric Kohl IO_STATUS_BLOCK Iosb;
152a2aaa284SEric Kohl NTSTATUS Status;
153a2aaa284SEric Kohl WCHAR Buffer[MAX_PATH];
154a2aaa284SEric Kohl UNICODE_STRING Name;
155a2aaa284SEric Kohl HANDLE FileHandle = NULL;
156a2aaa284SEric Kohl PUCHAR pSectorBuffer = NULL;
157a2aaa284SEric Kohl LARGE_INTEGER FileOffset;
158a2aaa284SEric Kohl LONGLONG Sector;
159a2aaa284SEric Kohl LPWSTR endptr = NULL;
160a2aaa284SEric Kohl
161a2aaa284SEric Kohl
162a2aaa284SEric Kohl #if 0
163a2aaa284SEric Kohl if (argc == 2)
164a2aaa284SEric Kohl {
165a2aaa284SEric Kohl ConResPuts(StdOut, IDS_HELP_CMD_DUMP_DISK);
166a2aaa284SEric Kohl return TRUE;
167a2aaa284SEric Kohl }
168a2aaa284SEric Kohl #endif
169a2aaa284SEric Kohl
170a2aaa284SEric Kohl if (CurrentDisk == NULL)
171a2aaa284SEric Kohl {
172a2aaa284SEric Kohl ConResPuts(StdOut, IDS_SELECT_NO_DISK);
173*417687e9SEric Kohl return TRUE;
174a2aaa284SEric Kohl }
175a2aaa284SEric Kohl
176a2aaa284SEric Kohl if (CurrentPartition == NULL)
177a2aaa284SEric Kohl {
178a2aaa284SEric Kohl ConResPuts(StdOut, IDS_SELECT_NO_PARTITION);
179*417687e9SEric Kohl return TRUE;
180a2aaa284SEric Kohl }
181a2aaa284SEric Kohl
182a2aaa284SEric Kohl Sector = _wcstoi64(argv[2], &endptr, 0);
183a2aaa284SEric Kohl if (((Sector == 0) && (endptr == argv[2])) ||
184a2aaa284SEric Kohl (Sector < 0))
185a2aaa284SEric Kohl {
186a2aaa284SEric Kohl ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
187*417687e9SEric Kohl return TRUE;
188a2aaa284SEric Kohl }
189a2aaa284SEric Kohl
190a2aaa284SEric Kohl pSectorBuffer = RtlAllocateHeap(RtlGetProcessHeap(), HEAP_ZERO_MEMORY, CurrentDisk->BytesPerSector);
191a2aaa284SEric Kohl if (pSectorBuffer == NULL)
192a2aaa284SEric Kohl {
193a2aaa284SEric Kohl DPRINT1("\n");
194a2aaa284SEric Kohl /* Error message */
195a2aaa284SEric Kohl goto done;
196a2aaa284SEric Kohl }
197a2aaa284SEric Kohl
198a2aaa284SEric Kohl swprintf(Buffer,
199a2aaa284SEric Kohl L"\\Device\\Harddisk%d\\Partition%d",
200a2aaa284SEric Kohl CurrentDisk->DiskNumber,
201a2aaa284SEric Kohl CurrentPartition->PartitionNumber);
202a2aaa284SEric Kohl RtlInitUnicodeString(&Name,
203a2aaa284SEric Kohl Buffer);
204a2aaa284SEric Kohl
205a2aaa284SEric Kohl InitializeObjectAttributes(&ObjectAttributes,
206a2aaa284SEric Kohl &Name,
207a2aaa284SEric Kohl 0,
208a2aaa284SEric Kohl NULL,
209a2aaa284SEric Kohl NULL);
210a2aaa284SEric Kohl
211a2aaa284SEric Kohl Status = NtOpenFile(&FileHandle,
212a2aaa284SEric Kohl FILE_READ_DATA | FILE_READ_ATTRIBUTES | SYNCHRONIZE,
213a2aaa284SEric Kohl &ObjectAttributes,
214a2aaa284SEric Kohl &Iosb,
215a2aaa284SEric Kohl FILE_SHARE_READ,
216a2aaa284SEric Kohl FILE_SYNCHRONOUS_IO_NONALERT);
217a2aaa284SEric Kohl if (!NT_SUCCESS(Status))
218a2aaa284SEric Kohl {
219a2aaa284SEric Kohl DPRINT1("\n");
220a2aaa284SEric Kohl goto done;
221a2aaa284SEric Kohl }
222a2aaa284SEric Kohl
223a2aaa284SEric Kohl FileOffset.QuadPart = Sector * CurrentDisk->BytesPerSector;
224a2aaa284SEric Kohl Status = NtReadFile(FileHandle,
225a2aaa284SEric Kohl NULL,
226a2aaa284SEric Kohl NULL,
227a2aaa284SEric Kohl NULL,
228a2aaa284SEric Kohl &Iosb,
229a2aaa284SEric Kohl (PVOID)pSectorBuffer,
230a2aaa284SEric Kohl CurrentDisk->BytesPerSector,
231a2aaa284SEric Kohl &FileOffset,
232a2aaa284SEric Kohl NULL);
233a2aaa284SEric Kohl if (!NT_SUCCESS(Status))
234a2aaa284SEric Kohl {
235a2aaa284SEric Kohl DPRINT1("NtReadFile failed, status=%x\n", Status);
236a2aaa284SEric Kohl goto done;
237a2aaa284SEric Kohl }
238a2aaa284SEric Kohl
239a2aaa284SEric Kohl HexDump(pSectorBuffer, CurrentDisk->BytesPerSector);
240a2aaa284SEric Kohl
241a2aaa284SEric Kohl done:
242a2aaa284SEric Kohl if (FileHandle)
243a2aaa284SEric Kohl NtClose(FileHandle);
244a2aaa284SEric Kohl
245a2aaa284SEric Kohl RtlFreeHeap(RtlGetProcessHeap(), 0, pSectorBuffer);
246a2aaa284SEric Kohl
247a2aaa284SEric Kohl return TRUE;
248a2aaa284SEric Kohl }
249