Skip to content

Commit 2504fb8

Browse files
committed
framework_lib: Parse CMSE versions correctly
After CSME update, the three versions will be different. Signed-off-by: Daniel Schaefer <[email protected]>
1 parent 72e0acb commit 2504fb8

File tree

2 files changed

+68
-26
lines changed

2 files changed

+68
-26
lines changed

framework_lib/src/commandline/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,9 @@ fn print_versions(ec: &CrosEc) {
390390
println!("CSME");
391391
if let Ok(csme) = csme::csme_from_sysfs() {
392392
println!(" Enabled: {}", csme.enabled);
393-
println!(" Version: {}", csme.version);
393+
println!(" Version: {}", csme.main_ver);
394+
println!(" Recovery Ver: {}", csme.recovery_ver);
395+
println!(" Original Ver: {}", csme.fitc_ver);
394396
} else {
395397
println!(" Unknown");
396398
}

framework_lib/src/csme.rs

+65-25
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@ pub struct CsmeInfo {
1414
/// Whether the CSME is currently enabled or not
1515
pub enabled: bool,
1616
/// Currently running CSME firmware version
17-
pub version: CsmeVersion,
17+
pub main_ver: CsmeVersion,
18+
pub recovery_ver: CsmeVersion,
19+
pub fitc_ver: CsmeVersion,
1820
}
1921
/// CSME Version
2022
///
2123
/// Example: 0:16.0.15.1810
24+
#[derive(Debug, PartialEq, Eq)]
2225
pub struct CsmeVersion {
2326
pub platform: u32,
2427
pub major: u32,
@@ -27,6 +30,51 @@ pub struct CsmeVersion {
2730
pub buildno: u32,
2831
}
2932

33+
impl From<&str> for CsmeVersion {
34+
fn from(fw_ver: &str) -> Self {
35+
// Parse the CSME version
36+
// Example: 0:16.0.15.1810
37+
let mut sections = fw_ver.split(':');
38+
39+
let left = sections
40+
.next()
41+
.unwrap()
42+
.parse::<u32>()
43+
.expect("Unexpected value");
44+
let mut right = sections.next().unwrap().split('.');
45+
46+
let second = right
47+
.next()
48+
.unwrap()
49+
.parse::<u32>()
50+
.expect("Unexpected value");
51+
let third = right
52+
.next()
53+
.unwrap()
54+
.parse::<u32>()
55+
.expect("Unexpected value");
56+
let fourth = right
57+
.next()
58+
.unwrap()
59+
.parse::<u32>()
60+
.expect("Unexpected value");
61+
let fifth = right
62+
.next()
63+
.unwrap()
64+
.trim()
65+
.parse::<u32>()
66+
.expect("Unexpected value");
67+
68+
CsmeVersion {
69+
platform: left,
70+
major: second,
71+
minor: third,
72+
hotfix: fourth,
73+
buildno: fifth,
74+
}
75+
}
76+
}
77+
3078
impl fmt::Display for CsmeVersion {
3179
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3280
write!(
@@ -50,36 +98,28 @@ pub fn csme_from_sysfs() -> io::Result<CsmeInfo> {
5098
let path = csmeme_entry.path();
5199
if path.is_dir() {
52100
let dev_state = fs::read_to_string(path.join("dev_state"))?;
53-
// TODO: Make sure invalid cases are handled and not silently ignored
101+
// Can be one of INITIALIZING, INIT_CLIENTS, ENABLED, RESETTING, DISABLED,
102+
// POWER_DOWN, POWER_UP
103+
// See linux kernel at: Documentation/ABI/testing/sysfs-class-mei
54104
let enabled = matches!(dev_state.as_str(), "ENABLED");
55105

106+
// Kernel gives us multiple \n separated lines in a file
56107
let fw_vers = fs::read_to_string(path.join("fw_ver"))?;
57-
// Kernel gives us multiple \n separated lines
58-
let fw_vers: Vec<&str> = fw_vers.lines().collect();
59-
// TODO: I don't understand why the kernel gives me 4 versions.
60-
// Make sure my assumption that all versios are the same holds tru.
61-
assert!(fw_vers.iter().all(|&item| item == fw_vers[0]));
62-
let fw_ver: &str = fw_vers[0];
63-
// Parse the CSME version
64-
// Example: 0:16.0.15.1810
65-
let sections: Vec<&str> = fw_ver.split(':').collect();
66-
let first = sections[0].parse::<u32>().expect("Unexpected value");
67-
let right: Vec<&str> = sections[1].split('.').collect();
68-
let second = right[0].parse::<u32>().expect("Unexpected value");
69-
let third = right[1].parse::<u32>().expect("Unexpected value");
70-
let fourth = right[2].parse::<u32>().expect("Unexpected value");
71-
let fifth = right[3].trim().parse::<u32>().expect("Unexpected value");
108+
let fw_vers = fw_vers.lines();
109+
110+
let mut infos = fw_vers.map(CsmeVersion::from);
111+
let main_ver = infos.next().unwrap();
112+
let recovery_ver = infos.next().unwrap();
113+
let fitc_ver = infos.next().unwrap();
114+
// Make sure there are three and no more
115+
assert_eq!(infos.next(), None);
72116

73117
csme_info = Some(CsmeInfo {
74118
enabled,
75-
version: CsmeVersion {
76-
platform: first,
77-
major: second,
78-
minor: third,
79-
hotfix: fourth,
80-
buildno: fifth,
81-
},
82-
});
119+
main_ver,
120+
recovery_ver,
121+
fitc_ver,
122+
})
83123
}
84124
}
85125
}

0 commit comments

Comments
 (0)