Skip to content

Support dumping Turin data structures and Milan data structures at th… #136

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
Sep 18, 2024
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
12 changes: 3 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "amd-apcb"
version = "0.3.3"
version = "0.4.0"
authors = ["Oxide Computer"]
edition = "2021"

Expand All @@ -12,14 +12,8 @@ byteorder = { version = "1.4.3", default-features = false }
four-cc = { version = "0.3.0", default-features = false }
memoffset = "0.5"
modular-bitfield = { version = "0.11.2", default-features = false }
#
# In order to use macros as discriminants in enums that make use of derive
# macros (e.g., AsBytes, FromPrimitive), we need the syn crate to have "full"
# enabled. The easiest way to do this is to use num-derive's "full-syntax",
# which passes "full" through to syn.
#
num-derive = { version = "0.3.0", features = [ "full-syntax" ] }
num-traits = { version = "0.2.12", default-features = false }
num-derive = { version = "0.4.2", features = [ ] }
num-traits = { version = "0.2.19", default-features = false }
paste = "1.0"
pre = { version = "0.2.1", default-features = false, features = [] }
static_assertions = "1.1.0"
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "nightly-2024-06-19"
components = [ "rustfmt", "rust-src" ]
components = [ "rustfmt", "rust-src", "rust-analyzer" ]
69 changes: 53 additions & 16 deletions src/apcb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use crate::types::{Error, FileSystemError, PtrMut, Result};
use crate::types::{ApcbContext, Error, FileSystemError, PtrMut, Result};

use crate::entry::EntryItemBody;
use crate::group::{GroupItem, GroupMutItem};
Expand Down Expand Up @@ -50,29 +50,41 @@
#[derive(Clone)]
pub struct ApcbIoOptions {
pub check_checksum: bool,
pub context: ApcbContext,
}

impl Default for ApcbIoOptions {
fn default() -> Self {
Self { check_checksum: true }
Self { check_checksum: true, context: ApcbContext::default() }
}
}

impl ApcbIoOptions {
pub fn builder() -> Self {
Self::default()
}
pub fn check_checksum(&self) -> bool {
self.check_checksum
}
pub fn context(&self) -> ApcbContext {
self.context
}
pub fn with_check_checksum(&mut self, value: bool) -> &mut Self {
self.check_checksum = value;
self
}
pub fn with_context(&mut self, value: ApcbContext) -> &mut Self {
self.context = value;
self
}
pub fn build(&self) -> Self {
self.clone()
}
}

