Skip to content

Commit c37ac4d

Browse files
committed
PhysicalDisk (Windows): adds type virtual and type unknown detection
1 parent 643db92 commit c37ac4d

1 file changed

Lines changed: 93 additions & 94 deletions

File tree

src/detection/physicaldisk/physicaldisk_windows.c

Lines changed: 93 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,29 @@ static bool detectPhysicalDisk(const wchar_t* szDevice, FFlist* result, FFPhysic
1313
}
1414

1515
DWORD retSize;
16-
char sddBuffer[4096];
16+
FFPhysicalDiskType type = FF_PHYSICALDISK_TYPE_NONE;
17+
18+
uint64_t size = 0;
19+
{
20+
alignas(DISK_GEOMETRY_EX) uint8_t dgeBuffer[4096];
21+
if (DeviceIoControl(
22+
hDevice,
23+
IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
24+
NULL,
25+
0,
26+
dgeBuffer,
27+
sizeof(dgeBuffer),
28+
&retSize,
29+
NULL)) {
30+
const DISK_GEOMETRY_EX* dge = (const DISK_GEOMETRY_EX*) dgeBuffer;
31+
size = (uint64_t) dge->DiskSize.QuadPart;
32+
}
33+
}
34+
if (size == 0) {
35+
type |= FF_PHYSICALDISK_TYPE_UNKNOWN;
36+
}
37+
38+
alignas(STORAGE_DEVICE_DESCRIPTOR) uint8_t sddBuffer[4096];
1739
if (!DeviceIoControl(
1840
hDevice,
1941
IOCTL_STORAGE_QUERY_PROPERTY,
@@ -29,146 +51,126 @@ static bool detectPhysicalDisk(const wchar_t* szDevice, FFlist* result, FFPhysic
2951
retSize == 0) {
3052
return true;
3153
}
54+
const STORAGE_DEVICE_DESCRIPTOR* sdd = (const STORAGE_DEVICE_DESCRIPTOR*) sddBuffer;
3255

33-
FFPhysicalDiskResult* device = (FFPhysicalDiskResult*) ffListAdd(result);
34-
ffStrbufInit(&device->serial);
35-
ffStrbufInit(&device->revision);
36-
ffStrbufInit(&device->name);
37-
ffStrbufInit(&device->devPath);
38-
ffStrbufInit(&device->interconnect);
39-
device->type = FF_PHYSICALDISK_TYPE_NONE;
40-
device->size = 0;
41-
device->temperature = FF_PHYSICALDISK_TEMP_UNSET;
42-
43-
STORAGE_DEVICE_DESCRIPTOR* sdd = (STORAGE_DEVICE_DESCRIPTOR*) sddBuffer;
44-
45-
if (sdd->VendorIdOffset != 0) {
46-
ffStrbufSetS(&device->name, (const char*) sddBuffer + sdd->VendorIdOffset);
47-
ffStrbufTrim(&device->name, ' ');
48-
}
49-
if (sdd->ProductIdOffset != 0) {
50-
if (device->name.length) {
51-
ffStrbufAppendC(&device->name, ' ');
52-
}
53-
54-
ffStrbufAppendS(&device->name, (const char*) sddBuffer + sdd->ProductIdOffset);
55-
ffStrbufTrimRight(&device->name, ' ');
56-
}
57-
58-
if (!device->name.length) {
59-
ffStrbufSetWS(&device->name, szDevice);
60-
}
61-
62-
if (options->namePrefix.length && !ffStrbufStartsWith(&device->name, &options->namePrefix)) {
63-
ffStrbufDestroy(&device->name);
64-
result->length--;
65-
return true;
66-
}
67-
68-
ffStrbufSetWS(&device->devPath, szDevice);
69-
if (sdd->SerialNumberOffset != 0) {
70-
ffStrbufSetS(&device->serial, (const char*) sddBuffer + sdd->SerialNumberOffset);
71-
ffStrbufTrimSpace(&device->serial);
72-
}
73-
74-
if (sdd->ProductRevisionOffset != 0) {
75-
ffStrbufSetS(&device->revision, (const char*) sddBuffer + sdd->ProductRevisionOffset);
76-
ffStrbufTrimRightSpace(&device->revision);
77-
}
78-
79-
device->type |= sdd->RemovableMedia ? FF_PHYSICALDISK_TYPE_REMOVABLE : FF_PHYSICALDISK_TYPE_FIXED;
80-
56+
const char* interconnect;
8157
switch (sdd->BusType) {
82-
case BusTypeUnknown:
83-
ffStrbufSetStatic(&device->interconnect, "Unknown");
84-
break;
8558
case BusTypeScsi:
86-
ffStrbufSetStatic(&device->interconnect, "SCSI");
59+
interconnect = "SCSI";
8760
break;
8861
case BusTypeAtapi:
89-
ffStrbufSetStatic(&device->interconnect, "ATAPI");
62+
interconnect = "ATAPI";
9063
break;
9164
case BusTypeAta:
92-
ffStrbufSetStatic(&device->interconnect, "ATA");
65+
interconnect = "ATA";
9366
break;
9467
case BusType1394:
95-
ffStrbufSetStatic(&device->interconnect, "IEEE 1394");
68+
interconnect = "IEEE 1394";
9669
break;
9770
case BusTypeSsa:
98-
ffStrbufSetStatic(&device->interconnect, "SSA");
71+
interconnect = "SSA";
9972
break;
10073
case BusTypeFibre:
101-
ffStrbufSetStatic(&device->interconnect, "Fibre");
74+
interconnect = "Fibre";
10275
break;
10376
case BusTypeUsb:
104-
ffStrbufSetStatic(&device->interconnect, "USB");
77+
interconnect = "USB";
10578
break;
10679
case BusTypeRAID:
107-
ffStrbufSetStatic(&device->interconnect, "RAID");
80+
interconnect = "RAID";
10881
break;
10982
case BusTypeiScsi:
110-
ffStrbufSetStatic(&device->interconnect, "iSCSI");
83+
interconnect = "iSCSI";
11184
break;
11285
case BusTypeSas:
113-
ffStrbufSetStatic(&device->interconnect, "SAS");
86+
interconnect = "SAS";
11487
break;
11588
case BusTypeSata:
116-
ffStrbufSetStatic(&device->interconnect, "SATA");
89+
interconnect = "SATA";
11790
break;
11891
case BusTypeSd:
119-
ffStrbufSetStatic(&device->interconnect, "SD");
92+
interconnect = "SD";
12093
break;
12194
case BusTypeMmc:
122-
ffStrbufSetStatic(&device->interconnect, "MMC");
95+
interconnect = "MMC";
12396
break;
12497
case BusTypeVirtual:
125-
ffStrbufSetStatic(&device->interconnect, "Virtual");
126-
device->type |= FF_PHYSICALDISK_TYPE_VIRTUAL;
98+
interconnect = "Virtual";
99+
type |= FF_PHYSICALDISK_TYPE_VIRTUAL;
127100
break;
128101
case BusTypeFileBackedVirtual:
129-
ffStrbufSetStatic(&device->interconnect, "File Backed Virtual");
130-
device->type |= FF_PHYSICALDISK_TYPE_VIRTUAL;
102+
interconnect = "File Backed Virtual";
103+
type |= FF_PHYSICALDISK_TYPE_VIRTUAL;
131104
break;
132105
case BusTypeSpaces:
133-
ffStrbufSetStatic(&device->interconnect, "Storage Spaces");
134-
device->type |= FF_PHYSICALDISK_TYPE_VIRTUAL;
106+
interconnect = "Storage Spaces";
107+
type |= FF_PHYSICALDISK_TYPE_VIRTUAL;
135108
break;
136109
case BusTypeNvme:
137-
ffStrbufSetStatic(&device->interconnect, "NVMe");
110+
interconnect = "NVMe";
138111
break;
139112
case BusTypeSCM:
140-
ffStrbufSetStatic(&device->interconnect, "SCM");
113+
interconnect = "SCM";
141114
break;
142115
case BusTypeUfs:
143-
ffStrbufSetStatic(&device->interconnect, "UFS");
116+
interconnect = "UFS";
144117
break;
145118
case 0x14 /*BusTypeNvmeof*/:
146-
ffStrbufSetStatic(&device->interconnect, "NVMe-oF");
119+
interconnect = "NVMe-oF";
147120
break;
148121
default:
149-
ffStrbufSetF(&device->interconnect, "Unknown (%d)", (int) sdd->BusType);
122+
interconnect = "Unknown";
150123
break;
151124
}
152125

153-
{
154-
DISK_GEOMETRY_EX dge = {};
155-
if (DeviceIoControl(
156-
hDevice,
157-
IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
158-
NULL,
159-
0,
160-
&dge,
161-
sizeof(dge),
162-
&retSize,
163-
NULL)) {
164-
device->size = (uint64_t) dge.DiskSize.QuadPart;
165-
} else {
166-
device->size = 0;
126+
FFPhysicalDiskResult* device = (FFPhysicalDiskResult*) ffListAdd(result);
127+
ffStrbufInit(&device->serial);
128+
ffStrbufInit(&device->revision);
129+
ffStrbufInit(&device->name);
130+
ffStrbufInit(&device->devPath);
131+
ffStrbufInitStatic(&device->interconnect, interconnect);
132+
device->type = type;
133+
device->size = size;
134+
device->temperature = FF_PHYSICALDISK_TEMP_UNSET;
135+
136+
if (sdd->VendorIdOffset != 0) {
137+
ffStrbufSetS(&device->name, (const char*) sddBuffer + sdd->VendorIdOffset);
138+
ffStrbufTrim(&device->name, ' ');
139+
}
140+
if (sdd->ProductIdOffset != 0) {
141+
if (device->name.length) {
142+
ffStrbufAppendC(&device->name, ' ');
167143
}
144+
145+
ffStrbufAppendS(&device->name, (const char*) sddBuffer + sdd->ProductIdOffset);
146+
ffStrbufTrimRight(&device->name, ' ');
147+
}
148+
149+
if (!device->name.length) {
150+
ffStrbufSetWS(&device->name, szDevice);
151+
}
152+
153+
if (options->namePrefix.length && !ffStrbufStartsWith(&device->name, &options->namePrefix)) {
154+
ffStrbufDestroy(&device->name);
155+
result->length--;
156+
return true;
157+
}
158+
159+
ffStrbufSetWS(&device->devPath, szDevice);
160+
if (sdd->SerialNumberOffset != 0) {
161+
ffStrbufSetS(&device->serial, (const char*) sddBuffer + sdd->SerialNumberOffset);
162+
ffStrbufTrimSpace(&device->serial);
168163
}
169164

165+
if (sdd->ProductRevisionOffset != 0) {
166+
ffStrbufSetS(&device->revision, (const char*) sddBuffer + sdd->ProductRevisionOffset);
167+
ffStrbufTrimRightSpace(&device->revision);
168+
}
169+
170+
device->type |= sdd->RemovableMedia ? FF_PHYSICALDISK_TYPE_REMOVABLE : FF_PHYSICALDISK_TYPE_FIXED;
171+
170172
{
171-
alignas(GET_MEDIA_TYPES) uint8_t buffer[sizeof(GET_MEDIA_TYPES) + sizeof(DEVICE_MEDIA_INFO) * 7] = {};
173+
alignas(GET_MEDIA_TYPES) uint8_t buffer[4096];
172174
GET_MEDIA_TYPES* gmt = (GET_MEDIA_TYPES*) buffer;
173175
if (DeviceIoControl(
174176
hDevice,
@@ -188,9 +190,6 @@ static bool detectPhysicalDisk(const wchar_t* szDevice, FFlist* result, FFPhysic
188190
} else if (diskInfo->MediaCharacteristics & MEDIA_READ_WRITE) {
189191
device->type |= FF_PHYSICALDISK_TYPE_READWRITE;
190192
}
191-
if (device->size == 0) {
192-
device->size = (uint64_t) diskInfo->NumberMediaSides * diskInfo->TracksPerCylinder * diskInfo->SectorsPerTrack * diskInfo->BytesPerSector;
193-
}
194193
} else {
195194
__auto_type tapeInfo = &gmt->MediaInfo[0].DeviceSpecific.TapeInfo;
196195
if (tapeInfo->MediaCharacteristics & MEDIA_READ_ONLY) {

0 commit comments

Comments
 (0)