181 lines
4.6 KiB
Go
181 lines
4.6 KiB
Go
package host
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"fmt"
|
|
)
|
|
|
|
// HostMemorySpec describes the host's physical memory speed and generation
|
|
// parsed from SMBIOS Type 17 (Memory Device) tables.
|
|
type HostMemorySpec struct {
|
|
SpeedMHz int `json:"speed_mhz,omitempty"`
|
|
MemoryType int `json:"memory_type,omitempty"`
|
|
TypeName string `json:"type_name,omitempty"`
|
|
}
|
|
|
|
// FormatMemorySpec returns a concise human-readable label such as
|
|
// "6400 MHz DDR5" or "3200 MHz", or "" when speed is unknown.
|
|
func FormatMemorySpec(spec *HostMemorySpec) string {
|
|
if spec == nil || spec.SpeedMHz <= 0 {
|
|
return ""
|
|
}
|
|
name := spec.TypeName
|
|
if name == "" && spec.MemoryType > 0 {
|
|
name = SMBIOSMemoryTypeName(spec.MemoryType)
|
|
}
|
|
if name != "" {
|
|
return fmt.Sprintf("%d MHz %s", spec.SpeedMHz, name)
|
|
}
|
|
return fmt.Sprintf("%d MHz", spec.SpeedMHz)
|
|
}
|
|
|
|
// SMBIOSMemoryTypeName maps DMTF SMBIOS Type 17 Memory Type codes to short
|
|
// display names. Returns "" for unknown/unspecified codes.
|
|
func SMBIOSMemoryTypeName(code int) string {
|
|
switch code {
|
|
case 18:
|
|
return "DDR"
|
|
case 19:
|
|
return "DDR2"
|
|
case 20:
|
|
return "DDR2 FB-DIMM"
|
|
case 24:
|
|
return "DDR3"
|
|
case 26:
|
|
return "DDR4"
|
|
case 27:
|
|
return "LPDDR"
|
|
case 28:
|
|
return "LPDDR2"
|
|
case 29:
|
|
return "LPDDR3"
|
|
case 30:
|
|
return "LPDDR4"
|
|
case 34:
|
|
return "DDR5"
|
|
case 35:
|
|
return "LPDDR5"
|
|
default:
|
|
return ""
|
|
}
|
|
}
|
|
|
|
// ParseWindowsRawSMBIOS parses the RawSMBIOSData buffer returned by Win32
|
|
// GetSystemFirmwareTable('RSMB', 0, ...) and extracts the host physical memory
|
|
// speed and SMBIOS memory type from populated Type 17 entries.
|
|
func ParseWindowsRawSMBIOS(buf []byte) *HostMemorySpec {
|
|
// RawSMBIOSData header:
|
|
// BYTE Used20CallingMethod;
|
|
// BYTE SMBIOSMajorVersion;
|
|
// BYTE SMBIOSMinorVersion;
|
|
// BYTE DmiRevision;
|
|
// DWORD Length;
|
|
// BYTE SMBIOSTableData[];
|
|
if len(buf) < 8 {
|
|
return nil
|
|
}
|
|
tableLen := int(binary.LittleEndian.Uint32(buf[4:8]))
|
|
table := buf[8:]
|
|
if tableLen > 0 && tableLen < len(table) {
|
|
table = table[:tableLen]
|
|
}
|
|
return ParseSMBIOSType17Table(table)
|
|
}
|
|
|
|
// ParseSMBIOSType17Table walks a raw SMBIOS structure stream and extracts the
|
|
// configured speed (falling back to rated speed) and SMBIOS Memory Type from
|
|
// populated Type 17 (Memory Device) structures.
|
|
//
|
|
// Only non-empty DIMM slots are considered. When multiple populated DIMMs
|
|
// report differing speeds, the lowest valid speed is selected. Speeds are
|
|
// bounded to uint16 (1..65535) because QEMU's `-smbios type=17,speed=...`
|
|
// stores a 16-bit value.
|
|
func ParseSMBIOSType17Table(table []byte) *HostMemorySpec {
|
|
var bestSpeed int
|
|
var bestType int
|
|
|
|
i := 0
|
|
for i+4 <= len(table) {
|
|
typ := table[i]
|
|
length := int(table[i+1])
|
|
if length < 4 || i+length > len(table) {
|
|
break
|
|
}
|
|
if typ == 127 {
|
|
// End-of-Table structure.
|
|
break
|
|
}
|
|
|
|
if typ == 17 && length >= 0x15 {
|
|
structData := table[i : i+length]
|
|
size := binary.LittleEndian.Uint16(structData[0x0C:0x0E])
|
|
populated := size != 0 && size != 0xFFFF
|
|
if size == 0x7FFF && length >= 0x20 {
|
|
extSize := binary.LittleEndian.Uint32(structData[0x1C:0x20])
|
|
populated = extSize != 0
|
|
}
|
|
if populated {
|
|
memType := int(structData[0x12])
|
|
if memType <= 2 {
|
|
// 0 = reserved, 1 = Other, 2 = Unknown
|
|
memType = 0
|
|
}
|
|
|
|
var speed int
|
|
// Prefer ConfiguredMemorySpeed (offset 0x20, SMBIOS 2.7+) so
|
|
// XMP/EXPO/actual running clock is reported instead of JEDEC base.
|
|
if length >= 0x22 {
|
|
cfg := int(binary.LittleEndian.Uint16(structData[0x20:0x22]))
|
|
if cfg > 0 && cfg < 0xFFFF {
|
|
speed = cfg
|
|
} else if cfg == 0xFFFF && length >= 0x5C {
|
|
ext := int(binary.LittleEndian.Uint32(structData[0x58:0x5C]))
|
|
if ext > 0 && ext <= 0xFFFF {
|
|
speed = ext
|
|
}
|
|
}
|
|
}
|
|
if speed == 0 && length >= 0x17 {
|
|
rated := int(binary.LittleEndian.Uint16(structData[0x15:0x17]))
|
|
if rated > 0 && rated < 0xFFFF {
|
|
speed = rated
|
|
} else if rated == 0xFFFF && length >= 0x58 {
|
|
ext := int(binary.LittleEndian.Uint32(structData[0x54:0x58]))
|
|
if ext > 0 && ext <= 0xFFFF {
|
|
speed = ext
|
|
}
|
|
}
|
|
}
|
|
|
|
if speed >= 200 && speed <= 65535 {
|
|
if bestSpeed == 0 || speed < bestSpeed {
|
|
bestSpeed = speed
|
|
}
|
|
}
|
|
if bestType == 0 && memType > 0 {
|
|
bestType = memType
|
|
}
|
|
}
|
|
}
|
|
|
|
// Advance past formatted structure and its double-NUL terminated string set.
|
|
next := i + length
|
|
for next+1 < len(table) && !(table[next] == 0 && table[next+1] == 0) {
|
|
next++
|
|
}
|
|
if next+1 >= len(table) {
|
|
break
|
|
}
|
|
i = next + 2
|
|
}
|
|
|
|
if bestSpeed == 0 {
|
|
return nil
|
|
}
|
|
return &HostMemorySpec{
|
|
SpeedMHz: bestSpeed,
|
|
MemoryType: bestType,
|
|
TypeName: SMBIOSMemoryTypeName(bestType),
|
|
}
|
|
}
|