1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
#include "physicaldisk.h"
#include "common/io.h"
#include "common/windows/unicode.h"
#include "common/mallocHelper.h"
#include "common/debug.h"
#include <stdalign.h>
#include <windows.h>
#include <winioctl.h>
#include <cfgmgr32.h>
static const char* detectPhysicalDisk(const char* physicalType, const wchar_t* szDevice, FFlist* result, FFPhysicalDiskOptions* options) {
FF_AUTO_CLOSE_FD HANDLE hDevice = CreateFileW(szDevice, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (hDevice == INVALID_HANDLE_VALUE) {
return "CreateFileW() failed";
}
DWORD retSize;
FFPhysicalDiskType type = FF_PHYSICALDISK_TYPE_NONE;
FF_STRBUF_AUTO_DESTROY name = ffStrbufCreate();
const char* interconnect = NULL;
uint64_t size = 0;
{
alignas(DISK_GEOMETRY_EX) uint8_t dgeBuffer[4096];
if (DeviceIoControl(
hDevice,
IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
NULL,
0,
dgeBuffer,
sizeof(dgeBuffer),
&retSize,
NULL)) {
const DISK_GEOMETRY_EX* dge = (const DISK_GEOMETRY_EX*) dgeBuffer;
size = (uint64_t) dge->DiskSize.QuadPart;
} else if (DeviceIoControl(
hDevice,
IOCTL_DISK_GET_DRIVE_GEOMETRY,
NULL,
0,
dgeBuffer,
sizeof(dgeBuffer),
&retSize,
NULL) &&
retSize >= sizeof(DISK_GEOMETRY)) {
const DISK_GEOMETRY* dg = (const DISK_GEOMETRY*) dgeBuffer;
size = (uint64_t) dg->BytesPerSector * dg->SectorsPerTrack * dg->TracksPerCylinder * (uint64_t) dg->Cylinders.QuadPart;
switch (dg->MediaType) {
case F3_1Pt44_512:
case F3_2Pt88_512:
case F3_20Pt8_512:
case F3_720_512:
case F3_120M_512:
case F3_640_512:
case F3_1Pt2_512:
case F3_1Pt23_1024:
case F3_128Mb_512:
case F3_230Mb_512:
case F3_200Mb_512:
case F3_240M_512:
case F3_32M_512:
ffStrbufSetStatic(&name, "3.5-inch Floppy Disk");
break;
case F5_1Pt2_512:
case F5_360_512:
case F5_320_512:
case F5_320_1024:
case F5_180_512:
case F5_160_512:
case F5_640_512:
case F5_720_512:
case F5_1Pt23_1024:
ffStrbufSetStatic(&name, "5.25-inch Floppy Disk");
break;
case F8_256_128:
ffStrbufSetStatic(&name, "8-inch Floppy Disk");
break;
default:
return "Unsupported media type";
}
interconnect = "Floppy Controller";
type |= FF_PHYSICALDISK_TYPE_HDD | FF_PHYSICALDISK_TYPE_REMOVABLE;
}
}
if (size == 0) {
if (options->hideType & FF_PHYSICALDISK_TYPE_UNUSED) {
return "Skipping unknown disk with size 0";
}
type |= FF_PHYSICALDISK_TYPE_UNUSED;
}
const STORAGE_DEVICE_DESCRIPTOR* sdd = NULL;
alignas(STORAGE_DEVICE_DESCRIPTOR) uint8_t sddBuffer[4096];
if (!interconnect) {
if (DeviceIoControl(
hDevice,
IOCTL_STORAGE_QUERY_PROPERTY,
&(STORAGE_PROPERTY_QUERY) {
.PropertyId = StorageDeviceProperty,
.QueryType = PropertyStandardQuery,
},
sizeof(STORAGE_PROPERTY_QUERY),
&sddBuffer,
sizeof(sddBuffer),
&retSize,
NULL) ||
retSize == 0) {
sdd = (const STORAGE_DEVICE_DESCRIPTOR*) sddBuffer;
switch (sdd->BusType) {
case BusTypeScsi:
interconnect = "SCSI";
break;
case BusTypeAtapi:
interconnect = "ATAPI";
break;
case BusTypeAta:
interconnect = "ATA";
break;
case BusType1394:
interconnect = "IEEE 1394";
break;
case BusTypeSsa:
interconnect = "SSA";
break;
case BusTypeFibre:
interconnect = "Fibre";
break;
case BusTypeUsb:
interconnect = "USB";
break;
case BusTypeRAID:
interconnect = "RAID";
break;
case BusTypeiScsi:
interconnect = "iSCSI";
break;
case BusTypeSas:
interconnect = "SAS";
break;
case BusTypeSata:
interconnect = "SATA";
break;
case BusTypeSd:
interconnect = "SD";
break;
case BusTypeMmc:
interconnect = "MMC";
break;
case BusTypeVirtual:
interconnect = "Virtual";
type |= FF_PHYSICALDISK_TYPE_VIRTUAL;
break;
case BusTypeFileBackedVirtual:
interconnect = "File Backed Virtual";
type |= FF_PHYSICALDISK_TYPE_VIRTUAL;
break;
case BusTypeSpaces:
interconnect = "Storage Spaces";
type |= FF_PHYSICALDISK_TYPE_VIRTUAL;
break;
case BusTypeNvme:
interconnect = "NVMe";
break;
case BusTypeSCM:
interconnect = "SCM";
break;
case BusTypeUfs:
interconnect = "UFS";
break;
case 0x14 /*BusTypeNvmeof*/:
interconnect = "NVMe-oF";
break;
default:
interconnect = "Unknown";
break;
}
if (type & FF_PHYSICALDISK_TYPE_VIRTUAL && options->hideType & FF_PHYSICALDISK_TYPE_VIRTUAL) {
return "Skipping virtual disk";
}
if (sdd->VendorIdOffset != 0) {
ffStrbufSetS(&name, (const char*) sdd + sdd->VendorIdOffset);
ffStrbufTrim(&name, ' ');
}
if (sdd->ProductIdOffset != 0) {
if (name.length) {
ffStrbufAppendC(&name, ' ');
}
ffStrbufAppendS(&name, (const char*) sdd + sdd->ProductIdOffset);
ffStrbufTrimRight(&name, ' ');
}
}
if (!name.length) {
ffStrbufSetStatic(&name, physicalType);
}
}
if (options->namePrefix.length && !ffStrbufStartsWith(&name, &options->namePrefix)) {
return "Name prefix mismatch";
}
FFPhysicalDiskResult* device = FF_LIST_ADD(FFPhysicalDiskResult, *result);
ffStrbufInit(&device->serial);
ffStrbufInit(&device->revision);
ffStrbufInitMove(&device->name, &name);
ffStrbufInit(&device->devPath);
ffStrbufInitStatic(&device->interconnect, interconnect);
device->type = type;
device->size = size;
device->temperature = FF_PHYSICALDISK_TEMP_UNSET;
ffStrbufSetWS(&device->devPath, szDevice);
if (sdd) {
if (sdd->SerialNumberOffset != 0) {
ffStrbufSetS(&device->serial, (const char*) sdd + sdd->SerialNumberOffset);
ffStrbufTrimSpace(&device->serial);
}
if (sdd->ProductRevisionOffset != 0) {
ffStrbufSetS(&device->revision, (const char*) sdd + sdd->ProductRevisionOffset);
ffStrbufTrimRightSpace(&device->revision);
}
device->type |= sdd->RemovableMedia ? FF_PHYSICALDISK_TYPE_REMOVABLE : FF_PHYSICALDISK_TYPE_FIXED;
}
{
alignas(GET_MEDIA_TYPES) uint8_t buffer[4096];
GET_MEDIA_TYPES* gmt = (GET_MEDIA_TYPES*) buffer;
if (DeviceIoControl(
hDevice,
IOCTL_STORAGE_GET_MEDIA_TYPES_EX,
NULL,
0,
gmt,
sizeof(buffer),
&retSize,
NULL) &&
gmt->MediaInfoCount > 0) {
// DiskInfo and RemovableDiskInfo have the same structures. TapeInfo doesn't.
if (gmt->DeviceType != FILE_DEVICE_TAPE) {
__auto_type diskInfo = &gmt->MediaInfo[0].DeviceSpecific.DiskInfo;
if (diskInfo->MediaCharacteristics & MEDIA_READ_ONLY) {
device->type |= FF_PHYSICALDISK_TYPE_READONLY;
} else if (diskInfo->MediaCharacteristics & MEDIA_READ_WRITE) {
device->type |= FF_PHYSICALDISK_TYPE_READWRITE;
}
} else {
__auto_type tapeInfo = &gmt->MediaInfo[0].DeviceSpecific.TapeInfo;
if (tapeInfo->MediaCharacteristics & MEDIA_READ_ONLY) {
device->type |= FF_PHYSICALDISK_TYPE_READONLY;
} else if (tapeInfo->MediaCharacteristics & MEDIA_READ_WRITE) {
device->type |= FF_PHYSICALDISK_TYPE_READWRITE;
}
}
}
}
if (!(device->type & FF_PHYSICALDISK_TYPE_VIRTUAL) && !(device->type & FF_PHYSICALDISK_TYPE_HDD)) {
DEVICE_SEEK_PENALTY_DESCRIPTOR dspd = {};
if (DeviceIoControl(
hDevice,
IOCTL_STORAGE_QUERY_PROPERTY,
&(STORAGE_PROPERTY_QUERY) {
.PropertyId = StorageDeviceSeekPenaltyProperty,
.QueryType = PropertyStandardQuery,
},
sizeof(STORAGE_PROPERTY_QUERY),
&dspd,
sizeof(dspd),
&retSize,
NULL) &&
retSize == sizeof(dspd)) {
device->type |= dspd.IncursSeekPenalty ? FF_PHYSICALDISK_TYPE_HDD : FF_PHYSICALDISK_TYPE_SSD;
}
if (options->temp) {
STORAGE_TEMPERATURE_DATA_DESCRIPTOR stdd = {};
if (DeviceIoControl(
hDevice,
IOCTL_STORAGE_QUERY_PROPERTY,
&(STORAGE_PROPERTY_QUERY) {
.PropertyId = StorageDeviceTemperatureProperty,
.QueryType = PropertyStandardQuery,
},
sizeof(STORAGE_PROPERTY_QUERY),
&stdd,
sizeof(stdd),
&retSize,
NULL) &&
retSize == sizeof(stdd)) {
device->temperature = stdd.TemperatureInfo[0].Temperature;
}
}
}
return NULL;
}
static void detectPhysicalDisksByInterfaceClass(const char* type, const GUID* interfaceClassGuid, FFlist* result, FFPhysicalDiskOptions* options) {
ULONG cchDeviceInterfaces = 0;
if (CM_Get_Device_Interface_List_SizeW(
&cchDeviceInterfaces,
(LPGUID) interfaceClassGuid,
NULL,
CM_GET_DEVICE_INTERFACE_LIST_PRESENT) != CR_SUCCESS ||
cchDeviceInterfaces <= 1) {
return;
}
wchar_t* FF_AUTO_FREE mszDeviceInterfaces = (wchar_t*) malloc(cchDeviceInterfaces * sizeof(wchar_t));
if (!mszDeviceInterfaces) {
return;
}
if (CM_Get_Device_Interface_ListW(
(LPGUID) interfaceClassGuid,
NULL,
mszDeviceInterfaces,
cchDeviceInterfaces,
CM_GET_DEVICE_INTERFACE_LIST_PRESENT) != CR_SUCCESS) {
return;
}
// MULTI_SZ: "str1\0str2\0...\0\0"
for (const wchar_t* p = mszDeviceInterfaces; *p; p += wcslen(p) + 1) {
FF_DEBUG("Probing %s: %ls", type, p);
FF_A_UNUSED const char* error = detectPhysicalDisk(type, p, result, options);
if (error == NULL) {
FF_DEBUG("Detected device \"%s\"", FF_LIST_LAST(FFPhysicalDiskResult, *result)->name.chars);
} else {
FF_DEBUG("Failed to detect device %s: %s", type, error);
}
}
}
const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options) {
detectPhysicalDisksByInterfaceClass("Floppy", &GUID_DEVINTERFACE_FLOPPY, result, options);
detectPhysicalDisksByInterfaceClass("Disk", &GUID_DEVINTERFACE_DISK, result, options);
detectPhysicalDisksByInterfaceClass("CD-ROM", &GUID_DEVINTERFACE_CDROM, result, options);
detectPhysicalDisksByInterfaceClass("Tape", &GUID_DEVINTERFACE_TAPE, result, options);
return NULL;
}
|