Skip to content

lib: add support for generic decode definitions #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2025
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
11 changes: 10 additions & 1 deletion lib/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
// SPDX-License-Identifier: MIT

#[cfg(feature = "collateral_manager")]
use crate::collateral::{ItemPath, PVSS};
use crate::{
collateral::{ItemPath, PVSS},
header::Version,
};
#[cfg(not(feature = "std"))]
use alloc::{fmt, str};
#[cfg(not(feature = "std"))]
Expand All @@ -18,6 +21,8 @@ pub enum Error {
NoCrashLogFound,
#[cfg(feature = "collateral_manager")]
MissingCollateral(PVSS, ItemPath),
#[cfg(feature = "collateral_manager")]
MissingDecodeDefinitions(Version),
InvalidBootErrorRecordRegion,
InvalidHeader,
InvalidHeaderType(u16),
Expand Down Expand Up @@ -46,6 +51,10 @@ impl fmt::Display for Error {
Error::MissingCollateral(pvss, item) => {
write!(f, "Missing {item} collateral file for {pvss}")
}
#[cfg(feature = "collateral_manager")]
Error::MissingDecodeDefinitions(version) => {
write!(f, "Missing decode definitions for {version}")
}
Error::InvalidBootErrorRecordRegion => write!(f, "Invalid Boot Error Record region"),
Error::InvalidHeader => write!(f, "Invalid Crash Log Header"),
Error::InvalidHeaderType(ht) => write!(f, "Invalid Crash Log Header Type: {ht}"),
Expand Down
62 changes: 41 additions & 21 deletions lib/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::node::Node;
use alloc::{
fmt, format,
string::{String, ToString},
vec,
vec::Vec,
};
#[cfg(feature = "std")]
Expand Down Expand Up @@ -307,37 +308,30 @@ impl Header {

/// Returns the type of the record.
pub fn record_type(&self) -> Result<&'static str, Error> {
Ok(match self.version.record_type {
record_types::PMC => "PMC",
record_types::PMC_FW_TRACE => "PMC_FW_Trace",
record_types::PUNIT => "Punit",
record_types::PCORE => "PCORE",
record_types::ECORE => "ECORE",
record_types::UNCORE => "UNCORE",
record_types::PMC_TRACE => "PMC_TRACE",
record_types::TCSS => "TCSS",
record_types::PMC_RST => "PMC_RST",
record_types::PCODE => "PCODE",
record_types::CRASHLOG_AGENT => "CRASHLOG_AGENT",
record_types::BOX => "BOX",
record_types::MCA => "MCA",
rt => return Err(Error::InvalidRecordType(rt)),
})
self.version.record_type_as_str()
}

#[cfg(feature = "collateral_manager")]
pub(super) fn decode_definitions_path<T: CollateralTree>(
pub(super) fn decode_definitions_paths<T: CollateralTree>(
&self,
cm: &CollateralManager<T>,
) -> Result<ItemPath, Error> {
) -> Result<Vec<ItemPath>, Error> {
let record_type = self.record_type()?;
let revision = self.revision().to_string();

Ok(if let Some(die) = self.die(cm) {
let die_id = die.trim_end_matches(char::is_numeric);
ItemPath::new(["decode-defs", record_type, die_id, &revision])
vec![ItemPath::new([
"decode-defs",
record_type,
die_id,
&revision,
])]
} else {
ItemPath::new(["decode-defs", record_type, &revision])
vec![
ItemPath::new(["decode-defs", record_type, &revision]),
ItemPath::new(["decode-defs", record_type, "all"]),
]
})
}

Expand Down Expand Up @@ -403,7 +397,7 @@ impl fmt::Display for Header {
}

/// Version of the Crash Log record
#[derive(Debug, Default)]
#[derive(Clone, Debug, Default)]
pub struct Version {
/// Revision of the record
pub revision: u32,
Expand Down Expand Up @@ -448,6 +442,32 @@ impl Version {
| ((self.header_type as u32) << 8)
| self.revision
}

fn record_type_as_str(&self) -> Result<&'static str, Error> {
Ok(match self.record_type {
record_types::PMC => "PMC",
record_types::PMC_FW_TRACE => "PMC_FW_Trace",
record_types::PUNIT => "Punit",
record_types::PCORE => "PCORE",
record_types::ECORE => "ECORE",
record_types::UNCORE => "UNCORE",
record_types::PMC_TRACE => "PMC_TRACE",
record_types::TCSS => "TCSS",
record_types::PMC_RST => "PMC_RST",
record_types::PCODE => "PCODE",
record_types::CRASHLOG_AGENT => "CRASHLOG_AGENT",
record_types::BOX => "BOX",
record_types::MCA => "MCA",
rt => return Err(Error::InvalidRecordType(rt)),
})
}
}

impl fmt::Display for Version {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let record_type = self.record_type_as_str().unwrap_or("UNKNOWN");
write!(f, "{} revision {}", record_type, self.revision)
}
}

/// Size of the Crash Log record
Expand Down
14 changes: 11 additions & 3 deletions lib/src/record/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,17 @@ impl Record {
decode_def: &str,
offset: usize,
) -> Result<Node, Error> {
let mut path = self.header.decode_definitions_path(cm)?;
path.push(decode_def);
self.decode_with_csv(cm.get_item_with_header(&self.header, path)?, offset)
let paths = self.header.decode_definitions_paths(cm)?;

for mut path in paths {
path.push(decode_def);
let Ok(layout) = cm.get_item_with_header(&self.header, path) else {
continue;
};
return self.decode_with_csv(layout, offset);
}

Err(Error::MissingDecodeDefinitions(self.header.version.clone()))
}

#[cfg(feature = "collateral_manager")]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name;offset;size;description;bitfield
mca;0;32;;0
mca.foo;0;32;;0
50 changes: 50 additions & 0 deletions lib/tests/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: MIT
#![feature(assert_matches)]

use intel_crashlog::header::{RecordSize, Version};
use intel_crashlog::prelude::*;
use std::assert_matches::assert_matches;
use std::fs;
Expand Down Expand Up @@ -87,6 +88,55 @@ fn decode() {
assert_eq!(version.kind, NodeType::Field { value: 1 });
}

#[test]
fn decode_generic() {
let record = Record {
header: Header {
version: Version {
record_type: 0x3e,
product_id: 0x7a,
revision: 42,
..Default::default()
},
size: RecordSize {
record_size: 1,
..Default::default()
},
..Default::default()
},
data: vec![0x42, 0, 0, 0],
};

let mut cm = CollateralManager::file_system_tree(Path::new(COLLATERAL_TREE_PATH)).unwrap();
let root = record.decode(&mut cm).unwrap();
let foo = root.get_by_path("mca.foo").unwrap();
assert_eq!(foo.kind, NodeType::Field { value: 0x42 });
}

#[test]
fn decode_missing_decode_defs() {
let record = Record {
header: Header {
version: Version {
record_type: 0x3e,
product_id: 0x1c,
revision: 42,
..Default::default()
},
size: RecordSize {
record_size: 1,
..Default::default()
},
..Default::default()
},
data: vec![0x42],
};

let mut cm = CollateralManager::file_system_tree(Path::new(COLLATERAL_TREE_PATH)).unwrap();
let root = record.decode(&mut cm);
assert_matches!(root, Err(Error::MissingDecodeDefinitions(_)));
}

#[test]
fn header_type6_decode() {
let mut cm = CollateralManager::file_system_tree(Path::new(COLLATERAL_TREE_PATH)).unwrap();
Expand Down