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: "msd11", detector_func: is_msd11_file, extractor_func: extract_msd11 }
|
2026-02-05 15:18:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
use std::fs::{self, OpenOptions};
|
2025-10-07 16:00:32 +02:00
|
|
|
use std::path::{Path};
|
2026-01-20 20:54:21 +01:00
|
|
|
use std::io::{Write};
|
2025-10-29 21:43:08 +01:00
|
|
|
use binrw::{BinRead, BinReaderExt};
|
2025-10-07 16:00:32 +02:00
|
|
|
|
2025-11-26 17:29:25 +01:00
|
|
|
use crate::utils::common;
|
2025-10-08 15:14:53 +02:00
|
|
|
use crate::keys;
|
2025-10-09 20:55:35 +02:00
|
|
|
use crate::formats::msd::{decrypt_aes_salted_tizen, decrypt_aes_tizen};
|
2026-01-20 20:54:21 +01:00
|
|
|
use crate::utils::msd_ouith_parser_tizen_1_9::{parse_blob_1_9};
|
2025-10-09 20:55:35 +02:00
|
|
|
|
2025-10-29 21:43:08 +01:00
|
|
|
#[derive(BinRead)]
|
|
|
|
|
struct FileHeader {
|
|
|
|
|
#[br(count = 6)] _magic_bytes: Vec<u8>,
|
2026-01-21 23:34:14 +01:00
|
|
|
_header_checksum: u32, //CRC32 of data with the size of "_header_size"
|
|
|
|
|
_header_size: u64,
|
|
|
|
|
section_count: u32,
|
2025-10-29 21:43:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(BinRead)]
|
|
|
|
|
struct SectionEntry {
|
|
|
|
|
index: u32,
|
|
|
|
|
offset: u64,
|
|
|
|
|
size: u64,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(BinRead)]
|
|
|
|
|
struct HeaderEntry {
|
|
|
|
|
offset: u64,
|
|
|
|
|
size: u32,
|
|
|
|
|
_name_length: u8,
|
|
|
|
|
#[br(count = _name_length)] name_bytes: Vec<u8>,
|
|
|
|
|
}
|
|
|
|
|
impl HeaderEntry {
|
|
|
|
|
fn name(&self) -> String {
|
|
|
|
|
common::string_from_bytes(&self.name_bytes)
|
2025-10-07 16:00:32 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct Section {
|
|
|
|
|
index: u32,
|
|
|
|
|
offset: u64,
|
|
|
|
|
size: u64,
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 18:53:35 +01:00
|
|
|
pub fn is_msd11_file(app_ctx: &AppContext) -> Result<Option<Box<dyn Any>>, Box<dyn std::error::Error>> {
|
2026-02-05 15:18:26 +01:00
|
|
|
let header = common::read_file(app_ctx.file, 0, 6)?;
|
2025-10-29 21:43:08 +01:00
|
|
|
if header == b"MSDU11" {
|
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
|
|
|
}
|
|
|
|
|
}
|
2025-10-07 16:00:32 +02:00
|
|
|
|
2026-02-05 18:53:35 +01:00
|
|
|
pub fn extract_msd11(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-10-29 21:43:08 +01:00
|
|
|
let header: FileHeader = file.read_le()?;
|
|
|
|
|
println!("\nNumber of sections: {}", header.section_count);
|
2025-10-07 16:00:32 +02:00
|
|
|
|
|
|
|
|
let mut sections: Vec<Section> = 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);
|
|
|
|
|
sections.push(Section {index: section.index, offset: section.offset, size: section.size});
|
2025-10-07 16:00:32 +02:00
|
|
|
}
|
|
|
|
|
|
2026-01-20 14:27:20 +01:00
|
|
|
let header_count: u32 = file.read_le()?;
|
2025-10-07 16:00:32 +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 16:00:32 +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 16:00:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//use first header
|
2025-10-29 21:43:08 +01:00
|
|
|
let firmware_name = &headers[0].name();
|
2025-10-07 16:00:32 +02:00
|
|
|
println!("\nFirmware name: {}", firmware_name);
|
|
|
|
|
|
|
|
|
|
let mut passphrase: Option<&str> = None;
|
|
|
|
|
let passphrase_bytes;
|
|
|
|
|
|
|
|
|
|
//find passphrase
|
2025-10-08 15:14:53 +02:00
|
|
|
for (prefix, value) in keys::MSD11 {
|
2025-10-07 16:00:32 +02:00
|
|
|
if firmware_name.starts_with(prefix) {
|
|
|
|
|
passphrase = Some(value);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if let Some(p) = passphrase {
|
|
|
|
|
println!("Passphrase: {}", p);
|
|
|
|
|
passphrase_bytes = hex::decode(p)?;
|
|
|
|
|
} else {
|
|
|
|
|
println!("Sorry, this firmware is not supported!");
|
|
|
|
|
std::process::exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let toc_offset = headers[0].offset + 8;
|
|
|
|
|
let toc_size = headers[0].size - 8;
|
|
|
|
|
let toc_data = common::read_file(&file, toc_offset as u64, toc_size as usize)?;
|
|
|
|
|
|
|
|
|
|
let toc = decrypt_aes_salted_tizen(&toc_data, &passphrase_bytes)?;
|
2026-01-20 20:54:21 +01:00
|
|
|
let (items, info) = parse_blob_1_9(&toc)?;
|
2025-10-07 16:00:32 +02:00
|
|
|
|
2026-01-20 20:54:21 +01:00
|
|
|
if let Some(info) = info {
|
|
|
|
|
println!("\nImage info:\n{} {}.{}",
|
|
|
|
|
info.name(), info.major_ver, info.minor_ver);
|
|
|
|
|
}
|
2025-10-07 16:00:32 +02:00
|
|
|
|
2026-01-20 20:54:21 +01:00
|
|
|
for (i, item) in items.iter().enumerate() {
|
2025-10-07 16:00:32 +02:00
|
|
|
let size = sections[i as usize].size;
|
2026-01-20 20:54:21 +01:00
|
|
|
let offset = sections[i as usize].offset;
|
2025-12-05 20:23:32 +01:00
|
|
|
|
2026-01-20 20:54:21 +01:00
|
|
|
println!("\n({}/{}) - {}, Size: {}",
|
2026-01-21 23:34:14 +01:00
|
|
|
i + 1, items.len(), item.name, size);
|
2025-12-05 20:23:32 +01:00
|
|
|
|
2026-01-20 20:54:21 +01:00
|
|
|
assert!(sections[i as usize].index == item.item_id, "Item ID in TOC does not match ID from header!");
|
2025-10-07 16:00:32 +02:00
|
|
|
|
2026-01-20 20:54:21 +01:00
|
|
|
let stored_data = common::read_file(&file, offset as u64, size as usize)?;
|
2025-10-07 16:00:32 +02:00
|
|
|
|
2026-01-20 20:54:21 +01:00
|
|
|
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 16:00:32 +02:00
|
|
|
|
2026-02-05 15:18:26 +01:00
|
|
|
let output_path = Path::new(app_ctx.output_dir).join(item.name.clone());
|
|
|
|
|
fs::create_dir_all(app_ctx.output_dir)?;
|
2026-01-20 20:54:21 +01:00
|
|
|
let mut out_file = OpenOptions::new().write(true).create(true).open(output_path)?;
|
|
|
|
|
out_file.write_all(&out_data)?;
|
2025-10-07 16:00:32 +02:00
|
|
|
|
|
|
|
|
println!("-- Saved file!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
println!("\nExtraction finished!");
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|