#[cfg_attr(feature = "std", derive(Clone))]
pub struct Apcb<'a> {
context: ApcbContext,
used_size: usize,
pub backing_store: PtrMut<'a, [u8]>,
}
Expand All @@ -83,7 +95,12 @@
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct SerdeApcb {
/// This field is out-of-band information. At the cost of slight redundancy
/// in user config and another extra field that isn't actually in the blob,
/// we can actually handle the out-of-band information quite natually.
#[cfg_attr(feature = "serde", serde(default))]
pub context: ApcbContext,
pub version: String,

Check warning on line 103 in src/apcb.rs

View workflow job for this annotation

GitHub Actions / clippy

field `version` is never read

warning: field `version` is never read --> src/apcb.rs:103:9 | 97 | pub struct SerdeApcb { | --------- field in this struct ... 103 | pub version: String, | ^^^^^^^ | = note: `#[warn(dead_code)]` on by default
pub header: V2_HEADER,
pub v3_header_ext: Option<V3_HEADER_EXT>,
pub groups: Vec<SerdeGroupItem>,
Expand All @@ -107,14 +124,21 @@
}

#[cfg(feature = "serde")]
use core::convert::TryFrom;

Check warning on line 127 in src/apcb.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `core::convert::TryFrom`

warning: unused import: `core::convert::TryFrom` --> src/apcb.rs:127:5 | 127 | use core::convert::TryFrom; | ^^^^^^^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default

#[cfg(feature = "serde")]
impl<'a> TryFrom<SerdeApcb> for Apcb<'a> {
type Error = Error;
impl<'a> Apcb<'a> {
pub fn context(&self) -> ApcbContext {
self.context
}
// type Error = Error;
fn try_from(serde_apcb: SerdeApcb) -> Result<Self> {
let buf = Cow::from(vec![0xFFu8; Self::MAX_SIZE]);
let mut apcb = Apcb::create(buf, 42, &ApcbIoOptions::default())?;
let mut apcb = Apcb::create(
buf,
42,
&ApcbIoOptions::default().with_context(serde_apcb.context).build(),
)?;
*apcb.header_mut()? = serde_apcb.header;
match serde_apcb.v3_header_ext {
Some(v3) => {
Expand Down Expand Up @@ -204,6 +228,7 @@
}
}

/// Note: Caller should probably verify sanity of context() afterwards
#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for Apcb<'_> {
fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
Expand All @@ -217,11 +242,13 @@
}

pub struct ApcbIterMut<'a> {
context: ApcbContext,
buf: &'a mut [u8],
remaining_used_size: usize,
}

pub struct ApcbIter<'a> {
context: ApcbContext,
buf: &'a [u8],
remaining_used_size: usize,
}
Expand All @@ -230,7 +257,10 @@
/// It's useful to have some way of NOT mutating self.buf. This is what
/// this function does. Note: The caller needs to manually decrease
/// remaining_used_size for each call if desired.
fn next_item<'b>(buf: &mut &'b mut [u8]) -> Result<GroupMutItem<'b>> {
fn next_item<'b>(
context: ApcbContext,
buf: &mut &'b mut [u8],
) -> Result<GroupMutItem<'b>> {
if buf.is_empty() {
return Err(Error::FileSystem(
FileSystemError::InconsistentHeader,
Expand All @@ -256,7 +286,7 @@
))?;
let body_len = body.len();

Ok(GroupMutItem { header, buf: body, used_size: body_len })
Ok(GroupMutItem { context, header, buf: body, used_size: body_len })
}

/// Moves the point to the group with the given GROUP_ID. Returns (offset,
Expand All @@ -273,12 +303,12 @@
if buf.is_empty() {
break;
}
let group = ApcbIterMut::next_item(&mut buf)?;
let group = ApcbIterMut::next_item(self.context, &mut buf)?;
let group_size = group.header.group_size.get();
if group.header.group_id.get() == group_id {
return Ok((offset, group_size as usize));
}
let group = ApcbIterMut::next_item(&mut self.buf)?;
let group = ApcbIterMut::next_item(self.context, &mut self.buf)?;
let group_size = group.header.group_size.get() as usize;
offset = offset
.checked_add(group_size)
Expand All @@ -295,7 +325,7 @@

