Skip to content

Commit aed51bb

Browse files
committed
factor out helper to obtain last partition
Also handle errors if none can be found. Signed-off-by: Daniel Maslowski <[email protected]>
1 parent 6d3e839 commit aed51bb

File tree

3 files changed

+60
-41
lines changed

3 files changed

+60
-41
lines changed

src/clean.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,12 @@ pub fn clean(
5151
warn!("Could not relocate: {e}")
5252
}
5353
}
54-
let cleaned = new_me.fpt_area.to_vec();
55-
let size = cleaned.len();
56-
data[me.base..me.base + size].copy_from_slice(&cleaned);
57-
Ok(data.to_vec())
54+
match new_me.fpt_area.to_vec() {
55+
Ok(cleaned) => {
56+
let size = cleaned.len();
57+
data[me.base..me.base + size].copy_from_slice(&cleaned);
58+
Ok(data.to_vec())
59+
}
60+
Err(e) => Err(e),
61+
}
5862
}

src/me.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,17 @@ impl FPTArea {
128128
}
129129

130130
/// Clear out fully removable partitions and adjust FPT
131-
pub fn to_vec(&self) -> Vec<u8> {
131+
pub fn to_vec(&self) -> Result<Vec<u8>, String> {
132132
let debug = true;
133133

134134
if debug {
135135
println!("Recreate ME region from components");
136136
}
137137

138-
let mut res = self.partitions.to_vec();
138+
let mut res = match self.partitions.to_vec() {
139+
Ok(r) => r,
140+
Err(e) => return Err(e),
141+
};
139142
if debug {
140143
println!(" Minimum size: {:08x}", res.len());
141144
}
@@ -168,7 +171,7 @@ impl FPTArea {
168171
}
169172
res[..s].copy_from_slice(&raw_fpt);
170173

171-
res
174+
Ok(res)
172175
}
173176
}
174177

src/part/partitions.rs

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,28 @@ impl Partitions {
185185
Ok(())
186186
}
187187

188-
pub fn to_vec(&self) -> Vec<u8> {
188+
/// Get the last FPT entry based on offsets, i.e., the one with the highest
189+
/// offset. Use its offset and size and round to next 4K to calculate the
190+
/// smallest possible ME region.
191+
pub fn last_entry(&self) -> Option<FPTEntry> {
192+
let sorted_parts = &self.get_sorted();
193+
// NOTE: We need to filter out NVRAM partitions, which have an offset of
194+
// 0xffff_ffff.
195+
if let Some(last) = &sorted_parts
196+
.into_iter()
197+
.filter(|p| {
198+
let f = p.entry().flags;
199+
f.kind() != PartitionKind::NVRAM
200+
})
201+
.last()
202+
{
203+
Some(last.entry().clone())
204+
} else {
205+
None
206+
}
207+
}
208+
209+
pub fn to_vec(&self) -> Result<Vec<u8>, String> {
189210
use log::debug;
190211
fn copy_parts(parts: &Vec<&dyn Partition>, data: &mut Vec<u8>) {
191212
for p in parts {
@@ -208,40 +229,31 @@ impl Partitions {
208229
}
209230
}
210231

211-
let sorted_parts = &self.get_sorted();
212-
213232
// This gets us the smallest possible slice to copy into.
214-
// NOTE: We need to filter out NVRAM partitions, which have an offset of
215-
// 0xffff_ffff.
216-
let last = &sorted_parts
217-
.into_iter()
218-
.filter(|p| {
219-
let f = p.entry().flags;
220-
f.kind() != PartitionKind::NVRAM
221-
})
222-
.last()
223-
.unwrap()
224-
.entry();
225-
let o = last.offset();
226-
let size = o + last.size();
227-
debug!("Last partition @ {o:08x}; final size: {size:08x}");
228-
let mut data = vec![EMPTY; size];
229-
230-
match sorted_parts {
231-
Partitions::Gen2(parts) => copy_parts(
232-
&parts.iter().map(|p| p as &dyn Partition).collect(),
233-
&mut data,
234-
),
235-
Partitions::Gen3(parts) => copy_parts(
236-
&parts.iter().map(|p| p as &dyn Partition).collect(),
237-
&mut data,
238-
),
239-
Partitions::Unknown(parts) => copy_parts(
240-
&parts.iter().map(|p| p as &dyn Partition).collect(),
241-
&mut data,
242-
),
243-
};
233+
if let Some(last) = self.last_entry() {
234+
let o = last.offset();
235+
let size = o + last.size();
236+
debug!("Last partition @ {o:08x}; final size: {size:08x}");
237+
let mut data = vec![EMPTY; size];
244238

245-
data
239+
match &self {
240+
Partitions::Gen2(parts) => copy_parts(
241+
&parts.iter().map(|p| p as &dyn Partition).collect(),
242+
&mut data,
243+
),
244+
Partitions::Gen3(parts) => copy_parts(
245+
&parts.iter().map(|p| p as &dyn Partition).collect(),
246+
&mut data,
247+
),
248+
Partitions::Unknown(parts) => copy_parts(
249+
&parts.iter().map(|p| p as &dyn Partition).collect(),
250+
&mut data,
251+
),
252+
};
253+
254+
Ok(data)
255+
} else {
256+
Err("no partition entries found".into())
257+
}
246258
}
247259
}

0 commit comments

Comments
 (0)