ReactOS  0.4.14-dev-614-gbfd8a84
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 
19 PCWSTR DiskFormat = L"\\Device\\Harddisk%lu\\Partition%lu";
20 
21 #include <pshpack1.h>
22 typedef 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 
49 typedef 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 
75 typedef struct {
87 
88 typedef 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 
203 int 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 
232  &IoStatusBlock,
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,
247  &IoStatusBlock,
248  Sector,
249  SECTOR_SIZE,
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 
293  &IoStatusBlock,
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,
308  &IoStatusBlock,
309  Sector,
310  SECTOR_SIZE,
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 }
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
static int argc
Definition: ServiceArgs.c:12
const uint16_t * PCWSTR
Definition: typedefs.h:55
USHORT MasterBootRecordMagic
Definition: disk.h:78
struct _MASTER_BOOT_RECORD MASTER_BOOT_RECORD
unsigned char Drive
Definition: parttest.c:40
#define TRUE
Definition: types.h:120
UCHAR Unused3[4]
Definition: parttest.c:60
UCHAR StartCylinder
Definition: parttest.c:79
USHORT EndSector
Definition: parttest.c:72
unsigned char FATCount
Definition: parttest.c:28
UCHAR StartSector
Definition: parttest.c:78
BOOL CheckAgainstNTFS(PNTFSBootSector Sector)
Definition: parttest.c:151
unsigned short ReservedSectors
Definition: parttest.c:27
UCHAR SectorsPerCluster
Definition: parttest.c:53
UCHAR EndHead
Definition: parttest.c:81
ULONG PartitionSectorCount
Definition: parttest.c:85
BOOL CheckAgainstMBR(PMASTER_BOOT_RECORD Sector)
Definition: parttest.c:193
char CHAR
Definition: xmlstorage.h:175
#define free
Definition: debug_ros.c:5
unsigned char Media
Definition: parttest.c:30
LONG NTSTATUS
Definition: precomp.h:26
ULONGLONG MftMirrLocation
Definition: parttest.c:64
unsigned char res0
Definition: parttest.c:23
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
#define argv
Definition: mplay32.c:18
unsigned char ExtBootSignature
Definition: parttest.c:42
ULONG SectorCountBeforePartition
Definition: parttest.c:84
#define FILE_SHARE_READ
Definition: compat.h:125
struct PARTITION_TABLE_ENTRY * PPARTITION_TABLE_ENTRY
UCHAR SystemIndicator
Definition: parttest.c:80
HANDLE FileHandle
Definition: stats.c:38
unsigned short FSInfoSector
Definition: parttest.c:37
unsigned short Sectors
Definition: parttest.c:29
unsigned int BOOL
Definition: ntddk_ex.h:94
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
USHORT SectorsPerTrack
Definition: parttest.c:57
struct NameRec_ * Name
Definition: cdprocs.h:464
int main(int argc, char **argv)
Definition: parttest.c:203
smooth NULL
Definition: ftsmooth.c:416
Definition: bufpool.h:45
CHAR ClustersPerMftRecord
Definition: parttest.c:65
unsigned long VolumeID
Definition: parttest.c:43
#define SECTOR_SIZE
Definition: parttest.c:16
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
UCHAR Unused0[7]
Definition: parttest.c:54
UCHAR BootIndicator
Definition: parttest.c:76
#define BOOT_RECORD_SIGNATURE
Definition: parttest.c:17
__wchar_t WCHAR
Definition: xmlstorage.h:180
ULONGLONG MftLocation
Definition: parttest.c:63
UCHAR StartHead
Definition: parttest.c:77
uint64_t ULONGLONG
Definition: typedefs.h:65
#define MAX_PATH
Definition: compat.h:26
#define swprintf(buf, format,...)
Definition: sprintf.c:56
DWORD ClusterSize
Definition: format.c:67
UCHAR MediaId
Definition: parttest.c:55
Definition: parttest.c:75
UCHAR EndCylinder
Definition: parttest.c:83
unsigned short FSVersion
Definition: parttest.c:35
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3399
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
struct FATBootSector * PFATBootSector
unsigned char UCHAR
Definition: xmlstorage.h:181
#define FILE_SHARE_DELETE
Definition: nt_native.h:682
static const WCHAR L[]
Definition: oid.c:1250
unsigned short SectorsPerTrack
Definition: parttest.c:31
#define GENERIC_READ
Definition: compat.h:124
unsigned short ExtFlag
Definition: parttest.c:34
#define SYNCHRONIZE
Definition: nt_native.h:61
unsigned short BytesPerSector
Definition: parttest.c:25
Status
Definition: gdiplustypes.h:24
struct NTFSBootSector * PNTFSBootSector
unsigned char Res4
Definition: parttest.c:41
unsigned long FATSectors32
Definition: parttest.c:33
USHORT BytesPerSector
Definition: parttest.c:52
CHAR ClustersPerIndexRecord
Definition: parttest.c:67
unsigned char SectorsPerCluster
Definition: parttest.c:26
USHORT MasterBootRecordMagic
Definition: parttest.c:93
unsigned short USHORT
Definition: pedump.c:61
unsigned long SectorsHuge
Definition: parttest.c:32
ULONGLONG SerialNumber
Definition: parttest.c:69
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:75
unsigned short BootBackup
Definition: parttest.c:38
#define FILE_SYNCHRONOUS_IO_NONALERT
Definition: from_kernel.h:31
#define NT_SUCCESS(_Status)
Definition: parttest.c:13
struct MASTER_BOOT_RECORD * PMASTER_BOOT_RECORD
UCHAR OEMID[8]
Definition: parttest.c:51
unsigned int ULONG
Definition: retypes.h:1
FILE * stderr
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
ULONGLONG SectorCount
Definition: parttest.c:62
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define malloc
Definition: debug_ros.c:4
PCWSTR DiskFormat
Definition: parttest.c:19
UCHAR EndSector
Definition: parttest.c:82
BOOL CheckAgainstFAT(PFATBootSector Sector)
Definition: parttest.c:97
int k
Definition: mpi.c:3369
unsigned short Signature1
Definition: parttest.c:46
#define RtlCompareMemory(s1, s2, l)
Definition: env_spec_w32.h:465
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)
struct _PARTITION_TABLE_ENTRY PARTITION_TABLE_ENTRY
USHORT Heads
Definition: parttest.c:58
#define printf
Definition: config.h:203
unsigned long RootCluster
Definition: parttest.c:36