From 446fe907d2496dd9eb6d0954cc626873bbf1896c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sat, 6 Jan 2024 11:28:00 +0800 Subject: [PATCH] Smbios: add comments, tidy --- src/detection/chassis/chassis_windows.c | 37 ++++++++++------- src/detection/host/host_windows.c | 34 ++++++++++------ src/util/smbiosHelper.h | 54 ++++++++++++++++++++++--- 3 files changed, 92 insertions(+), 33 deletions(-) diff --git a/src/detection/chassis/chassis_windows.c b/src/detection/chassis/chassis_windows.c index 707f4c3a9..5a3698928 100644 --- a/src/detection/chassis/chassis_windows.c +++ b/src/detection/chassis/chassis_windows.c @@ -4,18 +4,27 @@ // 7.4 typedef struct FFSmbiosSystemEnclosure { - uint8_t Manufacturer; - uint8_t Type; - uint8_t Version; - uint8_t SerialNumber; - uint8_t AssetTagNumber; - uint8_t BootupState; - uint8_t PowerSupplyState; - uint8_t ThermalState; - uint8_t SecurityStatus; - uint32_t OEMDefined; - uint8_t Height; - uint8_t NumberOfPowerCords; + FFSmbiosHeader Header; + + uint8_t Manufacturer; // string + uint8_t Type; // varies + uint8_t Version; // string + uint8_t SerialNumber; // string + uint8_t AssetTagNumber; // string + + // 2.1+ + uint8_t BootupState; // enum + uint8_t PowerSupplyState; // enum + uint8_t ThermalState; // enum + uint8_t SecurityStatus; // enum + + // 2.3+ + uint32_t OEMDefined; // varies + uint8_t Height; // varies + uint8_t NumberOfPowerCords; // varies + uint8_t ContainedElementCount; // varies + uint8_t ContainedRecordLength; // varies + uint8_t ContainedElements[]; // varies } FFSmbiosSystemEnclosure; const char* ffDetectChassis(FFChassisResult* result) @@ -24,14 +33,14 @@ const char* ffDetectChassis(FFChassisResult* result) for ( const FFSmbiosHeader* header = (const FFSmbiosHeader*) data->SMBIOSTableData; - (const uint8_t*) header < data->SMBIOSTableData + data->Length && header->Type < FF_SMBIOS_TYPE_LAST; + (const uint8_t*) header < data->SMBIOSTableData + data->Length && header->Type != FF_SMBIOS_TYPE_END_OF_TABLE; header = ffSmbiosSkipLastStr(header) ) { if (header->Type != FF_SMBIOS_TYPE_SYSTEM_ENCLOSURE) continue; - const FFSmbiosSystemEnclosure* data = (const FFSmbiosSystemEnclosure*) header->Data; + const FFSmbiosSystemEnclosure* data = (const FFSmbiosSystemEnclosure*) header; const char* strings = (const char*) header + header->Length; ffStrbufSetStatic(&result->vendor, ffSmbiosLocateString(strings, data->Manufacturer)); diff --git a/src/detection/host/host_windows.c b/src/detection/host/host_windows.c index 6d382330d..26969173b 100644 --- a/src/detection/host/host_windows.c +++ b/src/detection/host/host_windows.c @@ -3,10 +3,14 @@ typedef struct FFSmbiosSystemInfo { - uint8_t Manufacturer; - uint8_t ProductName; - uint8_t Version; - uint8_t SerialNumber; + FFSmbiosHeader Header; + + uint8_t Manufacturer; // string + uint8_t ProductName; // string + uint8_t Version; // string + uint8_t SerialNumber; // string + + // 2.1+ struct { uint32_t TimeLow; uint16_t TimeMid; @@ -14,10 +18,12 @@ typedef struct FFSmbiosSystemInfo uint8_t ClockSeqHiAndReserved; uint8_t ClockSeqLow; uint8_t Node[6]; - } UUID; - uint8_t WakeUpType; - uint8_t SKUNumber; - uint8_t Family; + } UUID; // varies + uint8_t WakeUpType; // enum + + // 2.4+ + uint8_t SKUNumber; // string + uint8_t Family; // string } FFSmbiosSystemInfo; const char* ffDetectHost(FFHostResult* host) @@ -26,14 +32,14 @@ const char* ffDetectHost(FFHostResult* host) for ( const FFSmbiosHeader* header = (const FFSmbiosHeader*) fullData->SMBIOSTableData; - (const uint8_t*) header < fullData->SMBIOSTableData + fullData->Length && header->Type < FF_SMBIOS_TYPE_LAST; + (const uint8_t*) header < fullData->SMBIOSTableData + fullData->Length && header->Type != FF_SMBIOS_TYPE_END_OF_TABLE; header = ffSmbiosSkipLastStr(header) ) { if (header->Type != FF_SMBIOS_TYPE_SYSTEM_INFO) continue; - const FFSmbiosSystemInfo* data = (const FFSmbiosSystemInfo*) header->Data; + const FFSmbiosSystemInfo* data = (const FFSmbiosSystemInfo*) header; const char* strings = (const char*) header + header->Length; ffStrbufSetStatic(&host->vendor, ffSmbiosLocateString(strings, data->Manufacturer)); @@ -45,15 +51,17 @@ const char* ffDetectHost(FFHostResult* host) ffStrbufSetStatic(&host->serial, ffSmbiosLocateString(strings, data->SerialNumber)); ffCleanUpSmbiosValue(&host->serial); - if (header->Length > 0x08) + static_assert(offsetof(FFSmbiosSystemInfo, UUID) == 0x08, "FFSmbiosSystemInfo.UUID offset is wrong"); + if (header->Length > offsetof(FFSmbiosSystemInfo, UUID)) { - // Order? ffStrbufSetF(&host->uuid, "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X", data->UUID.TimeLow, data->UUID.TimeMid, data->UUID.TimeHighAndVersion, data->UUID.ClockSeqHiAndReserved, data->UUID.ClockSeqLow, data->UUID.Node[0], data->UUID.Node[1], data->UUID.Node[2], data->UUID.Node[3], data->UUID.Node[4], data->UUID.Node[5]); } - if (header->Length > 0x19) + + static_assert(offsetof(FFSmbiosSystemInfo, SKUNumber) == 0x19, "FFSmbiosSystemInfo.SKUNumber offset is wrong"); + if (header->Length > offsetof(FFSmbiosSystemInfo, SKUNumber)) { ffStrbufSetStatic(&host->family, ffSmbiosLocateString(strings, data->Family)); ffCleanUpSmbiosValue(&host->family); diff --git a/src/util/smbiosHelper.h b/src/util/smbiosHelper.h index bfb7c97af..c09655ab0 100644 --- a/src/util/smbiosHelper.h +++ b/src/util/smbiosHelper.h @@ -25,14 +25,51 @@ typedef enum __attribute__((__packed__)) FFSmbiosType // : uint8_t FF_SMBIOS_TYPE_BASEBOARD_INFO = 2, FF_SMBIOS_TYPE_SYSTEM_ENCLOSURE = 3, FF_SMBIOS_TYPE_PROCESSOR_INFO = 4, - FF_SMBIOS_TYPE_MEMORY_MODULE_INFO = 6, + FF_SMBIOS_TYPE_MEMORY_CONTROLLER_INFO = 5, // obsolete + FF_SMBIOS_TYPE_MEMORY_MODULE_INFO = 6, // obsolete FF_SMBIOS_TYPE_CACHE_INFO = 7, + FF_SMBIOS_TYPE_PORT_CONNECTOR_INFO = 8, + FF_SMBIOS_TYPE_SYSTEM_SLOTS = 9, + FF_SMBIOS_TYPE_ON_BOARD_DEVICES_INFO = 10, // obsolete FF_SMBIOS_TYPE_OEM_STRING = 11, + FF_SMBIOS_TYPE_SYSTEM_CONFIGURATION_OPTIONS = 12, + FF_SMBIOS_TYPE_BIOS_LANGUAGE_INFO = 13, + FF_SMBIOS_TYPE_GROUP_ASSOCIATIONS = 14, + FF_SMBIOS_TYPE_SYSTEM_EVENT_LOG = 15, + FF_SMBIOS_TYPE_PHYSICAL_MEMORY_ARRAY = 16, FF_SMBIOS_TYPE_MEMORY_DEVICE = 17, + FF_SMBIOS_TYPE_32BIT_MEMORY_ERROR_INFO = 18, FF_SMBIOS_TYPE_MEMORY_ARRAY_MAPPED_ADDRESS = 19, - FF_SMBIOS_TYPE_BUILTIN_POINT_DEVICE = 21, + FF_SMBIOS_TYPE_MEMORY_DEVICE_MAPPED_ADDRESS = 20, + FF_SMBIOS_TYPE_BUILTIN_POINTING_DEVICE = 21, FF_SMBIOS_TYPE_PORTABLE_BATTERY = 22, - FF_SMBIOS_TYPE_LAST = 127, + FF_SMBIOS_TYPE_SYSTEM_RESET = 23, + FF_SMBIOS_TYPE_HARDWARE_SECURITY = 24, + FF_SMBIOS_TYPE_SYSTEM_POWER_CONTROLS = 25, + FF_SMBIOS_TYPE_VOLTAGE_PROBE = 26, + FF_SMBIOS_TYPE_COOLING_DEVICE = 27, + FF_SMBIOS_TYPE_TEMPERATURE_PROBE = 28, + FF_SMBIOS_TYPE_ELECTRICAL_CURRENT_PROBE = 29, + FF_SMBIOS_TYPE_OUT_OF_BAND_REMOTE_ACCESS = 30, + FF_SMBIOS_TYPE_BOOT_INTEGRITY_SERVICES_ENTRY_POINT = 31, // reserved + FF_SMBIOS_TYPE_SYSTEM_BOOT_INFO = 32, + FF_SMBIOS_TYPE_64BIT_MEMORY_ERROR_INFO = 33, + FF_SMBIOS_TYPE_MANAGEMENT_DEVICE = 34, + FF_SMBIOS_TYPE_MANAGEMENT_DEVICE_COMPONENT = 35, + FF_SMBIOS_TYPE_MANAGEMENT_DEVICE_THRESHOLD_DATA = 36, + FF_SMBIOS_TYPE_MEMORY_CHANNEL = 37, + FF_SMBIOS_TYPE_IPMI_DEVICE_INFO = 38, + FF_SMBIOS_TYPE_SYSTEM_POWER_SUPPLY = 39, + FF_SMBIOS_TYPE_ADDITIONAL_INFO = 40, + FF_SMBIOS_TYPE_ONBOARD_DEVICE_EXTENDED_INFO = 41, + FF_SMBIOS_TYPE_MANAGEMENT_CONTROLLER_HOST_INTERFACE = 42, + FF_SMBIOS_TYPE_TPM_DEVICE = 43, + FF_SMBIOS_TYPE_PROCESSOR_ADDITIONAL_INFO = 44, + FF_SMBIOS_TYPE_FIRMWARE_INVENTORY_INFO = 45, + FF_SMBIOS_TYPE_STRING_PROPERTY = 46, + FF_SMBIOS_TYPE_INACTIVE = 126, + FF_SMBIOS_TYPE_END_OF_TABLE = 127, + // system- and OEM-specific information 128~256 } FFSmbiosType; static_assert(sizeof(FFSmbiosType) == 1, "FFSmbiosType should be 1 byte"); @@ -41,7 +78,6 @@ typedef struct FFSmbiosHeader FFSmbiosType Type; uint8_t Length; uint16_t Handle; - uint8_t Data[]; } FFSmbiosHeader; static_assert(sizeof(FFSmbiosHeader) == 4, "FFSmbiosHeader should be 4 bytes"); @@ -67,8 +103,14 @@ static inline const char* ffSmbiosLocateString(const char* start, uint8_t index static inline const FFSmbiosHeader* ffSmbiosSkipLastStr(const FFSmbiosHeader* header) { const char* p = ((const char*) header) + header->Length; - while (*p) - p += strlen(p) + 1; + if (*p) + { + do + p += strlen(p) + 1; + while (*p); + } + else // The terminator is always double 0 even if there is no string + p ++; return (const FFSmbiosHeader*) (p + 1); }