#squashfs #linux-kernel #linux #image-compression #deku #data-file

backhand

Library for the reading, creating, and modification of SquashFS file systems

25 releases (breaking)

0.19.0 Dec 1, 2024
0.18.0 May 25, 2024
0.17.0 May 6, 2024
0.15.0 Mar 24, 2024
0.11.0 Mar 15, 2023

#58 in Filesystem

Download history 1532/week @ 2024-08-20 1552/week @ 2024-08-27 1401/week @ 2024-09-03 1158/week @ 2024-09-10 987/week @ 2024-09-17 1163/week @ 2024-09-24 1381/week @ 2024-10-01 966/week @ 2024-10-08 1359/week @ 2024-10-15 1277/week @ 2024-10-22 1158/week @ 2024-10-29 1054/week @ 2024-11-05 614/week @ 2024-11-12 1252/week @ 2024-11-19 714/week @ 2024-11-26 818/week @ 2024-12-03

3,516 downloads per month
Used in 9 crates (7 directly)

MIT/Apache and maybe GPL-2.0

185KB
3.5K SLoC

backhand

github crates.io docs.rs build status Codecov

Library and binaries for the reading, creating, and modification of SquashFS file systems.

  • Library — Backhand provides an easy way for programmatic analysis of Squashfs 4.0 images, including the extraction and modification of images.
  • Feature Flags — Supported compression and decompression are feature flagged, so your final binary (or unsquashfs) only needs to include code to extract one type of image.
  • Unconventional Support — As well as supporting normal linux kernel SquashFS 4.0, we also support the "wonderful world of vendor formats" with a Kind struct. This allows changing the magic bytes, custom compression algorithms, and the Endian-ness of either the Data or Metadata fields.

Library

Compiler support: requires rustc 1.72.1+

Add the following to your Cargo.toml file:

[dependencies]
backhand = "0.19.0"

Target Support

Although additional targets may be supported, only the following have been fully tested or confirmed to build successfully.

Target build test
x86_64-unknown-linux-musl
aarch64-unknown-linux-musl
arm-unknown-linux-musleabi
armv7-unknown-linux-musleabi
aarch64-unknown-linux-musl
x86_64-apple-darwin
x86_64-pc-windows-gnu

Reading/Writing/Modifying Firmware

use std::fs::File;
use std::io::{Cursor, BufReader};
use backhand::{FilesystemReader, FilesystemWriter, NodeHeader};

// read
let file = BufReader::new(File::open("file.squashfs").unwrap());
let read_filesystem = FilesystemReader::from_reader(file).unwrap();

// convert to writer
let mut write_filesystem = FilesystemWriter::from_fs_reader(&read_filesystem).unwrap();

// add file with data from slice
let d = NodeHeader::default();
let bytes = Cursor::new(b"Fear is the mind-killer.");
write_filesystem.push_file(bytes, "a/d/e/new_file", d);

// add file with data from file
let new_file = File::open("dune").unwrap();
write_filesystem.push_file(new_file, "/root/dune", d);

// modify file
let bytes = Cursor::new(b"The sleeper must awaken.\n");
write_filesystem.replace_file("/a/b/c/d/e/first_file", bytes).unwrap();

// write into a new file
let mut output = File::create("modified.squashfs").unwrap();
write_filesystem.write(&mut output).unwrap();

Binaries

Compiler support: requires rustc 1.77+

These are currently under development and are missing features, MR's welcome!

To install, run cargo install backhand-cli --locked, or download from the latest github release.

See --help for more information.

Target Support

Although additional targets may be supported, only the following have been tested and included in our GitHub releases.

Target test release
x86_64-unknown-linux-musl
aarch64-unknown-linux-musl
arm-unknown-linux-musleabi
armv7-unknown-linux-musleabi
aarch64-unknown-linux-musl
x86_64-apple-darwin

unsquashfs-backhand

tool to uncompress, extract and list squashfs filesystems

Usage: unsquashfs-backhand [OPTIONS] [FILESYSTEM]

Arguments:
  [FILESYSTEM]  Squashfs file

Options:
  -o, --offset <BYTES>             Skip BYTES at the start of FILESYSTEM [default: 0]
  -a, --auto-offset                Find first instance of squashfs --kind magic
  -l, --list                       List filesystem, do not write to DEST (ignores --quiet)
  -d, --dest <PATHNAME>            Extract to [PATHNAME] [default: squashfs-root]
  -i, --info                       Print files as they are extracted
      --path-filter <PATH_FILTER>  Limit filesystem extraction [default: /]
  -f, --force                      If file already exists then overwrite
  -s, --stat                       Display filesystem superblock information (ignores --quiet)
  -k, --kind <KIND>                Kind(type of image) to parse [default: le_v4_0] [possible
                                   values: be_v4_0, le_v4_0, avm_be_v4_0]
      --completions <COMPLETIONS>  Emit shell completion scripts [possible values: bash, elvish,
                                   fish, powershell, zsh]
      --quiet                      Silence all progress bar and RUST_LOG output
  -h, --help                       Print help (see more with '--help')
  -V, --version                    Print version

add-backhand

tool to add a file or directory to squashfs filesystems

Usage: add-backhand [OPTIONS] <INPUT_IMAGE> <FILE_PATH_IN_IMAGE> <OUTPUT_IMAGE>

Arguments:
  <INPUT_IMAGE>         Squashfs input image
  <FILE_PATH_IN_IMAGE>  Path of file once inserted into squashfs
  <OUTPUT_IMAGE>        Squashfs output image path

Options:
  -d, --dir                     Create empty directory
  -f, --file <FILE>             Path of file to read, to write into squashfs
      --mode <MODE>             Override mode read from <FILE>
      --uid <UID>               Override uid read from <FILE>
      --gid <GID>               Override gid read from <FILE>
      --mtime <MTIME>           Override mtime read from <FILE>
      --pad-len <PAD_LEN>       Custom KiB padding length
      --no-compression-options  Don't emit compression options
  -h, --help                    Print help
  -V, --version                 Print version

replace-backhand

tool to replace files in squashfs filesystems

Usage: replace-backhand [OPTIONS] <INPUT_IMAGE> <FILE> <FILE_PATH_IN_IMAGE> <OUTPUT_IMAGE>

Arguments:
  <INPUT_IMAGE>         Squashfs input image
  <FILE>                Path of file to read, to write into squashfs
  <FILE_PATH_IN_IMAGE>  Path of file replaced in image
  <OUTPUT_IMAGE>        Squashfs output image

Options:
      --pad-len <PAD_LEN>       Custom KiB padding length
      --no-compression-options  Don't emit compression options
  -h, --help                    Print help
  -V, --version                 Print version

Performance

See BENCHMARK.md.

Testing

See backhand-test.

Dependencies

~9MB
~169K SLoC