RESTRUCTURING 1

- change formats to have their own folders with mod + include files + anything they want
- change format list to be defined in formats.rs instead of in each format
- move utils to their respective formats
- some minor code changes in some formats
This commit is contained in:
theubusu
2026-02-17 17:28:59 +01:00
parent 9fe39280cd
commit aa2249f024
69 changed files with 1678 additions and 1629 deletions
+49
View File
@@ -0,0 +1,49 @@
use binrw::BinRead;
pub static FILE_KEY: [u8; 16] = [
0x2A, 0x54, 0xA5, 0x30, 0xE0, 0x09, 0xA3, 0xDC,
0x03, 0xFB, 0xC3, 0x5E, 0x23, 0xA2, 0xC1, 0x0D,
];
pub static FILE_IV: [u8; 16] = [0x00; 16];
#[derive(BinRead)]
pub struct ImageHeader {
_empty: [u8; 8],
_magic_bytes: [u8; 8], //imgARMcC
_target_bytes: [u8; 4],
_platform_id: [u8; 4],
pub image_type: u32,
pub size1: u32,
_size2: u32,
pub data_start_offset: u32,
_data_link_address: u32,
_data_entry_point_offset: u32,
pub flags: u32,
_timestamp: u32,
_build_host: [u8; 4],
_unk: [u8; 4],
_rest_of_header: [u8; 192],
}
impl ImageHeader {
pub fn is_encrypted(&self) -> bool {
self.flags == 0x80
}
pub fn type_string(&self) -> &str {
if self.image_type == 0xa {
return "initfs"
} else if self.image_type == 0x18 {
return "uImage"
} else if self.image_type == 0x3 {
return "loader"
} else if self.image_type == 0xd {
return "app_cramfs"
} else if self.image_type == 0x6 {
return "Customization Package"
} else if self.image_type == 0xe {
return "firmware_blob"
} else {
return "unknown"
}
}
}
+100
View File
@@ -0,0 +1,100 @@
mod include;
use std::any::Any;
use crate::AppContext;
use std::fs::{self, OpenOptions};
use std::path::Path;
use std::io::{Write, Seek, Read, Cursor};
use tar::Archive;
use binrw::BinReaderExt;
use crate::utils::common;
use crate::utils::aes::{decrypt_aes128_cbc_nopad, decrypt_aes128_cbc_pcks7};
use include::*;
pub fn is_roku_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, 32)?;
let try_decrypt_header = decrypt_aes128_cbc_nopad(&header, &FILE_KEY, &FILE_IV)?;
if try_decrypt_header.starts_with(b"manifest\x00\x00\x00\x00\x00\x00\x00\x00") {
Ok(Some(Box::new(())))
} else {
Ok(None)
}
}
pub fn extract_roku(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 mut encrypted_data = Vec::new();
file.read_to_end(&mut encrypted_data)?;
println!("\nDecrypting...\n");
let tar_data = decrypt_aes128_cbc_pcks7(&encrypted_data, &FILE_KEY, &FILE_IV)?;
let tar_reader = Cursor::new(tar_data);
let mut tar_archive = Archive::new(tar_reader);
for entry_result in tar_archive.entries_with_seek()? {
let mut entry = entry_result?;
let path = entry.path()?.to_path_buf();
if path == std::path::Path::new("manifest") {
let size = entry.header().size()? as usize;
let mut contents = Vec::new();
entry.read_to_end(&mut contents)?;
let text = String::from_utf8_lossy(&contents[..size - 256]); //dont display signature
println!("Manifest file:\n{}", text);
} else {
let mut contents = Vec::new();
entry.read_to_end(&mut contents)?; //entry cant seek
if contents.starts_with(b"\x00\x00\x00\x00\x00\x00\x00\x00imgARMcC") {
println!("\nImage file: {:?}:", path);
let size = entry.header().size()? as usize;
let mut image_reader = Cursor::new(contents);
let mut i = 1;
while image_reader.stream_position()? < size as u64 {
let image: ImageHeader = image_reader.read_le()?;
println!("\nImage {} - Type: {:x}({}), Size: {}, Flags: {:x}{}, Data offset: {}",
i ,image.image_type, image.type_string(), image.size1, image.flags, if image.is_encrypted(){"(Encrypted)"}else{" "}, image.data_start_offset);
let data =
if image.data_start_offset == 0 {
common::read_exact(&mut image_reader, image.size1 as usize - 256)?
} else {
let _extra_data = common::read_exact(&mut image_reader, image.data_start_offset as usize - 256)?;
common::read_exact(&mut image_reader, image.size1 as usize - image.data_start_offset as usize)?
};
let folder_path = Path::new(&app_ctx.output_dir).join(&path);
let output_path = Path::new(&folder_path).join(format!("{}_{}.bin", i, image.type_string()));
fs::create_dir_all(&folder_path)?;
let mut out_file = OpenOptions::new().write(true).create(true).open(output_path)?;
out_file.write_all(&data)?;
println!("- Saved file!");
i += 1;
}
} else {
println!("\nOther/Unknown file: {:?}", path);
let output_path = Path::new(&app_ctx.output_dir).join(&path);
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(&contents)?;
println!("- Saved file!");
}
}
}
println!("\nExtraction finished!");
Ok(())
}