ReactOS 0.4.15-dev-8236-g99f0937
parttest.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS partitions tests/dump
3 * LICENSE: LGPLv2+ - See COPYING.LIB in the top level directory
4 * PURPOSE: Open disk & partition trying to get information about volumes & MBR
5 * PROGRAMMER: Pierre Schweitzer <pierre@reactos.org>
6 */
7
8#include <stdio.h>
9#include <stdlib.h>
10#include <winternl.h>
11
12#ifndef NT_SUCCESS
13# define NT_SUCCESS(_Status) (((NTSTATUS)(_Status)) >= 0)
14#endif
15
16#define SECTOR_SIZE 512
17#define BOOT_RECORD_SIGNATURE 0xAA55
18
19PCWSTR DiskFormat = L"\\Device\\Harddisk%lu\\Partition%lu";
20
21#include <pshpack1.h>
22typedef struct {
23 unsigned char magic0, res0, magic1;
24 unsigned char OEMName[8];
25 unsigned short BytesPerSector;
26 unsigned char SectorsPerCluster;
27 unsigned short ReservedSectors;
28 unsigned char FATCount;
29 unsigned short RootEntries, Sectors;
30 unsigned char Media;
31 unsigned short FATSectors, SectorsPerTrack, Heads;
32 unsigned long HiddenSectors, SectorsHuge;
33 unsigned long FATSectors32;
34 unsigned short ExtFlag;
35 unsigned short FSVersion;
36 unsigned long RootCluster;
37 unsigned short FSInfoSector;
38 unsigned short BootBackup;
39 unsigned char Res3[12];
40 unsigned char Drive;
41 unsigned char Res4;
42 unsigned char ExtBootSignature;
43 unsigned long VolumeID;
44 unsigned char VolumeLabel[11], SysType[8];
45 unsigned char Res2[420];
46 unsigned short Signature1;
48
49typedef struct {
50 UCHAR Jump[3];
51 UCHAR OEMID[8];
54 UCHAR Unused0[7];
56 UCHAR Unused1[2];
59 UCHAR Unused2[4];
60 UCHAR Unused3[4];
66 UCHAR Unused4[3];
68 UCHAR Unused5[3];
70 UCHAR Checksum[4];
71 UCHAR BootStrap[426];
74
75typedef struct {
87
88typedef struct {
89 UCHAR MasterBootRecordCodeAndData[0x1B8];
92 PARTITION_TABLE_ENTRY PartitionTable[4];
95#include <poppack.h>
96
98{
99 if (Sector->Signature1 != 0xaa55)
100 {
101 return FALSE;
102 }
103
104 if (Sector->BytesPerSector != 512 &&
105 Sector->BytesPerSector != 1024 &&
106 Sector->BytesPerSector != 2048 &&
107 Sector->BytesPerSector != 4096)
108 {
109 return FALSE;
110 }
111
112 if (Sector->FATCount != 1 &&
113 Sector->FATCount != 2)
114 {
115 return FALSE;
116 }
117
118 if (Sector->Media != 0xf0 &&
119 Sector->Media != 0xf8 &&
120 Sector->Media != 0xf9 &&
121 Sector->Media != 0xfa &&
122 Sector->Media != 0xfb &&
123 Sector->Media != 0xfc &&
124 Sector->Media != 0xfd &&
125 Sector->Media != 0xfe &&
126 Sector->Media != 0xff)
127 {
128 return FALSE;
129 }
130
131 if (Sector->SectorsPerCluster != 1 &&
132 Sector->SectorsPerCluster != 2 &&
133 Sector->SectorsPerCluster != 4 &&
134 Sector->SectorsPerCluster != 8 &&
135 Sector->SectorsPerCluster != 16 &&
136 Sector->SectorsPerCluster != 32 &&
137 Sector->SectorsPerCluster != 64 &&
138 Sector->SectorsPerCluster != 128)
139 {
140 return FALSE;
141 }
142
143 if (Sector->BytesPerSector * Sector->SectorsPerCluster > 32 * 1024)
144 {
145 return FALSE;
146 }
147
148 return TRUE;
149}
150
152{
153 ULONG k;
155
156 /* OEMID: this field must be NTFS */
157 if (RtlCompareMemory(Sector->OEMID, "NTFS ", 8) != 8)
158 {
159 return FALSE;
160 }
161
162 /* Unused0: this field must be COMPLETELY null */
163 for (k = 0; k < 7; k++)
164 {
165 if (Sector->Unused0[k] != 0)
166 {
167 return FALSE;
168 }
169 }
170
171 /* Unused3: this field must be COMPLETELY null */
172 for (k = 0; k < 4; k++)
173 {
174 if (Sector->Unused3[k] != 0)
175 {
176 return FALSE;
177 }
178 }
179
180 /* Check cluster size */
181 ClusterSize = Sector->BytesPerSector * Sector->SectorsPerCluster;
182 if (ClusterSize != 512 && ClusterSize != 1024 &&
183 ClusterSize != 2048 && ClusterSize != 4096 &&
184 ClusterSize != 8192 && ClusterSize != 16384 &&
185 ClusterSize != 32768 && ClusterSize != 65536)
186 {
187 return FALSE;
188 }
189
190 return TRUE;
191}
192
194{
196 {
197 return FALSE;
198 }
199
200 return TRUE;
201}
202
203int main(int argc, char ** argv)
204{
211 PVOID Sector;
212
213 Sector = malloc(SECTOR_SIZE);
214 if (Sector == NULL)
215 {
216 fprintf(stderr, "Failed allocating memory!\n");
217 return 0;
218 }
219
220 /* We first open disk */
221 swprintf(Buffer, DiskFormat, 0, 0);
224 &Name,
226 NULL,
227 NULL);
228
233 0,
235 if (!NT_SUCCESS(Status))
236 {
237 free(Sector);
238 fprintf(stderr, "Failed opening disk! %lx\n", Status);
239 return 0;
240 }
241
242 /* Read first sector of the disk */
244 NULL,
245 NULL,
246 NULL,
248 Sector,
250 NULL,
251 NULL);
253 if (!NT_SUCCESS(Status))
254 {
255 free(Sector);
256 fprintf(stderr, "Failed reading sector 0! %lx\n", Status);
257 return 0;
258 }
259
260 /* Is it FAT? */
261 if (CheckAgainstFAT(Sector))
262 {
263 printf("Sector 0 seems to be FAT boot sector\n");
264 }
265 /* Is it NTFS? */
266 else if (CheckAgainstNTFS(Sector))
267 {
268 printf("Sector 0 seems to be NTFS boot sector\n");
269 }
270 /* Is it MBR? */
271 else if (CheckAgainstMBR(Sector))
272 {
273 printf("Sector 0 might be MBR\n");
274 }
275 /* We don't support anything else */
276 else
277 {
278 printf("Sector 0 not recognized\n");
279 }
280
281 /* Redo it with first partition */
282 swprintf(Buffer, DiskFormat, 0, 1);
285 &Name,
287 NULL,
288 NULL);
289
296 if (!NT_SUCCESS(Status))
297 {
298 free(Sector);
299 fprintf(stderr, "Failed opening partition! %lx\n", Status);
300 return 0;
301 }
302
303 /* Read first sector of the partition */
305 NULL,
306 NULL,
307 NULL,
309 Sector,
311 NULL,
312 NULL);
313 if (!NT_SUCCESS(Status))
314 {
315 free(Sector);
316 fprintf(stderr, "Failed reading first sector of the partition! %lx\n", Status);
317 return 0;
318 }
319
320 /* Is it FAT? */
321 if (CheckAgainstFAT(Sector))
322 {
323 printf("Seems to be a FAT partittion\n");
324 }
325 /* Is it NTFS? */
326 else if (CheckAgainstNTFS(Sector))
327 {
328 printf("Seems to be a NTFS partition\n");
329 }
330 /* Is it MBR? */
331 else if (CheckAgainstMBR(Sector))
332 {
333 printf("Seems to be MBR\n");
334 }
335 /* We don't support anything else */
336 else
337 {
338 printf("Not recognized\n");
339 }
340
341 free(Sector);
342
343 return 0;
344}
static int argc
Definition: ServiceArgs.c:12
struct NameRec_ * Name
Definition: cdprocs.h:460
LONG NTSTATUS
Definition: precomp.h:26
DWORD ClusterSize
Definition: format.c:67
Definition: bufpool.h:45
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:36
#define free
Definition: debug_ros.c:5
#define malloc
Definition: debug_ros.c:4
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define GENERIC_READ
Definition: compat.h:135
#define MAX_PATH
Definition: compat.h:34
#define FILE_SHARE_READ
Definition: compat.h:136
#define swprintf
Definition: precomp.h:40
int main()
Definition: test.c:6
#define RtlCompareMemory(s1, s2, l)
Definition: env_spec_w32.h:465
unsigned int BOOL
Definition: ntddk_ex.h:94
_Must_inspect_result_ _In_opt_ PFLT_INSTANCE _Out_ PHANDLE FileHandle
Definition: fltkernel.h:1231
#define printf
Definition: freeldr.h:97
#define FILE_SYNCHRONOUS_IO_NONALERT
Definition: from_kernel.h:31
Status
Definition: gdiplustypes.h:25
@ Unknown
Definition: i8042prt.h:114
#define stderr
Definition: stdio.h:100
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:75
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
int k
Definition: mpi.c:3369
#define argv
Definition: mplay32.c:18
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:3952
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
#define SYNCHRONIZE
Definition: nt_native.h:61
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define FILE_SHARE_DELETE
Definition: nt_native.h:682
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3402
#define L(x)
Definition: ntvdm.h:50
BOOL CheckAgainstFAT(PFATBootSector Sector)
Definition: parttest.c:97
BOOL CheckAgainstMBR(PMASTER_BOOT_RECORD Sector)
Definition: parttest.c:193
PCWSTR DiskFormat
Definition: parttest.c:19
#define NT_SUCCESS(_Status)
Definition: parttest.c:13
struct MASTER_BOOT_RECORD * PMASTER_BOOT_RECORD
struct PARTITION_TABLE_ENTRY * PPARTITION_TABLE_ENTRY
BOOL CheckAgainstNTFS(PNTFSBootSector Sector)
Definition: parttest.c:151
#define SECTOR_SIZE
Definition: parttest.c:16
#define BOOT_RECORD_SIGNATURE
Definition: parttest.c:17
struct NTFSBootSector * PNTFSBootSector
struct FATBootSector * PFATBootSector
unsigned short USHORT
Definition: pedump.c:61
NTSTATUS NTAPI NtReadFile(HANDLE FileHandle, HANDLE Event, PIO_APC_ROUTINE ApcRoutine, PVOID ApcContext, PIO_STATUS_BLOCK IoStatusBlock, PVOID Buffer, ULONG Length, PLARGE_INTEGER ByteOffset, PULONG Key)
unsigned char Media
Definition: parttest.c:30
unsigned short ReservedSectors
Definition: parttest.c:27
unsigned char ExtBootSignature
Definition: parttest.c:42
unsigned char magic0
Definition: parttest.c:23
unsigned short ExtFlag
Definition: parttest.c:34
unsigned short BytesPerSector
Definition: parttest.c:25
unsigned char Res4
Definition: parttest.c:41
unsigned char FATCount
Definition: parttest.c:28
unsigned char SectorsPerCluster
Definition: parttest.c:26
unsigned char Drive
Definition: parttest.c:40
unsigned short Signature1
Definition: parttest.c:46
unsigned short BootBackup
Definition: parttest.c:38
unsigned short RootEntries
Definition: parttest.c:29
unsigned short FSVersion
Definition: parttest.c:35
unsigned short FATSectors
Definition: parttest.c:31
unsigned long FATSectors32
Definition: parttest.c:33
unsigned long RootCluster
Definition: parttest.c:36
unsigned short FSInfoSector
Definition: parttest.c:37
unsigned long HiddenSectors
Definition: parttest.c:32
unsigned long VolumeID
Definition: parttest.c:43
USHORT MasterBootRecordMagic
Definition: parttest.c:93
USHORT Heads
Definition: parttest.c:58
USHORT EndSector
Definition: parttest.c:72
ULONGLONG SerialNumber
Definition: parttest.c:69
UCHAR SectorsPerCluster
Definition: parttest.c:53
USHORT SectorsPerTrack
Definition: parttest.c:57
UCHAR OEMID[8]
Definition: parttest.c:51
ULONGLONG MftLocation
Definition: parttest.c:63
CHAR ClustersPerMftRecord
Definition: parttest.c:65
ULONGLONG SectorCount
Definition: parttest.c:62
UCHAR Unused0[7]
Definition: parttest.c:54
UCHAR Unused3[4]
Definition: parttest.c:60
CHAR ClustersPerIndexRecord
Definition: parttest.c:67
ULONGLONG MftMirrLocation
Definition: parttest.c:64
UCHAR MediaId
Definition: parttest.c:55
USHORT BytesPerSector
Definition: parttest.c:52
Definition: parttest.c:75
UCHAR StartHead
Definition: parttest.c:77
UCHAR EndSector
Definition: parttest.c:82
UCHAR EndCylinder
Definition: parttest.c:83
ULONG PartitionSectorCount
Definition: parttest.c:85
UCHAR StartCylinder
Definition: parttest.c:79
UCHAR EndHead
Definition: parttest.c:81
ULONG SectorCountBeforePartition
Definition: parttest.c:84
UCHAR StartSector
Definition: parttest.c:78
UCHAR BootIndicator
Definition: parttest.c:76
UCHAR SystemIndicator
Definition: parttest.c:80
USHORT MasterBootRecordMagic
Definition: disk.h:78
const uint16_t * PCWSTR
Definition: typedefs.h:57
uint32_t ULONG
Definition: typedefs.h:59
uint64_t ULONGLONG
Definition: typedefs.h:67
_In_ ULONG _In_ ULONG SectorsPerTrack
Definition: iofuncs.h:2071
unsigned char UCHAR
Definition: xmlstorage.h:181
__wchar_t WCHAR
Definition: xmlstorage.h:180
char CHAR
Definition: xmlstorage.h:175