Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Features:
* Added glob and environment variable expansion to logo sources on Windows. `%VAR%`, `$VAR` and `${VAR}` are supported, and a glob matching several files picks one randomly. (#2718, Logo, Windows)
* Added opt-in drive health detection for NVMe disks via udisks2, which doesn't require root. (#2726, PhysicalDisk, Linux)
* Added opt-in drive health detection for NVMe disks, via udisks2 on Linux (no root required), NVMe SMART on macOS and the NVMe health log on Windows. (#2726, PhysicalDisk, Linux / macOS / Windows)
* Enable it with `"health": true`. The default output appends `Health <100 - percentage used>%` and any critical warnings.
* New format args: `{health}`, `{percent-used}`, `{available-spare}` and `{critical-warning}`.

Expand Down
17 changes: 12 additions & 5 deletions src/detection/physicaldisk/physicaldisk_apple.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static inline void wrapIoDestroyPlugInInterface(IOCFPlugInInterface*** pluginInf
}
#endif

static const char* detectSsdTemp(io_service_t entryPhysical, double* temp) {
static const char* detectNvmeSmart(io_service_t entryPhysical, FFPhysicalDiskResult* device, FFPhysicalDiskOptions* options) {
#ifdef MAC_OS_X_VERSION_10_15
[[gnu::cleanup(wrapIoDestroyPlugInInterface)]] IOCFPlugInInterface** pluginInf = nullptr;
int32_t score;
Expand All @@ -39,15 +39,22 @@ static const char* detectSsdTemp(io_service_t entryPhysical, double* temp) {
NVMeSMARTData smartData;
const char* error = nullptr;
if ((*smartInf)->SMARTReadData(smartInf, &smartData) == kIOReturnSuccess) {
*temp = smartData.TEMPERATURE - 273;
if (options->temp) {
device->temperature = smartData.TEMPERATURE - 273;
}
if (options->health) {
device->percentUsed = (uint8_t) (smartData.PERCENTAGE_USED > 254 ? 254 : smartData.PERCENTAGE_USED);
device->availableSpare = (uint8_t) (smartData.AVAILABLE_SPARE > 100 ? 100 : smartData.AVAILABLE_SPARE);
device->criticalWarning = (FFPhysicalDiskWarning) (smartData.CRITICAL_WARNING & 0x3F);
}
} else {
error = "SMARTReadData() failed";
}

(*pluginInf)->Release(smartInf);
return error;
#else
FF_DEBUG("SSD temperature detection needs macOS 10.15 or newer");
FF_DEBUG("NVMe SMART detection needs macOS 10.15 or newer");
return "No support for old MacOS version";
#endif
}
Expand Down Expand Up @@ -186,10 +193,10 @@ const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options)
}

#ifdef MAC_OS_X_VERSION_10_15
if (!(device->type & FF_PHYSICALDISK_TYPE_VIRTUAL) && options->temp) {
if (!(device->type & FF_PHYSICALDISK_TYPE_VIRTUAL) && (options->temp || options->health)) {
FF_CFTYPE_AUTO_RELEASE CFBooleanRef nvmeSMARTCapable = IORegistryEntryCreateCFProperty(entryPhysical, CFSTR(kIOPropertyNVMeSMARTCapableKey), nullptr, kNilOptions);
if (nvmeSMARTCapable && CFBooleanGetValue(nvmeSMARTCapable)) {
detectSsdTemp(entryPhysical, &device->temperature);
detectNvmeSmart(entryPhysical, device, options);
}
}
#endif
Expand Down
36 changes: 36 additions & 0 deletions src/detection/physicaldisk/physicaldisk_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <windows.h>
#include <winioctl.h>
#include <cfgmgr32.h>
#include <nvme.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, nullptr, OPEN_EXISTING, 0, nullptr);
Expand Down Expand Up @@ -302,6 +303,41 @@ static const char* detectPhysicalDisk(const char* physicalType, const wchar_t* s
device->temperature = stdd.TemperatureInfo[0].Temperature;
}
}

if (options->health && sdd && sdd->BusType == BusTypeNvme) {
alignas(STORAGE_PROPERTY_QUERY) uint8_t buffer[offsetof(STORAGE_PROPERTY_QUERY, AdditionalParameters) + sizeof(STORAGE_PROTOCOL_SPECIFIC_DATA) + sizeof(NVME_HEALTH_INFO_LOG)] = {};
STORAGE_PROPERTY_QUERY* query = (STORAGE_PROPERTY_QUERY*) buffer;
query->PropertyId = StorageDeviceProtocolSpecificProperty;
query->QueryType = PropertyStandardQuery;
*(STORAGE_PROTOCOL_SPECIFIC_DATA*) query->AdditionalParameters = (STORAGE_PROTOCOL_SPECIFIC_DATA) {
.ProtocolType = ProtocolTypeNvme,
.DataType = NVMeDataTypeLogPage,
.ProtocolDataRequestValue = NVME_LOG_PAGE_HEALTH_INFO,
.ProtocolDataOffset = sizeof(STORAGE_PROTOCOL_SPECIFIC_DATA),
.ProtocolDataLength = sizeof(NVME_HEALTH_INFO_LOG),
};
if (DeviceIoControl(
hDevice,
IOCTL_STORAGE_QUERY_PROPERTY,
buffer,
sizeof(buffer),
buffer,
sizeof(buffer),
&retSize,
nullptr)) {
const STORAGE_PROTOCOL_DATA_DESCRIPTOR* spdd = (const STORAGE_PROTOCOL_DATA_DESCRIPTOR*) buffer;
if (spdd->ProtocolSpecificData.ProtocolDataOffset == sizeof(STORAGE_PROTOCOL_SPECIFIC_DATA) &&
spdd->ProtocolSpecificData.ProtocolDataLength >= sizeof(NVME_HEALTH_INFO_LOG) &&
retSize == sizeof(buffer)) {
const NVME_HEALTH_INFO_LOG* log = (const NVME_HEALTH_INFO_LOG*) ((const uint8_t*) &spdd->ProtocolSpecificData + sizeof(STORAGE_PROTOCOL_SPECIFIC_DATA));
device->percentUsed = (uint8_t) (log->PercentageUsed > 254 ? 254 : log->PercentageUsed);
device->availableSpare = (uint8_t) (log->AvailableSpare > 100 ? 100 : log->AvailableSpare);
device->criticalWarning = (FFPhysicalDiskWarning) (log->CriticalWarning.AsUchar & 0x3F);
}
} else {
FF_DEBUG("Failed to query NVMe health log: %s", ffDebugWin32Error(GetLastError()));
}
}
}

return nullptr;
Expand Down
Loading