diff --git a/.bestool.nix b/.bestool.nix new file mode 100644 index 0000000..593bc85 --- /dev/null +++ b/.bestool.nix @@ -0,0 +1,26 @@ +{ lib +, pkgs +, rustPlatform +, +}: +rustPlatform.buildRustPackage { + name = "bestool"; + + src = ./bestool; + + cargoLock = { + lockFile = ./bestool/Cargo.lock; + allowBuiltinFetchGit = true; + }; + + nativeBuildInputs = with pkgs; [ pkg-config ]; + buildInputs = with pkgs; [ systemd.dev ]; + + meta = with lib; { + description = ""; + homepage = "https://github.com/Ralim/bestool"; + platforms = platforms.linux; + maintainers = with maintainers; [ shymega ]; + license = licenses.mit; + }; +} diff --git a/.github/workflows/ci-debug-builds-linux.yml b/.github/workflows/ci-debug-builds-linux.yml new file mode 100644 index 0000000..0d664b6 --- /dev/null +++ b/.github/workflows/ci-debug-builds-linux.yml @@ -0,0 +1,60 @@ +--- +name: Nightly builder (Linux) + +on: + workflow_call: + +env: + CARGO_TERM_COLOR: always + +jobs: + build-nightlies-linux: + name: Nightly builds for Linux + runs-on: ubuntu-latest + strategy: + fail-fast: true + matrix: + target: [aarch64-unknown-linux-musl, x86_64-unknown-linux-musl] + steps: + - name: Checkout sources + uses: actions/checkout@v4 + - name: Cache Rust assets + id: rust-cache + uses: Swatinem/rust-cache@v2 + - name: Configure Rust + if: ${{ steps.rust-cache.outputs.cache-hit != 'true' }} + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + default: true + override: true + target: ${{ matrix.target }} + profile: minimal + - name: Install Cross + if: ${{ steps.rust-cache.outputs.cache-hit != 'true' }} + uses: brndnmtthws/rust-action-cargo-binstall@v1.1.0 + with: + packages: cross + - name: Build bestool release for Linux. + id: build + uses: actions-rs/cargo@v1 + with: + command: build + args: >- + --all --locked --verbose --target=${{ matrix.target }} + use-cross: true + - name: Create nightly release + continue-on-error: true + uses: actions/create-release@v1 + with: + draft: false + prerelease: true + body: "Nightly build of `bestool` for Linux: ${{ matrix.target }}" + release_name: bestool-nightly-${{ matrix.target }} + tag_name: bestool-nightly-${{ matrix.target }} + env: + GITHUB_TOKEN: ${{ github.token }} + - name: Upload debug nightly build + run: gh release upload bestool-nightly-${{ matrix.target }} ${{ github.workspace }}/target/${{ matrix.target }}/debug/bestool --clobber + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/ci-debug-builds-macos.yml b/.github/workflows/ci-debug-builds-macos.yml new file mode 100644 index 0000000..e7a9ae8 --- /dev/null +++ b/.github/workflows/ci-debug-builds-macos.yml @@ -0,0 +1,54 @@ +--- +name: Nightly builder (macOS) + +on: + workflow_call: + +env: + CARGO_TERM_COLOR: always + +jobs: + build-nightlies-macos: + name: Nightly builds for macOS + runs-on: macos-14 + strategy: + fail-fast: true + matrix: + target: [aarch64-apple-darwin, x86_64-apple-darwin] + steps: + - name: Checkout sources + uses: actions/checkout@v4 + - name: Cache Rust assets + id: rust-cache + uses: Swatinem/rust-cache@v2 + - name: Configure Rust + if: ${{ steps.rust-cache.outputs.cache-hit != 'true' }} + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + default: true + override: true + target: ${{ matrix.target }} + profile: minimal + - name: Build bestool release for macOS. + id: build + uses: actions-rs/cargo@v1 + with: + command: build + args: >- + --all --locked --verbose --target=${{ matrix.target }} + - name: Create nightly release + continue-on-error: true + uses: actions/create-release@v1 + with: + draft: false + prerelease: true + body: "Nightly build of `bestool` for macOS: ${{ matrix.target }}" + release_name: bestool-nightly-${{ matrix.target }} + tag_name: bestool-nightly-${{ matrix.target }} + env: + GITHUB_TOKEN: ${{ github.token }} + - name: Upload debug nightly build + run: gh release upload bestool-nightly-${{ matrix.target }} ${{ github.workspace }}/target/${{ matrix.target }}/debug/bestool --clobber + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/ci-debug-builds-windows.yml b/.github/workflows/ci-debug-builds-windows.yml new file mode 100644 index 0000000..b06346f --- /dev/null +++ b/.github/workflows/ci-debug-builds-windows.yml @@ -0,0 +1,54 @@ +--- +name: Nightly builder (Windows) + +on: + workflow_call: + +env: + CARGO_TERM_COLOR: always + +jobs: + build-nightlies-windows: + name: Nightly builds for Windows + runs-on: windows-latest + strategy: + fail-fast: true + matrix: + target: [x86_64-pc-windows-gnu] + steps: + - name: Checkout sources + uses: actions/checkout@v4 + - name: Cache Rust assets + id: rust-cache + uses: Swatinem/rust-cache@v2 + - name: Configure Rust + if: ${{ steps.rust-cache.outputs.cache-hit != 'true' }} + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + default: true + override: true + target: ${{ matrix.target }} + profile: minimal + - name: Build bestool release for Windows. + id: build + uses: actions-rs/cargo@v1 + with: + command: build + args: >- + --all --locked --verbose --target=${{ matrix.target }} + - name: Create nightly release + continue-on-error: true + uses: actions/create-release@v1 + with: + draft: false + prerelease: true + body: "Nightly build of `bestool` for Windows: ${{ matrix.target }}" + release_name: bestool-nightly-${{ matrix.target }} + tag_name: bestool-nightly-${{ matrix.target }} + env: + GITHUB_TOKEN: ${{ github.token }} + - name: Upload debug nightly build + run: gh release upload bestool-nightly-${{ matrix.target }} ${{ github.workspace }}/target/${{ matrix.target }}/debug/bestool.exe --clobber + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/ci-debug-builds.yml b/.github/workflows/ci-debug-builds.yml new file mode 100644 index 0000000..254402f --- /dev/null +++ b/.github/workflows/ci-debug-builds.yml @@ -0,0 +1,43 @@ +--- +name: Nightly Builders + +on: + workflow_dispatch: + schedule: + - cron: 0 0 * * 1 + +jobs: + check_date: + runs-on: ubuntu-latest + name: Check latest commit + if: ${{ github.event_name == 'schedule' }} + outputs: + should_run: ${{ steps.should_run.outputs.should_run }} + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - id: should_run + continue-on-error: true + name: Check latest commit is less than a day + run: test -z $(git rev-list --after="24 hours" ${{ github.sha }}) && echo + "::set-output name=should_run::false" + + build-macos-debug: + needs: check_date + if: ${{ needs.check_date.outputs.should_run != 'false' }} + uses: ./.github/workflows/ci-debug-builds-macos.yml + secrets: inherit + + build-linux-debug: + needs: check_date + if: ${{ needs.check_date.outputs.should_run != 'false' }} + uses: ./.github/workflows/ci-debug-builds-linux.yml + secrets: inherit + + build-windows-debug: + needs: check_date + if: ${{ needs.check_date.outputs.should_run != 'false' }} + uses: ./.github/workflows/ci-debug-builds-windows.yml + secrets: inherit diff --git a/.github/workflows/ci-tests-common.yml b/.github/workflows/ci-tests-common.yml new file mode 100644 index 0000000..fa94e43 --- /dev/null +++ b/.github/workflows/ci-tests-common.yml @@ -0,0 +1,34 @@ +--- +name: CI tests (Common) + +on: + workflow_call: + +env: + CARGO_TERM_COLOR: always + +jobs: + run-tests: + name: Common CI tests + runs-on: ubuntu-latest + steps: + - name: Checkout sources + uses: actions/checkout@v4 + - name: Configure Rust + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + profile: minimal + components: clippy,rustfmt + override: true + default: true + - name: Install `cargo-audit` + run: cargo install --locked cargo-audit + - name: Install `cargo-vet` + run: cargo install --locked cargo-vet --version "0.9.0" + - name: Run `cargo-vet` + run: cargo vet --locked + working-directory: src/oldbestool + - name: Run `cargo-audit` + run: cargo audit -D unsound -D yanked -D unmaintained --ignore "RUSTSEC-2020-0168" + working-directory: src/oldbestool diff --git a/.github/workflows/ci-tests-linux.yml b/.github/workflows/ci-tests-linux.yml new file mode 100644 index 0000000..36e25f2 --- /dev/null +++ b/.github/workflows/ci-tests-linux.yml @@ -0,0 +1,59 @@ +--- +name: CI tests (Linux) + +on: + workflow_call: + +env: + CARGO_TERM_COLOR: always + +jobs: + run-linux-tests: + name: CI tests for ${{ matrix.target }} + runs-on: ubuntu-latest + strategy: + fail-fast: true + matrix: + target: [aarch64-unknown-linux-musl, x86_64-unknown-linux-musl] + steps: + - name: Checkout sources + uses: actions/checkout@v4 + - name: Cache Rust assets + id: rust-cache + uses: Swatinem/rust-cache@v2 + - name: Configure Rust + if: ${{ steps.rust-cache.outputs.cache-hit != 'true' }} + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + profile: minimal + components: clippy,rustfmt + override: true + target: ${{ matrix.target }} + default: true + - name: Install Cross + if: ${{ steps.rust-cache.outputs.cache-hit != 'true' }} + uses: brndnmtthws/rust-action-cargo-binstall@v1.1.0 + with: + packages: cross + + - name: Run Linux tests of `bestool` + uses: actions-rs/cargo@v1 + with: + command: test + args: --target=${{ matrix.target }} + use-cross: true + + - name: Run Linux checks of `bestool` + uses: actions-rs/cargo@v1 + with: + command: check + args: --target=${{ matrix.target }} + use-cross: true + + - name: Run Linux Clippy tests of `bestool` + uses: actions-rs/cargo@v1 + with: + command: clippy + args: --target=${{ matrix.target }} + use-cross: true diff --git a/.github/workflows/ci-tests-macos.yml b/.github/workflows/ci-tests-macos.yml new file mode 100644 index 0000000..20f17ce --- /dev/null +++ b/.github/workflows/ci-tests-macos.yml @@ -0,0 +1,48 @@ +--- +name: CI tests (macOS) + +on: + workflow_call: + +env: + CARGO_TERM_COLOR: always + +jobs: + run-macos-tests: + name: CI tests for ${{ matrix.target }} + runs-on: macos-14 + strategy: + fail-fast: true + matrix: + target: [aarch64-apple-darwin, x86_64-apple-darwin] + steps: + - name: Checkout sources + uses: actions/checkout@v4 + - name: Cache Rust assets + id: rust-cache + uses: Swatinem/rust-cache@v2 + - name: Configure Rust + if: ${{ steps.rust-cache.outputs.cache-hit != 'true' }} + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + profile: minimal + components: clippy,rustfmt + override: true + target: ${{ matrix.target }} + default: true + - name: Run macOS tests of `bestool` + uses: actions-rs/cargo@v1 + with: + command: test + args: --target=${{ matrix.target }} + - name: Run macOS checks of `bestool` + uses: actions-rs/cargo@v1 + with: + command: check + args: --target=${{ matrix.target }} + - name: Run macOS Clippy tests of `bestool` + uses: actions-rs/cargo@v1 + with: + command: clippy + args: --target=${{ matrix.target }} diff --git a/.github/workflows/ci-tests-nix.yml b/.github/workflows/ci-tests-nix.yml new file mode 100644 index 0000000..ca153bf --- /dev/null +++ b/.github/workflows/ci-tests-nix.yml @@ -0,0 +1,22 @@ +--- +name: CI tests (Nix) + +on: + workflow_call: + +jobs: + run-tests: + name: Nix CI tests + runs-on: ubuntu-latest + steps: + - name: Checkout sources + uses: actions/checkout@v4 + - uses: cachix/install-nix-action@v25 + - name: Build Nix package + run: nix build + - name: Check Flake formatting + run: nix run nixpkgs#nixpkgs-fmt -- --check ./ + - name: Check Flake with Nix + run: nix flake check + - name: Check Flake with Statix + run: nix run nixpkgs#statix -- check ./ diff --git a/.github/workflows/ci-tests-windows.yml b/.github/workflows/ci-tests-windows.yml new file mode 100644 index 0000000..a72bcd5 --- /dev/null +++ b/.github/workflows/ci-tests-windows.yml @@ -0,0 +1,48 @@ +--- +name: CI tests (Windows) + +on: + workflow_call: + +env: + CARGO_TERM_COLOR: always + +jobs: + run-windows-tests: + name: CI tests for ${{ matrix.target }} + runs-on: windows-latest + strategy: + fail-fast: true + matrix: + target: [x86_64-pc-windows-gnu] + steps: + - name: Checkout sources + uses: actions/checkout@v4 + - name: Cache Rust assets + id: rust-cache + uses: Swatinem/rust-cache@v2 + - name: Configure Rust + if: ${{ steps.rust-cache.outputs.cache-hit != 'true' }} + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + profile: minimal + components: clippy,rustfmt + override: true + target: ${{ matrix.target }} + default: true + - name: Run Windows tests of `bestool` + uses: actions-rs/cargo@v1 + with: + command: test + args: --target=${{ matrix.target }} + - name: Run Windows checks of `bestool` + uses: actions-rs/cargo@v1 + with: + command: check + args: --target=${{ matrix.target }} + - name: Run Windows Clippy tests of `bestool` + uses: actions-rs/cargo@v1 + with: + command: clippy + args: --target=${{ matrix.target }} diff --git a/.github/workflows/ci-tests.yml b/.github/workflows/ci-tests.yml new file mode 100644 index 0000000..2a02964 --- /dev/null +++ b/.github/workflows/ci-tests.yml @@ -0,0 +1,28 @@ +--- +name: CI Tests + +on: + push: + paths: ["src/", "Cargo.toml", "Cargo.lock"] + workflow_dispatch: + +jobs: + test-macos: + uses: ./.github/workflows/ci-tests-macos.yml + secrets: inherit + + test-nix: + uses: ./.github/workflows/ci-tests-nix.yml + secrets: inherit + + test-linux: + uses: ./.github/workflows/ci-tests-linux.yml + secrets: inherit + + test-windows: + uses: ./.github/workflows/ci-tests-windows.yml + secrets: inherit + + test-common: + uses: ./.github/workflows/ci-tests-common.yml + secrets: inherit diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml deleted file mode 100644 index a0db7d1..0000000 --- a/.github/workflows/test.yml +++ /dev/null @@ -1,51 +0,0 @@ -name: Tests & Build -on: [push, pull_request] - -jobs: - test: - name: cargo test - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@stable - - run: sudo apt-get update && sudo apt-get install -y libudev-dev - - run: cargo test - working-directory: bestool - audit: - name: cargo audits - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@stable - with: - components: clippy,rustfmt - - run: sudo apt-get update && sudo apt-get install -y libudev-dev - - run: cargo install cargo-audit - working-directory: bestool - - run: cargo clippy -- -D warnings --no-deps - working-directory: bestool - # Ignoring mach being unmaintained for now as limited options + its only for MacOS - - run: cargo audit -D unsound -D yanked -D unmaintained --ignore "RUSTSEC-2020-0168" - working-directory: bestool - vet: - name: cargo vet - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@stable - with: - components: clippy,rustfmt - - run: sudo apt-get update && sudo apt-get install -y libudev-dev - - run: cargo install --locked cargo-vet --version "0.9.0" - working-directory: bestool - - run: cargo vet --locked - working-directory: bestool - build: - name: cargo build - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@stable - - run: sudo apt-get update && sudo apt-get install -y libudev-dev - - run: cargo build - working-directory: bestool diff --git a/bestool/res/chunk1.bin b/bestool/res/chunk1.bin new file mode 100644 index 0000000..13535bc Binary files /dev/null and b/bestool/res/chunk1.bin differ diff --git a/bestool/res/chunk2.bin b/bestool/res/chunk2.bin new file mode 100644 index 0000000..0e9a20d Binary files /dev/null and b/bestool/res/chunk2.bin differ diff --git a/bestool/res/programmer.bin b/bestool/res/programmer.bin new file mode 100644 index 0000000..34eaf62 Binary files /dev/null and b/bestool/res/programmer.bin differ diff --git a/bestool/src/beslink/bootloader.rs b/bestool/src/beslink/bootloader.rs index 1196d35..6013eae 100644 --- a/bestool/src/beslink/bootloader.rs +++ b/bestool/src/beslink/bootloader.rs @@ -4,7 +4,7 @@ use std::io::Write; use tracing::error; use tracing::info; //Embed the bin file for future -const PROGRAMMER_BINARY: &[u8; 75928] = include_bytes!("../../../programmer.bin"); +const PROGRAMMER_BINARY: &[u8; 75928] = include_bytes!("../../res/programmer.bin"); pub fn load_programmer_runtime_binary_blob( serial_port: &mut Box, diff --git a/bestool/src/beslink/write_flash.rs b/bestool/src/beslink/write_flash.rs index 575bf9e..21a0b83 100644 --- a/bestool/src/beslink/write_flash.rs +++ b/bestool/src/beslink/write_flash.rs @@ -196,8 +196,8 @@ mod tests { use crate::beslink::write_flash::get_flash_chunk_msg; //Embed the bin file for future - const CHUNK1_TEST: &[u8; 32768] = include_bytes!("../../../chunk1.bin"); - const CHUNK2_TEST: &[u8; 32768] = include_bytes!("../../../chunk2.bin"); + const CHUNK1_TEST: &[u8; 32768] = include_bytes!("../../res/chunk1.bin"); + const CHUNK2_TEST: &[u8; 32768] = include_bytes!("../../res/chunk2.bin"); #[test] fn test_get_flash_chunk_msg() { diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..7aef5d9 --- /dev/null +++ b/flake.lock @@ -0,0 +1,78 @@ +{ + "nodes": { + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1733328505, + "narHash": "sha256-NeCCThCEP3eCl2l/+27kNNK7QrwZB1IJCrXfrbv5oqU=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "ff81ac966bb2cae68946d5ed5fc4994f96d0ffec", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1740019556, + "narHash": "sha256-vn285HxnnlHLWnv59Og7muqECNMS33mWLM14soFIv2g=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "dad564433178067be1fbdfcce23b546254b6d641", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-compat": "flake-compat", + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..c6e2401 --- /dev/null +++ b/flake.nix @@ -0,0 +1,42 @@ +{ + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + flake-utils.url = "github:numtide/flake-utils"; + flake-compat = { + url = "github:edolstra/flake-compat"; + flake = false; + }; + }; + outputs = + { self + , nixpkgs + , flake-utils + , ... + }: + flake-utils.lib.eachDefaultSystem + (system: + let + pkgs = import nixpkgs { + inherit system; + }; + in + with pkgs; { + packages.bestool = callPackage ./.bestool.nix { }; + packages.default = self.packages.${system}.bestool; + + devShells.default = mkShell { + inputsFrom = lib.singleton self.packages.${system}.default; + buildInputs = [ + cargo-edit + clippy + rustfmt + ]; + RUST_SRC_PATH = rustPlatform.rustLibSrc; + }; + }) + // { + overlays.default = final: prev: { + inherit (self.packages.${final.system}) bestool; + }; + }; +}