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>
13 # define NT_SUCCESS(_Status) (((NTSTATUS)(_Status)) >= 0)
16 #define SECTOR_SIZE 512
17 #define BOOT_RECORD_SIGNATURE 0xAA55
19 PCWSTR DiskFormat
= L
"\\Device\\Harddisk%lu\\Partition%lu";
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
;
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];
42 unsigned char ExtBootSignature
;
43 unsigned long VolumeID
;
44 unsigned char VolumeLabel
[11], SysType
[8];
45 unsigned char Res2
[420];
46 unsigned short Signature1
;
47 } FATBootSector
, *PFATBootSector
;
52 USHORT BytesPerSector
;
53 UCHAR SectorsPerCluster
;
57 USHORT SectorsPerTrack
;
62 ULONGLONG SectorCount
;
63 ULONGLONG MftLocation
;
64 ULONGLONG MftMirrLocation
;
65 CHAR ClustersPerMftRecord
;
67 CHAR ClustersPerIndexRecord
;
69 ULONGLONG SerialNumber
;
73 } NTFSBootSector
, *PNTFSBootSector
;
80 UCHAR SystemIndicator
;
84 ULONG SectorCountBeforePartition
;
85 ULONG PartitionSectorCount
;
86 } PARTITION_TABLE_ENTRY
, *PPARTITION_TABLE_ENTRY
;
89 UCHAR MasterBootRecordCodeAndData
[0x1B8];
92 PARTITION_TABLE_ENTRY PartitionTable
[4];
93 USHORT MasterBootRecordMagic
;
94 } MASTER_BOOT_RECORD
, *PMASTER_BOOT_RECORD
;
97 BOOL
CheckAgainstFAT(PFATBootSector Sector
)
99 if (Sector
->Signature1
!= 0xaa55)
104 if (Sector
->BytesPerSector
!= 512 &&
105 Sector
->BytesPerSector
!= 1024 &&
106 Sector
->BytesPerSector
!= 2048 &&
107 Sector
->BytesPerSector
!= 4096)
112 if (Sector
->FATCount
!= 1 &&
113 Sector
->FATCount
!= 2)
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)
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)
143 if (Sector
->BytesPerSector
* Sector
->SectorsPerCluster
> 32 * 1024)
151 BOOL
CheckAgainstNTFS(PNTFSBootSector Sector
)
156 /* OEMID: this field must be NTFS */
157 if (RtlCompareMemory(Sector
->OEMID
, "NTFS ", 8) != 8)
162 /* Unused0: this field must be COMPLETELY null */
163 for (k
= 0; k
< 7; k
++)
165 if (Sector
->Unused0
[k
] != 0)
171 /* Unused3: this field must be COMPLETELY null */
172 for (k
= 0; k
< 4; k
++)
174 if (Sector
->Unused3
[k
] != 0)
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)
193 BOOL
CheckAgainstMBR(PMASTER_BOOT_RECORD Sector
)
195 if (Sector
->MasterBootRecordMagic
!= BOOT_RECORD_SIGNATURE
)
203 int main(int argc
, char ** argv
)
207 OBJECT_ATTRIBUTES ObjectAttributes
;
208 IO_STATUS_BLOCK IoStatusBlock
;
209 WCHAR Buffer
[MAX_PATH
];
213 Sector
= malloc(SECTOR_SIZE
);
216 fprintf(stderr
, "Failed allocating memory!\n");
220 /* We first open disk */
221 swprintf(Buffer
, DiskFormat
, 0, 0);
222 RtlInitUnicodeString(&Name
, Buffer
);
223 InitializeObjectAttributes(&ObjectAttributes
,
225 OBJ_CASE_INSENSITIVE
,
229 Status
= NtOpenFile(&FileHandle
,
230 GENERIC_READ
| SYNCHRONIZE
,
234 FILE_SYNCHRONOUS_IO_NONALERT
);
235 if (!NT_SUCCESS(Status
))
238 fprintf(stderr
, "Failed opening disk! %lx\n", Status
);
242 /* Read first sector of the disk */
243 Status
= NtReadFile(FileHandle
,
253 if (!NT_SUCCESS(Status
))
256 fprintf(stderr
, "Failed reading sector 0! %lx\n", Status
);
261 if (CheckAgainstFAT(Sector
))
263 printf("Sector 0 seems to be FAT boot sector\n");
266 else if (CheckAgainstNTFS(Sector
))
268 printf("Sector 0 seems to be NTFS boot sector\n");
271 else if (CheckAgainstMBR(Sector
))
273 printf("Sector 0 might be MBR\n");
275 /* We don't support anything else */
278 printf("Sector 0 not recognized\n");
281 /* Redo it with first partition */
282 swprintf(Buffer
, DiskFormat
, 0, 1);
283 RtlInitUnicodeString(&Name
, Buffer
);
284 InitializeObjectAttributes(&ObjectAttributes
,
286 OBJ_CASE_INSENSITIVE
,
290 Status
= NtOpenFile(&FileHandle
,
291 GENERIC_READ
| SYNCHRONIZE
,
294 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
295 FILE_SYNCHRONOUS_IO_NONALERT
);
296 if (!NT_SUCCESS(Status
))
299 fprintf(stderr
, "Failed opening partition! %lx\n", Status
);
303 /* Read first sector of the partition */
304 Status
= NtReadFile(FileHandle
,
313 if (!NT_SUCCESS(Status
))
316 fprintf(stderr
, "Failed reading first sector of the partition! %lx\n", Status
);
321 if (CheckAgainstFAT(Sector
))
323 printf("Seems to be a FAT partittion\n");
326 else if (CheckAgainstNTFS(Sector
))
328 printf("Seems to be a NTFS partition\n");
331 else if (CheckAgainstMBR(Sector
))
333 printf("Seems to be MBR\n");
335 /* We don't support anything else */
338 printf("Not recognized\n");