-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathprogramfile.rs
More file actions
286 lines (256 loc) · 8.94 KB
/
programfile.rs
File metadata and controls
286 lines (256 loc) · 8.94 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
use anyhow::{Ok, bail};
use indexmap::IndexMap;
use std::{
fs,
io::{Read, Seek, SeekFrom, Write},
path::Path,
};
use xmltree::{self, Element, XMLNode};
use qdl::{
firehose_checksum_storage, firehose_patch, firehose_program_storage, firehose_read_storage,
types::QdlChan,
};
use android_sparse_image::{
ChunkHeader, ChunkHeaderBytes, ChunkType, FILE_HEADER_BYTES_LEN, FileHeader, FileHeaderBytes,
};
fn parse_read_cmd<T: Read + Write + QdlChan>(
channel: &mut T,
out_dir: &Path,
attrs: &IndexMap<String, String>,
checksum_only: bool,
) -> anyhow::Result<()> {
let num_sectors = attrs
.get("num_partition_sectors")
.unwrap()
.parse::<usize>()
.unwrap();
let phys_part_idx = attrs
.get("physical_partition_number")
.unwrap()
.parse::<u8>()
.unwrap();
let start_sector = attrs.get("start_sector").unwrap().parse::<u32>().unwrap();
if checksum_only {
return firehose_checksum_storage(channel, num_sectors, phys_part_idx, start_sector);
}
if !attrs.contains_key("filename") {
bail!("Got '<read>' tag without a filename");
}
let mut outfile = fs::File::create(out_dir.join(attrs.get("filename").unwrap()))?;
firehose_read_storage(
channel,
&mut outfile,
num_sectors,
phys_part_idx,
start_sector,
)
}
fn parse_patch_cmd<T: Read + Write + QdlChan>(
channel: &mut T,
attrs: &IndexMap<String, String>,
verbose: bool,
) -> anyhow::Result<()> {
if let Some(filename) = attrs.get("filename") {
if filename != "DISK" && verbose {
println!("Skipping <patch> tag trying to alter {filename} on Host filesystem");
return Ok(());
}
} else {
bail!("Got '<patch>' tag without a filename");
}
let byte_off = attrs.get("byte_offset").unwrap().parse::<u64>().unwrap();
let phys_part_idx = attrs
.get("physical_partition_number")
.unwrap()
.parse::<u8>()
.unwrap();
let size = attrs.get("size_in_bytes").unwrap().parse::<u64>().unwrap();
let start_sector = attrs.get("start_sector").unwrap();
let val = attrs.get("value").unwrap();
firehose_patch(channel, byte_off, phys_part_idx, size, start_sector, val)
}
const BOOTABLE_PART_NAMES: [&str; 3] = ["xbl", "xbl_a", "sbl1"];
// TODO: readbackverify
fn parse_program_cmd<T: Read + Write + QdlChan>(
channel: &mut T,
program_file_dir: &Path,
attrs: &IndexMap<String, String>,
allow_missing_files: bool,
bootable_part_idx: &mut Option<u8>,
verbose: bool,
) -> anyhow::Result<()> {
let sector_size = attrs
.get("SECTOR_SIZE_IN_BYTES")
.unwrap()
.parse::<usize>()
.unwrap();
if sector_size != channel.fh_config().storage_sector_size {
bail!(
"Mismatch in storage sector size! Programfile requests {}",
sector_size
);
}
let num_sectors = attrs
.get("num_partition_sectors")
.unwrap()
.parse::<usize>()
.unwrap();
let phys_part_idx = attrs
.get("physical_partition_number")
.unwrap()
.parse::<u8>()
.unwrap();
let start_sector = attrs.get("start_sector").unwrap();
let file_sector_offset = attrs
.get("file_sector_offset")
.unwrap_or(&"".to_owned())
.parse::<u32>()
.unwrap_or(0);
let label = attrs.get("label").unwrap();
if num_sectors == 0 {
println!("Skipping 0-length entry for {label}");
return Ok(());
}
if BOOTABLE_PART_NAMES.contains(&&label[..]) {
*bootable_part_idx = Some(phys_part_idx);
}
let filename = attrs.get("filename").unwrap();
let file_path = program_file_dir.join(filename);
if allow_missing_files {
if filename.is_empty() {
if verbose {
println!("Skipping bogus entry for {label}");
}
return Ok(());
} else if !file_path.exists() {
if verbose {
println!("Skipping non-existent file {}", file_path.to_str().unwrap());
}
return Ok(());
}
}
let sparse = attrs
.get("sparse")
.unwrap_or(&"false".to_owned())
.parse::<bool>()
.unwrap_or(false);
let mut buf = fs::File::open(file_path)?;
if sparse {
let mut header_bytes: FileHeaderBytes = [0; FILE_HEADER_BYTES_LEN];
buf.read_exact(&mut header_bytes)?;
let header = FileHeader::from_bytes(&header_bytes)?;
let mut offset: usize = 0;
let start_sector = start_sector.parse::<usize>()?;
for index in 0..header.chunks {
let label_sparse = format!("{label}_{index}");
let mut chunk_bytes = ChunkHeaderBytes::default();
buf.read_exact(&mut chunk_bytes)?;
let chunk = ChunkHeader::from_bytes(&chunk_bytes)?;
let out_size = chunk.out_size(&header);
let num_sectors = out_size / sector_size;
let start_offset = start_sector + offset;
match chunk.chunk_type {
ChunkType::Raw => {
firehose_program_storage(
channel,
&mut buf,
&label_sparse,
num_sectors,
phys_part_idx,
start_offset.to_string().as_str(),
)?;
}
ChunkType::Fill => {
let mut fill_value = [0u8; 4];
buf.read_exact(&mut fill_value)?;
let mut fill_vec = Vec::<u8>::with_capacity(out_size);
for _ in 0..out_size / 4 {
fill_vec.extend_from_slice(&fill_value[..]);
}
firehose_program_storage(
channel,
&mut &fill_vec[..],
&label_sparse,
num_sectors,
phys_part_idx,
start_offset.to_string().as_str(),
)?;
}
ChunkType::DontCare => {
// Don't Care, skip
}
ChunkType::Crc32 => {
// Not supported, on qcom tools is ignored, seek if present
buf.seek_relative(4)?;
}
}
offset += out_size;
}
return Ok(());
}
buf.seek(SeekFrom::Current(
sector_size as i64 * file_sector_offset as i64,
))?;
firehose_program_storage(
channel,
&mut buf,
label,
num_sectors,
phys_part_idx,
start_sector,
)
}
// TODO: there's some funny optimizations to make here, such as OoO loading files into memory, or doing things while we're waiting on the device to finish
pub fn parse_program_xml<T: Read + Write + QdlChan>(
channel: &mut T,
xml: &Element,
program_file_dir: &Path,
out_dir: &Path,
allow_missing_files: bool,
verbose: bool,
) -> anyhow::Result<Option<u8>> {
let mut bootable_part_idx: Option<u8> = None;
// First make sure we have all the necessary files (and fail unless specified otherwise)
for node in xml.children.iter() {
if let XMLNode::Element(e) = node {
match e.name.to_lowercase().as_str() {
"program" => {
if !e.attributes.contains_key("filename") {
bail!("Got '<program>' tag without a filename");
}
let filename = e.attributes.get("filename").unwrap();
let file_path = program_file_dir.join(filename);
if !file_path.exists() && !allow_missing_files {
bail!("{} doesn't exist!", file_path.to_str().unwrap())
}
}
_ => continue,
}
}
}
// At last, do the things we're supposed to do
for node in xml.children.iter() {
if let XMLNode::Element(e) = node {
match e.name.to_lowercase().as_str() {
"getsha256digest" => parse_read_cmd(channel, out_dir, &e.attributes, true)?,
"patch" => parse_patch_cmd(channel, &e.attributes, verbose)?,
"program" => parse_program_cmd(
channel,
program_file_dir,
&e.attributes,
allow_missing_files,
&mut bootable_part_idx,
verbose,
)?,
"read" => parse_read_cmd(channel, out_dir, &e.attributes, false)?,
unknown => bail!(
"Got unknown instruction ({}), failing to prevent damage",
unknown
),
};
}
}
Ok(bootable_part_idx)
}