stop the abuse of having the ddk directory in the path when including a ddk header
[reactos.git] / reactos / apps / utils / partinfo / partinfo.c
1 /*
2 * partinfo - partition info program
3 */
4
5 #include <windows.h>
6 //#include <winioctl.h>
7 #include <string.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10
11 #include <ntddk.h>
12
13 //#define DUMP_DATA
14 #define DUMP_SIZE_INFO
15 #define UNICODE
16
17 #ifdef DUMP_DATA
18 void HexDump(char *buffer, ULONG size)
19 {
20 ULONG offset = 0;
21 unsigned char *ptr;
22
23 while (offset < (size & ~15))
24 {
25 ptr = (unsigned char*)((ULONG)buffer + offset);
26 printf("%08lx %02hx %02hx %02hx %02hx %02hx %02hx %02hx %02hx-%02hx %02hx %02hx %02hx %02hx %02hx %02hx %02hx\n",
27 offset,
28 ptr[0],
29 ptr[1],
30 ptr[2],
31 ptr[3],
32 ptr[4],
33 ptr[5],
34 ptr[6],
35 ptr[7],
36 ptr[8],
37 ptr[9],
38 ptr[10],
39 ptr[11],
40 ptr[12],
41 ptr[13],
42 ptr[14],
43 ptr[15]);
44 offset += 16;
45 }
46
47 ptr = (unsigned char*)((ULONG)buffer + offset);
48 printf("%08lx ", offset);
49 while (offset < size)
50 {
51 printf(" %02hx", *ptr);
52 offset++;
53 ptr++;
54 }
55
56 printf("\n\n\n");
57 }
58 #endif
59
60
61 void Usage(void)
62 {
63 puts("Usage: partinfo <drive number>");
64 }
65
66
67 int main (int argc, char *argv[])
68 {
69 HANDLE hDisk;
70 DWORD dwRead;
71 DWORD i;
72 char *Buffer;
73 DRIVE_LAYOUT_INFORMATION *LayoutBuffer;
74 DISK_GEOMETRY DiskGeometry;
75 ULONG ulDrive;
76 CHAR DriveName[40];
77 SYSTEM_DEVICE_INFORMATION DeviceInfo;
78 NTSTATUS Status;
79
80 if (argc != 2)
81 {
82 Usage();
83 return(0);
84 }
85
86 ulDrive = strtoul(argv[1], NULL, 10);
87 if (errno != 0)
88 {
89 printf("Error: Malformed drive number\n");
90 return(0);
91 }
92
93 /* Check drive number */
94 Status = NtQuerySystemInformation(SystemDeviceInformation,
95 &DeviceInfo,
96 sizeof(SYSTEM_DEVICE_INFORMATION),
97 &i);
98 if (!NT_SUCCESS(Status))
99 {
100 printf("NtQuerySystemInformation() failed (Status %lx)\n", Status);
101 return(0);
102 }
103
104 if (DeviceInfo.NumberOfDisks == 0)
105 {
106 printf("No disk drive installed!\n");
107 return(0);
108 }
109
110 if (ulDrive >= DeviceInfo.NumberOfDisks)
111 {
112 printf("Invalid disk drive number! Valid drive numbers [0-%lu]\n",
113 DeviceInfo.NumberOfDisks-1);
114 return(0);
115 }
116
117 /* Build full drive name */
118 sprintf(DriveName, "\\\\.\\PHYSICALDRIVE%lu", ulDrive);
119
120 /* Open drive */
121 hDisk = CreateFile(DriveName,
122 GENERIC_READ,
123 FILE_SHARE_READ | FILE_SHARE_WRITE,
124 NULL,
125 OPEN_EXISTING,
126 0,
127 NULL);
128 if (hDisk == INVALID_HANDLE_VALUE)
129 {
130 printf("Invalid disk handle!");
131 return 0;
132 }
133
134 /* Get drive geometry */
135 if (!DeviceIoControl(hDisk,
136 IOCTL_DISK_GET_DRIVE_GEOMETRY,
137 NULL,
138 0,
139 &DiskGeometry,
140 sizeof(DISK_GEOMETRY),
141 &dwRead,
142 NULL))
143 {
144 CloseHandle(hDisk);
145 printf("DeviceIoControl failed! Error: %lu\n",
146 GetLastError());
147 return 0;
148 }
149
150 #ifdef DUMP_DATA
151 HexDump((char*)&DiskGeometry, dwRead);
152 #endif
153 printf("Drive number: %lu\n", ulDrive);
154 printf("Cylinders: %I64u\nMediaType: %x\nTracksPerCylinder: %lu\n"
155 "SectorsPerTrack: %lu\nBytesPerSector: %lu\n\n",
156 DiskGeometry.Cylinders.QuadPart,
157 DiskGeometry.MediaType,
158 DiskGeometry.TracksPerCylinder,
159 DiskGeometry.SectorsPerTrack,
160 DiskGeometry.BytesPerSector);
161
162
163 Buffer = (char*)malloc(8192);
164 if (Buffer == NULL)
165 {
166 CloseHandle(hDisk);
167 printf("Out of memory!");
168 return 0;
169 }
170 memset(Buffer, 0, 8192);
171
172 if (!DeviceIoControl(hDisk,
173 IOCTL_DISK_GET_DRIVE_LAYOUT,
174 NULL,
175 0,
176 Buffer,
177 8192,
178 &dwRead,
179 NULL))
180 {
181 CloseHandle(hDisk);
182 printf("DeviceIoControl(IOCTL_DISK_GET_DRIVE_LAYOUT) failed! Error: %lu\n",
183 GetLastError());
184 free(Buffer);
185 return 0;
186 }
187
188 CloseHandle(hDisk);
189
190 #ifdef DUMP_DATA
191 HexDump(Buffer, dwRead);
192 #endif
193
194 LayoutBuffer = (DRIVE_LAYOUT_INFORMATION*)Buffer;
195
196 printf("Partitions %lu Signature %lx\n",
197 LayoutBuffer->PartitionCount,
198 LayoutBuffer->Signature);
199
200 for (i = 0; i < LayoutBuffer->PartitionCount; i++)
201 {
202 printf(" %ld: nr: %ld boot: %1x type: %x start: 0x%I64x count: 0x%I64x\n",
203 i,
204 LayoutBuffer->PartitionEntry[i].PartitionNumber,
205 LayoutBuffer->PartitionEntry[i].BootIndicator,
206 LayoutBuffer->PartitionEntry[i].PartitionType,
207 LayoutBuffer->PartitionEntry[i].StartingOffset.QuadPart,
208 LayoutBuffer->PartitionEntry[i].PartitionLength.QuadPart);
209 }
210
211 free(Buffer);
212
213 return 0;
214 }