-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgen3.rs
More file actions
206 lines (196 loc) · 6.46 KB
/
gen3.rs
File metadata and controls
206 lines (196 loc) · 6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use serde::{Deserialize, Serialize};
use crate::dir::{
gen3::{ALWAYS_RETAIN, CPD_MAGIC_BYTES, CodePartitionDirectory},
man::Manifest,
};
use crate::dump48;
use crate::part::{
fpt::{DIR_PARTS, FPT, FPTEntry, FS_PARTS, FTPR},
part::{
ClearOptions, Partition, UnknownOrMalformedPartition, dir_clean, retain, strs_to_strings,
},
};
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct CPDPartition {
pub entry: FPTEntry,
pub data: Vec<u8>,
pub cpd: CodePartitionDirectory,
}
impl CPDPartition {
pub fn check_signature(&self) -> Result<(), String> {
if let Ok(m) = &self.cpd.manifest {
if m.verify() {
return Ok(());
} else {
return Err("hash mismatch".into());
}
} else {
Err("no manifest found".into())
}
}
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct DataPartition {
pub entry: FPTEntry,
pub data: Vec<u8>,
pub manifest: Manifest,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub enum Gen3Partition {
Dir(CPDPartition),
Data(DataPartition),
MalformedOrUnknown(UnknownOrMalformedPartition),
}
impl Partition for Gen3Partition {
fn data(&self) -> &Vec<u8> {
match self {
Self::Dir(d) => &d.data,
Self::Data(d) => &d.data,
Self::MalformedOrUnknown(d) => &d.data,
}
}
fn entry(&self) -> &FPTEntry {
match self {
Self::Dir(d) => &d.entry,
Self::Data(d) => &d.entry,
Self::MalformedOrUnknown(d) => &d.entry,
}
}
fn set_data(&mut self, data: Vec<u8>) {
match self {
Self::Dir(d) => d.data = data,
Self::Data(d) => d.data = data,
Self::MalformedOrUnknown(d) => d.data = data,
}
}
fn set_entry(&mut self, entry: FPTEntry) {
match self {
Self::Dir(d) => d.entry = entry,
Self::Data(d) => d.entry = entry,
Self::MalformedOrUnknown(d) => d.entry = entry,
}
}
}
impl Gen3Partition {
pub fn parse(data: &[u8], entry: FPTEntry, debug: bool) -> Self {
let o = entry.offset();
let n = entry.name();
let n = n.as_str();
let data = data.to_vec();
match entry {
_ if data.len() > 4 && &data[..4] == CPD_MAGIC_BYTES => {
if !DIR_PARTS.contains(&n) && debug {
println!("Unknown CPD {n} @ 0x{o:08x}");
}
match CodePartitionDirectory::new(&data, o) {
Ok(cpd) => Gen3Partition::Dir(CPDPartition { entry, data, cpd }),
Err(e) => {
let note =
format!("Expected CPD {n} @ 0x{o:08x}, but could not parse it: {e}");
Gen3Partition::MalformedOrUnknown(UnknownOrMalformedPartition {
entry,
data,
note,
})
}
}
}
_ if FS_PARTS.contains(&n) => {
// TODO: parse MFS
let note = "file system parsing not yet implemented".to_string();
Gen3Partition::MalformedOrUnknown(UnknownOrMalformedPartition { entry, data, note })
}
_ => {
if let Ok(manifest) = Manifest::new(&data) {
if debug {
println!("Manifest found in {n} @ 0x{o:08x}: {manifest}");
}
return Gen3Partition::Data(DataPartition {
entry,
data,
manifest,
});
}
let note = format!("Cannot (yet) parse {n} @ 0x{o:08x}, skipping...");
if debug {
println!("{note}");
dump48(&data);
}
Gen3Partition::MalformedOrUnknown(UnknownOrMalformedPartition { entry, data, note })
}
}
}
pub fn relocate(&mut self, offset: u32) -> Result<(), String> {
match self {
Self::Dir(p) => p.entry.set_offset(offset),
Self::Data(p) => p.entry.set_offset(offset),
Self::MalformedOrUnknown(p) => p.entry.set_offset(offset),
}
Ok(())
}
}
pub fn parse(fpt: &FPT, data: &[u8], debug: bool) -> Vec<Gen3Partition> {
let parts = fpt
.entries
.iter()
.map(|e| {
let offset = e.offset();
let size = e.size as usize;
let end = offset + size;
let l = data.len();
if end > l {
let note = format!("{offset:08x}..{end:08x} out of bounds ({l:08x})");
Gen3Partition::MalformedOrUnknown(UnknownOrMalformedPartition {
entry: *e,
data: vec![],
note,
})
} else {
// NOTE: We pass the exact data slice to be kept by
// the partition besides its table entry metadata.
Gen3Partition::parse(&data[offset..end], *e, debug)
}
})
.collect();
parts
}
pub fn clean(parts: &Vec<Gen3Partition>, options: &ClearOptions) -> Vec<Gen3Partition> {
use log::info;
// Step 1: Reduce down to the partitions to be kept, i.e., non-removable
// ones.
let mut reduced = parts
.iter()
.filter(|p| {
let e = p.entry();
let n = e.name();
if retain(n, options) {
info!("Retain {e}");
true
} else {
info!("Remove {e}");
false
}
})
.map(|p| p.clone())
.collect::<Vec<Gen3Partition>>();
if options.keep_modules {
return reduced;
}
// Step 2: Clean the FTPR directory, retaining non-removable modules.
if let Some(p) = reduced.iter_mut().find(|p| p.entry().name() == FTPR) {
let offset = p.entry().offset();
info!("FTPR @ {offset:08x}");
// TODO: Extend with user-provided list
let retention_list = strs_to_strings(ALWAYS_RETAIN);
let mut cleaned = p.data().clone();
match &p {
Gen3Partition::Dir(dir) => {
dir_clean(&dir.cpd, &retention_list, &mut cleaned);
}
_ => {}
};
p.set_data(cleaned);
}
// Step 3: Profit.
reduced
}