Files
unixtract/src/formats/novatek.rs
T

86 lines
2.5 KiB
Rust
Raw Normal View History

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: "novatek", detector_func: is_novatek_file, extractor_func: extract_novatek }
2026-02-05 15:18:26 +01:00
}
2025-10-07 00:04:47 +02:00
use std::path::Path;
use std::fs::{self, OpenOptions};
use std::io::{Write};
2025-10-07 00:04:47 +02:00
use binrw::{BinRead, BinReaderExt};
use crate::utils::common;
2025-10-07 00:04:47 +02:00
2025-10-10 22:04:51 +02:00
#[derive(BinRead)]
struct Header {
#[br(count = 4)] _magic_bytes: Vec<u8>,
version_major: u32,
version_minor: u32,
_unused: u32,
#[br(count = 16)] firmware_name_bytes: Vec<u8>,
data_size: u32,
#[br(count = 16)] _md5_checksum: Vec<u8>, //data checksum
part_count: u32,
_data_start_offset: u32,
#[br(count = 128)] _signature: Vec<u8>,
_header_checksum: u32, //CRC32, calculated with the field set to 0
}
impl Header {
fn firmware_name(&self) -> String {
common::string_from_bytes(&self.firmware_name_bytes)
}
}
2025-10-10 22:04:51 +02:00
#[derive(BinRead)]
struct PartEntry {
id: u32,
size: u32,
offset: u32,
#[br(count = 16)] _md5_checksum: Vec<u8>,
}
2026-02-05 18:53:35 +01:00
pub fn is_novatek_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, 4)?;
if header == b"NFWB" {
2026-02-05 15:18:26 +01:00
Ok(Some(Box::new(())))
2025-10-07 00:04:47 +02:00
} else {
2026-02-05 15:18:26 +01:00
Ok(None)
2025-10-07 00:04:47 +02:00
}
}
2026-02-05 18:53:35 +01:00
pub fn extract_novatek(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;
let header: Header = file.read_le()?;
2025-10-07 00:04:47 +02:00
println!("File info:\nFirmware name: {}\nVersion: {}.{}\nData size: {}\nPart count: {}",
header.firmware_name(), header.version_major, header.version_minor, header.data_size, header.part_count);
2025-10-07 00:04:47 +02:00
let mut entries: Vec<PartEntry> = Vec::new();
for _i in 0..header.part_count {
let part: PartEntry = file.read_le()?;
entries.push(part);
}
2025-10-07 00:04:47 +02:00
let mut e_i = 0;
for entry in &entries {
e_i += 1;
println!("\n({}/{}) - ID: {}, Offset: {}, Size: {}", e_i, entries.len(), entry.id, entry.offset, entry.size);
2025-10-07 00:04:47 +02:00
let data = common::read_file(&file, entry.offset as u64, entry.size as usize)?;
2025-10-07 00:04:47 +02:00
2026-02-05 15:18:26 +01:00
let output_path = Path::new(app_ctx.output_dir).join(format!("{}_{}.bin", e_i, entry.id));
2025-10-07 00:04:47 +02:00
2026-02-05 15:18:26 +01:00
fs::create_dir_all(app_ctx.output_dir)?;
2025-10-07 00:04:47 +02:00
let mut out_file = OpenOptions::new()
.write(true)
.create(true)
.open(output_path)?;
out_file.write_all(&data)?;
println!("- Saved file!");
2025-10-07 00:04:47 +02:00
}
Ok(())
}