2026-02-05 15:18:26 +01:00
|
|
|
use std::any::Any;
|
2026-02-05 18:53:35 +01:00
|
|
|
use crate::{AppContext, formats::Format};
|
2026-02-05 15:18:26 +01:00
|
|
|
pub fn format() -> Format {
|
2026-02-05 18:53:35 +01:00
|
|
|
Format { name: "mtk_pkg_old", detector_func: is_mtk_pkg_old_file, extractor_func: extract_mtk_pkg_old }
|
2026-02-05 15:18:26 +01:00
|
|
|
}
|
|
|
|
|
|
2025-12-14 01:08:48 +01:00
|
|
|
use std::path::Path;
|
2026-02-05 15:18:26 +01:00
|
|
|
use std::fs::{self, OpenOptions};
|
2025-12-14 01:08:48 +01:00
|
|
|
use std::io::{Write, Cursor, Seek};
|
|
|
|
|
use binrw::{BinRead, BinReaderExt};
|
|
|
|
|
|
|
|
|
|
use crate::utils::common;
|
|
|
|
|
use crate::utils::mtk_crypto::{decrypt};
|
2026-01-12 15:40:05 +01:00
|
|
|
use crate::utils::lzhs::{decompress_lzhs_fs_file2file_old};
|
2025-12-14 01:08:48 +01:00
|
|
|
|
|
|
|
|
static KEY: u32 = 0x94102909; //09 29 10 94
|
|
|
|
|
// first 4 bytes of header and content are additionally XORed, they have different masks although only differ by half a byte
|
|
|
|
|
static HEADER_XOR_MASK: u32 = 0x04BE7C75; //75 7C BE 04
|
|
|
|
|
static CONTENT_XOR_MASK: u32 = 0x04BE7C72; //72 7C BE 04
|
|
|
|
|
|
2026-02-02 01:54:35 +01:00
|
|
|
use crate::formats::mtk_pkg::{MTK_HEADER_MAGIC, MTK_META_MAGIC, MTK_META_PAD_MAGIC};
|
|
|
|
|
|
2025-12-14 01:08:48 +01:00
|
|
|
#[derive(BinRead)]
|
|
|
|
|
struct Header {
|
|
|
|
|
#[br(count = 4)] vendor_magic_bytes: Vec<u8>,
|
|
|
|
|
#[br(count = 8)] _mtk_magic: Vec<u8>, //#DH@FiRm
|
|
|
|
|
#[br(count = 68)] version_bytes: Vec<u8>,
|
|
|
|
|
file_size: u32,
|
|
|
|
|
_flags: u32,
|
|
|
|
|
#[br(count = 32)] product_name_bytes: Vec<u8>,
|
2025-12-27 17:09:08 +01:00
|
|
|
#[br(count = 32)] _digest: Vec<u8>,
|
2025-12-14 01:08:48 +01:00
|
|
|
}
|
|
|
|
|
impl Header {
|
|
|
|
|
fn vendor_magic(&self) -> String {
|
|
|
|
|
common::string_from_bytes(&self.vendor_magic_bytes)
|
|
|
|
|
}
|
|
|
|
|
fn version(&self) -> String {
|
|
|
|
|
common::string_from_bytes(&self.version_bytes)
|
|
|
|
|
}
|
|
|
|
|
fn product_name(&self) -> String {
|
|
|
|
|
common::string_from_bytes(&self.product_name_bytes)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(BinRead)]
|
|
|
|
|
struct PartEntry {
|
|
|
|
|
#[br(count = 4)] name_bytes: Vec<u8>,
|
|
|
|
|
flags: u32,
|
|
|
|
|
size: u32,
|
|
|
|
|
}
|
|
|
|
|
impl PartEntry {
|
|
|
|
|
fn name(&self) -> String {
|
|
|
|
|
common::string_from_bytes(&self.name_bytes)
|
|
|
|
|
}
|
|
|
|
|
fn is_encrypted(&self) -> bool {
|
|
|
|
|
(self.flags & 1 << 0) == 1 << 0
|
|
|
|
|
}
|
2026-01-12 15:40:05 +01:00
|
|
|
fn is_compressed(&self) -> bool { //lzhs fs
|
|
|
|
|
(self.flags & 1 << 8) != 0
|
|
|
|
|
}
|
2025-12-14 01:08:48 +01:00
|
|
|
}
|
|
|
|
|
|
2026-02-02 01:54:35 +01:00
|
|
|
static HEADER_SIZE: usize = 0x98;
|
|
|
|
|
|
2026-02-05 18:53:35 +01:00
|
|
|
pub fn is_mtk_pkg_old_file(app_ctx: &AppContext) -> Result<Option<Box<dyn Any>>, Box<dyn std::error::Error>> {
|
2026-02-05 15:18:26 +01:00
|
|
|
let mut file = app_ctx.file;
|
|
|
|
|
let encrypted_header = common::read_file(&file, 0, HEADER_SIZE)?;
|
2025-12-14 01:08:48 +01:00
|
|
|
let header = decrypt(&encrypted_header, KEY, Some(HEADER_XOR_MASK));
|
2026-02-02 01:54:35 +01:00
|
|
|
if &header[4..12] == MTK_HEADER_MAGIC {
|
2026-02-05 15:18:26 +01:00
|
|
|
Ok(Some(Box::new(())))
|
2026-02-02 01:54:35 +01:00
|
|
|
} else if &header[68..76] == MTK_HEADER_MAGIC {
|
2025-12-15 00:38:15 +01:00
|
|
|
//check for 64 byte additional header used in some Sony and Philips firmwares and skip it
|
2026-02-05 15:18:26 +01:00
|
|
|
file.seek(std::io::SeekFrom::Start(64))?;
|
|
|
|
|
Ok(Some(Box::new(())))
|
2026-02-02 01:54:35 +01:00
|
|
|
} else if &header[132..140] == MTK_HEADER_MAGIC {
|
2025-12-15 00:38:15 +01:00
|
|
|
//check for 128 byte additional header used in some Philips firmwares and skip it
|
2026-02-05 15:18:26 +01:00
|
|
|
file.seek(std::io::SeekFrom::Start(128))?;
|
|
|
|
|
Ok(Some(Box::new(())))
|
2025-12-14 01:08:48 +01:00
|
|
|
} else {
|
2026-02-05 15:18:26 +01:00
|
|
|
Ok(None)
|
2025-12-14 01:08:48 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 18:53:35 +01:00
|
|
|
pub fn extract_mtk_pkg_old(app_ctx: &AppContext, _ctx: Option<Box<dyn Any>>) -> Result<(), Box<dyn std::error::Error>> {
|
2026-02-05 15:18:26 +01:00
|
|
|
let mut file = app_ctx.file;
|
2025-12-14 01:08:48 +01:00
|
|
|
let file_size = file.metadata()?.len();
|
2026-02-02 01:54:35 +01:00
|
|
|
let encrypted_header = common::read_exact(&mut file, HEADER_SIZE)?;
|
2025-12-14 01:08:48 +01:00
|
|
|
let header = decrypt(&encrypted_header, KEY, Some(HEADER_XOR_MASK));
|
|
|
|
|
let mut hdr_reader = Cursor::new(header);
|
|
|
|
|
let hdr: Header = hdr_reader.read_le()?;
|
|
|
|
|
|
|
|
|
|
println!("File info:\nFile size: {}\nVendor magic: {}\nVersion info: {}\nProduct name: {}" ,
|
|
|
|
|
hdr.file_size, hdr.vendor_magic(), hdr.version(), hdr.product_name());
|
|
|
|
|
|
|
|
|
|
let mut part_n = 0;
|
|
|
|
|
while file.stream_position()? < file_size as u64 {
|
|
|
|
|
part_n += 1;
|
|
|
|
|
let part_entry: PartEntry = file.read_le()?;
|
|
|
|
|
|
2026-01-12 15:40:05 +01:00
|
|
|
println!("\n#{} - {}, Size: {}{} {}",
|
|
|
|
|
part_n, part_entry.name(), part_entry.size, if part_entry.is_compressed() {" [COMPRESSED]"} else {""}, if part_entry.is_encrypted() {"[ENCRYPTED]"} else {""} );
|
2025-12-14 01:08:48 +01:00
|
|
|
|
|
|
|
|
let data = common::read_exact(&mut file, part_entry.size as usize)?;
|
|
|
|
|
let out_data;
|
|
|
|
|
if part_entry.is_encrypted() {
|
|
|
|
|
//decrypt with the vendor magic
|
|
|
|
|
println!("- Decrypting...");
|
|
|
|
|
let vendor_magic_u32 = u32::from_le_bytes(hdr.vendor_magic_bytes.clone().try_into().unwrap());
|
|
|
|
|
out_data = decrypt(&data, vendor_magic_u32, Some(CONTENT_XOR_MASK));
|
|
|
|
|
} else {
|
|
|
|
|
out_data = data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//strip iMtK thing and get version
|
2026-02-02 01:54:35 +01:00
|
|
|
let extra_header_len = if &out_data[0..4] == MTK_META_MAGIC {
|
2025-12-14 01:08:48 +01:00
|
|
|
let imtk_len = u32::from_le_bytes(out_data[4..8].try_into().unwrap());
|
2026-02-02 01:54:35 +01:00
|
|
|
if &out_data[8..12] != MTK_META_PAD_MAGIC {
|
2025-12-17 01:55:40 +01:00
|
|
|
let version_len = u32::from_le_bytes(out_data[8..12].try_into().unwrap());
|
|
|
|
|
let version = common::string_from_bytes(&out_data[12..12 + version_len as usize]);
|
|
|
|
|
println!("- Version: {}", version);
|
|
|
|
|
}
|
2025-12-14 01:08:48 +01:00
|
|
|
imtk_len + 8
|
|
|
|
|
} else {
|
|
|
|
|
0
|
|
|
|
|
};
|
|
|
|
|
|
2026-01-12 15:40:05 +01:00
|
|
|
//for compressed part create temp file
|
2026-02-05 15:18:26 +01:00
|
|
|
let output_path = Path::new(app_ctx.output_dir).join(part_entry.name() + if part_entry.is_compressed() {".lzhs"} else {".bin"});
|
|
|
|
|
fs::create_dir_all(app_ctx.output_dir)?;
|
2026-01-12 15:40:05 +01:00
|
|
|
let mut out_file = OpenOptions::new().write(true).read(true)/* for lzhs */.create(true).open(&output_path)?;
|
2025-12-14 01:08:48 +01:00
|
|
|
out_file.write_all(&out_data[extra_header_len as usize..])?;
|
|
|
|
|
|
2026-01-12 15:40:05 +01:00
|
|
|
if part_entry.is_compressed() {
|
2026-02-05 15:18:26 +01:00
|
|
|
let lzhs_out_path = Path::new(app_ctx.output_dir).join(part_entry.name() + ".bin");
|
2026-01-12 15:40:05 +01:00
|
|
|
match decompress_lzhs_fs_file2file_old(&out_file, lzhs_out_path) {
|
|
|
|
|
Ok(()) => {
|
|
|
|
|
println!("- Decompressed Successfully!");
|
|
|
|
|
//after successfull decompression remove the temporary .lzhs file
|
|
|
|
|
fs::remove_file(&output_path)?;
|
|
|
|
|
},
|
|
|
|
|
Err(e) => {
|
|
|
|
|
eprintln!("Failed to decompress partition!, Error: {}. Saving compressed data...", e);
|
|
|
|
|
//if the decompression is not successfull leave out compressed data.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-14 01:08:48 +01:00
|
|
|
println!("-- Saved file!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
println!("\nExtraction finished!");
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|