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