#[allow(unused_imports)]
use super::{ProfileGroup, Setting, SettingRisk, SettingValue, Subsystem};
#[cfg_attr(not(any(target_os = "linux", test)), allow(dead_code))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum DramType {
Ddr4,
Ddr5,
Other(u8),
}
#[cfg(target_os = "linux")]
pub fn scan_xmp_groups() -> Vec<ProfileGroup> {
use std::fs;
let base = std::path::Path::new("/sys/bus/i2c/devices");
let Ok(rd) = fs::read_dir(base) else {
return Vec::new();
};
let mut groups = Vec::new();
for entry in rd.flatten() {
let path = entry.path();
let eeprom = path.join("eeprom");
if !eeprom.exists() {
continue;
}
let Ok(bytes) = fs::read(&eeprom) else {
continue;
};
if bytes.len() < 384 {
continue;
}
if let Some(g) = decode_blob(
&bytes,
&eeprom.display().to_string(),
entry.file_name().to_string_lossy().to_string(),
) {
groups.push(g);
}
}
groups
}
#[cfg(not(target_os = "linux"))]
pub fn scan_xmp_groups() -> Vec<ProfileGroup> {
Vec::new()
}
#[cfg_attr(not(any(target_os = "linux", test)), allow(dead_code))]
fn dram_type(bytes: &[u8]) -> DramType {
match bytes.get(2).copied() {
Some(0x0C) => DramType::Ddr4,
Some(0x12) => DramType::Ddr5,
Some(other) => DramType::Other(other),
None => DramType::Other(0),
}
}
#[cfg_attr(not(any(target_os = "linux", test)), allow(dead_code))]
fn decode_blob(bytes: &[u8], source: &str, locator: String) -> Option<ProfileGroup> {
let dram = dram_type(bytes);
let dram_label = match dram {
DramType::Ddr4 => "DDR4",
DramType::Ddr5 => "DDR5",
DramType::Other(_) => return None,
};
let mut group = ProfileGroup::new(
Subsystem::Memory,
format!("DIMM @ {} ({})", locator, dram_label),
"SPD XMP/EXPO profiles",
source.to_string(),
);
let mut any_profile = false;
if bytes.len() >= 386 && bytes[384] == 0x0C && bytes[385] == 0x4A {
any_profile = true;
decode_xmp(&mut group, bytes, dram);
}
if bytes.len() >= 772 && &bytes[768..772] == b"EXPO" {
any_profile = true;
decode_expo(&mut group, bytes);
}
if !any_profile {
group.note(
"No XMP or EXPO magic found in this SPD blob — this DIMM ships \
with JEDEC profiles only.",
);
}
Some(group)
}
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
fn decode_xmp(group: &mut ProfileGroup, bytes: &[u8], dram: DramType) {
let enabled = bytes.get(385).copied().unwrap_or(0);
let profile_count = enabled.count_ones() as u64;
group.push(
Setting::info(
"xmp.version",
"XMP Version",
SettingValue::Text(match dram {
DramType::Ddr5 => "XMP 3.0".into(),
_ => "XMP 2.0".into(),
}),
)
.with_source("SPD offset 384"),
);
group.push(
Setting::info(
"xmp.profile_count",
"XMP Profiles Present",
SettingValue::Uint(profile_count),
)
.with_description("Number of XMP profiles enabled in this DIMM's SPD.")
.with_source("SPD offset 385"),
);
let (slot_stride, slot_base) = match dram {
DramType::Ddr5 => (64usize, 393usize),
_ => (35usize, 393usize),
};
for slot in 0..5 {
if (enabled >> slot) & 1 == 0 {
continue;
}
let base = slot_base + slot * slot_stride;
if base + 4 >= bytes.len() {
break;
}
let vdd_raw = bytes[base] as u32;
let voltage = match dram {
DramType::Ddr4 => Some(1.0 + (vdd_raw & 0x7F) as f64 * 0.005),
_ => None,
};
if let Some(v) = voltage {
group.push(
Setting::info(
format!("xmp.slot{}.vdd_v", slot),
format!("XMP Profile {} Vdd", slot + 1),
SettingValue::Float(v),
)
.with_unit("V")
.with_risk(SettingRisk::Dangerous)
.with_source(format!("SPD offset {}", base)),
);
} else {
group.push(
Setting::info(
format!("xmp.slot{}.vdd_raw", slot),
format!("XMP Profile {} Vdd (raw)", slot + 1),
SettingValue::Uint(vdd_raw as u64),
)
.with_source(format!("SPD offset {}", base)),
);
}
let mtb_lo = bytes.get(base + 1).copied().unwrap_or(0) as u32;
let mtb_hi = bytes.get(base + 2).copied().unwrap_or(0) as u32;
let mtb = mtb_lo | (mtb_hi << 8);
if mtb > 0 {
let cycle_ps = mtb as f64 * 125.0; let mts = (2.0 * 1_000_000.0 / cycle_ps).round() as u64;
group.push(
Setting::info(
format!("xmp.slot{}.speed_mts", slot),
format!("XMP Profile {} Speed", slot + 1),
SettingValue::Uint(mts),
)
.with_unit("MT/s")
.with_source(format!("SPD offset {}–{}", base + 1, base + 2)),
);
}
}
if profile_count == 0 {
group.note("XMP magic present but no profiles flagged enabled in byte 385.");
}
}
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
fn decode_expo(group: &mut ProfileGroup, bytes: &[u8]) {
let enabled = bytes.get(772).copied().unwrap_or(0);
let profile_count = enabled.count_ones() as u64;
group.push(
Setting::info(
"expo.version",
"EXPO Version",
SettingValue::Text("AMD EXPO".into()),
)
.with_source("SPD offset 768"),
);
group.push(
Setting::info(
"expo.profile_count",
"EXPO Profiles Present",
SettingValue::Uint(profile_count),
)
.with_description("Number of AMD EXPO profiles enabled in this DIMM's SPD.")
.with_source("SPD offset 772"),
);
for slot in 0..2 {
if (enabled >> slot) & 1 == 0 {
continue;
}
let base = 776 + slot * 64;
if base + 4 >= bytes.len() {
break;
}
let vdd_raw = bytes[base] as u32;
group.push(
Setting::info(
format!("expo.slot{}.vdd_raw", slot),
format!("EXPO Profile {} Vdd (raw)", slot + 1),
SettingValue::Uint(vdd_raw as u64),
)
.with_risk(SettingRisk::Dangerous)
.with_source(format!("SPD offset {}", base)),
);
}
}
#[cfg(test)]
mod tests {
use super::*;
fn make_ddr4_blob() -> Vec<u8> {
let mut b = vec![0u8; 512];
b[2] = 0x0C; b[384] = 0x0C; b[385] = 0x4A;
b[386] = 0b0000_0011; b[393] = 0x28;
b[394] = 0x07; b[395] = 0x00;
b
}
#[test]
fn detects_ddr4_xmp() {
let blob = make_ddr4_blob();
let g = decode_blob(&blob, "test", "0-0050".into()).unwrap();
assert!(g.settings.iter().any(|s| s.id == "xmp.version"));
let profile_count = g
.settings
.iter()
.find(|s| s.id == "xmp.profile_count")
.unwrap();
assert!(matches!(profile_count.value, SettingValue::Uint(_)));
}
#[test]
fn skips_unknown_dram_type() {
let mut blob = vec![0u8; 512];
blob[2] = 0xFF;
assert!(decode_blob(&blob, "test", "x".into()).is_none());
}
#[test]
fn detects_expo_magic() {
let mut b = vec![0u8; 1024];
b[2] = 0x12; b[768..772].copy_from_slice(b"EXPO");
b[772] = 0b0000_0001;
b[776] = 0x50;
let g = decode_blob(&b, "test", "y".into()).unwrap();
assert!(g.settings.iter().any(|s| s.id == "expo.version"));
}
}