diff --git a/README.md b/README.md index 7348e9c..17fa933 100644 --- a/README.md +++ b/README.md @@ -156,6 +156,12 @@ Tip: if you have split ROM (.ROM-00 and .ROM-01), extract both into the same fol **Notes:** **Depends on keys** - see keys.rs **Thanks to:** https://github.com/frederic/pflupg-tool +## Philips BDP +**Used in:** Philips MediaTek-based Blu-ray players/Home theatre systems +**Notes:** The main partition (ID 0) can be sometimes encrypted, and there is no good way to detect that. So if MTK BDP extraction fails, try running with `philips_bdp:decrypt` option. +**Options:** +`philips_bdp:decrypt` - Decrypt main partition + ## PUP **Used in:** Sony PlayStation 4/5 **Notes:** File has to be decrypted. @@ -201,7 +207,7 @@ Tip: if you have split ROM (.ROM-00 and .ROM-01), extract both into the same fol **Notes:** None, all files should be supported. ## Sony BDP -**Used in:** Sony Blu-Ray players +**Used in:** Sony MediaTek-based Blu-Ray players **Notes:** Only platforms up to MSB18 are supported. **Thanks to:** http://malcolmstagg.com/bdp/s390-firmware.html diff --git a/src/formats.rs b/src/formats.rs index 12e69c4..60ecc42 100644 --- a/src/formats.rs +++ b/src/formats.rs @@ -31,6 +31,7 @@ pub mod sdimage; pub mod cd5; pub mod gx_dvb; pub mod onkyo; +pub mod philips_bdp; pub mod pup; @@ -212,6 +213,11 @@ pub fn get_registry() -> Vec { detector_func: crate::formats::mtk_pkg_new::is_mtk_pkg_new_file, extractor_func: crate::formats::mtk_pkg_new::extract_mtk_pkg_new, }, + Format { + name: "philips_bdp", + detector_func: crate::formats::philips_bdp::is_philips_bdp_file, + extractor_func: crate::formats::philips_bdp::extract_philips_bdp, + }, Format { name: "mtk_bdp", detector_func: crate::formats::mtk_bdp::is_mtk_bdp_file, @@ -232,6 +238,5 @@ pub fn get_registry() -> Vec { detector_func: crate::formats::onkyo::is_onkyo_file, extractor_func: crate::formats::onkyo::extract_onkyo, }, - ] } \ No newline at end of file diff --git a/src/formats/philips_bdp/include.rs b/src/formats/philips_bdp/include.rs new file mode 100644 index 0000000..04b2fa9 --- /dev/null +++ b/src/formats/philips_bdp/include.rs @@ -0,0 +1,138 @@ +use crate::utils::common; +use binrw::BinRead; +use aes::Aes256; +use aes::cipher::{BlockEncrypt, KeyInit, generic_array::GenericArray}; + +pub static KEY1: [u8; 32] = [ + 0x01, 0x06, 0x18, 0x00, 0x0a, 0x22, 0x02, 0x41, 0x4d, 0x41, 0x08, 0x22, 0x12, 0x09, 0x04, 0x20, + 0x22, 0x12, 0x02, 0x11, 0x01, 0x05, 0x41, 0x00, 0x05, 0x22, 0x22, 0x0a, 0x24, 0x08, 0x40, 0x24 +]; + +pub static IV1: [u8; 16] = [ + 0x58, 0x87, 0x40, 0x13, 0x20, 0x00, 0x01, 0x30, 0x03, 0x58, 0x81, 0x42, 0x04, 0x22, 0x9c, 0x01 +]; + +//AES 256 CFB with IV reset every 0x80 bytes +pub fn bebin_decrypt_aes256cfb(data: &[u8], key: &[u8; 32], iv: &[u8; 16]) -> Vec { + let cipher = Aes256::new(GenericArray::from_slice(key)); + let mut out = Vec::with_capacity(data.len()); + + for chunk in data.chunks(0x80) { + let mut state = *iv; + + for block in chunk.chunks(16) { + let mut ks = state.into(); + cipher.encrypt_block(&mut ks); + + let pt: Vec = block.iter() + .zip(ks.iter()) + .map(|(&c, &k)| c ^ k) + .collect(); + + let n = block.len(); + state[..n].copy_from_slice(block); + + out.extend_from_slice(&pt); + } + } + + out +} + +#[derive(PartialEq, Debug)] +pub enum HeaderType { + Old, + New +} + +pub trait UpgHeader { + fn _magic_num(&self) -> [u8; 7]; + fn name(&self) -> String; + fn target_num(&self) -> u8; + fn version(&self) -> String; + fn target_size(&self) -> u32; + fn _target_verify(&self) -> u32; + fn entries(&self) -> &[UpgEntry]; +} + +#[derive(BinRead)] +pub struct UpgHeaderOld { + _magic_num: [u8; 7], //PHILIPS + target_name_bytes: [u8; 8], + pub target_num: u8, + target_version_bytes: [u8; 8], + pub target_size: u32, + _target_verify: u32, //checksum + #[br(count=target_num)] pub entries: Vec, +} +impl UpgHeader for UpgHeaderOld { + fn _magic_num(&self) -> [u8; 7] { + self._magic_num + } + fn name(&self) -> String { + common::string_from_bytes(&self.target_name_bytes) + } + fn target_num(&self) -> u8 { + self.target_num + } + fn version(&self) -> String { + common::string_from_bytes(&self.target_version_bytes) + } + fn target_size(&self) -> u32 { + self.target_size + } + fn _target_verify(&self) -> u32 { + self._target_verify + } + fn entries(&self) -> &[UpgEntry] { + &self.entries + } +} + +#[derive(BinRead)] +pub struct UpgHeaderNew { + _magic_num: [u8; 7], //PHILIPS + target_name_bytes: [u8; 12], + pub target_num: u8, + target_version_bytes: [u8; 8], + pub target_size: u32, + _target_verify: u32, //checksum + #[br(count=target_num)] pub entries: Vec, +} +impl UpgHeader for UpgHeaderNew { + fn _magic_num(&self) -> [u8; 7] { + self._magic_num + } + fn name(&self) -> String { + common::string_from_bytes(&self.target_name_bytes) + } + fn target_num(&self) -> u8 { + self.target_num + } + fn version(&self) -> String { + common::string_from_bytes(&self.target_version_bytes) + } + fn target_size(&self) -> u32 { + self.target_size + } + fn _target_verify(&self) -> u32 { + self._target_verify + } + fn entries(&self) -> &[UpgEntry] { + &self.entries + } +} + +#[derive(BinRead)] +pub struct UpgEntry { + pub id: u8, + pub iic: u8, + version_bytes: [u8; 4], + pub offset: u32, + pub size: u32, +} +impl UpgEntry { + pub fn version(&self) -> String { + common::string_from_bytes(&self.version_bytes) + } +} \ No newline at end of file diff --git a/src/formats/philips_bdp/mod.rs b/src/formats/philips_bdp/mod.rs new file mode 100644 index 0000000..275e4d3 --- /dev/null +++ b/src/formats/philips_bdp/mod.rs @@ -0,0 +1,94 @@ +mod include; +use std::any::Any; +use crate::{AppContext, InputTarget, formats}; + +use std::path::Path; +use std::fs::{self, File, OpenOptions}; +use std::io::{Write, Seek}; +use binrw::BinReaderExt; + +use crate::utils::common; +use include::*; + +struct PhilipsBdpCtx { + header_type: HeaderType, +} + +pub fn is_philips_bdp_file(app_ctx: &AppContext) -> Result>, Box> { + let file = match app_ctx.file() {Some(f) => f, None => return Ok(None)}; + + let header = common::read_file(&file, 0, 16)?; + if header.starts_with(b"PHILIPS") { + if header[15].is_ascii_alphanumeric() { + Ok(Some(Box::new(PhilipsBdpCtx {header_type: HeaderType::New}))) + } else { + Ok(Some(Box::new(PhilipsBdpCtx {header_type: HeaderType::Old}))) + } + } else { + Ok(None) + } +} + +pub fn extract_philips_bdp(app_ctx: &AppContext, ctx: Box) -> Result<(), Box> { + let mut file = app_ctx.file().ok_or("Extractor expected file")?; + let ctx = ctx.downcast::().expect("Missing context"); + + let header: Box = match ctx.header_type { + HeaderType::Old => Box::new(file.read_le::()?), + HeaderType::New => Box::new(file.read_le::()?), + }; + + let header_size = file.stream_position()?; + + println!("File info -\nName: {}\nVersion: {}\nTarget size: {}\nEntry count: {}\nHeader type: {:?}\nHeader size: {}", + header.name(), header.version(), header.target_size(), header.target_num(), ctx.header_type, header_size); + + for (i, entry) in header.entries().iter().enumerate() { + if entry.id == 0xFF && entry.size == 0xFFFFFFFF { + break + } + + println!("\n#{} - ID: {:x}, IIC: {:x}, Version: {}, Offset: {}, Size: {}", + i+1, entry.id, entry.iic, entry.version(), entry.offset, entry.size); + + let data = common::read_file(&file, entry.offset as u64 + header_size, entry.size as usize)?; + + let out_data; + if entry.id == 0 && app_ctx.has_option("philips_bdp:decrypt") { + println!("- Decrypting..."); + out_data = bebin_decrypt_aes256cfb(&data, &KEY1, &IV1); + } else { + out_data = data; + } + + let output_path = Path::new(&app_ctx.output_dir).join(format!("{}.bin", entry.id)); + + 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)?; + + println!("- Saved file!"); + + //ID 0 should be the main MTK bdp file, since this is just an extra container for that format (like Sony BDP), so we can try to extract it here. + if entry.id == 0 { + println!("Checking if it's also MTK BDP..."); + + let new_file = File::open(&output_path)?; + //DUMB + let mtk_ctx: AppContext = AppContext { input: InputTarget::File(new_file), output_dir: app_ctx.output_dir.join("0"), options: app_ctx.options.clone() }; + + if let Some(result) = formats::mtk_bdp::is_mtk_bdp_file(&mtk_ctx)? { + println!("- MTK BDP file detected!\n"); + formats::mtk_bdp::extract_mtk_bdp(&mtk_ctx, result)?; + } else { + if app_ctx.has_option("philips_bdp:decrypt") { + println!("- Not an MTK BDP file"); + } else { + println!("- Not an MTK BDP file (try with decrypt?)"); + } + } + } + } + + Ok(()) +} \ No newline at end of file