diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml deleted file mode 100644 index b0caeb0..0000000 --- a/.github/workflows/release.yaml +++ /dev/null @@ -1,159 +0,0 @@ -name: release -on: - push: - branches: [mainline] - pull_request: - workflow_dispatch: -env: - CARGO_INCREMENTAL: 0 -permissions: - contents: write - -jobs: - release: - name: ${{ matrix.target }} - runs-on: ${{ matrix.os }} - strategy: - fail-fast: false - matrix: - include: - # MUSL Targets - - os: ubuntu-latest - target: x86_64-unknown-linux-musl - - os: ubuntu-latest - target: arm-unknown-linux-musleabihf - - os: ubuntu-latest - target: armv7-unknown-linux-musleabihf - - os: ubuntu-latest - target: aarch64-unknown-linux-musl - - os: ubuntu-latest - target: i686-unknown-linux-musl - - # Android targets - - os: ubuntu-latest - target: aarch64-linux-android - - os: ubuntu-latest - target: armv7-linux-androideabi - - # Darwin targets - - os: macos-latest - target: x86_64-apple-darwin - - os: macos-latest - target: aarch64-apple-darwin - - # Windows targets - - os: windows-latest - target: x86_64-pc-windows-msvc - - os: windows-latest - target: aarch64-pc-windows-msvc - - steps: - - name: Checkout repository - uses: actions/checkout@v4 - with: - fetch-depth: 0 - - - name: Get version - id: get_version - uses: SebRollen/toml-action@v1.2.0 - with: - file: Cargo.toml - field: package.version - - - name: Install Rust - uses: dtolnay/rust-toolchain@stable - with: - targets: ${{ matrix.target }} - - - name: Setup cache - uses: Swatinem/rust-cache@v2 - with: - key: ${{ matrix.target }} - - - name: Install build deps (macOS) - if: runner.os == 'macOS' - run: brew install protobuf - - - name: Install build deps (Linux) - if: runner.os == 'Linux' - run: | - sudo apt-get update - sudo apt-get install -y protobuf-compiler - - - name: Install zig - if: runner.os == 'Linux' && !contains(matrix.target, 'android') - uses: mlugg/setup-zig@v2 - - - name: Install cargo-zigbuild - if: runner.os == 'Linux' && !contains(matrix.target, 'android') - uses: taiki-e/install-action@v2 - with: - tool: cargo-zigbuild - - - name: Install cargo-ndk - if: contains(matrix.target, 'android') - uses: taiki-e/install-action@v2 - with: - tool: cargo-ndk - - - name: Install Android NDK - if: contains(matrix.target, 'android') - uses: nttld/setup-ndk@v1 - with: - ndk-version: r23 - local-cache: true - - - name: Install protoc (Windows) - if: runner.os == 'Windows' - uses: arduino/setup-protoc@v1 - with: - version: "3.x" - - - name: Build binary (Linux) - if: runner.os == 'Linux' && !contains(matrix.target, 'android') - run: | - cargo zigbuild --release --locked --target=${{ matrix.target }} --color=always --verbose - - - name: Build binary (Android) - if: contains(matrix.target, 'android') - run: | - cargo ndk --target=${{ matrix.target }} build --release --locked --color=always --verbose - - - name: Build binary (macOS / Windows) - if: runner.os == 'macOS' || runner.os == 'Windows' - run: | - cargo build --release --locked --target=${{ matrix.target }} --color=always --verbose - - - name: Package (*nix) - if: runner.os != 'Windows' - run: > - tar -cv - LICENSE README.md - -C target/${{ matrix.target }}/release/ otadump - | gzip --best - > 'otadump-${{ steps.get_version.outputs.value }}-${{ matrix.target }}.tar.gz' - - name: Package (Windows) - if: runner.os == 'Windows' - run: > - 7z a 'otadump-${{ steps.get_version.outputs.value }}-${{ matrix.target }}.zip' - LICENSE README.md - ./target/${{ matrix.target }}/release/otadump.exe - - name: Upload artifact - uses: actions/upload-artifact@v4.6.0 - with: - name: ${{ matrix.target }} - path: | - *.deb - *.tar.gz - *.zip - - name: Create release - if: ${{ github.ref == 'refs/heads/mainline' && startsWith(github.event.head_commit.message, 'chore(release)') }} - uses: softprops/action-gh-release@v1 - with: - draft: true - files: | - *.deb - *.tar.gz - *.zip - name: ${{ steps.get_version.outputs.value }} - tag_name: "" diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..9f9214d --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,35 @@ +name: test + +on: + pull_request: + push: + branches: [mainline] + +permissions: + contents: read + +jobs: + python: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.9", "3.13"] + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + - run: python -m unittest discover -s tests -v + - run: python -m pip install build + - run: python -m build + - run: python -m pip install dist/*.whl + - run: python -c "import otadump; assert callable(otadump.extract)" + + artifact: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: "3.13" + - run: python tests/artifact_smoke.py diff --git a/.gitignore b/.gitignore index 43d1f25..e9319cd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,15 +1,6 @@ -### Rust ### -# Generated by Cargo -# will have compiled files and executables -debug/ dist/ -target/ - -# These are backup files generated by rustfmt -**/*.rs.bk - -# MSVC Windows builds of rustc generate these, which store debugging information -*.pdb +*.egg-info/ +.venv/ ### Custom ### __pycache__/ diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 0000000..a7ae026 --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,5 @@ +# Testing + +- Run `python -m unittest discover -s tests -v` for every change. +- Unit tests must not use the network or the user's real artifact cache. +- Run `python tests/artifact_smoke.py` when changing artifact acquisition or process invocation. diff --git a/Cargo.lock b/Cargo.lock deleted file mode 100644 index 370ea0e..0000000 --- a/Cargo.lock +++ /dev/null @@ -1,1212 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 4 - -[[package]] -name = "aho-corasick" -version = "1.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" -dependencies = [ - "memchr", -] - -[[package]] -name = "anstream" -version = "0.6.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "301af1932e46185686725e0fad2f8f2aa7da69dd70bf6ecc44d6b703844a3933" -dependencies = [ - "anstyle", - "anstyle-parse", - "anstyle-query", - "anstyle-wincon", - "colorchoice", - "is_terminal_polyfill", - "utf8parse", -] - -[[package]] -name = "anstyle" -version = "1.0.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "862ed96ca487e809f1c8e5a8447f6ee2cf102f846893800b20cebdf541fc6bbd" - -[[package]] -name = "anstyle-parse" -version = "0.2.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2" -dependencies = [ - "utf8parse", -] - -[[package]] -name = "anstyle-query" -version = "1.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c8bdeb6047d8983be085bab0ba1472e6dc604e7041dbf6fcd5e71523014fae9" -dependencies = [ - "windows-sys 0.59.0", -] - -[[package]] -name = "anstyle-wincon" -version = "3.0.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "403f75924867bb1033c59fbf0797484329750cfbe3c4325cd33127941fabc882" -dependencies = [ - "anstyle", - "once_cell_polyfill", - "windows-sys 0.59.0", -] - -[[package]] -name = "anyhow" -version = "1.0.98" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e16d2d3311acee920a9eb8d33b8cbc1787ce4a264e85f964c2404b969bdcd487" - -[[package]] -name = "arbitrary" -version = "1.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dde20b3d026af13f561bdd0f15edf01fc734f0dafcedbaf42bba506a9517f223" -dependencies = [ - "derive_arbitrary", -] - -[[package]] -name = "bitflags" -version = "2.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967" - -[[package]] -name = "bumpalo" -version = "3.18.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "793db76d6187cd04dff33004d8e6c9cc4e05cd330500379d2394209271b4aeee" - -[[package]] -name = "bytes" -version = "1.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d71b6127be86fdcfddb610f7182ac57211d4b18a3e9c82eb2d17662f2227ad6a" - -[[package]] -name = "bzip2" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bea8dcd42434048e4f7a304411d9273a411f647446c1234a65ce0554923f4cff" -dependencies = [ - "libbz2-rs-sys", -] - -[[package]] -name = "cc" -version = "1.2.27" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d487aa071b5f64da6f19a3e848e3578944b726ee5a4854b82172f02aa876bfdc" -dependencies = [ - "jobserver", - "libc", - "shlex", -] - -[[package]] -name = "cfg-if" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9555578bc9e57714c812a1f84e4fc5b4d21fcb063490c624de019f7464c91268" - -[[package]] -name = "clap" -version = "4.5.40" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "40b6887a1d8685cebccf115538db5c0efe625ccac9696ad45c409d96566e910f" -dependencies = [ - "clap_builder", - "clap_derive", -] - -[[package]] -name = "clap_builder" -version = "4.5.40" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0c66c08ce9f0c698cbce5c0279d0bb6ac936d8674174fe48f736533b964f59e" -dependencies = [ - "anstream", - "anstyle", - "clap_lex", - "strsim", -] - -[[package]] -name = "clap_derive" -version = "4.5.40" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2c7947ae4cc3d851207c1adb5b5e260ff0cca11446b1d6d1423788e442257ce" -dependencies = [ - "heck", - "proc-macro2", - "quote", - "syn 2.0.104", -] - -[[package]] -name = "clap_lex" -version = "0.7.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b94f61472cee1439c0b966b47e3aca9ae07e45d070759512cd390ea2bebc6675" - -[[package]] -name = "color-print" -version = "0.3.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3aa954171903797d5623e047d9ab69d91b493657917bdfb8c2c80ecaf9cdb6f4" -dependencies = [ - "color-print-proc-macro", -] - -[[package]] -name = "color-print-proc-macro" -version = "0.3.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "692186b5ebe54007e45a59aea47ece9eb4108e141326c304cdc91699a7118a22" -dependencies = [ - "nom", - "proc-macro2", - "quote", - "syn 2.0.104", -] - -[[package]] -name = "colorchoice" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" - -[[package]] -name = "console" -version = "0.15.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "054ccb5b10f9f2cbf51eb355ca1d05c2d279ce1804688d0db74b4733a5aeafd8" -dependencies = [ - "encode_unicode", - "libc", - "once_cell", - "unicode-width", - "windows-sys 0.59.0", -] - -[[package]] -name = "crc32fast" -version = "1.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3" -dependencies = [ - "cfg-if", -] - -[[package]] -name = "crossbeam-deque" -version = "0.8.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51" -dependencies = [ - "crossbeam-epoch", - "crossbeam-utils", -] - -[[package]] -name = "crossbeam-epoch" -version = "0.9.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" -dependencies = [ - "crossbeam-utils", -] - -[[package]] -name = "crossbeam-utils" -version = "0.8.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" - -[[package]] -name = "derive_arbitrary" -version = "1.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30542c1ad912e0e3d22a1935c290e12e8a29d704a420177a31faad4a601a0800" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.104", -] - -[[package]] -name = "either" -version = "1.15.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" - -[[package]] -name = "encode_unicode" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34aa73646ffb006b8f5147f3dc182bd4bcb190227ce861fc4a4844bf8e3cb2c0" - -[[package]] -name = "equivalent" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" - -[[package]] -name = "errno" -version = "0.3.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "778e2ac28f6c47af28e4907f13ffd1e1ddbd400980a9abd7c8df189bf578a5ad" -dependencies = [ - "libc", - "windows-sys 0.60.2", -] - -[[package]] -name = "fastrand" -version = "2.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" - -[[package]] -name = "fixedbitset" -version = "0.5.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d674e81391d1e1ab681a28d99df07927c6d4aa5b027d7da16ba32d1d21ecd99" - -[[package]] -name = "getrandom" -version = "0.2.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "335ff9f135e4384c8150d6f27c6daed433577f86b4750418338c01a1a2528592" -dependencies = [ - "cfg-if", - "libc", - "wasi 0.11.1+wasi-snapshot-preview1", -] - -[[package]] -name = "getrandom" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26145e563e54f2cadc477553f1ec5ee650b00862f0a58bcd12cbdc5f0ea2d2f4" -dependencies = [ - "cfg-if", - "libc", - "r-efi", - "wasi 0.14.2+wasi-0.2.4", -] - -[[package]] -name = "hashbrown" -version = "0.15.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5971ac85611da7067dbfcabef3c70ebb5606018acd9e2a3903a0da507521e0d5" - -[[package]] -name = "heck" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" - -[[package]] -name = "hex" -version = "0.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" - -[[package]] -name = "indexmap" -version = "2.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cea70ddb795996207ad57735b50c5982d8844f38ba9ee5f1aedcfb708a2aa11e" -dependencies = [ - "equivalent", - "hashbrown", -] - -[[package]] -name = "indicatif" -version = "0.17.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "183b3088984b400f4cfac3620d5e076c84da5364016b4f49473de574b2586235" -dependencies = [ - "console", - "number_prefix", - "portable-atomic", - "unicode-width", - "web-time", -] - -[[package]] -name = "is_terminal_polyfill" -version = "1.70.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" - -[[package]] -name = "itertools" -version = "0.14.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285" -dependencies = [ - "either", -] - -[[package]] -name = "jobserver" -version = "0.1.33" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38f262f097c174adebe41eb73d66ae9c06b2844fb0da69969647bbddd9b0538a" -dependencies = [ - "getrandom 0.3.3", - "libc", -] - -[[package]] -name = "js-sys" -version = "0.3.77" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f" -dependencies = [ - "once_cell", - "wasm-bindgen", -] - -[[package]] -name = "libbz2-rs-sys" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "775bf80d5878ab7c2b1080b5351a48b2f737d9f6f8b383574eebcc22be0dfccb" - -[[package]] -name = "libc" -version = "0.2.174" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1171693293099992e19cddea4e8b849964e9846f4acee11b3948bcc337be8776" - -[[package]] -name = "liblzma" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0791ab7e08ccc8e0ce893f6906eb2703ed8739d8e89b57c0714e71bad09024c8" -dependencies = [ - "liblzma-sys", -] - -[[package]] -name = "liblzma-sys" -version = "0.4.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "01b9596486f6d60c3bbe644c0e1be1aa6ccc472ad630fe8927b456973d7cb736" -dependencies = [ - "cc", - "libc", - "pkg-config", -] - -[[package]] -name = "linux-raw-sys" -version = "0.9.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd945864f07fe9f5371a27ad7b52a172b4b499999f1d97574c9fa68373937e12" - -[[package]] -name = "log" -version = "0.4.27" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94" - -[[package]] -name = "memchr" -version = "2.7.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32a282da65faaf38286cf3be983213fcf1d2e2a58700e808f83f4ea9a4804bc0" - -[[package]] -name = "memmap2" -version = "0.9.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd3f7eed9d3848f8b98834af67102b720745c4ec028fcd0aa0239277e7de374f" -dependencies = [ - "libc", -] - -[[package]] -name = "minimal-lexical" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" - -[[package]] -name = "multimap" -version = "0.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d87ecb2933e8aeadb3e3a02b828fed80a7528047e68b4f424523a0981a3a084" - -[[package]] -name = "nom" -version = "7.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" -dependencies = [ - "memchr", - "minimal-lexical", -] - -[[package]] -name = "nom-derive" -version = "0.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ff943d68b88d0b87a6e0d58615e8fa07f9fd5a1319fa0a72efc1f62275c79a7" -dependencies = [ - "nom", - "nom-derive-impl", - "rustversion", -] - -[[package]] -name = "nom-derive-impl" -version = "0.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd0b9a93a84b0d3ec3e70e02d332dc33ac6dfac9cde63e17fcb77172dededa62" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", -] - -[[package]] -name = "number_prefix" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3" - -[[package]] -name = "once_cell" -version = "1.21.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" - -[[package]] -name = "once_cell_polyfill" -version = "1.70.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4895175b425cb1f87721b59f0f286c2092bd4af812243672510e1ac53e2e0ad" - -[[package]] -name = "otadump" -version = "0.1.3" -dependencies = [ - "anyhow", - "bzip2", - "clap", - "color-print", - "hex", - "indicatif", - "liblzma", - "memmap2", - "nom", - "nom-derive", - "prost", - "prost-build", - "protoc-bin-vendored", - "pyo3", - "rayon", - "ring", - "sync-unsafe-cell", - "tempfile", - "zip", - "zstd", -] - -[[package]] -name = "petgraph" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3672b37090dbd86368a4145bc067582552b29c27377cad4e0a306c97f9bd7772" -dependencies = [ - "fixedbitset", - "indexmap", -] - -[[package]] -name = "pkg-config" -version = "0.3.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c" - -[[package]] -name = "portable-atomic" -version = "1.11.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f84267b20a16ea918e43c6a88433c2d54fa145c92a811b5b047ccbe153674483" - -[[package]] -name = "prettyplease" -version = "0.2.35" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "061c1221631e079b26479d25bbf2275bfe5917ae8419cd7e34f13bfc2aa7539a" -dependencies = [ - "proc-macro2", - "syn 2.0.104", -] - -[[package]] -name = "proc-macro2" -version = "1.0.95" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "prost" -version = "0.14.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7231bd9b3d3d33c86b58adbac74b5ec0ad9f496b19d22801d773636feaa95f3d" -dependencies = [ - "bytes", - "prost-derive", -] - -[[package]] -name = "prost-build" -version = "0.14.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac6c3320f9abac597dcbc668774ef006702672474aad53c6d596b62e487b40b1" -dependencies = [ - "heck", - "itertools", - "log", - "multimap", - "once_cell", - "petgraph", - "prettyplease", - "prost", - "prost-types", - "regex", - "syn 2.0.104", - "tempfile", -] - -[[package]] -name = "prost-derive" -version = "0.14.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9120690fafc389a67ba3803df527d0ec9cbbc9cc45e4cc20b332996dfb672425" -dependencies = [ - "anyhow", - "itertools", - "proc-macro2", - "quote", - "syn 2.0.104", -] - -[[package]] -name = "prost-types" -version = "0.14.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9b4db3d6da204ed77bb26ba83b6122a73aeb2e87e25fbf7ad2e84c4ccbf8f72" -dependencies = [ - "prost", -] - -[[package]] -name = "protoc-bin-vendored" -version = "3.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d1c381df33c98266b5f08186583660090a4ffa0889e76c7e9a5e175f645a67fa" -dependencies = [ - "protoc-bin-vendored-linux-aarch_64", - "protoc-bin-vendored-linux-ppcle_64", - "protoc-bin-vendored-linux-s390_64", - "protoc-bin-vendored-linux-x86_32", - "protoc-bin-vendored-linux-x86_64", - "protoc-bin-vendored-macos-aarch_64", - "protoc-bin-vendored-macos-x86_64", - "protoc-bin-vendored-win32", -] - -[[package]] -name = "protoc-bin-vendored-linux-aarch_64" -version = "3.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c350df4d49b5b9e3ca79f7e646fde2377b199e13cfa87320308397e1f37e1a4c" - -[[package]] -name = "protoc-bin-vendored-linux-ppcle_64" -version = "3.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a55a63e6c7244f19b5c6393f025017eb5d793fd5467823a099740a7a4222440c" - -[[package]] -name = "protoc-bin-vendored-linux-s390_64" -version = "3.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1dba5565db4288e935d5330a07c264a4ee8e4a5b4a4e6f4e83fad824cc32f3b0" - -[[package]] -name = "protoc-bin-vendored-linux-x86_32" -version = "3.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8854774b24ee28b7868cd71dccaae8e02a2365e67a4a87a6cd11ee6cdbdf9cf5" - -[[package]] -name = "protoc-bin-vendored-linux-x86_64" -version = "3.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b38b07546580df720fa464ce124c4b03630a6fb83e05c336fea2a241df7e5d78" - -[[package]] -name = "protoc-bin-vendored-macos-aarch_64" -version = "3.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89278a9926ce312e51f1d999fee8825d324d603213344a9a706daa009f1d8092" - -[[package]] -name = "protoc-bin-vendored-macos-x86_64" -version = "3.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81745feda7ccfb9471d7a4de888f0652e806d5795b61480605d4943176299756" - -[[package]] -name = "protoc-bin-vendored-win32" -version = "3.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95067976aca6421a523e491fce939a3e65249bac4b977adee0ee9771568e8aa3" - -[[package]] -name = "pyo3" -version = "0.29.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4688ddedf473e32662b9b067670129a8afb8c18e351482c70d62ba4a88171e8b" -dependencies = [ - "libc", - "once_cell", - "portable-atomic", - "pyo3-build-config", - "pyo3-ffi", - "pyo3-macros", -] - -[[package]] -name = "pyo3-build-config" -version = "0.29.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f41027e41b4bd03f6e60f9f417fe24a6341a6bb744edd62b6f709f2a52ea30e9" -dependencies = [ - "target-lexicon", -] - -[[package]] -name = "pyo3-ffi" -version = "0.29.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e591a95526fead067432c3b3a33fc74770b87b1e04e73671090d9c2055a2b327" -dependencies = [ - "libc", - "pyo3-build-config", -] - -[[package]] -name = "pyo3-macros" -version = "0.29.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73225868fc1cd84eef2c3c230ddb91273bf1de46aeb8a4248da76d32a0924a1c" -dependencies = [ - "proc-macro2", - "pyo3-macros-backend", - "quote", - "syn 2.0.104", -] - -[[package]] -name = "pyo3-macros-backend" -version = "0.29.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "571575aa3749fa6216757dd47d2a3e7ef360f329a40f0666a9fbd14889024952" -dependencies = [ - "heck", - "proc-macro2", - "quote", - "syn 2.0.104", -] - -[[package]] -name = "quote" -version = "1.0.40" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" -dependencies = [ - "proc-macro2", -] - -[[package]] -name = "r-efi" -version = "5.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" - -[[package]] -name = "rayon" -version = "1.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa" -dependencies = [ - "either", - "rayon-core", -] - -[[package]] -name = "rayon-core" -version = "1.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2" -dependencies = [ - "crossbeam-deque", - "crossbeam-utils", -] - -[[package]] -name = "regex" -version = "1.11.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" -dependencies = [ - "aho-corasick", - "memchr", - "regex-automata", - "regex-syntax", -] - -[[package]] -name = "regex-automata" -version = "0.4.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" -dependencies = [ - "aho-corasick", - "memchr", - "regex-syntax", -] - -[[package]] -name = "regex-syntax" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" - -[[package]] -name = "ring" -version = "0.17.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7" -dependencies = [ - "cc", - "cfg-if", - "getrandom 0.2.16", - "libc", - "untrusted", - "windows-sys 0.52.0", -] - -[[package]] -name = "rustix" -version = "1.0.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c71e83d6afe7ff64890ec6b71d6a69bb8a610ab78ce364b3352876bb4c801266" -dependencies = [ - "bitflags", - "errno", - "libc", - "linux-raw-sys", - "windows-sys 0.59.0", -] - -[[package]] -name = "rustversion" -version = "1.0.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a0d197bd2c9dc6e53b84da9556a69ba4cdfab8619eb41a8bd1cc2027a0f6b1d" - -[[package]] -name = "shlex" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" - -[[package]] -name = "strsim" -version = "0.11.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" - -[[package]] -name = "syn" -version = "1.0.109" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - -[[package]] -name = "syn" -version = "2.0.104" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "17b6f705963418cdb9927482fa304bc562ece2fdd4f616084c50b7023b435a40" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - -[[package]] -name = "sync-unsafe-cell" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8deaecba5382c095cb432cd1e21068dadb144208f057b13720e76bf89749beb4" - -[[package]] -name = "target-lexicon" -version = "0.13.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "adb6935a6f5c20170eeceb1a3835a49e12e19d792f6dd344ccc76a985ca5a6ca" - -[[package]] -name = "tempfile" -version = "3.20.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8a64e3985349f2441a1a9ef0b853f869006c3855f2cda6862a94d26ebb9d6a1" -dependencies = [ - "fastrand", - "getrandom 0.3.3", - "once_cell", - "rustix", - "windows-sys 0.59.0", -] - -[[package]] -name = "unicode-ident" -version = "1.0.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" - -[[package]] -name = "unicode-width" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4a1a07cc7db3810833284e8d372ccdc6da29741639ecc70c9ec107df0fa6154c" - -[[package]] -name = "untrusted" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" - -[[package]] -name = "utf8parse" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" - -[[package]] -name = "wasi" -version = "0.11.1+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" - -[[package]] -name = "wasi" -version = "0.14.2+wasi-0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3" -dependencies = [ - "wit-bindgen-rt", -] - -[[package]] -name = "wasm-bindgen" -version = "0.2.100" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5" -dependencies = [ - "cfg-if", - "once_cell", - "wasm-bindgen-macro", -] - -[[package]] -name = "wasm-bindgen-backend" -version = "0.2.100" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6" -dependencies = [ - "bumpalo", - "log", - "proc-macro2", - "quote", - "syn 2.0.104", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-macro" -version = "0.2.100" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407" -dependencies = [ - "quote", - "wasm-bindgen-macro-support", -] - -[[package]] -name = "wasm-bindgen-macro-support" -version = "0.2.100" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.104", - "wasm-bindgen-backend", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-shared" -version = "0.2.100" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "web-time" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a6580f308b1fad9207618087a65c04e7a10bc77e02c8e84e9b00dd4b12fa0bb" -dependencies = [ - "js-sys", - "wasm-bindgen", -] - -[[package]] -name = "windows-sys" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" -dependencies = [ - "windows-targets 0.52.6", -] - -[[package]] -name = "windows-sys" -version = "0.59.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" -dependencies = [ - "windows-targets 0.52.6", -] - -[[package]] -name = "windows-sys" -version = "0.60.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2f500e4d28234f72040990ec9d39e3a6b950f9f22d3dba18416c35882612bcb" -dependencies = [ - "windows-targets 0.53.2", -] - -[[package]] -name = "windows-targets" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" -dependencies = [ - "windows_aarch64_gnullvm 0.52.6", - "windows_aarch64_msvc 0.52.6", - "windows_i686_gnu 0.52.6", - "windows_i686_gnullvm 0.52.6", - "windows_i686_msvc 0.52.6", - "windows_x86_64_gnu 0.52.6", - "windows_x86_64_gnullvm 0.52.6", - "windows_x86_64_msvc 0.52.6", -] - -[[package]] -name = "windows-targets" -version = "0.53.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c66f69fcc9ce11da9966ddb31a40968cad001c5bedeb5c2b82ede4253ab48aef" -dependencies = [ - "windows_aarch64_gnullvm 0.53.0", - "windows_aarch64_msvc 0.53.0", - "windows_i686_gnu 0.53.0", - "windows_i686_gnullvm 0.53.0", - "windows_i686_msvc 0.53.0", - "windows_x86_64_gnu 0.53.0", - "windows_x86_64_gnullvm 0.53.0", - "windows_x86_64_msvc 0.53.0", -] - -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" - -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.53.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86b8d5f90ddd19cb4a147a5fa63ca848db3df085e25fee3cc10b39b6eebae764" - -[[package]] -name = "windows_aarch64_msvc" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" - -[[package]] -name = "windows_aarch64_msvc" -version = "0.53.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7651a1f62a11b8cbd5e0d42526e55f2c99886c77e007179efff86c2b137e66c" - -[[package]] -name = "windows_i686_gnu" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" - -[[package]] -name = "windows_i686_gnu" -version = "0.53.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1dc67659d35f387f5f6c479dc4e28f1d4bb90ddd1a5d3da2e5d97b42d6272c3" - -[[package]] -name = "windows_i686_gnullvm" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" - -[[package]] -name = "windows_i686_gnullvm" -version = "0.53.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ce6ccbdedbf6d6354471319e781c0dfef054c81fbc7cf83f338a4296c0cae11" - -[[package]] -name = "windows_i686_msvc" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" - -[[package]] -name = "windows_i686_msvc" -version = "0.53.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "581fee95406bb13382d2f65cd4a908ca7b1e4c2f1917f143ba16efe98a589b5d" - -[[package]] -name = "windows_x86_64_gnu" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" - -[[package]] -name = "windows_x86_64_gnu" -version = "0.53.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e55b5ac9ea33f2fc1716d1742db15574fd6fc8dadc51caab1c16a3d3b4190ba" - -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" - -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.53.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0a6e035dd0599267ce1ee132e51c27dd29437f63325753051e71dd9e42406c57" - -[[package]] -name = "windows_x86_64_msvc" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" - -[[package]] -name = "windows_x86_64_msvc" -version = "0.53.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "271414315aff87387382ec3d271b52d7ae78726f5d44ac98b4f4030c91880486" - -[[package]] -name = "wit-bindgen-rt" -version = "0.39.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1" -dependencies = [ - "bitflags", -] - -[[package]] -name = "zip" -version = "4.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af7dcdb4229c0e79c2531a24de7726a0e980417a74fb4d030a35f535665439a0" -dependencies = [ - "arbitrary", - "crc32fast", - "indexmap", - "memchr", -] - -[[package]] -name = "zstd" -version = "0.13.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e91ee311a569c327171651566e07972200e76fcfe2242a4fa446149a3881c08a" -dependencies = [ - "zstd-safe", -] - -[[package]] -name = "zstd-safe" -version = "7.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f49c4d5f0abb602a93fb8736af2a4f4dd9512e36f7f570d66e65ff867ed3b9d" -dependencies = [ - "zstd-sys", -] - -[[package]] -name = "zstd-sys" -version = "2.0.15+zstd.1.5.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb81183ddd97d0c74cedf1d50d85c8d08c1b8b68ee863bdee9e706eedba1a237" -dependencies = [ - "cc", - "pkg-config", -] diff --git a/Cargo.toml b/Cargo.toml deleted file mode 100644 index 542c789..0000000 --- a/Cargo.toml +++ /dev/null @@ -1,51 +0,0 @@ -[package] -authors = [ - "Kartik Sharma ", - "Ajeet D'Souza <98ajeet@gmail.com>", -] -description = "Extract partitions from Android OTA files" -categories = ["command-line-utilities", "android"] -edition = "2024" -keywords = ["android", "cli", "terminal"] -license = "MIT" -name = "otadump" -repository = "https://github.com/crazystylus/otadump" -readme = "README.md" -rust-version = "1.85.0" -version = "0.1.3" - -[lib] -crate-type = ["cdylib", "rlib"] - -[features] -python = ["dep:pyo3"] - -[build-dependencies] -prost-build = "0.14.1" -protoc-bin-vendored = "3.2.0" - -[dependencies] -anyhow = "1.0.69" -bzip2 = { version = "0.6.0" } -clap = { version = "4.2.1", features = ["derive"] } -color-print = "0.3.4" -hex = "0.4.3" -indicatif = "0.17.3" -liblzma = { version = "0.4.2", features = ["static"] } -memmap2 = "0.9.5" -nom = "7.1.1" -nom-derive = "0.10.1" -prost = "0.14.1" -pyo3 = { version = "0.29.2", optional = true, features = ["abi3-py39", "extension-module"] } -rayon = "1.7.0" -ring = { version = "0.17.11", features = ["std"] } -sync-unsafe-cell = "0.1.0" -tempfile = "3.6.0" -zip = { version = "4.1.0", default-features = false } -zstd = "0.13.2" - -[profile.release] -codegen-units = 1 -debug = 0 -lto = true -strip = true diff --git a/README.md b/README.md index 5da5dd6..c2f263a 100644 --- a/README.md +++ b/README.md @@ -1,92 +1,22 @@ - - -
- # otadump -[![crates.io][crates.io-badge]][crates.io] - -**`otadump` helps you extract partitions from Android OTA files.**
-Partitions can be individually flashed to your device using `fastboot`. - -Compared to other tools, `otadump` is significantly faster and handles file -verification - no fear of a bad OTA file bricking your device. - -![Demo][demo] - -
- -## Features - -| | [crazystylus/otadump] | [ssut/payload-dumper-go] | [vm03/payload_dumper] | -| ---------------------------- | --------------------- | ------------------------ | ----------------------------------------- | -| Input file verification | ✔ | ✔ | | -| Output file verification | ✔ | | | -| Extract selective partitions | ✔ | ✔ | ✔ | -| Parallelized extraction | ✔ | ✔ | | -| Runs directly on .zip files | ✔ | ✔ | | -| Incremental OTA support | | | [Partial][payload_dumper-incremental-ota] | +`otadump` is a small Python interface to AOSP's `ota_extractor`. It supports +full and incremental Android OTA payloads without maintaining a separate delta +implementation. -## Benchmarks - -Comparing the time taken to extract all partitions from a few sample files -(lower is better): - -![Benchmarks][benchmarks] - -**Note:** `otadump` was run with args `--no-verify -c 12` and `payload-dumper-go` was run with args `-c 12` - -System specifications: - -- Processor: AMD Ryzen 5 5600X (12) @ 3.700GHz -- RAM: 16 GiB -- OS: Pop!_OS 22.04 / Linux 6.0.6 -- SSD: Samsung 970 EVO 250GB +The package supports Linux x86_64. On first use it downloads a pinned runtime +bundle containing unmodified files from the official Android 17 +`android17-release` otatools build 14524720. The bundle and every extracted +file are verified against `otadump/_artifact_lock.json` before execution. ## Installation -### macOS / Linux - -Install a pre-built binary: - ```sh -curl -sS https://raw.githubusercontent.com/crazystylus/otadump/mainline/install.sh | bash +python -m pip install . ``` -Otherwise, using Cargo: - -```sh -# Needs LZMA, Protobuf and pkg-config libraries installed. -# - On macOS: brew install protobuf xz pkg-config -# - On Debian / Ubuntu: apt install liblzma-dev protobuf-compiler pkg-config -cargo install --locked otadump -``` - -### Windows - -Download the pre-built binary from the [Releases] page. Extract it and run the -`otadump.exe` file. - ## Usage -Run the following command in your terminal: - -```sh -# Run directly on .zip file. -otadump ota.zip - -# Run on payload.bin file. -otadump payload.bin -``` - -### Python - -Build and install the native Python module with `pip` or -[maturin](https://www.maturin.rs/), then call `otadump.extract`: - ```python from pathlib import Path @@ -95,27 +25,28 @@ import otadump otadump.extract( Path("payload.bin"), Path("output"), + source_dir=Path("old-images"), # Required only for incremental OTAs. partitions=["boot", "system"], - overwrite=True, ) ``` -The optional keyword arguments are `num_threads`, `overwrite`, `partitions`, -and `verify`. Extraction releases the Python GIL. +`payload_file` may be a raw `payload.bin` or an OTA ZIP whose `payload.bin` is +stored without compression, as required by Android OTA packages. Set +`single_thread=True` to request AOSP's serial extraction mode. + +Extraction failures raise `otadump.OtaDumpError` with the native tool's error +output. Runtime files are cached under `$XDG_CACHE_HOME/otadump` (or +`~/.cache/otadump`). Set `OTADUMP_CACHE_DIR` to use another cache location. + +## Provenance -## Contributors +The runtime is derived from AOSP's official artifact: -- [Kartik Sharma][crazystylus] -- [Ajeet D'Souza][ajeetdsouza] +- Build: `android17-release` build `14524720` +- Source tag: `android-17.0.0_r1` +- `system/update_engine`: `4591637f51644f5429f9e2fe589ab22b92f61842` +- Upstream SHA-256: `4f8da78667e4bbc49fb993d4d6dca52d09e7cf6ed9fc9a62ebad2647d2dc454c` -[ajeetdsouza]: https://github.com/ajeetdsouza -[benchmarks]: contrib/benchmarks.svg -[crates.io-badge]: https://img.shields.io/crates/v/otadump?logo=rust&logoColor=white&style=flat-square -[crates.io]: https://crates.io/crates/otadump -[crazystylus]: https://github.com/crazystylus -[crazystylus/otadump]: https://github.com/crazystylus/otadump -[demo]: contrib/demo.gif -[payload_dumper-incremental-ota]: https://github.com/vm03/payload_dumper/issues/53 -[releases]: https://github.com/crazystylus/otadump/releases -[ssut/payload-dumper-go]: https://github.com/ssut/payload-dumper-go -[vm03/payload_dumper]: https://github.com/vm03/payload_dumper +The reproducible subset builder is `scripts/build-artifact.py`. It only copies +the extractor and its runtime shared-library closure; it does not compile or +modify AOSP code. diff --git a/build.rs b/build.rs deleted file mode 100644 index 38cec17..0000000 --- a/build.rs +++ /dev/null @@ -1,11 +0,0 @@ -fn main() { - let protoc = protoc_bin_vendored::protoc_bin_path().expect("unable to find vendored protoc"); - let mut config = prost_build::Config::new(); - config.protoc_executable(protoc); - config - .compile_protos( - &["src/protos/chromeos_update_engine/update_metadata.proto"], - &[] as &[&str], - ) - .expect("error compiling protobuf files"); -} diff --git a/install.sh b/install.sh deleted file mode 100755 index 9ad31f5..0000000 --- a/install.sh +++ /dev/null @@ -1,381 +0,0 @@ -#!/bin/sh -# shellcheck shell=dash - -# The official otadump installer. -# -# It runs on Unix shells like {a,ba,da,k,z}sh. It uses the common `local` -# extension. Note: Most shells limit `local` to 1 var per line, contra bash. - -main() { - if [ "$KSH_VERSION" = 'Version JM 93t+ 2010-03-05' ]; then - # The version of ksh93 that ships with many illumos systems does not - # support the "local" extension. Print a message rather than fail in - # subtle ways later on: - err 'the installer does not work with this ksh93 version; please try bash' - fi - - set -u - - # Detect and print host target triple. - ensure get_architecture - local _arch="$RETVAL" - assert_nz "$_arch" "arch" - echo "Detected architecture: $_arch" - - # Create and enter a temporary directory. - local _tmp_dir - _tmp_dir="$(mktemp -d)" || err "mktemp: could not create temporary directory" - cd "$_tmp_dir" || err "cd: failed to enter directory: $_tmp_dir" - - # Download and extract otadump. - ensure download_otadump "$_arch" - local _package="$RETVAL" - assert_nz "$_package" "package" - echo "Downloaded package: $_package" - case "$_package" in - *.tar.gz) - need_cmd tar - ensure tar -xf "$_package" - ;; - *.zip) - need_cmd unzip - ensure unzip -oq "$_package" - ;; - *) - err "unsupported package format: $_package" - ;; - esac - - # Install binary. - local _bin_dir="$HOME/.local/bin" - local _bin_name - case "$_arch" in - *windows*) _bin_name="otadump.exe" ;; - *) _bin_name="otadump" ;; - esac - ensure mkdir -p "$_bin_dir" - ensure cp "$_bin_name" "$_bin_dir" - echo "Installed otadump to $_bin_dir" - - # Print success message and check $PATH. - echo "" - echo "otadump is installed!" - if ! echo ":$PATH:" | grep -Fq ":$_bin_dir:"; then - echo "NOTE: $_bin_dir is not on your \$PATH. otadump will not work unless it is added to \$PATH." - fi -} - -download_otadump() { - local _arch="$1" - - if check_cmd curl; then - _dld=curl - elif check_cmd wget; then - _dld=wget - else - need_cmd 'curl or wget' - fi - need_cmd grep - - local _releases_url="https://api.github.com/repos/crazystylus/otadump/releases/latest" - local _releases - case "$_dld" in - curl) _releases="$(curl -sL "$_releases_url")" || - err "curl: failed to download $_releases_url" ;; - wget) _releases="$(wget -qO- "$_releases_url")" || - err "wget: failed to download $_releases_url" ;; - esac - (echo "$_releases" | grep -q 'API rate limit exceeded') && - err "you have exceeded GitHub's API rate limit. Please try again later, or use a different installation method: https://github.com/crazystylus/otadump/#installation" - - local _package_url - _package_url="$(echo "$_releases" | grep "browser_download_url" | cut -d '"' -f 4 | grep "$_arch")" || - err "otadump has not yet been packaged for your architecture ($_arch), please file an issue: https://github.com/crazystylus/otadump/issues" - - local _ext - case "$_package_url" in - *.tar.gz) _ext="tar.gz" ;; - *.zip) _ext="zip" ;; - *) err "unsupported package format: $_package_url" ;; - esac - - local _package="otadump.$_ext" - case "$_dld" in - curl) _releases="$(curl -sLo "$_package" "$_package_url")" || err "curl: failed to download $_package_url" ;; - wget) _releases="$(wget -qO "$_package" "$_package_url")" || err "wget: failed to download $_package_url" ;; - esac - - RETVAL="$_package" -} - -# The below functions have been extracted with minor modifications from the -# Rustup install script: -# -# https://github.com/rust-lang/rustup/blob/4c1289b2c3f3702783900934a38d7c5f912af787/rustup-init.sh - -get_architecture() { - local _ostype _cputype _bitness _arch _clibtype - _ostype="$(uname -s)" - _cputype="$(uname -m)" - _clibtype="gnu" - - if [ "$_ostype" = Linux ]; then - if [ "$(uname -o)" = Android ]; then - _ostype=Android - fi - fi - - if [ "$_ostype" = Darwin ] && [ "$_cputype" = i386 ]; then - # Darwin `uname -m` lies - if sysctl hw.optional.x86_64 | grep -q ': 1'; then - _cputype=x86_64 - fi - fi - - if [ "$_ostype" = SunOS ]; then - # Both Solaris and illumos presently announce as "SunOS" in "uname -s" - # so use "uname -o" to disambiguate. We use the full path to the - # system uname in case the user has coreutils uname first in PATH, - # which has historically sometimes printed the wrong value here. - if [ "$(/usr/bin/uname -o)" = illumos ]; then - _ostype=illumos - fi - - # illumos systems have multi-arch userlands, and "uname -m" reports the - # machine hardware name; e.g., "i86pc" on both 32- and 64-bit x86 - # systems. Check for the native (widest) instruction set on the - # running kernel: - if [ "$_cputype" = i86pc ]; then - _cputype="$(isainfo -n)" - fi - fi - - case "$_ostype" in - Android) - _ostype=linux-android - ;; - Linux) - check_proc - _ostype=unknown-linux-$_clibtype - _bitness=$(get_bitness) - ;; - FreeBSD) - _ostype=unknown-freebsd - ;; - NetBSD) - _ostype=unknown-netbsd - ;; - DragonFly) - _ostype=unknown-dragonfly - ;; - Darwin) - _ostype=apple-darwin - ;; - illumos) - _ostype=unknown-illumos - ;; - MINGW* | MSYS* | CYGWIN* | Windows_NT) - _ostype=pc-windows-msvc - ;; - *) - err "unrecognized OS type: $_ostype" - ;; - esac - - case "$_cputype" in - i386 | i486 | i686 | i786 | x86) - _cputype=i686 - ;; - xscale | arm) - _cputype=arm - if [ "$_ostype" = "linux-android" ]; then - _ostype=linux-androideabi - fi - ;; - armv6l) - _cputype=arm - if [ "$_ostype" = "linux-android" ]; then - _ostype=linux-androideabi - else - _ostype="${_ostype}eabihf" - fi - ;; - armv7l | armv8l) - _cputype=armv7 - if [ "$_ostype" = "linux-android" ]; then - _ostype=linux-androideabi - else - _ostype="${_ostype}eabihf" - fi - ;; - aarch64 | arm64) - _cputype=aarch64 - ;; - x86_64 | x86-64 | x64 | amd64) - _cputype=x86_64 - ;; - mips) - _cputype=$(get_endianness mips '' el) - ;; - mips64) - if [ "$_bitness" -eq 64 ]; then - # only n64 ABI is supported for now - _ostype="${_ostype}abi64" - _cputype=$(get_endianness mips64 '' el) - fi - ;; - ppc) - _cputype=powerpc - ;; - ppc64) - _cputype=powerpc64 - ;; - ppc64le) - _cputype=powerpc64le - ;; - s390x) - _cputype=s390x - ;; - riscv64) - _cputype=riscv64gc - ;; - *) - err "unknown CPU type: $_cputype" - ;; - esac - - # Detect 64-bit linux with 32-bit userland - if [ "${_ostype}" = unknown-linux-gnu ] && [ "${_bitness}" -eq 32 ]; then - case $_cputype in - x86_64) - # 32-bit executable for amd64 = x32 - if is_host_amd64_elf; then { - echo "x32 userland is unsupported" 1>&2 - exit 1 - }; else - _cputype=i686 - fi - ;; - mips64) - _cputype=$(get_endianness mips '' el) - ;; - powerpc64) - _cputype=powerpc - ;; - aarch64) - _cputype=armv7 - if [ "$_ostype" = "linux-android" ]; then - _ostype=linux-androideabi - else - _ostype="${_ostype}eabihf" - fi - ;; - riscv64gc) - err "riscv64 with 32-bit userland unsupported" - ;; - esac - fi - - # Detect armv7 but without the CPU features Rust needs in that build, - # and fall back to arm. - # See https://github.com/rust-lang/rustup.rs/issues/587. - if [ "$_ostype" = "unknown-linux-gnueabihf" ] && [ "$_cputype" = armv7 ]; then - if ensure grep '^Features' /proc/cpuinfo | grep -q -v neon; then - # At least one processor does not have NEON. - _cputype=arm - fi - fi - - _arch="${_cputype}-${_ostype}" - RETVAL="$_arch" -} - -get_bitness() { - need_cmd head - # Architecture detection without dependencies beyond coreutils. - # ELF files start out "\x7fELF", and the following byte is - # 0x01 for 32-bit and - # 0x02 for 64-bit. - # The printf builtin on some shells like dash only supports octal - # escape sequences, so we use those. - local _current_exe_head - _current_exe_head=$(head -c 5 /proc/self/exe) - if [ "$_current_exe_head" = "$(printf '\177ELF\001')" ]; then - echo 32 - elif [ "$_current_exe_head" = "$(printf '\177ELF\002')" ]; then - echo 64 - else - err "unknown platform bitness" - fi -} - -get_endianness() { - local cputype=$1 - local suffix_eb=$2 - local suffix_el=$3 - - # detect endianness without od/hexdump, like get_bitness() does. - need_cmd head - need_cmd tail - - local _current_exe_endianness - _current_exe_endianness="$(head -c 6 /proc/self/exe | tail -c 1)" - if [ "$_current_exe_endianness" = "$(printf '\001')" ]; then - echo "${cputype}${suffix_el}" - elif [ "$_current_exe_endianness" = "$(printf '\002')" ]; then - echo "${cputype}${suffix_eb}" - else - err "unknown platform endianness" - fi -} - -is_host_amd64_elf() { - need_cmd head - need_cmd tail - # ELF e_machine detection without dependencies beyond coreutils. - # Two-byte field at offset 0x12 indicates the CPU, - # but we're interested in it being 0x3E to indicate amd64, or not that. - local _current_exe_machine - _current_exe_machine=$(head -c 19 /proc/self/exe | tail -c 1) - [ "$_current_exe_machine" = "$(printf '\076')" ] -} - -check_proc() { - # Check for /proc by looking for the /proc/self/exe link. - # This is only run on Linux. - if ! test -L /proc/self/exe; then - err "unable to find /proc/self/exe. Is /proc mounted? Installation cannot proceed without /proc." - fi -} - -need_cmd() { - if ! check_cmd "$1"; then - err "need '$1' (command not found)" - fi -} - -check_cmd() { - command -v "$1" >/dev/null 2>&1 -} - -# Run a command that should never fail. If the command fails execution -# will immediately terminate with an error showing the failing -# command. -ensure() { - if ! "$@"; then err "command failed: $*"; fi -} - -assert_nz() { - if [ -z "$1" ]; then err "found empty string: $2"; fi -} - -err() { - echo "Error: $1" >&2 - exit 1 -} - -# This is put in braces to ensure that the script does not run until it is -# downloaded completely. -{ - main "$@" || exit 1 -} diff --git a/otadump/__init__.py b/otadump/__init__.py new file mode 100644 index 0000000..f2cc065 --- /dev/null +++ b/otadump/__init__.py @@ -0,0 +1,3 @@ +from ._extract import OtaDumpError, extract + +__all__ = ["OtaDumpError", "extract"] diff --git a/otadump/_artifact.py b/otadump/_artifact.py new file mode 100644 index 0000000..6faa6f3 --- /dev/null +++ b/otadump/_artifact.py @@ -0,0 +1,92 @@ +from __future__ import annotations + +import hashlib +import json +import os +import platform +import shutil +import tempfile +import urllib.request +import zipfile +from importlib.resources import files +from pathlib import Path + + +class ArtifactError(RuntimeError): + pass + + +def _lock() -> dict: + return json.loads(files("otadump").joinpath("_artifact_lock.json").read_text()) + + +def _sha256(path: Path) -> str: + digest = hashlib.sha256() + with path.open("rb") as stream: + for chunk in iter(lambda: stream.read(1024 * 1024), b""): + digest.update(chunk) + return digest.hexdigest() + + +def _cache_root() -> Path: + override = os.environ.get("OTADUMP_CACHE_DIR") + if override: + return Path(override).expanduser() + return Path(os.environ.get("XDG_CACHE_HOME", Path.home() / ".cache")) / "otadump" + + +def _runtime_valid(runtime: Path, expected: dict[str, str]) -> bool: + return all( + (runtime / name).is_file() and _sha256(runtime / name) == digest + for name, digest in expected.items() + ) + + +def executable() -> Path: + if platform.system() != "Linux" or platform.machine() not in {"x86_64", "AMD64"}: + raise ArtifactError("otadump supports Linux x86_64 only") + import fcntl + + lock = _lock() + runtime = _cache_root() / lock["bundle_sha256"] + binary = runtime / "bin" / "ota_extractor" + if _runtime_valid(runtime, lock["files"]): + return binary + + runtime.parent.mkdir(parents=True, exist_ok=True) + with (runtime.parent / ".install.lock").open("w") as install_lock: + fcntl.flock(install_lock, fcntl.LOCK_EX) + if _runtime_valid(runtime, lock["files"]): + return binary + if runtime.exists(): + shutil.rmtree(runtime) + + with tempfile.TemporaryDirectory(prefix="otadump-", dir=runtime.parent) as temp_name: + temp = Path(temp_name) + bundle = temp / "runtime.zip" + try: + urllib.request.urlretrieve(lock["bundle_url"], bundle) + except OSError as error: + raise ArtifactError(f"could not download AOSP ota_extractor: {error}") from error + if _sha256(bundle) != lock["bundle_sha256"]: + raise ArtifactError("AOSP ota_extractor bundle checksum mismatch") + + unpacked = temp / "runtime" + unpacked.mkdir() + with zipfile.ZipFile(bundle) as archive: + if set(archive.namelist()) != set(lock["files"]): + raise ArtifactError("AOSP ota_extractor bundle contents do not match lock") + for name, expected_hash in lock["files"].items(): + target = unpacked / name + target.parent.mkdir(parents=True, exist_ok=True) + with archive.open(name) as source, target.open("wb") as destination: + shutil.copyfileobj(source, destination) + if _sha256(target) != expected_hash: + raise ArtifactError(f"AOSP runtime checksum mismatch: {name}") + + (unpacked / "bin" / "ota_extractor").chmod(0o755) + unpacked.rename(runtime) + + if not _runtime_valid(runtime, lock["files"]): + raise ArtifactError("AOSP ota_extractor cache installation failed") + return binary diff --git a/otadump/_artifact_lock.json b/otadump/_artifact_lock.json new file mode 100644 index 0000000..aa7bf96 --- /dev/null +++ b/otadump/_artifact_lock.json @@ -0,0 +1,30 @@ +{ + "aosp_build": "android17-release/14524720", + "aosp_source_tag": "android-17.0.0_r1", + "aosp_update_engine_commit": "4591637f51644f5429f9e2fe589ab22b92f61842", + "upstream_url": "https://ci.android.com/builds/submitted/14524720/android17-release/latest/otatools.zip", + "upstream_sha256": "4f8da78667e4bbc49fb993d4d6dca52d09e7cf6ed9fc9a62ebad2647d2dc454c", + "bundle_url": "https://github.com/AndroidDumps/otadump/releases/download/aosp-ota-extractor-android-17.0.0_r1/aosp-ota-extractor-14524720.zip", + "bundle_sha256": "96c5df0c64be9ce51bb4c8992f06ba279187d83dc805e99e15e916bd733e2c5a", + "files": { + "bin/ota_extractor": "8ce3fcdbe39f8d2e3f7a56a264725b28c91137f723c176d68adf1172e8916b1e", + "lib64/libbase.so": "873c30ca9cc59f925a8a414d3937c9cea174ec46dbe6a950c72ecca4f267d737", + "lib64/libbrillo-stream.so": "66df60dd0ca5967f642f0fe9fbc396702d93223dd2ffa49103b43da2eaccb287", + "lib64/libbrillo.so": "1869814ab6efbf8e103b2bf91390f89525fbe8a04d26793ca03fadb9e21e8d0f", + "lib64/libc++.so": "aab89aa18f9bec8e01c632b4230f4544fb6128b5d3df7ce6971c52b8c77bb11c", + "lib64/libchrome.so": "7cf69ccfe4337b7f1e9ed2a3e4b427d2a42def726c36ac0b7a186538b9ccb813", + "lib64/libcrypto-host.so": "aa40f48fda2ddf87945681fe0e0b3490a9e5b720e2fa89f81b5096debae08fbc", + "lib64/libcrypto_utils.so": "7f04059441825997dcc54ac971755a8e82863f549c667ddf0177055ad0cfcee5", + "lib64/libcutils.so": "18059b6c87bdb8f8b93c775f31fd64eee251157b9a38edca88d60188af5515db", + "lib64/libevent-host.so": "b3d393bd147eac29f1fa3c8c640dad4c2514d075227b0c251c4fa86621ec89d0", + "lib64/libext4_utils.so": "e72e5d6590252091a11cbb4cb9cf5b9f66a4d2a158873272a794c307714fe118", + "lib64/libfec.so": "f55b6ac2bf6f40e2435f94e51e39cbb792bd36f1441fd525f26162fecae0eaba", + "lib64/liblog.so": "5bdd1193cc34fbe37fdd6f640b7cb16359ae92c1b4993f9e55cd390d5d15bebb", + "lib64/liblz4.so": "91f254e2549cfc0305730078d6bf34cb695bfbffabe910c2e2c10216234e60e5", + "lib64/libprotobuf-cpp-lite.so": "85519577353639fd79a63e822ea419bcfaffffcbf670f2fb313f2c4d8acf1ba1", + "lib64/libsquashfs_utils.so": "0dff5db06e04aeb2ef47e2c83b50aa78aee8530f0614cbf6e042bb989f168702", + "lib64/libssl-host.so": "9a8ef2fec6d330c081aff6951bcf2483fa8e29d308f50faa857a314a12443f54", + "lib64/libz-host.so": "db58a2e3aef638cf68b7ed3dde5ad70ceb1566ddbd84c6157d871ed695983ec3", + "lib64/libziparchive.so": "a2b7e6490a5e7d0eb478975c2e5fefa5d77ba2a67585e9ded26c48feec245b7b" + } +} diff --git a/otadump/_extract.py b/otadump/_extract.py new file mode 100644 index 0000000..149af69 --- /dev/null +++ b/otadump/_extract.py @@ -0,0 +1,85 @@ +from __future__ import annotations + +import struct +import subprocess +import zipfile +from pathlib import Path +from typing import Iterable, Union + +from . import _artifact + +PathLike = Union[str, "Path"] + + +class OtaDumpError(RuntimeError): + """Raised when the AOSP extractor cannot reconstruct the requested images.""" + + +def _zip_payload_offset(path: Path) -> int: + try: + with zipfile.ZipFile(path) as archive: + info = archive.getinfo("payload.bin") + if info.compress_type != zipfile.ZIP_STORED: + raise OtaDumpError("OTA payload.bin must be stored without compression") + with path.open("rb") as stream: + stream.seek(info.header_offset) + header = stream.read(30) + except (OSError, zipfile.BadZipFile, KeyError) as error: + raise OtaDumpError(f"could not read OTA ZIP: {error}") from error + + if len(header) != 30 or header[:4] != b"PK\x03\x04": + raise OtaDumpError("invalid payload.bin local ZIP header") + fields = struct.unpack("<4s5H3L2H", header) + return info.header_offset + 30 + fields[-2] + fields[-1] + + +def extract( + payload_file: PathLike, + output_dir: PathLike, + *, + source_dir: PathLike | None = None, + partitions: Iterable[str] | None = None, + single_thread: bool = False, +) -> None: + """Extract images from a full or incremental Android OTA payload.""" + payload = Path(payload_file).expanduser().resolve() + output = Path(output_dir).expanduser().resolve() + if not payload.is_file(): + raise OtaDumpError(f"payload does not exist: {payload}") + output.mkdir(parents=True, exist_ok=True) + + try: + executable = _artifact.executable() + except _artifact.ArtifactError as error: + raise OtaDumpError(str(error)) from error + + command = [ + str(executable), + f"--payload={payload}", + f"--output_dir={output}", + ] + if zipfile.is_zipfile(payload): + command.append(f"--payload_offset={_zip_payload_offset(payload)}") + if source_dir is not None: + source = Path(source_dir).expanduser().resolve() + if not source.is_dir(): + raise OtaDumpError(f"source directory does not exist: {source}") + command.append(f"--input_dir={source}") + if partitions is not None: + if isinstance(partitions, (str, bytes)): + raise TypeError("partitions must be an iterable of partition names") + selected = list(partitions) + if not selected or any(not name or "," in name for name in selected): + raise ValueError("partitions must contain non-empty names without commas") + command.append(f"--partitions={','.join(selected)}") + if single_thread: + command.append("--single_thread") + try: + result = subprocess.run(command, text=True, capture_output=True, check=False) + except OSError as error: + raise OtaDumpError(f"could not start AOSP ota_extractor: {error}") from error + if result.returncode: + detail = result.stderr.strip() or result.stdout.strip() or "no diagnostic output" + raise OtaDumpError( + f"AOSP ota_extractor failed with status {result.returncode}: {detail}" + ) diff --git a/pyproject.toml b/pyproject.toml index 717b81c..6ee4ea6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,22 +1,28 @@ [build-system] -requires = ["maturin>=1.14,<2"] -build-backend = "maturin" +requires = ["hatchling"] +build-backend = "hatchling.build" [project] name = "otadump" -dynamic = ["version"] +version = "0.2.0" description = "Extract partitions from Android OTA files" readme = "README.md" requires-python = ">=3.9" -license = "MIT" +license = { file = "LICENSE" } authors = [ { name = "Kartik Sharma", email = "kartik.sharma522@gmail.com" }, { name = "Ajeet D'Souza", email = "98ajeet@gmail.com" }, ] classifiers = [ "Programming Language :: Python :: 3", - "Programming Language :: Rust", + "Operating System :: POSIX :: Linux", ] -[tool.maturin] -features = ["python"] +[project.urls] +Repository = "https://github.com/AndroidDumps/otadump" + +[tool.hatch.build.targets.wheel] +packages = ["otadump"] + +[tool.hatch.build.targets.sdist] +include = ["/otadump", "/scripts", "/tests", "/AGENTS.md", "/LICENSE", "/README.md", "/pyproject.toml"] diff --git a/rustfmt.toml b/rustfmt.toml deleted file mode 100644 index 72efef7..0000000 --- a/rustfmt.toml +++ /dev/null @@ -1,8 +0,0 @@ -group_imports = "StdExternalCrate" -imports_granularity = "Module" -newline_style = "Native" -use_field_init_shorthand = true -use_small_heuristics = "Max" -use_try_shorthand = true -wrap_comments = true -style_edition = "2024" diff --git a/scripts/build-artifact.py b/scripts/build-artifact.py new file mode 100644 index 0000000..865bf9d --- /dev/null +++ b/scripts/build-artifact.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python3 +"""Build the locked runtime subset from the official AOSP otatools archive.""" + +import hashlib +import json +import sys +import urllib.request +import zipfile +from pathlib import Path + +UPSTREAM_URL = "https://ci.android.com/builds/submitted/14524720/android17-release/latest/otatools.zip" +UPSTREAM_SHA256 = "4f8da78667e4bbc49fb993d4d6dca52d09e7cf6ed9fc9a62ebad2647d2dc454c" +FILES = ( + "bin/ota_extractor", + "lib64/libbase.so", + "lib64/libbrillo.so", + "lib64/libbrillo-stream.so", + "lib64/libchrome.so", + "lib64/libcrypto-host.so", + "lib64/libcrypto_utils.so", + "lib64/libc++.so", + "lib64/libcutils.so", + "lib64/libevent-host.so", + "lib64/libext4_utils.so", + "lib64/libfec.so", + "lib64/liblog.so", + "lib64/liblz4.so", + "lib64/libprotobuf-cpp-lite.so", + "lib64/libsquashfs_utils.so", + "lib64/libssl-host.so", + "lib64/libz-host.so", + "lib64/libziparchive.so", +) + + +def digest(data: bytes) -> str: + return hashlib.sha256(data).hexdigest() + + +def file_digest(path: Path) -> str: + value = hashlib.sha256() + with path.open("rb") as stream: + for chunk in iter(lambda: stream.read(1024 * 1024), b""): + value.update(chunk) + return value.hexdigest() + + +def main() -> None: + output = Path(sys.argv[1] if len(sys.argv) > 1 else "aosp-ota-extractor-14524720.zip") + source = Path(sys.argv[2] if len(sys.argv) > 2 else "otatools-14524720.zip") + if not source.exists(): + urllib.request.urlretrieve(UPSTREAM_URL, source) + if file_digest(source) != UPSTREAM_SHA256: + raise SystemExit("upstream otatools checksum mismatch") + + hashes = {} + with zipfile.ZipFile(source) as upstream, zipfile.ZipFile( + output, "w", compression=zipfile.ZIP_STORED + ) as bundle: + for name in FILES: + data = upstream.read(name) + hashes[name] = digest(data) + info = zipfile.ZipInfo(name, (1980, 1, 1, 0, 0, 0)) + info.create_system = 3 + info.external_attr = (0o755 if name == "bin/ota_extractor" else 0o644) << 16 + bundle.writestr(info, data) + + print(json.dumps(hashes, indent=2, sort_keys=True)) + print(f"bundle_sha256={file_digest(output)}") + + +if __name__ == "__main__": + main() diff --git a/src/lib.rs b/src/lib.rs deleted file mode 100644 index 5da31f5..0000000 --- a/src/lib.rs +++ /dev/null @@ -1,502 +0,0 @@ -#[allow(clippy::all)] -#[allow(dead_code)] -mod chromeos_update_engine { - include!(concat!(env!("OUT_DIR"), "/chromeos_update_engine.rs")); -} -mod payload; -#[cfg(feature = "python")] -mod python; - -use std::cmp::Reverse; -use std::collections::HashSet; -use std::fs::{self, File, OpenOptions}; -use std::io::Read; -use std::num::NonZero; -use std::ops::Mul; -use std::path::Path; -use std::sync::OnceLock; -use std::sync::atomic::{AtomicUsize, Ordering}; -use std::{error, result, slice, thread}; - -use anyhow::{Context as _, Error, Result, bail, ensure}; -use bzip2::read::BzDecoder; -use chromeos_update_engine::install_operation::Type; -use chromeos_update_engine::{DeltaArchiveManifest, InstallOperation, PartitionUpdate}; -use liblzma::read::XzDecoder; -use memmap2::{Mmap, MmapMut, MmapOptions}; -use prost::Message as _; -use rayon::{BroadcastContext, ThreadPoolBuilder}; -use ring::digest; -use sync_unsafe_cell::SyncUnsafeCell; -use zip::ZipArchive; -use zip::result::ZipError; -use zstd::Decoder as ZstdDecoder; - -pub use crate::payload::Payload; - -// The chunk size is the number of bytes we verify before we count one "tick" in -// the progress tracker. -const VERIFY_CHUNK_SIZE: usize = 2 * 1024 * 1024; // 2 MiB - -pub struct ExtractOptions<'a> { - num_threads: Option, - overwrite: bool, - partitions: Option>, - progress_reporter: &'a dyn ProgressReporter, - verify: bool, -} - -impl<'a> ExtractOptions<'a> { - /// Creates a blank new set of options ready for configuration. - pub fn new() -> Self { - Self { - num_threads: None, - overwrite: false, - partitions: None, - progress_reporter: &NoOpProgressReporter, - verify: true, - } - } - - /// Extracts the payload file to the output directory. - pub fn extract( - &self, - payload_file: P, - output_dir: Q, - ) -> result::Result<(), Box> - where - P: AsRef, - Q: AsRef, - { - let payload_file = payload_file.as_ref(); - let output_dir = output_dir.as_ref(); - self.extract_impl(payload_file, output_dir)?; - Ok(()) - } - - fn extract_impl(&self, payload_file: &Path, output_dir: &Path) -> Result<()> { - self.progress_reporter.report_progress(0.); - - let payload_file = Self::open_payload_file(payload_file)?; - let payload = Payload::parse(&payload_file)?; - - let mut manifest = - DeltaArchiveManifest::decode(payload.manifest).context("Unable to parse manifest")?; - - // Skip partitions that are not in the list of partitions to be extracted. - if let Some(partitions) = &self.partitions { - manifest.partitions.retain(|update| partitions.contains(&update.partition_name)); - } - - // Verification is slow for large partitions, and cannot be parallelized. - // Extracting the largest partition first allows us to start verifying - // it as early as possible. - manifest.partitions.sort_unstable_by_key(|partition| { - Reverse(partition.new_partition_info.as_ref().and_then(|info| info.size).unwrap_or(0)) - }); - - let extract_ops = - manifest.partitions.iter().map(|update| update.operations.len()).sum::(); - let verify_ops = if self.verify { - manifest - .partitions - .iter() - .map(|update| { - let partition_size = - update.new_partition_info.as_ref().and_then(|info| info.size).unwrap_or(0) - as usize; - partition_size.div_ceil(VERIFY_CHUNK_SIZE) - }) - .sum() - } else { - 0 - }; - let total_ops = extract_ops + verify_ops; - let total_ops_completed = AtomicUsize::new(0); - - let block_size = manifest.block_size.context("block_size not defined")? as usize; - - // Ensure that all partitions to be extracted are present in the manifest. - for partition_name in self.partitions.iter().flatten() { - ensure!( - manifest.partitions.iter().any(|update| &update.partition_name == partition_name), - "Partition not found: {partition_name}", - ); - } - - fs::create_dir_all(output_dir) - .with_context(|| format!("Could not create output directory: {output_dir:?}"))?; - - let num_threads = self - .num_threads - .unwrap_or_else(|| thread::available_parallelism().map(NonZero::get).unwrap_or(1)) - .max(1); - let threadpool = ThreadPoolBuilder::new() - .num_threads(num_threads) - .build() - .context("Unable to start threadpool")?; - let mut error = OnceLock::new(); - - threadpool.in_place_scope_fifo(|scope| -> Result<()> { - for update in &manifest.partitions { - // Exit early if an error has occurred. - if error.get().is_some() { - break; - } - - // Create and broadcast a task to the threadpool. - let partition = self.open_partition_file(update, output_dir)?; - let task = Task { - payload: &payload, - block_size, - verify: self.verify, - update, - op_idx: AtomicUsize::new(0), - partition: SyncUnsafeCell::new(partition), - total_ops, - total_ops_completed: &total_ops_completed, - progress_reporter: self.progress_reporter, - error: &error, - }; - scope.spawn_broadcast(move |_, ctx| { - if let Err(e) = task.run(ctx) { - _ = task.error.set(e); - } - }); - } - Ok(()) - })?; - - if let Some(e) = error.take() { - return Err(e); - } - - self.progress_reporter.report_progress(1.); - Ok(()) - } - - fn open_payload_file(path: &Path) -> Result { - let in_file = File::open(path) - .with_context(|| format!("Failed to open file for reading: {path:?}"))?; - - // Assume the file is a zip archive. If it's not, we get an InvalidArchive - // error, and we can treat it as a payload.bin file. - match ZipArchive::new(&in_file) { - Ok(mut archive) => { - let mut zip_file = archive - .by_name("payload.bin") - .context("Could not find payload.bin file in archive")?; - - match zip_file.compression() { - // Most OTA zip files use the STORED (no compression) method for the payload.bin - // file, which means we can use it directly. - zip::CompressionMethod::Stored => unsafe { - MmapOptions::new() - .offset(zip_file.data_start()) - .len(zip_file.compressed_size() as usize) - .map(&in_file) - .with_context(|| format!("Failed to mmap file: {path:?}")) - }, - _ => { - let out_file = - tempfile::tempfile().context("Failed to create temporary file")?; - out_file - .set_len(zip_file.size()) - .context("Failed to set size of temporary file")?; - let mut out_file = unsafe { MmapMut::map_mut(&out_file) } - .context("Failed to mmap temporary file")?; - - zip_file - .read_exact(&mut out_file) - .context("Failed to write to temporary file")?; - out_file.make_read_only().context("Failed to make temporary file read-only") - } - } - } - Err(ZipError::InvalidArchive(_)) => unsafe { Mmap::map(&in_file) } - .with_context(|| format!("Failed to mmap file: {path:?}")), - Err(e) => Err(e).with_context(|| format!("Failed to open payload file: {path:?}")), - } - } - - fn open_partition_file( - &self, - update: &PartitionUpdate, - partition_dir: impl AsRef, - ) -> Result { - let partition_len = update - .new_partition_info - .as_ref() - .and_then(|info| info.size) - .context("Unable to determine output file size")?; - - let filename = Path::new(&update.partition_name).with_extension("img"); - let path = partition_dir.as_ref().join(filename); - - let file = OpenOptions::new() - .create(true) - .create_new(!self.overwrite) - .read(true) - .write(true) - .truncate(true) - .open(&path) - .with_context(|| format!("Unable to open file for writing: {path:?}"))?; - file.set_len(partition_len)?; - - let file = unsafe { MmapMut::map_mut(&file) } - .with_context(|| format!("Failed to mmap file: {path:?}"))?; - Ok(file) - } - - /// Number of threads to use for extraction. By default, this is set to the - /// number of logical CPUs on the system. - pub fn num_threads(&mut self, num_threads: usize) -> &mut Self { - self.num_threads = Some(num_threads); - self - } - - /// Whether to overwrite existing files when extracting. By default, - /// existing files are not overwritten. - pub fn overwrite(&mut self, overwrite: bool) -> &mut Self { - self.overwrite = overwrite; - self - } - - /// Extract only the specified partitions from the payload. By default, all - /// partitions are extracted. - pub fn partitions(&mut self, partitions: I) -> &mut Self - where - I: IntoIterator, - S: AsRef, - { - self.partitions = - Some(partitions.into_iter().map(|partition| partition.as_ref().to_string()).collect()); - self - } - - /// Set a progress reporter to report extraction progress. - pub fn progress_reporter(&mut self, progress_reporter: &'a dyn ProgressReporter) -> &mut Self { - self.progress_reporter = progress_reporter; - self - } - - /// Verify the input and output partitions. This is enabled by default. - pub fn verify(&mut self, verify: bool) -> &mut Self { - self.verify = verify; - self - } -} - -impl Default for ExtractOptions<'_> { - fn default() -> Self { - Self::new() - } -} - -struct Task<'a> { - payload: &'a Payload<'a>, - block_size: usize, - verify: bool, - - update: &'a PartitionUpdate, - op_idx: AtomicUsize, - partition: SyncUnsafeCell, - - total_ops: usize, - total_ops_completed: &'a AtomicUsize, - progress_reporter: &'a dyn ProgressReporter, - - error: &'a OnceLock, -} - -impl Task<'_> { - fn run(&self, ctx: BroadcastContext<'_>) -> Result<()> { - // If an error has already occurred, stop processing the partition. - while self.error.get().is_none() { - let op_idx = self.op_idx.fetch_add(1, Ordering::AcqRel); - match self.update.operations.get(op_idx) { - Some(op) => { - self.run_op(op)?; - self.increment_progress(); - } - None => { - // If this is the last thread to fetch an operation for this - // partition, the partition is fully extracted and can now be - // verified. - if op_idx + 1 == self.update.operations.len() + ctx.num_threads() { - self.verify_partition()?; - unsafe { (*self.partition.get()).flush() } - .context("Error while flushing file to disk")?; - }; - break; - } - } - } - Ok(()) - } - - fn run_op(&self, op: &InstallOperation) -> Result<()> { - let mut dst_extents = - self.extract_dst_extents(op).context("Error extracting dst_extents")?; - match Type::try_from(op.r#type).context("Invalid operation")? { - Type::Replace => { - let data = self.extract_data(op).context("Error extracting data")?; - self.run_op_replace(&mut &*data, &mut dst_extents) - .context("Error in REPLACE operation") - } - Type::ReplaceBz => { - let data = self.extract_data(op).context("Error extracting data")?; - let mut decoder = BzDecoder::new(data); - self.run_op_replace(&mut decoder, &mut dst_extents) - .context("Error in REPLACE_BZ operation") - } - Type::ReplaceXz => { - let data = self.extract_data(op).context("Error extracting data")?; - let mut decoder = XzDecoder::new(data); - self.run_op_replace(&mut decoder, &mut dst_extents) - .context("Error in REPLACE_XZ operation") - } - Type::ReplaceZstd => { - let data = self.extract_data(op).context("Error extracting data")?; - let mut decoder = - ZstdDecoder::with_buffer(data).context("Unable to initialize zstd decoder")?; - self.run_op_replace(&mut decoder, &mut dst_extents) - .context("Error in REPLACE_ZSTD operation") - } - Type::Zero => Ok(()), // This is a no-op since the partition is already zeroed - op => bail!("Unimplemented operation: {op:?}"), - } - } - - fn run_op_replace(&self, reader: &mut impl Read, dst_extents: &mut [&mut [u8]]) -> Result<()> { - let dst_len = dst_extents.iter().map(|extent| extent.len()).sum::(); - let mut bytes_written = 0usize; - - for extent in dst_extents { - let mut extent = &mut **extent; - loop { - match reader.read(extent).context("Failed to write to buffer")? { - 0 => break, - n => { - bytes_written += n; - extent = &mut extent[n..]; - } - } - } - } - ensure!(reader.read(&mut [0])? == 0, "Read fewer bytes than expected"); - - // Align number of bytes written to block size. The formula for alignment is: - // ((operand + alignment - 1) / alignment) * alignment. - let bytes_written_aligned = bytes_written.div_ceil(self.block_size).mul(self.block_size); - ensure!(bytes_written_aligned == dst_len, "More dst blocks than data, even with padding"); - - Ok(()) - } - - fn extract_dst_extents(&self, op: &InstallOperation) -> Result> { - let partition = unsafe { (*self.partition.get()).as_mut_ptr() }; - let partition_len = unsafe { (&(*self.partition.get())).len() }; - - op.dst_extents - .iter() - .map(|extent| { - let start_block = - extent.start_block.context("start_block not defined in extent")? as usize; - let num_blocks = - extent.num_blocks.context("num_blocks not defined in extent")? as usize; - - let partition_offset = start_block * self.block_size; - let extent_len = num_blocks * self.block_size; - - ensure!( - partition_offset + extent_len <= partition_len, - "Extent exceeds partition size" - ); - let extent = unsafe { - slice::from_raw_parts_mut(partition.add(partition_offset), extent_len) - }; - - Ok(extent) - }) - .collect() - } - - fn extract_data<'a>(&'a self, op: &InstallOperation) -> Result<&'a [u8]> { - let data_len = op.data_length.context("data_length not defined")? as usize; - let data = { - let offset = op.data_offset.context("data_offset not defined")? as usize; - self.payload - .data - .get(offset..offset + data_len) - .context("Data offset exceeds payload size")? - }; - self.verify_op(op, data)?; - Ok(data) - } - - fn verify_op(&self, op: &InstallOperation, data: &[u8]) -> Result<()> { - if !self.verify { - return Ok(()); - } - let Some(exp_hash) = &op.data_sha256_hash else { - return Ok(()); - }; - - let got_hash = digest::digest(&digest::SHA256, data); - let got_hash = got_hash.as_ref(); - ensure!( - got_hash == exp_hash, - "Input verification failed: hash mismatch: expected {}, got {got_hash:?}", - hex::encode(exp_hash) - ); - Ok(()) - } - - fn verify_partition(&self) -> Result<()> { - if !self.verify { - return Ok(()); - } - - let Some(exp_hash) = - self.update.new_partition_info.as_ref().and_then(|info| info.hash.as_ref()) - else { - return Ok(()); - }; - - let mut digest = digest::Context::new(&digest::SHA256); - for chunk in unsafe { (*self.partition.get()).chunks(VERIFY_CHUNK_SIZE) } { - digest.update(chunk); - self.increment_progress(); - } - - let got_hash = digest.finish(); - let got_hash = got_hash.as_ref(); - ensure!( - got_hash == exp_hash, - "Output verification failed: hash mismatch: expected {}, got {got_hash:?}", - hex::encode(exp_hash) - ); - Ok(()) - } - - fn increment_progress(&self) { - let total_ops_completed = self.total_ops_completed.fetch_add(1, Ordering::Relaxed) + 1; - if total_ops_completed % 16 == 0 { - let progress = total_ops_completed as f64 / self.total_ops as f64; - self.progress_reporter.report_progress(progress); - } - } -} - -pub trait ProgressReporter: Sync { - /// Reports the progress of the extraction process. The progress is provided - /// as a value between 0 and 1. - fn report_progress(&self, progress: f64); -} - -pub struct NoOpProgressReporter; - -impl ProgressReporter for NoOpProgressReporter { - fn report_progress(&self, _progress: f64) {} -} diff --git a/src/main.rs b/src/main.rs deleted file mode 100644 index 5c2e977..0000000 --- a/src/main.rs +++ /dev/null @@ -1,93 +0,0 @@ -use anyhow::Result; -use clap::Parser; -use indicatif::{ProgressBar, ProgressStyle}; -use otadump::{ExtractOptions, ProgressReporter}; - -fn main() -> Result<()> { - extract(); - Ok(()) -} - -const HELP_TEMPLATE: &str = color_print::cstr!( - "\ -{before-help}{name} {version} -{author} -https://github.com/crazystylus/otadump - -{about} - -{usage-heading} -{tab}{usage} - -{all-args}{after-help}" -); - -#[derive(Debug, Parser)] -#[clap( - about, - author, - help_template = HELP_TEMPLATE, - disable_help_subcommand = true, - propagate_version = true, - version, -)] -pub struct Args { - /// Path to the payload file - #[clap(required = true)] - pub payload_file: String, - - /// Path to the output directory - #[clap(long)] - pub output_dir: String, -} - -pub fn extract() { - let args = Args::parse(); - - let reporter = Box::new(CliProgressReporter::new()); - let reporter = reporter.as_ref(); - - let result = ExtractOptions::new() - .progress_reporter(reporter) - .extract(&args.payload_file, &args.output_dir); - reporter.progress_bar.finish_and_clear(); - match result { - Ok(()) => { - let message = format!("Extraction complete: {}", args.output_dir); - reporter.progress_bar.println(message); - } - Err(e) => { - let message = format!("Error: {e:?}"); - reporter.progress_bar.println(message); - } - } -} - -#[derive(Debug)] -struct CliProgressReporter { - progress_bar: ProgressBar, -} - -impl CliProgressReporter { - const PROGRESS_TICKS: usize = 10_000; - - fn new() -> Self { - let style = ProgressStyle::with_template( - "{prefix:>16!.cyan.bold} [{wide_bar:.white.dim}] {percent:>3.white}%", - ) - .expect("unable to build progress bar template") - .progress_chars("=> "); - let progress_bar = ProgressBar::new(Self::PROGRESS_TICKS as u64) - .with_prefix("Extracting") - .with_style(style); - progress_bar.println("Extracting files..."); - Self { progress_bar } - } -} - -impl ProgressReporter for CliProgressReporter { - fn report_progress(&self, progress: f64) { - let position = progress * Self::PROGRESS_TICKS as f64; - self.progress_bar.set_position(position as u64); - } -} diff --git a/src/payload.rs b/src/payload.rs deleted file mode 100644 index c8d2a57..0000000 --- a/src/payload.rs +++ /dev/null @@ -1,49 +0,0 @@ -use anyhow::{Result, bail}; -use nom::Finish; -use nom_derive::{NomBE, Parse}; - -/// Update file format: contains all the operations needed to update a system to -/// a specific version. It can be a full payload which can update from any -/// version, or a delta payload which can only update from a specific version. -#[allow(dead_code)] -#[derive(Debug, NomBE)] -pub struct Payload<'a> { - /// Should be "CrAU". - #[nom(Tag = r#"b"CrAU""#)] - pub magic_bytes: &'a [u8], - - /// Payload major version. - pub file_format_version: u64, - - /// Size of [`DeltaArchiveManifest`]. - pub manifest_size: u64, - - /// Only present if format_version >= 2. - #[nom(If = "file_format_version > 1")] - pub metadata_signature_size: Option, - - /// This is a serialized [`DeltaArchiveManifest`] message. - #[nom(Take = "manifest_size")] - pub manifest: &'a [u8], - - /// The signature of the metadata (from the beginning of the payload up to - /// this location, not including the signature itself). This is a serialized - /// [`Signatures`] message. - #[nom(If = "metadata_signature_size.is_some()", Take = "metadata_signature_size.unwrap()")] - pub metadata_signature: Option<&'a [u8]>, - - /// Data blobs for files, no specific format. The specific offset and length - /// of each data blob is recorded in the [`DeltaArchiveManifest`]. - #[nom(Parse = "::nom::combinator::rest")] - pub data: &'a [u8], -} - -impl<'a> Payload<'a> { - pub fn parse(bytes: &'a [u8]) -> Result { - match Parse::parse(bytes).finish() { - Ok((_, payload)) => Ok(payload), - Err(e) if e.code == nom::error::ErrorKind::Tag => bail!("Invalid payload file"), - Err(_) => bail!("Unable to parse payload file"), - } - } -} diff --git a/src/protos/chromeos_update_engine/update_metadata.proto b/src/protos/chromeos_update_engine/update_metadata.proto deleted file mode 100644 index add2c17..0000000 --- a/src/protos/chromeos_update_engine/update_metadata.proto +++ /dev/null @@ -1,374 +0,0 @@ -// Source: https://android.googlesource.com/platform/system/update_engine/+/HEAD/update_metadata.proto -// -// Copyright (C) 2010 The Android Open Source Project -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// Update file format: An update file contains all the operations needed -// to update a system to a specific version. It can be a full payload which -// can update from any version, or a delta payload which can only update -// from a specific version. -// The update format is represented by this struct pseudocode: -// struct delta_update_file { -// char magic[4] = "CrAU"; -// uint64 file_format_version; // payload major version -// uint64 manifest_size; // Size of protobuf DeltaArchiveManifest -// -// // Only present if format_version >= 2: -// uint32 metadata_signature_size; -// -// // The DeltaArchiveManifest protobuf serialized, not compressed. -// char manifest[manifest_size]; -// -// // The signature of the metadata (from the beginning of the payload up to -// // this location, not including the signature itself). This is a serialized -// // Signatures message. -// char metadata_signature_message[metadata_signature_size]; -// -// // Data blobs for files, no specific format. The specific offset -// // and length of each data blob is recorded in the DeltaArchiveManifest. -// struct { -// char data[]; -// } blobs[]; -// -// // The signature of the entire payload, everything up to this location, -// // except that metadata_signature_message is skipped to simplify signing -// // process. These two are not signed: -// uint64 payload_signatures_message_size; -// // This is a serialized Signatures message. -// char payload_signatures_message[payload_signatures_message_size]; -// -// }; -// The DeltaArchiveManifest protobuf is an ordered list of InstallOperation -// objects. These objects are stored in a linear array in the -// DeltaArchiveManifest. Each operation is applied in order by the client. -// The DeltaArchiveManifest also contains the initial and final -// checksums for the device. -// The client will perform each InstallOperation in order, beginning even -// before the entire delta file is downloaded (but after at least the -// protobuf is downloaded). The types of operations are explained: -// - REPLACE: Replace the dst_extents on the drive with the attached data, -// zero padding out to block size. -// - REPLACE_BZ: bzip2-uncompress the attached data and write it into -// dst_extents on the drive, zero padding to block size. -// - MOVE: Copy the data in src_extents to dst_extents. Extents may overlap, -// so it may be desirable to read all src_extents data into memory before -// writing it out. (deprecated) -// - SOURCE_COPY: Copy the data in src_extents in the old partition to -// dst_extents in the new partition. There's no overlapping of data because -// the extents are in different partitions. -// - BSDIFF: Read src_length bytes from src_extents into memory, perform -// bspatch with attached data, write new data to dst_extents, zero padding -// to block size. (deprecated) -// - SOURCE_BSDIFF: Read the data in src_extents in the old partition, perform -// bspatch with the attached data and write the new data to dst_extents in the -// new partition. -// - ZERO: Write zeros to the destination dst_extents. -// - DISCARD: Discard the destination dst_extents blocks on the physical medium. -// the data read from those blocks is undefined. -// - REPLACE_XZ: Replace the dst_extents with the contents of the attached -// xz file after decompression. The xz file should only use crc32 or no crc at -// all to be compatible with xz-embedded. -// - PUFFDIFF: Read the data in src_extents in the old partition, perform -// puffpatch with the attached data and write the new data to dst_extents in -// the new partition. -// -// The operations allowed in the payload (supported by the client) depend on the -// major and minor version. See InstallOperation.Type below for details. -syntax = "proto2"; -package chromeos_update_engine; -// Data is packed into blocks on disk, always starting from the beginning -// of the block. If a file's data is too large for one block, it overflows -// into another block, which may or may not be the following block on the -// physical partition. An ordered list of extents is another -// representation of an ordered list of blocks. For example, a file stored -// in blocks 9, 10, 11, 2, 18, 12 (in that order) would be stored in -// extents { {9, 3}, {2, 1}, {18, 1}, {12, 1} } (in that order). -// In general, files are stored sequentially on disk, so it's more efficient -// to use extents to encode the block lists (this is effectively -// run-length encoding). -// A sentinel value (kuint64max) as the start block denotes a sparse-hole -// in a file whose block-length is specified by num_blocks. -message Extent { - optional uint64 start_block = 1; - optional uint64 num_blocks = 2; -} -// Signatures: Updates may be signed by the OS vendor. The client verifies -// an update's signature by hashing the entire download. The section of the -// download that contains the signature is at the end of the file, so when -// signing a file, only the part up to the signature part is signed. -// Then, the client looks inside the download's Signatures message for a -// Signature message that it knows how to handle. Generally, a client will -// only know how to handle one type of signature, but an update may contain -// many signatures to support many different types of client. Then client -// selects a Signature message and uses that, along with a known public key, -// to verify the download. The public key is expected to be part of the -// client. -message Signatures { - message Signature { - optional uint32 version = 1 [deprecated = true]; - optional bytes data = 2; - // The DER encoded signature size of EC keys is nondeterministic for - // different input of sha256 hash. However, we need the size of the - // serialized signatures protobuf string to be fixed before signing; - // because this size is part of the content to be signed. Therefore, we - // always pad the signature data to the maximum possible signature size of - // a given key. And the payload verifier will truncate the signature to - // its correct size based on the value of |unpadded_signature_size|. - optional fixed32 unpadded_signature_size = 3; - } - repeated Signature signatures = 1; -} -message PartitionInfo { - optional uint64 size = 1; - optional bytes hash = 2; -} -message InstallOperation { - enum Type { - REPLACE = 0; // Replace destination extents w/ attached data. - REPLACE_BZ = 1; // Replace destination extents w/ attached bzipped data. - MOVE = 2 [deprecated = true]; // Move source extents to target extents. - BSDIFF = 3 [deprecated = true]; // The data is a bsdiff binary diff. - // On minor version 2 or newer, these operations are supported: - SOURCE_COPY = 4; // Copy from source to target partition - SOURCE_BSDIFF = 5; // Like BSDIFF, but read from source partition - // On minor version 3 or newer and on major version 2 or newer, these - // operations are supported: - REPLACE_XZ = 8; // Replace destination extents w/ attached xz data. - // On minor version 4 or newer, these operations are supported: - ZERO = 6; // Write zeros in the destination. - DISCARD = 7; // Discard the destination blocks, reading as undefined. - BROTLI_BSDIFF = 10; // Like SOURCE_BSDIFF, but compressed with brotli. - // On minor version 5 or newer, these operations are supported: - PUFFDIFF = 9; // The data is in puffdiff format. - // On minor version 8 or newer, these operations are supported: - ZUCCHINI = 11; - // On minor version 9 or newer, these operations are supported: - LZ4DIFF_BSDIFF = 12; - LZ4DIFF_PUFFDIFF = 13; - // This is not part of the original spec, but is currently in use for some - // types of payloads. - REPLACE_ZSTD = 14; // Replace destination extents w/ attached zstd data. - } - required Type type = 1; - // Only minor version 6 or newer support 64 bits |data_offset| and - // |data_length|, older client will read them as uint32. - // The offset into the delta file (after the protobuf) - // where the data (if any) is stored - optional uint64 data_offset = 2; - // The length of the data in the delta file - optional uint64 data_length = 3; - // Ordered list of extents that are read from (if any) and written to. - repeated Extent src_extents = 4; - // Byte length of src, equal to the number of blocks in src_extents * - // block_size. It is used for BSDIFF and SOURCE_BSDIFF, because we need to - // pass that external program the number of bytes to read from the blocks we - // pass it. This is not used in any other operation. - optional uint64 src_length = 5; - repeated Extent dst_extents = 6; - // Byte length of dst, equal to the number of blocks in dst_extents * - // block_size. Used for BSDIFF and SOURCE_BSDIFF, but not in any other - // operation. - optional uint64 dst_length = 7; - // Optional SHA 256 hash of the blob associated with this operation. - // This is used as a primary validation for http-based downloads and - // as a defense-in-depth validation for https-based downloads. If - // the operation doesn't refer to any blob, this field will have - // zero bytes. - optional bytes data_sha256_hash = 8; - // Indicates the SHA 256 hash of the source data referenced in src_extents at - // the time of applying the operation. If present, the update_engine daemon - // MUST read and verify the source data before applying the operation. - optional bytes src_sha256_hash = 9; -} -// Hints to VAB snapshot to skip writing some blocks if these blocks are -// identical to the ones on the source image. The src & dst extents for each -// CowMergeOperation should be contiguous, and they're a subset of an OTA -// InstallOperation. -// During merge time, we need to follow the pre-computed sequence to avoid -// read after write, similar to the inplace update schema. -message CowMergeOperation { - enum Type { - COW_COPY = 0; // identical blocks - COW_XOR = 1; // used when src/dst blocks are highly similar - COW_REPLACE = 2; // Raw replace operation - } - optional Type type = 1; - optional Extent src_extent = 2; - optional Extent dst_extent = 3; - // For COW_XOR, source location might be unaligned, so this field is in range - // [0, block_size), representing how much should the src_extent shift toward - // larger block number. If this field is non-zero, then src_extent will - // include 1 extra block in the end, as the merge op actually references the - // first |src_offset| bytes of that extra block. For example, if |dst_extent| - // is [10, 15], |src_offset| is 500, then src_extent might look like [25, 31]. - // Note that |src_extent| contains 1 extra block than the |dst_extent|. - optional uint32 src_offset = 4; -} -// Describes the update to apply to a single partition. -message PartitionUpdate { - // A platform-specific name to identify the partition set being updated. For - // example, in Chrome OS this could be "ROOT" or "KERNEL". - required string partition_name = 1; - // Whether this partition carries a filesystem with post-install program that - // must be run to finalize the update process. See also |postinstall_path| and - // |filesystem_type|. - optional bool run_postinstall = 2; - // The path of the executable program to run during the post-install step, - // relative to the root of this filesystem. If not set, the default "postinst" - // will be used. This setting is only used when |run_postinstall| is set and - // true. - optional string postinstall_path = 3; - // The filesystem type as passed to the mount(2) syscall when mounting the new - // filesystem to run the post-install program. If not set, a fixed list of - // filesystems will be attempted. This setting is only used if - // |run_postinstall| is set and true. - optional string filesystem_type = 4; - // If present, a list of signatures of the new_partition_info.hash signed with - // different keys. If the update_engine daemon requires vendor-signed images - // and has its public key installed, one of the signatures should be valid - // for /postinstall to run. - repeated Signatures.Signature new_partition_signature = 5; - optional PartitionInfo old_partition_info = 6; - optional PartitionInfo new_partition_info = 7; - // The list of operations to be performed to apply this PartitionUpdate. The - // associated operation blobs (in operations[i].data_offset, data_length) - // should be stored contiguously and in the same order. - repeated InstallOperation operations = 8; - // Whether a failure in the postinstall step for this partition should be - // ignored. - optional bool postinstall_optional = 9; - // On minor version 6 or newer, these fields are supported: - // The extent for data covered by verity hash tree. - optional Extent hash_tree_data_extent = 10; - // The extent to store verity hash tree. - optional Extent hash_tree_extent = 11; - // The hash algorithm used in verity hash tree. - optional string hash_tree_algorithm = 12; - // The salt used for verity hash tree. - optional bytes hash_tree_salt = 13; - // The extent for data covered by FEC. - optional Extent fec_data_extent = 14; - // The extent to store FEC. - optional Extent fec_extent = 15; - // The number of FEC roots. - optional uint32 fec_roots = 16 [default = 2]; - // Per-partition version used for downgrade detection, added - // as an effort to support partial updates. For most partitions, - // this is the build timestamp. - optional string version = 17; - // A sorted list of CowMergeOperation. When writing cow, we can choose to - // skip writing the raw bytes for these extents. During snapshot merge, the - // bytes will read from the source partitions instead. - repeated CowMergeOperation merge_operations = 18; - // Estimated size for COW image. This is used by libsnapshot - // as a hint. If set to 0, libsnapshot should use alternative - // methods for estimating size. - optional uint64 estimate_cow_size = 19; -} -message DynamicPartitionGroup { - // Name of the group. - required string name = 1; - // Maximum size of the group. The sum of sizes of all partitions in the group - // must not exceed the maximum size of the group. - optional uint64 size = 2; - // A list of partitions that belong to the group. - repeated string partition_names = 3; -} -message VABCFeatureSet { - optional bool threaded = 1; - optional bool batch_writes = 2; -} -// Metadata related to all dynamic partitions. -message DynamicPartitionMetadata { - // All updatable groups present in |partitions| of this DeltaArchiveManifest. - // - If an updatable group is on the device but not in the manifest, it is - // not updated. Hence, the group will not be resized, and partitions cannot - // be added to or removed from the group. - // - If an updatable group is in the manifest but not on the device, the group - // is added to the device. - repeated DynamicPartitionGroup groups = 1; - // Whether dynamic partitions have snapshots during the update. If this is - // set to true, the update_engine daemon creates snapshots for all dynamic - // partitions if possible. If this is unset, the update_engine daemon MUST - // NOT create snapshots for dynamic partitions. - optional bool snapshot_enabled = 2; - // If this is set to false, update_engine should not use VABC regardless. If - // this is set to true, update_engine may choose to use VABC if device - // supports it, but not guaranteed. - // VABC stands for Virtual AB Compression - optional bool vabc_enabled = 3; - // The compression algorithm used by VABC. Available ones are "gz", "brotli". - // See system/core/fs_mgr/libsnapshot/cow_writer.cpp for available options, - // as this parameter is ultimated forwarded to libsnapshot's CowWriter - optional string vabc_compression_param = 4; - // COW version used by VABC. The represents the major version in the COW - // header - optional uint32 cow_version = 5; - // A collection of knobs to tune Virtual AB Compression - optional VABCFeatureSet vabc_feature_set = 6; -} -// Definition has been duplicated from -// $ANDROID_BUILD_TOP/build/tools/releasetools/ota_metadata.proto. Keep in sync. -message ApexInfo { - optional string package_name = 1; - optional int64 version = 2; - optional bool is_compressed = 3; - optional int64 decompressed_size = 4; -} -// Definition has been duplicated from -// $ANDROID_BUILD_TOP/build/tools/releasetools/ota_metadata.proto. Keep in sync. -message ApexMetadata { - repeated ApexInfo apex_info = 1; -} -message DeltaArchiveManifest { - // Only present in major version = 1. List of install operations for the - // kernel and rootfs partitions. For major version = 2 see the |partitions| - // field. - reserved 1, 2; - // (At time of writing) usually 4096 - optional uint32 block_size = 3 [default = 4096]; - // If signatures are present, the offset into the blobs, generally - // tacked onto the end of the file, and the length. We use an offset - // rather than a bool to allow for more flexibility in future file formats. - // If either is absent, it means signatures aren't supported in this - // file. - optional uint64 signatures_offset = 4; - optional uint64 signatures_size = 5; - // Fields deprecated in major version 2. - reserved 6,7,8,9,10,11; - // The minor version, also referred as "delta version", of the payload. - // Minor version 0 is full payload, everything else is delta payload. - optional uint32 minor_version = 12 [default = 0]; - // Only present in major version >= 2. List of partitions that will be - // updated, in the order they will be updated. This field replaces the - // |install_operations|, |kernel_install_operations| and the - // |{old,new}_{kernel,rootfs}_info| fields used in major version = 1. This - // array can have more than two partitions if needed, and they are identified - // by the partition name. - repeated PartitionUpdate partitions = 13; - // The maximum timestamp of the OS allowed to apply this payload. - // Can be used to prevent downgrading the OS. - optional int64 max_timestamp = 14; - // Metadata related to all dynamic partitions. - optional DynamicPartitionMetadata dynamic_partition_metadata = 15; - // If the payload only updates a subset of partitions on the device. - // optional bool partial_update = 16; - // Information on compressed APEX to figure out how much space is required for - // their decompression - // repeated ApexInfo apex_info = 17; - // Security patch level of the device, usually in the format of - // yyyy-mm-dd - // optional string security_patch_level = 18; - reserved 16 to 18; -} diff --git a/src/python.rs b/src/python.rs deleted file mode 100644 index 3173fb8..0000000 --- a/src/python.rs +++ /dev/null @@ -1,53 +0,0 @@ -use std::path::PathBuf; - -use pyo3::create_exception; -use pyo3::exceptions::PyException; -use pyo3::prelude::*; - -use crate::ExtractOptions; - -create_exception!(otadump, OtaDumpError, PyException); - -/// Extract partitions from an Android OTA payload or OTA ZIP. -#[pyfunction] -#[pyo3(signature = ( - payload_file, - output_dir, - *, - num_threads = None, - overwrite = false, - partitions = None, - verify = true, -))] -fn extract( - py: Python<'_>, - payload_file: PathBuf, - output_dir: PathBuf, - num_threads: Option, - overwrite: bool, - partitions: Option>, - verify: bool, -) -> PyResult<()> { - let mut options = ExtractOptions::new(); - options.overwrite(overwrite).verify(verify); - - if let Some(num_threads) = num_threads { - options.num_threads(num_threads); - } - if let Some(partitions) = partitions { - options.partitions(partitions); - } - - py.detach(move || { - options - .extract(payload_file, output_dir) - .map_err(|error| OtaDumpError::new_err(error.to_string())) - }) -} - -#[pymodule] -fn otadump(module: &Bound<'_, PyModule>) -> PyResult<()> { - module.add("OtaDumpError", module.py().get_type::())?; - module.add_function(wrap_pyfunction!(extract, module)?)?; - Ok(()) -} diff --git a/tests/artifact_smoke.py b/tests/artifact_smoke.py new file mode 100644 index 0000000..db0679f --- /dev/null +++ b/tests/artifact_smoke.py @@ -0,0 +1,14 @@ +import subprocess +import sys +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) + +from otadump._artifact import executable + + +result = subprocess.run([executable(), "--help"], text=True, capture_output=True, check=False) +assert result.returncode == 1 # gflags uses status 1 after printing help. +assert "system/update_engine/aosp/ota_extractor.cc" in result.stdout +assert "input_dir" in result.stdout +assert "single_thread" in result.stdout diff --git a/tests/python_smoke.py b/tests/python_smoke.py deleted file mode 100644 index 8c9d270..0000000 --- a/tests/python_smoke.py +++ /dev/null @@ -1,26 +0,0 @@ -from inspect import signature -from pathlib import Path -from tempfile import TemporaryDirectory - -import otadump - - -def main() -> None: - assert issubclass(otadump.OtaDumpError, Exception) - assert "partitions" in str(signature(otadump.extract)) - - with TemporaryDirectory() as temporary_directory: - temporary_path = Path(temporary_directory) - payload_file = temporary_path / "invalid.bin" - payload_file.write_bytes(b"invalid") - - try: - otadump.extract(payload_file, temporary_path / "output") - except otadump.OtaDumpError as error: - assert str(error) == "Invalid payload file" - else: - raise AssertionError("invalid payload unexpectedly succeeded") - - -if __name__ == "__main__": - main() diff --git a/tests/test_artifact.py b/tests/test_artifact.py new file mode 100644 index 0000000..c0bcf3e --- /dev/null +++ b/tests/test_artifact.py @@ -0,0 +1,55 @@ +import hashlib +import os +import tempfile +import unittest +import zipfile +from pathlib import Path +from unittest import mock + +from otadump import _artifact + + +class ArtifactTest(unittest.TestCase): + def test_download_verify_and_cache(self): + with tempfile.TemporaryDirectory() as name: + root = Path(name) + bundle = root / "bundle.zip" + data = b"#!/bin/sh\nexit 0\n" + with zipfile.ZipFile(bundle, "w") as archive: + archive.writestr("bin/ota_extractor", data) + lock = { + "bundle_url": bundle.as_uri(), + "bundle_sha256": hashlib.sha256(bundle.read_bytes()).hexdigest(), + "files": {"bin/ota_extractor": hashlib.sha256(data).hexdigest()}, + } + cache = root / "cache" + with mock.patch.object(_artifact, "_lock", return_value=lock), mock.patch.dict( + os.environ, {"OTADUMP_CACHE_DIR": str(cache)} + ): + executable = _artifact.executable() + self.assertTrue(os.access(executable, os.X_OK)) + executable.write_bytes(b"tampered") + repaired = _artifact.executable() + self.assertEqual(repaired.read_bytes(), data) + bundle.unlink() + self.assertEqual(_artifact.executable(), executable) + + def test_bundle_checksum_is_enforced(self): + with tempfile.TemporaryDirectory() as name: + root = Path(name) + bundle = root / "bundle.zip" + bundle.write_bytes(b"not trusted") + lock = { + "bundle_url": bundle.as_uri(), + "bundle_sha256": "0" * 64, + "files": {"bin/ota_extractor": "1" * 64}, + } + with mock.patch.object(_artifact, "_lock", return_value=lock), mock.patch.dict( + os.environ, {"OTADUMP_CACHE_DIR": str(root / "cache")} + ): + with self.assertRaisesRegex(_artifact.ArtifactError, "checksum mismatch"): + _artifact.executable() + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_extract.py b/tests/test_extract.py new file mode 100644 index 0000000..3af6ce0 --- /dev/null +++ b/tests/test_extract.py @@ -0,0 +1,83 @@ +import subprocess +import tempfile +import unittest +import zipfile +from pathlib import Path +from unittest import mock + +import otadump + + +class ExtractTest(unittest.TestCase): + def setUp(self): + self.temp = tempfile.TemporaryDirectory() + self.root = Path(self.temp.name) + self.payload = self.root / "payload.bin" + self.payload.write_bytes(b"CrAU payload") + + def tearDown(self): + self.temp.cleanup() + + @mock.patch("otadump._extract._artifact.executable", return_value=Path("/tool")) + @mock.patch("otadump._extract.subprocess.run") + def test_raw_payload_arguments(self, run, _executable): + run.return_value = subprocess.CompletedProcess([], 0, "", "") + source = self.root / "source" + source.mkdir() + + otadump.extract( + self.payload, + self.root / "output", + source_dir=source, + partitions=["boot", "system"], + single_thread=True, + ) + + command = run.call_args.args[0] + self.assertIn(f"--payload={self.payload}", command) + self.assertIn(f"--input_dir={source}", command) + self.assertIn("--partitions=boot,system", command) + self.assertIn("--single_thread", command) + + @mock.patch("otadump._extract._artifact.executable", return_value=Path("/tool")) + @mock.patch("otadump._extract.subprocess.run") + def test_zip_payload_offset_points_to_payload(self, run, _executable): + run.return_value = subprocess.CompletedProcess([], 0, "", "") + ota = self.root / "ota.zip" + with zipfile.ZipFile(ota, "w") as archive: + archive.writestr("metadata", "value") + archive.writestr("payload.bin", b"CrAU payload") + + otadump.extract(ota, self.root / "output") + + command = run.call_args.args[0] + offset = int(next(arg.split("=", 1)[1] for arg in command if arg.startswith("--payload_offset="))) + with ota.open("rb") as stream: + stream.seek(offset) + self.assertEqual(stream.read(4), b"CrAU") + + @mock.patch("otadump._extract._artifact.executable", return_value=Path("/tool")) + @mock.patch("otadump._extract.subprocess.run") + def test_native_failure_is_raised(self, run, _executable): + run.return_value = subprocess.CompletedProcess([], 1, "", "bad manifest") + with self.assertRaisesRegex(otadump.OtaDumpError, "bad manifest"): + otadump.extract(self.payload, self.root / "output") + + @mock.patch("otadump._extract._artifact.executable") + def test_artifact_failure_is_public_error(self, executable): + executable.side_effect = otadump._extract._artifact.ArtifactError("unsupported") + with self.assertRaisesRegex(otadump.OtaDumpError, "unsupported"): + otadump.extract(self.payload, self.root / "output") + + @mock.patch("otadump._extract._artifact.executable", return_value=Path("/tool")) + def test_partition_name_is_not_treated_as_iterable(self, _executable): + with self.assertRaises(TypeError): + otadump.extract(self.payload, self.root / "output", partitions="boot") + + def test_missing_inputs_fail_before_artifact_download(self): + with self.assertRaisesRegex(otadump.OtaDumpError, "payload does not exist"): + otadump.extract(self.root / "missing", self.root / "output") + + +if __name__ == "__main__": + unittest.main()