2026-02-17 17:28:59 +01:00
|
|
|
mod include;
|
|
|
|
|
mod android_ota_update_metadata;
|
2026-02-05 15:18:26 +01:00
|
|
|
use std::any::Any;
|
2026-02-17 17:28:59 +01:00
|
|
|
use crate::AppContext;
|
2026-02-05 15:18:26 +01:00
|
|
|
|
|
|
|
|
use std::fs::{self, OpenOptions};
|
2026-02-17 17:28:59 +01:00
|
|
|
use std::path::Path;
|
|
|
|
|
use std::io::Write;
|
|
|
|
|
use binrw::BinReaderExt;
|
2025-12-12 22:14:01 +01:00
|
|
|
use prost::Message;
|
|
|
|
|
|
|
|
|
|
use crate::utils::common;
|
2026-02-17 17:28:59 +01:00
|
|
|
use android_ota_update_metadata::{DeltaArchiveManifest, install_operation};
|
2025-12-12 22:14:01 +01:00
|
|
|
use crate::utils::compression::{decompress_bzip, decompress_xz};
|
2026-02-17 17:28:59 +01:00
|
|
|
use include::*;
|
2025-12-12 22:14:01 +01:00
|
|
|
|
2026-02-05 18:53:35 +01:00
|
|
|
pub fn is_android_ota_payload_file(app_ctx: &AppContext) -> Result<Option<Box<dyn Any>>, Box<dyn std::error::Error>> {
|
2026-02-06 16:27:13 +01:00
|
|
|
let file = match app_ctx.file() {Some(f) => f, None => return Ok(None)};
|
2026-02-06 15:08:38 +01:00
|
|
|
|
|
|
|
|
let header = common::read_file(&file, 0, 4)?;
|
2025-12-12 22:14:01 +01:00
|
|
|
if header == b"CrAU" {
|
2026-02-05 15:18:26 +01:00
|
|
|
Ok(Some(Box::new(())))
|
2025-12-12 22:14:01 +01:00
|
|
|
} else {
|
2026-02-05 15:18:26 +01:00
|
|
|
Ok(None)
|
2025-12-12 22:14:01 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-06 16:27:13 +01:00
|
|
|
pub fn extract_android_ota_payload(app_ctx: &AppContext, _ctx: Box<dyn Any>) -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
|
let mut file = app_ctx.file().ok_or("Extractor expected file")?;
|
2026-02-06 15:08:38 +01:00
|
|
|
|
2025-12-12 22:14:01 +01:00
|
|
|
let header: Header = file.read_be()?;
|
|
|
|
|
println!("File info:\nFormat version: {}\nManifest size: {}", header.file_format_version, header.manifest_size);
|
|
|
|
|
|
|
|
|
|
if header.file_format_version != 2 {
|
|
|
|
|
println!("\nSorry, this version of the file is not supported!");
|
|
|
|
|
return Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let base_offset = 24 /* size of header */ + header.manifest_size + header.metadata_signature_size as u64;
|
|
|
|
|
|
|
|
|
|
let read_manifest = common::read_exact(&mut file, header.manifest_size as usize)?;
|
|
|
|
|
let manifest = DeltaArchiveManifest::decode(&*read_manifest)?;
|
|
|
|
|
|
|
|
|
|
for (i, partition) in manifest.partitions.into_iter().enumerate() {
|
|
|
|
|
let operation_count = partition.operations.len();
|
|
|
|
|
println!("\n#{} - {}, Size: {}, Operations: {}",
|
|
|
|
|
i + 1, partition.partition_name, partition.new_partition_info.unwrap().size.unwrap(), operation_count);
|
|
|
|
|
|
|
|
|
|
for (i, operation) in partition.operations.into_iter().enumerate() {
|
|
|
|
|
let operation_name_str = match install_operation::Type::try_from(operation.r#type) {
|
|
|
|
|
Ok(t) => t.as_str_name(),
|
|
|
|
|
Err(_) => "UNKNOWN",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let offset = base_offset + operation.data_offset.unwrap();
|
|
|
|
|
let size = operation.data_length.unwrap();
|
|
|
|
|
|
|
|
|
|
//because the amount of operations can reach up to the thousands, i think its best to update the current line
|
|
|
|
|
//to not clog up the terminal and so you know what the program is actually doing
|
|
|
|
|
print!("\r- ({}/{}) - {}({}), Offset: {}, Size: {}",
|
|
|
|
|
i + 1, operation_count, operation_name_str, operation.r#type, offset, size);
|
|
|
|
|
std::io::stdout().flush()?;
|
|
|
|
|
|
|
|
|
|
let data = common::read_file(&file, offset, size as usize)?;
|
|
|
|
|
|
|
|
|
|
let out_data;
|
|
|
|
|
if operation.r#type == 0 { //REPLACE - just write the stored data
|
|
|
|
|
out_data = data;
|
|
|
|
|
}
|
|
|
|
|
else if operation.r#type == 1 { //REPLACE_BZ - decompress with bzip and write
|
|
|
|
|
out_data = decompress_bzip(&data)?;
|
|
|
|
|
}
|
|
|
|
|
else if operation.r#type == 8 { //REPLACE_XZ - decompress with xz and write
|
|
|
|
|
out_data = decompress_xz(&data)?;
|
|
|
|
|
} else {
|
|
|
|
|
println!("-- Unsupported operation!");
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-06 15:08:38 +01:00
|
|
|
fs::create_dir_all(&app_ctx.output_dir)?;
|
|
|
|
|
let output_path = Path::new(&app_ctx.output_dir).join(format!("{}.bin", partition.partition_name));
|
2025-12-12 22:14:01 +01:00
|
|
|
let mut out_file = OpenOptions::new().append(true).create(true).open(output_path)?;
|
|
|
|
|
out_file.write_all(&out_data)?;
|
|
|
|
|
}
|
|
|
|
|
println!("\n-- Saved!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
println!("\nExtraction finished!");
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|