Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for OpenWrt compression options #239

Merged
merged 1 commit into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/bin/unsquashfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ fn stat(args: Args, mut file: BufReader<File>, kind: Kind) {
println!("{superblock:#08x?}");

// show info about compression options
println!("Compression Options: {compression_options:#08x?}");
println!("Compression Options: {compression_options:#x?}");

// show info about flags
if superblock.inodes_uncompressed() {
Expand Down
73 changes: 56 additions & 17 deletions src/compressor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,48 @@ pub struct Lzo {
pub struct Xz {
pub dictionary_size: u32,
pub filters: XzFilter,

// the rest of these fields are from OpenWRT. These are optional, as the kernel will ignore
// these fields when seen. We follow the same behaviour and don't attempt to parse if the bytes
// for these aren't found
// TODO: both are currently unused in this library
// TODO: in openwrt, git-hash:f97ad870e11ebe5f3dcf833dda6c83b9165b37cb shows that before
// offical squashfs-tools had xz support they had the dictionary_size field as the last field
// in this struct. If we get test images, I guess we can support this in the future.
#[deku(cond = "!deku::rest.is_empty()")]
pub bit_opts: Option<u16>,
#[deku(cond = "!deku::rest.is_empty()")]
pub fb: Option<u16>,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, DekuRead, DekuWrite)]
#[deku(endian = "endian", ctx = "endian: deku::ctx::Endian")]
#[deku(type = "u32")]
#[rustfmt::skip]
pub enum XzFilter {
X86 = 0x01,
PowerPC = 0x02,
IA64 = 0x04,
Arm = 0x08,
ArmThumb = 0x10,
Sparc = 0x20,
pub struct XzFilter(u32);

impl XzFilter {
fn x86(&self) -> bool {
self.0 & 0x0001 == 0x0001
}

fn powerpc(&self) -> bool {
self.0 & 0x0002 == 0x0002
}

fn ia64(&self) -> bool {
self.0 & 0x0004 == 0x0004
}

fn arm(&self) -> bool {
self.0 & 0x0008 == 0x0008
}

fn armthumb(&self) -> bool {
self.0 & 0x0010 == 0x0010
}

fn sparc(&self) -> bool {
self.0 & 0x0020 == 0x0020
}
}

#[derive(Debug, DekuRead, DekuWrite, PartialEq, Eq, Clone, Copy)]
Expand Down Expand Up @@ -227,14 +256,24 @@ impl CompressionAction for DefaultCompressor {

let mut filters = Filters::new();
if let Some(CompressionOptions::Xz(xz)) = option {
match xz.filters {
XzFilter::X86 => filters.x86(),
XzFilter::PowerPC => filters.powerpc(),
XzFilter::IA64 => filters.ia64(),
XzFilter::Arm => filters.arm(),
XzFilter::ArmThumb => filters.arm_thumb(),
XzFilter::Sparc => filters.sparc(),
};
if xz.filters.x86() {
filters.x86();
}
if xz.filters.powerpc() {
filters.powerpc();
}
if xz.filters.ia64() {
filters.ia64();
}
if xz.filters.arm() {
filters.arm();
}
if xz.filters.armthumb() {
filters.arm_thumb();
}
if xz.filters.sparc() {
filters.sparc();
}
}
filters.lzma2(&opts);

Expand Down
58 changes: 15 additions & 43 deletions src/squashfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,24 +117,6 @@ pub struct SuperBlock {
pub const NOT_SET: u64 = 0xffff_ffff_ffff_ffff;

impl SuperBlock {
/// Extract size of optional compression options
fn compression_options_size(&self) -> Option<usize> {
if self.compressor_options_are_present() {
let size = match self.compressor {
Compressor::Lzma => 0,
Compressor::Gzip => 8,
Compressor::Lzo => 8,
Compressor::Xz => 8,
Compressor::Lz4 => 8,
Compressor::Zstd => 4,
Compressor::None => 0,
};
Some(size)
} else {
None
}
}

/// flag value
pub fn inodes_uncompressed(&self) -> bool {
self.flags & Flags::InodesStoredUncompressed as u16 != 0
Expand Down Expand Up @@ -302,33 +284,23 @@ impl Squashfs {

// Parse Compression Options, if any
info!("Reading Compression options");
let compression_options = if superblock.compressor != Compressor::None {
match superblock.compression_options_size() {
Some(size) => {
let bytes = metadata::read_block(reader, &superblock, kind)?;

// Some firmware (such as openwrt) that uses XZ compression has an extra 4 bytes.
// squashfs-tools/unsquashfs complains about this also
if bytes.len() != size {
tracing::warn!(
"Non standard compression options! CompressionOptions might be incorrect: {:02x?}",
bytes
);
}
// data -> compression options
let bv = BitVec::from_slice(&bytes);
match CompressionOptions::read(
&bv,
(deku::ctx::Endian::Little, superblock.compressor),
) {
Ok(co) => Some(co.1),
Err(e) => {
error!("invalid compression options: {e:?}[{bytes:02x?}], not using");
None
},
let compression_options = if superblock.compressor != Compressor::None
&& superblock.compressor_options_are_present()
{
let bytes = metadata::read_block(reader, &superblock, kind)?;
// data -> compression options
let bv = BitVec::from_slice(&bytes);
match CompressionOptions::read(&bv, (kind.inner.type_endian, superblock.compressor)) {
Ok(co) => {
if !co.0.is_empty() {
error!("invalid compression options, bytes left over, using");
}
Some(co.1)
},
Err(e) => {
error!("invalid compression options: {e:?}[{bytes:02x?}], not using");
None
},
None => None,
}
} else {
None
Expand Down
6 changes: 5 additions & 1 deletion tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ fn full_test(
let file = BufReader::new(File::open(&og_path).unwrap());
info!("calling from_reader");
let og_filesystem = FilesystemReader::from_reader_with_offset(file, offset).unwrap();
let og_comp_opts = og_filesystem.compression_options;
let mut new_filesystem = FilesystemWriter::from_fs_reader(&og_filesystem).unwrap();

// convert to bytes
Expand All @@ -51,7 +52,10 @@ fn full_test(
// assert that our library can atleast read the output, use unsquashfs to really assert this
info!("calling from_reader");
let created_file = BufReader::new(File::open(&new_path).unwrap());
let _new_filesystem = FilesystemReader::from_reader_with_offset(created_file, offset).unwrap();
let written_new_filesystem =
FilesystemReader::from_reader_with_offset(created_file, offset).unwrap();
let new_comp_opts = written_new_filesystem.compression_options;
assert_eq!(og_comp_opts, new_comp_opts);

match verify {
Verify::Extract => {
Expand Down