pub(crate) fn next1(&mut self) -> Result<GroupMutItem<'a>> {
assert!(self.remaining_used_size != 0, "Internal error");
let item = Self::next_item(&mut self.buf)?;
let item = Self::next_item(self.context, &mut self.buf)?;
let group_size = item.header.group_size.get() as usize;
if group_size <= self.remaining_used_size {
self.remaining_used_size -= group_size;
Expand Down Expand Up @@ -324,7 +354,10 @@
/// It's useful to have some way of NOT mutating self.buf. This is what
/// this function does. Note: The caller needs to manually decrease
/// remaining_used_size for each call if desired.
fn next_item<'b>(buf: &mut &'b [u8]) -> Result<GroupItem<'b>> {
fn next_item<'b>(
context: ApcbContext,
buf: &mut &'b [u8],
) -> Result<GroupItem<'b>> {
if buf.is_empty() {
return Err(Error::FileSystem(
FileSystemError::InconsistentHeader,
Expand All @@ -351,12 +384,12 @@

let body_len = body.len();

Ok(GroupItem { header, buf: body, used_size: body_len })
Ok(GroupItem { context, header, buf: body, used_size: body_len })
}

pub(crate) fn next1(&mut self) -> Result<GroupItem<'a>> {
assert!(self.remaining_used_size != 0, "Internal error");
let item = Self::next_item(&mut self.buf)?;
let item = Self::next_item(self.context, &mut self.buf)?;
let group_size = item.header.group_size.get() as usize;
if group_size <= self.remaining_used_size {
self.remaining_used_size -= group_size;
Expand Down Expand Up @@ -400,7 +433,7 @@
const ROME_VERSION: u16 = 0x30;
const V3_HEADER_EXT_SIZE: usize =
size_of::<V2_HEADER>() + size_of::<V3_HEADER_EXT>();
pub const MAX_SIZE: usize = 0x6500;
pub const MAX_SIZE: usize = 0x8000;

pub fn header(&self) -> Result<LayoutVerified<&[u8], V2_HEADER>> {
LayoutVerified::<&[u8], V2_HEADER>::new_unaligned_from_prefix(
Expand Down Expand Up @@ -509,6 +542,7 @@

pub fn groups(&self) -> Result<ApcbIter<'_>> {
Ok(ApcbIter {
context: self.context,
buf: self.beginning_of_groups()?,
remaining_used_size: self.used_size,
})
Expand All @@ -529,6 +563,7 @@
pub fn groups_mut(&mut self) -> Result<ApcbIterMut<'_>> {
let used_size = self.used_size;
Ok(ApcbIterMut {
context: self.context,
buf: &mut *self.beginning_of_groups_mut()?,
remaining_used_size: used_size,
})
Expand Down Expand Up @@ -1030,6 +1065,7 @@
signature: [u8; 4],
) -> Result<GroupMutItem<'_>> {
// TODO: insert sorted.
let context = self.context;

if !match group_id {
GroupId::Psp => signature == *b"PSPG",
Expand Down Expand Up @@ -1090,7 +1126,7 @@
))?;
let body_len = body.len();

Ok(GroupMutItem { header, buf: body, used_size: body_len })
Ok(GroupMutItem { context, header, buf: body, used_size: body_len })
}

pub(crate) fn calculate_checksum(
Expand Down Expand Up @@ -1263,7 +1299,8 @@
));
}
}
let result = Self { backing_store: bs, used_size };
let result =
Self { context: options.context(), backing_store: bs, used_size };

