Skip to content
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
32 changes: 18 additions & 14 deletions src/dir/gen2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ pub struct Header {
#[derive(Serialize, Deserialize, Clone, Debug)]
#[repr(C)]
pub struct Directory {
pub manifest: Manifest,
pub manifest: (Manifest, Vec<u8>),
pub header: Header,
pub modules: Vec<Module>,
pub offset: usize,
Expand All @@ -210,7 +210,7 @@ impl Display for Directory {
let n = &self.name;
let o = self.offset;
let s = self.size;
let m = self.manifest;
let (m, _) = self.manifest;
write!(f, "{n} @ {o:08x}, {s} bytes, {m}")
}
}
Expand All @@ -222,32 +222,36 @@ impl Directory {
let Ok(manifest) = Manifest::new(data) else {
return Err("cannot parse Gen 2 directory manifest".to_string());
};
let count = manifest.header.entries as usize;
let d = &data[man::MANIFEST_SIZE..];
let Ok((header, _)) = Header::read_from_prefix(d) else {

let data_len = manifest.data_len();
let mdata = &data[man::MANIFEST_SIZE..man::MANIFEST_SIZE + data_len];

let Ok((header, _)) = Header::read_from_prefix(mdata) else {
return Err("cannot parse ME FW Gen 2 directory header".to_string());
};
let pos = man::MANIFEST_SIZE + HEADER_SIZE;
let m = &data[pos..pos + 4];
let name = match from_utf8(&header.name) {
Ok(n) => n.trim_end_matches('\0').to_string(),
Err(_) => format!("{:02x?}", header.name),
};

// Check magic bytes of first entry.
let pos = HEADER_SIZE;
let m = &mdata[pos..pos + 4];
if !m.eq(MODULE_MAGIC_BYTES) {
return Err(format!(
"entry magic not found, got {m:02x?}, wanted {MODULE_MAGIC_BYTES:02x?} ({MODULE_MAGIC})"
));
}

let slice = &data[pos..];
// Parse the entries themselves.
let slice = &mdata[pos..];
let count = manifest.header.entries as usize;
let Ok((r, _)) = Ref::<_, [Entry]>::from_prefix_with_elems(slice, count) else {
return Err(format!(
"cannot parse ME FW Gen 2 directory entries @ {:08x}",
pos
));
};
let entries = r.to_vec();
let name = match from_utf8(&header.name) {
Ok(n) => n.trim_end_matches('\0').to_string(),
Err(_) => format!("{:02x?}", header.name),
};

// Check for consistency and wrap entries with additional information.
let modules = entries
Expand Down Expand Up @@ -295,7 +299,7 @@ impl Directory {

let size = data.len();
Ok(Self {
manifest,
manifest: (manifest, mdata.to_vec()),
header,
modules,
offset,
Expand Down
16 changes: 13 additions & 3 deletions src/dir/gen3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl Display for CPDEntry {
#[repr(C)]
pub struct CodePartitionDirectory {
pub header: CPDHeader,
pub manifest: Result<Manifest, String>,
pub manifest: Result<(Manifest, Vec<u8>), String>,
pub entries: Vec<CPDEntry>,
pub offset: usize,
pub size: usize,
Expand All @@ -125,7 +125,7 @@ impl Display for CodePartitionDirectory {
let n = &self.name;
let l1 = format!("{n} @ {o:08x}, checksum or version: {checksum:08x}");
let l2 = match &self.manifest {
Ok(m) => {
Ok((m, _)) => {
let h = stringify_vec(m.hash_key());
let m = format!("{m}");
let kh = format!("Key hash: {h}");
Expand Down Expand Up @@ -173,7 +173,17 @@ impl CodePartitionDirectory {
let manifest = {
if let Some(e) = entries.iter().find(|e| e.name() == manifest_name) {
let o = e.flags_and_offset.offset() as usize;
Manifest::new(&data[o..])
let end = o + e.size as usize;
match Manifest::new(&data[o..end]) {
Ok(m) => {
// The manifest carries additional data after its header
// and key material.
let header_len = m.header_len();
let mdata = data[o + header_len..end].to_vec();
Ok((m, mdata))
}
Err(e) => Err(e),
}
} else {
Err("no manifest found".to_string())
}
Expand Down
5 changes: 3 additions & 2 deletions src/part/partitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ impl Partitions {
if let Some(Gen2Partition::Dir(d)) =
parts.iter().find(|p| matches!(p, Gen2Partition::Dir(_)))
{
Some(d.dir.manifest.header.version)
let (m, _) = d.dir.manifest;
Some(m.header.version)
} else {
None
}
Expand All @@ -88,7 +89,7 @@ impl Partitions {
if let Some(Gen3Partition::Dir(d)) =
parts.iter().find(|p| matches!(p, Gen3Partition::Dir(_)))
{
if let Ok(m) = d.cpd.manifest {
if let Ok((m, _)) = d.cpd.manifest {
Some(m.header.version)
} else {
None
Expand Down