Files
unixtract/src/formats/msd10/mod.rs
T

198 lines
7.5 KiB
Rust
Raw Normal View History

2026-02-17 17:28:59 +01:00
mod include;
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, Seek, SeekFrom};
2026-02-17 17:28:59 +01:00
use binrw::BinReaderExt;
2025-10-07 13:29:44 +02:00
use crate::utils::common;
use crate::utils::global::opt_dump_dec_hdr;
use crate::keys;
use crate::formats::msd::{decrypt_aes_salted_old, decrypt_aes_salted_tizen, decrypt_aes_tizen};
2026-02-17 17:28:59 +01:00
use crate::formats::msd::msd_ouith_parser_old::{parse_ouith_blob};
use crate::formats::msd::msd_ouith_parser_tizen_1_8::{parse_blob_1_8};
use include::*;
2025-10-07 13:29:44 +02:00
2026-02-05 18:53:35 +01:00
pub fn is_msd10_file(app_ctx: &AppContext) -> Result<Option<Box<dyn Any>>, Box<dyn std::error::Error>> {
let file = match app_ctx.file() {Some(f) => f, None => return Ok(None)};
let header = common::read_file(&file, 0, 6)?;
2025-10-29 21:43:08 +01:00
if header == b"MSDU10" {
2026-02-05 15:18:26 +01:00
Ok(Some(Box::new(())))
2025-10-29 21:43:08 +01:00
} else {
2026-02-05 15:18:26 +01:00
Ok(None)
2025-10-29 21:43:08 +01:00
}
}
pub fn extract_msd10(app_ctx: &AppContext, _ctx: Box<dyn Any>) -> Result<(), Box<dyn std::error::Error>> {
let mut file = app_ctx.file().ok_or("Extractor expected file")?;
let save_cmac = app_ctx.options.iter().any(|e| e == "msd10:save_cmac");
let print_ouith_tree = app_ctx.options.iter().any(|e| e == "msd:print_ouith");
2025-10-29 21:43:08 +01:00
let header: FileHeader = file.read_le()?;
println!("\nNumber of sections: {}", header.section_count);
2025-10-07 13:29:44 +02:00
2026-02-17 17:28:59 +01:00
let mut sections: Vec<SectionEntry> = Vec::new();
2025-10-29 21:43:08 +01:00
for _i in 0..header.section_count {
let section: SectionEntry = file.read_le()?;
println!("Section {}: offset: {}, size: {}", section.index, section.offset, section.size);
2026-02-17 17:28:59 +01:00
sections.push(section);
2025-10-07 13:29:44 +02:00
}
let _zero_padding = common::read_exact(&mut file, 4)?;
let header_count: u32 = file.read_le()?;
2025-10-07 13:29:44 +02:00
println!("\nNumber of headers: {}", header_count);
2025-10-29 21:43:08 +01:00
let mut headers: Vec<HeaderEntry> = Vec::new();
2025-10-07 13:29:44 +02:00
for i in 0..header_count {
2025-10-29 21:43:08 +01:00
let header: HeaderEntry = file.read_le()?;
println!("Header {}: {}, offset: {}, size: {}", i + 1, header.name(), header.offset, header.size);
headers.push(header);
2025-10-07 13:29:44 +02:00
}
//use first header
2025-10-29 21:43:08 +01:00
let firmware_name = &headers[0].name();
2025-10-07 13:29:44 +02:00
println!("\nFirmware name: {}", firmware_name);
let toc_offset = headers[0].offset;
let toc_size = headers[0].size;
let toc_data = common::read_file(&file, toc_offset as u64, toc_size as usize)?;
2025-10-07 13:29:44 +02:00
//find passphrase
let mut passphrase_bytes: Option<Vec<u8>> = None;
let mut passphrase_name: &str = "";
let mut firmware_type: Option<FirmwareType> = None;
for (key_hex, name) in keys::MSD10 {
let key_bytes = hex::decode(key_hex)?;
if key_bytes.len() == 20 {
match decrypt_aes_salted_old(&toc_data, &key_bytes) {
Ok(_) => {
passphrase_bytes = Some(key_bytes);
passphrase_name = name;
firmware_type = Some(FirmwareType::Old);
break
},
Err(_) => continue,
};
}
else if key_bytes.len() == 16 {
match decrypt_aes_salted_tizen(&toc_data, &key_bytes) {
Ok(_) => {
passphrase_bytes = Some(key_bytes);
passphrase_name = name;
firmware_type = Some(FirmwareType::Tizen);
break
},
Err(_) => continue,
};
2025-10-07 13:29:44 +02:00
}
}
let (passphrase_bytes, firmware_type) = if let (Some(p), Some(t)) = (passphrase_bytes, firmware_type) {
println!("Using passphrase: {}", passphrase_name);
(p, t)
} else {
return Err("No matching key found!".into());
};
2025-10-07 13:29:44 +02:00
//parse TOC
if firmware_type == FirmwareType::Tizen {
2025-10-07 13:29:44 +02:00
let toc = decrypt_aes_salted_tizen(&toc_data, &passphrase_bytes)?;
opt_dump_dec_hdr(app_ctx, &toc, "toc")?;
let (items, info) = parse_blob_1_8(&toc, print_ouith_tree)?;
2025-10-07 13:29:44 +02:00
if let Some(info) = info {
println!("\nImage info:\n{} {}.{} {}/{}/{}",
info.name(), info.major_ver, info.minor_ver, info.date_day, info.date_month, info.date_year);
}
2025-10-07 13:29:44 +02:00
for (i, item) in items.iter().enumerate() {
2025-10-07 13:29:44 +02:00
let size = sections[i as usize].size;
let offset = sections[i as usize].offset;
println!("\n({}/{}) - {}, Size: {}",
2026-01-21 23:34:14 +01:00
i + 1, items.len(), item.name, size);
if sections[i as usize].index != item.item_id {
return Err("Item ID in TOC does not match ID from header!".into());
}
let stored_data = common::read_file(&file, offset as u64, size as usize)?;
let out_data;
if item.aes_encryption {
println!("- Decrypting...");
let salt = item.aes_salt.as_ref().ok_or("AES salt missing!")?;
out_data = decrypt_aes_tizen(&stored_data, &passphrase_bytes, salt)?;
} else {
out_data = stored_data;
}
2025-10-07 13:29:44 +02:00
let output_path = Path::new(&app_ctx.output_dir).join(format!("{}", item.name));
fs::create_dir_all(&app_ctx.output_dir)?;
let mut out_file = OpenOptions::new().write(true).create(true).open(output_path)?;
out_file.write_all(&out_data)?;
2025-10-07 13:29:44 +02:00
println!("-- Saved file!");
}
} else if firmware_type == FirmwareType::Old {
2025-10-07 13:29:44 +02:00
let toc = decrypt_aes_salted_old(&toc_data, &passphrase_bytes)?;
opt_dump_dec_hdr(app_ctx, &toc, "toc")?;
let (items, info) = parse_ouith_blob(&toc, print_ouith_tree)?;
2025-10-07 13:29:44 +02:00
if let Some(info) = info {
println!("\nImage info:\n{} {}.{} {}/{}/20{}",
info.name(), info.major_ver, info.minor_ver, info.date_day, info.date_month, info.date_year);
}
2025-10-07 13:29:44 +02:00
for (i, item) in items.iter().enumerate() {
let offset = sections[i as usize].offset;
let type_str = if item.item_type == 0x0A {"Partition"} else if item.item_type == 0x0B {"File"} else if item.item_type == 0x11 {"CMAC Data"} else {"Unknown"};
println!("\n({}/{}) - {}, Type: {}, Size: {}",
item.item_id, items.len(), item.name, type_str, item.all_size);
2025-10-07 13:29:44 +02:00
if sections[i as usize].index != item.item_id {
return Err("Item ID in TOC does not match ID from header!".into());
}
2025-10-07 13:29:44 +02:00
let mut out_filename = format!("{}", item.name);
if item.item_type == 0x11 { //CMAC DATA
if save_cmac {
out_filename = format!("{}.cmac", item.name); //add an additional extension, because the CMAC data has the same name as its item
} else {
println!("- Skipping CMAC Data...");
continue
}
2025-10-07 13:29:44 +02:00
}
file.seek(SeekFrom::Start(offset as u64))?;
//skip heading metadata thing
file.seek(SeekFrom::Current(item.heading_size as i64))?;
let stored_data = common::read_exact(&mut file, item.data_size as usize)?;
let out_data;
if item.aes_encryption {
println!("- Decrypting...");
out_data = decrypt_aes_salted_old(&stored_data, &passphrase_bytes)?;
} else {
out_data = stored_data;
}
2025-10-07 13:29:44 +02:00
let output_path = Path::new(&app_ctx.output_dir).join(out_filename);
fs::create_dir_all(&app_ctx.output_dir)?;
let mut out_file = OpenOptions::new().write(true).create(true).open(output_path)?;
2025-10-07 13:29:44 +02:00
out_file.write_all(&out_data)?;
println!("-- Saved file!");
}
}
Ok(())
}