match result.groups()?.validate() {
Ok(_) => {}
Expand Down
50 changes: 40 additions & 10 deletions src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
};
use crate::ondisk::{Parameters, ParametersIter};
use crate::tokens_entry::TokensEntryBodyItem;
use crate::types::{Error, FileSystemError, Result};
use crate::types::{
ApcbContext, Error, FileSystemError, MemDfeSearchVersion, Result,
};
use core::marker::PhantomData;
use core::mem::size_of;
use num_traits::FromPrimitive;
Expand Down Expand Up @@ -46,6 +48,7 @@
pub(crate) fn from_slice(
header: &ENTRY_HEADER,
b: &'a mut [u8],
context: ApcbContext,
) -> Result<EntryItemBody<&'a mut [u8]>> {
let context_type = ContextType::from_u8(header.context_type).ok_or(
Error::FileSystem(
Expand All @@ -66,7 +69,7 @@
ContextType::Tokens => {
let used_size = b.len();
Ok(Self::Tokens(TokensEntryBodyItem::<&mut [u8]>::new(
header, b, used_size,
header, b, used_size, context,
)?))
}
ContextType::Parameters => Err(Error::EntryTypeMismatch),
Expand All @@ -78,6 +81,7 @@
pub(crate) fn from_slice(
header: &ENTRY_HEADER,
b: &'a [u8],
context: ApcbContext,
) -> Result<EntryItemBody<&'a [u8]>> {
let context_type = ContextType::from_u8(header.context_type).ok_or(
Error::FileSystem(
Expand All @@ -98,7 +102,7 @@
ContextType::Tokens => {
let used_size = b.len();
Ok(Self::Tokens(TokensEntryBodyItem::<&[u8]>::new(
header, b, used_size,
header, b, used_size, context,
)?))
}
ContextType::Parameters => Err(Error::EntryTypeMismatch),
Expand All @@ -117,6 +121,7 @@

#[derive(Debug)]
pub struct EntryMutItem<'a> {
pub(crate) context: ApcbContext,

Check warning on line 124 in src/entry.rs

View workflow job for this annotation

GitHub Actions / clippy

field `context` is never read

warning: field `context` is never read --> src/entry.rs:124:16 | 123 | pub struct EntryMutItem<'a> { | ------------ field in this struct 124 | pub(crate) context: ApcbContext, | ^^^^^^^ | = note: `EntryMutItem` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
pub(crate) header: &'a mut ENTRY_HEADER,
pub body: EntryItemBody<&'a mut [u8]>,
}
Expand Down Expand Up @@ -420,6 +425,7 @@

#[derive(Clone)]
pub struct EntryItem<'a> {
pub(crate) context: ApcbContext,
pub(crate) header: &'a ENTRY_HEADER,
pub body: EntryItemBody<&'a [u8]>,
}
Expand Down Expand Up @@ -677,12 +683,6 @@
} else if let Some(s) = self.body_as_struct_array::<memory::Ddr5CaPinMapElement>() {
let v = s.iter().collect::<Vec<_>>();
state.serialize_field("Ddr5CaPinMapElement", &v)?;
// } else if let Some(s) = self.body_as_struct_array::<memory::MemDfeSearchElement32>() { // UH OH
// let v = s.iter().collect::<Vec<_>>();
// state.serialize_field("MemDfeSearchElement32", &v)?;
} else if let Some(s) = self.body_as_struct_array::<memory::MemDfeSearchElement36>() {
let v = s.iter().collect::<Vec<_>>();
state.serialize_field("MemDfeSearchElement36", &v)?;
} else if let Some(s) = self.body_as_struct_array::<memory::DdrDqPinMapElement>() {
let v = s.iter().collect::<Vec<_>>();
state.serialize_field("DdrDqPinMapElement", &v)?;
Expand Down Expand Up @@ -747,7 +747,27 @@
let v = parameters.collect::<Vec<_>>();
state.serialize_field("parameters", &v)?;
} else {
state.serialize_field("struct_body", &buf)?;
match self.context.mem_dfe_search_version() {
Some(MemDfeSearchVersion::Genoa2) => {
if let Some(s) = self.body_as_struct_array::<memory::MemDfeSearchElement32>() {
let v = s.iter().collect::<Vec<_>>();
state.serialize_field("MemDfeSearchElement32", &v)?;
} else {
state.serialize_field("struct_body", &buf)?;
}
},
Some(MemDfeSearchVersion::Turin1) => {
if let Some(s) = self.body_as_struct_array::<memory::MemDfeSearchElement36>() {
let v = s.iter().collect::<Vec<_>>();
state.serialize_field("MemDfeSearchElement36", &v)?;
} else {
state.serialize_field("struct_body", &buf)?;
}
}
_ => {
state.serialize_field("struct_body", &buf)?;
}
}
}
}
}
Expand Down Expand Up @@ -1223,12 +1243,22 @@
)?;
}
Field::MemDfeSearchElement32 => {
// TODO: maybe also sanity-check
// context.mem_dfe_search_version()
// Note: context.mem_dfe_search_version is optional.
// If it's not specified, it deserialization should
// still work.
struct_vec_to_body::<
memory::MemDfeSearchElement32,
V,
>(&mut body, &mut map)?;
}
Field::MemDfeSearchElement36 => {
// TODO: maybe also sanity-check
// context.mem_dfe_search_version()
// Note: context.mem_dfe_search_version is optional.
// If it's not specified, it deserialization should
// still work.
struct_vec_to_body::<
memory::MemDfeSearchElement36,
V,
Expand Down
Loading
Loading