ReactOS 0.4.15-dev-7942-gd23573b
parttest.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <winternl.h>
#include <pshpack1.h>
#include <poppack.h>
Include dependency graph for parttest.c:

Go to the source code of this file.

Classes

struct  FATBootSector
 
struct  NTFSBootSector
 
struct  PARTITION_TABLE_ENTRY
 
struct  MASTER_BOOT_RECORD
 

Macros

#define NT_SUCCESS(_Status)   (((NTSTATUS)(_Status)) >= 0)
 
#define SECTOR_SIZE   512
 
#define BOOT_RECORD_SIGNATURE   0xAA55
 

Typedefs

typedef struct FATBootSectorPFATBootSector
 
typedef struct NTFSBootSectorPNTFSBootSector
 
typedef struct PARTITION_TABLE_ENTRYPPARTITION_TABLE_ENTRY
 
typedef struct MASTER_BOOT_RECORDPMASTER_BOOT_RECORD
 

Functions

BOOL CheckAgainstFAT (PFATBootSector Sector)
 
BOOL CheckAgainstNTFS (PNTFSBootSector Sector)
 
BOOL CheckAgainstMBR (PMASTER_BOOT_RECORD Sector)
 
int main (int argc, char **argv)
 

Variables

PCWSTR DiskFormat = L"\\Device\\Harddisk%lu\\Partition%lu"
 

Macro Definition Documentation

◆ BOOT_RECORD_SIGNATURE

#define BOOT_RECORD_SIGNATURE   0xAA55

Definition at line 17 of file parttest.c.

◆ NT_SUCCESS

#define NT_SUCCESS (   _Status)    (((NTSTATUS)(_Status)) >= 0)

Definition at line 13 of file parttest.c.

◆ SECTOR_SIZE

#define SECTOR_SIZE   512

Definition at line 16 of file parttest.c.

Typedef Documentation

◆ PFATBootSector

◆ PMASTER_BOOT_RECORD

◆ PNTFSBootSector

◆ PPARTITION_TABLE_ENTRY

Function Documentation

◆ CheckAgainstFAT()

BOOL CheckAgainstFAT ( PFATBootSector  Sector)

Definition at line 97 of file parttest.c.

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}
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
unsigned char Media
Definition: parttest.c:30
unsigned short BytesPerSector
Definition: parttest.c:25
unsigned char FATCount
Definition: parttest.c:28
unsigned char SectorsPerCluster
Definition: parttest.c:26
unsigned short Signature1
Definition: parttest.c:46

Referenced by main().

◆ CheckAgainstMBR()

BOOL CheckAgainstMBR ( PMASTER_BOOT_RECORD  Sector)

Definition at line 193 of file parttest.c.

194{
196 {
197 return FALSE;
198 }
199
200 return TRUE;
201}
#define BOOT_RECORD_SIGNATURE
Definition: parttest.c:17
USHORT MasterBootRecordMagic
Definition: disk.h:78

Referenced by main().

◆ CheckAgainstNTFS()

BOOL CheckAgainstNTFS ( PNTFSBootSector  Sector)

Definition at line 151 of file parttest.c.

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}
DWORD ClusterSize
Definition: format.c:67
#define RtlCompareMemory(s1, s2, l)
Definition: env_spec_w32.h:465
int k
Definition: mpi.c:3369
UCHAR SectorsPerCluster
Definition: parttest.c:53
UCHAR OEMID[8]
Definition: parttest.c:51
UCHAR Unused0[7]
Definition: parttest.c:54
UCHAR Unused3[4]
Definition: parttest.c:60
USHORT BytesPerSector
Definition: parttest.c:52
uint32_t ULONG
Definition: typedefs.h:59

Referenced by main().

◆ main()

int main ( int argc  ,
char **  argv 
)

Definition at line 203 of file parttest.c.

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}
struct NameRec_ * Name
Definition: cdprocs.h:460
LONG NTSTATUS
Definition: precomp.h:26
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 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
_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
#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
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
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
BOOL CheckAgainstNTFS(PNTFSBootSector Sector)
Definition: parttest.c:151
#define SECTOR_SIZE
Definition: parttest.c:16
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)
__wchar_t WCHAR
Definition: xmlstorage.h:180

Variable Documentation

◆ DiskFormat

PCWSTR DiskFormat = L"\\Device\\Harddisk%lu\\Partition%lu"

Definition at line 19 of file parttest.c.

Referenced by main().