much needed overhaul to msd10 and msd11

This commit is contained in:
theubusu
2025-10-29 21:43:08 +01:00
parent b3cfb5c43f
commit 45a13f2695
2 changed files with 153 additions and 156 deletions
+65 -66
View File
@@ -1,12 +1,60 @@
use std::fs::{self, File, OpenOptions};
use std::path::{Path};
use std::io::{Cursor, Write, Seek, SeekFrom};
use binrw::{BinRead, BinReaderExt};
use crate::common;
use crate::keys;
use crate::formats::msd::{decrypt_aes_salted_tizen, decrypt_aes_tizen};
#[derive(BinRead)]
struct FileHeader {
#[br(count = 6)] _magic_bytes: Vec<u8>,
#[br(count = 12)] _unk: Vec<u8>,
section_count: u32
}
#[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)
}
}
struct Section {
index: u32,
offset: u64,
size: u64,
}
#[derive(BinRead)]
struct TocEntry {
#[br(count = 74)] _unk1: Vec<u8>,
_name_length: u8,
#[br(count = _name_length)] name_bytes: Vec<u8>,
#[br(count = 39)] _unk2: Vec<u8>,
#[br(count = 8)] salt: Vec<u8>,
#[br(count = 267)] _unk3: Vec<u8>,
}
impl TocEntry {
fn name(&self) -> String {
common::string_from_bytes(&self.name_bytes)
}
}
pub fn is_msd11_file(file: &File) -> bool {
let header = common::read_file(&file, 0, 6).expect("Failed to read from file.");
if header == b"MSDU11" {
@@ -16,67 +64,32 @@ pub fn is_msd11_file(file: &File) -> bool {
}
}
struct Section {
index: u32,
offset: u64,
size: u64,
name: String,
}
pub fn extract_msd11(mut file: &File, output_folder: &str) -> Result<(), Box<dyn std::error::Error>> {
let _magic = common::read_exact(&mut file, 6)?; //MSDU11 magic
let _ = common::read_exact(&mut file, 12)?;
let section_count_bytes = common::read_exact(&mut file, 4)?;
let section_count = u32::from_le_bytes(section_count_bytes.try_into().unwrap());
println!("\nNumber of sections: {}", section_count);
let header: FileHeader = file.read_le()?;
println!("\nNumber of sections: {}", header.section_count);
let mut sections: Vec<Section> = Vec::new();
//parse sections
for _i in 0..section_count {
let index_bytes = common::read_exact(&mut file, 4)?;
let index = u32::from_le_bytes(index_bytes.try_into().unwrap());
let offset_bytes = common::read_exact(&mut file, 8)?;
let offset = u64::from_le_bytes(offset_bytes.try_into().unwrap());
let size_bytes = common::read_exact(&mut file, 8)?;
let size = u64::from_le_bytes(size_bytes.try_into().unwrap());
println!("Section {}: offset: {}, size: {}", index, offset, size);
sections.push(Section { index, offset, size , name: "".to_string() });
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});
}
let header_count_bytes = common::read_exact(&mut file, 4)?;
let header_count = u32::from_le_bytes(header_count_bytes.try_into().unwrap());
println!("\nNumber of headers: {}", header_count);
let mut headers: Vec<Section> = Vec::new();
let mut headers: Vec<HeaderEntry> = Vec::new();
//parse headers
for i in 0..header_count {
let offset_bytes = common::read_exact(&mut file, 8)?;
let offset = u64::from_le_bytes(offset_bytes.try_into().unwrap());
let size_bytes = common::read_exact(&mut file, 4)?;
let size = u32::from_le_bytes(size_bytes.try_into().unwrap());
let name_length_byte = common::read_exact(&mut file, 1)?;
let name_length = u8::from_le_bytes(name_length_byte.try_into().unwrap());
let name_bytes = common::read_exact(&mut file, name_length as usize)?;
let name = String::from_utf8(name_bytes)?;
println!("Header {}: {}, offset: {}, size: {}", i + 1, name, offset, size);
headers.push(Section { index: 0, offset, size: size as u64 , name });
let header: HeaderEntry = file.read_le()?;
println!("Header {}: {}, offset: {}, size: {}", i + 1, header.name(), header.offset, header.size);
headers.push(header);
}
//use first header
let firmware_name = &headers[0].name;
let firmware_name = &headers[0].name();
println!("\nFirmware name: {}", firmware_name);
let mut passphrase: Option<&str> = None;
@@ -101,38 +114,25 @@ pub fn extract_msd11(mut file: &File, output_folder: &str) -> Result<(), Box<dyn
let toc_size = headers[0].size - 8;
let toc_data = common::read_file(&file, toc_offset as u64, toc_size as usize)?;
//parse TOC
let toc = decrypt_aes_salted_tizen(&toc_data, &passphrase_bytes)?;
let mut toc_reader = Cursor::new(toc);
toc_reader.seek(SeekFrom::Current(262))?; // probably signature
toc_reader.seek(SeekFrom::Current(50))?; // Tizen Software Upgrade Tree Binary Format ver. 1.9
for i in 0..section_count {
toc_reader.seek(SeekFrom::Current(74))?; //unknown
for i in 0..header.section_count {
let entry: TocEntry = toc_reader.read_le()?;
let name_length_byte = common::read_exact(&mut toc_reader, 1)?;
let name_length = u8::from_le_bytes(name_length_byte.try_into().unwrap());
let name_bytes = common::read_exact(&mut toc_reader, name_length as usize)?;
let name = String::from_utf8(name_bytes)?;
toc_reader.seek(SeekFrom::Current(39))?; //unknown
let salt = common::read_exact(&mut toc_reader, 8)?;
toc_reader.seek(SeekFrom::Current(267))?; //unknown
println!("\nSection {}: {}", sections[i as usize].index, name);
println!("\nSection {}: {}", sections[i as usize].index, entry.name());
let offset = sections[i as usize].offset;
let size = sections[i as usize].size;
let encrypted_data = common::read_file(&file, offset as u64, size as usize)?;
println!("- Decrypting...");
let decrypted_data = decrypt_aes_tizen(&encrypted_data, &passphrase_bytes, &salt)?;
let decrypted_data = decrypt_aes_tizen(&encrypted_data, &passphrase_bytes, &entry.salt)?;
let output_path = Path::new(&output_folder).join(name);
let output_path = Path::new(&output_folder).join(entry.name());
fs::create_dir_all(&output_folder)?;
let mut out_file = OpenOptions::new()
@@ -143,7 +143,6 @@ pub fn extract_msd11(mut file: &File, output_folder: &str) -> Result<(), Box<dyn
out_file.write_all(&decrypted_data)?;
println!("-- Saved file!");
}
println!("\nExtraction finished!");