Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1fa4aa5532 | ||
|
|
315c595714 | ||
|
|
50102800a3 | ||
|
|
a998d7a3e3 | ||
|
|
cd02ff47a5 | ||
|
|
836a531e93 | ||
|
|
8d72f07cf0 | ||
|
|
bdea96229b | ||
|
|
d318b734fa | ||
|
|
4305da5e57 | ||
|
|
dc787f1f3b | ||
|
|
f63127d633 | ||
|
|
b02a14bcfc | ||
|
|
20dff12c05 | ||
|
|
ac6bc20c68 | ||
|
|
8f0accf21d | ||
|
|
69c5023f1e | ||
|
|
b242e34ca0 | ||
|
|
82492231bc | ||
|
|
27e1dc15e8 | ||
|
|
684be52d5a | ||
|
|
76de56fef5 | ||
|
|
ae9e2c67cc | ||
|
|
400edbe913 | ||
|
|
462ced01a6 | ||
|
|
c79ee1315d | ||
|
|
cce24b6880 | ||
|
|
b1316b2bd9 | ||
|
|
9f6eccc5ac | ||
|
|
4d1adeb05e | ||
|
|
628c0630a5 | ||
|
|
086c883c4a | ||
|
|
4324d3046e | ||
|
|
de5c1a5a15 | ||
|
|
d22ee79159 | ||
|
|
3d1fd36811 | ||
|
|
7f2ea8a8dc | ||
|
|
b26f389460 | ||
|
|
b46f15b340 | ||
|
|
c9d32f9793 | ||
|
|
5cf9d44d7e | ||
|
|
d9dc60918c | ||
|
|
bb31a7befc | ||
|
|
b1b7a3dbc0 | ||
|
|
8275f893dd | ||
|
|
36f12d6d4e | ||
|
|
a4d82bf45b | ||
|
|
d925414bc8 | ||
|
|
9f9fe18926 | ||
|
|
d8d044f439 | ||
|
|
517bee9b9e | ||
|
|
10d7fd6d95 | ||
|
|
9b440a46e3 | ||
|
|
c3b0dc6c8a | ||
|
|
bfda29a845 | ||
|
|
2c58cbac48 | ||
|
|
174b162dfd | ||
|
|
cbf0f70cf6 | ||
|
|
332a3f7419 | ||
|
|
3931b6348f | ||
|
|
69b1b6e6fb | ||
|
|
a732f01ac9 | ||
|
|
6da34a55e8 | ||
|
|
be5edfc2bf | ||
|
|
569ef9dcd8 | ||
|
|
b1313fd059 | ||
|
|
75b7424a0d | ||
|
|
8d4fe502be | ||
|
|
038c1b1c2b | ||
|
|
20d7f49c9d | ||
|
|
74cee50d35 | ||
|
|
32a75c92ef | ||
|
|
9ad8b56c1f | ||
|
|
28f8f9ce80 | ||
|
|
d35cedd056 | ||
|
|
a825299ebc | ||
|
|
a17f108050 | ||
|
|
3faa35ea70 | ||
|
|
16490dd3b9 | ||
|
|
f4d9c12bb6 | ||
|
|
e6ae9d9d50 | ||
|
|
fc1ed3e4e5 | ||
|
|
d7a914258e | ||
|
|
2108f47eea | ||
|
|
d4a1db7243 | ||
|
|
18acc31a28 | ||
|
|
d780fe007a | ||
|
|
f18bad36f3 | ||
|
|
d7ae81dce9 | ||
|
|
10c93a3246 | ||
|
|
50ff6c60ce | ||
|
|
cfe1f5be3b | ||
|
|
22e9106606 | ||
|
|
c6b67c3b86 | ||
|
|
77aacef528 | ||
|
|
9bab162d64 | ||
|
|
d0bbed568c | ||
|
|
41142e707a | ||
|
|
7f106e2deb | ||
|
|
5cdce25329 | ||
|
|
01dd9ca287 | ||
|
|
cb4c56679d | ||
|
|
d0ecaea174 | ||
|
|
a77cd3a682 | ||
|
|
c3efda87c3 | ||
|
|
afbaac0c0e | ||
|
|
c287aaaf58 | ||
|
|
6e09624120 | ||
|
|
1edc446940 | ||
|
|
f86f34bd90 | ||
|
|
ff0724f96a | ||
|
|
41d2bd6d91 | ||
|
|
8a75ee40bc | ||
|
|
75be20163d | ||
|
|
0013dd45c2 | ||
|
|
a3ee239900 | ||
|
|
5fc08e408e | ||
|
|
5098be30d1 | ||
|
|
b610ce3d7f | ||
|
|
1a5dcd17a9 | ||
|
|
3b1b1fa63e | ||
|
|
7880b14e47 | ||
|
|
5bbf5b71b7 | ||
|
|
cfec9606a4 | ||
|
|
e8d7cdc77a | ||
|
|
1323dc1571 | ||
|
|
8ba585679a | ||
|
|
848a78ed5f | ||
|
|
ea102130ac | ||
|
|
e656402ec8 | ||
|
|
8d092ae417 | ||
|
|
80f3c22582 | ||
|
|
7445b05e57 | ||
|
|
a79ac3e09e | ||
|
|
faae4ee145 | ||
|
|
6580005e77 | ||
|
|
942801ee4b | ||
|
|
2734c8e356 | ||
|
|
8de1bdffd4 | ||
|
|
0c2897ce37 | ||
|
|
77237023e3 | ||
|
|
fbc903fa6a | ||
|
|
fc573f075a | ||
|
|
fe9617ab56 | ||
|
|
1c364d4ff1 | ||
|
|
1e9e4f16b5 | ||
|
|
d54bd6473f | ||
|
|
6a6a5e7ded | ||
|
|
a5b07d22ae | ||
|
|
e20b6890d3 | ||
|
|
3d17326460 | ||
|
|
56339a5eca | ||
|
|
17bee91248 | ||
|
|
63d0de8ae3 | ||
|
|
00bb760451 | ||
|
|
9a8efc807a | ||
|
|
5f472435a7 | ||
|
|
9879a0eb25 | ||
|
|
f22fe19a4d | ||
|
|
03c19ef7c6 | ||
|
|
8ab83edc73 | ||
|
|
2540801140 | ||
|
|
601da0548b | ||
|
|
dd28fa233c | ||
|
|
4592b927ad | ||
|
|
e5ca519df0 | ||
|
|
5e347315e2 | ||
|
|
06c8e01eb5 | ||
|
|
603c49a395 | ||
|
|
990439985d | ||
|
|
bc8e0535d1 | ||
|
|
54136b71ef | ||
|
|
cf1eaebae1 | ||
|
|
5d18304f0f | ||
|
|
f89f220f3e | ||
|
|
c18a37eebc | ||
|
|
8bbf844bb6 | ||
|
|
d3a06d682f | ||
|
|
97607a0a91 | ||
|
|
4d530721dc | ||
|
|
0b691ca943 | ||
|
|
ed0f781851 | ||
|
|
613132035a | ||
|
|
b3dc5a336e | ||
|
|
a4d0cb65f0 | ||
|
|
29b9b17c67 | ||
|
|
8deecf3558 | ||
|
|
3a6a190ce1 | ||
|
|
1f0a4cbe76 | ||
|
|
fbc43216fb | ||
|
|
a4fcb6e763 | ||
|
|
76409b0d3b | ||
|
|
4aec6d40ad | ||
|
|
33a116d9fd | ||
|
|
45d9632590 | ||
|
|
0265370be2 | ||
|
|
fadd851e04 | ||
|
|
24f9d9682d | ||
|
|
3f9fc4a24b | ||
|
|
8ab607de6c | ||
|
|
a60bb34e64 | ||
|
|
c944897807 | ||
|
|
a6e22581d7 | ||
|
|
b15de7f747 | ||
|
|
f887affd6d | ||
|
|
e5e6b0f1e1 | ||
|
|
ad28dab542 | ||
|
|
42e3bf8f5b | ||
|
|
4154a50ed0 | ||
|
|
0ab822940e | ||
|
|
c1aa6db834 | ||
|
|
7feab6117b | ||
|
|
808fd4de1d | ||
|
|
63ada5bb99 | ||
|
|
52b7c3f2a1 | ||
|
|
571c2305aa | ||
|
|
79ecc100d3 | ||
|
|
51b4eb2556 | ||
|
|
3b18219e4e | ||
|
|
bcc0ba38b6 | ||
|
|
749cdc4eed | ||
|
|
19e469ebc4 | ||
|
|
6cd732dec5 | ||
|
|
95b8428c12 | ||
|
|
a7ed34b8e4 | ||
|
|
b839e9012d | ||
|
|
d378a484d6 |
@@ -0,0 +1,20 @@
|
||||
# cargo-audit configuration (read by `cargo audit` from the project root).
|
||||
#
|
||||
# The two entries below are accepted, documented exceptions -- both live only in
|
||||
# the wx-rs BUILD-dependency tree (reqwest 0.11, used by wx-rs's build.rs to
|
||||
# download the wxWidgets source on Windows/macOS). On Linux download_dist()
|
||||
# returns before ever constructing an HTTP client, and none of this is linked
|
||||
# into the runtime binary, which talks to miners over asic-rs's reqwest 0.13
|
||||
# (hyper 1.x / h2 0.4). The build tree is fully vendored and offline, so these
|
||||
# cannot be upgraded without re-vendoring a newer reqwest.
|
||||
#
|
||||
# Re-review whenever the vendored dependency set is refreshed.
|
||||
|
||||
[advisories]
|
||||
ignore = [
|
||||
# h2 0.3 unbounded empty DATA frames (DoS). Build-time only; our build-time
|
||||
# HTTP client only fetches a fixed HTTPS URL, and only on Windows/macOS.
|
||||
"RUSTSEC-2026-0258",
|
||||
# rustls-pemfile 1.0.4 unmaintained. Same build-only reqwest 0.11 subtree.
|
||||
"RUSTSEC-2025-0134",
|
||||
]
|
||||
@@ -0,0 +1,36 @@
|
||||
name: public-benchmark-pages
|
||||
|
||||
on:
|
||||
schedule:
|
||||
- cron: '*/15 * * * *'
|
||||
workflow_dispatch:
|
||||
|
||||
concurrency:
|
||||
group: alkane-toolkit-public-benchmark-pages
|
||||
cancel-in-progress: false
|
||||
|
||||
jobs:
|
||||
publish:
|
||||
runs-on: alkane-verify
|
||||
container:
|
||||
image: gitea.bluefla.re/blueflare/alkane-ci@sha256:4929b2979851f1f12fb8f92ef74236ae4c2ea735a41ead94a16af416a4f0a1a5
|
||||
permissions:
|
||||
contents: read
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Install SSH client
|
||||
run: |
|
||||
apt-get update -qq
|
||||
apt-get install -y -qq --no-install-recommends openssh-client
|
||||
- name: Render measured benchmark history
|
||||
env:
|
||||
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
||||
run: python3 ci/bench_history.py render
|
||||
- name: Publish public benchmark page
|
||||
env:
|
||||
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
||||
BENCH_PAGES_DEPLOY_KEY: ${{ secrets.BENCH_PAGES_DEPLOY_KEY }}
|
||||
run: python3 ci/publish-bench-pages.py toolkit
|
||||
- name: Clean generated page
|
||||
if: always()
|
||||
run: python3 ci/cleanup-generated.py
|
||||
@@ -0,0 +1,21 @@
|
||||
name: completed-ci-badge
|
||||
|
||||
on:
|
||||
workflow_run:
|
||||
workflows: [build-test-release, next-build]
|
||||
types: [completed]
|
||||
|
||||
jobs:
|
||||
publish:
|
||||
runs-on: alkane-verify
|
||||
container:
|
||||
image: gitea.bluefla.re/blueflare/alkane-ci@sha256:4929b2979851f1f12fb8f92ef74236ae4c2ea735a41ead94a16af416a4f0a1a5
|
||||
permissions:
|
||||
actions: read
|
||||
contents: write
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Publish last completed attempt
|
||||
env:
|
||||
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
||||
run: python3 ci/last-attempt-badge.py
|
||||
@@ -0,0 +1,168 @@
|
||||
name: next-build
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [next]
|
||||
|
||||
concurrency:
|
||||
group: alkane-toolkit-next
|
||||
cancel-in-progress: false
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
|
||||
jobs:
|
||||
# Windows build of the candidate (see release.yml's windows job); the
|
||||
# candidate job attaches its zip next to the Linux tarball.
|
||||
windows:
|
||||
runs-on: alkane-ci
|
||||
container:
|
||||
image: gitea.bluefla.re/blueflare/alkane-ci@sha256:4929b2979851f1f12fb8f92ef74236ae4c2ea735a41ead94a16af416a4f0a1a5
|
||||
env:
|
||||
CARGO_TARGET_DIR: target
|
||||
CARGO_BUILD_JOBS: 2
|
||||
WX_WINDOWS_PREFIX: /root/.cache/wx-3.3.3-mingw
|
||||
ALKANE_EMBEDDED_TOKEN: ${{ secrets.ALKANE_EMBEDDED_TOKEN }}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
- name: Select candidate version
|
||||
id: version
|
||||
run: bash ci/compute-rc-version.sh >> "$GITHUB_OUTPUT"
|
||||
- name: Install Windows toolchain
|
||||
if: steps.version.outputs.release == 'true'
|
||||
run: bash ci/windows-ci.sh toolchain
|
||||
- name: Configure bounded Rust compiler cache
|
||||
if: steps.version.outputs.release == 'true'
|
||||
run: bash ci/configure-rust-cache.sh
|
||||
- name: Compute wxMSW cache key
|
||||
if: steps.version.outputs.release == 'true'
|
||||
id: wx-key
|
||||
run: bash ci/wx-cache-key.sh windows
|
||||
- name: Check prebuilt wxMSW
|
||||
if: steps.version.outputs.release == 'true'
|
||||
id: wx-image
|
||||
run: bash ci/check-wx-image-prefix.sh windows
|
||||
- id: wx-cache
|
||||
if: steps.version.outputs.release == 'true' && steps.wx-image.outputs.available != 'true'
|
||||
uses: actions/cache/restore@v4
|
||||
continue-on-error: true
|
||||
with:
|
||||
path: /root/.cache/wx-3.3.3-mingw
|
||||
key: ${{ steps.wx-key.outputs.key }}
|
||||
- name: Build wxMSW
|
||||
if: steps.version.outputs.release == 'true'
|
||||
id: wx-build
|
||||
run: bash ci/ensure-wx-prefix.sh windows
|
||||
- name: Save vendored wxMSW build
|
||||
if: steps.version.outputs.release == 'true' && steps.wx-cache.outputs.cache-hit != 'true' && steps.wx-build.outputs.built == 'true'
|
||||
uses: actions/cache/save@v4
|
||||
continue-on-error: true
|
||||
with:
|
||||
path: /root/.cache/wx-3.3.3-mingw
|
||||
key: ${{ steps.wx-key.outputs.key }}
|
||||
- name: Build Windows candidate
|
||||
if: steps.version.outputs.release == 'true'
|
||||
run: bash ci/windows-ci.sh build
|
||||
- name: Sign Windows candidate (Azure Artifact Signing)
|
||||
if: steps.version.outputs.release == 'true'
|
||||
env:
|
||||
AZURE_SIGNING_PROFILE: ${{ vars.AZURE_SIGNING_PROFILE }}
|
||||
AZURE_SIGNING_TENANT_ID: ${{ secrets.AZURE_SIGNING_TENANT_ID }}
|
||||
AZURE_SIGNING_CLIENT_ID: ${{ secrets.AZURE_SIGNING_CLIENT_ID }}
|
||||
AZURE_SIGNING_CLIENT_SECRET: ${{ secrets.AZURE_SIGNING_CLIENT_SECRET }}
|
||||
run: bash ci/windows-ci.sh sign
|
||||
- name: Package Windows candidate
|
||||
if: steps.version.outputs.release == 'true'
|
||||
run: bash ci/windows-ci.sh package "${{ steps.version.outputs.version }}"
|
||||
- uses: https://github.com/ChristopherHX/gitea-upload-artifact@81f940d004763f986ba3582c007fd842dd5cb0d7
|
||||
if: steps.version.outputs.release == 'true'
|
||||
with:
|
||||
name: alkane-toolkit-windows
|
||||
path: dist/alkane-toolkit-v*-windows-x86_64.zip
|
||||
- name: Clean generated Toolkit build
|
||||
if: always()
|
||||
run: python3 ci/cleanup-generated.py
|
||||
|
||||
candidate:
|
||||
needs: windows
|
||||
runs-on: alkane-ci
|
||||
container:
|
||||
image: gitea.bluefla.re/blueflare/alkane-ci@sha256:4929b2979851f1f12fb8f92ef74236ae4c2ea735a41ead94a16af416a4f0a1a5
|
||||
env:
|
||||
CARGO_TARGET_DIR: target
|
||||
CARGO_BUILD_JOBS: 2
|
||||
WX_PREFIX: /root/.cache/wx-3.3.3-linux
|
||||
WX_RS_WXCONFIG: /root/.cache/wx-3.3.3-linux/bin/wx-config
|
||||
ALKANE_EMBEDDED_TOKEN: ${{ secrets.ALKANE_EMBEDDED_TOKEN }}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
- name: Check runner image pins
|
||||
run: python3 ci/check-runner-image.py
|
||||
- name: Install build dependencies
|
||||
run: bash ci/ensure-build-deps.sh base
|
||||
- name: Configure bounded Rust compiler cache
|
||||
run: bash ci/configure-rust-cache.sh
|
||||
- name: Compute wxWidgets cache key
|
||||
id: wx-key
|
||||
run: bash ci/wx-cache-key.sh
|
||||
- name: Check prebuilt wxWidgets
|
||||
id: wx-image
|
||||
run: bash ci/check-wx-image-prefix.sh
|
||||
- id: wx-cache
|
||||
if: steps.wx-image.outputs.available != 'true'
|
||||
uses: actions/cache/restore@v4
|
||||
continue-on-error: true
|
||||
with:
|
||||
path: /root/.cache/wx-3.3.3-linux
|
||||
key: ${{ steps.wx-key.outputs.key }}
|
||||
- name: Build wxWidgets
|
||||
id: wx-build
|
||||
run: bash ci/ensure-wx-prefix.sh
|
||||
- name: Save vendored wxWidgets build
|
||||
if: steps.wx-cache.outputs.cache-hit != 'true' && steps.wx-build.outputs.built == 'true'
|
||||
uses: actions/cache/save@v4
|
||||
continue-on-error: true
|
||||
with:
|
||||
path: /root/.cache/wx-3.3.3-linux
|
||||
key: ${{ steps.wx-key.outputs.key }}
|
||||
- name: Build release candidate
|
||||
run: cargo build --release -p alkane-gui -p alkane-artifacts -p alkane-flash -p alkane-cli --offline
|
||||
- name: Select candidate version
|
||||
id: version
|
||||
run: bash ci/compute-rc-version.sh >> "$GITHUB_OUTPUT"
|
||||
- name: Package candidate
|
||||
if: steps.version.outputs.release == 'true'
|
||||
run: |
|
||||
mkdir -p dist
|
||||
cp target/release/alkane-toolkit dist/
|
||||
tar -C dist -czf "dist/alkane-toolkit-${{ steps.version.outputs.version }}-linux-x86_64.tar.gz" alkane-toolkit
|
||||
- name: Fetch Windows candidate
|
||||
if: steps.version.outputs.release == 'true'
|
||||
uses: https://github.com/ChristopherHX/gitea-download-artifact@75635f32b4c1c41c4b3d64e8f85210112ed4c9c7
|
||||
with:
|
||||
name: alkane-toolkit-windows
|
||||
path: dist/windows
|
||||
- name: Create private prerelease
|
||||
if: steps.version.outputs.release == 'true'
|
||||
env:
|
||||
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
V="${{ steps.version.outputs.version }}"
|
||||
WIN="dist/windows/alkane-toolkit-v${V}-windows-x86_64.zip"
|
||||
[ -s "$WIN" ] || { echo "Windows candidate for ${V} is missing" >&2; exit 1; }
|
||||
python3 ci/release-rc.py "${{ steps.version.outputs.tag }}" true \
|
||||
"dist/alkane-toolkit-${V}-linux-x86_64.tar.gz" "$WIN"
|
||||
- name: Rust compiler cache statistics
|
||||
if: always()
|
||||
run: |
|
||||
if [ -n "${SCCACHE_DIR:-}" ]; then
|
||||
sccache --show-stats
|
||||
fi
|
||||
- name: Clean generated Toolkit build
|
||||
if: always()
|
||||
run: python3 ci/cleanup-generated.py
|
||||
@@ -0,0 +1,47 @@
|
||||
name: pages
|
||||
|
||||
# The project site, https://blueflare.gitea.bluefla.re/alkane-toolkit/ :
|
||||
# overview, user manual (docs/manual), code-quality dashboard (from the
|
||||
# quality record main CI publishes with the badges) and benchmark history.
|
||||
# Rebuilt after every main build-test-release run and published to this
|
||||
# repository's gh-pages branch.
|
||||
on:
|
||||
workflow_run:
|
||||
workflows: [build-test-release]
|
||||
types: [completed]
|
||||
workflow_dispatch:
|
||||
|
||||
concurrency:
|
||||
group: alkane-toolkit-pages
|
||||
cancel-in-progress: false
|
||||
|
||||
jobs:
|
||||
publish:
|
||||
runs-on: alkane-verify
|
||||
container:
|
||||
image: gitea.bluefla.re/blueflare/alkane-ci@sha256:4929b2979851f1f12fb8f92ef74236ae4c2ea735a41ead94a16af416a4f0a1a5
|
||||
permissions:
|
||||
contents: write
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Install page tools
|
||||
run: bash ci/ensure-build-deps.sh pages
|
||||
- name: Render measured benchmark history
|
||||
env:
|
||||
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
||||
run: python3 ci/bench_history.py render
|
||||
- name: Fetch measured quality record
|
||||
env:
|
||||
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
||||
run: python3 ci/fetch-quality.py quality.json
|
||||
- name: Build site
|
||||
run: |
|
||||
python3 ci/build-pages.py --out site --quality quality.json \
|
||||
--bench bench-site/index.html --sha "$GITHUB_SHA"
|
||||
- name: Publish site
|
||||
env:
|
||||
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
||||
run: python3 ci/publish-pages.py site
|
||||
- name: Clean generated site
|
||||
if: always()
|
||||
run: python3 ci/cleanup-generated.py
|
||||
@@ -1,16 +1,36 @@
|
||||
name: build-test-release
|
||||
|
||||
# Runs on every merge/push to main: build the GUI, run tests + a microbenchmark,
|
||||
# and (when the change warrants it) cut an X.Y.Z release with the binary attached.
|
||||
# Runs on every merge/push to main: build the GUI for Linux and Windows, run the
|
||||
# tests + a microbenchmark, and (when the change warrants it) cut an X.Y.Z
|
||||
# release with both binaries attached.
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
|
||||
concurrency:
|
||||
group: alkane-toolkit-main
|
||||
cancel-in-progress: false
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
actions: write
|
||||
|
||||
jobs:
|
||||
release:
|
||||
# Windows build of the same commit: cross-compiled with mingw-w64 against the
|
||||
# vendored wxMSW, unit-tested and UI-self-tested under Wine, packaged as a
|
||||
# zip. The release job below waits for it and attaches the zip next to the
|
||||
# Linux tarball, so a release never ships one platform without the other.
|
||||
windows:
|
||||
runs-on: alkane-ci
|
||||
container:
|
||||
image: gitea.bluefla.re/blueflare/alkane-ci@sha256:4929b2979851f1f12fb8f92ef74236ae4c2ea735a41ead94a16af416a4f0a1a5
|
||||
env:
|
||||
WX_PREFIX: /root/.cache/wx-3.3.3-linux
|
||||
CARGO_TARGET_DIR: target
|
||||
CARGO_BUILD_JOBS: 2
|
||||
WX_WINDOWS_PREFIX: /root/.cache/wx-3.3.3-mingw
|
||||
# Read-only token baked into the GUI so the release picker can list the
|
||||
# private alkane-os releases with no user setup (option_env!).
|
||||
ALKANE_EMBEDDED_TOKEN: ${{ secrets.ALKANE_EMBEDDED_TOKEN }}
|
||||
steps:
|
||||
- name: Checkout (full history for tags)
|
||||
uses: actions/checkout@v4
|
||||
@@ -21,33 +41,181 @@ jobs:
|
||||
id: ver
|
||||
run: bash ci/compute-version.sh | tee -a "$GITHUB_OUTPUT"
|
||||
|
||||
- name: Test version logic
|
||||
- name: Install Windows toolchain
|
||||
run: bash ci/windows-ci.sh toolchain
|
||||
|
||||
- name: Configure bounded Rust compiler cache
|
||||
run: bash ci/configure-rust-cache.sh
|
||||
|
||||
- name: Compute wxMSW cache key
|
||||
id: wx-key
|
||||
run: bash ci/wx-cache-key.sh windows
|
||||
|
||||
- name: Check prebuilt wxMSW
|
||||
id: wx-image
|
||||
run: bash ci/check-wx-image-prefix.sh windows
|
||||
|
||||
- name: Cache vendored wxMSW build
|
||||
if: steps.wx-image.outputs.available != 'true'
|
||||
id: wx-cache
|
||||
uses: actions/cache/restore@v4
|
||||
continue-on-error: true
|
||||
with:
|
||||
path: /root/.cache/wx-3.3.3-mingw
|
||||
key: ${{ steps.wx-key.outputs.key }}
|
||||
|
||||
- name: Build wxMSW (once, then cached)
|
||||
id: wx-build
|
||||
run: bash ci/ensure-wx-prefix.sh windows
|
||||
|
||||
- name: Save vendored wxMSW build
|
||||
if: steps.wx-cache.outputs.cache-hit != 'true' && steps.wx-build.outputs.built == 'true'
|
||||
uses: actions/cache/save@v4
|
||||
continue-on-error: true
|
||||
with:
|
||||
path: /root/.cache/wx-3.3.3-mingw
|
||||
key: ${{ steps.wx-key.outputs.key }}
|
||||
|
||||
- name: Clippy (Windows target, warnings are errors)
|
||||
run: bash ci/windows-ci.sh clippy
|
||||
|
||||
- name: Set crate version to ${{ steps.ver.outputs.version }}
|
||||
if: steps.ver.outputs.release == 'true'
|
||||
run: |
|
||||
sed -i "0,/^version = \".*\"/s//version = \"${{ steps.ver.outputs.version }}\"/" Cargo.toml
|
||||
grep -m1 '^version' Cargo.toml
|
||||
|
||||
- name: Build release (Windows)
|
||||
run: bash ci/windows-ci.sh build
|
||||
|
||||
- name: Unit tests (Windows build, under Wine)
|
||||
run: bash ci/windows-ci.sh test
|
||||
|
||||
- name: Headless UI self-test (Windows build, under Wine)
|
||||
run: bash ci/windows-ci.sh selftest
|
||||
|
||||
- name: Sign Windows executable (Azure Artifact Signing)
|
||||
env:
|
||||
AZURE_SIGNING_PROFILE: ${{ vars.AZURE_SIGNING_PROFILE }}
|
||||
AZURE_SIGNING_TENANT_ID: ${{ secrets.AZURE_SIGNING_TENANT_ID }}
|
||||
AZURE_SIGNING_CLIENT_ID: ${{ secrets.AZURE_SIGNING_CLIENT_ID }}
|
||||
AZURE_SIGNING_CLIENT_SECRET: ${{ secrets.AZURE_SIGNING_CLIENT_SECRET }}
|
||||
run: bash ci/windows-ci.sh sign
|
||||
|
||||
- name: Package Windows artifact
|
||||
if: steps.ver.outputs.release == 'true'
|
||||
run: bash ci/windows-ci.sh package "${{ steps.ver.outputs.version }}"
|
||||
|
||||
- name: Bundle Windows test executables
|
||||
run: bash ci/windows-ci.sh test-bundle
|
||||
|
||||
- uses: https://github.com/ChristopherHX/gitea-upload-artifact@81f940d004763f986ba3582c007fd842dd5cb0d7
|
||||
if: steps.ver.outputs.release == 'true'
|
||||
with:
|
||||
name: alkane-toolkit-windows
|
||||
path: dist/alkane-toolkit-v*-windows-x86_64.zip
|
||||
|
||||
# Consumed by .gitea/workflows/windows-native.yml on the Windows VM runner.
|
||||
- uses: https://github.com/ChristopherHX/gitea-upload-artifact@81f940d004763f986ba3582c007fd842dd5cb0d7
|
||||
with:
|
||||
name: alkane-toolkit-windows-tests
|
||||
path: |
|
||||
dist/alkane-toolkit-windows-tests.zip
|
||||
target/x86_64-pc-windows-gnu/release/alkane-toolkit.exe
|
||||
|
||||
- name: Clean generated Toolkit build
|
||||
if: always()
|
||||
run: python3 ci/cleanup-generated.py
|
||||
|
||||
release:
|
||||
needs: windows
|
||||
runs-on: alkane-ci
|
||||
container:
|
||||
image: gitea.bluefla.re/blueflare/alkane-ci@sha256:4929b2979851f1f12fb8f92ef74236ae4c2ea735a41ead94a16af416a4f0a1a5
|
||||
env:
|
||||
CARGO_TARGET_DIR: target
|
||||
CARGO_BUILD_JOBS: 2
|
||||
WX_PREFIX: /root/.cache/wx-3.3.3-linux
|
||||
# Set once for the whole job so build/clippy/test/bench steps don't each
|
||||
# re-export it (round 18 #12).
|
||||
WX_RS_WXCONFIG: /root/.cache/wx-3.3.3-linux/bin/wx-config
|
||||
# Read-only token baked into the GUI so the release picker can list the
|
||||
# private alkane-os releases with no user setup (option_env!).
|
||||
ALKANE_EMBEDDED_TOKEN: ${{ secrets.ALKANE_EMBEDDED_TOKEN }}
|
||||
steps:
|
||||
- name: Checkout (full history for tags)
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Check runner image pins
|
||||
run: python3 ci/check-runner-image.py
|
||||
|
||||
- name: Check wx-rs patch queue reproduces the patched tree
|
||||
run: python3 scripts/wx_rs_patches.py check
|
||||
|
||||
- name: Check first-party Rust formatting
|
||||
run: |
|
||||
rustup component add rustfmt >/dev/null
|
||||
cargo fmt -p alkane-gui -p alkane-artifacts -p alkane-flash -p alkane-cli -p alkane-access -p alkane-core -- --check
|
||||
|
||||
|
||||
- name: Compute version
|
||||
id: ver
|
||||
run: bash ci/compute-version.sh | tee -a "$GITHUB_OUTPUT"
|
||||
|
||||
- name: Test version logic
|
||||
run: bash ci/test-compute-version.sh
|
||||
|
||||
- name: Last-attempt badge checks
|
||||
run: python3 ci/test-last-attempt-badge.py
|
||||
|
||||
- name: Benchmark history checks
|
||||
run: python3 ci/test-bench-history.py
|
||||
|
||||
- name: Benchmark suite checks
|
||||
run: python3 ci/test-bench-badge.py
|
||||
|
||||
- name: Install build dependencies
|
||||
if: steps.ver.outputs.release == 'true'
|
||||
run: |
|
||||
apt-get update -qq
|
||||
apt-get install -y -qq --no-install-recommends \
|
||||
libssl-dev pkg-config ca-certificates >/dev/null
|
||||
echo "libssl-dev + pkg-config installed (openssl-sys needs them)"
|
||||
run: bash ci/ensure-build-deps.sh all
|
||||
|
||||
- name: Configure bounded Rust compiler cache
|
||||
run: bash ci/configure-rust-cache.sh
|
||||
|
||||
- name: Compute wxWidgets cache key
|
||||
id: wx-key
|
||||
run: bash ci/wx-cache-key.sh
|
||||
|
||||
- name: Check prebuilt wxWidgets
|
||||
id: wx-image
|
||||
run: bash ci/check-wx-image-prefix.sh
|
||||
|
||||
- name: Cache vendored wxWidgets build
|
||||
if: steps.ver.outputs.release == 'true'
|
||||
uses: actions/cache@v4
|
||||
if: steps.wx-image.outputs.available != 'true'
|
||||
id: wx-cache
|
||||
uses: actions/cache/restore@v4
|
||||
continue-on-error: true
|
||||
with:
|
||||
path: /root/.cache/wx-3.3.3-linux
|
||||
key: wx-3.3.3-${{ runner.os }}-${{ hashFiles('thirdparty/wx-rs/VERSION') }}
|
||||
key: ${{ steps.wx-key.outputs.key }}
|
||||
|
||||
- name: Build wxWidgets (once, then cached)
|
||||
if: steps.ver.outputs.release == 'true'
|
||||
id: wx-build
|
||||
run: bash ci/ensure-wx-prefix.sh
|
||||
|
||||
- name: Save vendored wxWidgets build
|
||||
if: steps.wx-cache.outputs.cache-hit != 'true' && steps.wx-build.outputs.built == 'true'
|
||||
uses: actions/cache/save@v4
|
||||
continue-on-error: true
|
||||
with:
|
||||
path: /root/.cache/wx-3.3.3-linux
|
||||
key: ${{ steps.wx-key.outputs.key }}
|
||||
|
||||
- name: Clippy (our crate, warnings are errors)
|
||||
run: |
|
||||
if [ ! -x "$WX_PREFIX/bin/wx-config" ]; then
|
||||
bash scripts/build-wx-linux.sh
|
||||
else
|
||||
echo "wx prefix restored from cache"
|
||||
fi
|
||||
rustup component add clippy >/dev/null 2>&1 || true
|
||||
cargo clippy -p alkane-gui --no-deps --release --bins --offline -- -D warnings
|
||||
cargo clippy --workspace --exclude alkane-gui --exclude wx-rs --all-targets --offline -- -D warnings
|
||||
|
||||
- name: Set crate version to ${{ steps.ver.outputs.version }}
|
||||
if: steps.ver.outputs.release == 'true'
|
||||
@@ -56,31 +224,107 @@ jobs:
|
||||
grep -m1 '^version' Cargo.toml
|
||||
|
||||
- name: Build release
|
||||
if: steps.ver.outputs.release == 'true'
|
||||
run: |
|
||||
export WX_RS_WXCONFIG="$WX_PREFIX/bin/wx-config"
|
||||
cargo build --release -p alkane-gui --offline
|
||||
|
||||
- name: Unit tests
|
||||
if: steps.ver.outputs.release == 'true'
|
||||
run: |
|
||||
export WX_RS_WXCONFIG="$WX_PREFIX/bin/wx-config"
|
||||
cargo test -p alkane-gui --offline
|
||||
set -o pipefail
|
||||
cargo test -p alkane-gui --offline 2>&1 | tee tests-gui.txt
|
||||
cargo test --workspace --exclude alkane-gui --exclude wx-rs --offline 2>&1 | tee tests-core.txt
|
||||
cat tests-gui.txt tests-core.txt > tests-current.txt
|
||||
|
||||
- name: Load previous main benchmark measurement
|
||||
env:
|
||||
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
||||
run: python3 ci/bench_history.py prepare
|
||||
|
||||
- name: Microbenchmarks
|
||||
if: steps.ver.outputs.release == 'true'
|
||||
run: |
|
||||
export WX_RS_WXCONFIG="$WX_PREFIX/bin/wx-config"
|
||||
cargo test -p alkane-gui --release --offline -- --ignored --nocapture bench_
|
||||
# Require every declared benchmark before publishing history or a release.
|
||||
set -o pipefail
|
||||
cargo test -p alkane-gui -p alkane-artifacts --release --offline -- --ignored --nocapture bench_ --test-threads=1 \
|
||||
| tee bench-current.txt
|
||||
python3 ci/bench-badge.py --current bench-current.txt \
|
||||
--baseline badges/bench-baseline.json --no-write
|
||||
|
||||
- name: Headless UI self-test
|
||||
if: steps.ver.outputs.release == 'true'
|
||||
run: |
|
||||
apt-get update -qq && apt-get install -y -qq --no-install-recommends \
|
||||
xvfb xauth dbus-x11 fonts-dejavu-core >/dev/null
|
||||
BIN=target/release/alkane-toolkit
|
||||
xvfb-run -a env GDK_BACKEND=x11 ALKANE_SELFTEST=1 "$BIN"
|
||||
|
||||
- name: Dependency audit (best-effort)
|
||||
continue-on-error: true
|
||||
run: |
|
||||
if command -v cargo-audit >/dev/null 2>&1; then
|
||||
cargo audit || echo "cargo audit reported findings (accepted ones in .cargo/audit.toml)"
|
||||
else
|
||||
echo "cargo-audit not installed on this runner; skipping"
|
||||
fi
|
||||
|
||||
- name: Rust compiler cache statistics
|
||||
if: always()
|
||||
run: |
|
||||
if [ -n "${SCCACHE_DIR:-}" ]; then
|
||||
sccache --show-stats
|
||||
fi
|
||||
|
||||
# Merge coverage from unit tests and the headless UI self-test. A failed
|
||||
# measurement must not publish a stale badge or create a release.
|
||||
- name: Coverage
|
||||
id: cov
|
||||
run: |
|
||||
set -euo pipefail
|
||||
rustup component add llvm-tools-preview >/dev/null 2>&1 || true
|
||||
[ "$(cargo llvm-cov --version)" = 'cargo-llvm-cov 0.8.7' ]
|
||||
# Keep the whole table: gen-badges turns it into the per-crate and
|
||||
# per-file breakdown on the Pages quality dashboard.
|
||||
bash ci/coverage.sh --summary-only | tee coverage-summary.txt
|
||||
PCT=$(awk '/^TOTAL/{gsub(/%/,"",$10); print $10}' coverage-summary.txt)
|
||||
[ -n "$PCT" ] || { echo "coverage report produced no TOTAL line" >&2; exit 1; }
|
||||
echo "pct=${PCT}" >> "$GITHUB_OUTPUT"
|
||||
echo "line coverage (tests + self-test): ${PCT}%"
|
||||
|
||||
- name: Microbenchmark coverage
|
||||
id: benchcov
|
||||
run: |
|
||||
set -euo pipefail
|
||||
command -v cargo-llvm-cov >/dev/null
|
||||
PCT=$(bash ci/bench-coverage.sh --summary-only \
|
||||
| awk '/^TOTAL/{gsub(/%/,"",$10); print $10}')
|
||||
[ -n "$PCT" ] || { echo "microbenchmark coverage unavailable" >&2; exit 1; }
|
||||
echo "pct=${PCT}" >> "$GITHUB_OUTPUT"
|
||||
echo "microbenchmark line coverage: ${PCT}%"
|
||||
|
||||
- name: Generate measured badges
|
||||
run: |
|
||||
set -euo pipefail
|
||||
PASS=$(grep -E '^test result: ' tests-current.txt \
|
||||
| grep -oE '[0-9]+ passed' | grep -oE '[0-9]+' \
|
||||
| awk '{s+=$1} END{print s+0}')
|
||||
[ "$PASS" -gt 0 ] || { echo "no passing tests counted" >&2; exit 1; }
|
||||
BENCH_DELTA=$(python3 ci/bench-badge.py --current bench-current.txt \
|
||||
--baseline badges/bench-baseline.json --no-write)
|
||||
python3 ci/gen-badges.py --tests "$PASS" \
|
||||
--coverage "${{ steps.cov.outputs.pct }}" \
|
||||
--bench-coverage "${{ steps.benchcov.outputs.pct }}" \
|
||||
--bench-delta "$BENCH_DELTA" --clippy clean \
|
||||
--coverage-summary coverage-summary.txt
|
||||
|
||||
- name: Publish measured badges
|
||||
env:
|
||||
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
||||
run: python3 ci/publish-metrics.py
|
||||
|
||||
- name: Record private benchmark history
|
||||
env:
|
||||
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
||||
run: python3 ci/bench_history.py publish
|
||||
|
||||
- uses: https://github.com/ChristopherHX/gitea-upload-artifact@81f940d004763f986ba3582c007fd842dd5cb0d7
|
||||
with:
|
||||
name: alkane-toolkit-benchmark-site-preview
|
||||
path: bench-site/index.html
|
||||
- name: Package artifact
|
||||
if: steps.ver.outputs.release == 'true'
|
||||
id: pkg
|
||||
@@ -102,25 +346,34 @@ jobs:
|
||||
echo "asset=dist/${NAME}.tar.gz" >> "$GITHUB_OUTPUT"
|
||||
echo "assetname=${NAME}.tar.gz" >> "$GITHUB_OUTPUT"
|
||||
|
||||
- name: Fetch Windows artifact
|
||||
if: steps.ver.outputs.release == 'true'
|
||||
uses: https://github.com/ChristopherHX/gitea-download-artifact@75635f32b4c1c41c4b3d64e8f85210112ed4c9c7
|
||||
with:
|
||||
name: alkane-toolkit-windows
|
||||
path: dist/windows
|
||||
|
||||
- name: Locate Windows artifact
|
||||
if: steps.ver.outputs.release == 'true'
|
||||
id: winpkg
|
||||
run: |
|
||||
set -euo pipefail
|
||||
V="${{ steps.ver.outputs.version }}"
|
||||
ZIP="dist/windows/alkane-toolkit-v${V}-windows-x86_64.zip"
|
||||
[ -s "$ZIP" ] || { echo "Windows artifact for v${V} is missing" >&2; ls -lR dist/windows >&2; exit 1; }
|
||||
echo "asset=$ZIP" >> "$GITHUB_OUTPUT"
|
||||
|
||||
- name: Create Gitea release + upload artifact
|
||||
if: steps.ver.outputs.release == 'true'
|
||||
env:
|
||||
TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
||||
run: |
|
||||
TAG="${{ steps.ver.outputs.tag }}"
|
||||
API="${GITHUB_API_URL:-https://gitea.bluefla.re/api/v1}"
|
||||
REPO="${GITHUB_REPOSITORY}"
|
||||
body="Automated ${{ steps.ver.outputs.bump }} release from ${GITHUB_SHA}."
|
||||
rel=$(curl -sS -X POST -H "Authorization: token ${TOKEN}" -H "Content-Type: application/json" \
|
||||
"${API}/repos/${REPO}/releases" \
|
||||
-d "{\"tag_name\":\"${TAG}\",\"target_commitish\":\"${GITHUB_SHA}\",\"name\":\"${TAG}\",\"body\":\"${body}\"}")
|
||||
id=$(echo "$rel" | grep -oE '"id":[0-9]+' | head -1 | cut -d: -f2)
|
||||
if [ -z "$id" ]; then echo "release create failed: $rel"; exit 1; fi
|
||||
curl -sS -X POST -H "Authorization: token ${TOKEN}" -H "Content-Type: application/gzip" \
|
||||
"${API}/repos/${REPO}/releases/${id}/assets?name=${{ steps.pkg.outputs.assetname }}" \
|
||||
--data-binary @"${{ steps.pkg.outputs.asset }}" | grep -oE '"browser_download_url":"[^"]+"' || true
|
||||
echo "Released ${TAG}"
|
||||
python3 ci/release-rc.py "${{ steps.ver.outputs.tag }}" false \
|
||||
"${{ steps.pkg.outputs.asset }}" "${{ steps.winpkg.outputs.asset }}"
|
||||
|
||||
- name: No release needed
|
||||
if: steps.ver.outputs.release != 'true'
|
||||
run: echo "No backend/UI change since ${{ steps.ver.outputs.last_tag }} - skipping release."
|
||||
- name: Clean generated Toolkit build
|
||||
if: always()
|
||||
run: python3 ci/cleanup-generated.py
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
name: source
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
branches: [next]
|
||||
workflow_dispatch:
|
||||
|
||||
concurrency:
|
||||
group: alkane-toolkit-source-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
check:
|
||||
runs-on: alkane-fast
|
||||
container:
|
||||
image: gitea.bluefla.re/blueflare/alkane-ci@sha256:4929b2979851f1f12fb8f92ef74236ae4c2ea735a41ead94a16af416a4f0a1a5
|
||||
env:
|
||||
CARGO_BUILD_JOBS: 2
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Check runner image pins
|
||||
run: python3 ci/check-runner-image.py
|
||||
- name: Source checks
|
||||
run: cargo test --workspace --exclude alkane-gui --exclude wx-rs --offline
|
||||
- name: Clean generated Toolkit build
|
||||
if: always()
|
||||
run: python3 ci/cleanup-generated.py
|
||||
@@ -0,0 +1,267 @@
|
||||
name: verify
|
||||
|
||||
# Full quality gate for pull requests to main.
|
||||
on:
|
||||
pull_request:
|
||||
branches: [main]
|
||||
|
||||
concurrency:
|
||||
group: alkane-toolkit-verify-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
verify:
|
||||
runs-on: alkane-verify
|
||||
container:
|
||||
image: gitea.bluefla.re/blueflare/alkane-ci@sha256:4929b2979851f1f12fb8f92ef74236ae4c2ea735a41ead94a16af416a4f0a1a5
|
||||
outputs:
|
||||
surface: ${{ steps.surface.outputs.surface }}
|
||||
env:
|
||||
CARGO_TARGET_DIR: target
|
||||
CARGO_BUILD_JOBS: 2
|
||||
WX_PREFIX: /root/.cache/wx-3.3.3-linux
|
||||
WX_RS_WXCONFIG: /root/.cache/wx-3.3.3-linux/bin/wx-config
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Check runner image pins
|
||||
run: python3 ci/check-runner-image.py
|
||||
|
||||
- name: Check wx-rs patch queue reproduces the patched tree
|
||||
run: python3 scripts/wx_rs_patches.py check
|
||||
|
||||
- name: Detect changed surface
|
||||
id: surface
|
||||
run: python3 ci/pr-surface.py >> "$GITHUB_OUTPUT"
|
||||
|
||||
- name: Check documentation change
|
||||
if: steps.surface.outputs.surface == 'documentation'
|
||||
run: |
|
||||
base="$(git merge-base HEAD "origin/$GITHUB_BASE_REF")"
|
||||
git diff --check "$base" HEAD
|
||||
# Vendored upstream trees carry their own test-fixture keys (openssl,
|
||||
# rustls-pemfile, aws-lc); only first-party sources are checked.
|
||||
if git grep -nIE '^[[:space:]]*-----BEGIN (RSA |EC |OPENSSH )?PRIVATE KEY-----[[:space:]]*$' -- ':!vendor' ':!thirdparty'; then
|
||||
echo "private key material found in tracked source" >&2
|
||||
exit 1
|
||||
else
|
||||
status=$?
|
||||
[ "$status" -eq 1 ]
|
||||
fi
|
||||
|
||||
- name: Check CI change surface
|
||||
if: steps.surface.outputs.surface != 'documentation'
|
||||
run: python3 ci/test-pr-surface.py
|
||||
|
||||
- name: Check first-party Rust formatting
|
||||
if: steps.surface.outputs.surface == 'headless' || steps.surface.outputs.surface == 'full'
|
||||
run: |
|
||||
rustup component add rustfmt >/dev/null
|
||||
cargo fmt -p alkane-gui -p alkane-artifacts -p alkane-flash -p alkane-cli -p alkane-access -p alkane-core -- --check
|
||||
|
||||
|
||||
- name: Check workflow-only change
|
||||
if: steps.surface.outputs.surface == 'workflow'
|
||||
run: |
|
||||
python3 - <<'PY'
|
||||
from pathlib import Path
|
||||
import yaml
|
||||
for path in Path('.gitea/workflows').glob('*.yml'):
|
||||
yaml.load(path.read_text(), Loader=yaml.BaseLoader)
|
||||
PY
|
||||
python3 ci/check-runner-image.py
|
||||
for script in ci/*.sh; do bash -n "$script"; done
|
||||
base="$(git merge-base HEAD "origin/$GITHUB_BASE_REF")"
|
||||
git diff --check "$base" HEAD
|
||||
# Vendored upstream trees carry their own test-fixture keys (openssl,
|
||||
# rustls-pemfile, aws-lc); only first-party sources are checked.
|
||||
if git grep -nIE '^[[:space:]]*-----BEGIN (RSA |EC |OPENSSH )?PRIVATE KEY-----[[:space:]]*$' -- ':!vendor' ':!thirdparty'; then
|
||||
echo "private key material found in tracked source" >&2
|
||||
exit 1
|
||||
else
|
||||
status=$?
|
||||
[ "$status" -eq 1 ]
|
||||
fi
|
||||
|
||||
- name: Install build dependencies
|
||||
if: steps.surface.outputs.surface == 'headless' || steps.surface.outputs.surface == 'full'
|
||||
run: bash ci/ensure-build-deps.sh base
|
||||
|
||||
- name: Configure bounded Rust compiler cache
|
||||
if: steps.surface.outputs.surface == 'headless' || steps.surface.outputs.surface == 'full'
|
||||
run: bash ci/configure-rust-cache.sh
|
||||
|
||||
- name: Compute wxWidgets cache key
|
||||
id: wx-key
|
||||
if: steps.surface.outputs.surface == 'full'
|
||||
run: bash ci/wx-cache-key.sh
|
||||
|
||||
- name: Check prebuilt wxWidgets
|
||||
id: wx-image
|
||||
if: steps.surface.outputs.surface == 'full'
|
||||
run: bash ci/check-wx-image-prefix.sh
|
||||
|
||||
- name: Cache vendored wxWidgets build
|
||||
if: steps.surface.outputs.surface == 'full' && steps.wx-image.outputs.available != 'true'
|
||||
id: wx-cache
|
||||
uses: actions/cache/restore@v4
|
||||
continue-on-error: true
|
||||
with:
|
||||
path: /root/.cache/wx-3.3.3-linux
|
||||
key: ${{ steps.wx-key.outputs.key }}
|
||||
|
||||
- name: Build wxWidgets (once, then cached)
|
||||
if: steps.surface.outputs.surface == 'full'
|
||||
id: wx-build
|
||||
run: bash ci/ensure-wx-prefix.sh
|
||||
|
||||
- name: Save vendored wxWidgets build
|
||||
if: steps.surface.outputs.surface == 'full' && steps.wx-cache.outputs.cache-hit != 'true' && steps.wx-build.outputs.built == 'true'
|
||||
uses: actions/cache/save@v4
|
||||
continue-on-error: true
|
||||
with:
|
||||
path: /root/.cache/wx-3.3.3-linux
|
||||
key: ${{ steps.wx-key.outputs.key }}
|
||||
|
||||
- name: Clippy (our crate, warnings are errors)
|
||||
if: steps.surface.outputs.surface == 'full'
|
||||
run: |
|
||||
rustup component add clippy >/dev/null 2>&1 || true
|
||||
cargo clippy -p alkane-gui --no-deps --release --bins --offline -- -D warnings
|
||||
cargo clippy --workspace --exclude alkane-gui --exclude wx-rs --all-targets --offline -- -D warnings
|
||||
|
||||
- name: Headless crate checks
|
||||
if: steps.surface.outputs.surface == 'headless'
|
||||
env:
|
||||
PACKAGES: ${{ steps.surface.outputs.packages }}
|
||||
run: |
|
||||
rustup component add clippy >/dev/null 2>&1 || true
|
||||
args=()
|
||||
for package in $PACKAGES; do
|
||||
case "$package" in
|
||||
alkane-access|alkane-artifacts|alkane-cli|alkane-flash) args+=(-p "$package") ;;
|
||||
*) echo "unrecognized crate: $package" >&2; exit 1 ;;
|
||||
esac
|
||||
done
|
||||
[ "${#args[@]}" -gt 0 ]
|
||||
cargo clippy "${args[@]}" --all-targets --offline -- -D warnings
|
||||
cargo test "${args[@]}" --offline
|
||||
|
||||
- name: Build
|
||||
if: steps.surface.outputs.surface == 'full'
|
||||
run: cargo build --release -p alkane-gui --offline
|
||||
|
||||
- name: Unit tests
|
||||
if: steps.surface.outputs.surface == 'full'
|
||||
run: |
|
||||
cargo test -p alkane-gui --offline
|
||||
cargo test --workspace --exclude alkane-gui --exclude wx-rs --offline
|
||||
python3 tools/report-relay/test_relay.py
|
||||
|
||||
- name: Microbenchmarks
|
||||
if: steps.surface.outputs.surface == 'full'
|
||||
run: cargo test -p alkane-gui -p alkane-artifacts --release --offline -- --ignored --nocapture bench_ --test-threads=1
|
||||
|
||||
- name: Headless UI self-test
|
||||
if: steps.surface.outputs.surface == 'full'
|
||||
run: |
|
||||
bash ci/ensure-build-deps.sh ui
|
||||
BIN=target/release/alkane-toolkit
|
||||
xvfb-run -a env GDK_BACKEND=x11 ALKANE_SELFTEST=1 "$BIN"
|
||||
|
||||
# Advisory audit is best-effort: it needs the RustSec DB over the network,
|
||||
# which the vendored/offline build otherwise never touches, so a locked-down
|
||||
# runner must not fail the gate on it. Accepted advisories live in
|
||||
# .cargo/audit.toml.
|
||||
- name: Dependency audit (best-effort)
|
||||
if: steps.surface.outputs.surface == 'headless' || steps.surface.outputs.surface == 'full'
|
||||
continue-on-error: true
|
||||
run: |
|
||||
if command -v cargo-audit >/dev/null 2>&1; then
|
||||
cargo audit || echo "cargo audit reported findings (see .cargo/audit.toml for accepted ones)"
|
||||
else
|
||||
echo "cargo-audit not installed on this runner; skipping"
|
||||
fi
|
||||
- name: Rust compiler cache statistics
|
||||
if: always() && (steps.surface.outputs.surface == 'headless' || steps.surface.outputs.surface == 'full')
|
||||
run: |
|
||||
if [ -n "${SCCACHE_DIR:-}" ]; then
|
||||
sccache --show-stats
|
||||
fi
|
||||
- name: Clean generated Toolkit build
|
||||
if: always()
|
||||
run: python3 ci/cleanup-generated.py
|
||||
|
||||
# The Windows build of the same change (main's release gate, run on the PR):
|
||||
# clippy for the Windows target, the release .exe, every unit test under
|
||||
# Wine, and the headless UI self-test under Wine. Only for changes that can
|
||||
# affect the binary (the "full" surface).
|
||||
verify-windows:
|
||||
needs: verify
|
||||
if: needs.verify.outputs.surface == 'full'
|
||||
runs-on: alkane-verify
|
||||
container:
|
||||
image: gitea.bluefla.re/blueflare/alkane-ci@sha256:4929b2979851f1f12fb8f92ef74236ae4c2ea735a41ead94a16af416a4f0a1a5
|
||||
env:
|
||||
CARGO_TARGET_DIR: target
|
||||
CARGO_BUILD_JOBS: 2
|
||||
WX_WINDOWS_PREFIX: /root/.cache/wx-3.3.3-mingw
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Install Windows toolchain
|
||||
run: bash ci/windows-ci.sh toolchain
|
||||
|
||||
- name: Configure bounded Rust compiler cache
|
||||
run: bash ci/configure-rust-cache.sh
|
||||
|
||||
- name: Compute wxMSW cache key
|
||||
id: wx-key
|
||||
run: bash ci/wx-cache-key.sh windows
|
||||
|
||||
- name: Check prebuilt wxMSW
|
||||
id: wx-image
|
||||
run: bash ci/check-wx-image-prefix.sh windows
|
||||
|
||||
- name: Cache vendored wxMSW build
|
||||
if: steps.wx-image.outputs.available != 'true'
|
||||
id: wx-cache
|
||||
uses: actions/cache/restore@v4
|
||||
continue-on-error: true
|
||||
with:
|
||||
path: /root/.cache/wx-3.3.3-mingw
|
||||
key: ${{ steps.wx-key.outputs.key }}
|
||||
|
||||
- name: Build wxMSW (once, then cached)
|
||||
id: wx-build
|
||||
run: bash ci/ensure-wx-prefix.sh windows
|
||||
|
||||
- name: Save vendored wxMSW build
|
||||
if: steps.wx-cache.outputs.cache-hit != 'true' && steps.wx-build.outputs.built == 'true'
|
||||
uses: actions/cache/save@v4
|
||||
continue-on-error: true
|
||||
with:
|
||||
path: /root/.cache/wx-3.3.3-mingw
|
||||
key: ${{ steps.wx-key.outputs.key }}
|
||||
|
||||
- name: Clippy (Windows target, warnings are errors)
|
||||
run: bash ci/windows-ci.sh clippy
|
||||
|
||||
- name: Build (Windows)
|
||||
run: bash ci/windows-ci.sh build
|
||||
|
||||
- name: Unit tests (Windows build, under Wine)
|
||||
run: bash ci/windows-ci.sh test
|
||||
|
||||
- name: Headless UI self-test (Windows build, under Wine)
|
||||
run: bash ci/windows-ci.sh selftest
|
||||
|
||||
- name: Clean generated Toolkit build
|
||||
if: always()
|
||||
run: python3 ci/cleanup-generated.py
|
||||
@@ -0,0 +1,101 @@
|
||||
name: windows-native
|
||||
|
||||
# Smoke test on real Windows. The self-hosted runner in the Windows 11 test VM
|
||||
# (label alkane-windows, host mode; see docs/windows.md) takes the Windows
|
||||
# build that main's build-test-release run cross-compiled -- the release .exe
|
||||
# and every unit-test executable, which already passed under Wine there -- and
|
||||
# runs them natively: real Win32, real comctl32/visual styles, real DPI and
|
||||
# real Windows networking.
|
||||
#
|
||||
# Advisory by design: a separate workflow, so it never gates or delays a
|
||||
# release, and while the VM is off the job simply waits for the runner.
|
||||
on:
|
||||
workflow_run:
|
||||
workflows: [build-test-release]
|
||||
types: [completed]
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
run_id:
|
||||
description: build-test-release run whose Windows build to test
|
||||
required: true
|
||||
|
||||
permissions:
|
||||
actions: read
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
native:
|
||||
if: github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success'
|
||||
runs-on: alkane-windows
|
||||
timeout-minutes: 30
|
||||
env:
|
||||
RUN_ID: ${{ github.event.workflow_run.id || inputs.run_id }}
|
||||
# Windows' default execution policy (Restricted) refuses the script files
|
||||
# the runner writes for each step; bypass it for these steps only, so the
|
||||
# runner works without changing the machine's policy.
|
||||
defaults:
|
||||
run:
|
||||
shell: powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -File {0}
|
||||
steps:
|
||||
- name: Fetch the cross-built Windows build
|
||||
env:
|
||||
TOKEN: ${{ github.token }}
|
||||
API: ${{ github.api_url }}/repos/${{ github.repository }}
|
||||
run: |
|
||||
$ErrorActionPreference = 'Stop'
|
||||
$ProgressPreference = 'SilentlyContinue'
|
||||
$work = Join-Path ([IO.Path]::GetTempPath()) "alkane-native-$env:RUN_ID"
|
||||
Remove-Item -Recurse -Force $work -ErrorAction SilentlyContinue
|
||||
New-Item -ItemType Directory $work | Out-Null
|
||||
$auth = @{ Authorization = "token $env:TOKEN" }
|
||||
$list = Invoke-RestMethod -Headers $auth "$env:API/actions/runs/$env:RUN_ID/artifacts"
|
||||
$art = $list.artifacts | Where-Object { $_.name -eq 'alkane-toolkit-windows-tests' } |
|
||||
Select-Object -First 1
|
||||
if (-not $art) { throw "run $env:RUN_ID has no alkane-toolkit-windows-tests artifact" }
|
||||
Invoke-WebRequest -Headers $auth -Uri $art.archive_download_url -OutFile "$work\artifact.zip"
|
||||
Expand-Archive "$work\artifact.zip" "$work\artifact"
|
||||
$bundle = Get-ChildItem -Recurse "$work\artifact" -Filter alkane-toolkit-windows-tests.zip |
|
||||
Select-Object -First 1
|
||||
Expand-Archive $bundle.FullName "$work\tests"
|
||||
$exe = Get-ChildItem -Recurse "$work\artifact" -Filter alkane-toolkit.exe | Select-Object -First 1
|
||||
Copy-Item $exe.FullName "$work\alkane-toolkit.exe"
|
||||
"ALKANE_NATIVE=$work" | Out-File -Append -Encoding utf8 $env:GITHUB_ENV
|
||||
$os = Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion'
|
||||
"Testing run $env:RUN_ID on Windows $($os.DisplayVersion) build $($os.CurrentBuild)"
|
||||
|
||||
- name: Authenticode signature (native Windows)
|
||||
env:
|
||||
SIGNING_ENABLED: ${{ vars.AZURE_SIGNING_PROFILE }}
|
||||
run: |
|
||||
# Windows itself judges the signature: chain to a trusted root,
|
||||
# intact file hash, valid timestamp. Required once signing is on.
|
||||
$sig = Get-AuthenticodeSignature "$env:ALKANE_NATIVE\alkane-toolkit.exe"
|
||||
Write-Host "Signature: $($sig.Status) $($sig.StatusMessage)"
|
||||
if ($sig.SignerCertificate) { Write-Host "Signer: $($sig.SignerCertificate.Subject)" }
|
||||
if ($sig.TimeStamperCertificate) { Write-Host "Timestamp: $($sig.TimeStamperCertificate.Subject)" }
|
||||
if ($env:SIGNING_ENABLED -and $sig.Status -ne 'Valid') { exit 1 }
|
||||
|
||||
- name: Unit tests (native Windows)
|
||||
run: |
|
||||
$failed = @()
|
||||
foreach ($test in Get-Content "$env:ALKANE_NATIVE\tests\tests.txt") {
|
||||
Write-Host "== $test"
|
||||
& "$env:ALKANE_NATIVE\tests\$test"
|
||||
if ($LASTEXITCODE -ne 0) { $failed += $test }
|
||||
}
|
||||
if ($failed) { Write-Host "FAILED: $($failed -join ', ')"; exit 1 }
|
||||
|
||||
- name: Headless UI self-test (native Windows)
|
||||
run: |
|
||||
# A GUI-subsystem executable: wait for it and read its redirected output.
|
||||
$env:ALKANE_SELFTEST = '1'
|
||||
$out = "$env:ALKANE_NATIVE\selftest.txt"
|
||||
$p = Start-Process -FilePath "$env:ALKANE_NATIVE\alkane-toolkit.exe" -Wait -PassThru `
|
||||
-NoNewWindow -RedirectStandardOutput $out -RedirectStandardError "$out.err"
|
||||
Get-Content $out, "$out.err"
|
||||
if ($p.ExitCode -ne 0) { exit $p.ExitCode }
|
||||
|
||||
- name: Remove the downloaded build
|
||||
if: always()
|
||||
run: |
|
||||
if ($env:ALKANE_NATIVE) { Remove-Item -Recurse -Force $env:ALKANE_NATIVE -ErrorAction SilentlyContinue }
|
||||
@@ -1,3 +1,9 @@
|
||||
/target
|
||||
**/target
|
||||
.claude/
|
||||
__pycache__/
|
||||
# Internal working notes live outside this public repository.
|
||||
/AGENTS.md
|
||||
/HANDOFF.md
|
||||
/CLAUDE.md
|
||||
code-review-report*.md
|
||||
|
||||
@@ -40,13 +40,20 @@ name = "alkane-artifacts"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"alkane-core",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"sha2",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "alkane-cli"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"alkane-artifacts",
|
||||
"alkane-core",
|
||||
"alkane-flash",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -69,7 +76,12 @@ dependencies = [
|
||||
name = "alkane-flash"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"alkane-artifacts",
|
||||
"alkane-core",
|
||||
"serde_json",
|
||||
"sha2",
|
||||
"tempfile",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -77,8 +89,10 @@ name = "alkane-gui"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"aes",
|
||||
"alkane-artifacts",
|
||||
"alkane-core",
|
||||
"alkane-discovery",
|
||||
"alkane-flash",
|
||||
"asic-rs",
|
||||
"base64 0.21.7",
|
||||
"ecb",
|
||||
|
||||
@@ -13,9 +13,22 @@ members = [
|
||||
[workspace.package]
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
license = "MIT OR Apache-2.0"
|
||||
license = "MIT"
|
||||
|
||||
[workspace.dependencies]
|
||||
thiserror = "2"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
sha2 = "0.10"
|
||||
serde_json = "1"
|
||||
|
||||
# Release binary: small, self-contained, and fast enough for a fleet GUI whose
|
||||
# hot path is network I/O, not CPU (round 18 #12). opt-level "s" + fat LTO +
|
||||
# one codegen unit + symbol stripping cut the binary substantially. panic stays
|
||||
# "unwind" on purpose: the scan runs many miners on tokio tasks and the wx event
|
||||
# dispatch relies on unwinding so one miner's panic never aborts the whole app.
|
||||
[profile.release]
|
||||
opt-level = "s"
|
||||
lto = "fat"
|
||||
codegen-units = 1
|
||||
strip = true
|
||||
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2026 BlueFlare Energy Solutions Inc.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -1,38 +1,115 @@
|
||||
# alkane-toolkit
|
||||
# Alkane Toolkit
|
||||
|
||||
Cross-platform desktop toolkit (Rust core, native GUI) to install **stock** or
|
||||
**alkaneOS** firmware on ASIC miner control boards, whatever they currently run
|
||||
(stock, Vnish/AnthillOS, Braiins OS+, LuxOS, ePIC, BixBit, WardenOS).
|
||||
[](https://gitea.bluefla.re/blueflare/alkane-toolkit/actions)
|
||||
[](https://blueflare.gitea.bluefla.re/alkane-toolkit/quality/)
|
||||
[](https://blueflare.gitea.bluefla.re/alkane-toolkit/quality/#coverage)
|
||||
[](https://blueflare.gitea.bluefla.re/alkane-toolkit/bench/)
|
||||
[](https://blueflare.gitea.bluefla.re/alkane-toolkit/quality/)
|
||||
[](https://blueflare.gitea.bluefla.re/alkane-toolkit/quality/#coverage)
|
||||
[](https://blueflare.gitea.bluefla.re/alkane-toolkit/quality/)
|
||||
[](https://blueflare.gitea.bluefla.re/alkane-toolkit/quality/)
|
||||
[](LICENSE)
|
||||
|
||||
Sibling to [alkane-sdk](https://gitea.bluefla.re/blueflare/alkane-sdk): the SDK
|
||||
owns board knowledge and build outputs; this toolkit is the orchestration
|
||||
engine and operator UI that executes its install recipes safely.
|
||||
The badges link to the [project site](https://blueflare.gitea.bluefla.re/alkane-toolkit/): the [manual](https://blueflare.gitea.bluefla.re/alkane-toolkit/manual/),
|
||||
the [code-quality dashboard](https://blueflare.gitea.bluefla.re/alkane-toolkit/quality/) (how the grade is set, coverage by
|
||||
crate and file) and the [benchmark history](https://blueflare.gitea.bluefla.re/alkane-toolkit/bench/).
|
||||
|
||||
## Status
|
||||
Desktop miner discovery and monitoring. The Rust GUI uses wxWidgets through
|
||||
a patched [wx-rs bridge](thirdparty/wx-rs). In-app image flashing is disabled
|
||||
while that path is reworked.
|
||||
|
||||
M1 primary view ships: discovery (vendored asic-rs 0.8.1) + the UI-SPEC
|
||||
table (6 KPIs, sortable/drag-reorderable columns, row checkboxes, scan-range
|
||||
editor, Excel-style filter, columns panel). Pure-Rust GUI on wxWidgets via
|
||||
vendored + patched wx-rs (docs/adr/0002, 0003).
|
||||
|
||||
## Build + run (Debian)
|
||||
## Run locally
|
||||
|
||||
```sh
|
||||
scripts/build-wx-linux.sh # once: builds vendored wx 3.3.3 to ~/.cache
|
||||
scripts/build-gui-linux.sh # cargo build --release -p alkane-gui
|
||||
~/.cache/alkane-tk-target/release/alkane-toolkit
|
||||
scripts/build-wx-linux.sh
|
||||
scripts/build-gui-linux.sh
|
||||
ALKANE_FAKE_SCAN=1 ~/.cache/alkane-tk-target/release/alkane-toolkit
|
||||
```
|
||||
|
||||
Windows/macOS: `scripts/build-gui-windows.sh` (needs a Windows/msys2
|
||||
toolchain) and `scripts/build-gui-macos.sh` (needs a Mac) - the GUI code is
|
||||
shared; only the C++ bridge + wx differ per platform.
|
||||
The first command builds wxWidgets 3.3.3 once. Linux needs GTK 3. Fake scan
|
||||
uses offline fixtures and does not contact hardware.
|
||||
|
||||
See `docs/IMPLEMENTATION-PLAN.md` for the roadmap.
|
||||
Windows 10/11 builds are cross-compiled on Linux (`scripts/build-wx-windows.sh`,
|
||||
then `scripts/build-gui-windows.sh`) and attached to every release as a
|
||||
self-contained `alkane-toolkit.exe`; see [docs/windows.md](docs/windows.md).
|
||||
macOS builds run on a Mac (`scripts/build-wx-macos.sh`, then
|
||||
`scripts/build-gui-macos.sh`) and produce one universal app for Apple Silicon
|
||||
and Intel, macOS 11 or later; see [docs/macos.md](docs/macos.md).
|
||||
|
||||
## Hard rules (from alkane-sdk `docs/install-methods.md`, encoded in code)
|
||||
## Current scope
|
||||
|
||||
1. Never write the bootloader/secure region.
|
||||
2. Verify on a channel the installer does not control.
|
||||
3. Reboot is root-only; a dropped SSH connection can be success.
|
||||
4. A packer is per board; never reuse across boards.
|
||||
5. Boot-switch handling is an explicit, verified step.
|
||||
- Discover miners by CIDR or IP range with vendored [asic-rs](vendor/asic-rs).
|
||||
- Show model, firmware, health, hashrate, temperatures, fans, PSU, and workers.
|
||||
- Filter and sort the desktop table; inspect probes with `miner_probe`.
|
||||
- Use controls supported by the selected firmware and restore supported
|
||||
aftermarket firmware to stock.
|
||||
|
||||
Discovery is read-only. Control and restore actions change miners. Check the
|
||||
selected devices before applying them.
|
||||
|
||||
## Headless .afw install (CV1835 and AM335x)
|
||||
|
||||
`alkane-cli` is the headless engine; the GUI uses the same engine. A verified
|
||||
`.afw` is a full OS bundle for exactly one board. The CV1835 installer writes
|
||||
only `/dev/mmcblk0p1` over SSH, holds the current p1 first, reads the whole
|
||||
partition back, and reboots only after the read-back matches.
|
||||
|
||||
The AM335x installer writes mtd8 (type-3 ramdisk), mtd6 (dtb) and mtd7
|
||||
(legacy kernel uImage) with `flash_erase` + `nandwrite`. It never touches
|
||||
mtd0-5 (SPL/U-Boot/bootenv), holds a backup of each target before writing it,
|
||||
and reads every target back before reboot. Its `verify-live` checks SSH
|
||||
(kernel string), the HTTP marker on port 80, the API on port 4028, and the
|
||||
eth0 DHCP lease.
|
||||
|
||||
```sh
|
||||
alkane-cli afw verify BUNDLE.afw --allow-unsigned
|
||||
alkane-cli afw plan BUNDLE.afw --host 10.255.1.167
|
||||
alkane-cli afw preflight BUNDLE.afw --host 10.255.1.167
|
||||
alkane-cli afw install BUNDLE.afw --host 10.255.1.167 --dry-run
|
||||
alkane-cli afw install BUNDLE.afw --host 10.255.1.167 --confirm FLASH
|
||||
alkane-cli afw verify-live BUNDLE.afw --host 10.255.1.167
|
||||
alkane-cli afw restore --host 10.255.1.167 --held-backup p1.img --confirm FLASH
|
||||
```
|
||||
|
||||
For AM335x the same subcommands infer the board from the bundle; restore names
|
||||
the three held copies explicitly:
|
||||
|
||||
```sh
|
||||
alkane-cli afw plan BUNDLE.afw --host 10.255.1.64
|
||||
alkane-cli afw preflight BUNDLE.afw --host 10.255.1.64
|
||||
alkane-cli afw install BUNDLE.afw --host 10.255.1.64 --dry-run
|
||||
alkane-cli afw install BUNDLE.afw --host 10.255.1.64 --confirm FLASH
|
||||
alkane-cli afw verify-live BUNDLE.afw --host 10.255.1.64 --timeout 300
|
||||
alkane-cli afw restore --board am335x --host 10.255.1.64 \
|
||||
--held-backup mtd8.img --held-kernel mtd7.img --held-dtb mtd6.img --confirm FLASH
|
||||
```
|
||||
|
||||
On stock Bitmain CV1835 the SSH login is the unprivileged `miner` account; the
|
||||
toolkit obtains root through the stock update-daemon loopback channel, not a
|
||||
serial console. Credentials come only from `ALKANE_BOARD_PASSWORD` or
|
||||
`ALKANE_BOARD_PASSWORD_FILE`, or from `--password-file` / `--identity-file`.
|
||||
No credential value is printed. `--allow-unsigned` is bench-only; site use
|
||||
needs an Ed25519-signed bundle.
|
||||
|
||||
## Development
|
||||
|
||||
```sh
|
||||
WX_RS_WXCONFIG="$HOME/.cache/wx-3.3.3-linux/bin/wx-config" \
|
||||
CARGO_TARGET_DIR="$HOME/.cache/alkane-tk-target" \
|
||||
cargo test -p alkane-gui --offline
|
||||
```
|
||||
|
||||
Keep build output off the source share. `main` runs full CI and publishes
|
||||
qualifying releases; `next` builds `-rc` candidates. Badges measure host
|
||||
code, not installation support. See the
|
||||
[benchmark history](https://blueflare.gitea.bluefla.re/alkane-toolkit/bench/).
|
||||
|
||||
## Firmware safety
|
||||
|
||||
Installation must follow the [SDK install rules](https://gitea.bluefla.re/blueflare/alkane-sdk/src/branch/main/docs/install-methods.md):
|
||||
use the exact board's packer and boot-switch procedure, verify through an
|
||||
independent channel, and never write bootloader or secure regions.
|
||||
|
||||
## License
|
||||
|
||||
[MIT](LICENSE). Vendored dependencies keep their own terms.
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"bench_parse_line_near_max_addrs": 807160.0,
|
||||
"bench_parse_targets_many_ranges_near_cap": 54465485.0,
|
||||
"bench_render_cells": 3503112.0,
|
||||
"bench_sort_by_each_column": 486975.0,
|
||||
"bench_targets_to_cidrs_large_range": 482167.0,
|
||||
"bench_upsert_cold_start_population": 8315814.0,
|
||||
"bench_view_pipeline": 191794.0,
|
||||
"bench_view_pipeline_default_sort": 193214.0,
|
||||
"bench_view_pipeline_with_filter": 253085.0
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="128.0" height="20" role="img" aria-label="bench coverage: 7%">
|
||||
<title>bench coverage: 7%</title>
|
||||
<linearGradient id="s" x2="0" y2="100%">
|
||||
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
|
||||
<stop offset="1" stop-opacity=".1"/>
|
||||
</linearGradient>
|
||||
<clipPath id="r"><rect width="128.0" height="20" rx="3" fill="#fff"/></clipPath>
|
||||
<g clip-path="url(#r)">
|
||||
<rect width="103.0" height="20" fill="#555"/>
|
||||
<rect x="103.0" width="25.0" height="20" fill="#e05d44"/>
|
||||
<rect width="128.0" height="20" fill="url(#s)"/>
|
||||
</g>
|
||||
<g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" font-size="11">
|
||||
<text x="51.5" y="15" fill="#010101" fill-opacity=".3">bench coverage</text>
|
||||
<text x="51.5" y="14">bench coverage</text>
|
||||
<text x="115.5" y="15" fill="#010101" fill-opacity=".3">7%</text>
|
||||
<text x="115.5" y="14">7%</text>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 946 B |
@@ -0,0 +1,19 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="141.0" height="20" role="img" aria-label="bench: +7.4% vs prev">
|
||||
<title>bench: +7.4% vs prev</title>
|
||||
<linearGradient id="s" x2="0" y2="100%">
|
||||
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
|
||||
<stop offset="1" stop-opacity=".1"/>
|
||||
</linearGradient>
|
||||
<clipPath id="r"><rect width="141.0" height="20" rx="3" fill="#fff"/></clipPath>
|
||||
<g clip-path="url(#r)">
|
||||
<rect width="44.5" height="20" fill="#555"/>
|
||||
<rect x="44.5" width="96.5" height="20" fill="#dfb317"/>
|
||||
<rect width="141.0" height="20" fill="url(#s)"/>
|
||||
</g>
|
||||
<g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" font-size="11">
|
||||
<text x="22.2" y="15" fill="#010101" fill-opacity=".3">bench</text>
|
||||
<text x="22.2" y="14">bench</text>
|
||||
<text x="92.8" y="15" fill="#010101" fill-opacity=".3">+7.4% vs prev</text>
|
||||
<text x="92.8" y="14">+7.4% vs prev</text>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 950 B |
@@ -0,0 +1,19 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="95.5" height="20" role="img" aria-label="coverage: 44%">
|
||||
<title>coverage: 44%</title>
|
||||
<linearGradient id="s" x2="0" y2="100%">
|
||||
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
|
||||
<stop offset="1" stop-opacity=".1"/>
|
||||
</linearGradient>
|
||||
<clipPath id="r"><rect width="95.5" height="20" rx="3" fill="#fff"/></clipPath>
|
||||
<g clip-path="url(#r)">
|
||||
<rect width="64.0" height="20" fill="#555"/>
|
||||
<rect x="64.0" width="31.5" height="20" fill="#dfb317"/>
|
||||
<rect width="95.5" height="20" fill="url(#s)"/>
|
||||
</g>
|
||||
<g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" font-size="11">
|
||||
<text x="32.0" y="15" fill="#010101" fill-opacity=".3">coverage</text>
|
||||
<text x="32.0" y="14">coverage</text>
|
||||
<text x="79.8" y="15" fill="#010101" fill-opacity=".3">44%</text>
|
||||
<text x="79.8" y="14">44%</text>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 919 B |
@@ -0,0 +1,19 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="121.5" height="20" role="img" aria-label="dependencies: 349">
|
||||
<title>dependencies: 349</title>
|
||||
<linearGradient id="s" x2="0" y2="100%">
|
||||
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
|
||||
<stop offset="1" stop-opacity=".1"/>
|
||||
</linearGradient>
|
||||
<clipPath id="r"><rect width="121.5" height="20" rx="3" fill="#fff"/></clipPath>
|
||||
<g clip-path="url(#r)">
|
||||
<rect width="90.0" height="20" fill="#555"/>
|
||||
<rect x="90.0" width="31.5" height="20" fill="#007ec6"/>
|
||||
<rect width="121.5" height="20" fill="url(#s)"/>
|
||||
</g>
|
||||
<g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" font-size="11">
|
||||
<text x="45.0" y="15" fill="#010101" fill-opacity=".3">dependencies</text>
|
||||
<text x="45.0" y="14">dependencies</text>
|
||||
<text x="105.8" y="15" fill="#010101" fill-opacity=".3">349</text>
|
||||
<text x="105.8" y="14">349</text>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 940 B |
@@ -0,0 +1,19 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="89.0" height="20" role="img" aria-label="license: MIT">
|
||||
<title>license: MIT</title>
|
||||
<linearGradient id="s" x2="0" y2="100%">
|
||||
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
|
||||
<stop offset="1" stop-opacity=".1"/>
|
||||
</linearGradient>
|
||||
<clipPath id="r"><rect width="89.0" height="20" rx="3" fill="#fff"/></clipPath>
|
||||
<g clip-path="url(#r)">
|
||||
<rect width="57.5" height="20" fill="#555"/>
|
||||
<rect x="57.5" width="31.5" height="20" fill="#007ec6"/>
|
||||
<rect width="89.0" height="20" fill="url(#s)"/>
|
||||
</g>
|
||||
<g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" font-size="11">
|
||||
<text x="28.8" y="15" fill="#010101" fill-opacity=".3">license</text>
|
||||
<text x="28.8" y="14">license</text>
|
||||
<text x="73.2" y="15" fill="#010101" fill-opacity=".3">MIT</text>
|
||||
<text x="73.2" y="14">MIT</text>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 915 B |
@@ -0,0 +1,19 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="141.0" height="20" role="img" aria-label="lines of code: 13.7K">
|
||||
<title>lines of code: 13.7K</title>
|
||||
<linearGradient id="s" x2="0" y2="100%">
|
||||
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
|
||||
<stop offset="1" stop-opacity=".1"/>
|
||||
</linearGradient>
|
||||
<clipPath id="r"><rect width="141.0" height="20" rx="3" fill="#fff"/></clipPath>
|
||||
<g clip-path="url(#r)">
|
||||
<rect width="96.5" height="20" fill="#555"/>
|
||||
<rect x="96.5" width="44.5" height="20" fill="#007ec6"/>
|
||||
<rect width="141.0" height="20" fill="url(#s)"/>
|
||||
</g>
|
||||
<g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" font-size="11">
|
||||
<text x="48.2" y="15" fill="#010101" fill-opacity=".3">lines of code</text>
|
||||
<text x="48.2" y="14">lines of code</text>
|
||||
<text x="118.8" y="15" fill="#010101" fill-opacity=".3">13.7K</text>
|
||||
<text x="118.8" y="14">13.7K</text>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 952 B |
@@ -0,0 +1,19 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="108.5" height="20" role="img" aria-label="code quality: A">
|
||||
<title>code quality: A</title>
|
||||
<linearGradient id="s" x2="0" y2="100%">
|
||||
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
|
||||
<stop offset="1" stop-opacity=".1"/>
|
||||
</linearGradient>
|
||||
<clipPath id="r"><rect width="108.5" height="20" rx="3" fill="#fff"/></clipPath>
|
||||
<g clip-path="url(#r)">
|
||||
<rect width="90.0" height="20" fill="#555"/>
|
||||
<rect x="90.0" width="18.5" height="20" fill="#4c1"/>
|
||||
<rect width="108.5" height="20" fill="url(#s)"/>
|
||||
</g>
|
||||
<g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" font-size="11">
|
||||
<text x="45.0" y="15" fill="#010101" fill-opacity=".3">code quality</text>
|
||||
<text x="45.0" y="14">code quality</text>
|
||||
<text x="99.2" y="15" fill="#010101" fill-opacity=".3">A</text>
|
||||
<text x="99.2" y="14">A</text>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 927 B |
@@ -0,0 +1,19 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="121.5" height="20" role="img" aria-label="tests: 95 passing">
|
||||
<title>tests: 95 passing</title>
|
||||
<linearGradient id="s" x2="0" y2="100%">
|
||||
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
|
||||
<stop offset="1" stop-opacity=".1"/>
|
||||
</linearGradient>
|
||||
<clipPath id="r"><rect width="121.5" height="20" rx="3" fill="#fff"/></clipPath>
|
||||
<g clip-path="url(#r)">
|
||||
<rect width="44.5" height="20" fill="#555"/>
|
||||
<rect x="44.5" width="77.0" height="20" fill="#4c1"/>
|
||||
<rect width="121.5" height="20" fill="url(#s)"/>
|
||||
</g>
|
||||
<g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" font-size="11">
|
||||
<text x="22.2" y="15" fill="#010101" fill-opacity=".3">tests</text>
|
||||
<text x="22.2" y="14">tests</text>
|
||||
<text x="83.0" y="15" fill="#010101" fill-opacity=".3">95 passing</text>
|
||||
<text x="83.0" y="14">95 passing</text>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 935 B |
@@ -0,0 +1,54 @@
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""Write small badge artifacts to the private Gitea CI badge branch."""
|
||||
|
||||
import base64
|
||||
import json
|
||||
from urllib.error import HTTPError
|
||||
from urllib.parse import quote
|
||||
from urllib.request import Request, urlopen
|
||||
|
||||
BADGE_BRANCH = "ci-badges"
|
||||
|
||||
|
||||
def request_json(url: str, token: str, method: str = "GET",
|
||||
payload: dict | None = None) -> dict:
|
||||
body = None if payload is None else json.dumps(payload).encode()
|
||||
request = Request(url, data=body, method=method,
|
||||
headers={"Authorization": f"token {token}",
|
||||
"Content-Type": "application/json"})
|
||||
with urlopen(request, timeout=20) as response:
|
||||
return json.load(response)
|
||||
|
||||
|
||||
def publish_file(api: str, repo: str, filename: str, data: bytes,
|
||||
token: str, message: str, max_bytes: int = 65_536) -> None:
|
||||
if not filename or filename.startswith("/") or any(
|
||||
part in ("", ".", "..") for part in filename.split("/")):
|
||||
raise ValueError("invalid badge path")
|
||||
if len(data) > max_bytes:
|
||||
raise ValueError("artifact exceeds size limit")
|
||||
path = f"{api}/repos/{repo}/contents/{quote(filename, safe='/')}"
|
||||
encoded = base64.b64encode(data).decode("ascii")
|
||||
for attempt in range(3):
|
||||
try:
|
||||
current = request_json(path + "?ref=" + BADGE_BRANCH, token)
|
||||
if base64.b64decode(current["content"]) == data:
|
||||
return
|
||||
sha = current["sha"]
|
||||
method = "PUT"
|
||||
except HTTPError as error:
|
||||
if error.code != 404:
|
||||
raise
|
||||
sha = None
|
||||
method = "POST"
|
||||
payload = {"branch": BADGE_BRANCH, "content": encoded,
|
||||
"message": message + " [skip ci]"}
|
||||
if sha is not None:
|
||||
payload["sha"] = sha
|
||||
try:
|
||||
request_json(path, token, method, payload)
|
||||
return
|
||||
except HTTPError as error:
|
||||
if error.code not in (409, 422) or attempt == 2:
|
||||
raise
|
||||
raise RuntimeError("badge write did not complete")
|
||||
@@ -0,0 +1,110 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Check the Toolkit benchmark suite and compare it with the prior release."""
|
||||
import argparse
|
||||
import json
|
||||
import math
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
|
||||
# A Duration Debug token: a number immediately followed by a time unit, with the
|
||||
# multi-char units listed before the bare "s" so alternation prefers them. The
|
||||
# trailing (?![a-zA-Z]) stops "s" in words like "rows"/"cidrs" from matching.
|
||||
_DUR = re.compile(r"([0-9]+(?:\.[0-9]+)?)\s*(ns|\u00b5s|us|ms|s)(?![a-zA-Z])")
|
||||
_UNIT_NS = {"ns": 1.0, "us": 1e3, "\u00b5s": 1e3, "ms": 1e6, "s": 1e9}
|
||||
_NAME = re.compile(r"^(?:test [A-Za-z0-9_:]+ \.\.\. )?(bench_[A-Za-z0-9_]+)\s*:")
|
||||
EXPECTED_BENCHES = {
|
||||
"bench_verify_artifact", "bench_naming_transforms",
|
||||
"bench_parse_line_near_max_addrs", "bench_parse_targets_many_ranges_near_cap",
|
||||
"bench_targets_to_cidrs_large_range", "bench_view_pipeline",
|
||||
"bench_render_cells", "bench_health_diagnostics",
|
||||
"bench_view_pipeline_default_sort", "bench_sort_by_each_column",
|
||||
"bench_view_pipeline_with_filter", "bench_upsert_cold_start_population",
|
||||
}
|
||||
|
||||
|
||||
def parse_bench(text: str) -> dict:
|
||||
"""Map each bench name to its timing in nanoseconds (the last Duration on the
|
||||
line, since every bench prints its timing last)."""
|
||||
out = {}
|
||||
for line in text.splitlines():
|
||||
m = _NAME.match(line.strip())
|
||||
if not m:
|
||||
continue
|
||||
durs = _DUR.findall(line)
|
||||
if not durs:
|
||||
raise ValueError(f"missing timing for {m.group(1)}")
|
||||
val, unit = durs[-1]
|
||||
elapsed = float(val) * _UNIT_NS[unit]
|
||||
if m.group(1) in out or not math.isfinite(elapsed) or elapsed <= 0:
|
||||
raise ValueError(f"duplicate or invalid timing for {m.group(1)}")
|
||||
out[m.group(1)] = elapsed
|
||||
return out
|
||||
|
||||
|
||||
def require_suite(results: dict) -> dict:
|
||||
if results.keys() != EXPECTED_BENCHES:
|
||||
raise ValueError("microbenchmark results differ from the declared suite")
|
||||
return results
|
||||
|
||||
|
||||
def benchmark_delta(current: dict, baseline: dict) -> float | None:
|
||||
"""Average each comparable benchmark's percentage improvement."""
|
||||
if not baseline:
|
||||
return None
|
||||
changes = [(baseline[name] - current[name]) / baseline[name]
|
||||
for name in baseline]
|
||||
delta = math.fsum(changes) / len(changes) * 100
|
||||
if not math.isfinite(delta):
|
||||
raise ValueError("benchmark comparison is not finite")
|
||||
return delta
|
||||
|
||||
|
||||
def main() -> int:
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("--current", required=True, help="captured bench stdout")
|
||||
ap.add_argument("--baseline", required=True, help="baseline JSON (read + rewrite)")
|
||||
ap.add_argument("--no-write", action="store_true", help="do not update the baseline")
|
||||
args = ap.parse_args()
|
||||
|
||||
try:
|
||||
with open(args.current, encoding="utf-8") as fh:
|
||||
current = require_suite(parse_bench(fh.read()))
|
||||
except (OSError, ValueError) as e:
|
||||
print(f"bench-badge: cannot read current results: {e}", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
baseline = {}
|
||||
try:
|
||||
with open(args.baseline, encoding="utf-8") as fh:
|
||||
baseline = json.load(fh)
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
except (OSError, ValueError) as e:
|
||||
print(f"bench-badge: invalid baseline: {e}", file=sys.stderr)
|
||||
return 1
|
||||
if (not isinstance(baseline, dict)
|
||||
or not set(baseline).issubset(current)
|
||||
or any(not isinstance(value, (int, float)) or isinstance(value, bool)
|
||||
or not math.isfinite(value) or value <= 0
|
||||
for value in baseline.values())):
|
||||
print("bench-badge: invalid or retired benchmark in baseline", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
try:
|
||||
delta = benchmark_delta(current, baseline)
|
||||
except ValueError as error:
|
||||
print(f"bench-badge: {error}", file=sys.stderr)
|
||||
return 1
|
||||
print("NEW" if delta is None else f"{delta:.1f}")
|
||||
|
||||
if not args.no_write:
|
||||
os.makedirs(os.path.dirname(args.baseline) or ".", exist_ok=True)
|
||||
with open(args.baseline, "w", encoding="utf-8") as fh:
|
||||
json.dump({k: round(v, 1) for k, v in sorted(current.items())}, fh, indent=2)
|
||||
fh.write("\n")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,8 @@
|
||||
#!/usr/bin/env bash
|
||||
# Measure only instrumented microbenchmarks, excluding unit and UI tests.
|
||||
set -euo pipefail
|
||||
export WX_RS_WXCONFIG="${WX_RS_WXCONFIG:-$HOME/.cache/wx-3.3.3-linux/bin/wx-config}"
|
||||
cargo llvm-cov clean --workspace
|
||||
cargo llvm-cov --no-report --offline --release -p alkane-gui -p alkane-artifacts -- \
|
||||
--ignored --nocapture bench_
|
||||
cargo llvm-cov report --release --ignore-filename-regex '(thirdparty|vendor)/' "$@"
|
||||
@@ -0,0 +1,227 @@
|
||||
#!/usr/bin/env python3
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""Keep measured main-branch benchmarks in the private CI artifact branch."""
|
||||
|
||||
import argparse
|
||||
import base64
|
||||
from datetime import datetime, timezone
|
||||
import html
|
||||
import json
|
||||
import math
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import subprocess
|
||||
import importlib.util
|
||||
from urllib.error import HTTPError
|
||||
from urllib.parse import quote
|
||||
import yaml
|
||||
|
||||
from badge_store import BADGE_BRANCH, publish_file, request_json
|
||||
from bench_host import identity as benchmark_host
|
||||
|
||||
bench_spec = importlib.util.spec_from_file_location(
|
||||
"bench_badge", Path(__file__).with_name("bench-badge.py"))
|
||||
bench_badge = importlib.util.module_from_spec(bench_spec)
|
||||
bench_spec.loader.exec_module(bench_badge)
|
||||
|
||||
INDEX = "history/index.json"
|
||||
MAX_INDEX_ENTRIES = 50
|
||||
BENCH_INPUT_PATHS = (
|
||||
"Cargo.toml", "Cargo.lock", ".cargo", "crates", "thirdparty", "vendor",
|
||||
"rust-toolchain.toml", "ci/bench-badge.py", "ci/bench_host.py",
|
||||
"ci/ensure-build-deps.sh", "ci/ensure-wx-prefix.sh",
|
||||
"ci/configure-rust-cache.sh", "ci/wx-cache-key.sh",
|
||||
)
|
||||
|
||||
|
||||
def read_artifact(api: str, repo: str, token: str, filename: str):
|
||||
url = f"{api}/repos/{repo}/contents/{quote(filename, safe='/')}?ref={BADGE_BRANCH}"
|
||||
try:
|
||||
value = request_json(url, token)
|
||||
except HTTPError as error:
|
||||
if error.code == 404:
|
||||
return None
|
||||
raise
|
||||
return json.loads(base64.b64decode(value["content"]))
|
||||
|
||||
|
||||
def valid_benches(metrics: dict) -> dict[str, float]:
|
||||
values = metrics.get("bench_ns_per_op")
|
||||
if not isinstance(values, dict) or not values:
|
||||
raise ValueError("benchmark measurements are missing")
|
||||
if any(not isinstance(key, str) or not re.fullmatch(r"[A-Za-z0-9_]+", key)
|
||||
or not isinstance(value, (float, int)) or isinstance(value, bool)
|
||||
or not math.isfinite(value) or value <= 0 for key, value in values.items()):
|
||||
raise ValueError("benchmark measurements are invalid")
|
||||
return values
|
||||
|
||||
|
||||
def git_distance(ancestor: str, sha: str) -> int | None:
|
||||
if not re.fullmatch(r"[0-9a-f]{40}", ancestor) or not re.fullmatch(r"[0-9a-f]{40}", sha):
|
||||
return None
|
||||
result = subprocess.run(("git", "rev-list", "--count", f"{ancestor}..{sha}"),
|
||||
capture_output=True, text=True, check=False)
|
||||
if result.returncode != 0:
|
||||
return None
|
||||
ancestor_check = subprocess.run(("git", "merge-base", "--is-ancestor", ancestor, sha),
|
||||
capture_output=True, check=False)
|
||||
return int(result.stdout.strip()) if ancestor_check.returncode == 0 else None
|
||||
|
||||
|
||||
def previous_entry(index: dict, sha: str, distance=None, host=None) -> dict | None:
|
||||
distance = distance or git_distance
|
||||
choices = []
|
||||
for entry in index.get("entries", []):
|
||||
ancestor = entry.get("sha")
|
||||
if ancestor == sha or not isinstance(ancestor, str):
|
||||
continue
|
||||
if host is not None and entry.get("metrics", {}).get("bench_host") != host:
|
||||
continue
|
||||
gap = distance(ancestor, sha)
|
||||
if gap is not None and gap > 0:
|
||||
choices.append((gap, entry))
|
||||
if not choices:
|
||||
return None
|
||||
return min(choices, key=lambda item: item[0])[1]
|
||||
|
||||
|
||||
def previous_measurement(index: dict, sha: str, distance=None, host=None) -> dict:
|
||||
entry = previous_entry(index, sha, distance, host)
|
||||
return valid_benches(entry["metrics"]) if entry else {}
|
||||
|
||||
|
||||
def benchmark_build_settings(sha: str, root: Path | str = ".") -> dict:
|
||||
source = subprocess.check_output(
|
||||
("git", "show", f"{sha}:.gitea/workflows/release.yml"),
|
||||
cwd=root, text=True)
|
||||
job = yaml.load(source, Loader=yaml.BaseLoader)["jobs"]["release"]
|
||||
commands = {step.get("name", ""): step.get("run") for step in job["steps"]}
|
||||
selected = ("Install build dependencies", "Configure bounded Rust compiler cache",
|
||||
"Build wxWidgets (once, then cached)", "Build release", "Microbenchmarks")
|
||||
return {"image": job["container"]["image"], "env": job.get("env", {}),
|
||||
"commands": {name: commands.get(name) for name in selected}}
|
||||
|
||||
|
||||
def same_benchmark_inputs(ancestor: str, sha: str, root: Path | str = ".") -> bool:
|
||||
"""Identify changes that only reroute CI without changing benchmark inputs."""
|
||||
if not re.fullmatch(r"[0-9a-f]{40}", ancestor) or not re.fullmatch(r"[0-9a-f]{40}", sha):
|
||||
raise ValueError("invalid benchmark comparison commit")
|
||||
diff = subprocess.run(("git", "diff", "--quiet", ancestor, sha, "--",
|
||||
*BENCH_INPUT_PATHS), cwd=root, check=False)
|
||||
if diff.returncode not in (0, 1):
|
||||
raise ValueError("cannot inspect benchmark input changes")
|
||||
if diff.returncode == 1:
|
||||
return False
|
||||
try:
|
||||
return benchmark_build_settings(ancestor, root) == benchmark_build_settings(sha, root)
|
||||
except (KeyError, TypeError, subprocess.CalledProcessError, yaml.YAMLError):
|
||||
return False
|
||||
|
||||
|
||||
def updated_index(index: dict, sha: str, metrics: dict, timestamp: str) -> dict:
|
||||
if not re.fullmatch(r"[0-9a-f]{40}", sha):
|
||||
raise ValueError("invalid commit SHA")
|
||||
valid_benches(metrics)
|
||||
entries = [entry for entry in index.get("entries", []) if entry.get("sha") != sha]
|
||||
entries.append({"sha": sha, "timestamp": timestamp, "metrics": metrics})
|
||||
return {"schema": 1, "entries": entries[-MAX_INDEX_ENTRIES:]}
|
||||
|
||||
|
||||
def render_html(index: dict) -> str:
|
||||
entries = list(reversed(index.get("entries", [])))
|
||||
names = sorted({name for entry in entries
|
||||
for name in valid_benches(entry["metrics"])})
|
||||
rows = []
|
||||
for entry in entries:
|
||||
sha = entry["sha"]
|
||||
if not re.fullmatch(r"[0-9a-f]{40}", sha):
|
||||
raise ValueError("invalid commit SHA in history")
|
||||
cells = [f'<td><a href="https://gitea.bluefla.re/blueflare/alkane-toolkit/commit/{sha}">{sha[:10]}</a></td>',
|
||||
f'<td>{html.escape(entry["timestamp"])}</td>']
|
||||
values = valid_benches(entry["metrics"])
|
||||
host = entry["metrics"].get("bench_host", {})
|
||||
host_label = host.get("cpu_model", "unknown") if isinstance(host, dict) else "unknown"
|
||||
cells.append(f'<td>{html.escape(str(host_label))}</td>')
|
||||
context = entry["metrics"].get("bench_context", {})
|
||||
if isinstance(context, dict) and context.get("schema") == 1:
|
||||
context_label = "same inputs" if context.get("same_inputs") else "changed inputs"
|
||||
else:
|
||||
context_label = "unclassified"
|
||||
cells.append(f'<td>{context_label}</td>')
|
||||
cells += [f'<td>{values[name]:.3f}</td>' if name in values else '<td>new</td>'
|
||||
for name in names]
|
||||
rows.append("<tr>" + "".join(cells) + "</tr>")
|
||||
headings = "".join(f"<th scope=\"col\">{html.escape(name)} (ns/op)</th>" for name in names)
|
||||
return ("<!doctype html>\n<html lang=\"en\"><head><meta charset=\"utf-8\">"
|
||||
"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"
|
||||
"<title>Alkane Toolkit benchmarks</title><style>"
|
||||
"body{font:16px system-ui,sans-serif;max-width:1100px;margin:2rem auto;padding:0 1rem;color:#17212b}"
|
||||
"h1{font-size:2rem}p{max-width:70ch;line-height:1.5}"
|
||||
"table{border-collapse:collapse;min-width:100%}th,td{padding:.5rem;border-bottom:1px solid #ccd3da;text-align:right}"
|
||||
"th:first-child,td:first-child{text-align:left}th:nth-child(2),td:nth-child(2){text-align:left}"
|
||||
"th{background:#edf2f6;position:sticky;top:0}a{color:#145a9a}"
|
||||
".scroll{overflow-x:auto}small{color:#55616d}</style></head><body>"
|
||||
"<h1>Alkane Toolkit microbenchmarks</h1>"
|
||||
"<p>Measured on main CI. Lower ns/op is faster. Runner load affects results. "
|
||||
"Same inputs marks a repeat of the benchmark source and build settings. "
|
||||
"A new benchmark has no earlier comparison.</p>"
|
||||
f"<p><small>Most recent {len(entries)} measured commits. Full per-commit JSON is retained "
|
||||
"in the private ci-badges branch.</small></p>"
|
||||
f"<div class=\"scroll\"><table><thead><tr><th scope=\"col\">Commit</th>"
|
||||
f"<th scope=\"col\">UTC</th><th scope=\"col\">CPU</th>"
|
||||
f"<th scope=\"col\">Inputs</th>{headings}</tr></thead><tbody>{''.join(rows)}"
|
||||
"</tbody></table></div></body></html>\n")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("action", choices=("prepare", "publish", "render"))
|
||||
args = parser.parse_args()
|
||||
api = os.environ["GITHUB_API_URL"].rstrip("/")
|
||||
repo = os.environ["GITHUB_REPOSITORY"]
|
||||
token = os.environ["GITEA_TOKEN"]
|
||||
sha = os.environ["GITHUB_SHA"]
|
||||
index = read_artifact(api, repo, token, INDEX) or {"schema": 1, "entries": []}
|
||||
if index.get("schema") != 1 or not isinstance(index.get("entries"), list):
|
||||
raise ValueError("unsupported benchmark history index")
|
||||
if args.action == "prepare":
|
||||
entry = previous_entry(index, sha, host=benchmark_host())
|
||||
baseline = valid_benches(entry["metrics"]) if entry else {}
|
||||
same_inputs = same_benchmark_inputs(entry["sha"], sha) if entry else False
|
||||
Path("badges").mkdir(exist_ok=True)
|
||||
Path("badges/bench-baseline.json").write_text(json.dumps(baseline) + "\n")
|
||||
Path("badges/bench-context.json").write_text(
|
||||
json.dumps({"schema": 1, "same_inputs": same_inputs,
|
||||
"baseline_sha": entry["sha"] if entry else None}) + "\n")
|
||||
print(f"Loaded {len(baseline)} prior benchmark measurements; "
|
||||
f"same inputs: {str(same_inputs).lower()}")
|
||||
return
|
||||
if args.action == "render":
|
||||
if not index["entries"]:
|
||||
raise ValueError("no measured benchmark history to publish")
|
||||
Path("bench-site").mkdir(exist_ok=True)
|
||||
Path("bench-site/index.html").write_text(render_html(index))
|
||||
print(f"Rendered {len(index['entries'])} measured benchmark commits")
|
||||
return
|
||||
context = json.loads(Path("badges/bench-context.json").read_text())
|
||||
if (not isinstance(context, dict) or context.get("schema") != 1 or
|
||||
not isinstance(context.get("same_inputs"), bool)):
|
||||
raise ValueError("invalid benchmark comparison context")
|
||||
metrics = {"bench_ns_per_op": bench_badge.require_suite(bench_badge.parse_bench(
|
||||
Path("bench-current.txt").read_text())), "bench_host": benchmark_host(),
|
||||
"bench_context": context}
|
||||
timestamp = datetime.now(timezone.utc).isoformat(timespec="seconds")
|
||||
index = updated_index(index, sha, metrics, timestamp)
|
||||
record = json.dumps(index["entries"][-1], indent=2).encode() + b"\n"
|
||||
publish_file(api, repo, f"history/{sha}.json", record, token,
|
||||
"Record Alkane Toolkit benchmarks", max_bytes=1_048_576)
|
||||
publish_file(api, repo, INDEX, json.dumps(index, indent=2).encode() + b"\n",
|
||||
token, "Update Alkane Toolkit benchmark index", max_bytes=1_048_576)
|
||||
Path("bench-site").mkdir(exist_ok=True)
|
||||
Path("bench-site/index.html").write_text(render_html(index))
|
||||
print(f"Recorded benchmarks for {sha[:10]}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,19 @@
|
||||
#!/usr/bin/env python3
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""Identify the CPU class used for comparable CI microbenchmark baselines."""
|
||||
|
||||
import os
|
||||
import platform
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def identity() -> dict[str, object]:
|
||||
model = "unknown"
|
||||
cpuinfo = Path("/proc/cpuinfo")
|
||||
if cpuinfo.is_file():
|
||||
for line in cpuinfo.read_text(errors="replace").splitlines():
|
||||
if line.startswith(("model name\t", "Hardware\t", "Processor\t")):
|
||||
model = line.partition(":")[2].strip()[:160]
|
||||
break
|
||||
return {"architecture": platform.machine(), "cpu_model": model,
|
||||
"logical_cpus": os.cpu_count() or 1}
|
||||
@@ -0,0 +1,293 @@
|
||||
#!/usr/bin/env python3
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""Build the Alkane Toolkit Pages site (served from this repo's gh-pages).
|
||||
|
||||
build-pages.py --out site [--manual docs/manual] [--quality quality.json]
|
||||
[--bench bench-site/index.html] [--sha SHA]
|
||||
|
||||
Pages:
|
||||
/ overview and downloads
|
||||
/manual/ the user manual, one page per docs/manual/NN-name.md chapter
|
||||
/quality/ code-quality dashboard from the measured quality.json that main
|
||||
CI publishes next to the badges (ci/gen-badges.py)
|
||||
/bench/ the measured benchmark history (ci/bench_history.py render)
|
||||
|
||||
Every value taken from CI data is HTML-escaped; the manual is Markdown written
|
||||
in this repository.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import html
|
||||
import json
|
||||
import re
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
|
||||
import markdown
|
||||
|
||||
REPO = "https://gitea.bluefla.re/blueflare/alkane-toolkit"
|
||||
RELEASES = REPO + "/releases"
|
||||
SECTIONS = (("", "Overview"), ("manual/", "Manual"), ("quality/", "Code quality"),
|
||||
("bench/", "Benchmarks"))
|
||||
|
||||
CSS = """
|
||||
:root { --bg:#ffffff; --fg:#1f2328; --muted:#59636e; --line:#d1d9e0; --card:#f6f8fa;
|
||||
--accent:#d9822b; --link:#0969da; --good:#1a7f37; --warn:#9a6700; --bad:#cf222e; }
|
||||
@media (prefers-color-scheme: dark) { :root { --bg:#0d1117; --fg:#e6edf3; --muted:#9198a1;
|
||||
--line:#30363d; --card:#151b23; --link:#4493f8; --good:#3fb950; --warn:#d29922; --bad:#f85149; } }
|
||||
* { box-sizing: border-box; }
|
||||
body { margin:0; background:var(--bg); color:var(--fg);
|
||||
font:16px/1.55 system-ui,-apple-system,"Segoe UI",Roboto,Ubuntu,sans-serif; }
|
||||
a { color:var(--link); text-decoration:none; } a:hover { text-decoration:underline; }
|
||||
header { border-bottom:1px solid var(--line); }
|
||||
header .bar, main { max-width:1040px; margin:0 auto; padding:0 16px; }
|
||||
header .bar { display:flex; align-items:center; gap:24px; height:56px; flex-wrap:wrap; }
|
||||
header .brand { font-weight:700; color:var(--fg); display:flex; gap:8px; align-items:center; }
|
||||
header .brand i { width:20px; height:20px; border-radius:5px; background:var(--accent); display:inline-block; }
|
||||
header nav { display:flex; gap:18px; flex-wrap:wrap; }
|
||||
header nav a { color:var(--muted); } header nav a.on { color:var(--fg); font-weight:600; }
|
||||
main { padding:28px 16px 64px; }
|
||||
h1 { font-size:1.9rem; margin:.2em 0 .4em; } h2 { margin-top:1.8em; border-bottom:1px solid var(--line); padding-bottom:.25em; }
|
||||
code { background:var(--card); border:1px solid var(--line); border-radius:5px; padding:.1em .35em; font-size:.92em; }
|
||||
pre { background:var(--card); border:1px solid var(--line); border-radius:8px; padding:12px; overflow:auto; }
|
||||
pre code { border:0; padding:0; }
|
||||
table { border-collapse:collapse; width:100%; margin:1em 0; display:block; overflow-x:auto; }
|
||||
th, td { border:1px solid var(--line); padding:6px 10px; text-align:left; vertical-align:top; }
|
||||
th { background:var(--card); }
|
||||
td.num, th.num { text-align:right; font-variant-numeric:tabular-nums; white-space:nowrap; }
|
||||
.cards { display:grid; grid-template-columns:repeat(auto-fill,minmax(180px,1fr)); gap:12px; margin:1.2em 0; }
|
||||
.card { background:var(--card); border:1px solid var(--line); border-radius:10px; padding:14px; }
|
||||
.card b { display:block; font-size:1.6rem; } .card span { color:var(--muted); font-size:.9rem; }
|
||||
.good { color:var(--good); } .warn { color:var(--warn); } .bad { color:var(--bad); }
|
||||
.bar-cell { min-width:120px; } .meter { height:8px; background:var(--line); border-radius:4px; overflow:hidden; }
|
||||
.meter i { display:block; height:100%; }
|
||||
.layout { display:grid; grid-template-columns:220px 1fr; gap:32px; }
|
||||
.layout aside ol { padding-left:1.2em; margin:0; } .layout aside li { margin:.3em 0; }
|
||||
.layout aside a.on { font-weight:600; color:var(--fg); }
|
||||
@media (max-width:760px) { .layout { grid-template-columns:1fr; } }
|
||||
.buttons a { display:inline-block; margin:4px 8px 4px 0; padding:8px 14px; border-radius:8px;
|
||||
border:1px solid var(--line); background:var(--card); color:var(--fg); }
|
||||
.buttons a.primary { background:var(--accent); border-color:var(--accent); color:#fff; }
|
||||
footer { color:var(--muted); font-size:.85rem; margin-top:48px; }
|
||||
"""
|
||||
|
||||
|
||||
def page(title: str, body: str, depth: int, current: str, sha: str) -> str:
|
||||
up = "../" * depth
|
||||
nav = "".join(
|
||||
f'<a href="{up}{path}"{" class=on" if path == current else ""}>{label}</a>'
|
||||
for path, label in SECTIONS)
|
||||
built = f"Built from {html.escape(sha[:10])}. " if sha else ""
|
||||
return f"""<!doctype html>
|
||||
<html lang="en"><head><meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>{html.escape(title)} - Alkane Toolkit</title>
|
||||
<link rel="stylesheet" href="{up}style.css"></head>
|
||||
<body><header><div class="bar"><a class="brand" href="{up}"><i></i>Alkane Toolkit</a>
|
||||
<nav>{nav}<a href="{RELEASES}">Downloads</a><a href="{REPO}">Source</a></nav></div></header>
|
||||
<main>{body}
|
||||
<footer>{built}Alkane Toolkit is MIT licensed.</footer></main></body></html>
|
||||
"""
|
||||
|
||||
|
||||
def write(out: Path, rel: str, text: str) -> None:
|
||||
path = out / rel
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text(text, encoding="utf-8")
|
||||
|
||||
|
||||
# ---- manual -----------------------------------------------------------------
|
||||
|
||||
def chapters(manual: Path) -> list[tuple[str, str, str]]:
|
||||
"""(slug, title, markdown) for docs/manual/NN-slug.md, in file-name order."""
|
||||
out = []
|
||||
for path in sorted(manual.glob("[0-9][0-9]-*.md")):
|
||||
text = path.read_text(encoding="utf-8")
|
||||
m = re.search(r"^# (.+)$", text, re.M)
|
||||
title = m.group(1).strip() if m else path.stem
|
||||
out.append((path.stem[3:], title, text))
|
||||
return out
|
||||
|
||||
|
||||
def build_manual(out: Path, manual: Path, sha: str) -> None:
|
||||
items = chapters(manual)
|
||||
if not items:
|
||||
raise SystemExit(f"no manual chapters in {manual}")
|
||||
|
||||
def toc(current: str) -> str:
|
||||
links = "".join(
|
||||
f'<li><a href="{slug}.html"{" class=on" if slug == current else ""}>'
|
||||
f"{html.escape(title)}</a></li>" for slug, title, _ in items)
|
||||
return f"<aside><ol>{links}</ol></aside>"
|
||||
|
||||
for slug, title, text in items:
|
||||
# Chapters link to each other as NN-name.md in the repo; on the site
|
||||
# they are name.html.
|
||||
text = re.sub(r"\]\((?:\./)?[0-9][0-9]-([a-z0-9-]+)\.md(#[^)]*)?\)", r"](\1.html\2)", text)
|
||||
body_html = markdown.markdown(text, extensions=["tables", "fenced_code", "toc"])
|
||||
write(out, f"manual/{slug}.html",
|
||||
page(title, f'<div class="layout">{toc(slug)}<article>{body_html}</article></div>',
|
||||
1, "manual/", sha))
|
||||
first = items[0][0]
|
||||
listing = "".join(f'<li><a href="{s}.html">{html.escape(t)}</a></li>' for s, t, _ in items)
|
||||
write(out, "manual/index.html",
|
||||
page("Manual", f"<h1>Manual</h1><p>Start with "
|
||||
f'<a href="{first}.html">{html.escape(items[0][1])}</a>.</p><ol>{listing}</ol>',
|
||||
1, "manual/", sha))
|
||||
|
||||
|
||||
# ---- quality dashboard ---------------------------------------------------------
|
||||
|
||||
def pct_class(pct: float | None) -> str:
|
||||
if pct is None:
|
||||
return ""
|
||||
return "good" if pct >= 80 else "warn" if pct >= 40 else "bad"
|
||||
|
||||
|
||||
def meter(pct: float) -> str:
|
||||
colour = {"good": "var(--good)", "warn": "var(--warn)", "bad": "var(--bad)"}[pct_class(pct)]
|
||||
return f'<div class="meter"><i style="width:{max(0.0, min(pct, 100.0)):.1f}%;background:{colour}"></i></div>'
|
||||
|
||||
|
||||
def build_quality(out: Path, quality: dict | None, sha: str) -> None:
|
||||
e = html.escape
|
||||
if not quality:
|
||||
write(out, "quality/index.html", page(
|
||||
"Code quality", "<h1>Code quality</h1><p>No measurement has been published yet; "
|
||||
"the next main build will fill this page.</p>", 1, "quality/", sha))
|
||||
return
|
||||
grade = str(quality.get("grade", "pending"))
|
||||
cov = quality.get("coverage_pct")
|
||||
bench_cov = quality.get("bench_coverage_pct")
|
||||
delta = quality.get("bench_delta_pct")
|
||||
|
||||
def card(value: str, label: str, cls: str = "") -> str:
|
||||
return f'<div class="card"><b class="{cls}">{value}</b><span>{label}</span></div>'
|
||||
|
||||
cards = "".join([
|
||||
card(e(grade), "Quality grade", {"A": "good", "B": "good", "C": "warn"}.get(grade, "bad")),
|
||||
card(e(str(quality.get("tests_passed", "-"))), "Tests passing", "good"),
|
||||
card(f"{cov:.1f}%" if cov is not None else "-", "Line coverage", pct_class(cov)),
|
||||
card(f"{bench_cov:.1f}%" if bench_cov is not None else "-", "Benchmark coverage", pct_class(bench_cov)),
|
||||
card(e(str(quality.get("clippy") or "-")), "Clippy (warnings are errors)",
|
||||
"good" if quality.get("clippy") == "clean" else "bad"),
|
||||
card(e(str(quality.get("rustfmt") or "-")), "rustfmt", "good" if quality.get("rustfmt") == "clean" else ""),
|
||||
card(f"{quality.get('loc', 0):,}", "Lines of Rust"),
|
||||
card(e(str(quality.get("dependencies", "-"))), "Crates in Cargo.lock"),
|
||||
card((f"{'+' if delta > 0 else ''}{delta:.1f}%" if delta is not None else "-"),
|
||||
"Benchmarks vs previous" + (" (same inputs)" if quality.get("bench_same_inputs") else ""),
|
||||
"" if delta is None else "good" if delta >= 2 else "" if delta > -2 else "warn"),
|
||||
])
|
||||
bands = ", ".join(f"{e(str(letter))} at {floor:.0f}% or more"
|
||||
for floor, letter in quality.get("grade_bands", []))
|
||||
grade_rule = (f"<p>The grade is F if Clippy or rustfmt fails on main. Otherwise it is the "
|
||||
f"line-coverage band: {bands}, and F below that. The coverage badge uses the "
|
||||
f"same bands for its colour, so the two never disagree.</p>")
|
||||
|
||||
crates = quality.get("coverage_by_crate", {})
|
||||
loc = quality.get("loc_by_crate", {})
|
||||
rows = []
|
||||
for name in sorted(set(crates) | set(loc)):
|
||||
c = crates.get(name)
|
||||
if c and c["lines"]:
|
||||
pct = 100.0 * (c["lines"] - c["missed"]) / c["lines"]
|
||||
cov_cells = (f'<td class="num {pct_class(pct)}">{pct:.1f}%</td>'
|
||||
f'<td class="bar-cell">{meter(pct)}</td>'
|
||||
f'<td class="num">{c["lines"] - c["missed"]:,} / {c["lines"]:,}</td>')
|
||||
else:
|
||||
cov_cells = '<td class="num">-</td><td></td><td class="num">-</td>'
|
||||
rows.append(f"<tr><td>{e(name)}</td>{cov_cells}<td class='num'>{loc.get(name, 0):,}</td></tr>")
|
||||
crate_table = ("<table><tr><th>Crate</th><th class=num>Line coverage</th><th></th>"
|
||||
"<th class=num>Covered lines</th><th class=num>Lines of Rust</th></tr>"
|
||||
+ "".join(rows) + "</table>")
|
||||
|
||||
files = sorted(quality.get("coverage_by_file", []),
|
||||
key=lambda r: (r["lines"] - r["missed"]) / r["lines"] if r["lines"] else 1)
|
||||
file_rows = "".join(
|
||||
f"<tr><td><code>{e(r['file'])}</code></td>"
|
||||
f"<td class='num {pct_class(100.0 * (r['lines'] - r['missed']) / r['lines'])}'>"
|
||||
f"{100.0 * (r['lines'] - r['missed']) / r['lines']:.1f}%</td>"
|
||||
f"<td class='num'>{r['missed']:,}</td><td class='num'>{r['lines']:,}</td></tr>"
|
||||
for r in files if r["lines"])
|
||||
file_table = ("<table><tr><th>File (least covered first)</th><th class=num>Coverage</th>"
|
||||
"<th class=num>Uncovered lines</th><th class=num>Lines</th></tr>"
|
||||
+ file_rows + "</table>") if file_rows else ""
|
||||
|
||||
measured = str(quality.get("sha", ""))[:10]
|
||||
of_build = f" (this page: <code>{e(measured)}</code>)" if measured else ""
|
||||
body = f"""<h1>Code quality</h1>
|
||||
<p>Measured by main CI on every build{of_build}:
|
||||
unit tests, line coverage (unit tests plus the headless UI self-test), Clippy with
|
||||
warnings as errors, and rustfmt. Windows builds run the same tests under Wine and
|
||||
on real Windows 11.</p>
|
||||
<div class="cards">{cards}</div>
|
||||
<h2 id="grade">How the grade is set</h2>{grade_rule}
|
||||
<h2 id="coverage">Coverage by crate</h2>{crate_table}
|
||||
<h2 id="files">Coverage by file</h2>{file_table}
|
||||
<p>The dependency count is every package in <code>Cargo.lock</code>, direct and
|
||||
transitive. Lines of Rust count every <code>.rs</code> line under <code>crates/</code>,
|
||||
tests included.</p>"""
|
||||
write(out, "quality/index.html", page("Code quality", body, 1, "quality/", sha))
|
||||
|
||||
|
||||
# ---- benchmarks + overview -------------------------------------------------------
|
||||
|
||||
def build_bench(out: Path, bench: Path | None, sha: str) -> None:
|
||||
if bench is None or not bench.is_file():
|
||||
write(out, "bench/index.html", page(
|
||||
"Benchmarks", "<h1>Benchmarks</h1><p>No benchmark history yet.</p>", 1, "bench/", sha))
|
||||
return
|
||||
rendered = bench.read_text(encoding="utf-8")
|
||||
# The history renderer emits a complete document; keep its body (and its
|
||||
# own styles) under the site's header.
|
||||
styles = "".join(re.findall(r"<style[^>]*>.*?</style>", rendered, re.S | re.I))
|
||||
m = re.search(r"<body[^>]*>(.*)</body>", rendered, re.S | re.I)
|
||||
inner = m.group(1) if m else rendered
|
||||
text = page("Benchmarks", inner, 1, "bench/", sha)
|
||||
write(out, "bench/index.html", text.replace("</head>", styles + "</head>", 1))
|
||||
|
||||
|
||||
def build_index(out: Path, quality: dict | None, sha: str) -> None:
|
||||
grade = html.escape(str((quality or {}).get("grade", "pending")))
|
||||
body = f"""<h1>Alkane Toolkit</h1>
|
||||
<p>Desktop discovery and monitoring for ASIC miners on your network: find every
|
||||
miner in your IP ranges, see hashrate, power, temperatures, fans and hashboard
|
||||
health at a glance, and run the controls each firmware supports.</p>
|
||||
<div class="buttons"><a class="primary" href="{RELEASES}">Download for Windows or Linux</a>
|
||||
<a href="manual/">Read the manual</a><a href="quality/">Code quality: {grade}</a>
|
||||
<a href="bench/">Benchmarks</a></div>
|
||||
<h2>Platforms</h2>
|
||||
<ul><li><b>Windows 10 and 11</b> (x86_64): one signed, self-contained <code>alkane-toolkit.exe</code>.</li>
|
||||
<li><b>Linux</b> (x86_64, GTK 3): one binary.</li></ul>
|
||||
<p>Every release is built, tested and signed by CI from the public source.</p>"""
|
||||
write(out, "index.html", page("Overview", body, 0, "", sha))
|
||||
|
||||
|
||||
def main() -> None:
|
||||
ap = argparse.ArgumentParser(description=__doc__,
|
||||
formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||
ap.add_argument("--out", required=True)
|
||||
ap.add_argument("--manual", default="docs/manual")
|
||||
ap.add_argument("--quality", default=None)
|
||||
ap.add_argument("--bench", default=None)
|
||||
ap.add_argument("--sha", default="")
|
||||
args = ap.parse_args()
|
||||
out = Path(args.out)
|
||||
if out.exists():
|
||||
shutil.rmtree(out)
|
||||
out.mkdir(parents=True)
|
||||
quality = None
|
||||
if args.quality and Path(args.quality).is_file():
|
||||
quality = json.loads(Path(args.quality).read_text())
|
||||
if not isinstance(quality, dict) or quality.get("schema") != 1:
|
||||
raise SystemExit("unsupported quality.json")
|
||||
(out / "style.css").write_text(CSS.strip() + "\n", encoding="utf-8")
|
||||
build_index(out, quality, args.sha)
|
||||
build_manual(out, Path(args.manual), args.sha)
|
||||
build_quality(out, quality, args.sha)
|
||||
build_bench(out, Path(args.bench) if args.bench else None, args.sha)
|
||||
print(f"built {sum(1 for _ in out.rglob('*.html'))} pages in {out}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,68 @@
|
||||
#!/usr/bin/env python3
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""Keep Toolkit CI on the verified Alkane runner image."""
|
||||
|
||||
from pathlib import Path
|
||||
import re
|
||||
import sys
|
||||
|
||||
import yaml
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
IMAGE = (ROOT / "ci/runner-image.txt").read_text().strip()
|
||||
|
||||
|
||||
def main() -> int:
|
||||
if not re.fullmatch(
|
||||
r"gitea\.bluefla\.re/blueflare/alkane-ci@sha256:[0-9a-f]{64}", IMAGE
|
||||
):
|
||||
print("Runner image must be pinned by Gitea registry digest", file=sys.stderr)
|
||||
return 1
|
||||
for path in sorted((ROOT / ".gitea/workflows").glob("*.yml")):
|
||||
workflow = yaml.load(path.read_text(), Loader=yaml.BaseLoader)
|
||||
for name, job in workflow.get("jobs", {}).items():
|
||||
if (job.get("runs-on") in {"alkane-fast", "alkane-pages"}
|
||||
and "secrets." in str(job)):
|
||||
print(f"{path.name}: {name} exposes a secret to the fast runner", file=sys.stderr)
|
||||
return 1
|
||||
if job.get("runs-on") not in {"alkane-ci", "alkane-fast", "alkane-verify"}:
|
||||
continue
|
||||
if job.get("container", {}).get("image") != IMAGE:
|
||||
print(f"{path.name}: {name} has a different runner image", file=sys.stderr)
|
||||
return 1
|
||||
pages = yaml.load(
|
||||
(ROOT / ".gitea/workflows/pages.yml").read_text(),
|
||||
Loader=yaml.BaseLoader,
|
||||
)
|
||||
if (pages["on"]["workflow_run"]["workflows"] != ["build-test-release"]
|
||||
or pages["jobs"]["publish"].get("runs-on") != "alkane-verify"):
|
||||
print("Pages must follow main CI on the trusted verifier", file=sys.stderr)
|
||||
return 1
|
||||
release = yaml.load(
|
||||
(ROOT / ".gitea/workflows/release.yml").read_text(),
|
||||
Loader=yaml.BaseLoader,
|
||||
)
|
||||
if release["jobs"]["release"].get("runs-on") != "alkane-ci":
|
||||
print("Main release must use the dedicated Alkane build pool", file=sys.stderr)
|
||||
return 1
|
||||
if any("ci/publish-pages.py" in step.get("run", "")
|
||||
for step in release["jobs"]["release"]["steps"]):
|
||||
print("Main release must not publish Pages from GARM", file=sys.stderr)
|
||||
return 1
|
||||
source = yaml.load(
|
||||
(ROOT / ".gitea/workflows/source.yml").read_text(),
|
||||
Loader=yaml.BaseLoader,
|
||||
)
|
||||
triggers = source["on"]
|
||||
if (triggers.get("pull_request", {}).get("branches") != ["next"]
|
||||
or "push" in triggers or "workflow_dispatch" not in triggers):
|
||||
print("Source checks must not duplicate main pull request verification",
|
||||
file=sys.stderr)
|
||||
return 1
|
||||
print("Toolkit runner image pins: PASS")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
@@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env bash
|
||||
# SPDX-License-Identifier: MIT
|
||||
# Avoid a runner-local cache download when the pinned image has this source.
|
||||
# check-wx-image-prefix.sh [linux|windows]
|
||||
set -euo pipefail
|
||||
|
||||
case "${1:-linux}" in
|
||||
linux) prefix="${WX_PREFIX:?WX_PREFIX is required}"; library=lib/libwx_gtk3u-3.3.a ;;
|
||||
windows) prefix="${WX_WINDOWS_PREFIX:?WX_WINDOWS_PREFIX is required}"
|
||||
library=lib/libwx_mswu-3.3-x86_64-w64-mingw32.a ;;
|
||||
*) echo "unknown wx platform: $1" >&2; exit 2 ;;
|
||||
esac
|
||||
expected="$(awk '$1 == "sha256:" {print $2}' thirdparty/wxwidgets/VERSION)"
|
||||
[[ "$expected" =~ ^[0-9a-f]{64}$ ]]
|
||||
available=false
|
||||
if [ ! -L "$prefix" ] && [ ! -L "$prefix/.alkane-source-sha256" ] &&
|
||||
[ -x "$prefix/bin/wx-config" ] &&
|
||||
[ -s "$prefix/$library" ] &&
|
||||
[ "$(cat "$prefix/.alkane-source-sha256" 2>/dev/null || true)" = "$expected" ]; then
|
||||
available=true
|
||||
fi
|
||||
printf 'available=%s\n' "$available" >> "${GITHUB_OUTPUT:?GITHUB_OUTPUT is required}"
|
||||
@@ -0,0 +1,53 @@
|
||||
#!/usr/bin/env python3
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""Remove generated Toolkit CI work after the release asset is uploaded."""
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import sys
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
DIRECTORIES = ("target", "dist", "coverage", "bench-site", "site")
|
||||
FILES = ("bench-current.txt", "coverage-summary.txt", "quality.json")
|
||||
BADGE_FILES = ("bench-baseline.json", "bench-context.json", "quality.json")
|
||||
|
||||
|
||||
def cleanup() -> None:
|
||||
if not os.environ.get("GITHUB_RUN_ID"):
|
||||
raise ValueError("cleanup is restricted to CI runs")
|
||||
for name in DIRECTORIES:
|
||||
path = ROOT / name
|
||||
if path.is_symlink() or path.parent.resolve() != ROOT.resolve():
|
||||
raise ValueError(f"unsafe generated directory: {path}")
|
||||
if path.is_dir():
|
||||
shutil.rmtree(path)
|
||||
print(f"Removed generated directory: {name}")
|
||||
elif path.exists():
|
||||
raise ValueError(f"expected a generated directory: {path}")
|
||||
for name in FILES:
|
||||
path = ROOT / name
|
||||
if path.is_symlink() or path.parent.resolve() != ROOT.resolve():
|
||||
raise ValueError(f"unsafe generated file: {path}")
|
||||
if path.is_file():
|
||||
path.unlink()
|
||||
print(f"Removed generated file: {name}")
|
||||
badge_dir = ROOT / "badges"
|
||||
if badge_dir.is_symlink():
|
||||
raise ValueError("unsafe generated badge directory")
|
||||
for name in BADGE_FILES:
|
||||
path = badge_dir / name
|
||||
if path.is_symlink() or path.parent.resolve() != badge_dir.resolve():
|
||||
raise ValueError(f"unsafe generated badge file: {path}")
|
||||
if path.is_file():
|
||||
path.unlink()
|
||||
print(f"Removed generated badge file: {name}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
cleanup()
|
||||
except (OSError, ValueError) as error:
|
||||
print(f"ERROR: Toolkit cleanup: {error}", file=sys.stderr)
|
||||
raise SystemExit(1)
|
||||
@@ -0,0 +1,15 @@
|
||||
#!/usr/bin/env bash
|
||||
# Convert the next final version into a monotonically numbered prerelease.
|
||||
set -euo pipefail
|
||||
result="$(bash ci/compute-version.sh)"
|
||||
release="$(printf '%s\n' "$result" | sed -n 's/^release=//p')"
|
||||
if [ "$release" != true ]; then
|
||||
echo "release=false"
|
||||
exit 0
|
||||
fi
|
||||
version="$(printf '%s\n' "$result" | sed -n 's/^version=//p')"
|
||||
last="$(git tag -l "v${version}-rc.*" | sed -n "s/^v${version//./\.}-rc\.\([0-9][0-9]*\)$/\1/p" | sort -n | tail -1)"
|
||||
next=$(( ${last:-0} + 1 ))
|
||||
echo "release=true"
|
||||
echo "version=${version}-rc.${next}"
|
||||
echo "tag=v${version}-rc.${next}"
|
||||
@@ -2,9 +2,10 @@
|
||||
# Compute the next release version for alkane-toolkit from the latest release
|
||||
# tag and the paths changed since it. Version scheme X.Y.Z:
|
||||
# X - major, bumped manually (ci/version-x.txt); a bump resets Y.Z to 0.0
|
||||
# Y - backend-logic changes (core/discovery/access/flash/artifacts/cli,
|
||||
# scan+model+main in the GUI crate, build/CI config); resets Z to 0
|
||||
# Z - UI changes (the GUI view + fixtures + the wx-rs widget bridge)
|
||||
# Y - backend-logic changes (the alkane-* crates and any GUI crate source
|
||||
# other than the view files below, plus vendored deps and build/CI config);
|
||||
# resets Z to 0
|
||||
# Z - UI changes (the GUI view: ui.rs + fixtures.rs + bin/ + the wx-rs bridge)
|
||||
# When both backend and UI changed, backend wins (bump Y). Docs-only changes
|
||||
# produce no release.
|
||||
#
|
||||
@@ -17,7 +18,7 @@ X=0
|
||||
[ -f "$x_file" ] && X=$(tr -dc '0-9' < "$x_file")
|
||||
X=${X:-0}
|
||||
|
||||
last=$(git tag -l 'v*.*.*' | sort -V | tail -1 || true)
|
||||
last=$(git tag -l 'v*.*.*' | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | sort -V | tail -1 || true)
|
||||
if [ -n "$last" ]; then
|
||||
v=${last#v}; lx=${v%%.*}; rest=${v#*.}; ly=${rest%%.*}; lz=${rest##*.}
|
||||
changed=$(git diff --name-only "$last"..HEAD)
|
||||
@@ -35,11 +36,17 @@ crates/alkane-gui/src/bin/*)
|
||||
ui=true ;;
|
||||
crates/alkane-core/*|crates/alkane-discovery/*|crates/alkane-access/*|\
|
||||
crates/alkane-flash/*|crates/alkane-artifacts/*|crates/alkane-cli/*|\
|
||||
crates/alkane-gui/src/scan.rs|crates/alkane-gui/src/model.rs|\
|
||||
crates/alkane-gui/src/main.rs|Cargo.toml|Cargo.lock|.gitea/*|ci/*|scripts/*|\
|
||||
crates/alkane-gui/src/*|crates/alkane-gui/Cargo.toml|crates/alkane-gui/build.rs|\
|
||||
crates/alkane-gui/windows/*|crates/alkane-gui/assets/*|\
|
||||
Cargo.toml|Cargo.lock|.gitea/*|ci/*|scripts/*|\
|
||||
vendor/*|thirdparty/asic-rs/*)
|
||||
# Any other GUI source file (naming, scan, model, pool_prefs, restore,
|
||||
# access_probe, credentials, ...) is backend logic, as is how the binary
|
||||
# is built and packaged (build.rs, the Windows manifest/icon/version
|
||||
# resources, embedded assets). ui.rs/fixtures.rs/bin
|
||||
# match the UI arm above first, so they still bump Z, not Y.
|
||||
backend=true ;;
|
||||
*) : ;; # docs, HANDOFF, README, thirdparty/wxwidgets tree: no bump
|
||||
*) : ;; # docs, README, thirdparty/wxwidgets tree: no bump
|
||||
esac
|
||||
done <<< "$changed"
|
||||
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
#!/usr/bin/env bash
|
||||
# SPDX-License-Identifier: MIT
|
||||
# Reuse Rust compiler results on persistent CI runners.
|
||||
set -euo pipefail
|
||||
|
||||
[ "$(sccache --version)" = 'sccache 0.18.0' ] || {
|
||||
echo 'CI image has an unexpected sccache version' >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
toolcache="${RUNNER_TOOL_CACHE:-}"
|
||||
if [ -z "$toolcache" ] || [ -L "$toolcache" ] ||
|
||||
[ ! -d "$toolcache" ] || [ ! -w "$toolcache" ]; then
|
||||
echo 'Persistent runner tool cache is unavailable; compiling without sccache'
|
||||
exit 0
|
||||
fi
|
||||
|
||||
cache_dir="$toolcache/alkane-toolkit-rust-sccache"
|
||||
if [ -L "$cache_dir" ]; then
|
||||
echo 'Rust cache path is a symlink' >&2
|
||||
exit 1
|
||||
fi
|
||||
umask 077
|
||||
mkdir -p "$cache_dir"
|
||||
if [ ! -d "$cache_dir" ] || [ ! -w "$cache_dir" ]; then
|
||||
echo 'Rust cache directory is unavailable' >&2
|
||||
exit 1
|
||||
fi
|
||||
chmod 0700 "$cache_dir"
|
||||
|
||||
{
|
||||
printf 'RUSTC_WRAPPER=%s\n' "$(command -v sccache)"
|
||||
printf 'SCCACHE_DIR=%s\n' "$cache_dir"
|
||||
printf 'SCCACHE_CACHE_SIZE=2G\n'
|
||||
printf 'CARGO_INCREMENTAL=0\n'
|
||||
} >> "${GITHUB_ENV:?GITHUB_ENV is required}"
|
||||
echo 'Bounded Rust compiler cache configured for Alkane Toolkit'
|
||||
@@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env bash
|
||||
# Measure line coverage from BOTH the unit tests AND the headless UI self-test.
|
||||
#
|
||||
# The self-test (ALKANE_SELFTEST=1) builds the real wxWidgets table, renders every
|
||||
# fixture through the real widgets, and checks each cell against the model.
|
||||
# It covers the fixture-rendering path, while interactive event handlers need
|
||||
# separate tests. Because it runs as a separate binary invocation, a plain
|
||||
# `cargo llvm-cov` (which only runs the #[test]s) never counts it. This
|
||||
# combines both runs' coverage.
|
||||
#
|
||||
# Usage: ci/coverage.sh [extra `cargo llvm-cov report` args, e.g. --summary-only]
|
||||
set -euo pipefail
|
||||
|
||||
export WX_RS_WXCONFIG="${WX_RS_WXCONFIG:-$HOME/.cache/wx-3.3.3-linux/bin/wx-config}"
|
||||
|
||||
# The dependency tree is fully vendored, so the instrumented builds run offline by
|
||||
# default; override with CARGO_LLVM_COV_FLAGS if you need something else.
|
||||
read -ra CARGO_FLAGS <<<"${CARGO_LLVM_COV_FLAGS:---offline}"
|
||||
|
||||
# A display is required for the self-test; skipping it would publish an
|
||||
# incomplete coverage number as if the measured test set had run.
|
||||
if ! command -v xvfb-run >/dev/null 2>&1; then
|
||||
echo "coverage.sh: xvfb-run is required for the UI self-test" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
cargo llvm-cov clean --workspace
|
||||
|
||||
# 1. Unit tests for the GUI and all first-party headless crates.
|
||||
cargo llvm-cov --no-report "${CARGO_FLAGS[@]}" -p alkane-gui --bin alkane-toolkit
|
||||
cargo llvm-cov --no-report "${CARGO_FLAGS[@]}" \
|
||||
--workspace --exclude alkane-gui --exclude wx-rs
|
||||
|
||||
# 2. Headless UI self-test, under the same instrumentation, its coverage appended.
|
||||
xvfb-run -a env GDK_BACKEND=x11 ALKANE_SELFTEST=1 \
|
||||
cargo llvm-cov run --no-report "${CARGO_FLAGS[@]}" --bin alkane-toolkit
|
||||
|
||||
# 3. Merged report, our source only (vendored asic-rs + the patched wx-rs bridge
|
||||
# are out of scope).
|
||||
cargo llvm-cov report --ignore-filename-regex '(thirdparty|vendor)/' "$@"
|
||||
@@ -0,0 +1,35 @@
|
||||
#!/usr/bin/env bash
|
||||
# SPDX-License-Identifier: MIT
|
||||
# Use the preinstalled CI image or install missing packages on older runners.
|
||||
set -euo pipefail
|
||||
|
||||
mode="${1:?usage: ensure-build-deps.sh base|ui|all|ssh|windows|pages}"
|
||||
case "$mode" in
|
||||
base) packages=(libssl-dev pkg-config ca-certificates) ;;
|
||||
ui) packages=(xvfb xauth dbus-x11 fonts-dejavu-core) ;;
|
||||
ssh) packages=(openssh-client) ;;
|
||||
pages) packages=(python3-markdown) ;;
|
||||
all) packages=(libssl-dev pkg-config ca-certificates openssh-client
|
||||
xvfb xauth dbus-x11 fonts-dejavu-core) ;;
|
||||
# Windows cross build (mingw-w64, posix threads) + Wine for its tests.
|
||||
windows) packages=(libssl-dev pkg-config ca-certificates
|
||||
gcc-mingw-w64-x86-64-posix g++-mingw-w64-x86-64-posix
|
||||
binutils-mingw-w64-x86-64 mingw-w64-x86-64-dev wine wine64 xvfb xauth
|
||||
openjdk-21-jre-headless) ;;
|
||||
*) echo "unknown dependency set: $mode" >&2; exit 2 ;;
|
||||
esac
|
||||
|
||||
installed="$(dpkg-query -W -f='${Package} ${Status}\n' "${packages[@]}" 2>/dev/null || true)"
|
||||
missing=()
|
||||
for package in "${packages[@]}"; do
|
||||
if ! grep -Fxq "$package install ok installed" <<< "$installed"; then
|
||||
missing+=("$package")
|
||||
fi
|
||||
done
|
||||
if [ "${#missing[@]}" -eq 0 ]; then
|
||||
printf 'Build packages already installed: %s\n' "$mode"
|
||||
exit 0
|
||||
fi
|
||||
printf 'Installing %s missing build packages\n' "${#missing[@]}"
|
||||
apt-get update -qq
|
||||
DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends "${packages[@]}"
|
||||
@@ -0,0 +1,58 @@
|
||||
#!/usr/bin/env bash
|
||||
# SPDX-License-Identifier: MIT
|
||||
# Use the matching image or cache prefix; rebuild after a vendored source bump.
|
||||
# ensure-wx-prefix.sh [linux|windows] (default linux: wxGTK in $WX_PREFIX;
|
||||
# windows: the mingw wxMSW prefix in $WX_WINDOWS_PREFIX)
|
||||
set -euo pipefail
|
||||
|
||||
platform="${1:-linux}"
|
||||
case "$platform" in
|
||||
linux)
|
||||
prefix="${WX_PREFIX:?WX_PREFIX is required}"
|
||||
allowed=/root/.cache/wx-3.3.3-linux
|
||||
library=lib/libwx_gtk3u-3.3.a
|
||||
build=scripts/build-wx-linux.sh ;;
|
||||
windows)
|
||||
prefix="${WX_WINDOWS_PREFIX:?WX_WINDOWS_PREFIX is required}"
|
||||
allowed=/root/.cache/wx-3.3.3-mingw
|
||||
library=lib/libwx_mswu-3.3-x86_64-w64-mingw32.a
|
||||
build=scripts/build-wx-windows.sh ;;
|
||||
*) echo "unknown wx platform: $platform" >&2; exit 2 ;;
|
||||
esac
|
||||
if [ -L "$prefix" ] || [ -L "$prefix/.alkane-source-sha256" ]; then
|
||||
echo 'refusing symlinked wxWidgets prefix or source marker' >&2
|
||||
exit 1
|
||||
fi
|
||||
expected="$(awk '$1 == "sha256:" {print $2}' thirdparty/wxwidgets/VERSION)"
|
||||
[[ "$expected" =~ ^[0-9a-f]{64}$ ]] || {
|
||||
echo 'invalid pinned wxWidgets source digest' >&2
|
||||
exit 1
|
||||
}
|
||||
marker="$prefix/.alkane-source-sha256"
|
||||
if [ -e "$marker" ] && [ "$(cat "$marker")" != "$expected" ]; then
|
||||
python3 - "$prefix" "$allowed" <<'PY'
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import sys
|
||||
|
||||
prefix = Path(sys.argv[1])
|
||||
allowed = Path(sys.argv[2])
|
||||
if prefix != allowed or prefix.is_symlink():
|
||||
raise SystemExit('refusing unsafe wxWidgets prefix reset')
|
||||
if prefix.is_dir():
|
||||
shutil.rmtree(prefix)
|
||||
PY
|
||||
fi
|
||||
built=false
|
||||
if [ ! -x "$prefix/bin/wx-config" ] ||
|
||||
[ ! -s "$prefix/$library" ]; then
|
||||
WX_WINDOWS_PREFIX="$prefix" sh "$build"
|
||||
built=true
|
||||
else
|
||||
echo 'wx prefix available from matching image or cache'
|
||||
fi
|
||||
[ -s "$prefix/$library" ]
|
||||
printf '%s\n' "$expected" > "$marker"
|
||||
if [ -n "${GITHUB_OUTPUT:-}" ]; then
|
||||
printf 'built=%s\n' "$built" >> "$GITHUB_OUTPUT"
|
||||
fi
|
||||
@@ -0,0 +1,41 @@
|
||||
#!/usr/bin/env python3
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""Download the measured quality record (badges/quality.json on ci-badges).
|
||||
|
||||
fetch-quality.py OUT.json
|
||||
|
||||
Writes nothing (and exits 0) when main CI has not published one yet, so the
|
||||
Pages build shows a "not measured yet" dashboard instead of failing.
|
||||
"""
|
||||
|
||||
import base64
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import sys
|
||||
from urllib.error import HTTPError
|
||||
from urllib.request import Request, urlopen
|
||||
|
||||
|
||||
def main() -> None:
|
||||
out = Path(sys.argv[1])
|
||||
api = os.environ["GITHUB_API_URL"].rstrip("/")
|
||||
repo = os.environ["GITHUB_REPOSITORY"]
|
||||
url = f"{api}/repos/{repo}/contents/badges/quality.json?ref=ci-badges"
|
||||
request = Request(url, headers={"Authorization": f"token {os.environ['GITEA_TOKEN']}"})
|
||||
try:
|
||||
with urlopen(request, timeout=20) as response:
|
||||
content = base64.b64decode(json.load(response)["content"])
|
||||
except HTTPError as error:
|
||||
if error.code == 404:
|
||||
print("No quality record published yet")
|
||||
return
|
||||
raise
|
||||
if not isinstance(json.loads(content), dict):
|
||||
sys.exit("invalid quality record")
|
||||
out.write_bytes(content)
|
||||
print(f"Fetched quality record ({len(content)} bytes)")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,330 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Generate self-contained shields-style SVG badges into badges/.
|
||||
|
||||
Main CI regenerates SVGs from measured metrics and publishes them to the
|
||||
private ci-badges branch. The README loads those authenticated raw artifacts.
|
||||
|
||||
Usage:
|
||||
gen-badges.py --tests N --coverage PCT --bench "label" [--out badges]
|
||||
|
||||
Metrics not passed on the command line are computed here (LOC from the source
|
||||
tree, dependency count from Cargo.lock, license from Cargo.toml).
|
||||
"""
|
||||
import argparse
|
||||
import json
|
||||
import math
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import sys
|
||||
|
||||
# Approximate per-character width at 11px DejaVu Sans, in 1/10 px, matching the
|
||||
# shields "flat" look closely enough for a fixed-width monospace-ish render.
|
||||
CHAR_W = 6.5
|
||||
PAD = 6.0
|
||||
|
||||
COLORS = {
|
||||
"green": "#4c1",
|
||||
"brightgreen": "#4c1",
|
||||
"yellowgreen": "#a4a61d",
|
||||
"yellow": "#dfb317",
|
||||
"orange": "#fe7d37",
|
||||
"red": "#e05d44",
|
||||
"blue": "#007ec6",
|
||||
"lightgrey": "#9f9f9f",
|
||||
"grey": "#555",
|
||||
}
|
||||
|
||||
|
||||
def text_width(s: str) -> float:
|
||||
return len(s) * CHAR_W + 2 * PAD
|
||||
|
||||
|
||||
def badge_svg(label: str, message: str, color: str,
|
||||
description: str | None = None) -> str:
|
||||
color = COLORS.get(color, color)
|
||||
lw = round(text_width(label), 1)
|
||||
mw = round(text_width(message), 1)
|
||||
total = round(lw + mw, 1)
|
||||
lx = round(lw / 2, 1)
|
||||
mx = round(lw + mw / 2, 1)
|
||||
# Text is drawn twice (a dark shadow then the light glyphs) for the shields look.
|
||||
description = description or f"{label}: {message}"
|
||||
return f'''<svg xmlns="http://www.w3.org/2000/svg" width="{total}" height="20" role="img" aria-label="{description}">
|
||||
<title>{description}</title>
|
||||
<linearGradient id="s" x2="0" y2="100%">
|
||||
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
|
||||
<stop offset="1" stop-opacity=".1"/>
|
||||
</linearGradient>
|
||||
<clipPath id="r"><rect width="{total}" height="20" rx="3" fill="#fff"/></clipPath>
|
||||
<g clip-path="url(#r)">
|
||||
<rect width="{lw}" height="20" fill="#555"/>
|
||||
<rect x="{lw}" width="{mw}" height="20" fill="{color}"/>
|
||||
<rect width="{total}" height="20" fill="url(#s)"/>
|
||||
</g>
|
||||
<g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" font-size="11">
|
||||
<text x="{lx}" y="15" fill="#010101" fill-opacity=".3">{label}</text>
|
||||
<text x="{lx}" y="14">{label}</text>
|
||||
<text x="{mx}" y="15" fill="#010101" fill-opacity=".3">{message}</text>
|
||||
<text x="{mx}" y="14">{message}</text>
|
||||
</g>
|
||||
</svg>
|
||||
'''
|
||||
|
||||
|
||||
def count_loc(root: str) -> int:
|
||||
total = 0
|
||||
for base in ("crates",):
|
||||
for dirpath, _dirs, files in os.walk(os.path.join(root, base)):
|
||||
for f in files:
|
||||
if f.endswith(".rs"):
|
||||
try:
|
||||
with open(os.path.join(dirpath, f), encoding="utf-8", errors="ignore") as fh:
|
||||
total += sum(1 for _ in fh)
|
||||
except OSError:
|
||||
pass
|
||||
return total
|
||||
|
||||
|
||||
def count_deps(root: str) -> int:
|
||||
lock = os.path.join(root, "Cargo.lock")
|
||||
try:
|
||||
with open(lock, encoding="utf-8") as fh:
|
||||
return sum(1 for line in fh if line.startswith("name = "))
|
||||
except OSError:
|
||||
return 0
|
||||
|
||||
|
||||
def read_license(root: str) -> str:
|
||||
try:
|
||||
with open(os.path.join(root, "Cargo.toml"), encoding="utf-8") as fh:
|
||||
for line in fh:
|
||||
m = re.match(r'\s*license\s*=\s*"([^"]+)"', line)
|
||||
if m:
|
||||
return m.group(1)
|
||||
except OSError:
|
||||
pass
|
||||
return "unknown"
|
||||
|
||||
|
||||
def loc_color(n: int) -> str:
|
||||
return "blue"
|
||||
|
||||
|
||||
def bench_color(delta_pct: float) -> str:
|
||||
# Positive delta means faster than the previous release. A +/-2% band
|
||||
# absorbs run-to-run jitter on the dedicated CI runner.
|
||||
if delta_pct >= 2.0:
|
||||
return "brightgreen"
|
||||
if delta_pct > -2.0:
|
||||
return "blue"
|
||||
if delta_pct > -10.0:
|
||||
return "yellow"
|
||||
return "orange"
|
||||
|
||||
|
||||
def cov_color(pct: float) -> str:
|
||||
if pct >= 80:
|
||||
return "brightgreen"
|
||||
if pct >= 60:
|
||||
return "yellowgreen"
|
||||
if pct >= 40:
|
||||
return "yellow"
|
||||
if pct >= 20:
|
||||
return "orange"
|
||||
return "red"
|
||||
|
||||
|
||||
# Code-quality grade (shown on the badge and explained on the Pages quality
|
||||
# dashboard). Lint failures fail the grade outright; otherwise the grade is the
|
||||
# measured line-coverage band, using the same bands as the coverage badge
|
||||
# colour, so the two badges can never disagree.
|
||||
GRADE_BANDS = ((80.0, "A"), (60.0, "B"), (40.0, "C"), (20.0, "D"))
|
||||
GRADE_COLORS = {"A": "brightgreen", "B": "yellowgreen", "C": "yellow",
|
||||
"D": "orange", "F": "red", "pending": "lightgrey"}
|
||||
|
||||
|
||||
def quality_grade(clippy: str | None, coverage: float | None = None) -> str:
|
||||
if clippy is None or (clippy == "clean" and coverage is None):
|
||||
return "pending"
|
||||
if clippy != "clean":
|
||||
return "F"
|
||||
for floor, grade in GRADE_BANDS:
|
||||
if coverage >= floor:
|
||||
return grade
|
||||
return "F"
|
||||
|
||||
|
||||
def coverage_by_file(summary_path: str | None, root: str) -> list[dict]:
|
||||
"""Per-file line coverage from a `cargo llvm-cov --summary-only` table.
|
||||
|
||||
Columns: Filename, Regions, Missed Regions, Cover, Functions, Missed
|
||||
Functions, Executed, Lines, Missed Lines, Cover, ...
|
||||
"""
|
||||
if not summary_path:
|
||||
return []
|
||||
rows = []
|
||||
root_abs = os.path.abspath(root).rstrip("/") + "/"
|
||||
for line in Path(summary_path).read_text(errors="replace").splitlines():
|
||||
cols = line.split()
|
||||
if len(cols) < 10 or cols[0] in ("Filename", "TOTAL") or not cols[0].endswith(".rs"):
|
||||
continue
|
||||
try:
|
||||
lines, missed = int(cols[7]), int(cols[8])
|
||||
except ValueError:
|
||||
continue
|
||||
name = cols[0][len(root_abs):] if cols[0].startswith(root_abs) else cols[0]
|
||||
# llvm-cov drops the prefix every file shares, so when only workspace
|
||||
# crates are measured "crates/alkane-gui/src/x.rs" is printed as
|
||||
# "alkane-gui/src/x.rs". Put the prefix back.
|
||||
first = Path(name).parts[0] if Path(name).parts else ""
|
||||
if not name.startswith("crates/") and (Path(root) / "crates" / first).is_dir():
|
||||
name = f"crates/{name}"
|
||||
rows.append({"file": name, "lines": lines, "missed": missed})
|
||||
return rows
|
||||
|
||||
|
||||
def crate_of(path: str) -> str:
|
||||
parts = Path(path).parts
|
||||
if "crates" in parts and parts.index("crates") + 1 < len(parts):
|
||||
return parts[parts.index("crates") + 1]
|
||||
if "wx-rs-0.2.0-patched" in parts:
|
||||
return "wx-rs (bridge)"
|
||||
return "other"
|
||||
|
||||
|
||||
def loc_by_crate(root: str) -> dict[str, int]:
|
||||
out: dict[str, int] = {}
|
||||
crates = Path(root) / "crates"
|
||||
for path in sorted(crates.glob("*/**/*.rs")):
|
||||
crate = path.relative_to(crates).parts[0]
|
||||
with open(path, encoding="utf-8", errors="ignore") as fh:
|
||||
out[crate] = out.get(crate, 0) + sum(1 for _ in fh)
|
||||
return out
|
||||
|
||||
|
||||
def human(n: int) -> str:
|
||||
suffixes = ("", "K", "M", "G")
|
||||
value = float(n)
|
||||
index = 0
|
||||
while value >= 999.5 and index < len(suffixes) - 1:
|
||||
value /= 1000
|
||||
index += 1
|
||||
if value >= 100:
|
||||
number = f"{value:.0f}"
|
||||
elif value >= 10:
|
||||
number = f"{value:.1f}".rstrip("0").rstrip(".")
|
||||
else:
|
||||
number = f"{value:.2f}".rstrip("0").rstrip(".")
|
||||
return number + suffixes[index]
|
||||
|
||||
|
||||
def main() -> int:
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("--out", default="badges")
|
||||
ap.add_argument("--root", default=".")
|
||||
ap.add_argument("--tests", type=int, default=None)
|
||||
ap.add_argument("--coverage", type=float, default=None)
|
||||
ap.add_argument("--bench-coverage", type=float, default=None)
|
||||
ap.add_argument("--bench", default=None)
|
||||
# Mean per-benchmark improvement vs the previous release, as a signed
|
||||
# percentage string, or "NEW" when there is no baseline.
|
||||
ap.add_argument("--bench-delta", default=None)
|
||||
ap.add_argument("--clippy", choices=("clean", "failed"))
|
||||
# The full `cargo llvm-cov --summary-only` table, for the per-crate and
|
||||
# per-file breakdown in quality.json (the Pages quality dashboard).
|
||||
ap.add_argument("--coverage-summary", default=None)
|
||||
args = ap.parse_args()
|
||||
for value in (args.coverage, args.bench_coverage):
|
||||
if value is not None and (not math.isfinite(value) or not 0 <= value <= 100):
|
||||
ap.error("coverage must be a finite percentage between 0 and 100")
|
||||
|
||||
os.makedirs(os.path.join(args.root, args.out), exist_ok=True)
|
||||
|
||||
def write(name, label, message, color, description=None):
|
||||
path = os.path.join(args.root, args.out, name + ".svg")
|
||||
with open(path, "w", encoding="utf-8") as fh:
|
||||
fh.write(badge_svg(label, message, color, description))
|
||||
print(f"wrote {path}: {label}={message}")
|
||||
|
||||
loc = count_loc(args.root)
|
||||
deps = count_deps(args.root)
|
||||
lic = read_license(args.root)
|
||||
|
||||
write("loc", "lines of code", human(loc), loc_color(loc))
|
||||
write("license", "license", lic, "blue")
|
||||
write("dependencies", "dependencies", str(deps), "blue")
|
||||
grade = quality_grade(args.clippy, args.coverage)
|
||||
write("quality", "code quality", grade, GRADE_COLORS[grade],
|
||||
f"code quality: {grade} (Clippy, rustfmt and line coverage)")
|
||||
if args.tests is not None:
|
||||
write("tests", "tests", f"{args.tests} passing", "brightgreen")
|
||||
if args.coverage is not None:
|
||||
write("coverage", "coverage", f"{args.coverage:.0f}%", cov_color(args.coverage))
|
||||
write("bench-coverage", "bench coverage",
|
||||
f"{args.bench_coverage:.0f}%" if args.bench_coverage is not None else "pending",
|
||||
cov_color(args.bench_coverage) if args.bench_coverage is not None else "lightgrey")
|
||||
context_path = Path(args.root) / args.out / "bench-context.json"
|
||||
same_inputs = False
|
||||
if context_path.is_file():
|
||||
context = json.loads(context_path.read_text())
|
||||
if (not isinstance(context, dict) or context.get("schema") != 1 or
|
||||
not isinstance(context.get("same_inputs"), bool)):
|
||||
raise ValueError("invalid benchmark comparison context")
|
||||
same_inputs = context["same_inputs"]
|
||||
# Bench badge: prefer the mean per-benchmark improvement vs the previous
|
||||
# release. Use the static label when no comparison is available.
|
||||
delta = None
|
||||
if args.bench_delta not in (None, "", "NEW"):
|
||||
try:
|
||||
delta = float(args.bench_delta)
|
||||
except ValueError:
|
||||
delta = None
|
||||
if delta is not None:
|
||||
# Positive = faster than the previous release.
|
||||
msg = f"{'+' if delta > 0 else ''}{delta:.1f}%"
|
||||
if same_inputs:
|
||||
write("bench", "bench", msg + " same inputs", "lightgrey")
|
||||
else:
|
||||
write("bench", "bench", msg + " vs prev", bench_color(delta))
|
||||
elif args.bench_delta == "NEW":
|
||||
write("bench", "bench", "baseline set", "blue")
|
||||
elif args.bench is not None:
|
||||
write("bench", "bench", args.bench, "blue")
|
||||
|
||||
# Everything above, plus the per-crate breakdowns, for the Pages quality
|
||||
# dashboard (ci/build-pages.py).
|
||||
files = coverage_by_file(args.coverage_summary, args.root)
|
||||
crates: dict[str, dict] = {}
|
||||
for row in files:
|
||||
c = crates.setdefault(crate_of(row["file"]), {"lines": 0, "missed": 0})
|
||||
c["lines"] += row["lines"]
|
||||
c["missed"] += row["missed"]
|
||||
quality = {
|
||||
"schema": 1,
|
||||
"sha": os.environ.get("GITHUB_SHA", ""),
|
||||
"grade": grade,
|
||||
"grade_bands": [[floor, letter] for floor, letter in GRADE_BANDS],
|
||||
"clippy": args.clippy,
|
||||
"rustfmt": "clean" if args.clippy is not None else None,
|
||||
"tests_passed": args.tests,
|
||||
"coverage_pct": args.coverage,
|
||||
"bench_coverage_pct": args.bench_coverage,
|
||||
"bench_delta_pct": delta,
|
||||
"bench_same_inputs": same_inputs,
|
||||
"loc": loc,
|
||||
"loc_by_crate": loc_by_crate(args.root),
|
||||
"dependencies": deps,
|
||||
"license": lic,
|
||||
"coverage_by_crate": crates,
|
||||
"coverage_by_file": files,
|
||||
}
|
||||
path = os.path.join(args.root, args.out, "quality.json")
|
||||
with open(path, "w", encoding="utf-8") as fh:
|
||||
json.dump(quality, fh, indent=1, sort_keys=True)
|
||||
print(f"wrote {path}")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,59 @@
|
||||
# syntax=docker/dockerfile:1
|
||||
# Alkane Toolkit CI image (ci/runner-image.txt pins the pushed digest).
|
||||
#
|
||||
# Layered on the previously verified image, so every Linux job keeps exactly
|
||||
# the toolchain it had (Rust 1.97.1, prebuilt wxGTK prefix, sccache,
|
||||
# cargo-llvm-cov), and adds what the Windows jobs need:
|
||||
# - mingw-w64 (posix threads) and the rustup target x86_64-pc-windows-gnu
|
||||
# - a prebuilt static wxMSW 3.3.3 prefix, built from the vendored source by
|
||||
# scripts/build-wx-windows.sh and stamped with the source digest the same
|
||||
# way as the wxGTK prefix (ci/ensure-wx-prefix.sh rebuilds on a mismatch)
|
||||
# - Wine with an initialised prefix, to run the Windows tests and self-test
|
||||
# - a Java runtime and jsign (checksum-pinned), to Authenticode-sign the .exe
|
||||
# through Azure Artifact Signing (ci/sign-windows.sh)
|
||||
#
|
||||
# Build and push from the workstation with ci/images/build-image.sh.
|
||||
ARG BASE=gitea.bluefla.re/blueflare/alkane-ci@sha256:9d2a992bbcb91014af3c85d2e1ec8c5990701cecf83fd06c8d46f9ef13dbd948
|
||||
FROM ${BASE}
|
||||
|
||||
RUN DEBIAN_FRONTEND=noninteractive apt-get update -qq \
|
||||
&& DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends \
|
||||
gcc-mingw-w64-x86-64-posix g++-mingw-w64-x86-64-posix \
|
||||
binutils-mingw-w64-x86-64 mingw-w64-x86-64-dev \
|
||||
wine wine64 xvfb xauth openjdk-21-jre-headless \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
# jsign (Maven Central net.jsign:jsign; keep in step with ci/windows-ci.sh).
|
||||
ARG JSIGN_VERSION=7.6
|
||||
ARG JSIGN_SHA256=6cf58aa080e7ff82656f320cff8d4a3bff30408df8766806dcba3740dd839fa5
|
||||
RUN mkdir -p /opt/jsign \
|
||||
&& curl -sSfL -o /opt/jsign/jsign.jar \
|
||||
"https://repo1.maven.org/maven2/net/jsign/jsign/${JSIGN_VERSION}/jsign-${JSIGN_VERSION}.jar" \
|
||||
&& echo "${JSIGN_SHA256} /opt/jsign/jsign.jar" | sha256sum -c - \
|
||||
&& java -jar /opt/jsign/jsign.jar --help >/dev/null
|
||||
|
||||
RUN rustup target add x86_64-pc-windows-gnu
|
||||
|
||||
# The build context is the repository root, filtered by
|
||||
# alkane-ci.Dockerfile.dockerignore down to the wx source and its build script.
|
||||
COPY thirdparty/wxwidgets /tmp/alkane/thirdparty/wxwidgets
|
||||
COPY scripts/build-wx-windows.sh /tmp/alkane/scripts/build-wx-windows.sh
|
||||
RUN HOME=/root WX_WINDOWS_PREFIX=/root/.cache/wx-3.3.3-mingw \
|
||||
sh /tmp/alkane/scripts/build-wx-windows.sh \
|
||||
&& awk '$1 == "sha256:" {print $2}' /tmp/alkane/thirdparty/wxwidgets/VERSION \
|
||||
> /root/.cache/wx-3.3.3-mingw/.alkane-source-sha256 \
|
||||
&& rm -rf /tmp/alkane
|
||||
|
||||
# A ready Wine prefix saves ~10 s per job (scripts/wine-run.sh reuses it).
|
||||
RUN WINEPREFIX=/root/.cache/alkane-wine WINEDEBUG=-all \
|
||||
WINEDLLOVERRIDES='winemenubuilder.exe=d;mscoree=d;mshtml=d' \
|
||||
sh -c 'wineboot --init; wineboot --shutdown; wineserver -w' >/dev/null 2>&1; \
|
||||
test -f /root/.cache/alkane-wine/system.reg
|
||||
|
||||
RUN test -s /root/.cache/wx-3.3.3-mingw/lib/libwx_mswu-3.3-x86_64-w64-mingw32.a \
|
||||
&& test "$(/root/.cache/wx-3.3.3-mingw/bin/wx-config --version)" = 3.3.3 \
|
||||
&& test -s /root/.cache/wx-3.3.3-linux/lib/libwx_gtk3u-3.3.a \
|
||||
&& x86_64-w64-mingw32-g++-posix --version >/dev/null \
|
||||
&& wine --version \
|
||||
&& test -s /opt/jsign/jsign.jar \
|
||||
&& rustup target list --installed | grep -qx x86_64-pc-windows-gnu
|
||||
@@ -0,0 +1,4 @@
|
||||
# Only what the image build copies: the vendored wx tree and its build script.
|
||||
*
|
||||
!thirdparty/wxwidgets/**
|
||||
!scripts/build-wx-windows.sh
|
||||
@@ -0,0 +1,39 @@
|
||||
#!/bin/sh
|
||||
# Build the CI image from ci/images/alkane-ci.Dockerfile, push it to the Gitea
|
||||
# registry, and pin the pushed digest in ci/runner-image.txt and every
|
||||
# workflow (ci/check-runner-image.py enforces the pin).
|
||||
#
|
||||
# Run on the workstation, which holds the registry credential; it is passed to
|
||||
# a throwaway Docker config that is deleted afterwards, never to a runner.
|
||||
# ci/images/build-image.sh TAG e.g. ci/images/build-image.sh windows-2026-11-02
|
||||
set -eu
|
||||
TAG=${1:?usage: build-image.sh TAG}
|
||||
REPO=$(CDPATH= cd -- "$(dirname -- "$0")/../.." && pwd)
|
||||
REGISTRY=gitea.bluefla.re
|
||||
IMAGE=$REGISTRY/blueflare/alkane-ci
|
||||
TOKEN_FILE=${GITEA_TOKEN_FILE:-$HOME/.config/gitea/token}
|
||||
cd "$REPO"
|
||||
DOCKER_CONFIG=$(mktemp -d)
|
||||
export DOCKER_CONFIG
|
||||
trap 'rm -rf "$DOCKER_CONFIG"' 0
|
||||
user=$(curl -sS --config - "https://$REGISTRY/api/v1/user" <<CFG | python3 -c 'import json,sys; print(json.load(sys.stdin)["login"])'
|
||||
header = "Authorization: token $(cat "$TOKEN_FILE")"
|
||||
CFG
|
||||
)
|
||||
docker login "$REGISTRY" -u "$user" --password-stdin < "$TOKEN_FILE" >/dev/null
|
||||
DOCKER_BUILDKIT=1 docker build -f ci/images/alkane-ci.Dockerfile -t "$IMAGE:$TAG" .
|
||||
docker push "$IMAGE:$TAG" >/dev/null
|
||||
digest=$(docker inspect --format '{{range .RepoDigests}}{{println .}}{{end}}' "$IMAGE:$TAG" \
|
||||
| grep "^$IMAGE@sha256:" | head -1)
|
||||
[ -n "$digest" ] || { echo "no registry digest for $IMAGE:$TAG" >&2; exit 1; }
|
||||
old=$(cat ci/runner-image.txt)
|
||||
printf '%s\n' "$digest" > ci/runner-image.txt
|
||||
grep -rl "$old" .gitea/workflows | while read -r f; do
|
||||
python3 - "$f" "$old" "$digest" <<'PY'
|
||||
import pathlib, sys
|
||||
p, old, new = pathlib.Path(sys.argv[1]), sys.argv[2], sys.argv[3]
|
||||
p.write_text(p.read_text().replace(old, new))
|
||||
PY
|
||||
done
|
||||
python3 ci/check-runner-image.py
|
||||
echo "pinned $digest"
|
||||
@@ -0,0 +1,115 @@
|
||||
#!/usr/bin/env python3
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""Publish the last completed main/next CI result to a private badge branch."""
|
||||
|
||||
import base64
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
from urllib.error import HTTPError
|
||||
from urllib.parse import quote, urlencode
|
||||
from urllib.request import Request, urlopen
|
||||
|
||||
|
||||
WORKFLOWS = {"release.yml": "main", "next.yml": "next"}
|
||||
BADGE_BRANCH = "ci-badges"
|
||||
|
||||
|
||||
def badge(branch: str, conclusion: str, run_id: int) -> bytes:
|
||||
passed = conclusion == "success"
|
||||
word = "pass" if passed else "fail"
|
||||
color = "#2ea043" if passed else "#d1242f"
|
||||
title = f"CI {branch}: last completed attempt {word} (run {run_id})"
|
||||
return (f'<svg xmlns="http://www.w3.org/2000/svg" width="102" height="20" '
|
||||
f'role="img" aria-label="{title}"><title>{title}</title>'
|
||||
'<rect width="102" height="20" rx="3" fill="#555"/>'
|
||||
f'<path d="M39 0h60a3 3 0 0 1 3 3v14a3 3 0 0 1-3 3H39z" fill="{color}"/>'
|
||||
'<text x="20" y="14" fill="#fff" text-anchor="middle" '
|
||||
'font-family="sans-serif" font-size="11">CI</text>'
|
||||
f'<text x="70" y="14" fill="#fff" text-anchor="middle" '
|
||||
f'font-family="sans-serif" font-size="11">{word}</text></svg>\n').encode()
|
||||
|
||||
|
||||
def request_json(url: str, token: str, method: str = "GET",
|
||||
payload: dict | None = None) -> dict:
|
||||
body = None if payload is None else json.dumps(payload).encode()
|
||||
request = Request(url, data=body, method=method,
|
||||
headers={"Authorization": f"token {token}",
|
||||
"Content-Type": "application/json"})
|
||||
with urlopen(request, timeout=20) as response:
|
||||
return json.load(response)
|
||||
|
||||
|
||||
def latest_completed(api: str, repo: str, workflow: str, branch: str,
|
||||
token: str) -> tuple[int, str]:
|
||||
path = quote(workflow, safe="")
|
||||
latest = None
|
||||
for page in range(1, 21):
|
||||
query = urlencode({"branch": branch, "limit": 50, "page": page})
|
||||
url = f"{api}/repos/{repo}/actions/workflows/{path}/runs?{query}"
|
||||
runs = request_json(url, token).get("workflow_runs", [])
|
||||
for run in runs:
|
||||
if run.get("head_branch") != branch or run.get("status") != "completed":
|
||||
continue
|
||||
if run.get("conclusion") not in ("success", "failure"):
|
||||
continue
|
||||
candidate = (int(run["id"]), run["conclusion"])
|
||||
if latest is None or candidate[0] > latest[0]:
|
||||
latest = candidate
|
||||
if len(runs) < 50:
|
||||
break
|
||||
if latest is None:
|
||||
raise RuntimeError(f"no completed {branch} run found for {workflow}")
|
||||
return latest
|
||||
|
||||
|
||||
def publish(api: str, repo: str, branch: str, data: bytes, token: str) -> None:
|
||||
path = f"{api}/repos/{repo}/contents/{branch}.svg"
|
||||
encoded = base64.b64encode(data).decode("ascii")
|
||||
for attempt in range(3):
|
||||
try:
|
||||
current = request_json(path + "?ref=" + BADGE_BRANCH, token)
|
||||
current_data = base64.b64decode(current["content"])
|
||||
if current_data == data:
|
||||
return
|
||||
sha = current["sha"]
|
||||
method = "PUT"
|
||||
except HTTPError as error:
|
||||
if error.code != 404:
|
||||
raise
|
||||
sha = None
|
||||
method = "POST"
|
||||
payload = {"branch": BADGE_BRANCH, "content": encoded,
|
||||
"message": f"Record completed {branch} CI attempt"}
|
||||
if sha is not None:
|
||||
payload["sha"] = sha
|
||||
try:
|
||||
request_json(path, token, method, payload)
|
||||
return
|
||||
except HTTPError as error:
|
||||
if error.code not in (409, 422) or attempt == 2:
|
||||
raise
|
||||
raise RuntimeError("badge write did not complete")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
event = json.loads(Path(os.environ["GITHUB_EVENT_PATH"]).read_text())
|
||||
if event.get("action") != "completed":
|
||||
raise RuntimeError("expected a completed workflow_run event")
|
||||
run = event["workflow_run"]
|
||||
workflow = run["path"].split("@", 1)[0].rsplit("/", 1)[-1]
|
||||
expected_branch = WORKFLOWS.get(workflow)
|
||||
if expected_branch is None or run.get("head_branch") != expected_branch:
|
||||
raise RuntimeError("unexpected workflow or branch")
|
||||
repo = os.environ["GITHUB_REPOSITORY"]
|
||||
if event["repository"]["full_name"] != repo:
|
||||
raise RuntimeError("repository mismatch")
|
||||
api = os.environ["GITHUB_API_URL"].rstrip("/")
|
||||
token = os.environ["GITEA_TOKEN"]
|
||||
run_id, conclusion = latest_completed(api, repo, workflow, expected_branch, token)
|
||||
publish(api, repo, expected_branch, badge(expected_branch, conclusion, run_id), token)
|
||||
print(f"{expected_branch} badge records completed run {run_id}: {conclusion}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,109 @@
|
||||
#!/usr/bin/env python3
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""Package the universal macOS build as an app bundle, for release.
|
||||
|
||||
package-macos.py VERSION BINARY OUT_DIR
|
||||
|
||||
BINARY is the universal (arm64 + x86_64) alkane-toolkit executable made by
|
||||
scripts/build-gui-macos.sh. Writes
|
||||
dist/alkane-toolkit-vVERSION-macos-universal.zip holding "Alkane Toolkit.app",
|
||||
LICENSE and README.txt, and prints its path. Runs on macOS (lipo, codesign,
|
||||
ditto).
|
||||
|
||||
The bundle is ad-hoc signed only (Apple Silicon refuses to run unsigned
|
||||
code); it is not signed with a Developer ID or notarized, so Gatekeeper asks
|
||||
the user to confirm the first launch. README.txt says how.
|
||||
"""
|
||||
|
||||
import pathlib
|
||||
import plistlib
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
ROOT = pathlib.Path(__file__).resolve().parents[1]
|
||||
APP = "Alkane Toolkit.app"
|
||||
BUNDLE_ID = "re.bluefla.alkane-toolkit"
|
||||
MIN_MACOS = "11.0"
|
||||
|
||||
|
||||
def run(*args: str) -> str:
|
||||
return subprocess.run(args, check=True, capture_output=True, text=True).stdout
|
||||
|
||||
|
||||
def build_app(version: str, binary: pathlib.Path, dest: pathlib.Path) -> pathlib.Path:
|
||||
archs = run("lipo", "-archs", str(binary)).split()
|
||||
if sorted(archs) != ["arm64", "x86_64"]:
|
||||
sys.exit(f"{binary} is not a universal arm64 + x86_64 binary: {archs}")
|
||||
app = dest / APP
|
||||
macos = app / "Contents" / "MacOS"
|
||||
resources = app / "Contents" / "Resources"
|
||||
macos.mkdir(parents=True)
|
||||
resources.mkdir()
|
||||
shutil.copy2(binary, macos / "alkane-toolkit")
|
||||
shutil.copy2(ROOT / "crates/alkane-gui/macos/alkane-toolkit.icns", resources)
|
||||
info = {
|
||||
"CFBundleName": "Alkane Toolkit",
|
||||
"CFBundleDisplayName": "Alkane Toolkit",
|
||||
"CFBundleIdentifier": BUNDLE_ID,
|
||||
"CFBundleExecutable": "alkane-toolkit",
|
||||
"CFBundleIconFile": "alkane-toolkit",
|
||||
"CFBundlePackageType": "APPL",
|
||||
"CFBundleShortVersionString": version,
|
||||
"CFBundleVersion": version,
|
||||
"CFBundleInfoDictionaryVersion": "6.0",
|
||||
"LSMinimumSystemVersion": MIN_MACOS,
|
||||
"NSHighResolutionCapable": True,
|
||||
"NSPrincipalClass": "NSApplication",
|
||||
# The scanner talks to miners on the local network.
|
||||
"NSLocalNetworkUsageDescription":
|
||||
"Alkane Toolkit scans your local network to find and manage miners.",
|
||||
"NSHumanReadableCopyright":
|
||||
"Copyright (c) 2026 BlueFlare Energy Solutions Inc. (MIT License)",
|
||||
}
|
||||
with open(app / "Contents" / "Info.plist", "wb") as fh:
|
||||
plistlib.dump(info, fh)
|
||||
(app / "Contents" / "PkgInfo").write_text("APPL????")
|
||||
# Ad-hoc signature over the whole bundle (the linker's per-slice
|
||||
# signatures do not cover Info.plist or the resources).
|
||||
run("codesign", "--force", "--sign", "-", "--timestamp=none", str(app))
|
||||
run("codesign", "--verify", "--strict", str(app))
|
||||
return app
|
||||
|
||||
|
||||
def release(version: str, binary: str, out_dir: str) -> None:
|
||||
name = f"alkane-toolkit-v{version}-macos-universal"
|
||||
out = pathlib.Path(out_dir).resolve() / f"{name}.zip"
|
||||
out.parent.mkdir(parents=True, exist_ok=True)
|
||||
readme = (
|
||||
f"alkane-toolkit v{version} (macOS, Apple Silicon and Intel)\n"
|
||||
"\n"
|
||||
f"Runs on macOS {MIN_MACOS} (Big Sur) or later. Drag \"Alkane Toolkit\"\n"
|
||||
"to Applications, then open it.\n"
|
||||
"\n"
|
||||
"The app is not notarized by Apple, so the first launch is blocked\n"
|
||||
"with a warning. To allow it once:\n"
|
||||
" - macOS 15 or later: try to open it, then System Settings >\n"
|
||||
" Privacy & Security > \"Open Anyway\" for Alkane Toolkit.\n"
|
||||
" - macOS 11 to 14: Control-click the app, choose Open, then Open.\n"
|
||||
"Or, in Terminal: xattr -dr com.apple.quarantine \"/Applications/Alkane Toolkit.app\"\n"
|
||||
"\n"
|
||||
"Settings are stored in ~/.config/alkane-toolkit/.\n"
|
||||
"Manual: https://blueflare.gitea.bluefla.re/alkane-toolkit/manual/\n"
|
||||
)
|
||||
with tempfile.TemporaryDirectory(prefix="alkane-macos-") as tmp:
|
||||
folder = pathlib.Path(tmp) / name
|
||||
folder.mkdir()
|
||||
build_app(version, pathlib.Path(binary), folder)
|
||||
shutil.copy2(ROOT / "LICENSE", folder / "LICENSE")
|
||||
(folder / "README.txt").write_text(readme)
|
||||
out.unlink(missing_ok=True)
|
||||
run("ditto", "-c", "-k", "--keepParent", str(folder), str(out))
|
||||
print(out)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) != 4:
|
||||
sys.exit(__doc__)
|
||||
release(*sys.argv[1:])
|
||||
@@ -0,0 +1,108 @@
|
||||
#!/usr/bin/env python3
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""Package the cross-built Windows toolkit for release and for native testing.
|
||||
|
||||
package-windows.py release VERSION EXE OUT_DIR
|
||||
dist/alkane-toolkit-vVERSION-windows-x86_64.zip: one folder holding
|
||||
alkane-toolkit.exe, LICENSE and a README.txt. Prints the zip path.
|
||||
|
||||
package-windows.py tests CARGO_JSON OUT_ZIP
|
||||
A bundle of the test executables `cargo test --no-run
|
||||
--message-format=json` reported (CARGO_JSON is that output), plus
|
||||
tests.txt naming each one. The native Windows job runs every listed
|
||||
executable on real Windows; the same binaries already ran under Wine.
|
||||
"""
|
||||
|
||||
import json
|
||||
import pathlib
|
||||
import struct
|
||||
import sys
|
||||
import zipfile
|
||||
|
||||
ROOT = pathlib.Path(__file__).resolve().parents[1]
|
||||
TARGET = "x86_64-pc-windows-gnu"
|
||||
|
||||
|
||||
def release(version: str, exe: str, out_dir: str) -> None:
|
||||
exe_path = pathlib.Path(exe)
|
||||
if exe_path.suffix != ".exe" or not exe_path.is_file():
|
||||
sys.exit(f"not a Windows executable: {exe}")
|
||||
signed = _has_authenticode_signature(exe_path)
|
||||
name = f"alkane-toolkit-v{version}-windows-x86_64"
|
||||
out = pathlib.Path(out_dir) / f"{name}.zip"
|
||||
out.parent.mkdir(parents=True, exist_ok=True)
|
||||
readme = (
|
||||
f"alkane-toolkit v{version} (Windows x86_64)\r\n"
|
||||
"\r\n"
|
||||
"Run alkane-toolkit.exe. No installer and no extra runtime are needed:\r\n"
|
||||
"the executable is self-contained and runs on Windows 10 and 11.\r\n"
|
||||
"\r\n"
|
||||
+ (
|
||||
"The executable is signed by BlueFlare Energy Solutions Inc. (Azure Artifact Signing). To check:\r\n"
|
||||
"right-click alkane-toolkit.exe > Properties > Digital Signatures.\r\n"
|
||||
if signed else
|
||||
"This build is not code-signed, so Windows SmartScreen may warn on first\r\n"
|
||||
"run: choose More info > Run anyway.\r\n"
|
||||
)
|
||||
+ "\r\n"
|
||||
"Settings are kept in %APPDATA%\\alkane-toolkit.\r\n"
|
||||
"SSH-based actions use the OpenSSH client built into Windows (ssh.exe);\r\n"
|
||||
"HTTP actions use curl.exe, also built in.\r\n"
|
||||
"\r\n"
|
||||
"Offline demo: run from a Command Prompt with\r\n"
|
||||
" set ALKANE_FAKE_SCAN=1 && alkane-toolkit.exe\r\n"
|
||||
)
|
||||
with zipfile.ZipFile(out, "w", zipfile.ZIP_DEFLATED) as z:
|
||||
z.write(exe_path, f"{name}/alkane-toolkit.exe")
|
||||
z.write(ROOT / "LICENSE", f"{name}/LICENSE.txt")
|
||||
z.writestr(f"{name}/README.txt", readme)
|
||||
print(out)
|
||||
|
||||
|
||||
def _has_authenticode_signature(exe: pathlib.Path) -> bool:
|
||||
"""True if the PE file carries a certificate table (an Authenticode signature)."""
|
||||
data = exe.read_bytes()
|
||||
pe = struct.unpack_from("<I", data, 0x3C)[0]
|
||||
magic = struct.unpack_from("<H", data, pe + 24)[0]
|
||||
dirs = pe + 24 + (96 if magic == 0x10B else 112)
|
||||
return struct.unpack_from("<II", data, dirs + 4 * 8)[1] > 0
|
||||
|
||||
|
||||
def tests(cargo_json: str, out_zip: str) -> None:
|
||||
executables = []
|
||||
for line in pathlib.Path(cargo_json).read_text().splitlines():
|
||||
try:
|
||||
message = json.loads(line)
|
||||
except json.JSONDecodeError:
|
||||
continue
|
||||
if (message.get("reason") == "compiler-artifact"
|
||||
and message.get("profile", {}).get("test")
|
||||
and message.get("executable")):
|
||||
executables.append(pathlib.Path(message["executable"]))
|
||||
executables = sorted(set(executables))
|
||||
if not executables:
|
||||
sys.exit("no test executables in the cargo output")
|
||||
out = pathlib.Path(out_zip)
|
||||
out.parent.mkdir(parents=True, exist_ok=True)
|
||||
with zipfile.ZipFile(out, "w", zipfile.ZIP_DEFLATED) as z:
|
||||
names = []
|
||||
for exe in executables:
|
||||
if exe.suffix != ".exe" or TARGET not in exe.parts:
|
||||
sys.exit(f"not a {TARGET} test executable: {exe}")
|
||||
z.write(exe, exe.name)
|
||||
names.append(exe.name)
|
||||
z.writestr("tests.txt", "\n".join(names) + "\n")
|
||||
print(f"{out} ({len(executables)} test executables)")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
if len(sys.argv) == 5 and sys.argv[1] == "release":
|
||||
release(*sys.argv[2:])
|
||||
elif len(sys.argv) == 4 and sys.argv[1] == "tests":
|
||||
tests(*sys.argv[2:])
|
||||
else:
|
||||
sys.exit(__doc__)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,122 @@
|
||||
#!/usr/bin/env python3
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""Classify a pull request for scoped CI checks."""
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
import tomllib
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
DOC_FILES = {"README.md", "CONTRIBUTING.md", "CHANGELOG.md"}
|
||||
HEADLESS_CRATES = {"alkane-access", "alkane-artifacts", "alkane-cli", "alkane-flash"}
|
||||
|
||||
|
||||
def documentation_only(paths: list[str]) -> bool:
|
||||
return bool(paths) and all(
|
||||
path in DOC_FILES or
|
||||
(path.startswith("docs/") and Path(path).suffix in {".md", ".txt"})
|
||||
for path in paths
|
||||
)
|
||||
|
||||
|
||||
def workflow_only(paths: list[str]) -> bool:
|
||||
"""Limit the cheap PR check to workflow YAML, never policy code."""
|
||||
return bool(paths) and all(
|
||||
path.startswith(".gitea/workflows/") and
|
||||
len(Path(path).parts) == 3 and Path(path).suffix == ".yml"
|
||||
for path in paths
|
||||
)
|
||||
|
||||
|
||||
def headless_packages(paths: list[str]) -> list[str]:
|
||||
"""Return tested reverse dependents for code outside the GUI graph."""
|
||||
changed: set[str] = set()
|
||||
if not paths:
|
||||
return []
|
||||
for path in paths:
|
||||
parts = Path(path).parts
|
||||
if (len(parts) < 4 or parts[0] != "crates" or
|
||||
parts[1] not in HEADLESS_CRATES or
|
||||
parts[2] not in {"src", "tests"} or
|
||||
Path(path).suffix != ".rs"):
|
||||
return []
|
||||
changed.add(parts[1])
|
||||
|
||||
try:
|
||||
manifests = {}
|
||||
for path in (ROOT / "crates").glob("*/Cargo.toml"):
|
||||
manifest = tomllib.loads(path.read_text())
|
||||
name = manifest["package"]["name"]
|
||||
if name != path.parent.name:
|
||||
return []
|
||||
manifests[name] = manifest
|
||||
if not changed <= manifests.keys() or "alkane-gui" not in manifests:
|
||||
return []
|
||||
|
||||
def dependencies(value: object) -> set[str]:
|
||||
found: set[str] = set()
|
||||
if isinstance(value, dict):
|
||||
for key, nested in value.items():
|
||||
if key in {"dependencies", "dev-dependencies", "build-dependencies"}:
|
||||
for dep_name, spec in nested.items():
|
||||
found.add(spec.get("package", dep_name) if isinstance(spec, dict)
|
||||
else dep_name)
|
||||
else:
|
||||
found.update(dependencies(nested))
|
||||
return found & manifests.keys()
|
||||
|
||||
graph = {name: dependencies(manifest) for name, manifest in manifests.items()}
|
||||
affected = changed.copy()
|
||||
while True:
|
||||
added = {name for name, deps in graph.items() if deps & affected} - affected
|
||||
if not added:
|
||||
break
|
||||
affected.update(added)
|
||||
return [] if "alkane-gui" in affected else sorted(affected)
|
||||
except (OSError, KeyError, TypeError, ValueError):
|
||||
return []
|
||||
|
||||
|
||||
def changed_paths() -> list[str]:
|
||||
if os.environ.get("GITHUB_EVENT_NAME") != "pull_request":
|
||||
raise ValueError("not a pull request")
|
||||
if os.environ.get("GITHUB_BASE_REF") != "main":
|
||||
raise ValueError("unknown base branch")
|
||||
merge_base = subprocess.check_output(
|
||||
["git", "-C", str(ROOT), "merge-base", "HEAD",
|
||||
"refs/remotes/origin/main"],
|
||||
stderr=subprocess.DEVNULL, text=True,
|
||||
).strip()
|
||||
if len(merge_base) != 40:
|
||||
raise ValueError("missing merge base")
|
||||
raw = subprocess.check_output(
|
||||
["git", "-C", str(ROOT), "diff", "--no-renames", "--name-only", "-z",
|
||||
merge_base, "HEAD"], stderr=subprocess.DEVNULL,
|
||||
)
|
||||
return [path.decode("utf-8", "surrogateescape")
|
||||
for path in raw.split(b"\0") if path]
|
||||
|
||||
|
||||
def main() -> None:
|
||||
try:
|
||||
paths = changed_paths()
|
||||
docs = documentation_only(paths)
|
||||
workflow = workflow_only(paths) if not docs else False
|
||||
packages = headless_packages(paths) if not docs and not workflow else []
|
||||
except (OSError, subprocess.CalledProcessError, ValueError):
|
||||
paths = []
|
||||
docs = False
|
||||
workflow = False
|
||||
packages = []
|
||||
surface = ("documentation" if docs else "workflow" if workflow else
|
||||
"headless" if packages else "full")
|
||||
print(f"surface={surface}")
|
||||
print(f"packages={' '.join(packages)}")
|
||||
print(f"Changed paths: {len(paths)}", file=sys.stderr)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,121 @@
|
||||
#!/usr/bin/env python3
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""Publish a measured benchmark page to the public, data-only Pages repo."""
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shlex
|
||||
import subprocess
|
||||
import tempfile
|
||||
from urllib.request import Request, urlopen
|
||||
|
||||
PAGES_REPO = "ssh://git@gitea.bluefla.re:2222/blueflare/alkane-benchmarks.git"
|
||||
GUARD_REPO = "https://gitea.bluefla.re/blueflare/alkane-benchmarks.git"
|
||||
GUARD_COMMIT = "ea160545c49bc419a245692deda8dd7db7a611eb"
|
||||
PAGES_HOST = "gitea.bluefla.re"
|
||||
PAGES_HOST_FINGERPRINT = "SHA256:lqB5cmC9E5HNeg/lkzdOwQ++I8A70Wx7lnNs6liN24g"
|
||||
PAGES_HOST_KEY = "[gitea.bluefla.re]:2222 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIO1II6NkmRW+QAd+Wj+7qbNrA8Oo+/N/WRog4hELskn/"
|
||||
|
||||
|
||||
def run(*args: str, env: dict[str, str] | None = None,
|
||||
cwd: Path | None = None) -> subprocess.CompletedProcess[str]:
|
||||
return subprocess.run(args, env=env, cwd=cwd, text=True,
|
||||
capture_output=True, check=True)
|
||||
|
||||
|
||||
def current_main_sha() -> str:
|
||||
api = os.environ["GITHUB_API_URL"].rstrip("/")
|
||||
repo = os.environ["GITHUB_REPOSITORY"]
|
||||
token = os.environ["GITEA_TOKEN"]
|
||||
request = Request(f"{api}/repos/{repo}/branches/main",
|
||||
headers={"Authorization": f"token {token}"})
|
||||
with urlopen(request, timeout=20) as response:
|
||||
branch = json.load(response)
|
||||
return branch["commit"]["id"]
|
||||
|
||||
|
||||
def publish(site: str) -> None:
|
||||
if os.environ.get("GITHUB_REF") != "refs/heads/main":
|
||||
raise ValueError("benchmark Pages publication requires main")
|
||||
sha = os.environ["GITHUB_SHA"]
|
||||
if current_main_sha() != sha:
|
||||
print("Skipping benchmark page from a superseded main commit")
|
||||
return
|
||||
page = Path("bench-site/index.html").read_bytes()
|
||||
if not page.startswith(b"<!doctype html>") or len(page) > 1_048_576:
|
||||
raise ValueError("invalid benchmark page")
|
||||
with tempfile.TemporaryDirectory(prefix="alkane-pages-") as directory:
|
||||
temporary = Path(directory)
|
||||
guard_checkout = temporary / "guard"
|
||||
run("git", "clone", "--quiet", "--single-branch", "--branch", "next",
|
||||
GUARD_REPO, str(guard_checkout))
|
||||
guard_source = run("git", "show", f"{GUARD_COMMIT}:ci/validate_site.py",
|
||||
cwd=guard_checkout).stdout
|
||||
guard_script = temporary / "validate_site.py"
|
||||
guard_script.write_text(guard_source)
|
||||
run("python3", str(guard_script), "--page", site,
|
||||
str(Path("bench-site/index.html").resolve()))
|
||||
key = os.environ["BENCH_PAGES_DEPLOY_KEY"]
|
||||
if not key.startswith("-----BEGIN OPENSSH PRIVATE KEY-----"):
|
||||
raise ValueError("missing benchmark Pages deploy key")
|
||||
key_path = temporary / "deploy_key"
|
||||
key_path.write_text(key.rstrip("\n") + "\n")
|
||||
key_path.chmod(0o600)
|
||||
known_hosts = temporary / "known_hosts"
|
||||
known_hosts.write_text(PAGES_HOST_KEY + "\n")
|
||||
fingerprint = run("ssh-keygen", "-lf", str(known_hosts)).stdout.split()[1]
|
||||
if fingerprint != PAGES_HOST_FINGERPRINT:
|
||||
raise ValueError("benchmark Pages SSH host key changed")
|
||||
git_env = os.environ.copy()
|
||||
ssh_command = (f"ssh -i {shlex.quote(str(key_path))} -o IdentitiesOnly=yes "
|
||||
f"-o UserKnownHostsFile={shlex.quote(str(known_hosts))} "
|
||||
"-o StrictHostKeyChecking=yes -o ConnectTimeout=10")
|
||||
git_env["GIT_SSH_COMMAND"] = ssh_command
|
||||
git_env["GIT_TERMINAL_PROMPT"] = "0"
|
||||
checkout = temporary / "site"
|
||||
run("git", "clone", "--quiet", "--single-branch", "--branch", "gh-pages",
|
||||
PAGES_REPO, str(checkout), env=git_env)
|
||||
run("git", "config", "user.name", "Alkane CI", cwd=checkout)
|
||||
run("git", "config", "user.email", "ci@bluefla.re", cwd=checkout)
|
||||
for attempt in range(3):
|
||||
if current_main_sha() != sha:
|
||||
print("Skipping benchmark page from a superseded main commit")
|
||||
return
|
||||
run("git", "fetch", "--quiet", "origin", "gh-pages", env=git_env,
|
||||
cwd=checkout)
|
||||
run("git", "checkout", "-q", "-B", "bench-publish", "origin/gh-pages",
|
||||
cwd=checkout)
|
||||
destination = checkout / site / "index.html"
|
||||
destination.parent.mkdir(exist_ok=True)
|
||||
destination.write_bytes(page)
|
||||
run("git", "add", f"{site}/index.html", cwd=checkout)
|
||||
changed = subprocess.run(("git", "diff", "--cached", "--quiet"),
|
||||
cwd=checkout, check=False)
|
||||
if changed.returncode == 0:
|
||||
print(f"{site} benchmark page is already current")
|
||||
return
|
||||
if changed.returncode != 1:
|
||||
raise RuntimeError("cannot compare benchmark page")
|
||||
run("git", "commit", "-q", "-m",
|
||||
f"Update {site} benchmarks for {sha[:10]} [skip ci]", cwd=checkout)
|
||||
pushed = subprocess.run(("git", "push", "--quiet", "origin",
|
||||
"HEAD:gh-pages"), env=git_env, cwd=checkout,
|
||||
text=True, capture_output=True, check=False)
|
||||
if pushed.returncode == 0:
|
||||
print(f"Published {site} benchmark page for {sha[:10]}")
|
||||
return
|
||||
if attempt == 2:
|
||||
raise RuntimeError(f"benchmark Pages push failed: {pushed.stderr.strip()}")
|
||||
raise RuntimeError("benchmark Pages publication did not complete")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("site", choices=("sdk", "os", "toolkit"))
|
||||
publish(parser.parse_args().site)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,37 @@
|
||||
#!/usr/bin/env python3
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""Publish measured toolkit badges to the private Gitea badge branch."""
|
||||
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
from badge_store import publish_file
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
NAMES = ("tests", "coverage", "bench", "bench-coverage", "loc",
|
||||
"dependencies", "quality", "license")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
api = os.environ["GITHUB_API_URL"].rstrip("/")
|
||||
repo = os.environ["GITHUB_REPOSITORY"]
|
||||
token = os.environ["GITEA_TOKEN"]
|
||||
for name in NAMES:
|
||||
data = (ROOT / "badges" / f"{name}.svg").read_bytes()
|
||||
if not data.lstrip().startswith(b"<svg"):
|
||||
raise ValueError(f"invalid {name} SVG")
|
||||
publish_file(api, repo, f"badges/{name}.svg", data, token,
|
||||
f"Update measured {name} badge")
|
||||
# The full measurement record behind the badges, read by the Pages
|
||||
# quality dashboard (ci/build-pages.py).
|
||||
data = (ROOT / "badges" / "quality.json").read_bytes()
|
||||
if not isinstance(json.loads(data), dict):
|
||||
raise ValueError("invalid quality.json")
|
||||
publish_file(api, repo, "badges/quality.json", data, token,
|
||||
"Update measured quality record", max_bytes=262_144)
|
||||
print(f"Published {len(NAMES)} measured badge SVGs and quality.json")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,79 @@
|
||||
#!/usr/bin/env python3
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""Publish the built Pages site to this repository's gh-pages branch.
|
||||
|
||||
publish-pages.py SITE_DIR
|
||||
|
||||
Served at https://blueflare.gitea.bluefla.re/alkane-toolkit/ by the Pages
|
||||
server, which serves the gh-pages branch of public repositories. Each publish
|
||||
is one orphan commit (force-pushed), so the branch never accumulates history.
|
||||
Only main may publish, and a run for a superseded main commit skips.
|
||||
"""
|
||||
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
from urllib.request import Request, urlopen
|
||||
|
||||
BRANCH = "gh-pages"
|
||||
MAX_BYTES = 20 * 1024 * 1024
|
||||
|
||||
|
||||
def current_main_sha(api: str, repo: str, token: str) -> str:
|
||||
request = Request(f"{api}/repos/{repo}/branches/main",
|
||||
headers={"Authorization": f"token {token}"})
|
||||
with urlopen(request, timeout=20) as response:
|
||||
return json.load(response)["commit"]["id"]
|
||||
|
||||
|
||||
def git(*args: str, cwd: Path, env: dict | None = None) -> None:
|
||||
subprocess.run(("git", *args), cwd=cwd, env=env, check=True)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
if len(sys.argv) != 2:
|
||||
sys.exit(__doc__)
|
||||
site = Path(sys.argv[1])
|
||||
if not (site / "index.html").is_file():
|
||||
sys.exit(f"{site} has no index.html")
|
||||
size = sum(p.stat().st_size for p in site.rglob("*") if p.is_file())
|
||||
if size > MAX_BYTES:
|
||||
sys.exit(f"site is {size} bytes, over the {MAX_BYTES} byte limit")
|
||||
for path in site.rglob("*"):
|
||||
if path.is_file() and path.suffix not in {".html", ".css", ".svg", ".png", ".json"}:
|
||||
sys.exit(f"unexpected file type on the site: {path}")
|
||||
|
||||
api = os.environ["GITHUB_API_URL"].rstrip("/")
|
||||
repo = os.environ["GITHUB_REPOSITORY"]
|
||||
sha = os.environ["GITHUB_SHA"]
|
||||
token = os.environ["GITEA_TOKEN"]
|
||||
if os.environ.get("GITHUB_REF") != "refs/heads/main":
|
||||
sys.exit("Pages publication requires main")
|
||||
if current_main_sha(api, repo, token) != sha:
|
||||
print("Skipping Pages for a superseded main commit")
|
||||
return
|
||||
|
||||
server = api.removesuffix("/api/v1")
|
||||
with tempfile.TemporaryDirectory(prefix="alkane-pages-") as directory:
|
||||
work = Path(directory)
|
||||
shutil.copytree(site, work, dirs_exist_ok=True)
|
||||
env = dict(os.environ, GIT_TERMINAL_PROMPT="0")
|
||||
git("init", "-q", "-b", BRANCH, cwd=work)
|
||||
git("-c", "user.name=Alkane CI", "-c", "user.email=ci@noreply.gitea.bluefla.re",
|
||||
"add", "-A", cwd=work)
|
||||
git("-c", "user.name=Alkane CI", "-c", "user.email=ci@noreply.gitea.bluefla.re",
|
||||
"commit", "-q", "-m", f"Publish site for {sha[:10]} [skip ci]", cwd=work)
|
||||
# The token is read from the environment by the credential helper at
|
||||
# push time; it never appears on a command line or in the remote URL.
|
||||
helper = '!f() { echo username=alkane-ci; echo "password=$GITEA_TOKEN"; }; f'
|
||||
git("-c", f"credential.helper={helper}", "push", "-q", "--force",
|
||||
f"{server}/{repo}.git", f"HEAD:refs/heads/{BRANCH}", cwd=work, env=env)
|
||||
print(f"Published Pages for {sha[:10]} to {BRANCH}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,56 @@
|
||||
#!/usr/bin/env python3
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""Create one Gitea release and attach files using the runner's scoped token."""
|
||||
|
||||
import json
|
||||
import os
|
||||
import pathlib
|
||||
import sys
|
||||
import urllib.error
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
|
||||
|
||||
def request(url, payload, content_type):
|
||||
headers = {
|
||||
"Authorization": "token " + os.environ["GITEA_TOKEN"],
|
||||
"Content-Type": content_type,
|
||||
}
|
||||
req = urllib.request.Request(url, data=payload, headers=headers, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60) as response:
|
||||
return json.load(response)
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) < 4:
|
||||
sys.exit("usage: release.py TAG PRERELEASE FILE [FILE ...]")
|
||||
tag, prerelease, *files = sys.argv[1:]
|
||||
api = os.environ.get("GITHUB_API_URL", "https://gitea.bluefla.re/api/v1").rstrip("/")
|
||||
repo = os.environ["GITHUB_REPOSITORY"]
|
||||
sha = os.environ["GITHUB_SHA"]
|
||||
body = f"Automated Alkane toolkit build from {sha}."
|
||||
metadata = {
|
||||
"tag_name": tag,
|
||||
"target_commitish": sha,
|
||||
"name": tag,
|
||||
"body": body,
|
||||
"prerelease": prerelease.lower() == "true",
|
||||
}
|
||||
try:
|
||||
release = request(f"{api}/repos/{repo}/releases", json.dumps(metadata).encode(), "application/json")
|
||||
except urllib.error.HTTPError as exc:
|
||||
sys.exit(f"release creation failed: HTTP {exc.code}")
|
||||
release_id = release["id"]
|
||||
for name in files:
|
||||
path = pathlib.Path(name)
|
||||
query = urllib.parse.urlencode({"name": path.name})
|
||||
try:
|
||||
request(f"{api}/repos/{repo}/releases/{release_id}/assets?{query}",
|
||||
path.read_bytes(), "application/octet-stream")
|
||||
except urllib.error.HTTPError as exc:
|
||||
sys.exit(f"asset upload failed for {path.name}: HTTP {exc.code}")
|
||||
print(f"Created {tag} with {len(files)} assets")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1 @@
|
||||
gitea.bluefla.re/blueflare/alkane-ci@sha256:4929b2979851f1f12fb8f92ef74236ae4c2ea735a41ead94a16af416a4f0a1a5
|
||||
@@ -0,0 +1,88 @@
|
||||
#!/usr/bin/env bash
|
||||
# SPDX-License-Identifier: MIT
|
||||
# Authenticode-sign Windows executables with Azure Artifact Signing (formerly
|
||||
# Trusted Signing), from Linux, with jsign. Every signature is RFC 3161
|
||||
# timestamped: the signing certificates live only a few days, the timestamp is
|
||||
# what keeps a signature valid after that.
|
||||
#
|
||||
# ci/sign-windows.sh FILE.exe [FILE.exe ...]
|
||||
#
|
||||
# Configuration (environment):
|
||||
# AZURE_SIGNING_PROFILE certificate profile name; EMPTY = signing not set
|
||||
# up yet: the files are left unsigned (exit 0)
|
||||
# AZURE_SIGNING_ACCOUNT signing account (default blueflaresigning)
|
||||
# AZURE_SIGNING_ENDPOINT account endpoint (default https://wus2.codesigning.azure.net)
|
||||
# Credentials, one of:
|
||||
# AZURE_SIGNING_TENANT_ID + AZURE_SIGNING_CLIENT_ID + AZURE_SIGNING_CLIENT_SECRET
|
||||
# (the CI service principal alkane-toolkit-ci-signer, role
|
||||
# "Artifact Signing Certificate Profile Signer" on the account only)
|
||||
# AZURE_SIGNING_TOKEN an access token for https://codesigning.azure.net,
|
||||
# e.g. from `az account get-access-token --resource https://codesigning.azure.net`
|
||||
# ALKANE_JSIGN_JAR jsign jar (default /opt/jsign/jsign.jar, from the CI image)
|
||||
#
|
||||
# Once a profile is configured, any failure fails the job: a release is never
|
||||
# shipped half-signed or silently unsigned.
|
||||
set -euo pipefail
|
||||
|
||||
[ "$#" -gt 0 ] || { sed -n '3,25p' "$0" >&2; exit 2; }
|
||||
profile="${AZURE_SIGNING_PROFILE:-}"
|
||||
if [ -z "$profile" ]; then
|
||||
echo "Windows code signing is not enabled (AZURE_SIGNING_PROFILE unset); leaving $* unsigned"
|
||||
exit 0
|
||||
fi
|
||||
account="${AZURE_SIGNING_ACCOUNT:-blueflaresigning}"
|
||||
endpoint="${AZURE_SIGNING_ENDPOINT:-https://wus2.codesigning.azure.net}"
|
||||
jar="${ALKANE_JSIGN_JAR:-/opt/jsign/jsign.jar}"
|
||||
[ -f "$jar" ] || { echo "jsign not found at $jar" >&2; exit 1; }
|
||||
|
||||
if [ -z "${AZURE_SIGNING_TOKEN:-}" ]; then
|
||||
: "${AZURE_SIGNING_TENANT_ID:?signing credentials missing}"
|
||||
: "${AZURE_SIGNING_CLIENT_ID:?signing credentials missing}"
|
||||
: "${AZURE_SIGNING_CLIENT_SECRET:?signing credentials missing}"
|
||||
# Client-credentials token for the signing service. The secret and token
|
||||
# stay in this process's environment, never on a command line.
|
||||
AZURE_SIGNING_TOKEN="$(python3 - <<'PY'
|
||||
import json, os, sys, urllib.parse, urllib.request
|
||||
body = urllib.parse.urlencode({
|
||||
"grant_type": "client_credentials",
|
||||
"client_id": os.environ["AZURE_SIGNING_CLIENT_ID"],
|
||||
"client_secret": os.environ["AZURE_SIGNING_CLIENT_SECRET"],
|
||||
"scope": "https://codesigning.azure.net/.default",
|
||||
}).encode()
|
||||
url = f"https://login.microsoftonline.com/{os.environ['AZURE_SIGNING_TENANT_ID']}/oauth2/v2.0/token"
|
||||
try:
|
||||
with urllib.request.urlopen(url, body, timeout=60) as r:
|
||||
print(json.load(r)["access_token"])
|
||||
except Exception as e:
|
||||
sys.exit(f"could not get a signing token: {e}")
|
||||
PY
|
||||
)"
|
||||
fi
|
||||
export AZURE_SIGNING_TOKEN
|
||||
|
||||
for file in "$@"; do
|
||||
java -jar "$jar" \
|
||||
--storetype TRUSTEDSIGNING \
|
||||
--keystore "${endpoint#https://}" \
|
||||
--storepass env:AZURE_SIGNING_TOKEN \
|
||||
--alias "$account/$profile" \
|
||||
--alg SHA-256 \
|
||||
--tsaurl http://timestamp.acs.microsoft.com --tsmode RFC3161 \
|
||||
--tsretries 3 --tsretrywait 10 \
|
||||
--name "Alkane Toolkit" \
|
||||
--url https://gitea.bluefla.re/blueflare/alkane-toolkit \
|
||||
"$file"
|
||||
# jsign fails on any error; also make sure a signature actually landed in
|
||||
# the PE certificate table before the file goes anywhere.
|
||||
python3 - "$file" <<'PY'
|
||||
import struct, sys
|
||||
data = open(sys.argv[1], "rb").read()
|
||||
pe = struct.unpack_from("<I", data, 0x3C)[0]
|
||||
magic = struct.unpack_from("<H", data, pe + 24)[0]
|
||||
dirs = pe + 24 + (96 if magic == 0x10B else 112)
|
||||
offset, size = struct.unpack_from("<II", data, dirs + 4 * 8) # IMAGE_DIRECTORY_ENTRY_SECURITY
|
||||
if size == 0:
|
||||
sys.exit(f"{sys.argv[1]}: no Authenticode signature after signing")
|
||||
print(f"{sys.argv[1]}: signed ({size} byte certificate table)")
|
||||
PY
|
||||
done
|
||||
@@ -0,0 +1,97 @@
|
||||
#!/usr/bin/env python3
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""Check that every Toolkit benchmark contributes to the release badge."""
|
||||
|
||||
import importlib.util
|
||||
import re
|
||||
from pathlib import Path
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
spec = importlib.util.spec_from_file_location("bench_badge", ROOT / "ci/bench-badge.py")
|
||||
assert spec is not None and spec.loader is not None
|
||||
badge = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(badge)
|
||||
badge_spec = importlib.util.spec_from_file_location("gen_badges", ROOT / "ci/gen-badges.py")
|
||||
assert badge_spec is not None and badge_spec.loader is not None
|
||||
gen_badges = importlib.util.module_from_spec(badge_spec)
|
||||
badge_spec.loader.exec_module(gen_badges)
|
||||
|
||||
|
||||
class BenchBadgeTests(unittest.TestCase):
|
||||
def test_declared_suite_matches_rust_benchmarks(self):
|
||||
names = set()
|
||||
for directory in (ROOT / "crates/alkane-gui/src", ROOT / "crates/alkane-artifacts/src"):
|
||||
for path in directory.rglob("*.rs"):
|
||||
names.update(re.findall(
|
||||
r'#\[test\]\s*#\[ignore(?:\s*=\s*"[^"]*")?\]\s*'
|
||||
r'fn (bench_[A-Za-z0-9_]+)\s*\(', path.read_text()))
|
||||
self.assertEqual(names, badge.EXPECTED_BENCHES)
|
||||
|
||||
def test_incomplete_duplicate_and_malformed_results_fail(self):
|
||||
lines = [f"test tests::{name} ... {name}: 10ms/op"
|
||||
for name in sorted(badge.EXPECTED_BENCHES)]
|
||||
values = badge.require_suite(badge.parse_bench("\n".join(lines)))
|
||||
self.assertEqual(len(values), 12)
|
||||
with self.assertRaisesRegex(ValueError, "declared suite"):
|
||||
badge.require_suite(badge.parse_bench("\n".join(lines[:-1])))
|
||||
with self.assertRaisesRegex(ValueError, "duplicate"):
|
||||
badge.parse_bench("\n".join(lines + [lines[0]]))
|
||||
with self.assertRaisesRegex(ValueError, "missing timing"):
|
||||
badge.parse_bench(lines[0].replace("10ms/op", "missing"))
|
||||
|
||||
def test_badge_averages_each_benchmark_improvement(self):
|
||||
current = {"short": 50.0, "long": 900.0}
|
||||
baseline = {"short": 100.0, "long": 1000.0}
|
||||
self.assertEqual(badge.benchmark_delta(current, baseline), 30.0)
|
||||
self.assertAlmostEqual(badge.benchmark_delta(baseline, current), -55.5555556)
|
||||
self.assertIsNone(badge.benchmark_delta(current, {}))
|
||||
self.assertEqual(gen_badges.bench_color(30.0), "brightgreen")
|
||||
self.assertEqual(gen_badges.bench_color(-55.5555556), "orange")
|
||||
|
||||
def test_quality_grade_follows_lint_and_coverage(self):
|
||||
# A lint failure fails the grade; otherwise it is the coverage band,
|
||||
# with the same floors as the coverage badge colour.
|
||||
self.assertEqual(gen_badges.quality_grade("failed", 95.0), "F")
|
||||
self.assertEqual(gen_badges.quality_grade("clean", 85.0), "A")
|
||||
self.assertEqual(gen_badges.quality_grade("clean", 65.6), "B")
|
||||
self.assertEqual(gen_badges.quality_grade("clean", 48.0), "C")
|
||||
self.assertEqual(gen_badges.quality_grade("clean", 25.0), "D")
|
||||
self.assertEqual(gen_badges.quality_grade("clean", 5.0), "F")
|
||||
self.assertEqual(gen_badges.quality_grade("clean"), "pending")
|
||||
self.assertEqual(gen_badges.quality_grade(None, 90.0), "pending")
|
||||
for pct in (85.0, 65.6, 48.0, 25.0):
|
||||
grade = gen_badges.quality_grade("clean", pct)
|
||||
self.assertEqual(gen_badges.GRADE_COLORS[grade], gen_badges.cov_color(pct))
|
||||
|
||||
def test_coverage_table_is_split_by_crate(self):
|
||||
with tempfile.TemporaryDirectory(prefix="alkane-cov-") as directory:
|
||||
summary = Path(directory) / "summary.txt"
|
||||
summary.write_text(
|
||||
"Filename Regions Missed Regions Cover Functions Missed Functions "
|
||||
"Executed Lines Missed Lines Cover\n"
|
||||
"crates/alkane-gui/src/ui.rs 10 5 50.00% 4 2 50.00% 100 60 40.00%\n"
|
||||
"crates/alkane-core/src/platform.rs 4 0 100.00% 2 0 100.00% 50 5 90.00%\n"
|
||||
"TOTAL 14 5 64.29% 6 2 66.67% 150 65 56.67%\n")
|
||||
rows = gen_badges.coverage_by_file(str(summary), directory)
|
||||
self.assertEqual([r["file"] for r in rows],
|
||||
["crates/alkane-gui/src/ui.rs", "crates/alkane-core/src/platform.rs"])
|
||||
self.assertEqual(rows[0]["missed"], 60)
|
||||
self.assertEqual(gen_badges.crate_of(rows[1]["file"]), "alkane-core")
|
||||
|
||||
def test_coverage_table_restores_the_stripped_crates_prefix(self):
|
||||
with tempfile.TemporaryDirectory(prefix="alkane-cov-") as directory:
|
||||
(Path(directory) / "crates" / "alkane-cli").mkdir(parents=True)
|
||||
summary = Path(directory) / "summary.txt"
|
||||
summary.write_text(
|
||||
"alkane-cli/src/main.rs 4 4 0.00% 1 1 0.00% 23 23 0.00%\n")
|
||||
rows = gen_badges.coverage_by_file(str(summary), directory)
|
||||
self.assertEqual(rows[0], {"file": "crates/alkane-cli/src/main.rs",
|
||||
"lines": 23, "missed": 23})
|
||||
self.assertEqual(gen_badges.crate_of(rows[0]["file"]), "alkane-cli")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,130 @@
|
||||
#!/usr/bin/env python3
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""Exercise benchmark history selection and safe site rendering."""
|
||||
|
||||
import sys
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
||||
from bench_history import (bench_badge, git_distance, previous_measurement, render_html,
|
||||
same_benchmark_inputs, updated_index)
|
||||
|
||||
|
||||
class HistoryTests(unittest.TestCase):
|
||||
def test_cargo_nocapture_benchmark_lines_are_parsed(self):
|
||||
output = (
|
||||
"test tests::bench_verify_artifact ... bench_verify_artifact: 208.302µs\n"
|
||||
"test model::tests::bench_render_cells ... bench_render_cells: "
|
||||
"5000 rows x 10 cols = 8.007185ms/full-render (bytes=13553600)\n"
|
||||
"bench_direct: 12ns/op\n"
|
||||
)
|
||||
self.assertEqual(bench_badge.parse_bench(output), {
|
||||
"bench_verify_artifact": 208_302.0,
|
||||
"bench_render_cells": 8_007_185.0,
|
||||
"bench_direct": 12.0,
|
||||
})
|
||||
|
||||
def test_retry_uses_previous_commit(self):
|
||||
first = "a" * 40
|
||||
second = "b" * 40
|
||||
index = {"schema": 1, "entries": [
|
||||
{"sha": first, "timestamp": "2026-01-01T00:00:00+00:00",
|
||||
"metrics": {"bench_ns_per_op": {"hash": 10.0}}},
|
||||
{"sha": second, "timestamp": "2026-01-02T00:00:00+00:00",
|
||||
"metrics": {"bench_ns_per_op": {"hash": 8.0}}}]}
|
||||
self.assertEqual(previous_measurement(index, second,
|
||||
lambda ancestor, sha: 1 if ancestor == first else None),
|
||||
{"hash": 10.0})
|
||||
updated = updated_index(index, second, {"bench_ns_per_op": {"hash": 7.5}},
|
||||
"2026-01-03T00:00:00+00:00")
|
||||
self.assertEqual(len(updated["entries"]), 2)
|
||||
self.assertEqual(updated["entries"][-1]["metrics"]["bench_ns_per_op"]["hash"], 7.5)
|
||||
|
||||
def test_invalid_measurement_and_sha_rejected(self):
|
||||
self.assertIsNone(git_distance("--bad", "a" * 40))
|
||||
with self.assertRaises(ValueError):
|
||||
updated_index({}, "bad", {"bench_ns_per_op": {"hash": 1}}, "now")
|
||||
with self.assertRaises(ValueError):
|
||||
updated_index({}, "a" * 40, {"bench_ns_per_op": {"hash": -1}}, "now")
|
||||
|
||||
def test_baseline_must_match_cpu_class(self):
|
||||
host = {"architecture": "x86_64", "cpu_model": "A", "logical_cpus": 8}
|
||||
other = {"architecture": "x86_64", "cpu_model": "B", "logical_cpus": 8}
|
||||
index = {"entries": [{"sha": "a" * 40, "metrics": {
|
||||
"bench_ns_per_op": {"hash": 10.0}, "bench_host": host}}]}
|
||||
distance = lambda _ancestor, _sha: 1
|
||||
self.assertEqual(previous_measurement(index, "b" * 40, distance, host),
|
||||
{"hash": 10.0})
|
||||
self.assertEqual(previous_measurement(index, "b" * 40, distance, other), {})
|
||||
|
||||
def test_routing_change_is_marked_same_inputs_but_code_or_build_change_is_not(self):
|
||||
with tempfile.TemporaryDirectory(prefix="alkane-bench-inputs-") as directory:
|
||||
root = Path(directory)
|
||||
workflow = root / ".gitea/workflows/release.yml"
|
||||
workflow.parent.mkdir(parents=True)
|
||||
code = root / "crates/alkane-gui/src/main.rs"
|
||||
code.parent.mkdir(parents=True)
|
||||
code.write_text("fn main() {}\n")
|
||||
workflow.write_text(
|
||||
"jobs:\n release:\n runs-on: alkane-verify\n"
|
||||
" container:\n image: pinned-image\n"
|
||||
" env:\n RUSTFLAGS: stable\n steps:\n"
|
||||
" - name: Build release\n run: cargo build --release\n"
|
||||
" - name: Microbenchmarks\n run: cargo test --release bench_\n")
|
||||
|
||||
def git(*args):
|
||||
return subprocess.check_output(("git", "-C", str(root), *args),
|
||||
text=True, stderr=subprocess.DEVNULL).strip()
|
||||
|
||||
git("init", "-q")
|
||||
git("config", "user.name", "CI Test")
|
||||
git("config", "user.email", "ci@example.invalid")
|
||||
git("add", ".")
|
||||
git("commit", "-qm", "base")
|
||||
base = git("rev-parse", "HEAD")
|
||||
workflow.write_text(workflow.read_text().replace("alkane-verify", "alkane-ci"))
|
||||
git("commit", "-qam", "route")
|
||||
route = git("rev-parse", "HEAD")
|
||||
self.assertTrue(same_benchmark_inputs(base, route, root))
|
||||
code.write_text("fn main() { println!(\"changed\"); }\n")
|
||||
git("commit", "-qam", "code")
|
||||
changed_code = git("rev-parse", "HEAD")
|
||||
self.assertFalse(same_benchmark_inputs(route, changed_code, root))
|
||||
workflow.write_text(workflow.read_text().replace("RUSTFLAGS: stable", "RUSTFLAGS: tuned"))
|
||||
git("commit", "-qam", "flags")
|
||||
flags = git("rev-parse", "HEAD")
|
||||
self.assertFalse(same_benchmark_inputs(changed_code, flags, root))
|
||||
|
||||
def test_badge_keeps_raw_delta_and_marks_identical_inputs(self):
|
||||
with tempfile.TemporaryDirectory(prefix="alkane-bench-badge-context-") as directory:
|
||||
root = Path(directory)
|
||||
badges = root / "badges"
|
||||
badges.mkdir()
|
||||
(badges / "bench-context.json").write_text(
|
||||
json.dumps({"schema": 1, "same_inputs": True, "baseline_sha": "a" * 40}))
|
||||
command = (sys.executable, str(Path(__file__).with_name("gen-badges.py")),
|
||||
"--root", str(root), "--bench-delta=-36.4")
|
||||
subprocess.run(command, check=True, capture_output=True)
|
||||
self.assertIn("-36.4% same inputs", (badges / "bench.svg").read_text())
|
||||
(badges / "bench-context.json").unlink()
|
||||
subprocess.run(command, check=True, capture_output=True)
|
||||
self.assertIn("-36.4% vs prev", (badges / "bench.svg").read_text())
|
||||
|
||||
def test_site_escapes_history_content(self):
|
||||
entry = {"sha": "a" * 40, "timestamp": "<script>alert(1)</script>",
|
||||
"metrics": {"bench_ns_per_op": {"hash": 10.0},
|
||||
"bench_host": {"cpu_model": "<script>cpu</script>"},
|
||||
"bench_context": {"schema": 1, "same_inputs": True}}}
|
||||
page = render_html({"entries": [entry]})
|
||||
self.assertIn("<script>", page)
|
||||
self.assertNotIn("<script>", page)
|
||||
self.assertIn("10.000", page)
|
||||
self.assertIn("same inputs", page)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -53,6 +53,12 @@ R="$(new_repo)"; mkdir -p "$R/ci"; printf '2' > "$R/ci/version-x.txt"
|
||||
printf 'x' > "$R/README.md"; commit "$R" init
|
||||
run_case first_release_prebumped_x "$R" version=2.0.0 bump=initial release=true
|
||||
|
||||
# 3b. Windows packaging (build script / resources) is release-relevant: bumps Y
|
||||
R="$(new_repo)"; mkdir -p "$R/ci" "$R/crates/alkane-gui/windows"; printf '0' > "$R/ci/version-x.txt"
|
||||
printf 'x' > "$R/README.md"; commit "$R" init; git -C "$R" tag v1.2.3
|
||||
printf '<assembly/>' > "$R/crates/alkane-gui/windows/alkane-toolkit.exe.manifest"; commit "$R" winres
|
||||
run_case windows_resources_bump "$R" version=1.3.0 bump=backend release=true
|
||||
|
||||
# 3. Backend path change bumps Y
|
||||
R="$(new_repo)"; mkdir -p "$R/ci" "$R/crates/alkane-gui/src"; printf '0' > "$R/ci/version-x.txt"
|
||||
printf 'x' > "$R/README.md"; commit "$R" init; git -C "$R" tag v1.2.3
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
#!/usr/bin/env python3
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""Check that queued CI runs do not displace the last completed result."""
|
||||
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
import xml.etree.ElementTree as ET
|
||||
|
||||
path = Path(__file__).with_name("last-attempt-badge.py")
|
||||
spec = importlib.util.spec_from_file_location("last_attempt_badge", path)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
|
||||
|
||||
def runs(_url, _token):
|
||||
return {"workflow_runs": [
|
||||
{"id": 105, "head_branch": "main", "status": "completed", "conclusion": "cancelled"},
|
||||
{"id": 104, "head_branch": "main", "status": "queued", "conclusion": None},
|
||||
{"id": 103, "head_branch": "main", "status": "completed", "conclusion": "failure"},
|
||||
{"id": 102, "head_branch": "main", "status": "completed", "conclusion": "success"},
|
||||
]}
|
||||
|
||||
|
||||
with patch.object(module, "request_json", runs):
|
||||
assert module.latest_completed("https://example.invalid/api/v1", "owner/repo",
|
||||
"release.yml", "main", "token") == (103, "failure")
|
||||
|
||||
svg = module.badge("main", "failure", 103)
|
||||
root = ET.fromstring(svg)
|
||||
assert root.attrib["aria-label"] == "CI main: last completed attempt fail (run 103)"
|
||||
assert b">fail</text>" in svg
|
||||
assert b"#d1242f" in svg
|
||||
assert b">pass</text>" in module.badge("next", "success", 108)
|
||||
print("last completed CI badge: queued run ignored, pass/fail SVGs valid")
|
||||
@@ -0,0 +1,223 @@
|
||||
#!/usr/bin/env python3
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""Check that unknown changes cannot take a scoped CI path."""
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
|
||||
import importlib.util
|
||||
import yaml
|
||||
|
||||
|
||||
SCRIPT = Path(__file__).with_name("pr-surface.py")
|
||||
ROOT = SCRIPT.parents[1]
|
||||
spec = importlib.util.spec_from_file_location("pr_surface", SCRIPT)
|
||||
assert spec and spec.loader
|
||||
pr_surface = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(pr_surface)
|
||||
|
||||
|
||||
def git(directory: Path, *args: str) -> str:
|
||||
return subprocess.check_output(
|
||||
["git", "-C", str(directory), *args], text=True,
|
||||
stderr=subprocess.DEVNULL,
|
||||
).strip()
|
||||
|
||||
|
||||
def check_workflow_contract() -> None:
|
||||
"""Keep the short workflow check from silently dropping release gates."""
|
||||
required = [
|
||||
("release.yml", "main", "release", (
|
||||
"Check runner image pins", "Check first-party Rust formatting",
|
||||
"Clippy (our crate, warnings are errors)",
|
||||
"Build release", "Unit tests", "Microbenchmarks",
|
||||
"Headless UI self-test", "Coverage", "Microbenchmark coverage",
|
||||
"Generate measured badges", "Publish measured badges",
|
||||
"Record private benchmark history", "Create Gitea release + upload artifact",
|
||||
"Clean generated Toolkit build",
|
||||
)),
|
||||
("release.yml", "main", "windows", (
|
||||
"Clippy (Windows target, warnings are errors)", "Build release (Windows)",
|
||||
"Unit tests (Windows build, under Wine)",
|
||||
"Headless UI self-test (Windows build, under Wine)",
|
||||
"Sign Windows executable (Azure Artifact Signing)",
|
||||
"Package Windows artifact", "Clean generated Toolkit build",
|
||||
)),
|
||||
("next.yml", "next", "candidate", (
|
||||
"Check runner image pins", "Build release candidate",
|
||||
"Create private prerelease", "Clean generated Toolkit build",
|
||||
)),
|
||||
("next.yml", "next", "windows", (
|
||||
"Build Windows candidate", "Sign Windows candidate (Azure Artifact Signing)",
|
||||
"Package Windows candidate",
|
||||
"Clean generated Toolkit build",
|
||||
)),
|
||||
("verify.yml", "main", "verify", (
|
||||
"Check runner image pins", "Detect changed surface",
|
||||
"Check CI change surface", "Check first-party Rust formatting",
|
||||
"Check workflow-only change",
|
||||
"Clippy (our crate, warnings are errors)", "Build", "Unit tests",
|
||||
"Microbenchmarks", "Headless UI self-test", "Clean generated Toolkit build",
|
||||
)),
|
||||
("verify.yml", "main", "verify-windows", (
|
||||
"Clippy (Windows target, warnings are errors)", "Build (Windows)",
|
||||
"Unit tests (Windows build, under Wine)",
|
||||
"Headless UI self-test (Windows build, under Wine)",
|
||||
"Clean generated Toolkit build",
|
||||
)),
|
||||
]
|
||||
for filename, branch, job_name, gates in required:
|
||||
path = ROOT / ".gitea/workflows" / filename
|
||||
workflow = yaml.load(path.read_text(), Loader=yaml.BaseLoader)
|
||||
event = "pull_request" if filename == "verify.yml" else "push"
|
||||
assert workflow["on"][event]["branches"] == [branch], path
|
||||
job = workflow["jobs"][job_name]
|
||||
assert job["runs-on"] in {"alkane-ci", "alkane-verify"}, path
|
||||
steps = job["steps"]
|
||||
names = [step.get("name") for step in steps]
|
||||
positions = [names.index(gate) for gate in gates]
|
||||
assert positions == sorted(positions), path
|
||||
assert steps[positions[-1]].get("if") == "always()", path
|
||||
for gate, position in zip(gates[:-1], positions[:-1]):
|
||||
condition = steps[position].get("if")
|
||||
if gate in {"Create Gitea release + upload artifact",
|
||||
"Create private prerelease", "Package Windows artifact",
|
||||
"Build Windows candidate", "Package Windows candidate",
|
||||
"Sign Windows candidate (Azure Artifact Signing)"}:
|
||||
assert condition is not None and "release == 'true'" in condition, path
|
||||
elif filename == "verify.yml" and gate in {
|
||||
"Build", "Unit tests", "Microbenchmarks",
|
||||
"Clippy (our crate, warnings are errors)",
|
||||
"Headless UI self-test"}:
|
||||
assert condition == "steps.surface.outputs.surface == 'full'", path
|
||||
elif filename == "verify.yml" and gate == "Check CI change surface":
|
||||
assert condition == "steps.surface.outputs.surface != 'documentation'", path
|
||||
elif filename == "verify.yml" and gate == "Check first-party Rust formatting":
|
||||
assert condition == ("steps.surface.outputs.surface == 'headless' || "
|
||||
"steps.surface.outputs.surface == 'full'"), path
|
||||
elif filename == "verify.yml" and gate == "Check workflow-only change":
|
||||
assert condition == "steps.surface.outputs.surface == 'workflow'", path
|
||||
else:
|
||||
assert condition is None, path
|
||||
|
||||
|
||||
def main() -> None:
|
||||
check_workflow_contract()
|
||||
with tempfile.TemporaryDirectory(prefix="alkane-toolkit-workflow-policy-") as directory:
|
||||
global ROOT
|
||||
saved_root = ROOT
|
||||
ROOT = Path(directory)
|
||||
(ROOT / ".gitea/workflows").mkdir(parents=True)
|
||||
for filename in ("release.yml", "next.yml", "verify.yml"):
|
||||
shutil.copyfile(saved_root / ".gitea/workflows" / filename,
|
||||
ROOT / ".gitea/workflows" / filename)
|
||||
release = ROOT / ".gitea/workflows/release.yml"
|
||||
original = release.read_text()
|
||||
release.write_text(original.replace("- name: Coverage", "- name: Skipped coverage", 1))
|
||||
try:
|
||||
check_workflow_contract()
|
||||
except ValueError:
|
||||
pass
|
||||
else:
|
||||
raise AssertionError("removing the coverage gate passed workflow policy")
|
||||
ROOT = saved_root
|
||||
assert pr_surface.documentation_only(["README.md", "docs/setup.md"])
|
||||
assert pr_surface.workflow_only([".gitea/workflows/release.yml"])
|
||||
for paths in ([], ["ci/pr-surface.py"], ["ci/coverage.sh"],
|
||||
["ci/check-runner-image.py"],
|
||||
[".gitea/workflows/release.yml", "ci/check-runner-image.py"],
|
||||
[".gitea/workflows/release.yml", "crates/alkane-gui/src/main.rs"],
|
||||
[".gitea/workflows/../release.yml"]):
|
||||
assert not pr_surface.workflow_only(paths), paths
|
||||
for paths in ([], ["docs/generated.json"], ["src/main.rs"],
|
||||
["README.md", "ci/pr-surface.py"]):
|
||||
assert not pr_surface.documentation_only(paths), paths
|
||||
assert pr_surface.headless_packages(["crates/alkane-cli/src/main.rs"]) == ["alkane-cli"]
|
||||
# alkane-gui depends on alkane-flash and alkane-artifacts (right-click
|
||||
# install/uninstall and the release picker), so a change in either reaches
|
||||
# the GUI graph and needs the full build: not a headless-only change.
|
||||
assert pr_surface.headless_packages(["crates/alkane-artifacts/src/lib.rs"]) == []
|
||||
assert pr_surface.headless_packages(["crates/alkane-flash/src/lib.rs"]) == []
|
||||
assert pr_surface.headless_packages([
|
||||
"crates/alkane-access/src/lib.rs", "crates/alkane-flash/tests/update.rs",
|
||||
]) == []
|
||||
assert pr_surface.headless_packages([
|
||||
"crates/alkane-access/src/lib.rs", "crates/alkane-cli/src/main.rs",
|
||||
]) == ["alkane-access", "alkane-cli"]
|
||||
for paths in ([], ["crates/alkane-core/src/lib.rs"],
|
||||
["crates/alkane-discovery/src/lib.rs"],
|
||||
["crates/alkane-gui/src/main.rs"],
|
||||
["crates/alkane-artifacts/Cargo.toml"],
|
||||
["crates/alkane-cli/src/main.rs", "Cargo.lock"],
|
||||
["crates/alkane-cli/src/main.rs", "ci/pr-surface.py"],
|
||||
["crates/alkane-cli/README.md"]):
|
||||
assert pr_surface.headless_packages(paths) == [], paths
|
||||
|
||||
with tempfile.TemporaryDirectory(prefix="alkane-toolkit-graph-") as directory:
|
||||
root = Path(directory)
|
||||
for name, extra in (
|
||||
("alkane-gui", ""),
|
||||
("alkane-artifacts", ""),
|
||||
("alkane-flash", 'alkane-artifacts = { path = "../alkane-artifacts" }\n'),
|
||||
):
|
||||
crate = root / "crates" / name
|
||||
crate.mkdir(parents=True)
|
||||
(crate / "Cargo.toml").write_text(
|
||||
f'[package]\nname = "{name}"\nversion = "0.1.0"\n'
|
||||
f'[dependencies]\n{extra}'
|
||||
)
|
||||
saved_root = pr_surface.ROOT
|
||||
try:
|
||||
pr_surface.ROOT = root
|
||||
changed = ["crates/alkane-artifacts/src/lib.rs"]
|
||||
assert pr_surface.headless_packages(changed) == [
|
||||
"alkane-artifacts", "alkane-flash",
|
||||
]
|
||||
with (root / "crates/alkane-gui/Cargo.toml").open("a") as stream:
|
||||
stream.write('alkane-flash = { path = "../alkane-flash" }\n')
|
||||
assert pr_surface.headless_packages(changed) == []
|
||||
finally:
|
||||
pr_surface.ROOT = saved_root
|
||||
|
||||
with tempfile.TemporaryDirectory(prefix="alkane-toolkit-pr-surface-") as directory:
|
||||
repo = Path(directory)
|
||||
git(repo, "init", "-q")
|
||||
git(repo, "config", "user.name", "CI Test")
|
||||
git(repo, "config", "user.email", "ci@example.invalid")
|
||||
(repo / "README.md").write_text("initial\n")
|
||||
git(repo, "add", "README.md")
|
||||
git(repo, "commit", "-qm", "base")
|
||||
base = git(repo, "rev-parse", "HEAD")
|
||||
git(repo, "update-ref", "refs/remotes/origin/main", base)
|
||||
(repo / "README.md").write_text("documentation change\n")
|
||||
git(repo, "commit", "-qam", "docs")
|
||||
saved_root = pr_surface.ROOT
|
||||
saved_env = dict(os.environ)
|
||||
try:
|
||||
pr_surface.ROOT = repo
|
||||
os.environ["GITHUB_EVENT_NAME"] = "pull_request"
|
||||
os.environ["GITHUB_BASE_REF"] = "main"
|
||||
assert pr_surface.changed_paths() == ["README.md"]
|
||||
git(repo, "checkout", "-q", "-b", "rename-check", base)
|
||||
git(repo, "mv", "README.md", "code.rs")
|
||||
git(repo, "commit", "-qm", "rename")
|
||||
assert set(pr_surface.changed_paths()) == {"README.md", "code.rs"}
|
||||
os.environ.pop("GITHUB_BASE_REF")
|
||||
try:
|
||||
pr_surface.changed_paths()
|
||||
except ValueError:
|
||||
pass
|
||||
else:
|
||||
raise AssertionError("missing base must fall back to full checks")
|
||||
finally:
|
||||
pr_surface.ROOT = saved_root
|
||||
os.environ.clear()
|
||||
os.environ.update(saved_env)
|
||||
print("Toolkit PR change surface: documentation, workflow, headless, and fallback PASS")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,85 @@
|
||||
#!/usr/bin/env bash
|
||||
# SPDX-License-Identifier: MIT
|
||||
# Every Windows CI step in one place, so verify (PRs), the main release and the
|
||||
# next-branch candidates build and test the .exe the same way:
|
||||
#
|
||||
# windows-ci.sh toolchain mingw-w64 + Wine + the rustup target
|
||||
# windows-ci.sh clippy clippy for x86_64-pc-windows-gnu, -D warnings
|
||||
# windows-ci.sh build release alkane-toolkit.exe
|
||||
# windows-ci.sh test all unit tests, run under Wine
|
||||
# windows-ci.sh selftest headless UI self-test under Wine + Xvfb
|
||||
# windows-ci.sh sign Authenticode-sign alkane-toolkit.exe through
|
||||
# Azure Artifact Signing (ci/sign-windows.sh;
|
||||
# a no-op until AZURE_SIGNING_PROFILE is set)
|
||||
# windows-ci.sh package VERSION dist/alkane-toolkit-vVERSION-windows-x86_64.zip
|
||||
# windows-ci.sh test-bundle dist/alkane-toolkit-windows-tests.zip (for
|
||||
# the native Windows job)
|
||||
#
|
||||
# The wxMSW prefix comes from the CI image or ci/ensure-wx-prefix.sh windows.
|
||||
set -euo pipefail
|
||||
|
||||
cd "$(git rev-parse --show-toplevel)"
|
||||
export WX_WINDOWS_PREFIX="${WX_WINDOWS_PREFIX:-/root/.cache/wx-3.3.3-mingw}"
|
||||
export ALKANE_WINEPREFIX="${ALKANE_WINEPREFIX:-/root/.cache/alkane-wine}"
|
||||
# shellcheck source=../scripts/windows-cross-env.sh
|
||||
. scripts/windows-cross-env.sh
|
||||
target="$ALKANE_WIN_TARGET"
|
||||
out_dir="${CARGO_TARGET_DIR:-target}"
|
||||
exe="$out_dir/$target/release/alkane-toolkit.exe"
|
||||
# The GUI crate's tests link wx and some create windows; Wine wants a display.
|
||||
headless() { xvfb-run -a -s '-screen 0 1280x850x24' "$@"; }
|
||||
|
||||
case "${1:-}" in
|
||||
toolchain)
|
||||
bash ci/ensure-build-deps.sh windows
|
||||
rustup target list --installed | grep -qx "$target" || rustup target add "$target"
|
||||
# jsign ships in the CI image; fetch the same pinned jar on older images.
|
||||
jsign_version=7.6
|
||||
jsign_sha256=6cf58aa080e7ff82656f320cff8d4a3bff30408df8766806dcba3740dd839fa5
|
||||
if [ ! -s /opt/jsign/jsign.jar ]; then
|
||||
mkdir -p /opt/jsign
|
||||
curl -sSfL -o /opt/jsign/jsign.jar \
|
||||
"https://repo1.maven.org/maven2/net/jsign/jsign/$jsign_version/jsign-$jsign_version.jar"
|
||||
echo "$jsign_sha256 /opt/jsign/jsign.jar" | sha256sum -c -
|
||||
fi
|
||||
;;
|
||||
clippy)
|
||||
rustup component add clippy >/dev/null 2>&1 || true
|
||||
cargo clippy -p alkane-gui --no-deps --release --bins --target "$target" --offline -- -D warnings
|
||||
cargo clippy --workspace --exclude alkane-gui --exclude wx-rs --all-targets \
|
||||
--target "$target" --offline -- -D warnings
|
||||
;;
|
||||
build)
|
||||
cargo build --release -p alkane-gui --target "$target" --offline
|
||||
# One self-contained .exe: any toolchain DLL import would fail on a clean
|
||||
# Windows machine, so refuse to ship one.
|
||||
if x86_64-w64-mingw32-objdump -p "$exe" | grep -iE 'DLL Name: (libstdc|libgcc|libwinpthread)'; then
|
||||
echo "alkane-toolkit.exe imports a mingw runtime DLL" >&2
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
test)
|
||||
headless cargo test --workspace --exclude wx-rs --target "$target" --offline 2>&1 \
|
||||
| tee tests-windows.txt
|
||||
;;
|
||||
selftest)
|
||||
bash scripts/ui-selftest-windows.sh "$exe"
|
||||
;;
|
||||
sign)
|
||||
bash ci/sign-windows.sh "$exe"
|
||||
;;
|
||||
package)
|
||||
python3 ci/package-windows.py release "${2:?usage: windows-ci.sh package VERSION}" "$exe" dist
|
||||
;;
|
||||
test-bundle)
|
||||
mkdir -p dist
|
||||
cargo test --workspace --exclude wx-rs --target "$target" --offline --no-run \
|
||||
--message-format=json > "$out_dir/windows-tests.json"
|
||||
python3 ci/package-windows.py tests "$out_dir/windows-tests.json" \
|
||||
dist/alkane-toolkit-windows-tests.zip
|
||||
;;
|
||||
*)
|
||||
sed -n '3,19p' "$0" >&2
|
||||
exit 2
|
||||
;;
|
||||
esac
|
||||
@@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env bash
|
||||
# SPDX-License-Identifier: MIT
|
||||
# Key a wxWidgets prefix to its tracked source and build toolchain.
|
||||
# wx-cache-key.sh [linux|windows]
|
||||
set -euo pipefail
|
||||
|
||||
platform="${1:-linux}"
|
||||
if [ "$platform" = windows ]; then
|
||||
material="$({
|
||||
git rev-parse HEAD:thirdparty/wxwidgets/wxWidgets-3.3.3
|
||||
git rev-parse HEAD:scripts/build-wx-windows.sh
|
||||
x86_64-w64-mingw32-g++-posix -dumpmachine
|
||||
x86_64-w64-mingw32-g++-posix -dumpfullversion
|
||||
} )"
|
||||
digest="$(printf '%s\n' "$material" | sha256sum | cut -c1-24)"
|
||||
key="wx-3.3.3-mingw-$digest"
|
||||
if [ -n "${GITHUB_OUTPUT:-}" ]; then printf 'key=%s\n' "$key" >> "$GITHUB_OUTPUT"; else printf '%s\n' "$key"; fi
|
||||
exit 0
|
||||
fi
|
||||
|
||||
material="$({
|
||||
git rev-parse HEAD:thirdparty/wxwidgets/wxWidgets-3.3.3
|
||||
git rev-parse HEAD:scripts/build-wx-linux.sh
|
||||
cat thirdparty/wx-rs/VERSION
|
||||
gcc -dumpmachine
|
||||
gcc -dumpfullversion
|
||||
pkg-config --modversion gtk+-3.0
|
||||
mapfile -t modules < <(pkg-config --list-all | awk '{print $1}' | LC_ALL=C sort -u)
|
||||
for module in "${modules[@]}"; do
|
||||
printf '%s=%s\n' "$module" "$(pkg-config --modversion "$module" 2>/dev/null || printf unavailable)"
|
||||
done
|
||||
getconf GNU_LIBC_VERSION
|
||||
} )"
|
||||
digest="$(printf '%s\n' "$material" | sha256sum | cut -c1-24)"
|
||||
key="wx-3.3.3-$digest"
|
||||
if [ -n "${GITHUB_OUTPUT:-}" ]; then
|
||||
printf 'key=%s\n' "$key" >> "$GITHUB_OUTPUT"
|
||||
else
|
||||
printf '%s\n' "$key"
|
||||
fi
|
||||
@@ -1 +1,219 @@
|
||||
// alkane-access
|
||||
// SPDX-License-Identifier: MIT
|
||||
//! Bounded, read-only SSH probes for board identification.
|
||||
|
||||
use std::fs;
|
||||
use std::io::{self, Read};
|
||||
use std::net::IpAddr;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::{Command, Stdio};
|
||||
use std::thread;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
const MAX_PROBE_BYTES: usize = 64 * 1024;
|
||||
const PROBE_TIMEOUT: Duration = Duration::from_secs(12);
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum Probe {
|
||||
KernelRelease,
|
||||
MtdTable,
|
||||
DeviceTreeModel,
|
||||
}
|
||||
|
||||
impl Probe {
|
||||
fn command(self) -> &'static str {
|
||||
match self {
|
||||
Self::KernelRelease => "uname -r",
|
||||
Self::MtdTable => "cat /proc/mtd",
|
||||
Self::DeviceTreeModel => "cat /proc/device-tree/model",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum AccessError {
|
||||
InvalidTarget,
|
||||
InvalidKnownHosts,
|
||||
Timeout,
|
||||
OutputTooLarge,
|
||||
RemoteFailure,
|
||||
Io(io::Error),
|
||||
}
|
||||
|
||||
impl From<io::Error> for AccessError {
|
||||
fn from(error: io::Error) -> Self {
|
||||
Self::Io(error)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SshTarget {
|
||||
address: IpAddr,
|
||||
port: u16,
|
||||
user: String,
|
||||
known_hosts: PathBuf,
|
||||
}
|
||||
|
||||
impl SshTarget {
|
||||
pub fn new(
|
||||
address: IpAddr,
|
||||
port: u16,
|
||||
user: &str,
|
||||
known_hosts: &Path,
|
||||
) -> Result<Self, AccessError> {
|
||||
if port == 0
|
||||
|| user.is_empty()
|
||||
|| !user
|
||||
.bytes()
|
||||
.all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'-' | b'.'))
|
||||
{
|
||||
return Err(AccessError::InvalidTarget);
|
||||
}
|
||||
if !known_hosts.is_absolute()
|
||||
|| !fs::metadata(known_hosts).is_ok_and(|metadata| metadata.is_file())
|
||||
{
|
||||
return Err(AccessError::InvalidKnownHosts);
|
||||
}
|
||||
Ok(Self {
|
||||
address,
|
||||
port,
|
||||
user: user.to_owned(),
|
||||
known_hosts: known_hosts.to_owned(),
|
||||
})
|
||||
}
|
||||
|
||||
fn command(&self, probe: Probe) -> Command {
|
||||
let mut command = alkane_core::platform::quiet_command("ssh");
|
||||
command
|
||||
.arg("-F")
|
||||
.arg("none")
|
||||
.arg("-o")
|
||||
.arg("BatchMode=yes")
|
||||
.arg("-o")
|
||||
.arg("StrictHostKeyChecking=yes")
|
||||
.arg("-o")
|
||||
.arg("GlobalKnownHostsFile=none")
|
||||
.arg("-o")
|
||||
.arg(format!("UserKnownHostsFile={}", self.known_hosts.display()))
|
||||
.arg("-o")
|
||||
.arg("ConnectTimeout=5")
|
||||
.arg("-o")
|
||||
.arg("ServerAliveInterval=5")
|
||||
.arg("-o")
|
||||
.arg("ServerAliveCountMax=1")
|
||||
.arg("-o")
|
||||
.arg("RequestTTY=no")
|
||||
.arg("-p")
|
||||
.arg(self.port.to_string())
|
||||
.arg(format!("{}@{}", self.user, self.address))
|
||||
.arg(probe.command());
|
||||
command
|
||||
}
|
||||
|
||||
pub fn probe(&self, probe: Probe) -> Result<Vec<u8>, AccessError> {
|
||||
run_bounded(self.command(probe))
|
||||
}
|
||||
}
|
||||
|
||||
fn collect_bounded<R: Read + Send + 'static>(
|
||||
mut reader: R,
|
||||
) -> thread::JoinHandle<Result<Vec<u8>, AccessError>> {
|
||||
thread::spawn(move || {
|
||||
let mut data = Vec::new();
|
||||
let mut chunk = [0_u8; 4096];
|
||||
loop {
|
||||
let count = reader.read(&mut chunk)?;
|
||||
if count == 0 {
|
||||
return Ok(data);
|
||||
}
|
||||
if data.len() + count > MAX_PROBE_BYTES {
|
||||
return Err(AccessError::OutputTooLarge);
|
||||
}
|
||||
data.extend_from_slice(&chunk[..count]);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn run_bounded(command: Command) -> Result<Vec<u8>, AccessError> {
|
||||
run_bounded_with_timeout(command, PROBE_TIMEOUT)
|
||||
}
|
||||
|
||||
fn run_bounded_with_timeout(
|
||||
mut command: Command,
|
||||
timeout: Duration,
|
||||
) -> Result<Vec<u8>, AccessError> {
|
||||
command
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped());
|
||||
let mut child = command.spawn()?;
|
||||
let stdout = collect_bounded(child.stdout.take().expect("piped stdout"));
|
||||
let stderr = collect_bounded(child.stderr.take().expect("piped stderr"));
|
||||
let deadline = Instant::now() + timeout;
|
||||
let result = loop {
|
||||
if let Some(status) = child.try_wait()? {
|
||||
break status.success();
|
||||
}
|
||||
if Instant::now() >= deadline {
|
||||
let _ = child.kill();
|
||||
let _ = child.wait();
|
||||
let _ = stdout.join();
|
||||
let _ = stderr.join();
|
||||
return Err(AccessError::Timeout);
|
||||
}
|
||||
thread::sleep(Duration::from_millis(10));
|
||||
};
|
||||
let output = stdout.join().map_err(|_| AccessError::RemoteFailure)??;
|
||||
let error_output = stderr.join().map_err(|_| AccessError::RemoteFailure)??;
|
||||
if !result || !error_output.is_empty() {
|
||||
return Err(AccessError::RemoteFailure);
|
||||
}
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn target_rejects_injected_user_and_unknown_host_file() {
|
||||
let address = "127.0.0.1".parse().unwrap();
|
||||
assert!(matches!(
|
||||
SshTarget::new(
|
||||
address,
|
||||
22,
|
||||
"root;id",
|
||||
Path::new("/etc/ssh/ssh_known_hosts")
|
||||
),
|
||||
Err(AccessError::InvalidTarget)
|
||||
));
|
||||
assert!(matches!(
|
||||
SshTarget::new(address, 22, "root", Path::new("relative-known-hosts")),
|
||||
Err(AccessError::InvalidKnownHosts)
|
||||
));
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn bounded_runner_rejects_excess_output_and_failed_command() {
|
||||
let mut success = Command::new("/bin/sh");
|
||||
success.args(["-c", "printf board"]);
|
||||
assert_eq!(run_bounded(success).unwrap(), b"board");
|
||||
let mut overflow = Command::new("/bin/sh");
|
||||
overflow.args(["-c", "head -c 70000 /dev/zero"]);
|
||||
assert!(matches!(
|
||||
run_bounded(overflow),
|
||||
Err(AccessError::OutputTooLarge)
|
||||
));
|
||||
let mut failed = Command::new("/bin/sh");
|
||||
failed.args(["-c", "exit 7"]);
|
||||
assert!(matches!(
|
||||
run_bounded(failed),
|
||||
Err(AccessError::RemoteFailure)
|
||||
));
|
||||
let mut hanging = Command::new("/bin/sleep");
|
||||
hanging.arg("1");
|
||||
assert!(matches!(
|
||||
run_bounded_with_timeout(hanging, Duration::from_millis(20)),
|
||||
Err(AccessError::Timeout)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,3 +6,7 @@ license.workspace = true
|
||||
|
||||
[dependencies]
|
||||
alkane-core = { path = "../alkane-core" }
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
sha2.workspace = true
|
||||
thiserror.workspace = true
|
||||
|
||||
@@ -0,0 +1,404 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
//! Fetch the latest alkane-os `.afw` release from a Gitea repository.
|
||||
//!
|
||||
//! GitHub is retired for this project; releases live on the self-hosted Gitea.
|
||||
//! This resolves `releases/latest`, picks the `.afw` asset for a board, and
|
||||
//! downloads it so the normal verify -> plan -> preflight -> install flow runs on
|
||||
//! it exactly as it would on a local file. TLS is required, so this shells out to
|
||||
//! `curl` (as the GUI already does) rather than the plaintext TCP path used for
|
||||
//! liveness checks.
|
||||
//!
|
||||
//! A private repo needs a token; it is read from the environment at run time
|
||||
//! (`ALKANE_RELEASE_TOKEN`, else `GITEA_TOKEN`) and passed to curl through a
|
||||
//! 0600 config file, never on the command line.
|
||||
//!
|
||||
//! As a convenience for the GUI, a read-only token can be baked into the binary
|
||||
//! at build time by compiling with `ALKANE_EMBEDDED_TOKEN=<token>` set (see
|
||||
//! `EMBEDDED_TOKEN`). The source tree carries no secret - the value lives only in
|
||||
//! a binary built with that variable set - and the environment variables above
|
||||
//! always take precedence, so a user can override it. When no such token is baked
|
||||
//! in and none is in the environment, the token is simply absent (fine for public
|
||||
//! repos; a private repo then returns 401/404).
|
||||
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::Command;
|
||||
|
||||
use serde_json::Value;
|
||||
use thiserror::Error;
|
||||
|
||||
/// A read-only Gitea token baked in at build time when the crate is compiled
|
||||
/// with `ALKANE_EMBEDDED_TOKEN` set. `None` in an ordinary build (no secret in
|
||||
/// the source tree). The environment variables win over this, so it is only a
|
||||
/// fallback for a distributed binary (the GUI) that ships without asking the user
|
||||
/// to configure a token. Scope it read-only on the server side; anyone with the
|
||||
/// binary can extract it.
|
||||
const EMBEDDED_TOKEN: Option<&str> = option_env!("ALKANE_EMBEDDED_TOKEN");
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum FetchError {
|
||||
#[error("curl failed: {0}")]
|
||||
Curl(String),
|
||||
#[error("release metadata did not parse: {0}")]
|
||||
Parse(String),
|
||||
#[error("no .afw asset for board {0} in the latest release")]
|
||||
NoAsset(String),
|
||||
#[error("io: {0}")]
|
||||
Io(String),
|
||||
}
|
||||
|
||||
/// Where releases come from. Defaults to the alkane-os repo on the self-hosted
|
||||
/// Gitea; the base URL and token are read from the environment.
|
||||
pub struct ReleaseSource {
|
||||
pub base_url: String,
|
||||
pub owner: String,
|
||||
pub repo: String,
|
||||
pub token: Option<String>,
|
||||
}
|
||||
|
||||
impl ReleaseSource {
|
||||
/// The alkane-os release source, honoring `ALKANE_RELEASE_BASE` and a token
|
||||
/// from `ALKANE_RELEASE_TOKEN` or `GITEA_TOKEN`.
|
||||
pub fn alkane_os() -> Self {
|
||||
let base_url = std::env::var("ALKANE_RELEASE_BASE")
|
||||
.ok()
|
||||
.filter(|s| !s.is_empty())
|
||||
.unwrap_or_else(|| "https://gitea.bluefla.re".to_string());
|
||||
let token = std::env::var("ALKANE_RELEASE_TOKEN")
|
||||
.ok()
|
||||
.or_else(|| std::env::var("GITEA_TOKEN").ok())
|
||||
.filter(|t| !t.is_empty())
|
||||
.or_else(|| EMBEDDED_TOKEN.map(str::to_string).filter(|t| !t.is_empty()));
|
||||
Self {
|
||||
base_url,
|
||||
owner: "blueflare".to_string(),
|
||||
repo: "alkane-os".to_string(),
|
||||
token,
|
||||
}
|
||||
}
|
||||
|
||||
/// The releases list (newest first). Unlike `releases/latest`, this includes
|
||||
/// pre-releases, so an early/bench channel is still found by `--latest`.
|
||||
fn api_list(&self) -> String {
|
||||
format!(
|
||||
"{}/api/v1/repos/{}/{}/releases?limit=50",
|
||||
self.base_url.trim_end_matches('/'),
|
||||
self.owner,
|
||||
self.repo
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Find the first `.afw` asset for `board` across a newest-first list of
|
||||
/// releases. Returns (name, url).
|
||||
fn pick_asset(releases: &Value, board: &str) -> Option<(String, String)> {
|
||||
let needle = format!("-{board}-");
|
||||
for release in releases.as_array()? {
|
||||
let Some(assets) = release.get("assets").and_then(Value::as_array) else {
|
||||
continue;
|
||||
};
|
||||
for asset in assets {
|
||||
let name = asset.get("name").and_then(Value::as_str).unwrap_or("");
|
||||
if name.ends_with(".afw") && name.contains(&needle) {
|
||||
if let Some(url) = asset.get("browser_download_url").and_then(Value::as_str) {
|
||||
return Some((name.to_string(), url.to_string()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// A temp curl `--config` file carrying the auth header, removed on drop so the
|
||||
/// token never lands in a process argument or a lingering file.
|
||||
struct TokenConfig {
|
||||
path: Option<PathBuf>,
|
||||
}
|
||||
|
||||
impl TokenConfig {
|
||||
fn new(token: &Option<String>) -> Result<Self, FetchError> {
|
||||
let Some(token) = token else {
|
||||
return Ok(Self { path: None });
|
||||
};
|
||||
let path = std::env::temp_dir().join(format!("alkane-fetch-{}.cfg", std::process::id()));
|
||||
write_private(
|
||||
&path,
|
||||
&format!("header = \"Authorization: token {token}\"\n"),
|
||||
)?;
|
||||
Ok(Self { path: Some(path) })
|
||||
}
|
||||
|
||||
fn apply(&self, cmd: &mut Command) {
|
||||
if let Some(path) = &self.path {
|
||||
cmd.arg("--config").arg(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for TokenConfig {
|
||||
fn drop(&mut self) {
|
||||
if let Some(path) = &self.path {
|
||||
let _ = fs::remove_file(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn write_private(path: &Path, contents: &str) -> Result<(), FetchError> {
|
||||
use std::io::Write;
|
||||
use std::os::unix::fs::OpenOptionsExt;
|
||||
let mut file = fs::OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
.truncate(true)
|
||||
.mode(0o600)
|
||||
.open(path)
|
||||
.map_err(|e| FetchError::Io(e.to_string()))?;
|
||||
file.write_all(contents.as_bytes())
|
||||
.map_err(|e| FetchError::Io(e.to_string()))
|
||||
}
|
||||
|
||||
#[cfg(not(unix))]
|
||||
fn write_private(path: &Path, contents: &str) -> Result<(), FetchError> {
|
||||
fs::write(path, contents).map_err(|e| FetchError::Io(e.to_string()))
|
||||
}
|
||||
|
||||
fn curl_bytes(src: &ReleaseSource, url: &str) -> Result<Vec<u8>, FetchError> {
|
||||
let config = TokenConfig::new(&src.token)?;
|
||||
let mut cmd = Command::new("curl");
|
||||
cmd.arg("-fsSL").arg("--max-time").arg("120");
|
||||
config.apply(&mut cmd);
|
||||
cmd.arg(url);
|
||||
let out = cmd.output().map_err(|e| FetchError::Curl(e.to_string()))?;
|
||||
if !out.status.success() {
|
||||
return Err(FetchError::Curl(format!(
|
||||
"{} ({})",
|
||||
String::from_utf8_lossy(&out.stderr).trim(),
|
||||
out.status
|
||||
)));
|
||||
}
|
||||
Ok(out.stdout)
|
||||
}
|
||||
|
||||
fn curl_download(src: &ReleaseSource, url: &str, dest: &Path) -> Result<(), FetchError> {
|
||||
let config = TokenConfig::new(&src.token)?;
|
||||
let mut cmd = Command::new("curl");
|
||||
cmd.arg("-fsSL")
|
||||
.arg("--max-time")
|
||||
.arg("600")
|
||||
.arg("-o")
|
||||
.arg(dest);
|
||||
config.apply(&mut cmd);
|
||||
cmd.arg(url);
|
||||
let out = cmd.output().map_err(|e| FetchError::Curl(e.to_string()))?;
|
||||
if !out.status.success() {
|
||||
return Err(FetchError::Curl(format!(
|
||||
"{} ({})",
|
||||
String::from_utf8_lossy(&out.stderr).trim(),
|
||||
out.status
|
||||
)));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Resolve the latest release's `.afw` asset for `board`: returns (name, url).
|
||||
/// Scans the newest-first releases list (so a pre-release counts as latest).
|
||||
pub fn latest_afw_asset(src: &ReleaseSource, board: &str) -> Result<(String, String), FetchError> {
|
||||
let body = curl_bytes(src, &src.api_list())?;
|
||||
let releases: Value =
|
||||
serde_json::from_slice(&body).map_err(|e| FetchError::Parse(e.to_string()))?;
|
||||
pick_asset(&releases, board).ok_or_else(|| FetchError::NoAsset(board.to_string()))
|
||||
}
|
||||
|
||||
/// Download the latest `.afw` for `board` into `dest_dir`; returns its path.
|
||||
pub fn fetch_latest_afw(
|
||||
src: &ReleaseSource,
|
||||
board: &str,
|
||||
dest_dir: &Path,
|
||||
) -> Result<PathBuf, FetchError> {
|
||||
let (name, url) = latest_afw_asset(src, board)?;
|
||||
fs::create_dir_all(dest_dir).map_err(|e| FetchError::Io(e.to_string()))?;
|
||||
let dest = dest_dir.join(&name);
|
||||
curl_download(src, &url, &dest)?;
|
||||
Ok(dest)
|
||||
}
|
||||
|
||||
/// One `.afw` asset in a release.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AfwAsset {
|
||||
pub name: String,
|
||||
pub url: String,
|
||||
}
|
||||
|
||||
/// A release as listed for the GUI's release picker: its tag/name, whether it is
|
||||
/// a pre-release or draft, when it was published, and the `.afw` assets it holds.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ReleaseInfo {
|
||||
pub tag: String,
|
||||
pub name: String,
|
||||
pub prerelease: bool,
|
||||
pub draft: bool,
|
||||
pub published_at: String,
|
||||
pub afw_assets: Vec<AfwAsset>,
|
||||
}
|
||||
|
||||
impl ReleaseInfo {
|
||||
/// The `.afw` asset for `board` in this release, if any (matched by the
|
||||
/// `-<board>-` segment of the filename, as `pick_asset` does).
|
||||
pub fn asset_for_board(&self, board: &str) -> Option<&AfwAsset> {
|
||||
let needle = format!("-{board}-");
|
||||
self.afw_assets.iter().find(|a| a.name.contains(&needle))
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a Gitea releases array (newest first) into `ReleaseInfo`s, keeping only
|
||||
/// the `.afw` assets of each. Drafts are included so the caller can decide.
|
||||
fn parse_releases(releases: &Value) -> Vec<ReleaseInfo> {
|
||||
let mut out = Vec::new();
|
||||
let Some(arr) = releases.as_array() else {
|
||||
return out;
|
||||
};
|
||||
for release in arr {
|
||||
let tag = release
|
||||
.get("tag_name")
|
||||
.and_then(Value::as_str)
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
let name = release
|
||||
.get("name")
|
||||
.and_then(Value::as_str)
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
let prerelease = release
|
||||
.get("prerelease")
|
||||
.and_then(Value::as_bool)
|
||||
.unwrap_or(false);
|
||||
let draft = release
|
||||
.get("draft")
|
||||
.and_then(Value::as_bool)
|
||||
.unwrap_or(false);
|
||||
let published_at = release
|
||||
.get("published_at")
|
||||
.or_else(|| release.get("created_at"))
|
||||
.and_then(Value::as_str)
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
let mut afw_assets = Vec::new();
|
||||
if let Some(assets) = release.get("assets").and_then(Value::as_array) {
|
||||
for asset in assets {
|
||||
let aname = asset.get("name").and_then(Value::as_str).unwrap_or("");
|
||||
if aname.ends_with(".afw") {
|
||||
if let Some(url) = asset.get("browser_download_url").and_then(Value::as_str) {
|
||||
afw_assets.push(AfwAsset {
|
||||
name: aname.to_string(),
|
||||
url: url.to_string(),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
out.push(ReleaseInfo {
|
||||
tag,
|
||||
name,
|
||||
prerelease,
|
||||
draft,
|
||||
published_at,
|
||||
afw_assets,
|
||||
});
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// List the releases (newest first) of the source repo, each with its `.afw`
|
||||
/// assets. Used by the GUI to scan alkane-os releases and pick one to install.
|
||||
pub fn list_releases(src: &ReleaseSource) -> Result<Vec<ReleaseInfo>, FetchError> {
|
||||
let body = curl_bytes(src, &src.api_list())?;
|
||||
let releases: Value =
|
||||
serde_json::from_slice(&body).map_err(|e| FetchError::Parse(e.to_string()))?;
|
||||
Ok(parse_releases(&releases))
|
||||
}
|
||||
|
||||
/// Download a specific release asset (by its `browser_download_url`) into
|
||||
/// `dest_dir`, saving it under `name`; returns the written path.
|
||||
pub fn fetch_afw_url(
|
||||
src: &ReleaseSource,
|
||||
name: &str,
|
||||
url: &str,
|
||||
dest_dir: &Path,
|
||||
) -> Result<PathBuf, FetchError> {
|
||||
fs::create_dir_all(dest_dir).map_err(|e| FetchError::Io(e.to_string()))?;
|
||||
let dest = dest_dir.join(name);
|
||||
curl_download(src, url, &dest)?;
|
||||
Ok(dest)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn source_defaults_to_alkane_os_on_gitea() {
|
||||
std::env::remove_var("ALKANE_RELEASE_BASE");
|
||||
let src = ReleaseSource::alkane_os();
|
||||
assert_eq!(src.owner, "blueflare");
|
||||
assert_eq!(src.repo, "alkane-os");
|
||||
assert!(src
|
||||
.api_list()
|
||||
.contains("/api/v1/repos/blueflare/alkane-os/releases"));
|
||||
assert!(src.api_list().starts_with("https://gitea.bluefla.re"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pick_asset_takes_the_newest_matching_afw() {
|
||||
// Newest-first list of releases; the board's .afw in the first release wins.
|
||||
let releases = serde_json::json!([
|
||||
{"tag_name": "v0.1.0-bench1", "assets": [
|
||||
{"name": "SHA256SUMS", "browser_download_url": "u0"},
|
||||
{"name": "alkane-os-0.1.0-am335x-arm.afw", "browser_download_url": "u1"},
|
||||
{"name": "alkane-os-0.1.0-zynq-7010-armv7.afw", "browser_download_url": "u2"},
|
||||
]},
|
||||
{"tag_name": "v0.0.9", "assets": [
|
||||
{"name": "alkane-os-0.0.9-zynq-7010-armv7.afw", "browser_download_url": "old"},
|
||||
]},
|
||||
]);
|
||||
assert_eq!(
|
||||
pick_asset(&releases, "zynq-7010"),
|
||||
Some(("alkane-os-0.1.0-zynq-7010-armv7.afw".into(), "u2".into()))
|
||||
);
|
||||
assert_eq!(pick_asset(&releases, "cv1835"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn list_parses_releases_and_filters_afw_assets() {
|
||||
let releases = serde_json::json!([
|
||||
{"tag_name": "v0.2.0-bench1", "name": "bench", "prerelease": true, "draft": false,
|
||||
"published_at": "2026-11-11T10:00:00Z", "assets": [
|
||||
{"name": "SHA256SUMS", "browser_download_url": "s"},
|
||||
{"name": "alkane-os-0.2.0-zynq-7010-armv7.afw", "browser_download_url": "z"},
|
||||
]},
|
||||
{"tag_name": "v0.1.0", "name": "", "prerelease": false, "draft": false,
|
||||
"published_at": "2026-10-01T00:00:00Z", "assets": [
|
||||
{"name": "alkane-os-0.1.0-am335x-arm.afw", "browser_download_url": "a"},
|
||||
]},
|
||||
]);
|
||||
let parsed = parse_releases(&releases);
|
||||
assert_eq!(parsed.len(), 2);
|
||||
// Newest first; only .afw assets kept; SHA256SUMS dropped.
|
||||
assert_eq!(parsed[0].tag, "v0.2.0-bench1");
|
||||
assert!(parsed[0].prerelease);
|
||||
assert_eq!(parsed[0].afw_assets.len(), 1);
|
||||
// The board filter matches the -<board>- segment.
|
||||
assert_eq!(
|
||||
parsed[0]
|
||||
.asset_for_board("zynq-7010")
|
||||
.map(|a| a.url.as_str()),
|
||||
Some("z")
|
||||
);
|
||||
assert!(parsed[0].asset_for_board("am335x").is_none());
|
||||
assert_eq!(
|
||||
parsed[1].asset_for_board("am335x").map(|a| a.name.as_str()),
|
||||
Some("alkane-os-0.1.0-am335x-arm.afw")
|
||||
);
|
||||
assert!(parsed[1].asset_for_board("zynq-7010").is_none());
|
||||
}
|
||||
}
|
||||
@@ -1 +1,260 @@
|
||||
// alkane-artifacts
|
||||
// SPDX-License-Identifier: MIT
|
||||
//! Verify local firmware artifacts before an install path consumes them.
|
||||
|
||||
pub mod afw;
|
||||
pub mod fetch;
|
||||
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::collections::HashSet;
|
||||
use std::fs::File;
|
||||
use std::io::{Read, Seek, SeekFrom};
|
||||
use std::path::{Component, Path, PathBuf};
|
||||
use thiserror::Error;
|
||||
|
||||
const SEED: &str = include_str!("../data/manifest-seed.csv");
|
||||
const HEADER: &str = "sha256,size_bytes,path,family,format,applicability";
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub struct ArtifactRecord {
|
||||
pub sha256: String,
|
||||
pub size_bytes: u64,
|
||||
pub path: PathBuf,
|
||||
pub family: String,
|
||||
pub format: String,
|
||||
pub applicability: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum VerifyError {
|
||||
#[error("artifact I/O: {0}")]
|
||||
Io(#[from] std::io::Error),
|
||||
#[error("manifest line {0} is invalid")]
|
||||
Manifest(usize),
|
||||
#[error("artifact size differs from manifest")]
|
||||
Size,
|
||||
#[error("artifact signature does not match its declared format")]
|
||||
Signature,
|
||||
#[error("artifact SHA-256 differs from manifest")]
|
||||
Digest,
|
||||
#[error("artifact format is not recognized")]
|
||||
UnsupportedFormat,
|
||||
}
|
||||
|
||||
/// A verified, rewound descriptor. Consumers should read this descriptor
|
||||
/// directly instead of reopening the path after verification.
|
||||
pub struct VerifiedArtifact {
|
||||
file: File,
|
||||
record: ArtifactRecord,
|
||||
}
|
||||
|
||||
impl VerifiedArtifact {
|
||||
pub fn record(&self) -> &ArtifactRecord {
|
||||
&self.record
|
||||
}
|
||||
|
||||
pub fn reader(&mut self) -> &mut File {
|
||||
&mut self.file
|
||||
}
|
||||
|
||||
/// Recheck the same descriptor immediately before a guarded write.
|
||||
pub fn revalidate(&mut self) -> Result<(), VerifyError> {
|
||||
verify_open_file(&mut self.file, &self.record)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn seed_manifest() -> Result<Vec<ArtifactRecord>, VerifyError> {
|
||||
parse_manifest(SEED)
|
||||
}
|
||||
|
||||
pub fn parse_manifest(text: &str) -> Result<Vec<ArtifactRecord>, VerifyError> {
|
||||
let mut lines = text.lines();
|
||||
if lines.next() != Some(HEADER) {
|
||||
return Err(VerifyError::Manifest(1));
|
||||
}
|
||||
let mut paths = HashSet::new();
|
||||
let mut records = Vec::new();
|
||||
for (line_number, line) in lines.enumerate() {
|
||||
let line_number = line_number + 2;
|
||||
let parts: Vec<_> = line.splitn(6, ',').collect();
|
||||
if parts.len() != 6 || parts.iter().any(|part| part.trim().is_empty()) {
|
||||
return Err(VerifyError::Manifest(line_number));
|
||||
}
|
||||
let digest = parts[0];
|
||||
let path = PathBuf::from(parts[2]);
|
||||
if digest.len() != 64
|
||||
|| !digest.bytes().all(|byte| byte.is_ascii_hexdigit())
|
||||
|| !path
|
||||
.components()
|
||||
.all(|part| matches!(part, Component::Normal(_)))
|
||||
|| !paths.insert(path.clone())
|
||||
{
|
||||
return Err(VerifyError::Manifest(line_number));
|
||||
}
|
||||
let size_bytes = parts[1]
|
||||
.parse::<u64>()
|
||||
.map_err(|_| VerifyError::Manifest(line_number))?;
|
||||
if size_bytes == 0 {
|
||||
return Err(VerifyError::Manifest(line_number));
|
||||
}
|
||||
records.push(ArtifactRecord {
|
||||
sha256: digest.to_ascii_lowercase(),
|
||||
size_bytes,
|
||||
path,
|
||||
family: parts[3].to_owned(),
|
||||
format: parts[4].to_owned(),
|
||||
applicability: parts[5].to_owned(),
|
||||
});
|
||||
}
|
||||
if records.is_empty() {
|
||||
return Err(VerifyError::Manifest(1));
|
||||
}
|
||||
Ok(records)
|
||||
}
|
||||
|
||||
fn signature_matches(format: &str, first: &[u8]) -> Result<bool, VerifyError> {
|
||||
let prefix: &[u8] = match format {
|
||||
"IMAGEWTY-SD" | "update-zip" | "zip-installer" => b"PK\x03\x04",
|
||||
"ubifs" => b"\x31\x18\x10\x06",
|
||||
"swu" => b"070702",
|
||||
"wm-package-0x9a5ba57c" => b"\x7c\xa5\x5b\x9a",
|
||||
"bmu" => b"\x26\x01",
|
||||
"bin-AES-container" => b"\x3f\x0a\x64\xae",
|
||||
_ => return Err(VerifyError::UnsupportedFormat),
|
||||
};
|
||||
Ok(first.starts_with(prefix))
|
||||
}
|
||||
|
||||
pub fn verify_file(path: &Path, record: &ArtifactRecord) -> Result<VerifiedArtifact, VerifyError> {
|
||||
let mut file = File::open(path)?;
|
||||
verify_open_file(&mut file, record)?;
|
||||
Ok(VerifiedArtifact {
|
||||
file,
|
||||
record: record.clone(),
|
||||
})
|
||||
}
|
||||
|
||||
fn verify_open_file(file: &mut File, record: &ArtifactRecord) -> Result<(), VerifyError> {
|
||||
if file.metadata()?.len() != record.size_bytes {
|
||||
return Err(VerifyError::Size);
|
||||
}
|
||||
file.seek(SeekFrom::Start(0))?;
|
||||
let mut first = [0_u8; 16];
|
||||
let length = file.read(&mut first)?;
|
||||
if !signature_matches(&record.format, &first[..length])? {
|
||||
return Err(VerifyError::Signature);
|
||||
}
|
||||
file.seek(SeekFrom::Start(0))?;
|
||||
let mut hasher = Sha256::new();
|
||||
let mut buffer = [0_u8; 64 * 1024];
|
||||
let mut read_bytes = 0_u64;
|
||||
loop {
|
||||
let length = file.read(&mut buffer)?;
|
||||
if length == 0 {
|
||||
break;
|
||||
}
|
||||
read_bytes += length as u64;
|
||||
hasher.update(&buffer[..length]);
|
||||
}
|
||||
if read_bytes != record.size_bytes {
|
||||
return Err(VerifyError::Size);
|
||||
}
|
||||
let digest = hasher.finalize();
|
||||
let actual: String = digest.iter().map(|byte| format!("{byte:02x}")).collect();
|
||||
if actual != record.sha256 {
|
||||
return Err(VerifyError::Digest);
|
||||
}
|
||||
file.seek(SeekFrom::Start(0))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::io::Write;
|
||||
|
||||
#[test]
|
||||
fn manifest_has_unique_safe_records() {
|
||||
let records = seed_manifest().unwrap();
|
||||
assert_eq!(records.len(), 14);
|
||||
let traversal = format!("{HEADER}\n{},12,../escape,bin,bmu,all\n", "0".repeat(64));
|
||||
assert!(matches!(
|
||||
parse_manifest(&traversal),
|
||||
Err(VerifyError::Manifest(2))
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn checks_size_signature_and_digest_before_consumption() {
|
||||
let path =
|
||||
std::env::temp_dir().join(format!("alkane-artifact-test-{}", std::process::id()));
|
||||
let bytes = b"PK\x03\x04example";
|
||||
let mut file = File::create(&path).unwrap();
|
||||
file.write_all(bytes).unwrap();
|
||||
drop(file);
|
||||
let mut record = ArtifactRecord {
|
||||
sha256: Sha256::digest(bytes)
|
||||
.iter()
|
||||
.map(|byte| format!("{byte:02x}"))
|
||||
.collect(),
|
||||
size_bytes: bytes.len() as u64,
|
||||
path: PathBuf::from("fixture.zip"),
|
||||
family: "test".to_owned(),
|
||||
format: "update-zip".to_owned(),
|
||||
applicability: "test".to_owned(),
|
||||
};
|
||||
let mut verified = verify_file(&path, &record).unwrap();
|
||||
verified.revalidate().unwrap();
|
||||
let mut replay = Vec::new();
|
||||
verified.reader().read_to_end(&mut replay).unwrap();
|
||||
assert_eq!(replay, bytes);
|
||||
std::fs::write(&path, b"PK\x03\x04changed").unwrap();
|
||||
assert!(matches!(verified.revalidate(), Err(VerifyError::Digest)));
|
||||
std::fs::write(&path, bytes).unwrap();
|
||||
verified.revalidate().unwrap();
|
||||
record.size_bytes += 1;
|
||||
assert!(matches!(
|
||||
verify_file(&path, &record),
|
||||
Err(VerifyError::Size)
|
||||
));
|
||||
record.size_bytes -= 1;
|
||||
record.format = "ubifs".to_owned();
|
||||
assert!(matches!(
|
||||
verify_file(&path, &record),
|
||||
Err(VerifyError::Signature)
|
||||
));
|
||||
record.format = "update-zip".to_owned();
|
||||
record.sha256 = "0".repeat(64);
|
||||
assert!(matches!(
|
||||
verify_file(&path, &record),
|
||||
Err(VerifyError::Digest)
|
||||
));
|
||||
std::fs::remove_file(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "microbenchmark"]
|
||||
fn bench_verify_artifact() {
|
||||
let path =
|
||||
std::env::temp_dir().join(format!("alkane-artifact-bench-{}", std::process::id()));
|
||||
let mut bytes = vec![0x42_u8; 65_536];
|
||||
bytes[..4].copy_from_slice(b"PK\x03\x04");
|
||||
std::fs::write(&path, &bytes).unwrap();
|
||||
let record = ArtifactRecord {
|
||||
sha256: Sha256::digest(&bytes)
|
||||
.iter()
|
||||
.map(|byte| format!("{byte:02x}"))
|
||||
.collect(),
|
||||
size_bytes: bytes.len() as u64,
|
||||
path: PathBuf::from("bench.zip"),
|
||||
family: "test".to_owned(),
|
||||
format: "update-zip".to_owned(),
|
||||
applicability: "test".to_owned(),
|
||||
};
|
||||
let started = std::time::Instant::now();
|
||||
for _ in 0..100 {
|
||||
std::hint::black_box(verify_file(&path, &record).unwrap());
|
||||
}
|
||||
println!("bench_verify_artifact: {:?}", started.elapsed() / 100);
|
||||
std::fs::remove_file(path).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,3 +6,6 @@ license.workspace = true
|
||||
|
||||
[dependencies]
|
||||
alkane-core = { path = "../alkane-core" }
|
||||
alkane-artifacts = { path = "../alkane-artifacts" }
|
||||
alkane-flash = { path = "../alkane-flash" }
|
||||
serde_json.workspace = true
|
||||
|
||||
@@ -1 +1,3 @@
|
||||
// alkane-core
|
||||
|
||||
pub mod platform;
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
//! The few places the toolkit's behaviour depends on the operating system,
|
||||
//! kept together so the rest of the code stays platform-neutral.
|
||||
//!
|
||||
//! - where per-user settings live ([`config_dir`]),
|
||||
//! - how a secret file is written so only its owner can read it
|
||||
//! ([`write_private`]),
|
||||
//! - the null device for tools that insist on an output path ([`NULL_DEVICE`]),
|
||||
//! - how helper programs (`ssh`, `curl`) are started from the GUI without a
|
||||
//! console window popping up on Windows ([`quiet_command`]).
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::Command;
|
||||
|
||||
/// Directory name of the toolkit's settings under the per-user config root.
|
||||
pub const APP_DIR: &str = "alkane-toolkit";
|
||||
|
||||
/// Environment variable whose value is the per-user config root on this
|
||||
/// platform: `HOME` (settings in `$HOME/.config/alkane-toolkit`) on Unix,
|
||||
/// `APPDATA` (settings in `%APPDATA%\alkane-toolkit`) on Windows. Tests point it
|
||||
/// at a scratch directory.
|
||||
#[cfg(not(windows))]
|
||||
pub const CONFIG_ROOT_VAR: &str = "HOME";
|
||||
#[cfg(windows)]
|
||||
pub const CONFIG_ROOT_VAR: &str = "APPDATA";
|
||||
|
||||
/// The per-user settings directory (not created here).
|
||||
///
|
||||
/// Unix keeps the long-standing `$HOME/.config/alkane-toolkit`. Windows uses
|
||||
/// the roaming application-data folder, `%APPDATA%\alkane-toolkit`, which is
|
||||
/// where Windows applications keep per-user settings and which the user
|
||||
/// profile's ACL already restricts to its owner. A missing variable falls back
|
||||
/// to the working directory, as before.
|
||||
pub fn config_dir() -> PathBuf {
|
||||
let root = std::env::var_os(CONFIG_ROOT_VAR).unwrap_or_else(|| ".".into());
|
||||
if cfg!(windows) {
|
||||
PathBuf::from(root).join(APP_DIR)
|
||||
} else {
|
||||
PathBuf::from(root).join(".config").join(APP_DIR)
|
||||
}
|
||||
}
|
||||
|
||||
/// `config_dir()/file`.
|
||||
pub fn config_path(file: &str) -> PathBuf {
|
||||
config_dir().join(file)
|
||||
}
|
||||
|
||||
/// The null device, for tools that want an output path (`curl -o`,
|
||||
/// `ssh -o UserKnownHostsFile=`).
|
||||
#[cfg(not(windows))]
|
||||
pub const NULL_DEVICE: &str = "/dev/null";
|
||||
#[cfg(windows)]
|
||||
pub const NULL_DEVICE: &str = "NUL";
|
||||
|
||||
/// Write `data` to `path` so only the owner can read it.
|
||||
///
|
||||
/// On Unix the file is created 0600 (so a secret is never momentarily
|
||||
/// world-readable between write and chmod), and a pre-existing file is
|
||||
/// re-tightened while it is still empty, before the plaintext is written. On
|
||||
/// Windows the settings directory inherits the user profile's owner-only ACL,
|
||||
/// so a plain write already has the equivalent protection.
|
||||
pub fn write_private(path: &Path, data: &[u8]) -> std::io::Result<()> {
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::io::Write;
|
||||
use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};
|
||||
let mut f = std::fs::OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
.truncate(true)
|
||||
.mode(0o600)
|
||||
.open(path)?;
|
||||
f.set_permissions(std::fs::Permissions::from_mode(0o600))?;
|
||||
f.write_all(data)
|
||||
}
|
||||
#[cfg(not(unix))]
|
||||
{
|
||||
std::fs::write(path, data)
|
||||
}
|
||||
}
|
||||
|
||||
/// A [`Command`] for a helper program run in the background of the GUI.
|
||||
///
|
||||
/// On Windows a console program (`ssh.exe`, `curl.exe`) started from a GUI
|
||||
/// process gets a new console window by default, so every probe would flash a
|
||||
/// black window; `CREATE_NO_WINDOW` suppresses that while keeping its piped
|
||||
/// stdin/stdout/stderr. Elsewhere this is `Command::new`.
|
||||
pub fn quiet_command(program: &str) -> Command {
|
||||
#[allow(unused_mut)]
|
||||
let mut command = Command::new(program);
|
||||
#[cfg(windows)]
|
||||
{
|
||||
use std::os::windows::process::CommandExt;
|
||||
const CREATE_NO_WINDOW: u32 = 0x0800_0000;
|
||||
command.creation_flags(CREATE_NO_WINDOW);
|
||||
}
|
||||
command
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn config_dir_is_under_the_platform_root() {
|
||||
let dir = config_dir();
|
||||
assert!(dir.ends_with(APP_DIR), "{dir:?}");
|
||||
if cfg!(windows) {
|
||||
assert!(!dir.to_string_lossy().contains(".config"), "{dir:?}");
|
||||
} else {
|
||||
assert!(dir.parent().unwrap().ends_with(".config"), "{dir:?}");
|
||||
}
|
||||
assert_eq!(config_path("x.txt"), dir.join("x.txt"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn write_private_round_trips_and_restricts() {
|
||||
let dir = std::env::temp_dir().join(format!("alkane-private-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join("secret.txt");
|
||||
write_private(&path, b"one").unwrap();
|
||||
write_private(&path, b"two").unwrap();
|
||||
assert_eq!(std::fs::read(&path).unwrap(), b"two");
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let mode = std::fs::metadata(&path).unwrap().permissions().mode() & 0o777;
|
||||
assert_eq!(mode, 0o600);
|
||||
}
|
||||
std::fs::remove_dir_all(&dir).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn quiet_command_runs_with_piped_output() {
|
||||
// `cmd /C echo` and `sh -c echo` are the platform's own shells.
|
||||
let mut command = if cfg!(windows) {
|
||||
let mut c = quiet_command("cmd");
|
||||
c.args(["/C", "echo alkane"]);
|
||||
c
|
||||
} else {
|
||||
let mut c = quiet_command("sh");
|
||||
c.args(["-c", "echo alkane"]);
|
||||
c
|
||||
};
|
||||
let out = command.output().unwrap();
|
||||
assert!(out.status.success());
|
||||
assert!(String::from_utf8_lossy(&out.stdout).contains("alkane"));
|
||||
}
|
||||
}
|
||||
@@ -6,3 +6,8 @@ license.workspace = true
|
||||
|
||||
[dependencies]
|
||||
alkane-core = { path = "../alkane-core" }
|
||||
alkane-artifacts = { path = "../alkane-artifacts" }
|
||||
serde_json.workspace = true
|
||||
sha2.workspace = true
|
||||
thiserror.workspace = true
|
||||
tempfile = "3"
|
||||
|
||||
@@ -0,0 +1,880 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
//! Amlogic A113D / AXG (Antminer S21 Pro-class) board module: tier-2 network
|
||||
//! UPDATE of a board already running alkane. Our OS is staged as FILES in the
|
||||
//! nvdata UBI volume: alkane/{vmlinux.bin, devicetree.dtb, uramdisk.image.gz}.
|
||||
//! (The legacy vendor path name "anthillos" from AnthillOS is no longer used;
|
||||
//! our own bootcmd loads from alkane/.)
|
||||
//!
|
||||
//! The running alkane image exposes ONLY the nvdata region as an MTD (the board
|
||||
//! DT declares a single `nvdata` fixed-partition; the FIP bootloader and the
|
||||
//! stock boot/recovery/misc/config regions are deliberately undeclared, so they
|
||||
//! are never registered as MTD devices and cannot be written from Linux). This
|
||||
//! module attaches UBI to that one nvdata MTD, mounts the UBIFS volume, and
|
||||
//! overwrites the three files under alkane/. It performs no raw mtd0-5 write and
|
||||
//! never changes the bootloader: the brick-sensitive regions are physically
|
||||
//! unreachable.
|
||||
//!
|
||||
//! The first from-stock install (past the signed bootloader) is handled
|
||||
//! elsewhere and is NOT this module. This is the repeatable alkane-to-alkane
|
||||
//! update.
|
||||
|
||||
use crate::md5::md5_hex;
|
||||
use crate::preflight::{classify, parse_digest};
|
||||
use crate::transport::{Remote, RemoteError};
|
||||
use alkane_artifacts::afw::{AfwImage, BootloaderPolicy, VerifiedAfw};
|
||||
use std::fs;
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
use thiserror::Error;
|
||||
|
||||
pub const BOARD: &str = "a113d";
|
||||
pub const ARCH: &str = "arm64";
|
||||
/// The UBI volume subdirectory our boot chain reads our OS files from. This is
|
||||
/// alkane's own name; the board's `ourbootcmd` loads from here.
|
||||
pub const ALKANE_DIR: &str = "alkane";
|
||||
/// nvdata UBI usable size; the three files together must fit (0xB900000).
|
||||
pub const NVDATA_USABLE: u64 = 0xB900000;
|
||||
/// The only MTD partition our DT exposes. mtd0-5 (FIP + stock boot) are not
|
||||
/// declared in the DT at all, so they are not MTD devices on a running image.
|
||||
pub const NVDATA_LABEL: &str = "nvdata";
|
||||
|
||||
/// Authoritative file targets: (image, on-board basename, ceiling, format).
|
||||
/// Written rootfs -> dtb -> kernel so a mid-write interruption never leaves a
|
||||
/// new rootfs beside an old kernel (the kernel file lands last).
|
||||
const TARGETS: [(&str, &str, u64, &str); 3] = [
|
||||
("rootfs", "uramdisk.image.gz", 0x6000000, "uimage"),
|
||||
("dtb", "devicetree.dtb", 0x100000, "fdt"),
|
||||
("kernel", "vmlinux.bin", 0x4000000, "arm64-image"),
|
||||
];
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum A113dPlanError {
|
||||
#[error("bundle is for {0}, not a113d")]
|
||||
WrongBoard(String),
|
||||
#[error("bundle arch is {0}, a113d needs arm64")]
|
||||
WrongArch(String),
|
||||
#[error("a113d requires bootloader_policy preserve (the FIP is never written)")]
|
||||
BootloaderPolicy,
|
||||
#[error("a113d bundle carries a bootloader/spl/fip/tpl member: {0}")]
|
||||
BootloaderMember(String),
|
||||
#[error("manifest is not an OS payload")]
|
||||
PayloadType,
|
||||
#[error("manifest marker is empty")]
|
||||
Marker,
|
||||
#[error("a113d must install exactly kernel, dtb and rootfs")]
|
||||
InstallCount,
|
||||
#[error("a113d is missing the {0} image")]
|
||||
MissingImage(String),
|
||||
#[error("a113d {image} format is {actual}, expected {expected}")]
|
||||
ImageFormat {
|
||||
image: String,
|
||||
actual: String,
|
||||
expected: &'static str,
|
||||
},
|
||||
#[error("a113d {image} is {size} bytes, over the 0x{ceiling:x}-byte ceiling")]
|
||||
ImageTooLarge {
|
||||
image: String,
|
||||
size: u64,
|
||||
ceiling: u64,
|
||||
},
|
||||
#[error("a113d images total {total} bytes, over the nvdata UBI (0x{usable:x})")]
|
||||
OverNvdata { total: u64, usable: u64 },
|
||||
#[error("a113d {image} is not an arm64 Image")]
|
||||
NotArm64 { image: String },
|
||||
#[error("a113d {image} is not an FDT")]
|
||||
NotFdt { image: String },
|
||||
#[error("a113d {image} is not a legacy uImage")]
|
||||
NotUimage { image: String },
|
||||
#[error("a113d refuses: preserved partition {0} (mtd0-5) carries an image")]
|
||||
PreservedCarriesImage(String),
|
||||
#[error("a113d refuses an unsafe destination for {image}: {dest}")]
|
||||
UnsafeDest { image: String, dest: String },
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct A113dTarget {
|
||||
pub image_name: String,
|
||||
/// On-board basename inside the alkane directory (never a device path).
|
||||
pub dest: String,
|
||||
pub size: u64,
|
||||
pub data: Vec<u8>,
|
||||
pub expected_md5: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct A113dPlan {
|
||||
pub targets: Vec<A113dTarget>,
|
||||
pub marker: String,
|
||||
pub kernel_version: String,
|
||||
pub sdk_revision: String,
|
||||
pub os_revision: String,
|
||||
pub afw_sha256: String,
|
||||
pub min_free_bytes: u64,
|
||||
}
|
||||
|
||||
fn is_bootloaderish(image: &AfwImage) -> bool {
|
||||
let lowered = format!("{} {} {}", image.name, image.file, image.role).to_lowercase();
|
||||
image.role == "bootloader"
|
||||
|| lowered.contains("spl")
|
||||
|| lowered.contains("u-boot")
|
||||
|| lowered.contains("uboot")
|
||||
|| lowered.contains("fip")
|
||||
|| lowered.contains("tpl")
|
||||
}
|
||||
|
||||
/// A destination is safe only as a bare basename (no separators, no traversal,
|
||||
/// not a device path). This keeps every write inside the alkane/ directory.
|
||||
fn is_safe_dest(dest: &str) -> bool {
|
||||
!dest.is_empty()
|
||||
&& !dest.contains('/')
|
||||
&& !dest.contains("..")
|
||||
&& !dest.starts_with("/dev/")
|
||||
&& dest == dest.trim()
|
||||
}
|
||||
|
||||
impl A113dPlan {
|
||||
pub fn from_afw(afw: &VerifiedAfw) -> Result<Self, A113dPlanError> {
|
||||
let manifest = &afw.manifest;
|
||||
if manifest.board != BOARD {
|
||||
return Err(A113dPlanError::WrongBoard(manifest.board.clone()));
|
||||
}
|
||||
if manifest.arch != ARCH {
|
||||
return Err(A113dPlanError::WrongArch(manifest.arch.clone()));
|
||||
}
|
||||
if manifest.payload_type != "os" {
|
||||
return Err(A113dPlanError::PayloadType);
|
||||
}
|
||||
if manifest.bootloader_policy != BootloaderPolicy::Preserve {
|
||||
return Err(A113dPlanError::BootloaderPolicy);
|
||||
}
|
||||
if manifest.marker.is_empty() {
|
||||
return Err(A113dPlanError::Marker);
|
||||
}
|
||||
// FIP / bootloader members are never allowed - we reuse the board's.
|
||||
for image in &manifest.images {
|
||||
if is_bootloaderish(image) {
|
||||
return Err(A113dPlanError::BootloaderMember(image.file.clone()));
|
||||
}
|
||||
}
|
||||
// Defense in depth: only nvdata (mtd6) may carry an image; mtd0-5 preserved.
|
||||
for partition in &manifest.partitions {
|
||||
if partition.name != "nvdata" && partition.image.is_some() {
|
||||
return Err(A113dPlanError::PreservedCarriesImage(
|
||||
partition.name.clone(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
let installs: Vec<&AfwImage> = manifest.images.iter().filter(|i| i.install).collect();
|
||||
if installs.len() != TARGETS.len() {
|
||||
return Err(A113dPlanError::InstallCount);
|
||||
}
|
||||
|
||||
let mut targets = Vec::with_capacity(TARGETS.len());
|
||||
let mut total = 0u64;
|
||||
for (image_name, dest, ceiling, expected_format) in TARGETS {
|
||||
let image = manifest
|
||||
.images
|
||||
.iter()
|
||||
.find(|i| i.name == image_name && i.install)
|
||||
.ok_or_else(|| A113dPlanError::MissingImage(image_name.to_owned()))?;
|
||||
if image.format != expected_format {
|
||||
return Err(A113dPlanError::ImageFormat {
|
||||
image: image_name.to_owned(),
|
||||
actual: image.format.clone(),
|
||||
expected: expected_format,
|
||||
});
|
||||
}
|
||||
if image.size > ceiling {
|
||||
return Err(A113dPlanError::ImageTooLarge {
|
||||
image: image_name.to_owned(),
|
||||
size: image.size,
|
||||
ceiling,
|
||||
});
|
||||
}
|
||||
if !is_safe_dest(dest) {
|
||||
return Err(A113dPlanError::UnsafeDest {
|
||||
image: image_name.to_owned(),
|
||||
dest: dest.to_owned(),
|
||||
});
|
||||
}
|
||||
let data = afw
|
||||
.image(image_name)
|
||||
.map(|(_, data)| data)
|
||||
.ok_or_else(|| A113dPlanError::MissingImage(image_name.to_owned()))?;
|
||||
// Each arm validates a different magic; keeping them as explicit
|
||||
// arms (not match guards) reads more clearly than collapsing.
|
||||
#[allow(clippy::collapsible_match)]
|
||||
match expected_format {
|
||||
"arm64-image" => {
|
||||
if !(data.len() >= 0x3C && data[0x38..0x3C] == *b"ARMd") {
|
||||
return Err(A113dPlanError::NotArm64 {
|
||||
image: image_name.to_owned(),
|
||||
});
|
||||
}
|
||||
}
|
||||
"fdt" => {
|
||||
if !(data.len() >= 4 && data[0..4] == [0xd0, 0x0d, 0xfe, 0xed]) {
|
||||
return Err(A113dPlanError::NotFdt {
|
||||
image: image_name.to_owned(),
|
||||
});
|
||||
}
|
||||
}
|
||||
"uimage" => {
|
||||
if !(data.len() >= 4 && data[0..4] == [0x27, 0x05, 0x19, 0x56]) {
|
||||
return Err(A113dPlanError::NotUimage {
|
||||
image: image_name.to_owned(),
|
||||
});
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
total += image.size;
|
||||
targets.push(A113dTarget {
|
||||
image_name: image_name.to_owned(),
|
||||
dest: dest.to_owned(),
|
||||
size: image.size,
|
||||
expected_md5: md5_hex(data),
|
||||
data: data.to_vec(),
|
||||
});
|
||||
}
|
||||
if total > NVDATA_USABLE {
|
||||
return Err(A113dPlanError::OverNvdata {
|
||||
total,
|
||||
usable: NVDATA_USABLE,
|
||||
});
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
targets,
|
||||
marker: manifest.marker.clone(),
|
||||
kernel_version: manifest.kernel_version.clone(),
|
||||
sdk_revision: manifest.sdk_revision.clone(),
|
||||
os_revision: manifest.os_revision.clone(),
|
||||
afw_sha256: afw.archive_sha256.clone(),
|
||||
min_free_bytes: manifest
|
||||
.preflight
|
||||
.get("min_free_bytes")
|
||||
.and_then(|v| v.as_u64())
|
||||
.unwrap_or(16 * 1024 * 1024),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn render(&self) -> String {
|
||||
let mut out = format!("board {BOARD} arch {ARCH}\n");
|
||||
out.push_str(
|
||||
"tier 2 update: attach UBI to the nvdata MTD, mount it, and overwrite the OS files \
|
||||
under alkane/; the FIP + stock boot/recovery/misc/config are not MTD devices at all, \
|
||||
so no raw NAND write is possible\n",
|
||||
);
|
||||
out.push_str("image format kernel=arm64-image, dtb=fdt, rootfs=uimage\n");
|
||||
for target in &self.targets {
|
||||
out.push_str(&format!(
|
||||
"write {} -> {ALKANE_DIR}/{} size {} B md5 {}\n",
|
||||
target.image_name, target.dest, target.size, target.expected_md5
|
||||
));
|
||||
}
|
||||
out.push_str(&format!(
|
||||
"marker {} kernel {} sdk {} os {}",
|
||||
self.marker,
|
||||
self.kernel_version,
|
||||
short(&self.sdk_revision),
|
||||
short(&self.os_revision),
|
||||
));
|
||||
out
|
||||
}
|
||||
}
|
||||
|
||||
fn short(rev: &str) -> &str {
|
||||
rev.get(..12).unwrap_or(rev)
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum A113dError {
|
||||
#[error("preflight failed:\n{0}")]
|
||||
Preflight(String),
|
||||
#[error("--confirm FLASH is required to write")]
|
||||
Confirm,
|
||||
#[error("the running image exposes no '{0}' MTD; it needs a NAND-enabled a113d kernel")]
|
||||
NoNvdataMtd(String),
|
||||
#[error("could not attach UBI to the nvdata MTD / find its volume")]
|
||||
UbiAttach,
|
||||
#[error("could not mount the nvdata UBIFS volume")]
|
||||
Mount,
|
||||
#[error("refusing an unsafe destination: {0}")]
|
||||
UnsafeDest(String),
|
||||
#[error("{0} staged md5 differs from the verified bundle")]
|
||||
StageMismatch(String),
|
||||
#[error("{0} read-back md5 mismatch; do not reboot")]
|
||||
Readback(String),
|
||||
#[error(transparent)]
|
||||
Remote(#[from] RemoteError),
|
||||
#[error(transparent)]
|
||||
Io(#[from] std::io::Error),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct A113dCheck {
|
||||
pub name: String,
|
||||
pub ok: bool,
|
||||
pub detail: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct A113dPreflight {
|
||||
pub host: String,
|
||||
pub checks: Vec<A113dCheck>,
|
||||
/// The nvdata MTD index (e.g. 0 for /dev/mtd0), when the running image
|
||||
/// exposes it. Used for `ubiattach -m <index>`.
|
||||
pub nvdata_mtd: Option<u32>,
|
||||
}
|
||||
|
||||
impl A113dPreflight {
|
||||
pub fn ok(&self) -> bool {
|
||||
self.checks.iter().all(|c| c.ok)
|
||||
}
|
||||
pub fn render(&self) -> String {
|
||||
let mut out = format!("preflight {}\n", self.host);
|
||||
for c in &self.checks {
|
||||
out.push_str(&format!(
|
||||
"check {} {}: {}\n",
|
||||
c.name,
|
||||
if c.ok { "ok" } else { "FAIL" },
|
||||
c.detail
|
||||
));
|
||||
}
|
||||
out.push_str(&format!(
|
||||
"result {}",
|
||||
if self.ok() {
|
||||
"PASS"
|
||||
} else {
|
||||
"FAIL (do not write)"
|
||||
}
|
||||
));
|
||||
out
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse /proc/mtd for a partition labelled "nvdata" and return its mtd index.
|
||||
/// e.g. `mtd0: 0b900000 00020000 "nvdata"` -> Some(0).
|
||||
fn parse_nvdata_mtd(proc_mtd: &str) -> Option<u32> {
|
||||
for line in proc_mtd.lines() {
|
||||
let line = line.trim();
|
||||
if !line.starts_with("mtd") {
|
||||
continue;
|
||||
}
|
||||
let (dev, rest) = line.split_once(':')?;
|
||||
if rest.contains(&format!("\"{NVDATA_LABEL}\"")) {
|
||||
return dev.trim_start_matches("mtd").parse::<u32>().ok();
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn preflight(
|
||||
plan: &A113dPlan,
|
||||
remote: &mut dyn Remote,
|
||||
host: &str,
|
||||
) -> Result<A113dPreflight, A113dError> {
|
||||
let mut checks = Vec::new();
|
||||
|
||||
let fingerprint = remote.user_command(
|
||||
"uname -r; echo __MODEL__; cat /proc/device-tree/model 2>/dev/null; echo; \
|
||||
echo __RELEASE__; cat /etc/os-release 2>/dev/null; cat /etc/alkane-release 2>/dev/null \
|
||||
|| true",
|
||||
Duration::from_secs(20),
|
||||
)?;
|
||||
let text = String::from_utf8_lossy(&fingerprint).into_owned();
|
||||
let mut it = text.split("__MODEL__");
|
||||
let kernel = it.next().unwrap_or("").trim().to_owned();
|
||||
let rest = it.next().unwrap_or("");
|
||||
let mut it2 = rest.split("__RELEASE__");
|
||||
let _model = it2.next().unwrap_or("");
|
||||
let release = it2.next().unwrap_or("");
|
||||
let source = classify(&kernel, release, &text);
|
||||
checks.push(A113dCheck {
|
||||
name: "source-os".to_owned(),
|
||||
ok: source == "alkane",
|
||||
detail: format!("{source} (update path requires alkane)"),
|
||||
});
|
||||
|
||||
let uid = String::from_utf8_lossy(&remote.root_command("id -u", Duration::from_secs(20))?)
|
||||
.trim()
|
||||
.to_owned();
|
||||
checks.push(A113dCheck {
|
||||
name: "root".to_owned(),
|
||||
ok: uid == "0",
|
||||
detail: format!("uid {uid}"),
|
||||
});
|
||||
|
||||
let tools = remote.user_command(
|
||||
"for c in ubiattach ubidetach mount umount cp md5sum sync; do \
|
||||
command -v $c >/dev/null 2>&1 && echo $c || echo missing:$c; done",
|
||||
Duration::from_secs(15),
|
||||
)?;
|
||||
let missing: Vec<String> = String::from_utf8_lossy(&tools)
|
||||
.lines()
|
||||
.filter_map(|l| l.strip_prefix("missing:").map(str::to_owned))
|
||||
.collect();
|
||||
checks.push(A113dCheck {
|
||||
name: "commands".to_owned(),
|
||||
ok: missing.is_empty(),
|
||||
detail: if missing.is_empty() {
|
||||
"ubi/file tools present".to_owned()
|
||||
} else {
|
||||
format!("missing {}", missing.join(", "))
|
||||
},
|
||||
});
|
||||
|
||||
// The nvdata MTD must be exposed (NAND-enabled a113d kernel). If absent, the
|
||||
// running image cannot self-update - it needs the DT with the nvdata
|
||||
// fixed-partition. mtd0-5 are intentionally never exposed, so nvdata is the
|
||||
// ONLY mtd this board should ever present.
|
||||
let proc_mtd = String::from_utf8_lossy(
|
||||
&remote.user_command("cat /proc/mtd 2>/dev/null", Duration::from_secs(20))?,
|
||||
)
|
||||
.into_owned();
|
||||
let nvdata_mtd = parse_nvdata_mtd(&proc_mtd);
|
||||
checks.push(A113dCheck {
|
||||
name: "nvdata-mtd".to_owned(),
|
||||
ok: nvdata_mtd.is_some(),
|
||||
detail: match nvdata_mtd {
|
||||
Some(n) => format!("nvdata is /dev/mtd{n} (the only exposed MTD)"),
|
||||
None => format!("no 'nvdata' MTD exposed: {}", proc_mtd.replace('\n', " ")),
|
||||
},
|
||||
});
|
||||
|
||||
// Every destination is a bare basename - the structural brick guard.
|
||||
let dests_safe = plan.targets.iter().all(|t| is_safe_dest(&t.dest));
|
||||
checks.push(A113dCheck {
|
||||
name: "file-only".to_owned(),
|
||||
ok: dests_safe,
|
||||
detail: format!("all writes are files under {ALKANE_DIR}/ on the nvdata UBI"),
|
||||
});
|
||||
|
||||
Ok(A113dPreflight {
|
||||
host: host.to_owned(),
|
||||
checks,
|
||||
nvdata_mtd,
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct A113dInstallOptions {
|
||||
pub dry_run: bool,
|
||||
pub confirm: Option<String>,
|
||||
pub no_reboot: bool,
|
||||
pub backup_dir: PathBuf,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct A113dBackup {
|
||||
pub image_name: String,
|
||||
pub dest: String,
|
||||
pub path: Option<PathBuf>,
|
||||
pub md5: Option<String>,
|
||||
pub size: u64,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct A113dReceipt {
|
||||
pub volume: String,
|
||||
pub backups: Vec<A113dBackup>,
|
||||
pub rebooted: bool,
|
||||
}
|
||||
|
||||
pub enum A113dOutcome {
|
||||
DryRun(Box<A113dPreflight>),
|
||||
Installed(A113dReceipt),
|
||||
}
|
||||
|
||||
const MNT: &str = "/tmp/.alkane-nvdata";
|
||||
|
||||
/// Attach UBI to the nvdata MTD and mount its single user volume read-write.
|
||||
/// Returns (ubi device number, volume name). The volume name is DISCOVERED, never
|
||||
/// assumed (vendor histories leave different names: umc_os_rootfs, nvdada_log,
|
||||
/// overlay, ...).
|
||||
fn attach_and_mount(remote: &mut dyn Remote, nvdata_mtd: u32) -> Result<String, A113dError> {
|
||||
// Attach (idempotent: if ubi0 already exists, reuse it).
|
||||
let _ = remote.root_command(
|
||||
&format!(
|
||||
"if [ ! -d /sys/class/ubi/ubi0 ]; then ubiattach -m {nvdata_mtd} -d 0 >/dev/null 2>&1; fi"
|
||||
),
|
||||
Duration::from_secs(60),
|
||||
)?;
|
||||
// Discover the first user volume name under ubi0.
|
||||
let name = String::from_utf8_lossy(&remote.root_command(
|
||||
"for n in /sys/class/ubi/ubi0_*/name; do [ -f \"$n\" ] && cat \"$n\" && break; done",
|
||||
Duration::from_secs(20),
|
||||
)?)
|
||||
.trim()
|
||||
.to_owned();
|
||||
if name.is_empty() {
|
||||
return Err(A113dError::UbiAttach);
|
||||
}
|
||||
// Mount it read-write.
|
||||
remote.root_command(
|
||||
&format!("mkdir -p {MNT} && mount -t ubifs ubi0:{name} {MNT}"),
|
||||
Duration::from_secs(60),
|
||||
)?;
|
||||
// Confirm the mount actually took.
|
||||
let mounted = remote.user_command(
|
||||
&format!("mount | grep -c ' {MNT} '"),
|
||||
Duration::from_secs(20),
|
||||
)?;
|
||||
if String::from_utf8_lossy(&mounted).trim() == "0" {
|
||||
return Err(A113dError::Mount);
|
||||
}
|
||||
Ok(name)
|
||||
}
|
||||
|
||||
fn unmount(remote: &mut dyn Remote) {
|
||||
let _ = remote.root_command(
|
||||
&format!(
|
||||
"sync; umount {MNT} 2>/dev/null; ubidetach -d 0 2>/dev/null; rmdir {MNT} 2>/dev/null"
|
||||
),
|
||||
Duration::from_secs(60),
|
||||
);
|
||||
}
|
||||
|
||||
pub fn install(
|
||||
plan: &A113dPlan,
|
||||
remote: &mut dyn Remote,
|
||||
options: &A113dInstallOptions,
|
||||
host: &str,
|
||||
) -> Result<A113dOutcome, A113dError> {
|
||||
let report = preflight(plan, remote, host)?;
|
||||
if !report.ok() {
|
||||
return Err(A113dError::Preflight(report.render()));
|
||||
}
|
||||
if options.dry_run {
|
||||
return Ok(A113dOutcome::DryRun(Box::new(report)));
|
||||
}
|
||||
if options.confirm.as_deref() != Some("FLASH") {
|
||||
return Err(A113dError::Confirm);
|
||||
}
|
||||
let nvdata_mtd = report
|
||||
.nvdata_mtd
|
||||
.ok_or_else(|| A113dError::NoNvdataMtd(NVDATA_LABEL.to_owned()))?;
|
||||
|
||||
fs::create_dir_all(&options.backup_dir)?;
|
||||
let epoch = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap_or_default()
|
||||
.as_secs();
|
||||
|
||||
let volume = attach_and_mount(remote, nvdata_mtd)?;
|
||||
let alkane_dir = format!("{MNT}/{ALKANE_DIR}");
|
||||
let _ = remote.root_command(&format!("mkdir -p {alkane_dir}"), Duration::from_secs(20))?;
|
||||
|
||||
let mut backups = Vec::new();
|
||||
let result = (|| -> Result<(), A113dError> {
|
||||
for target in &plan.targets {
|
||||
// Structural guard at the point of writing: a bare basename only.
|
||||
if !is_safe_dest(&target.dest) {
|
||||
return Err(A113dError::UnsafeDest(target.dest.clone()));
|
||||
}
|
||||
let name = &target.image_name;
|
||||
let dest_path = format!("{alkane_dir}/{}", target.dest);
|
||||
|
||||
// Back up the existing file, if present.
|
||||
let have = remote.user_command(
|
||||
&format!("test -f {dest_path} && echo __YES__"),
|
||||
Duration::from_secs(15),
|
||||
)?;
|
||||
if String::from_utf8_lossy(&have).contains("__YES__") {
|
||||
let local = options
|
||||
.backup_dir
|
||||
.join(format!("{}-{epoch}.bak", target.dest));
|
||||
remote.pull(&dest_path, &local, Duration::from_secs(300))?;
|
||||
let data = fs::read(&local)?;
|
||||
backups.push(A113dBackup {
|
||||
image_name: name.clone(),
|
||||
dest: target.dest.clone(),
|
||||
md5: Some(md5_hex(&data)),
|
||||
size: data.len() as u64,
|
||||
path: Some(local),
|
||||
});
|
||||
} else {
|
||||
backups.push(A113dBackup {
|
||||
image_name: name.clone(),
|
||||
dest: target.dest.clone(),
|
||||
md5: None,
|
||||
size: 0,
|
||||
path: None,
|
||||
});
|
||||
}
|
||||
|
||||
// Stage the verified payload and prove its remote md5 before copying.
|
||||
let mut staged = tempfile::NamedTempFile::new()?;
|
||||
staged.write_all(&target.data)?;
|
||||
staged.flush()?;
|
||||
let remote_stage = format!("/tmp/.alkane-a113d-{name}.img");
|
||||
remote.push(staged.path(), &remote_stage, Duration::from_secs(300))?;
|
||||
let staged_md5 = parse_digest(
|
||||
&remote.root_command(&format!("md5sum {remote_stage}"), Duration::from_secs(60))?,
|
||||
32,
|
||||
)
|
||||
.unwrap_or_default();
|
||||
if staged_md5 != target.expected_md5 {
|
||||
let _ =
|
||||
remote.root_command(&format!("rm -f {remote_stage}"), Duration::from_secs(15));
|
||||
return Err(A113dError::StageMismatch(name.clone()));
|
||||
}
|
||||
|
||||
// Copy into the UBIFS volume (file write only), then sync.
|
||||
remote.root_command(
|
||||
&format!("cp -f {remote_stage} {dest_path} && sync"),
|
||||
Duration::from_secs(300),
|
||||
)?;
|
||||
|
||||
// Read back the copied file and compare md5.
|
||||
let readback = parse_digest(
|
||||
&remote.root_command(&format!("md5sum {dest_path}"), Duration::from_secs(60))?,
|
||||
32,
|
||||
)
|
||||
.unwrap_or_default();
|
||||
let _ = remote.root_command(&format!("rm -f {remote_stage}"), Duration::from_secs(15));
|
||||
if readback != target.expected_md5 {
|
||||
return Err(A113dError::Readback(name.clone()));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
})();
|
||||
|
||||
// Always unmount/detach, success or failure, before returning or rebooting.
|
||||
unmount(remote);
|
||||
result?;
|
||||
|
||||
let rebooted = if options.no_reboot {
|
||||
false
|
||||
} else {
|
||||
remote.reboot()?;
|
||||
true
|
||||
};
|
||||
Ok(A113dOutcome::Installed(A113dReceipt {
|
||||
volume,
|
||||
backups,
|
||||
rebooted,
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn owns_board(board: &str) -> bool {
|
||||
board == BOARD
|
||||
}
|
||||
|
||||
/// True when no plan target would ever name a preserved raw partition. Used by
|
||||
/// tests to assert the structural guard holds for the fixed TARGETS table.
|
||||
pub fn no_raw_device_targets(plan: &A113dPlan) -> bool {
|
||||
plan.targets.iter().all(|t| is_safe_dest(&t.dest))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use alkane_artifacts::afw::{AfwManifest, AfwMember};
|
||||
use serde_json::json;
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
const REV: &str = "0123456789abcdef0123456789abcdef01234567";
|
||||
|
||||
fn arm64_image(payload: &[u8]) -> Vec<u8> {
|
||||
let mut b = vec![0u8; 64];
|
||||
b[0x38..0x3C].copy_from_slice(b"ARMd");
|
||||
b.extend_from_slice(payload);
|
||||
b
|
||||
}
|
||||
|
||||
fn dtb() -> Vec<u8> {
|
||||
let mut b = vec![0u8; 2048];
|
||||
b[0..4].copy_from_slice(&[0xd0, 0x0d, 0xfe, 0xed]);
|
||||
b
|
||||
}
|
||||
|
||||
fn uramdisk() -> Vec<u8> {
|
||||
let mut b = vec![0u8; 64];
|
||||
b[0..4].copy_from_slice(&[0x27, 0x05, 0x19, 0x56]);
|
||||
b
|
||||
}
|
||||
|
||||
fn afw_with(
|
||||
kernel: Vec<u8>,
|
||||
dtb: Vec<u8>,
|
||||
rootfs: Vec<u8>,
|
||||
partitions: serde_json::Value,
|
||||
) -> VerifiedAfw {
|
||||
let image = |name: &str, file: &str, role: &str, fmt: &str, data: &[u8]| {
|
||||
let sha: String = Sha256::digest(data)
|
||||
.iter()
|
||||
.map(|b| format!("{b:02x}"))
|
||||
.collect();
|
||||
json!({"name": name, "file": file, "role": role, "format": fmt,
|
||||
"size": data.len(), "sha256": sha, "partition": "nvdata", "install": true})
|
||||
};
|
||||
let value = json!({
|
||||
"schema": 2, "format": "afw", "board": "a113d", "arch": "arm64",
|
||||
"version": "0.1.0", "build_date": "2026-11-06T00:00:00Z",
|
||||
"sdk_revision": REV, "os_revision": REV, "payload_type": "os",
|
||||
"bootloader_policy": "preserve",
|
||||
"signature": {"type": "unsigned", "member": null, "public_key_hex": null},
|
||||
"partitions": partitions,
|
||||
"images": [
|
||||
image("kernel", "payload/vmlinux.bin", "kernel", "arm64-image", &kernel),
|
||||
image("dtb", "payload/devicetree.dtb", "dtb", "fdt", &dtb),
|
||||
image("rootfs", "payload/uramdisk.image.gz", "rootfs", "uimage", &rootfs),
|
||||
],
|
||||
"boot_switch": {}, "preflight": {"min_free_bytes": 16777216},
|
||||
"recovery": {}, "marker": "Alkane", "kernel_version": "6.18.48"
|
||||
});
|
||||
let manifest: AfwManifest = serde_json::from_value(value).unwrap();
|
||||
VerifiedAfw {
|
||||
manifest,
|
||||
members: vec![
|
||||
AfwMember {
|
||||
name: "payload/vmlinux.bin".to_owned(),
|
||||
data: kernel,
|
||||
},
|
||||
AfwMember {
|
||||
name: "payload/devicetree.dtb".to_owned(),
|
||||
data: dtb,
|
||||
},
|
||||
AfwMember {
|
||||
name: "payload/uramdisk.image.gz".to_owned(),
|
||||
data: rootfs,
|
||||
},
|
||||
],
|
||||
manifest_sha256: "0".repeat(64),
|
||||
archive_sha256: "0".repeat(64),
|
||||
}
|
||||
}
|
||||
|
||||
fn preserved_partitions() -> serde_json::Value {
|
||||
json!([
|
||||
{"name":"bootloader","device":"/dev/mtd0","offset":0,"size":2097152,
|
||||
"secure":true,"boot_critical":true,"write_order":0,"image":null},
|
||||
{"name":"nvdata","device":"/dev/mtd6","offset":74448896,"size":193986560,
|
||||
"secure":false,"boot_critical":true,"write_order":1,"image":"kernel"}
|
||||
])
|
||||
}
|
||||
|
||||
fn good() -> VerifiedAfw {
|
||||
afw_with(arm64_image(b"k"), dtb(), uramdisk(), preserved_partitions())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accepts_a_good_plan_as_file_copies() {
|
||||
let plan = A113dPlan::from_afw(&good()).unwrap();
|
||||
assert_eq!(plan.targets.len(), 3);
|
||||
// write order rootfs, dtb, kernel (kernel file lands last)
|
||||
assert_eq!(plan.targets[0].image_name, "rootfs");
|
||||
assert_eq!(plan.targets[2].image_name, "kernel");
|
||||
assert!(no_raw_device_targets(&plan));
|
||||
for t in &plan.targets {
|
||||
assert!(
|
||||
!t.dest.contains('/'),
|
||||
"{} has a path, not a basename",
|
||||
t.image_name
|
||||
);
|
||||
}
|
||||
assert!(plan.render().contains("alkane/"));
|
||||
assert!(plan.render().contains("no raw NAND write is possible"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refuses_a_non_arm64_kernel() {
|
||||
let r = A113dPlan::from_afw(&afw_with(
|
||||
vec![0u8; 64],
|
||||
dtb(),
|
||||
uramdisk(),
|
||||
preserved_partitions(),
|
||||
));
|
||||
assert!(matches!(r, Err(A113dPlanError::NotArm64 { .. })));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refuses_an_oversize_kernel() {
|
||||
let big = arm64_image(&vec![0u8; 0x4000000]);
|
||||
let r = A113dPlan::from_afw(&afw_with(big, dtb(), uramdisk(), preserved_partitions()));
|
||||
assert!(matches!(r, Err(A113dPlanError::ImageTooLarge { .. })));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refuses_a_fip_member() {
|
||||
let mut v = good();
|
||||
v.manifest.images[0].role = "bootloader".to_owned();
|
||||
assert!(matches!(
|
||||
A113dPlan::from_afw(&v),
|
||||
Err(A113dPlanError::BootloaderMember(_))
|
||||
));
|
||||
let mut v = good();
|
||||
v.manifest.images[1].file = "payload/fip.bin".to_owned();
|
||||
assert!(matches!(
|
||||
A113dPlan::from_afw(&v),
|
||||
Err(A113dPlanError::BootloaderMember(_))
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refuses_a_preserved_partition_carrying_an_image() {
|
||||
let partitions = json!([
|
||||
{"name":"bootloader","device":"/dev/mtd0","offset":0,"size":2097152,
|
||||
"secure":true,"boot_critical":true,"write_order":0,"image":"kernel"},
|
||||
{"name":"nvdata","device":"/dev/mtd6","offset":74448896,"size":193986560,
|
||||
"secure":false,"boot_critical":true,"write_order":1,"image":"kernel"}
|
||||
]);
|
||||
let v = afw_with(arm64_image(b"k"), dtb(), uramdisk(), partitions);
|
||||
assert!(matches!(
|
||||
A113dPlan::from_afw(&v),
|
||||
Err(A113dPlanError::PreservedCarriesImage(_))
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wrong_board_and_arch_are_refused() {
|
||||
let mut v = good();
|
||||
v.manifest.board = "zynq-7010".to_owned();
|
||||
assert!(matches!(
|
||||
A113dPlan::from_afw(&v),
|
||||
Err(A113dPlanError::WrongBoard(_))
|
||||
));
|
||||
let mut v = good();
|
||||
v.manifest.arch = "armv7".to_owned();
|
||||
assert!(matches!(
|
||||
A113dPlan::from_afw(&v),
|
||||
Err(A113dPlanError::WrongArch(_))
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_target_is_a_basename_never_a_device() {
|
||||
for (image, dest, _ceiling, _fmt) in TARGETS {
|
||||
assert!(
|
||||
is_safe_dest(dest),
|
||||
"{image} dest {dest} is not a safe basename"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsafe_dest_is_rejected_by_guard() {
|
||||
assert!(!is_safe_dest("/dev/mtd0"));
|
||||
assert!(!is_safe_dest("../escape"));
|
||||
assert!(!is_safe_dest("sub/dir/file"));
|
||||
assert!(is_safe_dest("vmlinux.bin"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_the_nvdata_mtd_index() {
|
||||
let proc_mtd = "dev: size erasesize name\n\
|
||||
mtd0: 0b900000 00020000 \"nvdata\"\n";
|
||||
assert_eq!(parse_nvdata_mtd(proc_mtd), Some(0));
|
||||
// No nvdata line -> None (the no-NAND running image).
|
||||
assert_eq!(parse_nvdata_mtd("dev: size erasesize name\n"), None);
|
||||
// Different index is honored.
|
||||
assert_eq!(
|
||||
parse_nvdata_mtd("mtd3: 0b900000 00020000 \"nvdata\"\n"),
|
||||
Some(3)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,406 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
//! CV1835 board module: the only write is the 16 MiB p1 FIT.
|
||||
//!
|
||||
//! The vendor boot0, BL2, BL31 and U-Boot chain is preserved. The board plan is
|
||||
//! read from the verified `.afw` manifest, not guessed at runtime.
|
||||
|
||||
use crate::md5::md5_hex;
|
||||
use alkane_artifacts::afw::{
|
||||
AfwImage, AfwManifest, AfwPartition, BootloaderPolicy, VerifiedAfw, CV1835_P1_SIZE,
|
||||
};
|
||||
use thiserror::Error;
|
||||
|
||||
pub const BOARD: &str = "cv1835";
|
||||
pub const ARCH: &str = "arm64";
|
||||
pub const P1_DEVICE: &str = "/dev/mmcblk0p1";
|
||||
pub const MARKER: &str = "CVBOOT";
|
||||
pub const UPDATE_DAEMON_PORT: u16 = 12887;
|
||||
pub const GOLDEN_LBA_BYTES: u64 = 0x300000 * 512;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum CvPlanError {
|
||||
#[error("bundle is for {0}, not cv1835")]
|
||||
WrongBoard(String),
|
||||
#[error("bundle arch is {0}, cv1835 needs arm64")]
|
||||
WrongArch(String),
|
||||
#[error("cv1835 requires bootloader_policy preserve")]
|
||||
BootloaderPolicy,
|
||||
#[error("cv1835 bundle carries a bootloader member")]
|
||||
BootloaderMember,
|
||||
#[error("cv1835 bundle must install exactly p1")]
|
||||
MissingP1,
|
||||
#[error("cv1835 p1 is {0} bytes, not the 16 MiB padded FIT")]
|
||||
P1Size(u64),
|
||||
#[error("cv1835 p1 partition is not /dev/mmcblk0p1")]
|
||||
P1Device,
|
||||
#[error("cv1835 p1 partition must be 16 MiB, non-secure and written once")]
|
||||
P1Geometry,
|
||||
#[error("cv1835 p1 is not a bootable FIT: {0}")]
|
||||
Fit(String),
|
||||
#[error("manifest marker is empty")]
|
||||
Marker,
|
||||
#[error("manifest kernel_version is empty")]
|
||||
KernelVersion,
|
||||
#[error("manifest is not an OS payload")]
|
||||
PayloadType,
|
||||
#[error("manifest preflight is missing {0}")]
|
||||
Preflight(String),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Cv1835Plan {
|
||||
pub image: AfwImage,
|
||||
pub data: Vec<u8>,
|
||||
pub partition: AfwPartition,
|
||||
pub expected_md5: String,
|
||||
pub expected_sha256: String,
|
||||
pub marker: String,
|
||||
pub kernel_version: String,
|
||||
pub sdk_revision: String,
|
||||
pub os_revision: String,
|
||||
pub source_os: Vec<String>,
|
||||
pub required_commands: Vec<String>,
|
||||
pub min_free_bytes: u64,
|
||||
pub golden_lba_bytes: u64,
|
||||
}
|
||||
|
||||
impl Cv1835Plan {
|
||||
pub fn from_afw(afw: &VerifiedAfw) -> Result<Self, CvPlanError> {
|
||||
let manifest = &afw.manifest;
|
||||
if manifest.board != BOARD {
|
||||
return Err(CvPlanError::WrongBoard(manifest.board.clone()));
|
||||
}
|
||||
if manifest.arch != ARCH {
|
||||
return Err(CvPlanError::WrongArch(manifest.arch.clone()));
|
||||
}
|
||||
if manifest.payload_type != "os" {
|
||||
return Err(CvPlanError::PayloadType);
|
||||
}
|
||||
if manifest.bootloader_policy != BootloaderPolicy::Preserve {
|
||||
return Err(CvPlanError::BootloaderPolicy);
|
||||
}
|
||||
if manifest
|
||||
.images
|
||||
.iter()
|
||||
.any(|image| image.role == "bootloader")
|
||||
{
|
||||
return Err(CvPlanError::BootloaderMember);
|
||||
}
|
||||
if manifest.marker.is_empty() {
|
||||
return Err(CvPlanError::Marker);
|
||||
}
|
||||
if manifest.kernel_version.is_empty() {
|
||||
return Err(CvPlanError::KernelVersion);
|
||||
}
|
||||
|
||||
let install: Vec<&AfwImage> = manifest
|
||||
.images
|
||||
.iter()
|
||||
.filter(|image| image.install)
|
||||
.collect();
|
||||
if install.len() != 1 || install[0].name != "p1" {
|
||||
return Err(CvPlanError::MissingP1);
|
||||
}
|
||||
let image = install[0].clone();
|
||||
if image.format != "fit" {
|
||||
return Err(CvPlanError::Fit("format is not fit".to_owned()));
|
||||
}
|
||||
if image.size != CV1835_P1_SIZE {
|
||||
return Err(CvPlanError::P1Size(image.size));
|
||||
}
|
||||
let (_, data) = afw.install_image().ok_or(CvPlanError::MissingP1)?;
|
||||
if !data.starts_with(b"\xd0\x0d\xfe\xed") {
|
||||
return Err(CvPlanError::Fit("missing FDT/FIT magic".to_owned()));
|
||||
}
|
||||
if !contains(data, b"config@cv1835_miner_emmc") {
|
||||
return Err(CvPlanError::Fit(
|
||||
"missing config@cv1835_miner_emmc".to_owned(),
|
||||
));
|
||||
}
|
||||
if contains(data, b"signature@") {
|
||||
return Err(CvPlanError::Fit("carries signature@ nodes".to_owned()));
|
||||
}
|
||||
|
||||
let partition = manifest
|
||||
.partitions
|
||||
.iter()
|
||||
.find(|part| part.name == "p1")
|
||||
.ok_or(CvPlanError::P1Device)?
|
||||
.clone();
|
||||
if partition.device != P1_DEVICE {
|
||||
return Err(CvPlanError::P1Device);
|
||||
}
|
||||
if partition.size != CV1835_P1_SIZE
|
||||
|| partition.secure
|
||||
|| !partition.boot_critical
|
||||
|| partition.write_order != 1
|
||||
{
|
||||
return Err(CvPlanError::P1Geometry);
|
||||
}
|
||||
if manifest
|
||||
.partitions
|
||||
.iter()
|
||||
.any(|part| part.write_order > 0 && part.name != "p1")
|
||||
{
|
||||
return Err(CvPlanError::MissingP1);
|
||||
}
|
||||
|
||||
let source_os = json_strings(&manifest.preflight, "source_os");
|
||||
let required_commands = json_strings(&manifest.preflight, "required_commands");
|
||||
let min_free_bytes = manifest
|
||||
.preflight
|
||||
.get("min_free_bytes")
|
||||
.and_then(|value| value.as_u64())
|
||||
.unwrap_or(32 * 1024 * 1024);
|
||||
let golden_lba_bytes = manifest
|
||||
.recovery
|
||||
.get("golden_lba_bytes")
|
||||
.and_then(|value| value.as_u64())
|
||||
.unwrap_or(GOLDEN_LBA_BYTES);
|
||||
|
||||
Ok(Self {
|
||||
expected_md5: md5_hex(data),
|
||||
expected_sha256: image.sha256.clone(),
|
||||
image,
|
||||
data: data.to_vec(),
|
||||
partition,
|
||||
marker: manifest.marker.clone(),
|
||||
kernel_version: manifest.kernel_version.clone(),
|
||||
sdk_revision: manifest.sdk_revision.clone(),
|
||||
os_revision: manifest.os_revision.clone(),
|
||||
source_os,
|
||||
required_commands,
|
||||
min_free_bytes,
|
||||
golden_lba_bytes,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn render(&self) -> String {
|
||||
format!(
|
||||
"board cv1835 arch arm64\n\
|
||||
partition {} {} {} B secure=false boot_critical=true write_order={}\n\
|
||||
image {} {} {} B sha256 {}\n\
|
||||
image md5 {}\n\
|
||||
bootloader preserve (vendor boot0/BL2/BL31/U-Boot kept)\n\
|
||||
boot switch partition-overwrite (p1 is the switch)\n\
|
||||
marker {} kernel {} sdk {} os {}",
|
||||
self.partition.name,
|
||||
self.partition.device,
|
||||
self.partition.size,
|
||||
self.partition.write_order,
|
||||
self.image.name,
|
||||
self.image.file,
|
||||
self.image.size,
|
||||
self.image.sha256,
|
||||
self.expected_md5,
|
||||
self.marker,
|
||||
self.kernel_version,
|
||||
short_revision(&self.sdk_revision),
|
||||
short_revision(&self.os_revision),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn short_revision(revision: &str) -> &str {
|
||||
revision.get(..12).unwrap_or(revision)
|
||||
}
|
||||
|
||||
fn contains(haystack: &[u8], needle: &[u8]) -> bool {
|
||||
haystack
|
||||
.windows(needle.len())
|
||||
.any(|window| window == needle)
|
||||
}
|
||||
|
||||
fn json_strings(value: &serde_json::Value, key: &str) -> Vec<String> {
|
||||
value
|
||||
.get(key)
|
||||
.and_then(|item| item.as_array())
|
||||
.map(|items| {
|
||||
items
|
||||
.iter()
|
||||
.filter_map(|item| item.as_str().map(str::to_owned))
|
||||
.collect()
|
||||
})
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn manifest_from_json(value: serde_json::Value) -> Result<AfwManifest, serde_json::Error> {
|
||||
serde_json::from_value(value)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use alkane_artifacts::afw::AfwMember;
|
||||
use serde_json::json;
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
const P1: usize = CV1835_P1_SIZE as usize;
|
||||
const REV: &str = "0000000000000000000000000000000000000000";
|
||||
|
||||
fn sha_hex(data: &[u8]) -> String {
|
||||
Sha256::digest(data)
|
||||
.iter()
|
||||
.map(|byte| format!("{byte:02x}"))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn p1_image() -> Vec<u8> {
|
||||
let mut fit = vec![0_u8; P1];
|
||||
fit[..4].copy_from_slice(b"\xd0\x0d\xfe\xed");
|
||||
let marker = b"config@cv1835_miner_emmc";
|
||||
fit[0x100..0x100 + marker.len()].copy_from_slice(marker);
|
||||
fit
|
||||
}
|
||||
|
||||
fn manifest_value(p1: &[u8]) -> serde_json::Value {
|
||||
json!({
|
||||
"schema": 2,
|
||||
"format": "afw",
|
||||
"board": "cv1835",
|
||||
"arch": "arm64",
|
||||
"version": "0.1.0",
|
||||
"build_date": "2026-11-04T00:00:00Z",
|
||||
"sdk_revision": REV,
|
||||
"os_revision": REV,
|
||||
"payload_type": "os",
|
||||
"bootloader_policy": "preserve",
|
||||
"signature": {"type": "unsigned", "member": null, "public_key_hex": null},
|
||||
"partitions": [
|
||||
{"name": "p1", "device": "/dev/mmcblk0p1", "offset": 0,
|
||||
"size": CV1835_P1_SIZE, "secure": false, "boot_critical": true,
|
||||
"write_order": 1, "image": "p1"},
|
||||
{"name": "boot0", "device": "/dev/mmcblk0boot0", "offset": 0,
|
||||
"size": 4194304, "secure": true, "boot_critical": true,
|
||||
"write_order": 0, "image": null}
|
||||
],
|
||||
"images": [
|
||||
{"name": "p1", "file": "payload/p1.fit", "role": "boot",
|
||||
"format": "fit", "size": p1.len(),
|
||||
"sha256": sha_hex(p1),
|
||||
"partition": "p1", "install": true}
|
||||
],
|
||||
"boot_switch": {"method": "partition-overwrite"},
|
||||
"preflight": {"required_commands": ["dd", "md5sum", "sync"],
|
||||
"source_os": ["stock-bitmain", "vnish", "braiins", "ouros", "alkane"],
|
||||
"min_free_bytes": 33554432},
|
||||
"recovery": {"network": "afw restore", "golden_lba_bytes": GOLDEN_LBA_BYTES},
|
||||
"marker": "CVBOOT",
|
||||
"kernel_version": "6.18.48"
|
||||
})
|
||||
}
|
||||
|
||||
fn afw(p1: &[u8]) -> VerifiedAfw {
|
||||
let manifest = manifest_from_json(manifest_value(p1)).unwrap();
|
||||
VerifiedAfw {
|
||||
manifest,
|
||||
members: vec![AfwMember {
|
||||
name: "payload/p1.fit".to_owned(),
|
||||
data: p1.to_vec(),
|
||||
}],
|
||||
manifest_sha256: "0".repeat(64),
|
||||
archive_sha256: "0".repeat(64),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accepts_the_cv_plan() {
|
||||
let data = p1_image();
|
||||
let plan = Cv1835Plan::from_afw(&afw(&data)).unwrap();
|
||||
assert_eq!(plan.partition.device, "/dev/mmcblk0p1");
|
||||
assert_eq!(plan.image.size, CV1835_P1_SIZE);
|
||||
assert_eq!(plan.marker, "CVBOOT");
|
||||
assert!(plan
|
||||
.render()
|
||||
.contains("partition p1 /dev/mmcblk0p1 16777216"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refuses_a_bundle_for_another_board() {
|
||||
let data = p1_image();
|
||||
let mut verified = afw(&data);
|
||||
verified.manifest.board = "zynq-7010".to_owned();
|
||||
assert!(matches!(
|
||||
Cv1835Plan::from_afw(&verified),
|
||||
Err(CvPlanError::WrongBoard(_))
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refuses_include_bootloader_policy() {
|
||||
let data = p1_image();
|
||||
let mut verified = afw(&data);
|
||||
verified.manifest.bootloader_policy = BootloaderPolicy::Include;
|
||||
assert!(matches!(
|
||||
Cv1835Plan::from_afw(&verified),
|
||||
Err(CvPlanError::BootloaderPolicy)
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refuses_a_bootloader_member() {
|
||||
let data = p1_image();
|
||||
let mut verified = afw(&data);
|
||||
verified.manifest.images.push(AfwImage {
|
||||
name: "u-boot".to_owned(),
|
||||
file: "payload/u-boot.img".to_owned(),
|
||||
role: "bootloader".to_owned(),
|
||||
format: "uimage".to_owned(),
|
||||
size: 4,
|
||||
sha256: "0".repeat(64),
|
||||
partition: None,
|
||||
install: false,
|
||||
});
|
||||
assert!(matches!(
|
||||
Cv1835Plan::from_afw(&verified),
|
||||
Err(CvPlanError::BootloaderMember)
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refuses_a_short_p1() {
|
||||
let data = vec![0_u8; 1024];
|
||||
let verified = afw(&data);
|
||||
assert!(matches!(
|
||||
Cv1835Plan::from_afw(&verified),
|
||||
Err(CvPlanError::P1Size(1024))
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refuses_a_secure_p1() {
|
||||
let data = p1_image();
|
||||
let mut verified = afw(&data);
|
||||
verified.manifest.partitions[0].secure = true;
|
||||
assert!(matches!(
|
||||
Cv1835Plan::from_afw(&verified),
|
||||
Err(CvPlanError::P1Geometry)
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refuses_a_wrong_device() {
|
||||
let data = p1_image();
|
||||
let mut verified = afw(&data);
|
||||
verified.manifest.partitions[0].device = "/dev/mmcblk0p3".to_owned();
|
||||
assert!(matches!(
|
||||
Cv1835Plan::from_afw(&verified),
|
||||
Err(CvPlanError::P1Device)
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refuses_a_fit_without_the_board_config() {
|
||||
let mut data = p1_image();
|
||||
for byte in data.iter_mut() {
|
||||
if *byte == b'c' {
|
||||
*byte = b'x';
|
||||
}
|
||||
}
|
||||
let verified = afw(&data);
|
||||
assert!(matches!(
|
||||
Cv1835Plan::from_afw(&verified),
|
||||
Err(CvPlanError::Fit(_))
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
//! Board modules. Each module validates the verified manifest for its family
|
||||
//! and exposes the exact write plan the installer follows.
|
||||
|
||||
pub mod a113d;
|
||||
pub mod am335x;
|
||||
pub mod cv1835;
|
||||
pub mod zynq;
|
||||
@@ -0,0 +1,410 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
//! Zynq-7010 board module: the manifest gate and write plan for putting our
|
||||
//! OS onto a Zynq control board (Antminer S17/S19 class).
|
||||
//!
|
||||
//! This module decides WHAT may be written and WHERE, in absolute NAND terms.
|
||||
//! It does not write. The write itself (for a board already on alkane, and for
|
||||
//! the first install from stock) is done by the compiled-in method behind
|
||||
//! `crate::stock` (`update_in_place` / `install_from_stock`), because on a board
|
||||
//! running alkane the boot regions are deliberately not exposed to Linux and
|
||||
//! have to be re-registered first; that mechanism lives with the method.
|
||||
//!
|
||||
//! Layout (absolute NAND offsets, 128 KiB erase blocks): BOOT.bin (FSBL + FPGA
|
||||
//! bitstream + stock U-Boot + env) occupies 0x0..0x1a00000 and is never
|
||||
//! written. The stock U-Boot reads the dtb at 0x1a00000, the kernel at
|
||||
//! 0x2000000 and the ramdisk at 0x2800000, and boots the kernel and ramdisk as
|
||||
//! legacy uImages, so both are written with a 64-byte uImage header.
|
||||
//!
|
||||
//! History: an earlier version of this module wrote raw images at
|
||||
//! device-relative offsets on /dev/mtd0 and /dev/mtd1. That matched an early
|
||||
//! alkane image that exposed the whole NAND, not the current one (which exposes
|
||||
//! only the small `stock-cfg` and `alkane-cfg` partitions), and its raw kernel
|
||||
//! and ramdisk were not bootable by the stock U-Boot. It was removed.
|
||||
|
||||
use crate::md5::md5_hex;
|
||||
use alkane_artifacts::afw::{AfwImage, BootloaderPolicy, VerifiedAfw};
|
||||
use thiserror::Error;
|
||||
|
||||
pub const BOARD: &str = "zynq-7010";
|
||||
pub const ARCH: &str = "armv7";
|
||||
/// Absolute NAND 0x0..0x1a00000 is BOOT.bin (FSBL + FPGA bitstream + stock
|
||||
/// U-Boot) + the U-Boot env, and is NEVER erased or written.
|
||||
pub const BOOTBIN_PRESERVE: u64 = 0x1A00000;
|
||||
|
||||
/// Size of the legacy uImage header the stock U-Boot needs on the kernel and
|
||||
/// the ramdisk.
|
||||
const UIMAGE_HEADER: u64 = 64;
|
||||
|
||||
/// Write targets in write order (rootfs, dtb, kernel, so an interrupted write
|
||||
/// never leaves a new rootfs under an old kernel): (image, absolute NAND
|
||||
/// offset, slot size, expected manifest format, uImage-wrapped).
|
||||
const TARGETS: [(&str, u64, u64, &str, bool); 3] = [
|
||||
("rootfs", 0x2800000, 0x2000000, "cpio-gzip", true),
|
||||
("dtb", 0x1A00000, 0x20000, "fdt", false),
|
||||
("kernel", 0x2000000, 0x800000, "zimage", true),
|
||||
];
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum ZynqPlanError {
|
||||
#[error("bundle is for {0}, not zynq-7010")]
|
||||
WrongBoard(String),
|
||||
#[error("bundle arch is {0}, zynq-7010 needs armv7")]
|
||||
WrongArch(String),
|
||||
#[error("zynq-7010 requires bootloader_policy preserve")]
|
||||
BootloaderPolicy,
|
||||
#[error("zynq-7010 bundle carries a bootloader/spl/BOOT.bin member: {0}")]
|
||||
BootloaderMember(String),
|
||||
#[error("manifest is not an OS payload")]
|
||||
PayloadType,
|
||||
#[error("manifest marker is empty")]
|
||||
Marker,
|
||||
#[error("zynq-7010 must install exactly kernel, dtb and rootfs")]
|
||||
InstallCount,
|
||||
#[error("zynq-7010 is missing the {0} image")]
|
||||
MissingImage(String),
|
||||
#[error("zynq-7010 {image} format is {actual}, expected {expected}")]
|
||||
ImageFormat {
|
||||
image: String,
|
||||
actual: String,
|
||||
expected: &'static str,
|
||||
},
|
||||
#[error("zynq-7010 {image} is {size} bytes, over the {ceiling}-byte ceiling for its 0x{slot:x} slot")]
|
||||
ImageTooLarge {
|
||||
image: String,
|
||||
size: u64,
|
||||
ceiling: u64,
|
||||
slot: u64,
|
||||
},
|
||||
#[error("zynq-7010 {image} is not an ARM zImage")]
|
||||
NotZImage { image: String },
|
||||
#[error("zynq-7010 refuses to place {image} at 0x{offset:x}: below BOOT.bin (0x{bootbin:x})")]
|
||||
WouldClobberBootbin {
|
||||
image: String,
|
||||
offset: u64,
|
||||
bootbin: u64,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ZynqTarget {
|
||||
pub image_name: String,
|
||||
/// Absolute NAND offset of the slot.
|
||||
pub absolute: u64,
|
||||
pub slot: u64,
|
||||
/// Size of the image as shipped in the `.afw` (before any uImage header).
|
||||
pub size: u64,
|
||||
pub uimage_wrapped: bool,
|
||||
pub data: Vec<u8>,
|
||||
pub expected_md5: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ZynqPlan {
|
||||
pub targets: Vec<ZynqTarget>,
|
||||
pub marker: String,
|
||||
pub kernel_version: String,
|
||||
pub sdk_revision: String,
|
||||
pub os_revision: String,
|
||||
pub afw_sha256: String,
|
||||
}
|
||||
|
||||
fn is_bootloaderish(image: &AfwImage) -> bool {
|
||||
let lowered = format!("{} {} {}", image.name, image.file, image.role).to_lowercase();
|
||||
image.role == "bootloader"
|
||||
|| lowered.contains("spl")
|
||||
|| lowered.contains("u-boot")
|
||||
|| lowered.contains("uboot")
|
||||
|| lowered.contains("boot.bin")
|
||||
}
|
||||
|
||||
impl ZynqPlan {
|
||||
pub fn from_afw(afw: &VerifiedAfw) -> Result<Self, ZynqPlanError> {
|
||||
let manifest = &afw.manifest;
|
||||
if manifest.board != BOARD {
|
||||
return Err(ZynqPlanError::WrongBoard(manifest.board.clone()));
|
||||
}
|
||||
if manifest.arch != ARCH {
|
||||
return Err(ZynqPlanError::WrongArch(manifest.arch.clone()));
|
||||
}
|
||||
if manifest.payload_type != "os" {
|
||||
return Err(ZynqPlanError::PayloadType);
|
||||
}
|
||||
if manifest.bootloader_policy != BootloaderPolicy::Preserve {
|
||||
return Err(ZynqPlanError::BootloaderPolicy);
|
||||
}
|
||||
if manifest.marker.is_empty() {
|
||||
return Err(ZynqPlanError::Marker);
|
||||
}
|
||||
// BOOT.bin / bootloader members are never allowed - we reuse the board's.
|
||||
for image in &manifest.images {
|
||||
if is_bootloaderish(image) {
|
||||
return Err(ZynqPlanError::BootloaderMember(image.file.clone()));
|
||||
}
|
||||
}
|
||||
let installs: Vec<&AfwImage> = manifest.images.iter().filter(|i| i.install).collect();
|
||||
if installs.len() != TARGETS.len() {
|
||||
return Err(ZynqPlanError::InstallCount);
|
||||
}
|
||||
|
||||
let mut targets = Vec::with_capacity(TARGETS.len());
|
||||
for (image_name, absolute, slot, expected_format, wrapped) in TARGETS {
|
||||
let image = manifest
|
||||
.images
|
||||
.iter()
|
||||
.find(|i| i.name == image_name && i.install)
|
||||
.ok_or_else(|| ZynqPlanError::MissingImage(image_name.to_owned()))?;
|
||||
if image.format != expected_format {
|
||||
return Err(ZynqPlanError::ImageFormat {
|
||||
image: image_name.to_owned(),
|
||||
actual: image.format.clone(),
|
||||
expected: expected_format,
|
||||
});
|
||||
}
|
||||
let ceiling = if wrapped { slot - UIMAGE_HEADER } else { slot };
|
||||
if image.size > ceiling {
|
||||
return Err(ZynqPlanError::ImageTooLarge {
|
||||
image: image_name.to_owned(),
|
||||
size: image.size,
|
||||
ceiling,
|
||||
slot,
|
||||
});
|
||||
}
|
||||
// The hard brick guard: nothing is ever placed inside BOOT.bin.
|
||||
if absolute < BOOTBIN_PRESERVE {
|
||||
return Err(ZynqPlanError::WouldClobberBootbin {
|
||||
image: image_name.to_owned(),
|
||||
offset: absolute,
|
||||
bootbin: BOOTBIN_PRESERVE,
|
||||
});
|
||||
}
|
||||
let data = afw
|
||||
.image(image_name)
|
||||
.map(|(_, data)| data)
|
||||
.ok_or_else(|| ZynqPlanError::MissingImage(image_name.to_owned()))?;
|
||||
if expected_format == "zimage"
|
||||
&& !(data.len() >= 0x28 && data[0x24..0x28] == [0x18, 0x28, 0x6f, 0x01])
|
||||
{
|
||||
return Err(ZynqPlanError::NotZImage {
|
||||
image: image_name.to_owned(),
|
||||
});
|
||||
}
|
||||
targets.push(ZynqTarget {
|
||||
image_name: image_name.to_owned(),
|
||||
absolute,
|
||||
slot,
|
||||
size: image.size,
|
||||
uimage_wrapped: wrapped,
|
||||
expected_md5: md5_hex(data),
|
||||
data: data.to_vec(),
|
||||
});
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
targets,
|
||||
marker: manifest.marker.clone(),
|
||||
kernel_version: manifest.kernel_version.clone(),
|
||||
sdk_revision: manifest.sdk_revision.clone(),
|
||||
os_revision: manifest.os_revision.clone(),
|
||||
afw_sha256: afw.archive_sha256.clone(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn render(&self) -> String {
|
||||
let mut out = format!("board {BOARD} arch {ARCH}\n");
|
||||
out.push_str(&format!(
|
||||
"boot slots (absolute NAND); BOOT.bin 0x0..0x{BOOTBIN_PRESERVE:x} preserved, never written\n"
|
||||
));
|
||||
out.push_str(
|
||||
"kernel and rootfs are written as legacy uImages (what the stock U-Boot boots); dtb raw\n",
|
||||
);
|
||||
for target in &self.targets {
|
||||
out.push_str(&format!(
|
||||
"write {:<6} abs 0x{:x} slot 0x{:x} size {} B{} md5 {}\n",
|
||||
target.image_name,
|
||||
target.absolute,
|
||||
target.slot,
|
||||
target.size,
|
||||
if target.uimage_wrapped {
|
||||
" +64 B header"
|
||||
} else {
|
||||
""
|
||||
},
|
||||
target.expected_md5
|
||||
));
|
||||
}
|
||||
out.push_str(&format!(
|
||||
"marker {} kernel {} sdk {} os {}",
|
||||
self.marker,
|
||||
self.kernel_version,
|
||||
short(&self.sdk_revision),
|
||||
short(&self.os_revision),
|
||||
));
|
||||
out
|
||||
}
|
||||
}
|
||||
|
||||
fn short(rev: &str) -> &str {
|
||||
rev.get(..12).unwrap_or(rev)
|
||||
}
|
||||
|
||||
pub fn owns_board(board: &str) -> bool {
|
||||
board == BOARD
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use alkane_artifacts::afw::{AfwManifest, AfwMember};
|
||||
use serde_json::json;
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
const REV: &str = "0123456789abcdef0123456789abcdef01234567";
|
||||
|
||||
fn zimage(payload: &[u8]) -> Vec<u8> {
|
||||
let mut b = vec![0u8; 64];
|
||||
b[0x24..0x28].copy_from_slice(&[0x18, 0x28, 0x6f, 0x01]);
|
||||
b.extend_from_slice(payload);
|
||||
b
|
||||
}
|
||||
|
||||
fn afw(kernel: Vec<u8>, dtb: Vec<u8>, rootfs: Vec<u8>) -> VerifiedAfw {
|
||||
let image = |name: &str, file: &str, role: &str, fmt: &str, data: &[u8]| {
|
||||
let sha: String = Sha256::digest(data)
|
||||
.iter()
|
||||
.map(|b| format!("{b:02x}"))
|
||||
.collect();
|
||||
json!({"name": name, "file": file, "role": role, "format": fmt,
|
||||
"size": data.len(), "sha256": sha, "partition": "x", "install": true})
|
||||
};
|
||||
let value = json!({
|
||||
"schema": 2, "format": "afw", "board": "zynq-7010", "arch": "armv7",
|
||||
"version": "0.1.0", "build_date": "2026-11-06T00:00:00Z",
|
||||
"sdk_revision": REV, "os_revision": REV, "payload_type": "os",
|
||||
"bootloader_policy": "preserve",
|
||||
"signature": {"type": "unsigned", "member": null, "public_key_hex": null},
|
||||
"partitions": [],
|
||||
"images": [
|
||||
image("kernel", "payload/zImage", "kernel", "zimage", &kernel),
|
||||
image("dtb", "payload/zynq-alkane.dtb", "dtb", "fdt", &dtb),
|
||||
image("rootfs", "payload/rootfs.cpio.gz", "rootfs", "cpio-gzip", &rootfs),
|
||||
],
|
||||
"boot_switch": {}, "preflight": {"min_free_bytes": 16777216},
|
||||
"recovery": {}, "marker": "Alkane", "kernel_version": "6.18.48"
|
||||
});
|
||||
let manifest: AfwManifest = serde_json::from_value(value).unwrap();
|
||||
VerifiedAfw {
|
||||
manifest,
|
||||
members: vec![
|
||||
AfwMember {
|
||||
name: "payload/zImage".to_owned(),
|
||||
data: kernel,
|
||||
},
|
||||
AfwMember {
|
||||
name: "payload/zynq-alkane.dtb".to_owned(),
|
||||
data: dtb,
|
||||
},
|
||||
AfwMember {
|
||||
name: "payload/rootfs.cpio.gz".to_owned(),
|
||||
data: rootfs,
|
||||
},
|
||||
],
|
||||
manifest_sha256: "0".repeat(64),
|
||||
archive_sha256: "0".repeat(64),
|
||||
}
|
||||
}
|
||||
|
||||
fn dtb() -> Vec<u8> {
|
||||
let mut b = vec![0u8; 1024];
|
||||
b[0..4].copy_from_slice(&[0xd0, 0x0d, 0xfe, 0xed]);
|
||||
b
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accepts_a_good_plan_and_places_above_bootbin() {
|
||||
let plan = ZynqPlan::from_afw(&afw(zimage(b"k"), dtb(), b"\x1f\x8brf".to_vec())).unwrap();
|
||||
assert_eq!(plan.targets.len(), 3);
|
||||
// write order rootfs, dtb, kernel
|
||||
assert_eq!(plan.targets[0].image_name, "rootfs");
|
||||
assert_eq!(plan.targets[2].image_name, "kernel");
|
||||
for t in &plan.targets {
|
||||
assert!(
|
||||
t.absolute >= BOOTBIN_PRESERVE,
|
||||
"{} below BOOT.bin",
|
||||
t.image_name
|
||||
);
|
||||
}
|
||||
let render = plan.render();
|
||||
assert!(render.contains("BOOT.bin"));
|
||||
assert!(render.contains("uImage"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn slots_match_the_stock_uboot_layout() {
|
||||
let plan = ZynqPlan::from_afw(&afw(zimage(b"k"), dtb(), b"\x1f\x8brf".to_vec())).unwrap();
|
||||
let at = |name: &str| plan.targets.iter().find(|t| t.image_name == name).unwrap();
|
||||
assert_eq!((at("dtb").absolute, at("dtb").slot), (0x1A00000, 0x20000));
|
||||
assert_eq!(
|
||||
(at("kernel").absolute, at("kernel").slot),
|
||||
(0x2000000, 0x800000)
|
||||
);
|
||||
assert_eq!(
|
||||
(at("rootfs").absolute, at("rootfs").slot),
|
||||
(0x2800000, 0x2000000)
|
||||
);
|
||||
assert!(at("kernel").uimage_wrapped && at("rootfs").uimage_wrapped);
|
||||
assert!(!at("dtb").uimage_wrapped);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refuses_a_non_zimage_kernel() {
|
||||
let result = ZynqPlan::from_afw(&afw(vec![0u8; 64], dtb(), b"\x1f\x8brf".to_vec()));
|
||||
assert!(matches!(result, Err(ZynqPlanError::NotZImage { .. })));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refuses_a_kernel_that_would_not_fit_with_its_uimage_header() {
|
||||
// Exactly the slot size is too big once the 64-byte header is added.
|
||||
let big = zimage(&vec![0u8; 0x800000 - 64]);
|
||||
assert_eq!(big.len(), 0x800000);
|
||||
let result = ZynqPlan::from_afw(&afw(big, dtb(), b"\x1f\x8brf".to_vec()));
|
||||
assert!(matches!(result, Err(ZynqPlanError::ImageTooLarge { .. })));
|
||||
let fits = zimage(&vec![0u8; 0x800000 - 128]);
|
||||
assert!(ZynqPlan::from_afw(&afw(fits, dtb(), b"\x1f\x8brf".to_vec())).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refuses_a_bootloader_member() {
|
||||
let mut verified = afw(zimage(b"k"), dtb(), b"\x1f\x8brf".to_vec());
|
||||
verified.manifest.images[0].role = "bootloader".to_owned();
|
||||
assert!(matches!(
|
||||
ZynqPlan::from_afw(&verified),
|
||||
Err(ZynqPlanError::BootloaderMember(_))
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wrong_board_and_arch_are_refused() {
|
||||
let mut v = afw(zimage(b"k"), dtb(), b"\x1f\x8brf".to_vec());
|
||||
v.manifest.board = "am335x".to_owned();
|
||||
assert!(matches!(
|
||||
ZynqPlan::from_afw(&v),
|
||||
Err(ZynqPlanError::WrongBoard(_))
|
||||
));
|
||||
let mut v = afw(zimage(b"k"), dtb(), b"\x1f\x8brf".to_vec());
|
||||
v.manifest.arch = "arm".to_owned();
|
||||
assert!(matches!(
|
||||
ZynqPlan::from_afw(&v),
|
||||
Err(ZynqPlanError::WrongArch(_))
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_target_is_above_bootbin() {
|
||||
for (image, absolute, _slot, _fmt, _wrapped) in TARGETS {
|
||||
assert!(
|
||||
absolute >= BOOTBIN_PRESERVE,
|
||||
"{image} target below BOOT.bin"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,920 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
//! CV1835 install, restore and live verification over the network.
|
||||
//!
|
||||
//! Order of operations for an install: verify the bundle, preflight the board,
|
||||
//! hold the current p1 over SSH, stage the new p1, write it once, read the whole
|
||||
//! partition back, and only then reboot. Any doubt before the first write stops
|
||||
//! the sequence. A read-back mismatch stops before reboot and prints the exact
|
||||
//! restore command.
|
||||
|
||||
use crate::boards::cv1835::{Cv1835Plan, CvPlanError};
|
||||
use crate::md5::md5_hex;
|
||||
use crate::preflight::{preflight, PreflightError, PreflightOptions, PreflightReport};
|
||||
use crate::transport::{Remote, RemoteError, SshOptions, SshTransport};
|
||||
use alkane_artifacts::afw::{AfwError, VerifiedAfw};
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::fs;
|
||||
use std::io::{Read, Write};
|
||||
use std::net::{TcpStream, ToSocketAddrs};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
|
||||
use thiserror::Error;
|
||||
|
||||
const REMOTE_BACKUP: &str = "/tmp/.alkane-p1-backup.img";
|
||||
const REMOTE_STAGE: &str = "/tmp/.alkane-p1-stage.img";
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum InstallError {
|
||||
#[error("preflight failed; do not write\n{0}")]
|
||||
Preflight(String),
|
||||
#[error("install and restore need --confirm FLASH")]
|
||||
Confirm,
|
||||
#[error("verified .afw: {0}")]
|
||||
Afw(#[from] AfwError),
|
||||
#[error("cv1835 plan: {0}")]
|
||||
Plan(#[from] CvPlanError),
|
||||
#[error("preflight: {0}")]
|
||||
PreflightError(#[from] PreflightError),
|
||||
#[error("remote: {0}")]
|
||||
Remote(#[from] RemoteError),
|
||||
#[error("I/O: {0}")]
|
||||
Io(#[from] std::io::Error),
|
||||
#[error("backup {0} is not the 16 MiB p1 size")]
|
||||
BackupSize(PathBuf),
|
||||
#[error("backup md5 does not match the live p1; refusing to continue")]
|
||||
BackupMismatch,
|
||||
#[error("staged image md5 does not match the verified bundle; nothing written")]
|
||||
StageMismatch,
|
||||
#[error("p1 read-back md5 mismatch; do not reboot. Restore with: {0}")]
|
||||
Readback(String),
|
||||
#[error("restore read-back md5 mismatch; the board may still hold the new image")]
|
||||
RestoreReadback,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct InstallOptions {
|
||||
pub dry_run: bool,
|
||||
pub confirm: Option<String>,
|
||||
pub no_reboot: bool,
|
||||
pub backup_dir: PathBuf,
|
||||
pub held_backup: Option<PathBuf>,
|
||||
pub allow_unknown_source: bool,
|
||||
pub check_golden: bool,
|
||||
}
|
||||
|
||||
impl Default for InstallOptions {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
dry_run: false,
|
||||
confirm: None,
|
||||
no_reboot: false,
|
||||
backup_dir: default_backup_dir(),
|
||||
held_backup: None,
|
||||
allow_unknown_source: false,
|
||||
check_golden: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct InstallReceipt {
|
||||
pub backup_path: PathBuf,
|
||||
pub backup_md5: String,
|
||||
pub backup_sha256: String,
|
||||
pub image_md5: String,
|
||||
pub rebooted: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum InstallOutcome {
|
||||
DryRun(Box<PreflightReport>),
|
||||
Installed(InstallReceipt),
|
||||
}
|
||||
|
||||
pub fn install(
|
||||
_afw: &VerifiedAfw,
|
||||
plan: &Cv1835Plan,
|
||||
remote: &mut dyn Remote,
|
||||
options: &InstallOptions,
|
||||
host: &str,
|
||||
) -> Result<InstallOutcome, InstallError> {
|
||||
let report = preflight(
|
||||
plan,
|
||||
remote,
|
||||
&PreflightOptions {
|
||||
host,
|
||||
held_backup: options.held_backup.as_deref(),
|
||||
check_golden: options.check_golden,
|
||||
allow_unknown_source: options.allow_unknown_source,
|
||||
},
|
||||
)?;
|
||||
if !report.ok() {
|
||||
return Err(InstallError::Preflight(report.render()));
|
||||
}
|
||||
if options.dry_run {
|
||||
return Ok(InstallOutcome::DryRun(Box::new(report)));
|
||||
}
|
||||
if options.confirm.as_deref() != Some("FLASH") {
|
||||
return Err(InstallError::Confirm);
|
||||
}
|
||||
|
||||
fs::create_dir_all(&options.backup_dir)?;
|
||||
let epoch = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap_or_default()
|
||||
.as_secs();
|
||||
let backup_path = options.backup_dir.join(format!("p1-{epoch}.img"));
|
||||
let device = &plan.partition.device;
|
||||
|
||||
// Hold the current p1 as root, then pull it over the unprivileged SSH
|
||||
// session. The daemon channel is blind, so this two-step path is required.
|
||||
let remote_backup = REMOTE_BACKUP;
|
||||
remote.root_command(
|
||||
&format!("dd if={device} of={remote_backup} bs=1M 2>&1; chmod 644 {remote_backup}"),
|
||||
Duration::from_secs(180),
|
||||
)?;
|
||||
remote.pull(remote_backup, &backup_path, Duration::from_secs(240))?;
|
||||
let _ = remote.root_command(&format!("rm -f {remote_backup}"), Duration::from_secs(15));
|
||||
let backup_data = fs::read(&backup_path)?;
|
||||
if backup_data.len() as u64 != plan.partition.size {
|
||||
return Err(InstallError::BackupSize(backup_path));
|
||||
}
|
||||
let backup_md5 = md5_hex(&backup_data);
|
||||
if report.live_md5 != "unreadable" && backup_md5 != report.live_md5 {
|
||||
return Err(InstallError::BackupMismatch);
|
||||
}
|
||||
let backup_sha256: String = Sha256::digest(&backup_data)
|
||||
.iter()
|
||||
.map(|byte| format!("{byte:02x}"))
|
||||
.collect();
|
||||
write_sidecars(
|
||||
&backup_path,
|
||||
&backup_md5,
|
||||
&backup_sha256,
|
||||
&plan.partition,
|
||||
&plan.expected_sha256,
|
||||
epoch,
|
||||
)?;
|
||||
|
||||
stage_and_write(plan, remote, &backup_path)?;
|
||||
|
||||
let rebooted = if options.no_reboot {
|
||||
false
|
||||
} else {
|
||||
remote.reboot()?;
|
||||
true
|
||||
};
|
||||
Ok(InstallOutcome::Installed(InstallReceipt {
|
||||
backup_path,
|
||||
backup_md5,
|
||||
backup_sha256,
|
||||
image_md5: plan.expected_md5.clone(),
|
||||
rebooted,
|
||||
}))
|
||||
}
|
||||
|
||||
fn stage_and_write(
|
||||
plan: &Cv1835Plan,
|
||||
remote: &mut dyn Remote,
|
||||
backup_path: &Path,
|
||||
) -> Result<(), InstallError> {
|
||||
let mut staged =
|
||||
tempfile::NamedTempFile::new_in(backup_path.parent().unwrap_or_else(|| Path::new(".")))?;
|
||||
staged.write_all(&plan.data)?;
|
||||
staged.flush()?;
|
||||
remote.push(staged.path(), REMOTE_STAGE, Duration::from_secs(240))?;
|
||||
let staged_md5 =
|
||||
remote.root_command(&format!("md5sum {REMOTE_STAGE}"), Duration::from_secs(60))?;
|
||||
let staged_md5 = String::from_utf8_lossy(&staged_md5)
|
||||
.split_whitespace()
|
||||
.next()
|
||||
.unwrap_or("")
|
||||
.to_ascii_lowercase();
|
||||
if staged_md5 != plan.expected_md5 {
|
||||
let _ = remote.root_command(&format!("rm -f {REMOTE_STAGE}"), Duration::from_secs(15));
|
||||
return Err(InstallError::StageMismatch);
|
||||
}
|
||||
|
||||
remote.root_command(
|
||||
&format!(
|
||||
"dd if={REMOTE_STAGE} of={} bs=1M conv=fsync 2>&1; sync",
|
||||
plan.partition.device
|
||||
),
|
||||
Duration::from_secs(240),
|
||||
)?;
|
||||
let readback = remote.root_command(
|
||||
&format!("md5sum {}", plan.partition.device),
|
||||
Duration::from_secs(90),
|
||||
)?;
|
||||
let readback = String::from_utf8_lossy(&readback)
|
||||
.split_whitespace()
|
||||
.next()
|
||||
.unwrap_or("")
|
||||
.to_ascii_lowercase();
|
||||
let _ = remote.root_command(&format!("rm -f {REMOTE_STAGE}"), Duration::from_secs(15));
|
||||
if readback != plan.expected_md5 {
|
||||
return Err(InstallError::Readback(restore_command(
|
||||
&plan.partition.device,
|
||||
backup_path,
|
||||
)));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct RestoreOptions {
|
||||
pub confirm: Option<String>,
|
||||
pub no_reboot: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct RestoreReceipt {
|
||||
pub backup_path: PathBuf,
|
||||
pub image_md5: String,
|
||||
pub rebooted: bool,
|
||||
}
|
||||
|
||||
pub fn restore(
|
||||
backup: &Path,
|
||||
remote: &mut dyn Remote,
|
||||
options: &RestoreOptions,
|
||||
) -> Result<RestoreReceipt, InstallError> {
|
||||
if options.confirm.as_deref() != Some("FLASH") {
|
||||
return Err(InstallError::Confirm);
|
||||
}
|
||||
let data = fs::read(backup)?;
|
||||
if data.len() != 16 * 1024 * 1024 {
|
||||
return Err(InstallError::BackupSize(backup.to_path_buf()));
|
||||
}
|
||||
let md5 = md5_hex(&data);
|
||||
let sha256: String = Sha256::digest(&data)
|
||||
.iter()
|
||||
.map(|byte| format!("{byte:02x}"))
|
||||
.collect();
|
||||
if let Some(sidecar) = read_sidecar(backup) {
|
||||
if sidecar != sha256 {
|
||||
return Err(InstallError::BackupMismatch);
|
||||
}
|
||||
}
|
||||
|
||||
let root_uid = String::from_utf8_lossy(&remote.root_command("id -u", Duration::from_secs(20))?)
|
||||
.trim()
|
||||
.to_owned();
|
||||
if root_uid != "0" {
|
||||
return Err(InstallError::Preflight(format!(
|
||||
"root channel returned uid {root_uid}"
|
||||
)));
|
||||
}
|
||||
let size = remote.user_command(
|
||||
"cat /sys/class/block/mmcblk0p1/size 2>/dev/null || true",
|
||||
Duration::from_secs(15),
|
||||
)?;
|
||||
let sectors: u64 = String::from_utf8_lossy(&size).trim().parse().unwrap_or(0);
|
||||
if sectors.saturating_mul(512) != 16 * 1024 * 1024 {
|
||||
return Err(InstallError::Preflight(
|
||||
"the board does not expose a 16 MiB /dev/mmcblk0p1".to_owned(),
|
||||
));
|
||||
}
|
||||
|
||||
let staged = tempfile::NamedTempFile::new()?;
|
||||
fs::write(staged.path(), &data)?;
|
||||
remote.push(staged.path(), REMOTE_STAGE, Duration::from_secs(240))?;
|
||||
let staged_md5 =
|
||||
remote.root_command(&format!("md5sum {REMOTE_STAGE}"), Duration::from_secs(60))?;
|
||||
let staged_md5 = String::from_utf8_lossy(&staged_md5)
|
||||
.split_whitespace()
|
||||
.next()
|
||||
.unwrap_or("")
|
||||
.to_ascii_lowercase();
|
||||
if staged_md5 != md5 {
|
||||
let _ = remote.root_command(&format!("rm -f {REMOTE_STAGE}"), Duration::from_secs(15));
|
||||
return Err(InstallError::StageMismatch);
|
||||
}
|
||||
remote.root_command(
|
||||
&format!("dd if={REMOTE_STAGE} of=/dev/mmcblk0p1 bs=1M conv=fsync 2>&1; sync"),
|
||||
Duration::from_secs(240),
|
||||
)?;
|
||||
let readback = remote.root_command("md5sum /dev/mmcblk0p1", Duration::from_secs(90))?;
|
||||
let readback = String::from_utf8_lossy(&readback)
|
||||
.split_whitespace()
|
||||
.next()
|
||||
.unwrap_or("")
|
||||
.to_ascii_lowercase();
|
||||
let _ = remote.root_command(&format!("rm -f {REMOTE_STAGE}"), Duration::from_secs(15));
|
||||
if readback != md5 {
|
||||
return Err(InstallError::RestoreReadback);
|
||||
}
|
||||
let rebooted = if options.no_reboot {
|
||||
false
|
||||
} else {
|
||||
remote.reboot()?;
|
||||
true
|
||||
};
|
||||
Ok(RestoreReceipt {
|
||||
backup_path: backup.to_path_buf(),
|
||||
image_md5: md5,
|
||||
rebooted,
|
||||
})
|
||||
}
|
||||
|
||||
fn restore_command(device: &str, backup: &Path) -> String {
|
||||
format!(
|
||||
"alkane-cli afw restore --host <ip> --held-backup {} --confirm FLASH (the held copy of {device} is at {})",
|
||||
backup.display(),
|
||||
backup.display()
|
||||
)
|
||||
}
|
||||
|
||||
fn default_backup_dir() -> PathBuf {
|
||||
alkane_core::platform::config_path("backups")
|
||||
}
|
||||
|
||||
fn write_sidecars(
|
||||
path: &Path,
|
||||
md5: &str,
|
||||
sha256: &str,
|
||||
partition: &alkane_artifacts::afw::AfwPartition,
|
||||
image_sha256: &str,
|
||||
epoch: u64,
|
||||
) -> Result<(), InstallError> {
|
||||
let file_name = path
|
||||
.file_name()
|
||||
.map(|name| name.to_string_lossy().into_owned())
|
||||
.unwrap_or_else(|| "p1.img".to_owned());
|
||||
fs::write(
|
||||
path.with_extension("img.sha256"),
|
||||
format!("{sha256} {file_name}\n"),
|
||||
)?;
|
||||
let metadata = serde_json::json!({
|
||||
"board": "cv1835",
|
||||
"partition": partition.name,
|
||||
"device": partition.device,
|
||||
"size": partition.size,
|
||||
"md5": md5,
|
||||
"sha256": sha256,
|
||||
"afw_sha256": image_sha256,
|
||||
"created_unix": epoch,
|
||||
});
|
||||
fs::write(
|
||||
path.with_extension("img.json"),
|
||||
serde_json::to_vec_pretty(&metadata).unwrap_or_default(),
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn read_sidecar(path: &Path) -> Option<String> {
|
||||
let text = fs::read_to_string(path.with_extension("img.sha256")).ok()?;
|
||||
text.split_whitespace().next().map(str::to_owned)
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct LiveReport {
|
||||
pub attempts: u32,
|
||||
pub http: String,
|
||||
pub http_ok: bool,
|
||||
pub ssh: String,
|
||||
pub ssh_ok: bool,
|
||||
pub api: String,
|
||||
pub api_ok: bool,
|
||||
}
|
||||
|
||||
impl LiveReport {
|
||||
pub fn ok(&self) -> bool {
|
||||
self.http_ok && self.ssh_ok && self.api_ok
|
||||
}
|
||||
|
||||
pub fn render(&self) -> String {
|
||||
format!(
|
||||
"verify-live attempts {}\n http {}: {}\n ssh {}: {}\n api {}: {}\nresult {}",
|
||||
self.attempts,
|
||||
if self.http_ok { "ok" } else { "FAIL" },
|
||||
self.http,
|
||||
if self.ssh_ok { "ok" } else { "FAIL" },
|
||||
self.ssh,
|
||||
if self.api_ok { "ok" } else { "FAIL" },
|
||||
self.api,
|
||||
if self.ok() { "PASS" } else { "FAIL" }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn verify_live(
|
||||
host: &str,
|
||||
marker: &str,
|
||||
kernel_version: &str,
|
||||
timeout: Duration,
|
||||
ssh_options: &SshOptions,
|
||||
) -> LiveReport {
|
||||
let deadline = Instant::now() + timeout;
|
||||
let mut attempts = 0;
|
||||
loop {
|
||||
attempts += 1;
|
||||
let (http, http_ok) = match http_get(host, "/", Duration::from_secs(5)) {
|
||||
Ok(body) => {
|
||||
let ok = body.contains(marker);
|
||||
(
|
||||
if ok {
|
||||
format!("marker {marker} present")
|
||||
} else {
|
||||
format!("marker {marker} absent ({} bytes)", body.len())
|
||||
},
|
||||
ok,
|
||||
)
|
||||
}
|
||||
Err(error) => (error, false),
|
||||
};
|
||||
let (ssh, ssh_ok) = match SshTransport::connect(ssh_options.clone(), false) {
|
||||
Ok(mut session) => match session.user_command("uname -r", Duration::from_secs(15)) {
|
||||
Ok(output) => {
|
||||
let kernel = String::from_utf8_lossy(&output).trim().to_owned();
|
||||
let ok = !kernel.is_empty()
|
||||
&& (kernel_version.is_empty() || kernel.contains(kernel_version));
|
||||
(
|
||||
format!(
|
||||
"kernel {kernel}{}",
|
||||
if ok {
|
||||
""
|
||||
} else {
|
||||
" (expected the installed kernel)"
|
||||
}
|
||||
),
|
||||
ok,
|
||||
)
|
||||
}
|
||||
Err(error) => (format!("{error}"), false),
|
||||
},
|
||||
Err(error) => (format!("{error}"), false),
|
||||
};
|
||||
let (api, api_ok) = match api_version(host, Duration::from_secs(5)) {
|
||||
Ok(body) => {
|
||||
let ok = body.contains("\"STATUS\"")
|
||||
|| body.contains("\"VERSION\"")
|
||||
|| body.to_lowercase().contains("version");
|
||||
(format!("{} bytes", body.len()), ok)
|
||||
}
|
||||
Err(error) => (error, false),
|
||||
};
|
||||
if http_ok && ssh_ok && api_ok {
|
||||
return LiveReport {
|
||||
attempts,
|
||||
http,
|
||||
http_ok,
|
||||
ssh,
|
||||
ssh_ok,
|
||||
api,
|
||||
api_ok,
|
||||
};
|
||||
}
|
||||
if Instant::now() >= deadline {
|
||||
return LiveReport {
|
||||
attempts,
|
||||
http,
|
||||
http_ok,
|
||||
ssh,
|
||||
ssh_ok,
|
||||
api,
|
||||
api_ok,
|
||||
};
|
||||
}
|
||||
std::thread::sleep(Duration::from_secs(5));
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn http_get(host: &str, path: &str, timeout: Duration) -> Result<String, String> {
|
||||
let address = (host, 80_u16)
|
||||
.to_socket_addrs()
|
||||
.map_err(|error| format!("resolve: {error}"))?
|
||||
.next()
|
||||
.ok_or_else(|| "no address".to_owned())?;
|
||||
let mut stream = TcpStream::connect_timeout(&address, timeout)
|
||||
.map_err(|error| format!("connect: {error}"))?;
|
||||
stream
|
||||
.set_read_timeout(Some(timeout))
|
||||
.map_err(|error| error.to_string())?;
|
||||
let request = format!("GET {path} HTTP/1.0\r\nHost: {host}\r\nConnection: close\r\n\r\n");
|
||||
stream
|
||||
.write_all(request.as_bytes())
|
||||
.map_err(|error| error.to_string())?;
|
||||
let mut data = Vec::new();
|
||||
let mut buffer = [0_u8; 8192];
|
||||
loop {
|
||||
match stream.read(&mut buffer) {
|
||||
Ok(0) => break,
|
||||
Ok(read) => {
|
||||
data.extend_from_slice(&buffer[..read]);
|
||||
if data.len() > 512 * 1024 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => break,
|
||||
Err(error) if error.kind() == std::io::ErrorKind::TimedOut => break,
|
||||
Err(error) => return Err(error.to_string()),
|
||||
}
|
||||
}
|
||||
let text = String::from_utf8_lossy(&data).into_owned();
|
||||
let status_ok = text
|
||||
.lines()
|
||||
.next()
|
||||
.map(|line| line.contains(" 200 ") || line.ends_with(" 200"))
|
||||
.unwrap_or(false);
|
||||
if status_ok {
|
||||
Ok(text)
|
||||
} else {
|
||||
Err(text.lines().next().unwrap_or("no HTTP response").to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn api_version(host: &str, timeout: Duration) -> Result<String, String> {
|
||||
let address = (host, 4028_u16)
|
||||
.to_socket_addrs()
|
||||
.map_err(|error| format!("resolve: {error}"))?
|
||||
.next()
|
||||
.ok_or_else(|| "no address".to_owned())?;
|
||||
let mut stream = TcpStream::connect_timeout(&address, timeout)
|
||||
.map_err(|error| format!("connect: {error}"))?;
|
||||
stream
|
||||
.set_read_timeout(Some(timeout))
|
||||
.map_err(|error| error.to_string())?;
|
||||
stream
|
||||
.write_all(b"{\"command\":\"version\"}\n")
|
||||
.map_err(|error| error.to_string())?;
|
||||
let mut data = Vec::new();
|
||||
let mut buffer = [0_u8; 4096];
|
||||
loop {
|
||||
match stream.read(&mut buffer) {
|
||||
Ok(0) => break,
|
||||
Ok(read) => {
|
||||
data.extend_from_slice(&buffer[..read]);
|
||||
if data.len() > 64 * 1024 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => break,
|
||||
Err(error) if error.kind() == std::io::ErrorKind::TimedOut => break,
|
||||
Err(error) => return Err(error.to_string()),
|
||||
}
|
||||
}
|
||||
let text = String::from_utf8_lossy(&data).trim().to_owned();
|
||||
if text.is_empty() {
|
||||
Err("no API response".to_owned())
|
||||
} else {
|
||||
Ok(text)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn derives_backup_sidecar_names() {
|
||||
let path = Path::new("/tmp/p1-1.img");
|
||||
assert_eq!(
|
||||
path.with_extension("img.sha256"),
|
||||
Path::new("/tmp/p1-1.img.sha256")
|
||||
);
|
||||
assert_eq!(
|
||||
path.with_extension("img.json"),
|
||||
Path::new("/tmp/p1-1.img.json")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn restore_command_names_the_held_copy() {
|
||||
let command = restore_command("/dev/mmcblk0p1", Path::new("/tmp/p1.img"));
|
||||
assert!(command.contains("afw restore"));
|
||||
assert!(command.contains("/tmp/p1.img"));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod network_tests {
|
||||
use super::*;
|
||||
use alkane_artifacts::afw::{AfwImage, AfwManifest, AfwMember, AfwPartition, BootloaderPolicy};
|
||||
use serde_json::json;
|
||||
use std::collections::HashMap;
|
||||
|
||||
const P1: usize = 16 * 1024 * 1024;
|
||||
const REV: &str = "0000000000000000000000000000000000000000";
|
||||
|
||||
struct MockRemote {
|
||||
partition: Vec<u8>,
|
||||
files: HashMap<String, Vec<u8>>,
|
||||
commands: Vec<String>,
|
||||
rebooted: bool,
|
||||
readback_corrupt: bool,
|
||||
}
|
||||
|
||||
impl MockRemote {
|
||||
fn new(partition: Vec<u8>) -> Self {
|
||||
Self {
|
||||
partition,
|
||||
files: HashMap::new(),
|
||||
commands: Vec::new(),
|
||||
rebooted: false,
|
||||
readback_corrupt: false,
|
||||
}
|
||||
}
|
||||
|
||||
fn md5(&self, data: &[u8]) -> String {
|
||||
md5_hex(data)
|
||||
}
|
||||
}
|
||||
|
||||
impl Remote for MockRemote {
|
||||
fn channel(&self) -> &str {
|
||||
"mock"
|
||||
}
|
||||
|
||||
fn root_method(&self) -> &'static str {
|
||||
"direct"
|
||||
}
|
||||
|
||||
fn user_command(
|
||||
&mut self,
|
||||
command: &str,
|
||||
_timeout: Duration,
|
||||
) -> Result<Vec<u8>, RemoteError> {
|
||||
self.commands.push(command.to_owned());
|
||||
let text = if command.contains("uname -r") {
|
||||
"6.18.48\n__MODEL__\nAlkane CV1835\n__RELEASE__\nalkane\n".to_owned()
|
||||
} else if command.contains("test -b") {
|
||||
"block".to_owned()
|
||||
} else if command.contains("/sys/class/block/mmcblk0p1/size") {
|
||||
"32768".to_owned()
|
||||
} else if command.contains("for c in dd") {
|
||||
"dd\nmd5sum\nsync\n".to_owned()
|
||||
} else if command.contains("df -k") {
|
||||
"Filesystem 1K-blocks Used Available Use% Mounted on\n/dev/root 100000 1 90000 1% /tmp\n".to_owned()
|
||||
} else if command.contains("mmcblk0boot0") {
|
||||
"yes".to_owned()
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
Ok(text.into_bytes())
|
||||
}
|
||||
|
||||
fn root_command(
|
||||
&mut self,
|
||||
command: &str,
|
||||
_timeout: Duration,
|
||||
) -> Result<Vec<u8>, RemoteError> {
|
||||
self.commands.push(command.to_owned());
|
||||
if command.starts_with("id -u") || command == "id" {
|
||||
return Ok(b"0\n".to_vec());
|
||||
}
|
||||
if let Some(rest) = command.strip_prefix("md5sum /dev/mmcblk0p1") {
|
||||
let _ = rest;
|
||||
let mut data = self.partition.clone();
|
||||
if self.readback_corrupt && self.files.contains_key(REMOTE_STAGE) {
|
||||
if let Some(last) = data.last_mut() {
|
||||
*last ^= 0xff;
|
||||
}
|
||||
self.readback_corrupt = false;
|
||||
}
|
||||
return Ok(format!("{} /dev/mmcblk0p1\n", self.md5(&data)).into_bytes());
|
||||
}
|
||||
if let Some(rest) = command.strip_prefix("md5sum ") {
|
||||
let path = rest.split_whitespace().next().unwrap_or("");
|
||||
let data = self.files.get(path).cloned().unwrap_or_default();
|
||||
return Ok(format!("{} {path}\n", self.md5(&data)).into_bytes());
|
||||
}
|
||||
if let Some(rest) = command.strip_prefix("dd if=") {
|
||||
let mut parts = rest.splitn(2, " of=");
|
||||
let source = parts.next().unwrap_or("");
|
||||
let target = parts
|
||||
.next()
|
||||
.unwrap_or("")
|
||||
.split_whitespace()
|
||||
.next()
|
||||
.unwrap_or("");
|
||||
if source == "/dev/mmcblk0p1" {
|
||||
self.files.insert(target.to_owned(), self.partition.clone());
|
||||
} else if target == "/dev/mmcblk0p1" {
|
||||
if let Some(data) = self.files.get(source).cloned() {
|
||||
self.partition = data;
|
||||
}
|
||||
}
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
if command.starts_with("rm -f ") {
|
||||
let path = command.trim_start_matches("rm -f ").trim();
|
||||
self.files.remove(path);
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
Ok(Vec::new())
|
||||
}
|
||||
|
||||
fn push(
|
||||
&mut self,
|
||||
local: &Path,
|
||||
remote: &str,
|
||||
_timeout: Duration,
|
||||
) -> Result<(), RemoteError> {
|
||||
self.files.insert(remote.to_owned(), fs::read(local)?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn pull(
|
||||
&mut self,
|
||||
remote: &str,
|
||||
local: &Path,
|
||||
_timeout: Duration,
|
||||
) -> Result<(), RemoteError> {
|
||||
let data = self
|
||||
.files
|
||||
.get(remote)
|
||||
.cloned()
|
||||
.ok_or_else(|| RemoteError::Daemon("missing file".to_owned()))?;
|
||||
fs::write(local, data)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn reboot(&mut self) -> Result<(), RemoteError> {
|
||||
self.rebooted = true;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn p1_image() -> Vec<u8> {
|
||||
let mut fit = vec![0_u8; P1];
|
||||
fit[..4].copy_from_slice(b"\xd0\x0d\xfe\xed");
|
||||
let marker = b"config@cv1835_miner_emmc";
|
||||
fit[0x100..0x100 + marker.len()].copy_from_slice(marker);
|
||||
fit
|
||||
}
|
||||
|
||||
fn verified(data: &[u8]) -> VerifiedAfw {
|
||||
let manifest = AfwManifest {
|
||||
schema: 2,
|
||||
format: "afw".to_owned(),
|
||||
board: "cv1835".to_owned(),
|
||||
arch: "arm64".to_owned(),
|
||||
version: "0.1.0".to_owned(),
|
||||
build_date: "2026-11-04T00:00:00Z".to_owned(),
|
||||
sdk_revision: REV.to_owned(),
|
||||
os_revision: REV.to_owned(),
|
||||
payload_type: "os".to_owned(),
|
||||
bootloader_policy: BootloaderPolicy::Preserve,
|
||||
signature: alkane_artifacts::afw::AfwSignature {
|
||||
kind: "unsigned".to_owned(),
|
||||
member: None,
|
||||
public_key_hex: None,
|
||||
},
|
||||
partitions: vec![
|
||||
AfwPartition {
|
||||
name: "p1".to_owned(),
|
||||
device: "/dev/mmcblk0p1".to_owned(),
|
||||
offset: 0,
|
||||
size: P1 as u64,
|
||||
secure: false,
|
||||
boot_critical: true,
|
||||
write_order: 1,
|
||||
image: Some("p1".to_owned()),
|
||||
},
|
||||
AfwPartition {
|
||||
name: "boot0".to_owned(),
|
||||
device: "/dev/mmcblk0boot0".to_owned(),
|
||||
offset: 0,
|
||||
size: 4 * 1024 * 1024,
|
||||
secure: true,
|
||||
boot_critical: true,
|
||||
write_order: 0,
|
||||
image: None,
|
||||
},
|
||||
],
|
||||
images: vec![AfwImage {
|
||||
name: "p1".to_owned(),
|
||||
file: "payload/p1.fit".to_owned(),
|
||||
role: "boot".to_owned(),
|
||||
format: "fit".to_owned(),
|
||||
size: P1 as u64,
|
||||
sha256: Sha256::digest(data)
|
||||
.iter()
|
||||
.map(|byte| format!("{byte:02x}"))
|
||||
.collect(),
|
||||
partition: Some("p1".to_owned()),
|
||||
install: true,
|
||||
}],
|
||||
boot_switch: json!({"method": "partition-overwrite"}),
|
||||
preflight: json!({"required_commands": ["dd", "md5sum", "sync"],
|
||||
"source_os": ["stock-bitmain", "vnish", "braiins", "ouros", "alkane"],
|
||||
"min_free_bytes": 33554432}),
|
||||
recovery: json!({"network": "afw restore", "golden_lba_bytes": 0x300000u64 * 512}),
|
||||
marker: "Alkane".to_owned(),
|
||||
kernel_version: "6.18.48".to_owned(),
|
||||
notes: None,
|
||||
};
|
||||
VerifiedAfw {
|
||||
manifest,
|
||||
members: vec![AfwMember {
|
||||
name: "payload/p1.fit".to_owned(),
|
||||
data: data.to_vec(),
|
||||
}],
|
||||
manifest_sha256: "0".repeat(64),
|
||||
archive_sha256: "0".repeat(64),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn installs_p1_holds_a_backup_and_verifies_before_reboot() {
|
||||
let original = vec![0x11_u8; P1];
|
||||
let new = p1_image();
|
||||
let afw = verified(&new);
|
||||
let plan = Cv1835Plan::from_afw(&afw).unwrap();
|
||||
let mut remote = MockRemote::new(original.clone());
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
let options = InstallOptions {
|
||||
dry_run: false,
|
||||
confirm: Some("FLASH".to_owned()),
|
||||
no_reboot: true,
|
||||
backup_dir: temp.path().to_path_buf(),
|
||||
held_backup: None,
|
||||
allow_unknown_source: true,
|
||||
check_golden: false,
|
||||
};
|
||||
let outcome = install(&afw, &plan, &mut remote, &options, "10.255.1.1").unwrap();
|
||||
let InstallOutcome::Installed(receipt) = outcome else {
|
||||
panic!("expected an install");
|
||||
};
|
||||
assert_eq!(remote.partition, new);
|
||||
assert_eq!(receipt.backup_md5, md5_hex(&original));
|
||||
assert!(receipt.backup_path.is_file());
|
||||
assert!(!remote.rebooted);
|
||||
assert!(remote
|
||||
.commands
|
||||
.iter()
|
||||
.any(|command| command.starts_with("dd if=/tmp/.alkane-p1-stage.img")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dry_run_never_writes_the_partition() {
|
||||
let original = vec![0x22_u8; P1];
|
||||
let new = p1_image();
|
||||
let afw = verified(&new);
|
||||
let plan = Cv1835Plan::from_afw(&afw).unwrap();
|
||||
let mut remote = MockRemote::new(original.clone());
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
let options = InstallOptions {
|
||||
dry_run: true,
|
||||
confirm: None,
|
||||
no_reboot: false,
|
||||
backup_dir: temp.path().to_path_buf(),
|
||||
held_backup: None,
|
||||
allow_unknown_source: true,
|
||||
check_golden: false,
|
||||
};
|
||||
let outcome = install(&afw, &plan, &mut remote, &options, "10.255.1.1").unwrap();
|
||||
assert!(matches!(outcome, InstallOutcome::DryRun(_)));
|
||||
assert_eq!(remote.partition, original);
|
||||
assert!(!remote
|
||||
.commands
|
||||
.iter()
|
||||
.any(|command| command.starts_with("dd if=/tmp/.alkane-p1-stage.img")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_readback_mismatch_stops_before_reboot() {
|
||||
let original = vec![0x33_u8; P1];
|
||||
let new = p1_image();
|
||||
let afw = verified(&new);
|
||||
let plan = Cv1835Plan::from_afw(&afw).unwrap();
|
||||
let mut remote = MockRemote::new(original);
|
||||
remote.readback_corrupt = true;
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
let options = InstallOptions {
|
||||
dry_run: false,
|
||||
confirm: Some("FLASH".to_owned()),
|
||||
no_reboot: false,
|
||||
backup_dir: temp.path().to_path_buf(),
|
||||
held_backup: None,
|
||||
allow_unknown_source: true,
|
||||
check_golden: false,
|
||||
};
|
||||
let result = install(&afw, &plan, &mut remote, &options, "10.255.1.1");
|
||||
assert!(matches!(result, Err(InstallError::Readback(_))));
|
||||
assert!(!remote.rebooted);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn confirm_is_required_before_any_device_write() {
|
||||
let original = vec![0x44_u8; P1];
|
||||
let new = p1_image();
|
||||
let afw = verified(&new);
|
||||
let plan = Cv1835Plan::from_afw(&afw).unwrap();
|
||||
let mut remote = MockRemote::new(original.clone());
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
let options = InstallOptions {
|
||||
dry_run: false,
|
||||
confirm: None,
|
||||
no_reboot: false,
|
||||
backup_dir: temp.path().to_path_buf(),
|
||||
held_backup: None,
|
||||
allow_unknown_source: true,
|
||||
check_golden: false,
|
||||
};
|
||||
let result = install(&afw, &plan, &mut remote, &options, "10.255.1.1");
|
||||
assert!(matches!(result, Err(InstallError::Confirm)));
|
||||
assert_eq!(remote.partition, original);
|
||||
}
|
||||
}
|
||||
@@ -1 +1,476 @@
|
||||
// alkane-flash
|
||||
// SPDX-License-Identifier: MIT
|
||||
//! Guarded writes plus the network-only CV1835 installer.
|
||||
//!
|
||||
//! The original guarded-write helpers remain for boards that need an attached
|
||||
//! recovery channel. CV1835 uses the network modules below: a verified `.afw`,
|
||||
//! a board plan, an SSH transport with a stock-root stage, a read-only
|
||||
//! preflight, a whole-partition read-back and a network restore.
|
||||
|
||||
// The board modules are driven by static NAND/partition map tables (tuples) and
|
||||
// multi-field hardware write functions; the two style lints below are design
|
||||
// choices for this crate rather than defects.
|
||||
#![allow(clippy::type_complexity, clippy::too_many_arguments)]
|
||||
|
||||
pub mod boards;
|
||||
pub mod install;
|
||||
pub mod md5;
|
||||
pub mod preflight;
|
||||
pub mod stock;
|
||||
pub mod stock_firmware;
|
||||
// Network-only stock-crossing method bodies compiled into the toolkit; the
|
||||
// public interface lives in `stock`. See stock_method.rs. This module is
|
||||
// maintained as a self-contained unit; the style lints below are allowed here so
|
||||
// its internal layout is not churned to satisfy them.
|
||||
#[allow(
|
||||
clippy::needless_range_loop,
|
||||
clippy::type_complexity,
|
||||
clippy::too_many_arguments,
|
||||
clippy::manual_repeat_n,
|
||||
clippy::manual_is_multiple_of,
|
||||
clippy::unnecessary_map_or
|
||||
)]
|
||||
mod stock_method;
|
||||
pub mod transport;
|
||||
|
||||
use alkane_artifacts::VerifiedArtifact;
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::io::{Read, Seek, SeekFrom, Write};
|
||||
use thiserror::Error;
|
||||
|
||||
pub struct PartitionSpec {
|
||||
pub name: String,
|
||||
pub capacity_bytes: u64,
|
||||
pub secure: bool,
|
||||
pub boot_critical: bool,
|
||||
}
|
||||
|
||||
pub struct BackupEvidence {
|
||||
pub location: String,
|
||||
pub bytes: u64,
|
||||
pub sha256: [u8; 32],
|
||||
}
|
||||
|
||||
pub struct WriteReceipt {
|
||||
pub partition: String,
|
||||
pub backup: BackupEvidence,
|
||||
pub image_sha256: [u8; 32],
|
||||
}
|
||||
|
||||
pub trait Writer {
|
||||
type Error: std::fmt::Display;
|
||||
|
||||
fn channel_id(&self) -> &str;
|
||||
fn serial_console_attached(&mut self) -> Result<bool, Self::Error>;
|
||||
fn recovery_image_ready(&mut self, partition: &PartitionSpec) -> Result<bool, Self::Error>;
|
||||
fn backup(&mut self, partition: &PartitionSpec) -> Result<BackupEvidence, Self::Error>;
|
||||
fn write(
|
||||
&mut self,
|
||||
partition: &PartitionSpec,
|
||||
image: &mut dyn Read,
|
||||
image_size: u64,
|
||||
) -> Result<(), Self::Error>;
|
||||
}
|
||||
|
||||
pub trait IndependentReadback {
|
||||
type Error: std::fmt::Display;
|
||||
|
||||
fn channel_id(&self) -> &str;
|
||||
fn sha256(&mut self, partition: &PartitionSpec, length: u64) -> Result<[u8; 32], Self::Error>;
|
||||
}
|
||||
|
||||
#[derive(Debug, Error, Eq, PartialEq)]
|
||||
pub enum FlashError {
|
||||
#[error("secure partition write refused")]
|
||||
SecurePartition,
|
||||
#[error("image exceeds partition capacity")]
|
||||
ImageTooLarge,
|
||||
#[error("boot-critical write requires an attached serial console")]
|
||||
SerialRequired,
|
||||
#[error("boot-critical write requires verified recovery media")]
|
||||
RecoveryRequired,
|
||||
#[error("readback must use an independent channel")]
|
||||
SameChannel,
|
||||
#[error("complete backup evidence is missing")]
|
||||
BackupIncomplete,
|
||||
#[error("post-write readback differs from the image")]
|
||||
ReadbackMismatch,
|
||||
#[error("verified image has an invalid digest")]
|
||||
InvalidDigest,
|
||||
#[error("image changed since verification")]
|
||||
ArtifactChanged,
|
||||
#[error("writer did not consume the complete image")]
|
||||
IncompleteWrite,
|
||||
#[error("write transport: {0}")]
|
||||
Write(String),
|
||||
#[error("readback transport: {0}")]
|
||||
Readback(String),
|
||||
#[error("artifact I/O: {0}")]
|
||||
ArtifactIo(String),
|
||||
}
|
||||
|
||||
fn digest_bytes(hex: &str) -> Result<[u8; 32], FlashError> {
|
||||
if hex.len() != 64 {
|
||||
return Err(FlashError::InvalidDigest);
|
||||
}
|
||||
let mut result = [0_u8; 32];
|
||||
for (index, pair) in hex.as_bytes().chunks_exact(2).enumerate() {
|
||||
let text = std::str::from_utf8(pair).map_err(|_| FlashError::InvalidDigest)?;
|
||||
result[index] = u8::from_str_radix(text, 16).map_err(|_| FlashError::InvalidDigest)?;
|
||||
}
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
fn stage_verified_artifact(
|
||||
artifact: &mut VerifiedArtifact,
|
||||
size: u64,
|
||||
expected: [u8; 32],
|
||||
) -> Result<std::fs::File, FlashError> {
|
||||
let mut staged = tempfile::tempfile().map_err(|e| FlashError::ArtifactIo(e.to_string()))?;
|
||||
artifact
|
||||
.reader()
|
||||
.seek(SeekFrom::Start(0))
|
||||
.map_err(|e| FlashError::ArtifactIo(e.to_string()))?;
|
||||
let mut source = DigestReader {
|
||||
inner: artifact.reader(),
|
||||
remaining: size,
|
||||
digest: Sha256::new(),
|
||||
};
|
||||
std::io::copy(&mut source, &mut staged).map_err(|e| FlashError::ArtifactIo(e.to_string()))?;
|
||||
if source.remaining != 0 || <[u8; 32]>::from(source.digest.finalize()) != expected {
|
||||
return Err(FlashError::ArtifactChanged);
|
||||
}
|
||||
staged
|
||||
.flush()
|
||||
.map_err(|e| FlashError::ArtifactIo(e.to_string()))?;
|
||||
staged
|
||||
.seek(SeekFrom::Start(0))
|
||||
.map_err(|e| FlashError::ArtifactIo(e.to_string()))?;
|
||||
Ok(staged)
|
||||
}
|
||||
|
||||
struct DigestReader<'a> {
|
||||
inner: &'a mut dyn Read,
|
||||
remaining: u64,
|
||||
digest: Sha256,
|
||||
}
|
||||
|
||||
impl Read for DigestReader<'_> {
|
||||
fn read(&mut self, buffer: &mut [u8]) -> std::io::Result<usize> {
|
||||
let limit = buffer
|
||||
.len()
|
||||
.min(usize::try_from(self.remaining).unwrap_or(usize::MAX));
|
||||
let length = self.inner.read(&mut buffer[..limit])?;
|
||||
self.remaining -= length as u64;
|
||||
self.digest.update(&buffer[..length]);
|
||||
Ok(length)
|
||||
}
|
||||
}
|
||||
|
||||
/// Stop before reboot. The caller must still prove live boot and persistence.
|
||||
pub fn write_verified<W: Writer, R: IndependentReadback>(
|
||||
partition: &PartitionSpec,
|
||||
artifact: &mut VerifiedArtifact,
|
||||
writer: &mut W,
|
||||
readback: &mut R,
|
||||
) -> Result<WriteReceipt, FlashError> {
|
||||
if partition.secure {
|
||||
return Err(FlashError::SecurePartition);
|
||||
}
|
||||
let size = artifact.record().size_bytes;
|
||||
if size == 0 || partition.capacity_bytes == 0 || size > partition.capacity_bytes {
|
||||
return Err(FlashError::ImageTooLarge);
|
||||
}
|
||||
if writer.channel_id().is_empty()
|
||||
|| readback.channel_id().is_empty()
|
||||
|| writer.channel_id() == readback.channel_id()
|
||||
{
|
||||
return Err(FlashError::SameChannel);
|
||||
}
|
||||
if partition.boot_critical {
|
||||
if !writer
|
||||
.serial_console_attached()
|
||||
.map_err(|e| FlashError::Write(e.to_string()))?
|
||||
{
|
||||
return Err(FlashError::SerialRequired);
|
||||
}
|
||||
if !writer
|
||||
.recovery_image_ready(partition)
|
||||
.map_err(|e| FlashError::Write(e.to_string()))?
|
||||
{
|
||||
return Err(FlashError::RecoveryRequired);
|
||||
}
|
||||
}
|
||||
artifact
|
||||
.revalidate()
|
||||
.map_err(|_| FlashError::ArtifactChanged)?;
|
||||
let expected = digest_bytes(&artifact.record().sha256)?;
|
||||
// The source path may change while backup or transport I/O runs. Copy and
|
||||
// verify it before any device write; the anonymous file is removed on drop.
|
||||
let mut staged = stage_verified_artifact(artifact, size, expected)?;
|
||||
let backup = writer
|
||||
.backup(partition)
|
||||
.map_err(|e| FlashError::Write(e.to_string()))?;
|
||||
if backup.location.is_empty()
|
||||
|| backup.bytes != partition.capacity_bytes
|
||||
|| backup.sha256 == [0; 32]
|
||||
{
|
||||
return Err(FlashError::BackupIncomplete);
|
||||
}
|
||||
let mut stream = DigestReader {
|
||||
inner: &mut staged,
|
||||
remaining: size,
|
||||
digest: Sha256::new(),
|
||||
};
|
||||
writer
|
||||
.write(partition, &mut stream, size)
|
||||
.map_err(|e| FlashError::Write(e.to_string()))?;
|
||||
if stream.remaining != 0 {
|
||||
return Err(FlashError::IncompleteWrite);
|
||||
}
|
||||
let written_digest: [u8; 32] = stream.digest.finalize().into();
|
||||
if written_digest != expected {
|
||||
return Err(FlashError::ArtifactChanged);
|
||||
}
|
||||
let actual = readback
|
||||
.sha256(partition, size)
|
||||
.map_err(|e| FlashError::Readback(e.to_string()))?;
|
||||
if actual != expected {
|
||||
return Err(FlashError::ReadbackMismatch);
|
||||
}
|
||||
Ok(WriteReceipt {
|
||||
partition: partition.name.clone(),
|
||||
backup,
|
||||
image_sha256: expected,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use alkane_artifacts::{verify_file, ArtifactRecord};
|
||||
use std::path::PathBuf;
|
||||
|
||||
const SHA: &str = "bf7d458495ed067341c7148cd3d30e6d383a2113b2cfb37de12959e057a52528";
|
||||
const IMAGE: &[u8] = b"PK\x03\x04payload";
|
||||
|
||||
struct MockWriter {
|
||||
serial: bool,
|
||||
recovery: bool,
|
||||
backup_bytes: u64,
|
||||
max_read: Option<u64>,
|
||||
writes: usize,
|
||||
contents: Vec<u8>,
|
||||
mutate_source_on_write: Option<PathBuf>,
|
||||
}
|
||||
|
||||
impl Writer for MockWriter {
|
||||
type Error = &'static str;
|
||||
|
||||
fn channel_id(&self) -> &str {
|
||||
"ssh"
|
||||
}
|
||||
fn serial_console_attached(&mut self) -> Result<bool, Self::Error> {
|
||||
Ok(self.serial)
|
||||
}
|
||||
fn recovery_image_ready(&mut self, _: &PartitionSpec) -> Result<bool, Self::Error> {
|
||||
Ok(self.recovery)
|
||||
}
|
||||
fn backup(&mut self, _: &PartitionSpec) -> Result<BackupEvidence, Self::Error> {
|
||||
Ok(BackupEvidence {
|
||||
location: "recovery/partition.img".to_owned(),
|
||||
bytes: self.backup_bytes,
|
||||
sha256: [1; 32],
|
||||
})
|
||||
}
|
||||
fn write(
|
||||
&mut self,
|
||||
_: &PartitionSpec,
|
||||
image: &mut dyn Read,
|
||||
_: u64,
|
||||
) -> Result<(), Self::Error> {
|
||||
self.writes += 1;
|
||||
if let Some(path) = &self.mutate_source_on_write {
|
||||
std::fs::write(path, b"PK\x03\x04changed").map_err(|_| "mutation failed")?;
|
||||
}
|
||||
if let Some(limit) = self.max_read {
|
||||
image
|
||||
.take(limit)
|
||||
.read_to_end(&mut self.contents)
|
||||
.map_err(|_| "read failed")?;
|
||||
} else {
|
||||
image
|
||||
.read_to_end(&mut self.contents)
|
||||
.map_err(|_| "read failed")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
struct MockReadback {
|
||||
channel: &'static str,
|
||||
digest: [u8; 32],
|
||||
}
|
||||
|
||||
impl IndependentReadback for MockReadback {
|
||||
type Error = &'static str;
|
||||
|
||||
fn channel_id(&self) -> &str {
|
||||
self.channel
|
||||
}
|
||||
fn sha256(&mut self, _: &PartitionSpec, _: u64) -> Result<[u8; 32], Self::Error> {
|
||||
Ok(self.digest)
|
||||
}
|
||||
}
|
||||
|
||||
fn artifact(suffix: &str) -> (PathBuf, VerifiedArtifact) {
|
||||
let path =
|
||||
std::env::temp_dir().join(format!("alkane-flash-test-{}-{suffix}", std::process::id()));
|
||||
std::fs::write(&path, IMAGE).unwrap();
|
||||
let record = ArtifactRecord {
|
||||
sha256: SHA.to_owned(),
|
||||
size_bytes: IMAGE.len() as u64,
|
||||
path: PathBuf::from("fixture.zip"),
|
||||
family: "test".to_owned(),
|
||||
format: "update-zip".to_owned(),
|
||||
applicability: "test".to_owned(),
|
||||
};
|
||||
let verified = verify_file(&path, &record).unwrap();
|
||||
(path, verified)
|
||||
}
|
||||
|
||||
fn writer() -> MockWriter {
|
||||
MockWriter {
|
||||
serial: true,
|
||||
recovery: true,
|
||||
backup_bytes: 4096,
|
||||
max_read: None,
|
||||
writes: 0,
|
||||
contents: Vec::new(),
|
||||
mutate_source_on_write: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn partition() -> PartitionSpec {
|
||||
PartitionSpec {
|
||||
name: "rootfs".to_owned(),
|
||||
capacity_bytes: 4096,
|
||||
secure: false,
|
||||
boot_critical: true,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refuses_secure_and_unrecoverable_writes_before_device_mutation() {
|
||||
let (path, mut image) = artifact("refusals");
|
||||
let mut target = partition();
|
||||
let mut writer = writer();
|
||||
let mut readback = MockReadback {
|
||||
channel: "serial",
|
||||
digest: digest_bytes(SHA).unwrap(),
|
||||
};
|
||||
target.secure = true;
|
||||
assert!(matches!(
|
||||
write_verified(&target, &mut image, &mut writer, &mut readback),
|
||||
Err(FlashError::SecurePartition)
|
||||
));
|
||||
target.secure = false;
|
||||
target.capacity_bytes = 1;
|
||||
assert!(matches!(
|
||||
write_verified(&target, &mut image, &mut writer, &mut readback),
|
||||
Err(FlashError::ImageTooLarge)
|
||||
));
|
||||
target.capacity_bytes = 4096;
|
||||
writer.serial = false;
|
||||
assert!(matches!(
|
||||
write_verified(&target, &mut image, &mut writer, &mut readback),
|
||||
Err(FlashError::SerialRequired)
|
||||
));
|
||||
writer.serial = true;
|
||||
writer.recovery = false;
|
||||
assert!(matches!(
|
||||
write_verified(&target, &mut image, &mut writer, &mut readback),
|
||||
Err(FlashError::RecoveryRequired)
|
||||
));
|
||||
writer.recovery = true;
|
||||
writer.backup_bytes = 1;
|
||||
assert!(matches!(
|
||||
write_verified(&target, &mut image, &mut writer, &mut readback),
|
||||
Err(FlashError::BackupIncomplete)
|
||||
));
|
||||
assert_eq!(writer.writes, 0);
|
||||
std::fs::remove_file(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn requires_independent_matching_readback() {
|
||||
let (path, mut image) = artifact("readback");
|
||||
let target = partition();
|
||||
let mut writer = writer();
|
||||
let mut readback = MockReadback {
|
||||
channel: "ssh",
|
||||
digest: [0; 32],
|
||||
};
|
||||
assert!(matches!(
|
||||
write_verified(&target, &mut image, &mut writer, &mut readback),
|
||||
Err(FlashError::SameChannel)
|
||||
));
|
||||
assert_eq!(writer.writes, 0);
|
||||
readback.channel = "serial";
|
||||
assert!(matches!(
|
||||
write_verified(&target, &mut image, &mut writer, &mut readback),
|
||||
Err(FlashError::ReadbackMismatch)
|
||||
));
|
||||
assert_eq!(writer.writes, 1);
|
||||
assert_eq!(writer.contents, IMAGE);
|
||||
writer.contents.clear();
|
||||
readback.digest = digest_bytes(SHA).unwrap();
|
||||
let receipt = write_verified(&target, &mut image, &mut writer, &mut readback).unwrap();
|
||||
assert_eq!(receipt.image_sha256, readback.digest);
|
||||
assert_eq!(receipt.backup.bytes, 4096);
|
||||
assert_eq!(writer.writes, 2);
|
||||
assert_eq!(writer.contents, IMAGE);
|
||||
std::fs::remove_file(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refuses_changed_or_partially_consumed_artifacts() {
|
||||
let (path, mut image) = artifact("changed");
|
||||
let target = partition();
|
||||
let mut writer = writer();
|
||||
let mut readback = MockReadback {
|
||||
channel: "serial",
|
||||
digest: digest_bytes(SHA).unwrap(),
|
||||
};
|
||||
std::fs::write(&path, b"PK\x03\x04changed").unwrap();
|
||||
assert!(matches!(
|
||||
write_verified(&target, &mut image, &mut writer, &mut readback),
|
||||
Err(FlashError::ArtifactChanged)
|
||||
));
|
||||
assert_eq!(writer.writes, 0);
|
||||
|
||||
std::fs::write(&path, IMAGE).unwrap();
|
||||
writer.max_read = Some(4);
|
||||
assert!(matches!(
|
||||
write_verified(&target, &mut image, &mut writer, &mut readback),
|
||||
Err(FlashError::IncompleteWrite)
|
||||
));
|
||||
assert_eq!(writer.writes, 1);
|
||||
std::fs::remove_file(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn writes_verified_snapshot_when_source_changes_during_transport() {
|
||||
let (path, mut image) = artifact("mutated-during-write");
|
||||
let mut writer = writer();
|
||||
writer.mutate_source_on_write = Some(path.clone());
|
||||
let mut readback = MockReadback {
|
||||
channel: "serial",
|
||||
digest: digest_bytes(SHA).unwrap(),
|
||||
};
|
||||
let receipt = write_verified(&partition(), &mut image, &mut writer, &mut readback).unwrap();
|
||||
assert_eq!(receipt.image_sha256, digest_bytes(SHA).unwrap());
|
||||
assert_eq!(writer.contents, IMAGE);
|
||||
assert_eq!(std::fs::read(&path).unwrap(), b"PK\x03\x04changed");
|
||||
std::fs::remove_file(path).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
//! MD5 for the CV1835 whole-partition read-back.
|
||||
//!
|
||||
//! The stock busybox on this board ships `md5sum` but not `sha256sum`, and the
|
||||
//! proven installer gates the write on a whole-partition md5. This module is a
|
||||
//! small RFC 1321 implementation so the host can compare the same digest
|
||||
//! without shelling out to a tool that may not exist.
|
||||
|
||||
const S: [u32; 64] = [
|
||||
7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9,
|
||||
14, 20, 5, 9, 14, 20, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 6, 10, 15,
|
||||
21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21,
|
||||
];
|
||||
|
||||
const K: [u32; 64] = [
|
||||
0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
|
||||
0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
|
||||
0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
|
||||
0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
|
||||
0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
|
||||
0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
|
||||
0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
|
||||
0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391,
|
||||
];
|
||||
|
||||
pub fn md5(data: &[u8]) -> [u8; 16] {
|
||||
let mut state = [0x67452301_u32, 0xefcdab89, 0x98badcfe, 0x10325476];
|
||||
let bit_length = (data.len() as u64).wrapping_mul(8);
|
||||
let mut message = data.to_vec();
|
||||
message.push(0x80);
|
||||
while message.len() % 64 != 56 {
|
||||
message.push(0);
|
||||
}
|
||||
message.extend_from_slice(&bit_length.to_le_bytes());
|
||||
|
||||
for chunk in message.chunks_exact(64) {
|
||||
let mut words = [0_u32; 16];
|
||||
for (index, word) in words.iter_mut().enumerate() {
|
||||
let start = index * 4;
|
||||
*word = u32::from_le_bytes([
|
||||
chunk[start],
|
||||
chunk[start + 1],
|
||||
chunk[start + 2],
|
||||
chunk[start + 3],
|
||||
]);
|
||||
}
|
||||
let [mut a, mut b, mut c, mut d] = state;
|
||||
for index in 0..64 {
|
||||
let (f, g) = match index {
|
||||
0..=15 => ((b & c) | (!b & d), index),
|
||||
16..=31 => ((d & b) | (!d & c), (5 * index + 1) % 16),
|
||||
32..=47 => (b ^ c ^ d, (3 * index + 5) % 16),
|
||||
_ => (c ^ (b | !d), (7 * index) % 16),
|
||||
};
|
||||
let rotated = a
|
||||
.wrapping_add(f)
|
||||
.wrapping_add(K[index])
|
||||
.wrapping_add(words[g])
|
||||
.rotate_left(S[index]);
|
||||
a = d;
|
||||
d = c;
|
||||
c = b;
|
||||
b = b.wrapping_add(rotated);
|
||||
}
|
||||
state[0] = state[0].wrapping_add(a);
|
||||
state[1] = state[1].wrapping_add(b);
|
||||
state[2] = state[2].wrapping_add(c);
|
||||
state[3] = state[3].wrapping_add(d);
|
||||
}
|
||||
|
||||
let mut digest = [0_u8; 16];
|
||||
for (index, word) in state.iter().enumerate() {
|
||||
digest[index * 4..index * 4 + 4].copy_from_slice(&word.to_le_bytes());
|
||||
}
|
||||
digest
|
||||
}
|
||||
|
||||
pub fn md5_hex(data: &[u8]) -> String {
|
||||
md5(data).iter().map(|byte| format!("{byte:02x}")).collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn rfc1321_vectors() {
|
||||
assert_eq!(md5_hex(b""), "d41d8cd98f00b204e9800998ecf8427e");
|
||||
assert_eq!(md5_hex(b"a"), "0cc175b9c0f1b6a831c399e269772661");
|
||||
assert_eq!(md5_hex(b"abc"), "900150983cd24fb0d6963f7d28e17f72");
|
||||
assert_eq!(
|
||||
md5_hex(b"message digest"),
|
||||
"f96b697d7cb7938d525a2f31aaf161d0"
|
||||
);
|
||||
assert_eq!(
|
||||
md5_hex(b"abcdefghijklmnopqrstuvwxyz"),
|
||||
"c3fcd3d76192e4007dfb496cca67e13b"
|
||||
);
|
||||
assert_eq!(
|
||||
md5_hex(b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"),
|
||||
"d174ab98d277d9f5a5611c2c9f419d9f"
|
||||
);
|
||||
assert_eq!(
|
||||
md5_hex(
|
||||
b"12345678901234567890123456789012345678901234567890123456789012345678901234567890"
|
||||
),
|
||||
"57edf4a22be3c955ac49da2e2107b67a"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn handles_a_block_boundary() {
|
||||
let data = vec![0x42_u8; 64];
|
||||
assert_eq!(md5_hex(&data), "8e558437ffca99de04c18a63b55a4fad");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,420 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
//! Read-only CV1835 preflight: source-OS fingerprint, root channel, partition
|
||||
//! identity, held/golden backup hashes, free space and secure-region refusal.
|
||||
//!
|
||||
//! Every check runs over the network. A failure is reported, never repaired.
|
||||
|
||||
use crate::boards::cv1835::{Cv1835Plan, CvPlanError};
|
||||
use crate::md5::md5_hex;
|
||||
use crate::transport::{Remote, RemoteError};
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::Duration;
|
||||
use thiserror::Error;
|
||||
|
||||
const P1_NAME: &str = "mmcblk0p1";
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum PreflightError {
|
||||
#[error("preflight transport: {0}")]
|
||||
Remote(#[from] RemoteError),
|
||||
#[error("preflight I/O: {0}")]
|
||||
Io(#[from] std::io::Error),
|
||||
#[error("preflight plan: {0}")]
|
||||
Plan(#[from] CvPlanError),
|
||||
#[error("held backup {path} is {size} bytes, not 16 MiB")]
|
||||
BackupSize { path: PathBuf, size: u64 },
|
||||
#[error("held backup {0} has no readable md5")]
|
||||
BackupUnreadable(PathBuf),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Check {
|
||||
pub name: &'static str,
|
||||
pub ok: bool,
|
||||
pub detail: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct BackupCheck {
|
||||
pub path: PathBuf,
|
||||
pub md5: String,
|
||||
pub sha256: String,
|
||||
pub matches_live: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct PreflightReport {
|
||||
pub host: String,
|
||||
pub source_os: String,
|
||||
pub kernel: String,
|
||||
pub model: String,
|
||||
pub root_method: String,
|
||||
pub root_uid: String,
|
||||
pub partition_device: String,
|
||||
pub partition_size: u64,
|
||||
pub live_md5: String,
|
||||
pub held_backup: Option<BackupCheck>,
|
||||
pub golden_md5: Option<String>,
|
||||
pub free_bytes: u64,
|
||||
pub boot0_present: bool,
|
||||
pub checks: Vec<Check>,
|
||||
}
|
||||
|
||||
impl PreflightReport {
|
||||
pub fn ok(&self) -> bool {
|
||||
self.checks.iter().all(|check| check.ok)
|
||||
}
|
||||
|
||||
pub fn failures(&self) -> Vec<&Check> {
|
||||
self.checks.iter().filter(|check| !check.ok).collect()
|
||||
}
|
||||
|
||||
pub fn render(&self) -> String {
|
||||
let mut out = String::new();
|
||||
out.push_str(&format!("preflight {}\n", self.host));
|
||||
out.push_str(&format!(
|
||||
"source-os {} (kernel {})\n",
|
||||
self.source_os, self.kernel
|
||||
));
|
||||
if !self.model.is_empty() {
|
||||
out.push_str(&format!("model {}\n", self.model));
|
||||
}
|
||||
out.push_str(&format!(
|
||||
"root {} (uid {})\n",
|
||||
self.root_method, self.root_uid
|
||||
));
|
||||
out.push_str(&format!(
|
||||
"partition {} {} B\n",
|
||||
self.partition_device, self.partition_size
|
||||
));
|
||||
out.push_str(&format!("live p1 md5 {}\n", self.live_md5));
|
||||
match &self.held_backup {
|
||||
Some(backup) => out.push_str(&format!(
|
||||
"held backup {} md5 {} ({})\n",
|
||||
backup.path.display(),
|
||||
backup.md5,
|
||||
if backup.matches_live {
|
||||
"matches live p1"
|
||||
} else {
|
||||
"differs from live p1"
|
||||
}
|
||||
)),
|
||||
None => out.push_str("held backup none (install will create one before writing)\n"),
|
||||
}
|
||||
if let Some(golden) = &self.golden_md5 {
|
||||
out.push_str(&format!("golden raw p1 md5 {golden}\n"));
|
||||
}
|
||||
out.push_str(&format!(
|
||||
"free tmp {} MiB\n",
|
||||
self.free_bytes / (1024 * 1024)
|
||||
));
|
||||
out.push_str(&format!(
|
||||
"boot0 {} (never written)\n",
|
||||
if self.boot0_present {
|
||||
"present"
|
||||
} else {
|
||||
"not present"
|
||||
}
|
||||
));
|
||||
for check in &self.checks {
|
||||
out.push_str(&format!(
|
||||
"check {} {}: {}\n",
|
||||
check.name,
|
||||
if check.ok { "ok" } else { "FAIL" },
|
||||
check.detail
|
||||
));
|
||||
}
|
||||
out.push_str(if self.ok() {
|
||||
"result PASS\n"
|
||||
} else {
|
||||
"result FAIL (do not write)\n"
|
||||
});
|
||||
out
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PreflightOptions<'a> {
|
||||
pub host: &'a str,
|
||||
pub held_backup: Option<&'a Path>,
|
||||
pub check_golden: bool,
|
||||
pub allow_unknown_source: bool,
|
||||
}
|
||||
|
||||
pub fn preflight(
|
||||
plan: &Cv1835Plan,
|
||||
remote: &mut dyn Remote,
|
||||
options: &PreflightOptions<'_>,
|
||||
) -> Result<PreflightReport, PreflightError> {
|
||||
let mut checks = Vec::new();
|
||||
let fingerprint = remote.user_command(
|
||||
"uname -r; echo __MODEL__; cat /proc/device-tree/model 2>/dev/null; echo; \
|
||||
echo __RELEASE__; cat /etc/os-release 2>/dev/null; \
|
||||
ls /etc/bos_version /etc/fw-info /etc/alkane-release /etc/ouros-release 2>/dev/null; \
|
||||
test -x /nvdata/rroot && echo rroot; \
|
||||
test -x /usr/sbin/update-daemon && echo update-daemon",
|
||||
Duration::from_secs(20),
|
||||
)?;
|
||||
let text = String::from_utf8_lossy(&fingerprint).into_owned();
|
||||
let mut sections = text.split("__MODEL__");
|
||||
let kernel = sections.next().unwrap_or("").trim().to_owned();
|
||||
let rest = sections.next().unwrap_or("");
|
||||
let mut sections = rest.split("__RELEASE__");
|
||||
let model = sections.next().unwrap_or("").trim().to_owned();
|
||||
let release = sections.next().unwrap_or("");
|
||||
let source_os = classify(&kernel, release, &text);
|
||||
let source_ok = plan.source_os.is_empty()
|
||||
|| plan.source_os.iter().any(|item| item == &source_os)
|
||||
|| options.allow_unknown_source;
|
||||
checks.push(Check {
|
||||
name: "source-os",
|
||||
ok: source_ok,
|
||||
detail: format!(
|
||||
"{source_os} (allowed: {})",
|
||||
if plan.source_os.is_empty() {
|
||||
"any".to_owned()
|
||||
} else {
|
||||
plan.source_os.join(", ")
|
||||
}
|
||||
),
|
||||
});
|
||||
|
||||
let root_uid = String::from_utf8_lossy(&remote.root_command("id -u", Duration::from_secs(20))?)
|
||||
.trim()
|
||||
.to_owned();
|
||||
checks.push(Check {
|
||||
name: "root",
|
||||
ok: root_uid == "0",
|
||||
detail: format!("uid {root_uid}"),
|
||||
});
|
||||
|
||||
let partition_device = plan.partition.device.clone();
|
||||
let block = String::from_utf8_lossy(&remote.user_command(
|
||||
&format!("test -b {partition_device} && echo block || echo missing"),
|
||||
Duration::from_secs(15),
|
||||
)?)
|
||||
.trim()
|
||||
.to_owned();
|
||||
let size_out = remote.user_command(
|
||||
&format!("cat /sys/class/block/{P1_NAME}/size 2>/dev/null || true"),
|
||||
Duration::from_secs(15),
|
||||
)?;
|
||||
let sectors: u64 = String::from_utf8_lossy(&size_out)
|
||||
.trim()
|
||||
.parse()
|
||||
.unwrap_or(0);
|
||||
let partition_size = sectors.saturating_mul(512);
|
||||
checks.push(Check {
|
||||
name: "partition",
|
||||
ok: block == "block" && partition_size == plan.partition.size,
|
||||
detail: format!(
|
||||
"{partition_device} {partition_size} B (plan {} B)",
|
||||
plan.partition.size
|
||||
),
|
||||
});
|
||||
|
||||
let live = remote.root_command(
|
||||
&format!("md5sum {partition_device}"),
|
||||
Duration::from_secs(60),
|
||||
)?;
|
||||
let live_md5 = parse_digest(&live, 32).unwrap_or_else(|| "unreadable".to_owned());
|
||||
checks.push(Check {
|
||||
name: "live-p1-md5",
|
||||
ok: live_md5.len() == 32,
|
||||
detail: live_md5.clone(),
|
||||
});
|
||||
|
||||
let commands = remote.user_command(
|
||||
"for c in dd md5sum sync; do command -v $c >/dev/null 2>&1 && echo $c || echo missing:$c; done",
|
||||
Duration::from_secs(15),
|
||||
)?;
|
||||
let commands_text = String::from_utf8_lossy(&commands).into_owned();
|
||||
let missing: Vec<&str> = commands_text
|
||||
.lines()
|
||||
.filter_map(|line| line.strip_prefix("missing:"))
|
||||
.collect();
|
||||
checks.push(Check {
|
||||
name: "commands",
|
||||
ok: missing.is_empty(),
|
||||
detail: if missing.is_empty() {
|
||||
"dd, md5sum, sync present".to_owned()
|
||||
} else {
|
||||
format!("missing {}", missing.join(", "))
|
||||
},
|
||||
});
|
||||
|
||||
let free_bytes = free_bytes(remote)?;
|
||||
checks.push(Check {
|
||||
name: "free-space",
|
||||
ok: free_bytes >= plan.min_free_bytes,
|
||||
detail: format!(
|
||||
"{} MiB at /tmp (plan {} MiB)",
|
||||
free_bytes / (1024 * 1024),
|
||||
plan.min_free_bytes / (1024 * 1024)
|
||||
),
|
||||
});
|
||||
|
||||
let boot0 = String::from_utf8_lossy(&remote.user_command(
|
||||
"test -e /dev/mmcblk0boot0 && echo yes || echo no",
|
||||
Duration::from_secs(15),
|
||||
)?)
|
||||
.trim()
|
||||
.to_owned();
|
||||
let boot0_present = boot0 == "yes";
|
||||
checks.push(Check {
|
||||
name: "secure-regions",
|
||||
ok: !plan.partition.device.contains("boot0"),
|
||||
detail: format!(
|
||||
"boot0 {}; the write plan targets only {}",
|
||||
if boot0_present { "present" } else { "absent" },
|
||||
plan.partition.device
|
||||
),
|
||||
});
|
||||
|
||||
let mut held_backup = None;
|
||||
if let Some(path) = options.held_backup {
|
||||
let size = fs::metadata(path)?.len();
|
||||
if size != plan.partition.size {
|
||||
return Err(PreflightError::BackupSize {
|
||||
path: path.to_path_buf(),
|
||||
size,
|
||||
});
|
||||
}
|
||||
let data = fs::read(path)?;
|
||||
let md5 = md5_hex(&data);
|
||||
let sha256: String = Sha256::digest(&data)
|
||||
.iter()
|
||||
.map(|byte| format!("{byte:02x}"))
|
||||
.collect();
|
||||
let matches_live = md5 == live_md5;
|
||||
checks.push(Check {
|
||||
name: "held-backup",
|
||||
ok: true,
|
||||
detail: format!(
|
||||
"{} md5 {md5} ({})",
|
||||
path.display(),
|
||||
if matches_live {
|
||||
"matches live p1"
|
||||
} else {
|
||||
"differs from live p1; restore will still write it"
|
||||
}
|
||||
),
|
||||
});
|
||||
held_backup = Some(BackupCheck {
|
||||
path: path.to_path_buf(),
|
||||
md5,
|
||||
sha256,
|
||||
matches_live,
|
||||
});
|
||||
}
|
||||
|
||||
let mut golden_md5 = None;
|
||||
if options.check_golden {
|
||||
let out = remote.root_command(
|
||||
&format!(
|
||||
"dd if=/dev/mmcblk0 skip={} count=32768 bs=512 2>/dev/null | md5sum",
|
||||
plan.golden_lba_bytes / 512
|
||||
),
|
||||
Duration::from_secs(90),
|
||||
)?;
|
||||
if let Some(digest) = parse_digest(&out, 32) {
|
||||
golden_md5 = Some(digest);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(PreflightReport {
|
||||
host: options.host.to_owned(),
|
||||
source_os,
|
||||
kernel,
|
||||
model,
|
||||
root_method: remote.root_method().to_owned(),
|
||||
root_uid,
|
||||
partition_device,
|
||||
partition_size,
|
||||
live_md5,
|
||||
held_backup,
|
||||
golden_md5,
|
||||
free_bytes,
|
||||
boot0_present,
|
||||
checks,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn classify(kernel: &str, release: &str, all: &str) -> String {
|
||||
if all.contains("/etc/bos_version") || release.contains("Braiins") {
|
||||
return "braiins".to_owned();
|
||||
}
|
||||
if all.contains("/etc/alkane-release") || kernel.starts_with("6.18") {
|
||||
return "alkane".to_owned();
|
||||
}
|
||||
if all.contains("/etc/ouros-release") || kernel.contains("6.6.147") {
|
||||
return "ouros".to_owned();
|
||||
}
|
||||
if all.contains("/etc/fw-info") || release.contains("Vnish") || release.contains("AnthillOS") {
|
||||
return "vnish".to_owned();
|
||||
}
|
||||
// Stock Bitmain control boards: Amlogic stock is kernel 4.9.38; the AM335x
|
||||
// (BeagleBone-class) stock is kernel 3.8.13+ with an "Antminer" model line.
|
||||
if kernel.contains("4.9.38")
|
||||
|| kernel.starts_with("3.8.13")
|
||||
|| all.contains("update-daemon")
|
||||
|| all.contains("rroot")
|
||||
|| all.to_lowercase().contains("antminer")
|
||||
{
|
||||
return "stock-bitmain".to_owned();
|
||||
}
|
||||
"unknown".to_owned()
|
||||
}
|
||||
|
||||
pub fn parse_digest(output: &[u8], length: usize) -> Option<String> {
|
||||
let text = String::from_utf8_lossy(output);
|
||||
text.split_whitespace()
|
||||
.map(|token| token.trim().to_ascii_lowercase())
|
||||
.find(|token| token.len() == length && token.bytes().all(|byte| byte.is_ascii_hexdigit()))
|
||||
}
|
||||
|
||||
fn free_bytes(remote: &mut dyn Remote) -> Result<u64, RemoteError> {
|
||||
let out = remote.user_command("df -k /tmp 2>/dev/null || df -k /", Duration::from_secs(15))?;
|
||||
let text = String::from_utf8_lossy(&out);
|
||||
for line in text.lines().rev() {
|
||||
let fields: Vec<&str> = line.split_whitespace().collect();
|
||||
if fields.len() >= 4 {
|
||||
if let Ok(kib) = fields[3].parse::<u64>() {
|
||||
return Ok(kib.saturating_mul(1024));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn classifies_the_source_os() {
|
||||
assert_eq!(
|
||||
classify("4.9.38-tag-", "", "update-daemon\n"),
|
||||
"stock-bitmain"
|
||||
);
|
||||
assert_eq!(
|
||||
classify("4.9.38-tag-", "/etc/fw-info\n", "/etc/fw-info\n"),
|
||||
"vnish"
|
||||
);
|
||||
assert_eq!(classify("6.6.147", "/etc/ouros-release\n", ""), "ouros");
|
||||
assert_eq!(classify("6.18.48", "", ""), "alkane");
|
||||
assert_eq!(classify("4.9.38", "", "/etc/bos_version\n"), "braiins");
|
||||
assert_eq!(classify("5.10.0", "", ""), "unknown");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_md5_from_md5sum_output() {
|
||||
let output = b"3f43804f10ce256200bfc71e7fbdd9ee /dev/mmcblk0p1\n";
|
||||
assert_eq!(
|
||||
parse_digest(output, 32).as_deref(),
|
||||
Some("3f43804f10ce256200bfc71e7fbdd9ee")
|
||||
);
|
||||
assert_eq!(parse_digest(b"not a digest\n", 32), None);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,222 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
//! Stock <-> alkane crossing, compiled into the toolkit.
|
||||
//!
|
||||
//! The toolkit is self-contained: it installs alkane onto a board on stock and
|
||||
//! returns a board to stock entirely on its own, with no external helper and no
|
||||
//! runtime dependency on any other tool. A user's machine needs only this
|
||||
//! binary.
|
||||
//!
|
||||
//! Getting our first image onto a board past its stock safeguards (with root),
|
||||
//! and the reverse, are implemented in the sibling module `stock_method`. This
|
||||
//! file is the stable public boundary around that work: the interface
|
||||
//! (`StockMethod`, `StockError`) plus the toolkit-side orchestration and
|
||||
//! verification around the method. The method is plain Rust and must be
|
||||
//! network-only.
|
||||
|
||||
use alkane_artifacts::afw::VerifiedAfw;
|
||||
use std::path::{Path, PathBuf};
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum StockError {
|
||||
#[error("the stock method is not implemented in this build")]
|
||||
NotImplemented,
|
||||
#[error("could not reach the board: {0}")]
|
||||
Network(String),
|
||||
#[error("authentication failed: {0}")]
|
||||
Auth(String),
|
||||
#[error("the stock method failed: {0}")]
|
||||
Method(String),
|
||||
#[error("io: {0}")]
|
||||
Io(String),
|
||||
}
|
||||
|
||||
/// The genuine Bitmain stock kernel and rootfs to write back when restoring to
|
||||
/// stock, parsed from a vendor firmware package (OTA or SD) by `stock_firmware`.
|
||||
/// The matching NAND device tree is NOT taken from the package: Bitmain never
|
||||
/// ships the kernel device tree in its distributable firmware (it is factory-
|
||||
/// provisioned and preserved by every update), so the method supplies the
|
||||
/// correct per-board dtb itself. Borrowed so the large rootfs is not copied
|
||||
/// before the method needs it.
|
||||
pub struct RestoreImages<'a> {
|
||||
pub kernel: &'a [u8],
|
||||
pub rootfs: &'a [u8],
|
||||
}
|
||||
|
||||
/// Options for updating a board that already runs alkane, in place.
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct InPlaceOptions {
|
||||
/// Read-only: report the board state, the storage map and the write plan,
|
||||
/// and write nothing.
|
||||
pub dry_run: bool,
|
||||
/// Reboot after the images are written and read back, then wait for the
|
||||
/// board to return as alkane.
|
||||
pub reboot: bool,
|
||||
/// Where to keep a copy of each boot slot as it was before the write. When
|
||||
/// set, every slot is read and saved before any erase; a failed backup
|
||||
/// aborts the update with nothing written.
|
||||
pub backup_dir: Option<PathBuf>,
|
||||
}
|
||||
|
||||
/// One boot slot as it was before an in-place update.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct HeldSlot {
|
||||
pub name: String,
|
||||
/// Absolute NAND offset of the slot.
|
||||
pub absolute: u64,
|
||||
pub size: u64,
|
||||
pub path: PathBuf,
|
||||
pub md5: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct InPlaceReceipt {
|
||||
pub backups: Vec<HeldSlot>,
|
||||
pub rebooted: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum InPlaceOutcome {
|
||||
/// The dry-run report; nothing was written.
|
||||
DryRun(String),
|
||||
Updated(InPlaceReceipt),
|
||||
}
|
||||
|
||||
/// Result of a live check that a Zynq board is up on alkane: two channels
|
||||
/// (HTTP page marker, SSH kernel/OS) must agree.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct LiveCheck {
|
||||
pub attempts: u32,
|
||||
pub http: String,
|
||||
pub http_ok: bool,
|
||||
pub ssh: String,
|
||||
pub ssh_ok: bool,
|
||||
/// The running kernel command line (informational: on a NAND boot it is
|
||||
/// the board's U-Boot env bootargs).
|
||||
pub cmdline: String,
|
||||
}
|
||||
|
||||
impl LiveCheck {
|
||||
pub fn ok(&self) -> bool {
|
||||
self.http_ok && self.ssh_ok
|
||||
}
|
||||
|
||||
pub fn render(&self) -> String {
|
||||
format!(
|
||||
"verify-live attempts {}\n http {}: {}\n ssh {}: {}\n cmdline: {}\nresult {}\n",
|
||||
self.attempts,
|
||||
if self.http_ok { "PASS" } else { "FAIL" },
|
||||
self.http,
|
||||
if self.ssh_ok { "PASS" } else { "FAIL" },
|
||||
self.ssh,
|
||||
if self.cmdline.is_empty() {
|
||||
"(not read)"
|
||||
} else {
|
||||
&self.cmdline
|
||||
},
|
||||
if self.ok() { "PASS" } else { "FAIL" }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// The stock-side methods, implemented by `stock_method`. All implementations
|
||||
/// must be NETWORK-ONLY (no serial-console dependency).
|
||||
pub trait StockMethod {
|
||||
/// Put our first image (the `.afw`) onto a board currently on stock, past
|
||||
/// stock's gates and with root, over the network only. On Ok the board is
|
||||
/// installed with alkane; the caller verifies liveness.
|
||||
fn install_from_stock(
|
||||
&self,
|
||||
host: &str,
|
||||
afw_path: &Path,
|
||||
afw: &VerifiedAfw,
|
||||
password: Option<&str>,
|
||||
) -> Result<(), StockError>;
|
||||
|
||||
/// Return an aftermarket (alkane) board to genuine stock, over the network
|
||||
/// only, by writing the given genuine Bitmain stock images (`images`) back to
|
||||
/// NAND. `password` is the board's alkane root credential. On Ok the board is
|
||||
/// on stock; the caller verifies.
|
||||
fn restore_to_stock(
|
||||
&self,
|
||||
host: &str,
|
||||
images: &RestoreImages,
|
||||
password: Option<&str>,
|
||||
) -> Result<(), StockError>;
|
||||
|
||||
/// Update a board that already runs alkane to the `.afw`'s images, over the
|
||||
/// network only. Refuses a board that is not running alkane (a board on
|
||||
/// stock is installed with `install_from_stock`). The boot regions are not
|
||||
/// exposed to Linux on alkane, so they are re-registered the same way as for
|
||||
/// a restore; the images are written in the form the board's stock U-Boot
|
||||
/// boots, each read back before reboot, and BOOT.bin is never written.
|
||||
fn update_in_place(
|
||||
&self,
|
||||
host: &str,
|
||||
afw: &VerifiedAfw,
|
||||
password: Option<&str>,
|
||||
options: &InPlaceOptions,
|
||||
) -> Result<InPlaceOutcome, StockError>;
|
||||
}
|
||||
|
||||
/// The compiled-in method implementation.
|
||||
pub fn method() -> &'static dyn StockMethod {
|
||||
crate::stock_method::method()
|
||||
}
|
||||
|
||||
/// Install alkane onto a board on stock (network-only), via the compiled-in method.
|
||||
pub fn install_from_stock(
|
||||
host: &str,
|
||||
afw_path: &Path,
|
||||
afw: &VerifiedAfw,
|
||||
password: Option<&str>,
|
||||
) -> Result<(), StockError> {
|
||||
method().install_from_stock(host, afw_path, afw, password)
|
||||
}
|
||||
|
||||
/// Return an alkane board to stock (network-only) by writing the genuine Bitmain
|
||||
/// stock `images` back to NAND, via the compiled-in method.
|
||||
pub fn restore_to_stock(
|
||||
host: &str,
|
||||
images: &RestoreImages,
|
||||
password: Option<&str>,
|
||||
) -> Result<(), StockError> {
|
||||
method().restore_to_stock(host, images, password)
|
||||
}
|
||||
|
||||
/// Check, over the network, that a Zynq board is up on alkane: the web page
|
||||
/// carries `marker` (case-insensitive) and SSH reports alkane with a kernel
|
||||
/// containing `kernel_version` (if non-empty). Retries until `timeout`.
|
||||
pub fn verify_zynq_live(
|
||||
host: &str,
|
||||
password: Option<&str>,
|
||||
marker: &str,
|
||||
kernel_version: &str,
|
||||
timeout: std::time::Duration,
|
||||
) -> Result<LiveCheck, StockError> {
|
||||
crate::stock_method::live_check(host, password, marker, kernel_version, timeout)
|
||||
}
|
||||
|
||||
/// Update a board already on alkane to the `.afw`'s images, in place
|
||||
/// (network-only), via the compiled-in method.
|
||||
pub fn update_in_place(
|
||||
host: &str,
|
||||
afw: &VerifiedAfw,
|
||||
password: Option<&str>,
|
||||
options: &InPlaceOptions,
|
||||
) -> Result<InPlaceOutcome, StockError> {
|
||||
method().update_in_place(host, afw, password, options)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
// The interface is exercised against whatever `stock_method` provides. We
|
||||
// only assert the toolkit wires a method object in; its behaviour is the
|
||||
// method module's own tests.
|
||||
#[test]
|
||||
fn a_method_is_compiled_in() {
|
||||
let _m: &dyn StockMethod = method();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,282 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
//! Parse a genuine Bitmain Antminer stock firmware package into the flashable
|
||||
//! stock images, so the toolkit can restore a board to genuine stock using the
|
||||
//! vendor's own files (no carved blobs).
|
||||
//!
|
||||
//! Two genuine source shapes are handled for the Zynq-7010 (Antminer S17):
|
||||
//!
|
||||
//! - OTA update `.tar.gz` (`Antminer-S17-user-OM-<date>-sig*.tar.gz`): a signed
|
||||
//! outer tar holding `cert.pem`, `fw.tar.gz` and `runme.sh` (each with a
|
||||
//! `.sig`). The inner `fw.tar.gz` holds `BOOT.bin`, `uImage` (legacy kernel),
|
||||
//! `uramdisk.image.gz` (rootfs) and `md5_info` (md5 of the rootfs). The OTA
|
||||
//! package does NOT carry the device tree: the factory SD provisioning writes
|
||||
//! it once and OTA upgrades preserve it.
|
||||
//! - SD recovery `.zip` (`SD_Antminer-S17-*.zip`): the full factory image,
|
||||
//! which DOES include `devicetree.dtb` alongside `BOOT.bin`/`uImage`/
|
||||
//! `uramdisk.image.gz`. This is the complete source for a from-anywhere
|
||||
//! restore, because it carries the device tree the stock kernel needs.
|
||||
//!
|
||||
//! Extraction shells out to the system `tar`/`unzip`/`gzip` (as the release
|
||||
//! fetch shells out to `curl`); nothing is written outside a private temp dir.
|
||||
|
||||
use std::path::Path;
|
||||
use std::process::Command;
|
||||
|
||||
use crate::md5::md5_hex;
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum StockFirmwareError {
|
||||
#[error("io: {0}")]
|
||||
Io(String),
|
||||
#[error("could not read {0}")]
|
||||
Read(String),
|
||||
#[error("unrecognised firmware package (not a .tar.gz or .zip): {0}")]
|
||||
Format(String),
|
||||
#[error("extraction failed: {0}")]
|
||||
Extract(String),
|
||||
#[error("the firmware package is missing a stock {0} image")]
|
||||
MissingImage(&'static str),
|
||||
#[error("rootfs md5 mismatch: package lists {expected}, image is {actual}")]
|
||||
RootfsMd5 { expected: String, actual: String },
|
||||
#[error("needed system tool not found: {0}")]
|
||||
MissingTool(String),
|
||||
}
|
||||
|
||||
/// The stock images carved from a genuine Bitmain firmware package, ready to
|
||||
/// write back to NAND. `boot_bin` and `dtb` are optional (an OTA package omits
|
||||
/// the dtb; the bootloader is only written on an explicit full restore).
|
||||
pub struct StockFirmware {
|
||||
pub boot_bin: Option<Vec<u8>>,
|
||||
pub dtb: Option<Vec<u8>>,
|
||||
pub kernel: Vec<u8>,
|
||||
pub rootfs: Vec<u8>,
|
||||
pub version: Option<String>,
|
||||
/// The source package kind, for logging.
|
||||
pub source_kind: &'static str,
|
||||
}
|
||||
|
||||
impl StockFirmware {
|
||||
pub fn summary(&self) -> String {
|
||||
let v = self.version.as_deref().unwrap_or("unknown");
|
||||
format!(
|
||||
"{} (version {}): kernel {} B, rootfs {} B, dtb {}, BOOT.bin {}",
|
||||
self.source_kind,
|
||||
v,
|
||||
self.kernel.len(),
|
||||
self.rootfs.len(),
|
||||
self.dtb.as_ref().map_or("absent", |_| "present"),
|
||||
self.boot_bin.as_ref().map_or("absent", |_| "present"),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn have_tool(tool: &str) -> bool {
|
||||
Command::new(tool)
|
||||
.arg("--version")
|
||||
.output()
|
||||
.map(|o| o.status.success() || !o.stdout.is_empty() || !o.stderr.is_empty())
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
fn run(cmd: &mut Command, what: &str) -> Result<(), StockFirmwareError> {
|
||||
let out = cmd
|
||||
.output()
|
||||
.map_err(|e| StockFirmwareError::Extract(format!("{what}: {e}")))?;
|
||||
if !out.status.success() {
|
||||
return Err(StockFirmwareError::Extract(format!(
|
||||
"{what}: {}",
|
||||
String::from_utf8_lossy(&out.stderr).trim()
|
||||
)));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Recursively collect files under `dir` as (relative-path-lowercased, absolute path).
|
||||
fn collect_files(dir: &Path) -> Vec<(String, std::path::PathBuf)> {
|
||||
fn walk(base: &Path, dir: &Path, out: &mut Vec<(String, std::path::PathBuf)>) {
|
||||
let Ok(entries) = std::fs::read_dir(dir) else {
|
||||
return;
|
||||
};
|
||||
for entry in entries.flatten() {
|
||||
let path = entry.path();
|
||||
if path.is_dir() {
|
||||
walk(base, &path, out);
|
||||
} else if let Ok(rel) = path.strip_prefix(base) {
|
||||
out.push((rel.to_string_lossy().to_lowercase(), path.clone()));
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut out = Vec::new();
|
||||
walk(dir, dir, &mut out);
|
||||
out
|
||||
}
|
||||
|
||||
/// Pick the best file whose basename matches `name`, preferring paths that
|
||||
/// contain one of `prefer` path fragments (e.g. "bin/" for the SD image, "fw"
|
||||
/// for the OTA inner tree), then the shallowest path.
|
||||
fn pick<'a>(
|
||||
files: &'a [(String, std::path::PathBuf)],
|
||||
name: &str,
|
||||
prefer: &[&str],
|
||||
) -> Option<&'a std::path::PathBuf> {
|
||||
let name = name.to_lowercase();
|
||||
let mut matches: Vec<&(String, std::path::PathBuf)> = files
|
||||
.iter()
|
||||
.filter(|(rel, _)| rel.rsplit('/').next() == Some(name.as_str()) || *rel == name)
|
||||
.collect();
|
||||
if matches.is_empty() {
|
||||
return None;
|
||||
}
|
||||
matches.sort_by_key(|(rel, _)| {
|
||||
let pref_rank = prefer
|
||||
.iter()
|
||||
.position(|p| rel.contains(p))
|
||||
.unwrap_or(prefer.len());
|
||||
let depth = rel.matches('/').count();
|
||||
(pref_rank, depth)
|
||||
});
|
||||
matches.first().map(|(_, p)| p)
|
||||
}
|
||||
|
||||
fn read(path: &Path) -> Result<Vec<u8>, StockFirmwareError> {
|
||||
std::fs::read(path).map_err(|_| StockFirmwareError::Read(path.display().to_string()))
|
||||
}
|
||||
|
||||
/// Parse a genuine Bitmain stock firmware package (OTA `.tar.gz` or SD `.zip`)
|
||||
/// into its stock images.
|
||||
pub fn parse(path: &Path) -> Result<StockFirmware, StockFirmwareError> {
|
||||
let bytes = std::fs::read(path)
|
||||
.map_err(|e| StockFirmwareError::Io(format!("{}: {e}", path.display())))?;
|
||||
if bytes.len() < 4 {
|
||||
return Err(StockFirmwareError::Format(path.display().to_string()));
|
||||
}
|
||||
let is_gzip = bytes[0] == 0x1f && bytes[1] == 0x8b;
|
||||
let is_zip = &bytes[0..2] == b"PK";
|
||||
if !is_gzip && !is_zip {
|
||||
return Err(StockFirmwareError::Format(path.display().to_string()));
|
||||
}
|
||||
|
||||
let tmp = tempfile::Builder::new()
|
||||
.prefix("alkane-stockfw-")
|
||||
.tempdir()
|
||||
.map_err(|e| StockFirmwareError::Io(e.to_string()))?;
|
||||
let root = tmp.path();
|
||||
|
||||
let (source_kind, prefer): (&'static str, &[&str]) = if is_zip {
|
||||
if !have_tool("unzip") {
|
||||
return Err(StockFirmwareError::MissingTool("unzip".into()));
|
||||
}
|
||||
run(
|
||||
Command::new("unzip")
|
||||
.arg("-qq")
|
||||
.arg("-o")
|
||||
.arg(path)
|
||||
.arg("-d")
|
||||
.arg(root),
|
||||
"unzip SD recovery image",
|
||||
)?;
|
||||
("Bitmain SD recovery image", &["bin/"])
|
||||
} else {
|
||||
if !have_tool("tar") {
|
||||
return Err(StockFirmwareError::MissingTool("tar".into()));
|
||||
}
|
||||
run(
|
||||
Command::new("tar").arg("xzf").arg(path).arg("-C").arg(root),
|
||||
"untar OTA package",
|
||||
)?;
|
||||
// The signed OTA wraps the real images in an inner fw.tar.gz; extract it.
|
||||
let inner = collect_files(root)
|
||||
.into_iter()
|
||||
.find(|(rel, _)| rel.rsplit('/').next() == Some("fw.tar.gz"));
|
||||
if let Some((_, inner_path)) = inner {
|
||||
let inner_dir = root.join("fw");
|
||||
std::fs::create_dir_all(&inner_dir)
|
||||
.map_err(|e| StockFirmwareError::Io(e.to_string()))?;
|
||||
run(
|
||||
Command::new("tar")
|
||||
.arg("xzf")
|
||||
.arg(&inner_path)
|
||||
.arg("-C")
|
||||
.arg(&inner_dir),
|
||||
"untar OTA inner fw.tar.gz",
|
||||
)?;
|
||||
}
|
||||
("Bitmain OTA update package", &["fw/", "bin/"])
|
||||
};
|
||||
|
||||
let files = collect_files(root);
|
||||
|
||||
let kernel_path = pick(&files, "uImage", prefer)
|
||||
.ok_or(StockFirmwareError::MissingImage("uImage (kernel)"))?;
|
||||
let rootfs_path = pick(&files, "uramdisk.image.gz", prefer).ok_or(
|
||||
StockFirmwareError::MissingImage("uramdisk.image.gz (rootfs)"),
|
||||
)?;
|
||||
let kernel = read(kernel_path)?;
|
||||
let rootfs = read(rootfs_path)?;
|
||||
let boot_bin = pick(&files, "BOOT.bin", prefer)
|
||||
.map(|p| read(p))
|
||||
.transpose()?;
|
||||
let dtb = pick(&files, "devicetree.dtb", &["", "bin/"])
|
||||
.map(|p| read(p))
|
||||
.transpose()?;
|
||||
|
||||
// Verify the rootfs against md5_info when the package ships one (OTA does).
|
||||
if let Some(md5_path) = pick(&files, "md5_info", prefer) {
|
||||
if let Ok(raw) = std::fs::read_to_string(md5_path) {
|
||||
let expected = raw.split_whitespace().next().unwrap_or("").to_lowercase();
|
||||
if expected.len() == 32 {
|
||||
let actual = md5_hex(&rootfs);
|
||||
if actual != expected {
|
||||
return Err(StockFirmwareError::RootfsMd5 { expected, actual });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let version = pick(&files, "version", prefer)
|
||||
.or_else(|| pick(&files, "version_number", &[""]))
|
||||
.and_then(|p| std::fs::read_to_string(p).ok())
|
||||
.map(|s| s.trim().to_string())
|
||||
.filter(|s| !s.is_empty());
|
||||
|
||||
Ok(StockFirmware {
|
||||
boot_bin,
|
||||
dtb,
|
||||
kernel,
|
||||
rootfs,
|
||||
version,
|
||||
source_kind,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn rejects_non_archive() {
|
||||
let tmp = tempfile::NamedTempFile::new().unwrap();
|
||||
std::fs::write(tmp.path(), b"not an archive").unwrap();
|
||||
assert!(matches!(
|
||||
parse(tmp.path()),
|
||||
Err(StockFirmwareError::Format(_))
|
||||
));
|
||||
}
|
||||
|
||||
// Parse a real Bitmain firmware package. Set ALKANE_TEST_STOCK_FW to its path.
|
||||
// Ignored by default (needs the vendor file, not in the repo).
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn parses_real_package() {
|
||||
let Ok(path) = std::env::var("ALKANE_TEST_STOCK_FW") else {
|
||||
eprintln!("set ALKANE_TEST_STOCK_FW to a firmware file");
|
||||
return;
|
||||
};
|
||||
let fw = parse(Path::new(&path)).expect("parse");
|
||||
eprintln!("{}", fw.summary());
|
||||
assert!(!fw.kernel.is_empty(), "kernel present");
|
||||
assert!(!fw.rootfs.is_empty(), "rootfs present");
|
||||
// The kernel is a legacy u-boot uImage: magic 0x27051956 big-endian.
|
||||
assert_eq!(&fw.kernel[0..4], &[0x27, 0x05, 0x19, 0x56], "uImage magic");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,699 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
//! Network transport for the CV1835 installer.
|
||||
//!
|
||||
//! Two root channels are implemented, both over the network:
|
||||
//!
|
||||
//! * `direct`: the SSH login is already uid 0 (Braiins OS, ourOS/alkane with a
|
||||
//! root key, or an operator-supplied root login).
|
||||
//! * `update-daemon`: stock Bitmain (and Vnish, which runs on the stock
|
||||
//! kernel) exposes an SSH login as `miner` (uid 1001). `/usr/sbin/update-daemon`
|
||||
//! listens on 127.0.0.1:12887 and pipes its socket into `system()` as root.
|
||||
//! We reach it through an SSH local forward, exactly as documented in the RE
|
||||
//! notes (`cv-kvm-host` / `cvbuild/cvroot.py`,
|
||||
//! `alkane-sdk-wiki/40-boot-os-and-build/cross-install-matrix.md`). The
|
||||
//! channel is blind, so root output is redirected to a world-readable file
|
||||
//! and read back over the unprivileged SSH session.
|
||||
//! * `rroot`: an existing `/nvdata/rroot` setuid helper on CV stock/Vnish,
|
||||
//! documented in `60-reverse-engineering-methods/rroot-setuid-escalation.md`.
|
||||
//! It is a fallback because the helper is not durable across a p7 reformat.
|
||||
//!
|
||||
//! Credentials are never read here: a password is used only through
|
||||
//! `sshpass -f <path>`, and that path comes from the operator (environment
|
||||
//! variable materialized by the CLI, or a file named on the command line).
|
||||
|
||||
use std::fs;
|
||||
use std::io::{Read, Write};
|
||||
use std::net::{SocketAddr, TcpStream};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::{Child, Command, ExitStatus, Stdio};
|
||||
use std::thread;
|
||||
use std::time::{Duration, Instant};
|
||||
use thiserror::Error;
|
||||
|
||||
const MAX_CAPTURE: usize = 64 * 1024 * 1024;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum RemoteError {
|
||||
#[error("remote I/O: {0}")]
|
||||
Io(#[from] std::io::Error),
|
||||
#[error("cannot start {0}")]
|
||||
MissingTool(&'static str),
|
||||
#[error("invalid target: {0}")]
|
||||
InvalidTarget(String),
|
||||
#[error("SSH transport: {0}")]
|
||||
Ssh(String),
|
||||
#[error("SSH command timed out after {0:?}")]
|
||||
Timeout(Duration),
|
||||
#[error("remote command failed ({status}): {stderr}")]
|
||||
Command { status: String, stderr: String },
|
||||
#[error("root access was not obtained: {0}")]
|
||||
NotRoot(String),
|
||||
#[error("no network root channel is available on this board")]
|
||||
NoRootMethod,
|
||||
#[error("update-daemon: {0}")]
|
||||
Daemon(String),
|
||||
#[error("remote output is over the {0}-byte cap")]
|
||||
OutputTooLarge(usize),
|
||||
}
|
||||
|
||||
impl RemoteError {
|
||||
fn sanitize(text: &str) -> String {
|
||||
let text = text.trim();
|
||||
let mut shortened: String = text.chars().take(300).collect();
|
||||
if shortened.len() < text.len() {
|
||||
shortened.push_str("...");
|
||||
}
|
||||
shortened
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum RootMethod {
|
||||
Auto,
|
||||
Direct,
|
||||
UpdateDaemon,
|
||||
Rroot,
|
||||
}
|
||||
|
||||
impl RootMethod {
|
||||
pub fn parse(value: &str) -> Option<Self> {
|
||||
match value {
|
||||
"auto" => Some(Self::Auto),
|
||||
"direct" => Some(Self::Direct),
|
||||
"update-daemon" => Some(Self::UpdateDaemon),
|
||||
"rroot" => Some(Self::Rroot),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct SshOptions {
|
||||
pub host: String,
|
||||
pub port: u16,
|
||||
pub user: String,
|
||||
pub identity_file: Option<PathBuf>,
|
||||
pub password_file: Option<PathBuf>,
|
||||
pub known_hosts: Option<PathBuf>,
|
||||
pub root_method: RootMethod,
|
||||
pub update_daemon_port: Option<u16>,
|
||||
}
|
||||
|
||||
impl Default for SshOptions {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
host: String::new(),
|
||||
port: 22,
|
||||
user: "miner".to_owned(),
|
||||
identity_file: None,
|
||||
password_file: None,
|
||||
known_hosts: None,
|
||||
root_method: RootMethod::Auto,
|
||||
update_daemon_port: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl SshOptions {
|
||||
fn validate(&self) -> Result<(), RemoteError> {
|
||||
if self.host.is_empty()
|
||||
|| self.host.len() > 253
|
||||
|| self.host.starts_with('-')
|
||||
|| !self.host.bytes().all(|byte| {
|
||||
byte.is_ascii_alphanumeric() || matches!(byte, b'.' | b'-' | b':' | b'_')
|
||||
})
|
||||
{
|
||||
return Err(RemoteError::InvalidTarget("host".to_owned()));
|
||||
}
|
||||
if self.user.is_empty()
|
||||
|| !self
|
||||
.user
|
||||
.bytes()
|
||||
.all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'-' | b'.'))
|
||||
{
|
||||
return Err(RemoteError::InvalidTarget("user".to_owned()));
|
||||
}
|
||||
if self.port == 0 {
|
||||
return Err(RemoteError::InvalidTarget("port".to_owned()));
|
||||
}
|
||||
if let Some(path) = &self.password_file {
|
||||
if !path.is_file() {
|
||||
return Err(RemoteError::InvalidTarget(format!(
|
||||
"password file not found: {}",
|
||||
path.display()
|
||||
)));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
enum Strategy {
|
||||
Direct,
|
||||
Rroot,
|
||||
UpdateDaemon {
|
||||
local_port: u16,
|
||||
remote_port: u16,
|
||||
tunnel: Child,
|
||||
},
|
||||
}
|
||||
|
||||
impl Drop for Strategy {
|
||||
fn drop(&mut self) {
|
||||
if let Self::UpdateDaemon { tunnel, .. } = self {
|
||||
let _ = tunnel.kill();
|
||||
let _ = tunnel.wait();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SshTransport {
|
||||
options: SshOptions,
|
||||
known_hosts: PathBuf,
|
||||
strategy: Strategy,
|
||||
counter: u64,
|
||||
}
|
||||
|
||||
impl SshTransport {
|
||||
/// Open a session and (when `require_root`) prove a uid-0 channel.
|
||||
pub fn connect(options: SshOptions, require_root: bool) -> Result<Self, RemoteError> {
|
||||
options.validate()?;
|
||||
let known_hosts = options
|
||||
.known_hosts
|
||||
.clone()
|
||||
.unwrap_or_else(|| alkane_core::platform::config_path("known_hosts"));
|
||||
if let Some(parent) = known_hosts.parent() {
|
||||
fs::create_dir_all(parent)?;
|
||||
}
|
||||
if !known_hosts.exists() {
|
||||
fs::write(&known_hosts, b"")?;
|
||||
}
|
||||
if options.password_file.is_some() {
|
||||
Command::new("sshpass")
|
||||
.arg("-V")
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.status()
|
||||
.map_err(|_| RemoteError::MissingTool("sshpass"))?;
|
||||
}
|
||||
let mut transport = Self {
|
||||
options,
|
||||
known_hosts,
|
||||
strategy: Strategy::Direct,
|
||||
counter: 0,
|
||||
};
|
||||
let identity = transport.user_command("uname -r", Duration::from_secs(15))?;
|
||||
let _ = identity;
|
||||
if !require_root {
|
||||
return Ok(transport);
|
||||
}
|
||||
|
||||
let uid =
|
||||
String::from_utf8_lossy(&transport.user_command("id -u", Duration::from_secs(15))?)
|
||||
.trim()
|
||||
.to_owned();
|
||||
let direct = uid == "0";
|
||||
let method = match transport.options.root_method {
|
||||
RootMethod::Auto if direct => RootMethod::Direct,
|
||||
RootMethod::Auto => {
|
||||
if transport.has_update_daemon()? {
|
||||
RootMethod::UpdateDaemon
|
||||
} else if transport.has_rroot()? {
|
||||
RootMethod::Rroot
|
||||
} else {
|
||||
return Err(RemoteError::NoRootMethod);
|
||||
}
|
||||
}
|
||||
other => other,
|
||||
};
|
||||
match method {
|
||||
RootMethod::Direct => {
|
||||
if !direct {
|
||||
return Err(RemoteError::NotRoot(
|
||||
"the SSH login is not uid 0; use --root-method auto, update-daemon or rroot"
|
||||
.to_owned(),
|
||||
));
|
||||
}
|
||||
transport.strategy = Strategy::Direct;
|
||||
}
|
||||
RootMethod::Rroot => {
|
||||
if !transport.has_rroot()? {
|
||||
return Err(RemoteError::NotRoot(
|
||||
"/nvdata/rroot is not present or not executable".to_owned(),
|
||||
));
|
||||
}
|
||||
transport.strategy = Strategy::Rroot;
|
||||
}
|
||||
RootMethod::UpdateDaemon => {
|
||||
if !transport.has_update_daemon()? {
|
||||
return Err(RemoteError::NotRoot(
|
||||
"/usr/sbin/update-daemon is not present".to_owned(),
|
||||
));
|
||||
}
|
||||
let remote_port = transport
|
||||
.options
|
||||
.update_daemon_port
|
||||
.unwrap_or(transport.detect_update_daemon_port()?);
|
||||
let (local_port, tunnel) = transport.start_tunnel(remote_port)?;
|
||||
transport.strategy = Strategy::UpdateDaemon {
|
||||
local_port,
|
||||
remote_port,
|
||||
tunnel,
|
||||
};
|
||||
}
|
||||
RootMethod::Auto => unreachable!(),
|
||||
}
|
||||
|
||||
let root_output = transport.root_command("id", Duration::from_secs(20))?;
|
||||
let root = String::from_utf8_lossy(&root_output);
|
||||
if !root.contains("uid=0") {
|
||||
return Err(RemoteError::NotRoot(
|
||||
"the selected channel did not return uid=0".to_owned(),
|
||||
));
|
||||
}
|
||||
Ok(transport)
|
||||
}
|
||||
|
||||
pub fn channel(&self) -> String {
|
||||
format!("ssh:{}@{}", self.options.user, self.options.host)
|
||||
}
|
||||
|
||||
pub fn root_method(&self) -> &'static str {
|
||||
match &self.strategy {
|
||||
Strategy::Direct => "direct",
|
||||
Strategy::Rroot => "rroot",
|
||||
Strategy::UpdateDaemon { .. } => "update-daemon",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_daemon_port(&self) -> Option<u16> {
|
||||
match &self.strategy {
|
||||
Strategy::UpdateDaemon { remote_port, .. } => Some(*remote_port),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn ssh_base(&self) -> Command {
|
||||
let mut command = if let Some(password) = &self.options.password_file {
|
||||
let mut command = Command::new("sshpass");
|
||||
command.arg("-f").arg(password).arg("ssh");
|
||||
command
|
||||
} else {
|
||||
Command::new("ssh")
|
||||
};
|
||||
command
|
||||
.arg("-F")
|
||||
.arg("none")
|
||||
.arg("-o")
|
||||
.arg(if self.options.password_file.is_some() {
|
||||
"BatchMode=no"
|
||||
} else {
|
||||
"BatchMode=yes"
|
||||
})
|
||||
.arg("-o")
|
||||
.arg("StrictHostKeyChecking=accept-new")
|
||||
.arg("-o")
|
||||
.arg(format!("UserKnownHostsFile={}", self.known_hosts.display()))
|
||||
.arg("-o")
|
||||
.arg("GlobalKnownHostsFile=/dev/null")
|
||||
.arg("-o")
|
||||
.arg("ConnectTimeout=10")
|
||||
.arg("-o")
|
||||
.arg("ServerAliveInterval=5")
|
||||
.arg("-o")
|
||||
.arg("ServerAliveCountMax=3")
|
||||
.arg("-o")
|
||||
.arg("RequestTTY=no")
|
||||
.arg("-o")
|
||||
.arg("LogLevel=ERROR")
|
||||
.arg("-o")
|
||||
.arg("NumberOfPasswordPrompts=1")
|
||||
.arg("-o")
|
||||
.arg("PreferredAuthentications=publickey,password,keyboard-interactive");
|
||||
if let Some(identity) = &self.options.identity_file {
|
||||
command
|
||||
.arg("-i")
|
||||
.arg(identity)
|
||||
.arg("-o")
|
||||
.arg("IdentitiesOnly=yes");
|
||||
}
|
||||
command
|
||||
}
|
||||
|
||||
fn ssh_command(&self, command: &str) -> Command {
|
||||
let mut child = self.ssh_base();
|
||||
child
|
||||
.arg("-p")
|
||||
.arg(self.options.port.to_string())
|
||||
.arg(format!("{}@{}", self.options.user, self.options.host))
|
||||
.arg(command);
|
||||
child
|
||||
}
|
||||
|
||||
fn run_user(&self, command: &str, timeout: Duration) -> Result<Vec<u8>, RemoteError> {
|
||||
let (stdout, stderr, status) = run_bounded(self.ssh_command(command), timeout)?;
|
||||
if !status.success() {
|
||||
return Err(RemoteError::Command {
|
||||
status: status.to_string(),
|
||||
stderr: RemoteError::sanitize(&String::from_utf8_lossy(&stderr)),
|
||||
});
|
||||
}
|
||||
Ok(stdout)
|
||||
}
|
||||
|
||||
fn has_rroot(&self) -> Result<bool, RemoteError> {
|
||||
let out = self.run_user(
|
||||
"test -x /nvdata/rroot && echo yes || echo no",
|
||||
Duration::from_secs(15),
|
||||
)?;
|
||||
Ok(String::from_utf8_lossy(&out).contains("yes"))
|
||||
}
|
||||
|
||||
fn has_update_daemon(&self) -> Result<bool, RemoteError> {
|
||||
let out = self.run_user(
|
||||
"test -x /usr/sbin/update-daemon && echo yes || echo no",
|
||||
Duration::from_secs(15),
|
||||
)?;
|
||||
Ok(String::from_utf8_lossy(&out).contains("yes"))
|
||||
}
|
||||
|
||||
fn detect_update_daemon_port(&self) -> Result<u16, RemoteError> {
|
||||
let out = self.run_user(
|
||||
"cat /proc/net/tcp /proc/net/tcp6 2>/dev/null || true",
|
||||
Duration::from_secs(15),
|
||||
)?;
|
||||
let text = String::from_utf8_lossy(&out);
|
||||
for line in text.lines() {
|
||||
let fields: Vec<&str> = line.split_whitespace().collect();
|
||||
if fields.len() < 4 || fields[3] != "0A" {
|
||||
continue;
|
||||
}
|
||||
let local = fields[1];
|
||||
let port = local.rsplit(':').next().unwrap_or("");
|
||||
let port = u16::from_str_radix(port, 16).unwrap_or(0);
|
||||
if port == 12887 || port == 22026 {
|
||||
return Ok(port);
|
||||
}
|
||||
}
|
||||
// The CV1835 stock build listens on 12887. The fallback keeps the
|
||||
// explicit --update-daemon-port option available for other builds.
|
||||
Ok(12887)
|
||||
}
|
||||
|
||||
fn start_tunnel(&self, remote_port: u16) -> Result<(u16, Child), RemoteError> {
|
||||
let listener = std::net::TcpListener::bind(("127.0.0.1", 0))?;
|
||||
let local_port = listener.local_addr()?.port();
|
||||
drop(listener);
|
||||
let mut command = self.ssh_base();
|
||||
command
|
||||
.arg("-N")
|
||||
.arg("-o")
|
||||
.arg("ExitOnForwardFailure=yes")
|
||||
.arg("-L")
|
||||
.arg(format!("127.0.0.1:{local_port}:127.0.0.1:{remote_port}"))
|
||||
.arg("-p")
|
||||
.arg(self.options.port.to_string())
|
||||
.arg(format!("{}@{}", self.options.user, self.options.host))
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null());
|
||||
let mut tunnel = command.spawn()?;
|
||||
let deadline = Instant::now() + Duration::from_secs(20);
|
||||
loop {
|
||||
if TcpStream::connect_timeout(
|
||||
&SocketAddr::from(([127, 0, 0, 1], local_port)),
|
||||
Duration::from_millis(500),
|
||||
)
|
||||
.is_ok()
|
||||
{
|
||||
return Ok((local_port, tunnel));
|
||||
}
|
||||
if let Some(status) = tunnel.try_wait()? {
|
||||
return Err(RemoteError::Daemon(format!(
|
||||
"SSH forward exited before it was ready ({status})"
|
||||
)));
|
||||
}
|
||||
if Instant::now() >= deadline {
|
||||
let _ = tunnel.kill();
|
||||
return Err(RemoteError::Daemon(
|
||||
"SSH forward did not become ready".to_owned(),
|
||||
));
|
||||
}
|
||||
thread::sleep(Duration::from_millis(250));
|
||||
}
|
||||
}
|
||||
|
||||
fn daemon_send(&self, command: &str) -> Result<(), RemoteError> {
|
||||
let Strategy::UpdateDaemon { local_port, .. } = &self.strategy else {
|
||||
return Err(RemoteError::Daemon("not using update-daemon".to_owned()));
|
||||
};
|
||||
let local_port = *local_port;
|
||||
if command.contains('\n') || command.contains('\r') {
|
||||
return Err(RemoteError::Daemon("command contains a newline".to_owned()));
|
||||
}
|
||||
let mut stream = TcpStream::connect_timeout(
|
||||
&SocketAddr::from(([127, 0, 0, 1], local_port)),
|
||||
Duration::from_secs(10),
|
||||
)?;
|
||||
stream.set_write_timeout(Some(Duration::from_secs(10)))?;
|
||||
stream.write_all(format!(";{command};").as_bytes())?;
|
||||
stream.flush()?;
|
||||
let _ = stream.set_read_timeout(Some(Duration::from_millis(500)));
|
||||
let mut sink = [0_u8; 256];
|
||||
let _ = stream.read(&mut sink);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn daemon_capture(&mut self, command: &str, timeout: Duration) -> Result<Vec<u8>, RemoteError> {
|
||||
let path = format!("/tmp/.alkane-root-{}-{}", std::process::id(), self.counter);
|
||||
self.counter += 1;
|
||||
let wrapped = format!("{{ {command} ; }} > {path} 2>&1; chmod 644 {path}");
|
||||
self.daemon_send(&wrapped)?;
|
||||
let deadline = Instant::now() + timeout;
|
||||
loop {
|
||||
let probe = format!("test -f {path} && echo ready || true");
|
||||
let out = self.run_user(&probe, Duration::from_secs(15))?;
|
||||
if String::from_utf8_lossy(&out).contains("ready") {
|
||||
break;
|
||||
}
|
||||
if Instant::now() >= deadline {
|
||||
return Err(RemoteError::Daemon(format!(
|
||||
"root command produced no output file within {timeout:?}"
|
||||
)));
|
||||
}
|
||||
thread::sleep(Duration::from_millis(200));
|
||||
}
|
||||
let output = self.run_user(&format!("cat {path}"), Duration::from_secs(60))?;
|
||||
let _ = self.run_user(&format!("rm -f {path}"), Duration::from_secs(15));
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
fn daemon_reboot(&mut self) -> Result<(), RemoteError> {
|
||||
self.daemon_send("sync; echo b > /proc/sysrq-trigger")
|
||||
}
|
||||
}
|
||||
|
||||
impl Remote for SshTransport {
|
||||
fn channel(&self) -> &str {
|
||||
"ssh"
|
||||
}
|
||||
|
||||
fn root_method(&self) -> &'static str {
|
||||
SshTransport::root_method(self)
|
||||
}
|
||||
|
||||
fn user_command(&mut self, command: &str, timeout: Duration) -> Result<Vec<u8>, RemoteError> {
|
||||
self.run_user(command, timeout)
|
||||
}
|
||||
|
||||
fn root_command(&mut self, command: &str, timeout: Duration) -> Result<Vec<u8>, RemoteError> {
|
||||
match self.root_method() {
|
||||
"direct" => self.run_user(command, timeout),
|
||||
"rroot" => {
|
||||
let wrapped = format!("/nvdata/rroot -c {}", shell_quote(command));
|
||||
self.run_user(&wrapped, timeout)
|
||||
}
|
||||
"update-daemon" => self.daemon_capture(command, timeout),
|
||||
_ => Err(RemoteError::NoRootMethod),
|
||||
}
|
||||
}
|
||||
|
||||
fn push(&mut self, local: &Path, remote: &str, timeout: Duration) -> Result<(), RemoteError> {
|
||||
let remote = safe_remote_path(remote)?;
|
||||
let mut command = self.ssh_command(&format!("cat > {remote}"));
|
||||
command
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped());
|
||||
let mut child = command.spawn()?;
|
||||
let mut stdin = child.stdin.take().expect("piped stdin");
|
||||
let path = local.to_path_buf();
|
||||
let writer = thread::spawn(move || -> std::io::Result<()> {
|
||||
let mut source = fs::File::open(path)?;
|
||||
let mut buffer = [0_u8; 64 * 1024];
|
||||
loop {
|
||||
let read = source.read(&mut buffer)?;
|
||||
if read == 0 {
|
||||
break;
|
||||
}
|
||||
stdin.write_all(&buffer[..read])?;
|
||||
}
|
||||
stdin.flush()
|
||||
});
|
||||
let (stdout, stderr) = read_pipes(&mut child);
|
||||
let status = wait_timeout(&mut child, timeout)?;
|
||||
let _ = writer.join();
|
||||
let _ = stdout.join();
|
||||
let stderr = stderr.join().unwrap_or_default();
|
||||
if !status.success() {
|
||||
return Err(RemoteError::Command {
|
||||
status: status.to_string(),
|
||||
stderr: RemoteError::sanitize(&String::from_utf8_lossy(&stderr)),
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn pull(&mut self, remote: &str, local: &Path, timeout: Duration) -> Result<(), RemoteError> {
|
||||
let remote = safe_remote_path(remote)?;
|
||||
let data = self.run_user(&format!("cat {remote}"), timeout)?;
|
||||
if data.len() > MAX_CAPTURE {
|
||||
return Err(RemoteError::OutputTooLarge(MAX_CAPTURE));
|
||||
}
|
||||
fs::write(local, data)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn reboot(&mut self) -> Result<(), RemoteError> {
|
||||
match self.root_method() {
|
||||
"update-daemon" => self.daemon_reboot(),
|
||||
_ => {
|
||||
// A successful sysrq reset drops the SSH connection mid-command.
|
||||
let _ = self.run_user(
|
||||
"sync; echo b > /proc/sysrq-trigger",
|
||||
Duration::from_secs(15),
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Remote {
|
||||
fn channel(&self) -> &str;
|
||||
fn root_method(&self) -> &'static str {
|
||||
"network"
|
||||
}
|
||||
fn user_command(&mut self, command: &str, timeout: Duration) -> Result<Vec<u8>, RemoteError>;
|
||||
fn root_command(&mut self, command: &str, timeout: Duration) -> Result<Vec<u8>, RemoteError>;
|
||||
fn push(&mut self, local: &Path, remote: &str, timeout: Duration) -> Result<(), RemoteError>;
|
||||
fn pull(&mut self, remote: &str, local: &Path, timeout: Duration) -> Result<(), RemoteError>;
|
||||
fn reboot(&mut self) -> Result<(), RemoteError>;
|
||||
}
|
||||
|
||||
pub fn shell_quote(text: &str) -> String {
|
||||
format!("'{}'", text.replace('\'', "'\\''"))
|
||||
}
|
||||
|
||||
pub fn safe_remote_path(path: &str) -> Result<&str, RemoteError> {
|
||||
if !path.starts_with('/')
|
||||
|| path.len() > 200
|
||||
|| path.contains("..")
|
||||
|| !path
|
||||
.bytes()
|
||||
.all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'/' | b'.' | b'_' | b'-'))
|
||||
{
|
||||
return Err(RemoteError::InvalidTarget(format!(
|
||||
"unsafe remote path: {path}"
|
||||
)));
|
||||
}
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
fn read_pipes(child: &mut Child) -> (thread::JoinHandle<Vec<u8>>, thread::JoinHandle<Vec<u8>>) {
|
||||
let mut stdout = child.stdout.take().expect("piped stdout");
|
||||
let mut stderr = child.stderr.take().expect("piped stderr");
|
||||
let out = thread::spawn(move || {
|
||||
let mut data = Vec::new();
|
||||
let _ = stdout.read_to_end(&mut data);
|
||||
data
|
||||
});
|
||||
let err = thread::spawn(move || {
|
||||
let mut data = Vec::new();
|
||||
let _ = stderr.read_to_end(&mut data);
|
||||
data
|
||||
});
|
||||
(out, err)
|
||||
}
|
||||
|
||||
fn wait_timeout(child: &mut Child, timeout: Duration) -> Result<ExitStatus, RemoteError> {
|
||||
let deadline = Instant::now() + timeout;
|
||||
loop {
|
||||
if let Some(status) = child.try_wait()? {
|
||||
return Ok(status);
|
||||
}
|
||||
if Instant::now() >= deadline {
|
||||
let _ = child.kill();
|
||||
let _ = child.wait();
|
||||
return Err(RemoteError::Timeout(timeout));
|
||||
}
|
||||
thread::sleep(Duration::from_millis(20));
|
||||
}
|
||||
}
|
||||
|
||||
fn run_bounded(
|
||||
mut command: Command,
|
||||
timeout: Duration,
|
||||
) -> Result<(Vec<u8>, Vec<u8>, ExitStatus), RemoteError> {
|
||||
command
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped());
|
||||
let mut child = command.spawn()?;
|
||||
let (stdout, stderr) = read_pipes(&mut child);
|
||||
let status = wait_timeout(&mut child, timeout)?;
|
||||
let stdout = stdout.join().unwrap_or_default();
|
||||
let stderr = stderr.join().unwrap_or_default();
|
||||
if stdout.len() > MAX_CAPTURE {
|
||||
return Err(RemoteError::OutputTooLarge(MAX_CAPTURE));
|
||||
}
|
||||
Ok((stdout, stderr, status))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn quotes_shell_arguments_for_rroot() {
|
||||
assert_eq!(shell_quote("id"), "'id'");
|
||||
assert_eq!(shell_quote("echo 'x'"), "'echo '\\''x'\\'''");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_option_injection_and_unsafe_paths() {
|
||||
let mut options = SshOptions {
|
||||
host: "-oProxyCommand=bad".to_owned(),
|
||||
user: "root".to_owned(),
|
||||
..SshOptions::default()
|
||||
};
|
||||
assert!(options.validate().is_err());
|
||||
options.host = "10.255.1.167".to_owned();
|
||||
options.user = "root;id".to_owned();
|
||||
assert!(options.validate().is_err());
|
||||
assert!(safe_remote_path("/tmp/ok.img").is_ok());
|
||||
assert!(safe_remote_path("relative").is_err());
|
||||
assert!(safe_remote_path("/tmp/../etc/passwd").is_err());
|
||||
assert!(safe_remote_path("/tmp/a;b").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_root_methods() {
|
||||
assert_eq!(RootMethod::parse("auto"), Some(RootMethod::Auto));
|
||||
assert_eq!(
|
||||
RootMethod::parse("update-daemon"),
|
||||
Some(RootMethod::UpdateDaemon)
|
||||
);
|
||||
assert_eq!(RootMethod::parse("serial"), None);
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,11 @@ path = "src/main.rs"
|
||||
[dependencies]
|
||||
alkane-core = { path = "../alkane-core" }
|
||||
alkane-discovery = { path = "../alkane-discovery" }
|
||||
# The self-contained stock <-> alkane engine (compiled-in method) and the .afw
|
||||
# verify/fetch, so the GUI can install-from-stock and restore-to-stock and scan
|
||||
# alkane-os releases through the same code paths the CLI uses.
|
||||
alkane-flash = { path = "../alkane-flash" }
|
||||
alkane-artifacts = { path = "../alkane-artifacts" }
|
||||
# vendored + patched wx-rs (see vendor/wx-rs/patches/)
|
||||
wx-rs = { path = "../../thirdparty/wx-rs/wx-rs-0.2.0-patched" }
|
||||
asic-rs = "0.8.1"
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
//! Build script for the alkane-toolkit binary. On Windows targets it compiles
|
||||
//! windows/alkane-toolkit.rc (application manifest, icon, version info, and
|
||||
//! wxMSW's stock resources) and links the result into the GUI executable only.
|
||||
//! Other targets need nothing here.
|
||||
|
||||
use std::env;
|
||||
use std::path::PathBuf;
|
||||
use std::process::Command;
|
||||
|
||||
fn main() {
|
||||
println!("cargo:rerun-if-changed=build.rs");
|
||||
if env::var("CARGO_CFG_TARGET_OS").as_deref() != Ok("windows") {
|
||||
return;
|
||||
}
|
||||
for f in [
|
||||
"alkane-toolkit.rc",
|
||||
"alkane-toolkit.exe.manifest",
|
||||
"alkane-toolkit.ico",
|
||||
] {
|
||||
println!("cargo:rerun-if-changed=windows/{f}");
|
||||
}
|
||||
println!("cargo:rerun-if-env-changed=WX_RS_WXCONFIG");
|
||||
if let Err(e) = compile_resources() {
|
||||
panic!("alkane-gui: Windows resources: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
fn compile_resources() -> Result<(), String> {
|
||||
// The resource compiler and wx's resource include path come from the same
|
||||
// wx-config the wx-rs bridge builds against, so they always match the
|
||||
// linked wxMSW (and the mingw cross windres when cross-compiling).
|
||||
let wxconfig = env::var("WX_RS_WXCONFIG").unwrap_or_else(|_| "wx-config".into());
|
||||
let run = |arg: &str| -> Result<String, String> {
|
||||
let out = Command::new(&wxconfig)
|
||||
.arg(arg)
|
||||
.output()
|
||||
.map_err(|e| format!("cannot run {wxconfig} {arg}: {e}"))?;
|
||||
if !out.status.success() {
|
||||
return Err(format!("{wxconfig} {arg} failed"));
|
||||
}
|
||||
String::from_utf8(out.stdout).map_err(|e| e.to_string())
|
||||
};
|
||||
let rescomp = run("--rescomp")?;
|
||||
let mut words = rescomp.split_whitespace();
|
||||
let windres = words.next().ok_or("wx-config --rescomp printed nothing")?;
|
||||
let mut args: Vec<String> = words.map(str::to_owned).collect();
|
||||
// rcdefs.h lives in the build-specific setup directory, which --rescomp
|
||||
// omits; it is the -I entry of --cxxflags.
|
||||
for flag in run("--cxxflags")?.split_whitespace() {
|
||||
if let Some(dir) = flag.strip_prefix("-I") {
|
||||
args.push("--include-dir".into());
|
||||
args.push(dir.into());
|
||||
}
|
||||
}
|
||||
|
||||
let version = env::var("CARGO_PKG_VERSION").unwrap_or_default();
|
||||
let mut parts = version
|
||||
.split(['.', '-', '+'])
|
||||
.map(|p| p.parse::<u16>().unwrap_or(0));
|
||||
let (major, minor, patch) = (
|
||||
parts.next().unwrap_or(0),
|
||||
parts.next().unwrap_or(0),
|
||||
parts.next().unwrap_or(0),
|
||||
);
|
||||
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").map_err(|e| e.to_string())?);
|
||||
let out =
|
||||
PathBuf::from(env::var("OUT_DIR").map_err(|e| e.to_string())?).join("alkane-toolkit-res.o");
|
||||
let status = Command::new(windres)
|
||||
.args(&args)
|
||||
.arg("--include-dir")
|
||||
.arg(manifest_dir.join("windows"))
|
||||
.arg(format!("--define=ALKANE_VER_MAJOR={major}"))
|
||||
.arg(format!("--define=ALKANE_VER_MINOR={minor}"))
|
||||
.arg(format!("--define=ALKANE_VER_PATCH={patch}"))
|
||||
.arg(format!("--define=ALKANE_VER_STR=\\\"{version}\\\""))
|
||||
.arg("--input")
|
||||
.arg(manifest_dir.join("windows/alkane-toolkit.rc"))
|
||||
.arg("--output-format=coff")
|
||||
.arg("--output")
|
||||
.arg(&out)
|
||||
.status()
|
||||
.map_err(|e| format!("cannot run {windres}: {e}"))?;
|
||||
if !status.success() {
|
||||
return Err(format!("{windres} failed ({status})"));
|
||||
}
|
||||
// Only the GUI binary carries the manifest/icon; miner_probe stays a plain
|
||||
// console tool. A linked object (not an archive member) is never dropped by
|
||||
// the linker, which is what keeps the resource section in the .exe.
|
||||
println!("cargo:rustc-link-arg-bin=alkane-toolkit={}", out.display());
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
#!/bin/sh
|
||||
# Regenerate alkane-toolkit.icns (the macOS app icon) from the in-repo
|
||||
# microchip glyph: the same white chip on a rounded amber tile as the Windows
|
||||
# icon, at the sizes macOS asks for (16-1024 px). Needs rsvg-convert,
|
||||
# ImageMagick and python3; the .icns is committed, so a build never runs this.
|
||||
set -e
|
||||
cd "$(dirname "$0")"
|
||||
GLYPH=../assets/icons/microchip-solid-full.svg
|
||||
TMP=$(mktemp -d)
|
||||
trap 'rm -rf "$TMP"' 0
|
||||
sed 's/currentColor/#ffffff/g; s/<svg /<svg fill="#ffffff" /' "$GLYPH" > "$TMP/glyph.svg"
|
||||
for s in 16 32 64 128 256 512 1024; do
|
||||
# Apple's grid: the tile fills ~80% of the canvas, centred.
|
||||
t=$((s * 80 / 100)); m=$(((s - t) / 2)); r=$((t * 22 / 100))
|
||||
g=$((t * 60 / 100)); o=$(((s - g) / 2))
|
||||
rsvg-convert -w "$g" -h "$g" "$TMP/glyph.svg" -o "$TMP/g$s.png"
|
||||
convert -size "${s}x${s}" xc:none -fill '#d9822b' \
|
||||
-draw "roundrectangle $m,$m $((m + t - 1)),$((m + t - 1)) $r,$r" \
|
||||
"$TMP/g$s.png" -geometry "+$o+$o" -composite "PNG32:$TMP/i$s.png"
|
||||
done
|
||||
python3 - "$TMP" alkane-toolkit.icns <<'PY'
|
||||
import pathlib, struct, sys
|
||||
tmp, out = pathlib.Path(sys.argv[1]), sys.argv[2]
|
||||
# ICNS element types that hold PNG data, by pixel size.
|
||||
types = [("icp4", 16), ("icp5", 32), ("icp6", 64), ("ic07", 128), ("ic08", 256),
|
||||
("ic09", 512), ("ic10", 1024), ("ic11", 32), ("ic12", 64), ("ic13", 256),
|
||||
("ic14", 512)]
|
||||
body = b""
|
||||
for kind, size in types:
|
||||
data = (tmp / f"i{size}.png").read_bytes()
|
||||
body += kind.encode() + struct.pack(">I", len(data) + 8) + data
|
||||
pathlib.Path(out).write_bytes(b"icns" + struct.pack(">I", len(body) + 8) + body)
|
||||
PY
|
||||
echo "wrote $(pwd)/alkane-toolkit.icns"
|
||||
@@ -29,10 +29,10 @@ use std::net::{SocketAddr, TcpStream};
|
||||
use std::process::Command;
|
||||
use std::time::Duration;
|
||||
|
||||
use aes::cipher::{block_padding::ZeroPadding, BlockEncryptMut, KeyInit};
|
||||
use aes::Aes256;
|
||||
use aes::cipher::{BlockEncryptMut, KeyInit, block_padding::ZeroPadding};
|
||||
use base64::Engine;
|
||||
use base64::engine::general_purpose::STANDARD as BASE64;
|
||||
use base64::Engine;
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
type Aes256EcbEnc = ecb::Encryptor<Aes256>;
|
||||
@@ -42,9 +42,9 @@ type Aes256EcbEnc = ecb::Encryptor<Aes256>;
|
||||
/// authenticated/unauthorized answer is `None`.
|
||||
pub fn classify_http_code(code: &str) -> Option<bool> {
|
||||
match code.trim() {
|
||||
"200" | "204" => Some(false), // authenticated -> unlocked/writable
|
||||
"401" | "403" => Some(true), // rejected -> locked/view-only
|
||||
_ => None, // 000 (no connect), 404, 5xx, ... -> unknown
|
||||
"200" | "204" => Some(false), // authenticated -> unlocked/writable
|
||||
"401" | "403" => Some(true), // rejected -> locked/view-only
|
||||
_ => None, // 000 (no connect), 404, 5xx, ... -> unknown
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,27 +61,45 @@ fn endpoint(_firmware: &str) -> &'static str {
|
||||
/// could not run at all.
|
||||
pub fn probe_access(ip: &str, make: &str, user: &str, pass: &str) -> Result<Option<bool>, String> {
|
||||
if make.eq_ignore_ascii_case("whatsminer") {
|
||||
return Ok(probe_whatsminer_access(ip, &whatsminer_password_candidates()));
|
||||
// Try the caller's saved scan password first (round 18 #12: it was being
|
||||
// ignored for WhatsMiner, so a miner opened with the operator's real
|
||||
// password was mis-reported as locked), then the per-firmware override and
|
||||
// factory defaults. All attempts are non-mutating.
|
||||
let mut candidates = Vec::new();
|
||||
if !pass.is_empty() {
|
||||
candidates.push(pass.to_string());
|
||||
}
|
||||
for c in whatsminer_password_candidates() {
|
||||
if !candidates.contains(&c) {
|
||||
candidates.push(c);
|
||||
}
|
||||
}
|
||||
return Ok(probe_whatsminer_access(ip, &candidates));
|
||||
}
|
||||
let url = format!("http://{ip}{}", endpoint(make));
|
||||
let userpass = format!("{user}:{pass}");
|
||||
let mut cmd = Command::new("curl");
|
||||
let mut cmd = alkane_core::platform::quiet_command("curl");
|
||||
cmd.args([
|
||||
"-s",
|
||||
"-o",
|
||||
"/dev/null",
|
||||
alkane_core::platform::NULL_DEVICE,
|
||||
"-w",
|
||||
"%{http_code}",
|
||||
"--digest",
|
||||
"-u",
|
||||
&userpass,
|
||||
"--connect-timeout",
|
||||
"5",
|
||||
"--max-time",
|
||||
"9",
|
||||
"-K",
|
||||
"-", // read the rest of the config (the credentials) from stdin
|
||||
&url,
|
||||
]);
|
||||
let out = wait_with_timeout(cmd, Duration::from_secs(12))?;
|
||||
// The user:pass goes through a curl config on stdin, never argv, so it can't
|
||||
// be read from the process list (round 18 #12).
|
||||
let config = format!(
|
||||
"user = \"{}\"\n",
|
||||
curl_config_quote(&format!("{user}:{pass}"))
|
||||
);
|
||||
let out = wait_with_timeout(cmd, Duration::from_secs(12), Some(config.as_bytes()))?;
|
||||
// curl prints the status code to stdout regardless of exit status (000 on a
|
||||
// connection failure), so classify the code rather than the exit status.
|
||||
let code = String::from_utf8_lossy(&out.stdout);
|
||||
@@ -89,11 +107,26 @@ pub fn probe_access(ip: &str, make: &str, user: &str, pass: &str) -> Result<Opti
|
||||
}
|
||||
|
||||
/// Run a command with a wall-clock timeout (curl can hang past --max-time on a
|
||||
/// black-holed host).
|
||||
fn wait_with_timeout(mut cmd: Command, timeout: Duration) -> Result<std::process::Output, String> {
|
||||
use std::io::Read;
|
||||
cmd.stdout(std::process::Stdio::piped()).stderr(std::process::Stdio::piped());
|
||||
/// black-holed host). `stdin_data`, when set, is written to the child's stdin and
|
||||
/// the pipe closed -- used to feed a curl config (with the credentials) instead
|
||||
/// of putting them on the command line (round 18 #12).
|
||||
fn wait_with_timeout(
|
||||
mut cmd: Command,
|
||||
timeout: Duration,
|
||||
stdin_data: Option<&[u8]>,
|
||||
) -> Result<std::process::Output, String> {
|
||||
use std::io::{Read, Write};
|
||||
cmd.stdout(std::process::Stdio::piped())
|
||||
.stderr(std::process::Stdio::piped());
|
||||
if stdin_data.is_some() {
|
||||
cmd.stdin(std::process::Stdio::piped());
|
||||
}
|
||||
let mut child = cmd.spawn().map_err(|e| format!("spawn curl: {e}"))?;
|
||||
if let Some(data) = stdin_data {
|
||||
if let Some(mut si) = child.stdin.take() {
|
||||
let _ = si.write_all(data);
|
||||
} // dropping `si` closes the pipe so curl stops reading the config
|
||||
}
|
||||
let start = std::time::Instant::now();
|
||||
loop {
|
||||
match child.try_wait().map_err(|e| e.to_string())? {
|
||||
@@ -106,11 +139,16 @@ fn wait_with_timeout(mut cmd: Command, timeout: Duration) -> Result<std::process
|
||||
if let Some(mut e) = child.stderr.take() {
|
||||
let _ = e.read_to_end(&mut stderr);
|
||||
}
|
||||
return Ok(std::process::Output { status, stdout, stderr });
|
||||
return Ok(std::process::Output {
|
||||
status,
|
||||
stdout,
|
||||
stderr,
|
||||
});
|
||||
}
|
||||
None => {
|
||||
if start.elapsed() >= timeout {
|
||||
let _ = child.kill();
|
||||
let _ = child.wait(); // reap so we never leave a zombie (round 18 #12)
|
||||
return Err("curl timed out".into());
|
||||
}
|
||||
std::thread::sleep(Duration::from_millis(100));
|
||||
@@ -119,11 +157,17 @@ fn wait_with_timeout(mut cmd: Command, timeout: Duration) -> Result<std::process
|
||||
}
|
||||
}
|
||||
|
||||
/// Escape a value for a curl `--config` file (double-quoted string).
|
||||
fn curl_config_quote(s: &str) -> String {
|
||||
s.replace('\\', "\\\\").replace('"', "\\\"")
|
||||
}
|
||||
|
||||
/// Passwords to try for a WhatsMiner access check: a user-set per-firmware
|
||||
/// override (round 12 #13) wins; otherwise the two factory defaults ("admin" for
|
||||
/// V1/V2, "super" for V3). All attempts are non-mutating, so trying both is safe.
|
||||
fn whatsminer_password_candidates() -> Vec<String> {
|
||||
if let Some((_, pass)) = crate::credentials::load_firmware_credentials().get("WhatsMiner Stock") {
|
||||
if let Some((_, pass)) = crate::credentials::load_firmware_credentials().get("WhatsMiner Stock")
|
||||
{
|
||||
if !pass.is_empty() {
|
||||
return vec![pass.clone()];
|
||||
}
|
||||
@@ -135,9 +179,17 @@ fn whatsminer_password_candidates() -> Vec<String> {
|
||||
/// get_error_code command and format them one per line for the log viewer
|
||||
/// (round 12 #12). Returns None if the miner does not answer.
|
||||
pub fn whatsminer_get_error_code(ip: &str) -> Option<String> {
|
||||
let resp = wm_rpc(ip, "{\"command\":\"get_error_code\"}", Duration::from_secs(6)).ok()?;
|
||||
let resp = wm_rpc(
|
||||
ip,
|
||||
"{\"command\":\"get_error_code\"}",
|
||||
Duration::from_secs(6),
|
||||
)
|
||||
.ok()?;
|
||||
let v: serde_json::Value = serde_json::from_str(&resp).ok()?;
|
||||
let codes = v.get("Msg").and_then(|m| m.get("error_code")).and_then(|c| c.as_array());
|
||||
let codes = v
|
||||
.get("Msg")
|
||||
.and_then(|m| m.get("error_code"))
|
||||
.and_then(|c| c.as_array());
|
||||
let mut lines = vec![format!("WhatsMiner get_error_code ({ip}):")];
|
||||
match codes {
|
||||
Some(arr) if !arr.is_empty() => {
|
||||
@@ -163,7 +215,9 @@ fn wm_rpc(ip: &str, payload: &str, timeout: Duration) -> Result<String, String>
|
||||
let mut stream = TcpStream::connect_timeout(&addr, timeout).map_err(|e| e.to_string())?;
|
||||
stream.set_read_timeout(Some(timeout)).ok();
|
||||
stream.set_write_timeout(Some(timeout)).ok();
|
||||
stream.write_all(payload.as_bytes()).map_err(|e| e.to_string())?;
|
||||
stream
|
||||
.write_all(payload.as_bytes())
|
||||
.map_err(|e| e.to_string())?;
|
||||
let mut buf = Vec::new();
|
||||
let mut chunk = [0u8; 4096];
|
||||
loop {
|
||||
@@ -178,7 +232,9 @@ fn wm_rpc(ip: &str, payload: &str, timeout: Duration) -> Result<String, String>
|
||||
Err(_) => break,
|
||||
}
|
||||
}
|
||||
Ok(String::from_utf8_lossy(&buf).trim_matches('\u{0}').to_string())
|
||||
Ok(String::from_utf8_lossy(&buf)
|
||||
.trim_matches('\u{0}')
|
||||
.to_string())
|
||||
}
|
||||
|
||||
/// AES-256-ECB encrypt `data` under SHA256(pwd_md5), base64-encoded -- the exact
|
||||
@@ -205,49 +261,78 @@ fn wm_aes_ecb_enc(pwd_md5: &str, data: &str) -> Option<String> {
|
||||
fn probe_whatsminer_access(ip: &str, passwords: &[String]) -> Option<bool> {
|
||||
let timeout = Duration::from_secs(6);
|
||||
let mut reachable = false;
|
||||
// A transient error on one candidate must not abandon the others (round 18 #12):
|
||||
// skip to the next candidate, but remember it happened so we never report a
|
||||
// confident "locked" when the answer was really indeterminate.
|
||||
let mut errored = false;
|
||||
for pass in passwords {
|
||||
// 1. get_token (no auth) -> salt / newsalt / time.
|
||||
let tok_resp = match wm_rpc(ip, "{\"command\":\"get_token\"}", timeout) {
|
||||
Ok(s) => s,
|
||||
Err(_) => return None,
|
||||
Err(_) => {
|
||||
errored = true;
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let v: serde_json::Value = match serde_json::from_str(&tok_resp) {
|
||||
Ok(v) => v,
|
||||
Err(_) => return None,
|
||||
Err(_) => {
|
||||
errored = true;
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let Some(msg) = v.get("Msg") else { return None };
|
||||
let (Some(salt), Some(newsalt), Some(time)) = (
|
||||
msg.get("salt").and_then(|s| s.as_str()),
|
||||
msg.get("newsalt").and_then(|s| s.as_str()),
|
||||
msg.get("time").and_then(|s| s.as_str()),
|
||||
) else {
|
||||
return None;
|
||||
let (Some(salt), Some(newsalt), Some(time)) = v
|
||||
.get("Msg")
|
||||
.map(|msg| {
|
||||
(
|
||||
msg.get("salt").and_then(|s| s.as_str()),
|
||||
msg.get("newsalt").and_then(|s| s.as_str()),
|
||||
msg.get("time").and_then(|s| s.as_str()),
|
||||
)
|
||||
})
|
||||
.unwrap_or((None, None, None))
|
||||
else {
|
||||
errored = true;
|
||||
continue;
|
||||
};
|
||||
reachable = true;
|
||||
// 2. Derive the md5crypt password hash + write-token from the password.
|
||||
let crypted = md5crypt::md5crypt(pass.as_bytes(), salt.as_bytes());
|
||||
let full = String::from_utf8_lossy(&crypted);
|
||||
let Some(pwd_md5) = full.split('$').nth(3) else { continue };
|
||||
let sign_crypted = md5crypt::md5crypt(format!("{pwd_md5}{time}").as_bytes(), newsalt.as_bytes());
|
||||
let Some(pwd_md5) = full.split('$').nth(3) else {
|
||||
continue;
|
||||
};
|
||||
let sign_crypted =
|
||||
md5crypt::md5crypt(format!("{pwd_md5}{time}").as_bytes(), newsalt.as_bytes());
|
||||
let sign_full = String::from_utf8_lossy(&sign_crypted);
|
||||
let host_sign = sign_full.split('$').nth(3).unwrap_or("");
|
||||
// 3. Send an encrypted, read-only `summary`. If the AES key (from the
|
||||
// password) is right the miner replies encrypted; if wrong it returns
|
||||
// a plaintext "enc json load err".
|
||||
let cmd = format!("{{\"command\":\"summary\",\"token\":\"{host_sign}\"}}");
|
||||
let Some(enc) = wm_aes_ecb_enc(pwd_md5, &cmd) else { continue };
|
||||
let Some(enc) = wm_aes_ecb_enc(pwd_md5, &cmd) else {
|
||||
continue;
|
||||
};
|
||||
let payload = format!("{{\"enc\":1,\"data\":\"{enc}\"}}");
|
||||
let resp = match wm_rpc(ip, &payload, timeout) {
|
||||
Ok(s) => s,
|
||||
Err(_) => return None,
|
||||
Err(_) => {
|
||||
errored = true;
|
||||
continue;
|
||||
}
|
||||
};
|
||||
if resp.contains("\"enc\"") {
|
||||
return Some(false); // accepted -> unlocked/writable
|
||||
}
|
||||
// rejected -> try the next candidate password
|
||||
}
|
||||
// Reached the miner and every candidate password was rejected -> locked.
|
||||
if reachable { Some(true) } else { None }
|
||||
// Only report "locked" when we cleanly reached the miner and every candidate
|
||||
// was rejected with no transient errors; otherwise the result is indeterminate.
|
||||
if reachable && !errored {
|
||||
Some(true)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -256,14 +341,30 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn classify_codes() {
|
||||
assert_eq!(classify_http_code("200"), Some(false), "authenticated -> unlocked");
|
||||
assert_eq!(
|
||||
classify_http_code("200"),
|
||||
Some(false),
|
||||
"authenticated -> unlocked"
|
||||
);
|
||||
assert_eq!(classify_http_code("204"), Some(false));
|
||||
assert_eq!(classify_http_code("401"), Some(true), "unauthorized -> locked");
|
||||
assert_eq!(
|
||||
classify_http_code("401"),
|
||||
Some(true),
|
||||
"unauthorized -> locked"
|
||||
);
|
||||
assert_eq!(classify_http_code("403"), Some(true));
|
||||
assert_eq!(classify_http_code("000"), None, "no connection -> unknown");
|
||||
assert_eq!(classify_http_code("404"), None, "endpoint absent -> unknown");
|
||||
assert_eq!(
|
||||
classify_http_code("404"),
|
||||
None,
|
||||
"endpoint absent -> unknown"
|
||||
);
|
||||
assert_eq!(classify_http_code("500"), None);
|
||||
assert_eq!(classify_http_code(" 200 "), Some(false), "whitespace tolerated");
|
||||
assert_eq!(
|
||||
classify_http_code(" 200 "),
|
||||
Some(false),
|
||||
"whitespace tolerated"
|
||||
);
|
||||
assert_eq!(classify_http_code(""), None);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,305 @@
|
||||
//! Boards running Alkane OS on the network, and which alkane-os actions apply to
|
||||
//! a scanned row.
|
||||
//!
|
||||
//! asic-rs recognises miners by their cgminer socket (:4028) or a vendor web UI.
|
||||
//! A board running Alkane OS keeps its cgminer-compatible socket on loopback and
|
||||
//! serves its own console, so asic-rs reports nothing at that address and the
|
||||
//! board never appeared in the fleet. This probe finds it over plain HTTP:
|
||||
//! - `GET /api/identity` (alkane-os 0.1.2 and later): os, os_version, board,
|
||||
//! sdk_revision, hostname, mac, kernel;
|
||||
//! - otherwise the cgminer-style `GET /api/summary`, whose STATUS Description
|
||||
//! is "alkane-os" (earlier images: found, but the board family is unknown).
|
||||
//!
|
||||
//! Both endpoints are public and read-only on the board.
|
||||
|
||||
use crate::model::MinerRow;
|
||||
use serde_json::{json, Value};
|
||||
use std::net::IpAddr;
|
||||
use std::time::Duration;
|
||||
|
||||
/// Firmware family shown for a board running Alkane OS.
|
||||
pub const FIRMWARE: &str = "Alkane";
|
||||
|
||||
const CONNECT_TIMEOUT: Duration = Duration::from_secs(2);
|
||||
const READ_TIMEOUT: Duration = Duration::from_secs(4);
|
||||
const MAX_RESPONSE: usize = 64 * 1024;
|
||||
|
||||
/// Probe `ip` for Alkane OS. Returns a row for the fleet table, or None when the
|
||||
/// address is not an Alkane board (or does not answer on :80).
|
||||
pub async fn detect(ip: IpAddr) -> Option<MinerRow> {
|
||||
if let Some(doc) = get_json(ip, "/api/identity").await {
|
||||
if doc.get("os").and_then(Value::as_str) == Some("alkane-os") {
|
||||
return Some(row_from_identity(ip, &doc));
|
||||
}
|
||||
}
|
||||
let summary = get_json(ip, "/api/summary").await?;
|
||||
if is_alkane_summary(&summary) {
|
||||
return Some(row_from_identity(ip, &json!({})));
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// The cgminer-style summary an Alkane board serves names "alkane-os" in its
|
||||
/// STATUS Description.
|
||||
fn is_alkane_summary(doc: &Value) -> bool {
|
||||
doc.get("STATUS")
|
||||
.and_then(Value::as_array)
|
||||
.and_then(|status| status.first())
|
||||
.and_then(|entry| entry.get("Description"))
|
||||
.and_then(Value::as_str)
|
||||
== Some("alkane-os")
|
||||
}
|
||||
|
||||
/// Control-board labels (cpu, code) for an Alkane board family, matching the
|
||||
/// labels the scan gives the same board on stock firmware.
|
||||
fn board_labels(board: &str) -> (&'static str, &'static str) {
|
||||
match board {
|
||||
"zynq-7010" => ("XIL", "C49"),
|
||||
"am335x" => ("BBB", "C77"),
|
||||
"cv1835" => ("CVI", "C88"),
|
||||
"a113d" => ("AML", "C76"),
|
||||
_ => ("", ""),
|
||||
}
|
||||
}
|
||||
|
||||
fn row_from_identity(ip: IpAddr, doc: &Value) -> MinerRow {
|
||||
let text = |key: &str| {
|
||||
doc.get(key)
|
||||
.and_then(Value::as_str)
|
||||
.unwrap_or("")
|
||||
.trim()
|
||||
.to_string()
|
||||
};
|
||||
let board = text("board");
|
||||
let (cpu, code) = board_labels(&board);
|
||||
let control_board = if cpu.is_empty() {
|
||||
board.clone()
|
||||
} else {
|
||||
format!("{cpu} ({code})")
|
||||
};
|
||||
MinerRow {
|
||||
ip: ip.to_string(),
|
||||
// The Alkane-supported control boards are all Bitmain (Antminer) boards.
|
||||
brand: if cpu.is_empty() {
|
||||
String::new()
|
||||
} else {
|
||||
"Antminer".to_string()
|
||||
},
|
||||
firmware: FIRMWARE.to_string(),
|
||||
fw_version: text("os_version"),
|
||||
api_version: "alkane-os".to_string(),
|
||||
mac: text("mac"),
|
||||
hostname: text("hostname"),
|
||||
control_board,
|
||||
cpu: cpu.to_string(),
|
||||
board_code: code.to_string(),
|
||||
alkane_board: Some(board),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
/// HTTP/1.0 GET on :80 returning the JSON body of a 200 reply.
|
||||
async fn get_json(ip: IpAddr, path: &str) -> Option<Value> {
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
let connect = tokio::net::TcpStream::connect((ip, 80));
|
||||
let mut stream = tokio::time::timeout(CONNECT_TIMEOUT, connect)
|
||||
.await
|
||||
.ok()?
|
||||
.ok()?;
|
||||
let request = format!("GET {path} HTTP/1.0\r\nHost: {ip}\r\nConnection: close\r\n\r\n");
|
||||
stream.write_all(request.as_bytes()).await.ok()?;
|
||||
let mut raw = Vec::new();
|
||||
let _ = tokio::time::timeout(READ_TIMEOUT, async {
|
||||
let mut chunk = [0u8; 4096];
|
||||
loop {
|
||||
match stream.read(&mut chunk).await {
|
||||
Ok(0) | Err(_) => break,
|
||||
Ok(n) => {
|
||||
raw.extend_from_slice(&chunk[..n]);
|
||||
if raw.len() > MAX_RESPONSE {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.await;
|
||||
parse_http_json(&raw)
|
||||
}
|
||||
|
||||
fn parse_http_json(raw: &[u8]) -> Option<Value> {
|
||||
let text = std::str::from_utf8(raw).ok()?;
|
||||
let (head, body) = text.split_once("\r\n\r\n")?;
|
||||
let status = head.lines().next()?;
|
||||
if status.split_whitespace().nth(1) != Some("200") {
|
||||
return None;
|
||||
}
|
||||
serde_json::from_str(body.trim()).ok()
|
||||
}
|
||||
|
||||
/// Whether the row is a board running Alkane OS (found by `detect`).
|
||||
pub fn is_alkane(row: &MinerRow) -> bool {
|
||||
row.alkane_board.is_some()
|
||||
}
|
||||
|
||||
/// The alkane-os board family the row's control board is, when it can be told:
|
||||
/// what an Alkane board reports, or for a stock Antminer the Zynq-7010 (S17/T17
|
||||
/// era, "XIL (C49)") control board.
|
||||
pub fn board_family(row: &MinerRow) -> Option<String> {
|
||||
if let Some(board) = &row.alkane_board {
|
||||
return (!board.is_empty()).then(|| board.clone());
|
||||
}
|
||||
if !row.brand.to_ascii_lowercase().contains("antminer") {
|
||||
return None;
|
||||
}
|
||||
if row.cpu == "XIL" && row.board_code == "C49" {
|
||||
return Some("zynq-7010".to_string());
|
||||
}
|
||||
// A stock S17 whose miner software is down (no hashboards attached: it
|
||||
// restart-loops) reports its model but no control board. Its model then
|
||||
// decides; a control board that IS reported always wins above.
|
||||
if row.cpu.is_empty() && is_zynq_7010_model(&row.model) {
|
||||
return Some("zynq-7010".to_string());
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Stock models built only on the Zynq-7010 (XIL C49) control board.
|
||||
fn is_zynq_7010_model(model: &str) -> bool {
|
||||
matches!(model.trim(), "S17" | "S17 Pro" | "T17")
|
||||
}
|
||||
|
||||
/// Whether alkane-os can be installed on the row's board: its control board is
|
||||
/// one the toolkit supports.
|
||||
pub fn install_supported(row: &MinerRow) -> bool {
|
||||
board_family(row).is_some_and(|board| crate::stock_ops::is_supported_board(&board))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn ip() -> IpAddr {
|
||||
"10.0.0.5".parse().unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_a_200_json_reply_only() {
|
||||
let ok = b"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{\"os\":\"alkane-os\"}";
|
||||
assert_eq!(parse_http_json(ok).unwrap()["os"], "alkane-os");
|
||||
let denied = b"HTTP/1.1 401 Unauthorized\r\n\r\n{\"os\":\"alkane-os\"}";
|
||||
assert!(parse_http_json(denied).is_none());
|
||||
let html = b"HTTP/1.1 200 OK\r\n\r\n<!DOCTYPE html><title>x</title>";
|
||||
assert!(parse_http_json(html).is_none());
|
||||
assert!(parse_http_json(b"garbage").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn recognises_the_alkane_summary() {
|
||||
let alkane = json!({"STATUS":[{"Code":0,"Description":"alkane-os","STATUS":"S"}]});
|
||||
assert!(is_alkane_summary(&alkane));
|
||||
let cgminer = json!({"STATUS":[{"Description":"cgminer 4.11.1","STATUS":"S"}]});
|
||||
assert!(!is_alkane_summary(&cgminer));
|
||||
assert!(!is_alkane_summary(&json!({})));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn identity_becomes_a_supported_alkane_row() {
|
||||
let doc = json!({
|
||||
"os": "alkane-os", "os_version": "0.1.2", "board": "zynq-7010",
|
||||
"sdk_revision": "6a9080ac82c1", "hostname": "alkane-zynq",
|
||||
"mac": "26:4e:52:8b:e2:48", "kernel": "6.18.48",
|
||||
});
|
||||
let row = row_from_identity(ip(), &doc);
|
||||
assert_eq!(row.ip, "10.0.0.5");
|
||||
assert_eq!(row.firmware, "Alkane");
|
||||
assert_eq!(row.fw_version, "0.1.2");
|
||||
assert_eq!(row.control_board, "XIL (C49)");
|
||||
assert_eq!(row.mac, "26:4e:52:8b:e2:48");
|
||||
assert!(is_alkane(&row));
|
||||
assert!(install_supported(&row));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_old_alkane_board_is_alkane_but_its_family_is_unknown() {
|
||||
// Found via /api/summary only: uninstall applies, install cannot be
|
||||
// confirmed as supported.
|
||||
let row = row_from_identity(ip(), &json!({}));
|
||||
assert!(is_alkane(&row));
|
||||
assert_eq!(board_family(&row), None);
|
||||
assert!(!install_supported(&row));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stock_antminers_are_supported_only_on_the_zynq_7010_board() {
|
||||
let stock = |cpu: &str, code: &str| MinerRow {
|
||||
brand: "Antminer".into(),
|
||||
firmware: "Stock".into(),
|
||||
cpu: cpu.into(),
|
||||
board_code: code.into(),
|
||||
..Default::default()
|
||||
};
|
||||
let s17 = stock("XIL", "C49");
|
||||
assert!(install_supported(&s17));
|
||||
assert!(!is_alkane(&s17));
|
||||
assert!(!install_supported(&stock("XIL", "C55"))); // S19-era Zynq
|
||||
assert!(!install_supported(&stock("AML", "C76")));
|
||||
assert!(!install_supported(&stock("", "")));
|
||||
let whatsminer = MinerRow {
|
||||
brand: "WhatsMiner".into(),
|
||||
cpu: "XIL".into(),
|
||||
board_code: "C49".into(),
|
||||
..Default::default()
|
||||
};
|
||||
assert!(!install_supported(&whatsminer));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_stock_s17_without_a_reported_control_board_is_supported_by_model() {
|
||||
// As seen live: stock S17, miner software down, control board blank.
|
||||
let s17 = MinerRow {
|
||||
brand: "Antminer".into(),
|
||||
model: "S17".into(),
|
||||
firmware: "Stock".into(),
|
||||
..Default::default()
|
||||
};
|
||||
assert!(install_supported(&s17));
|
||||
let other = |model: &str| MinerRow {
|
||||
brand: "Antminer".into(),
|
||||
model: model.into(),
|
||||
..Default::default()
|
||||
};
|
||||
assert!(install_supported(&other("S17 Pro")));
|
||||
assert!(install_supported(&other("T17")));
|
||||
assert!(!install_supported(&other("S19")));
|
||||
assert!(!install_supported(&other("S17+")));
|
||||
assert!(!install_supported(&other("")));
|
||||
// A reported control board overrides the model.
|
||||
let s17_on_aml = MinerRow {
|
||||
brand: "Antminer".into(),
|
||||
model: "S17".into(),
|
||||
cpu: "AML".into(),
|
||||
board_code: "C76".into(),
|
||||
..Default::default()
|
||||
};
|
||||
assert!(!install_supported(&s17_on_aml));
|
||||
}
|
||||
|
||||
/// Live check against a real board:
|
||||
/// `ALKANE_TEST_IP=10.255.1.111 cargo test -p alkane-gui live_detect -- --ignored --nocapture`
|
||||
#[test]
|
||||
#[ignore = "needs a board on the network (ALKANE_TEST_IP)"]
|
||||
fn live_detect() {
|
||||
let Ok(ip) = std::env::var("ALKANE_TEST_IP") else {
|
||||
return;
|
||||
};
|
||||
let rt = tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
.unwrap();
|
||||
let row = rt.block_on(detect(ip.parse().unwrap()));
|
||||
println!("{row:#?}");
|
||||
let row = row.expect("an Alkane board at ALKANE_TEST_IP");
|
||||
assert!(is_alkane(&row));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
//! Non-interactive SSH password logins without `sshpass`.
|
||||
//!
|
||||
//! OpenSSH (Linux, and the `ssh.exe` built into Windows 10/11) asks the
|
||||
//! program named by `SSH_ASKPASS` for a password when `SSH_ASKPASS_REQUIRE=force`
|
||||
//! (OpenSSH 8.4+). The toolkit names its own executable: when ssh runs it with
|
||||
//! [`ROLE_ENV`] set, `main()` calls [`serve_if_askpass`] first thing, which
|
||||
//! prints the password from [`SECRET_ENV`] and exits before any GUI starts.
|
||||
//!
|
||||
//! This replaces `sshpass`, which Windows does not have, with one mechanism for
|
||||
//! every platform. Like `sshpass -e` the password travels in the child's
|
||||
//! environment, never on a command line where the process list would show it.
|
||||
|
||||
use std::ffi::OsStr;
|
||||
use std::process::{Command, Stdio};
|
||||
|
||||
/// Set (to "1") in the environment of an ssh child whose askpass helper is us.
|
||||
pub const ROLE_ENV: &str = "ALKANE_SSH_ASKPASS";
|
||||
/// The password the askpass helper hands back to ssh.
|
||||
pub const SECRET_ENV: &str = "ALKANE_SSH_SECRET";
|
||||
|
||||
/// If this process is ssh's askpass helper, answer the prompt and return the
|
||||
/// exit code to use; otherwise `None` and the app starts normally.
|
||||
pub fn serve_if_askpass() -> Option<i32> {
|
||||
std::env::var_os(ROLE_ENV)?;
|
||||
let prompt = std::env::args_os().nth(1).unwrap_or_default();
|
||||
Some(answer(&prompt, std::env::var_os(SECRET_ENV).as_deref()))
|
||||
}
|
||||
|
||||
/// Print the secret for a password prompt. Anything else ssh might ask through
|
||||
/// askpass (a host-key "yes/no" confirmation, a passphrase for a key we never
|
||||
/// configured) is refused, so a changed prompt can never be answered with the
|
||||
/// password by accident.
|
||||
fn answer(prompt: &OsStr, secret: Option<&OsStr>) -> i32 {
|
||||
let Some(secret) = secret else { return 1 };
|
||||
if !is_password_prompt(&prompt.to_string_lossy()) {
|
||||
return 1;
|
||||
}
|
||||
use std::io::Write;
|
||||
let mut out = std::io::stdout().lock();
|
||||
if out
|
||||
.write_all(secret.to_string_lossy().as_bytes())
|
||||
.and_then(|_| out.write_all(b"\n"))
|
||||
.and_then(|_| out.flush())
|
||||
.is_err()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
0
|
||||
}
|
||||
|
||||
fn is_password_prompt(prompt: &str) -> bool {
|
||||
let p = prompt.to_ascii_lowercase();
|
||||
p.contains("password") && !p.contains("yes/no")
|
||||
}
|
||||
|
||||
/// Configure an `ssh` command to log in with `password` and never block on a
|
||||
/// terminal prompt. ssh tries password (and keyboard-interactive) auth once;
|
||||
/// a wrong password fails fast with "Permission denied" instead of hanging.
|
||||
pub fn ssh_with_password(cmd: &mut Command, password: &str) -> Result<(), String> {
|
||||
let exe = std::env::current_exe().map_err(|e| format!("locate askpass helper: {e}"))?;
|
||||
cmd.env("SSH_ASKPASS", exe)
|
||||
.env("SSH_ASKPASS_REQUIRE", "force")
|
||||
.env(ROLE_ENV, "1")
|
||||
.env(SECRET_ENV, password)
|
||||
.stdin(Stdio::null());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// ssh `-o` options that go with [`ssh_with_password`].
|
||||
pub const PASSWORD_AUTH_OPTIONS: [&str; 4] = [
|
||||
"-o",
|
||||
"PreferredAuthentications=password,keyboard-interactive",
|
||||
"-o",
|
||||
"NumberOfPasswordPrompts=1",
|
||||
];
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn only_password_prompts_are_answered() {
|
||||
assert!(is_password_prompt("root@10.0.0.1's password: "));
|
||||
assert!(is_password_prompt("Password:"));
|
||||
assert!(!is_password_prompt(
|
||||
"Are you sure you want to continue connecting (yes/no/[fingerprint])?"
|
||||
));
|
||||
assert!(!is_password_prompt(
|
||||
"Enter passphrase for key '/x/id_ed25519':"
|
||||
));
|
||||
assert_eq!(
|
||||
answer(OsStr::new("Password:"), None),
|
||||
1,
|
||||
"no secret -> refuse"
|
||||
);
|
||||
assert_eq!(
|
||||
answer(OsStr::new("Continue (yes/no)?"), Some(OsStr::new("pw"))),
|
||||
1
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ssh_command_gets_askpass_environment() {
|
||||
let mut cmd = Command::new("ssh");
|
||||
ssh_with_password(&mut cmd, "hunter2").unwrap();
|
||||
let env: std::collections::HashMap<_, _> = cmd
|
||||
.get_envs()
|
||||
.map(|(k, v)| (k.to_owned(), v.map(|v| v.to_owned())))
|
||||
.collect();
|
||||
assert_eq!(
|
||||
env[OsStr::new("SSH_ASKPASS_REQUIRE")].as_deref(),
|
||||
Some(OsStr::new("force"))
|
||||
);
|
||||
assert_eq!(
|
||||
env[OsStr::new(SECRET_ENV)].as_deref(),
|
||||
Some(OsStr::new("hunter2"))
|
||||
);
|
||||
assert!(env[OsStr::new("SSH_ASKPASS")].is_some());
|
||||
// The password is never an argument.
|
||||
assert!(cmd.get_args().all(|a| a != "hunter2"));
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
//! miner_probe -- a read-only (by default) asic-rs diagnostic for live-hardware
|
||||
//! verification of the round-9 power-limit work. It reuses the same MinerFactory
|
||||
//! + saved-credential setup the GUI uses (see crate::credentials), but as a
|
||||
//! and saved-credential setup the GUI uses (see crate::credentials), but as a
|
||||
//! standalone CLI so hardware checks don't go through the wx event loop.
|
||||
//!
|
||||
//! Usage:
|
||||
@@ -19,18 +19,19 @@
|
||||
//! ALKANE_PROBE_USER / ALKANE_PROBE_PASS if set (so a test can authenticate
|
||||
//! without touching the app's saved config).
|
||||
|
||||
use asic_rs::MinerFactory;
|
||||
use asic_rs::core::traits::miner::MinerAuth;
|
||||
use asic_rs::MinerFactory;
|
||||
use futures_util::StreamExt;
|
||||
use measurements::Power;
|
||||
|
||||
fn load_creds() -> Option<(String, String)> {
|
||||
if let (Ok(u), Ok(pw)) = (std::env::var("ALKANE_PROBE_USER"), std::env::var("ALKANE_PROBE_PASS")) {
|
||||
if let (Ok(u), Ok(pw)) = (
|
||||
std::env::var("ALKANE_PROBE_USER"),
|
||||
std::env::var("ALKANE_PROBE_PASS"),
|
||||
) {
|
||||
return Some((u, pw));
|
||||
}
|
||||
let home = std::env::var("HOME").ok()?;
|
||||
let p = std::path::PathBuf::from(home)
|
||||
.join(".config/alkane-toolkit/scan-credentials.txt");
|
||||
let p = alkane_core::platform::config_path("scan-credentials.txt");
|
||||
let s = std::fs::read_to_string(p).ok()?;
|
||||
let mut l = s.lines();
|
||||
let u = l.next()?.to_string();
|
||||
@@ -39,12 +40,17 @@ fn load_creds() -> Option<(String, String)> {
|
||||
}
|
||||
|
||||
fn factory() -> MinerFactory {
|
||||
// This diagnostic CLI is a separate binary from the GUI, so it cannot share
|
||||
// the GUI's credentials::configured_factory. It applies only the global scan
|
||||
// credentials to every firmware; the GUI's per-firmware credential overrides
|
||||
// are NOT read here (round 18 #12), which the message below makes explicit so a
|
||||
// probe result is never mistaken for what the GUI (with overrides) would see.
|
||||
let mut f = MinerFactory::new();
|
||||
if let Some((u, pw)) = load_creds() {
|
||||
for fw in asic_rs::factory::default_firmware_registry() {
|
||||
f = f.with_firmware_discovery_auth(fw.as_ref(), MinerAuth::new(u.clone(), pw.clone()));
|
||||
}
|
||||
eprintln!("(using saved scan credentials for discovery auth)");
|
||||
eprintln!("(using the global scan credentials; per-firmware overrides are not applied)");
|
||||
} else {
|
||||
eprintln!("(no saved credentials -- backends use their default auth)");
|
||||
}
|
||||
@@ -87,8 +93,14 @@ async fn scan(cidrs: &[String]) {
|
||||
.map(|c| c.to_string())
|
||||
.unwrap_or_default();
|
||||
let fw_ver = data.firmware_version.clone().unwrap_or_default();
|
||||
let bs = data.best_share.map(|v| format!("{v:.0}")).unwrap_or_default();
|
||||
let sbs = data.session_best_share.map(|v| format!("{v:.0}")).unwrap_or_default();
|
||||
let bs = data
|
||||
.best_share
|
||||
.map(|v| format!("{v:.0}"))
|
||||
.unwrap_or_default();
|
||||
let sbs = data
|
||||
.session_best_share
|
||||
.map(|v| format!("{v:.0}"))
|
||||
.unwrap_or_default();
|
||||
println!(
|
||||
"{ip}\t{}\t{}\t{}\t{}\t{}\t{watts:.0}\t{}\t{}\t{bs}\t{sbs}",
|
||||
data.device_info.make,
|
||||
@@ -276,7 +288,10 @@ async fn msgs(ip_s: &str) {
|
||||
Ok(Some(m)) => {
|
||||
let d = m.get_data().await;
|
||||
let th = |h: &Option<asic_rs::core::data::hashrate::HashRate>| {
|
||||
h.clone().map(|x| x.as_unit(asic_rs::core::data::hashrate::HashRateUnit::TeraHash).value)
|
||||
h.clone().map(|x| {
|
||||
x.as_unit(asic_rs::core::data::hashrate::HashRateUnit::TeraHash)
|
||||
.value
|
||||
})
|
||||
};
|
||||
println!(
|
||||
"{ip}: {} {} hashboards={:?} expected={:?}",
|
||||
@@ -300,13 +315,17 @@ async fn msgs(ip_s: &str) {
|
||||
println!(" fans(rpm)={fans:?} psu_fans={}", d.psu_fans.len());
|
||||
for b in d.hashboards.iter() {
|
||||
println!(
|
||||
" board pos={} hr={:?}TH chips={:?}/{:?} board_temp={:?} outlet_temp={:?}",
|
||||
" board pos={} hr={:?}TH chips={:?}/{:?} board_temp={:?} outlet_temp={:?} freq={:?}MHz volt={:?}V chip_type={:?} hb_model={:?}",
|
||||
b.position,
|
||||
th(&b.hashrate),
|
||||
b.working_chips,
|
||||
b.expected_chips,
|
||||
b.board_temperature.as_ref().map(|t| t.as_celsius()),
|
||||
b.outlet_chip_temperature.as_ref().map(|t| t.as_celsius()),
|
||||
b.frequency.as_ref().map(|f| f.as_megahertz()),
|
||||
b.voltage.as_ref().map(|v| v.as_volts()),
|
||||
b.chip_type,
|
||||
b.hashboard_model,
|
||||
);
|
||||
}
|
||||
if d.messages.is_empty() {
|
||||
@@ -380,7 +399,7 @@ async fn lifecycle(ip_s: &str, verb: &str) {
|
||||
|
||||
/// Read (no tz arg) or set the miner's timezone (round 16 / G10). Capability-gated.
|
||||
async fn timezone(ip_s: &str, tz: Option<&str>) {
|
||||
use asic_rs::core::config::timezone::{Tz, TimezoneConfig};
|
||||
use asic_rs::core::config::timezone::{TimezoneConfig, Tz};
|
||||
let Ok(ip) = ip_s.parse::<std::net::IpAddr>() else {
|
||||
eprintln!("bad ip {ip_s}");
|
||||
return;
|
||||
@@ -557,7 +576,9 @@ async fn settune(ip_s: &str, kind: &str, value: f64) {
|
||||
TuningTarget::HashRate(hr)
|
||||
}
|
||||
None => {
|
||||
eprintln!("miner reports no current hashrate; cannot build a hashrate target");
|
||||
eprintln!(
|
||||
"miner reports no current hashrate; cannot build a hashrate target"
|
||||
);
|
||||
return;
|
||||
}
|
||||
},
|
||||
@@ -584,7 +605,9 @@ async fn settune(ip_s: &str, kind: &str, value: f64) {
|
||||
}
|
||||
} else if kind == "power" && m.supports_set_power_limit() {
|
||||
match m.set_power_limit(Power::from_watts(value)).await {
|
||||
Ok(v) => println!("set_power_limit({value} W) -> Ok({v}) [tuning_config unsupported]"),
|
||||
Ok(v) => println!(
|
||||
"set_power_limit({value} W) -> Ok({v}) [tuning_config unsupported]"
|
||||
),
|
||||
Err(e) => println!("set_power_limit({value} W) -> Err: {e:#}"),
|
||||
}
|
||||
} else {
|
||||
@@ -598,9 +621,83 @@ async fn settune(ip_s: &str, kind: &str, value: f64) {
|
||||
}
|
||||
}
|
||||
|
||||
/// READ: list the firmware's selectable profiles/presets (roadmap G3).
|
||||
async fn presets(ip_s: &str) {
|
||||
let ip: std::net::IpAddr = match ip_s.parse() {
|
||||
Ok(ip) => ip,
|
||||
Err(e) => {
|
||||
eprintln!("bad ip {ip_s}: {e}");
|
||||
return;
|
||||
}
|
||||
};
|
||||
let f = factory();
|
||||
match f.scan_miner(ip).await {
|
||||
Ok(Some(m)) => {
|
||||
if !m.supports_presets() {
|
||||
println!("{ip}: firmware does not expose named presets");
|
||||
return;
|
||||
}
|
||||
let list = m.get_presets().await;
|
||||
if list.is_empty() {
|
||||
println!("{ip}: no presets reported");
|
||||
return;
|
||||
}
|
||||
for p in list {
|
||||
match (p.pretty, p.status) {
|
||||
(Some(pretty), Some(status)) => println!("{}\t{pretty}\t[{status}]", p.name),
|
||||
(Some(pretty), None) => println!("{}\t{pretty}", p.name),
|
||||
(None, _) => println!("{}", p.name),
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(None) => eprintln!("no miner at {ip}"),
|
||||
Err(e) => eprintln!("scan_miner {ip}: {e}"),
|
||||
}
|
||||
}
|
||||
|
||||
/// WRITE (reversible): select a named firmware profile/preset (roadmap G3).
|
||||
/// `setpreset <ip> <name>`. Prints the prior target so it can be restored.
|
||||
async fn setpreset(ip_s: &str, name: &str) {
|
||||
use asic_rs::core::config::tuning::TuningConfig;
|
||||
use asic_rs::core::data::miner::TuningTarget;
|
||||
let ip: std::net::IpAddr = match ip_s.parse() {
|
||||
Ok(ip) => ip,
|
||||
Err(e) => {
|
||||
eprintln!("bad ip {ip_s}: {e}");
|
||||
return;
|
||||
}
|
||||
};
|
||||
let f = factory();
|
||||
match f.scan_miner(ip).await {
|
||||
Ok(Some(m)) => {
|
||||
let before = m.get_data().await;
|
||||
eprintln!(
|
||||
"target {ip}: {} {} {} (supports_presets={}); prior target = {:?}",
|
||||
before.device_info.make,
|
||||
before.device_info.firmware,
|
||||
before.device_info.model,
|
||||
m.supports_presets(),
|
||||
before.tuning_target,
|
||||
);
|
||||
let cfg = TuningConfig::new(TuningTarget::Preset(name.to_string()));
|
||||
match m.set_tuning_config(cfg, None).await {
|
||||
Ok(v) => println!("set_tuning_config(preset {name}) -> Ok({v})"),
|
||||
Err(e) => println!("set_tuning_config(preset {name}) -> Err: {e:#}"),
|
||||
}
|
||||
let after = m.get_data().await;
|
||||
println!(" target now = {:?}", after.tuning_target);
|
||||
}
|
||||
Ok(None) => eprintln!("no miner at {ip}"),
|
||||
Err(e) => eprintln!("scan_miner {ip}: {e}"),
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
let rt = match tokio::runtime::Builder::new_current_thread().enable_all().build() {
|
||||
let rt = match tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
{
|
||||
Ok(rt) => rt,
|
||||
Err(e) => {
|
||||
eprintln!("tokio runtime: {e}");
|
||||
@@ -630,6 +727,8 @@ fn main() {
|
||||
}
|
||||
Some("msgs") if args.len() == 3 => rt.block_on(msgs(&args[2])),
|
||||
Some("tune") if args.len() == 3 => rt.block_on(tune(&args[2])),
|
||||
Some("presets") if args.len() == 3 => rt.block_on(presets(&args[2])),
|
||||
Some("setpreset") if args.len() == 4 => rt.block_on(setpreset(&args[2], &args[3])),
|
||||
Some("settune") if args.len() == 5 && matches!(args[3].as_str(), "power" | "hashrate") => {
|
||||
let value: f64 = args[4].parse().unwrap_or(-1.0);
|
||||
if value <= 0.0 {
|
||||
@@ -651,6 +750,7 @@ fn main() {
|
||||
\x20 hashrate <cidr> [<cidr>...] raw hashrate/algo dump\n\
|
||||
\x20 msgs <ip> structured faults + per-board data\n\
|
||||
\x20 tune <ip> structured-tuning support + capability envelope\n\
|
||||
\x20 presets <ip> list selectable firmware profiles/presets\n\
|
||||
\n\
|
||||
WRITE (MUTATES the miner -- use the test-bed miners, not production):\n\
|
||||
\x20 light <ip> <on|off> set the locate light\n\
|
||||
@@ -660,6 +760,7 @@ fn main() {
|
||||
\x20 restart <ip> restart/reboot\n\
|
||||
\x20 setpl <ip> <watts> set power limit\n\
|
||||
\x20 settune <ip> <power|hashrate> <v> set a structured tuning target (prints prior)\n\
|
||||
\x20 setpreset <ip> <name> select a named firmware profile/preset (prints prior)\n\
|
||||
\x20 setpools <ip> <url> <user> <pass> set the default pool group\n\
|
||||
\x20 timezone <ip> [<IANA>] read (no arg) or set the timezone\n\
|
||||
\x20 passwd <ip> <newpass> change the web/API password (DANGEROUS)\n\
|
||||
|
||||
@@ -0,0 +1,437 @@
|
||||
//! Bug reports and feature requests from the toolbar's Bug report button.
|
||||
//!
|
||||
//! The app posts the report to the report relay (see tools/report-relay),
|
||||
//! which holds the Gitea token and files the issue in blueflare/alkane-toolkit.
|
||||
//! The app itself holds no secret. A report carries its type, the user's text,
|
||||
//! what the app knows about itself (version, OS, appearance, language, window
|
||||
//! size) and, if the user leaves it ticked, the recent activity log with
|
||||
//! credentials scrubbed. Nothing is stored locally.
|
||||
|
||||
use std::io::Write;
|
||||
use std::process::Stdio;
|
||||
use std::sync::mpsc::{channel, Receiver};
|
||||
|
||||
/// Where reports go. `ALKANE_REPORT_URL` overrides it (tests, staging).
|
||||
pub const DEFAULT_URL: &str = "https://gitea.bluefla.re/alkane-report/v1/report";
|
||||
/// Longest report text the relay accepts.
|
||||
pub const MAX_TEXT_CHARS: usize = 8000;
|
||||
/// Log sent with a report: at most this many of the newest lines...
|
||||
const LOG_LINES: usize = 80;
|
||||
/// ...cut to this many bytes on a line boundary.
|
||||
const LOG_BYTES: usize = 16 * 1024;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum Kind {
|
||||
Bug,
|
||||
Ui,
|
||||
Feature,
|
||||
}
|
||||
|
||||
impl Kind {
|
||||
/// In the order the dialog's type choice lists them.
|
||||
pub const ALL: [Kind; 3] = [Kind::Bug, Kind::Ui, Kind::Feature];
|
||||
|
||||
pub fn label(self) -> &'static str {
|
||||
match self {
|
||||
Kind::Bug => "Bug",
|
||||
Kind::Ui => "UI change",
|
||||
Kind::Feature => "Feature request",
|
||||
}
|
||||
}
|
||||
|
||||
/// The wire value, which is also the issue label.
|
||||
pub fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
Kind::Bug => "bug",
|
||||
Kind::Ui => "ui",
|
||||
Kind::Feature => "feature",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// What the app knows about itself, sent with every report.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct Context {
|
||||
pub app_version: String,
|
||||
pub platform: String,
|
||||
pub os: String,
|
||||
pub appearance: String,
|
||||
pub language: String,
|
||||
pub window: String,
|
||||
}
|
||||
|
||||
impl Context {
|
||||
/// The running app's context. `window` is the main window's size.
|
||||
pub fn current(dark: bool, language: &str, window: (i32, i32)) -> Context {
|
||||
Context {
|
||||
app_version: env!("CARGO_PKG_VERSION").to_string(),
|
||||
platform: format!("{}-{}", std::env::consts::OS, std::env::consts::ARCH),
|
||||
os: os_version(),
|
||||
appearance: if dark { "dark" } else { "light" }.to_string(),
|
||||
language: language.to_string(),
|
||||
window: format!("{}x{}", window.0, window.1),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A human-readable OS name and version, best effort.
|
||||
fn os_version() -> String {
|
||||
#[cfg(target_os = "linux")]
|
||||
{
|
||||
if let Ok(s) = std::fs::read_to_string("/etc/os-release") {
|
||||
for line in s.lines() {
|
||||
if let Some(v) = line.strip_prefix("PRETTY_NAME=") {
|
||||
return v.trim_matches('"').to_string();
|
||||
}
|
||||
}
|
||||
}
|
||||
"Linux".to_string()
|
||||
}
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
let out = alkane_core::platform::quiet_command("sw_vers")
|
||||
.arg("-productVersion")
|
||||
.output();
|
||||
match out {
|
||||
Ok(o) if o.status.success() => {
|
||||
format!("macOS {}", String::from_utf8_lossy(&o.stdout).trim())
|
||||
}
|
||||
_ => "macOS".to_string(),
|
||||
}
|
||||
}
|
||||
#[cfg(windows)]
|
||||
{
|
||||
let out = alkane_core::platform::quiet_command("cmd")
|
||||
.args(["/C", "ver"])
|
||||
.output();
|
||||
match out {
|
||||
Ok(o) if o.status.success() => {
|
||||
let v = String::from_utf8_lossy(&o.stdout).trim().to_string();
|
||||
if v.is_empty() {
|
||||
"Windows".to_string()
|
||||
} else {
|
||||
v
|
||||
}
|
||||
}
|
||||
_ => "Windows".to_string(),
|
||||
}
|
||||
}
|
||||
#[cfg(not(any(target_os = "linux", target_os = "macos", windows)))]
|
||||
{
|
||||
std::env::consts::OS.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
/// Names whose values are secrets: `name=value`, `name: value` and
|
||||
/// `"name": "value"` have the value replaced.
|
||||
const SECRET_NAMES: [&str; 12] = [
|
||||
"password",
|
||||
"passwd",
|
||||
"pass",
|
||||
"pwd",
|
||||
"secret",
|
||||
"token",
|
||||
"apikey",
|
||||
"api_key",
|
||||
"authorization",
|
||||
"cookie",
|
||||
"psk",
|
||||
"credential",
|
||||
];
|
||||
|
||||
/// Remove credentials from log text before it leaves the machine: values of
|
||||
/// secret-looking names, `user:password@` in URLs, and PEM private keys.
|
||||
/// What is removed becomes `[redacted]`.
|
||||
pub fn scrub(text: &str) -> String {
|
||||
let mut out = Vec::new();
|
||||
let mut in_pem = false;
|
||||
for line in text.lines() {
|
||||
if line.contains("-----BEGIN") && line.contains("PRIVATE KEY") {
|
||||
in_pem = true;
|
||||
out.push("[redacted]".to_string());
|
||||
continue;
|
||||
}
|
||||
if in_pem {
|
||||
if line.contains("-----END") {
|
||||
in_pem = false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
out.push(scrub_line(line));
|
||||
}
|
||||
out.join("\n")
|
||||
}
|
||||
|
||||
fn scrub_line(line: &str) -> String {
|
||||
let mut s = scrub_url_userinfo(line);
|
||||
let lower = s.to_ascii_lowercase();
|
||||
// Walk every occurrence of every secret name; redact the value after the
|
||||
// next `=` or `:` (quotes and spaces skipped) up to whitespace, a quote, a
|
||||
// comma, `&` or `;`.
|
||||
let mut cuts: Vec<(usize, usize)> = Vec::new();
|
||||
for name in SECRET_NAMES {
|
||||
let mut from = 0;
|
||||
while let Some(pos) = lower[from..].find(name) {
|
||||
let start = from + pos;
|
||||
from = start + name.len();
|
||||
let bytes = s.as_bytes();
|
||||
let mut i = start + name.len();
|
||||
// allow a closing quote and spaces between the name and the separator
|
||||
while i < bytes.len() && (bytes[i] == b'"' || bytes[i] == b'\'' || bytes[i] == b' ') {
|
||||
i += 1;
|
||||
}
|
||||
if i >= bytes.len() || (bytes[i] != b'=' && bytes[i] != b':') {
|
||||
continue;
|
||||
}
|
||||
i += 1;
|
||||
while i < bytes.len() && (bytes[i] == b' ' || bytes[i] == b'"' || bytes[i] == b'\'') {
|
||||
i += 1;
|
||||
}
|
||||
let vstart = i;
|
||||
if name == "authorization" || name == "cookie" {
|
||||
// "Authorization: Bearer <token>", "Cookie: a=b; c=d": the whole
|
||||
// rest of the line is the secret.
|
||||
i = bytes.len();
|
||||
}
|
||||
while i < bytes.len()
|
||||
&& !matches!(bytes[i], b' ' | b'"' | b'\'' | b',' | b'&' | b';' | b'\t')
|
||||
{
|
||||
i += 1;
|
||||
}
|
||||
if i > vstart {
|
||||
cuts.push((vstart, i));
|
||||
}
|
||||
}
|
||||
}
|
||||
if cuts.is_empty() {
|
||||
return s;
|
||||
}
|
||||
cuts.sort();
|
||||
let mut merged: Vec<(usize, usize)> = Vec::new();
|
||||
for (a, b) in cuts {
|
||||
match merged.last_mut() {
|
||||
Some(last) if a <= last.1 => last.1 = last.1.max(b),
|
||||
_ => merged.push((a, b)),
|
||||
}
|
||||
}
|
||||
for (a, b) in merged.into_iter().rev() {
|
||||
if s.is_char_boundary(a) && s.is_char_boundary(b) {
|
||||
s.replace_range(a..b, "[redacted]");
|
||||
}
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
/// `scheme://user:password@host` -> `scheme://user:[redacted]@host`.
|
||||
fn scrub_url_userinfo(line: &str) -> String {
|
||||
let mut s = line.to_string();
|
||||
let mut from = 0;
|
||||
while let Some(p) = s[from..].find("://") {
|
||||
let auth_start = from + p + 3;
|
||||
let rest = &s[auth_start..];
|
||||
let end = rest
|
||||
.find(|c: char| c == '/' || c.is_whitespace())
|
||||
.unwrap_or(rest.len());
|
||||
let authority = &rest[..end];
|
||||
if let Some(at) = authority.rfind('@') {
|
||||
if let Some(colon) = authority[..at].find(':') {
|
||||
let a = auth_start + colon + 1;
|
||||
let b = auth_start + at;
|
||||
s.replace_range(a..b, "[redacted]");
|
||||
}
|
||||
}
|
||||
from = auth_start;
|
||||
if from >= s.len() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
/// The newest activity-log lines, scrubbed and cut to the size limits.
|
||||
pub fn recent_log(full_log: &str) -> String {
|
||||
let lines: Vec<&str> = full_log.lines().collect();
|
||||
let tail = &lines[lines.len().saturating_sub(LOG_LINES)..];
|
||||
let mut text = scrub(&tail.join("\n"));
|
||||
if text.len() > LOG_BYTES {
|
||||
let mut cut = text.len() - LOG_BYTES;
|
||||
while !text.is_char_boundary(cut) {
|
||||
cut += 1;
|
||||
}
|
||||
text = text[cut..].to_string();
|
||||
if let Some(nl) = text.find('\n') {
|
||||
text = text[nl + 1..].to_string();
|
||||
}
|
||||
}
|
||||
text
|
||||
}
|
||||
|
||||
/// The JSON body the relay takes.
|
||||
pub fn payload(kind: Kind, text: &str, ctx: &Context, log: Option<&str>) -> String {
|
||||
serde_json::json!({
|
||||
"kind": kind.as_str(),
|
||||
"text": text,
|
||||
"app_version": ctx.app_version,
|
||||
"platform": ctx.platform,
|
||||
"os": ctx.os,
|
||||
"appearance": ctx.appearance,
|
||||
"language": ctx.language,
|
||||
"window": ctx.window,
|
||||
"log": log.unwrap_or(""),
|
||||
})
|
||||
.to_string()
|
||||
}
|
||||
|
||||
/// A filed issue: its number and web link.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Filed {
|
||||
pub number: u64,
|
||||
pub url: String,
|
||||
}
|
||||
|
||||
/// Read the relay's answer: curl's body, then the HTTP status on the last line.
|
||||
pub fn parse_response(curl_stdout: &str) -> Result<Filed, String> {
|
||||
let trimmed = curl_stdout.trim_end();
|
||||
let (body, code) = match trimmed.rfind('\n') {
|
||||
Some(i) => (&trimmed[..i], trimmed[i + 1..].trim()),
|
||||
None => ("", trimmed),
|
||||
};
|
||||
let json: Option<serde_json::Value> = serde_json::from_str(body).ok();
|
||||
if code == "201" || code == "200" {
|
||||
let number = json.as_ref().and_then(|j| j["number"].as_u64());
|
||||
let url = json.as_ref().and_then(|j| j["url"].as_str()).unwrap_or("");
|
||||
return match number {
|
||||
Some(number) => Ok(Filed {
|
||||
number,
|
||||
url: url.to_string(),
|
||||
}),
|
||||
None => Err("the report service answered without an issue number; \
|
||||
check the issue list before sending again"
|
||||
.into()),
|
||||
};
|
||||
}
|
||||
let reason = json
|
||||
.as_ref()
|
||||
.and_then(|j| j["error"].as_str())
|
||||
.map(str::to_string);
|
||||
Err(match (code, reason) {
|
||||
(_, Some(r)) => r,
|
||||
("000", None) | ("", None) => "could not reach the report service".into(),
|
||||
(c, None) => format!("the report service answered HTTP {c}"),
|
||||
})
|
||||
}
|
||||
|
||||
/// Send a report on a worker thread; the receiver gets one result.
|
||||
pub fn send(body: String) -> Receiver<Result<Filed, String>> {
|
||||
let url = std::env::var("ALKANE_REPORT_URL").unwrap_or_else(|_| DEFAULT_URL.to_string());
|
||||
let (tx, rx) = channel();
|
||||
std::thread::spawn(move || {
|
||||
let result = (|| {
|
||||
let mut child = alkane_core::platform::quiet_command("curl")
|
||||
.args([
|
||||
"-sS",
|
||||
"--max-time",
|
||||
"40",
|
||||
"-X",
|
||||
"POST",
|
||||
"-H",
|
||||
"Content-Type: application/json",
|
||||
"--data-binary",
|
||||
"@-",
|
||||
"-w",
|
||||
"\n%{http_code}",
|
||||
&url,
|
||||
])
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped())
|
||||
.spawn()
|
||||
.map_err(|e| format!("could not run curl: {e}"))?;
|
||||
if let Some(mut si) = child.stdin.take() {
|
||||
let _ = si.write_all(body.as_bytes());
|
||||
}
|
||||
let out = child
|
||||
.wait_with_output()
|
||||
.map_err(|e| format!("curl failed: {e}"))?;
|
||||
parse_response(&String::from_utf8_lossy(&out.stdout))
|
||||
})();
|
||||
let _ = tx.send(result);
|
||||
});
|
||||
rx
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn scrub_removes_credentials_and_keeps_the_rest() {
|
||||
let log = "12:00:01 set pools on 192.168.1.20: url=stratum+tcp://u:hunter2@pool:3333 password=hunter2\n\
|
||||
12:00:02 login {\"user\": \"root\", \"pass\": \"s3cret\"}\n\
|
||||
12:00:03 Authorization: Bearer abc.def\n\
|
||||
-----BEGIN OPENSSH PRIVATE KEY-----\nAAAA\n-----END OPENSSH PRIVATE KEY-----\n\
|
||||
12:00:04 scan finished: 9 miners";
|
||||
let s = scrub(log);
|
||||
assert!(!s.contains("hunter2"), "{s}");
|
||||
assert!(!s.contains("s3cret"), "{s}");
|
||||
assert!(!s.contains("abc.def"), "{s}");
|
||||
assert!(!s.contains("AAAA"), "{s}");
|
||||
assert!(s.contains("192.168.1.20"));
|
||||
assert!(s.contains("\"user\": \"root\""));
|
||||
assert!(s.contains("scan finished: 9 miners"));
|
||||
assert_eq!(s.matches("[redacted]").count(), 5, "{s}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn recent_log_keeps_the_newest_lines_within_limits() {
|
||||
let log: Vec<String> = (0..500)
|
||||
.map(|i| format!("line {i} {}", "x".repeat(300)))
|
||||
.collect();
|
||||
let tail = recent_log(&log.join("\n"));
|
||||
assert!(tail.len() <= LOG_BYTES);
|
||||
assert!(tail.ends_with(&log[499]));
|
||||
assert!(tail.starts_with("line "), "cut on a line boundary");
|
||||
assert!(!tail.contains("line 400 "), "only the newest lines");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn payload_carries_kind_text_and_context() {
|
||||
let ctx = Context {
|
||||
app_version: "1.2.3".into(),
|
||||
platform: "macos-aarch64".into(),
|
||||
os: "macOS 15.1".into(),
|
||||
appearance: "dark".into(),
|
||||
language: "en".into(),
|
||||
window: "1280x800".into(),
|
||||
};
|
||||
let v: serde_json::Value =
|
||||
serde_json::from_str(&payload(Kind::Ui, "Text \"quoted\"", &ctx, None)).unwrap();
|
||||
assert_eq!(v["kind"], "ui");
|
||||
assert_eq!(v["text"], "Text \"quoted\"");
|
||||
assert_eq!(v["app_version"], "1.2.3");
|
||||
assert_eq!(v["log"], "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn response_parsing() {
|
||||
assert_eq!(
|
||||
parse_response("{\"number\":42,\"url\":\"https://x/42\"}\n201"),
|
||||
Ok(Filed {
|
||||
number: 42,
|
||||
url: "https://x/42".into()
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
parse_response("{\"error\":\"Too many reports; try again later\"}\n429"),
|
||||
Err("Too many reports; try again later".into())
|
||||
);
|
||||
assert_eq!(
|
||||
parse_response("\n000"),
|
||||
Err("could not reach the report service".into())
|
||||
);
|
||||
assert_eq!(
|
||||
parse_response("<html>\n502"),
|
||||
Err("the report service answered HTTP 502".into())
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -6,11 +6,11 @@
|
||||
//! firmware registry, so `scan`'s callers never have to think about it.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::io::Write;
|
||||
use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};
|
||||
|
||||
use asic_rs::MinerFactory;
|
||||
use alkane_core::platform::write_private;
|
||||
|
||||
use asic_rs::core::traits::miner::MinerAuth;
|
||||
use asic_rs::MinerFactory;
|
||||
|
||||
use crate::scan::config_path;
|
||||
|
||||
@@ -40,20 +40,9 @@ fn save_creds_file(file: &str, user: &str, pass: &str) -> std::io::Result<()> {
|
||||
if let Some(dir) = p.parent() {
|
||||
std::fs::create_dir_all(dir)?;
|
||||
}
|
||||
// Open (and create) with mode 0600 up front so a freshly created file is
|
||||
// never world/group-readable, even for the instant before the write. For a
|
||||
// pre-existing file the create mode is ignored, so also tighten it to 0600
|
||||
// while it is still empty (post-truncate, pre-write) -- the plaintext is
|
||||
// never on disk under a looser mode.
|
||||
let mut f = std::fs::OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
.truncate(true)
|
||||
.mode(0o600)
|
||||
.open(&p)?;
|
||||
f.set_permissions(std::fs::Permissions::from_mode(0o600))?;
|
||||
f.write_all(format!("{user}\n{pass}\n").as_bytes())?;
|
||||
Ok(())
|
||||
// Owner-only from creation on (0600 on Unix, re-tightened if the file
|
||||
// already existed): the plaintext is never on disk under a looser mode.
|
||||
write_private(&p, format!("{user}\n{pass}\n").as_bytes())
|
||||
}
|
||||
|
||||
fn clear_creds_file(file: &str) -> std::io::Result<()> {
|
||||
@@ -80,13 +69,26 @@ pub fn load_ssh_credentials() -> Option<(String, String)> {
|
||||
load_creds_file(SSH_CREDENTIALS_FILE)
|
||||
}
|
||||
|
||||
/// Save the SSH username/password (0600). Retained for a future SSH-login dialog
|
||||
/// now that the SSH PIC probe that used to save it has been removed (round 14 #5).
|
||||
#[allow(dead_code)]
|
||||
/// Save the SSH username/password (0600), from the Credentials dialog.
|
||||
pub fn save_ssh_credentials(user: &str, pass: &str) -> std::io::Result<()> {
|
||||
save_creds_file(SSH_CREDENTIALS_FILE, user, pass)
|
||||
}
|
||||
|
||||
/// Remove the saved SSH login, if any. Not an error if none was saved.
|
||||
pub fn clear_ssh_credentials() -> std::io::Result<()> {
|
||||
clear_creds_file(SSH_CREDENTIALS_FILE)
|
||||
}
|
||||
|
||||
/// The SSH username to use: the saved one, or `root` (the control-board login
|
||||
/// on every firmware the restore flow drives over SSH) when none was entered.
|
||||
pub fn ssh_user_or_default(user: &str) -> String {
|
||||
if user.trim().is_empty() {
|
||||
"root".to_string()
|
||||
} else {
|
||||
user.trim().to_string()
|
||||
}
|
||||
}
|
||||
|
||||
/// Remove the saved web/API credentials, if any. Not an error if none were saved.
|
||||
pub fn clear_credentials() -> std::io::Result<()> {
|
||||
match clear_creds_file(CREDENTIALS_FILE) {
|
||||
@@ -164,20 +166,14 @@ pub fn save_firmware_credentials(map: &HashMap<String, (String, String)>) -> std
|
||||
if let Some(dir) = p.parent() {
|
||||
std::fs::create_dir_all(dir)?;
|
||||
}
|
||||
let mut f = std::fs::OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
.truncate(true)
|
||||
.mode(0o600)
|
||||
.open(&p)?;
|
||||
f.set_permissions(std::fs::Permissions::from_mode(0o600))?;
|
||||
let mut names: Vec<&String> = map.keys().collect();
|
||||
names.sort();
|
||||
let mut body = String::new();
|
||||
for name in names {
|
||||
let (user, pass) = &map[name];
|
||||
f.write_all(format!("{name}\t{user}\t{pass}\n").as_bytes())?;
|
||||
body.push_str(&format!("{name}\t{user}\t{pass}\n"));
|
||||
}
|
||||
Ok(())
|
||||
write_private(&p, body.as_bytes())
|
||||
}
|
||||
|
||||
/// Remove the per-firmware overrides file, if any. Not an error if none exists.
|
||||
@@ -191,7 +187,12 @@ pub fn clear_firmware_credentials() -> std::io::Result<()> {
|
||||
/// otherwise the backend falls back to its own default auth. With nothing saved
|
||||
/// this is plain `MinerFactory::new()`.
|
||||
pub fn configured_factory() -> MinerFactory {
|
||||
let mut factory = MinerFactory::new();
|
||||
// Apply the operator's configured identification timeout to every factory
|
||||
// (round 18 #12: config actions / refresh / pools / flash previously used
|
||||
// asic-rs's 10s default, so on a slow link they could fail to reach miners a
|
||||
// longer-timeout scan had found). start_scan may still override it explicitly.
|
||||
let mut factory = MinerFactory::new()
|
||||
.with_identification_timeout_secs(crate::scan::load_scan_timeout().max(1));
|
||||
let global = load_credentials();
|
||||
let per_fw = load_firmware_credentials();
|
||||
if global.is_none() && per_fw.is_empty() {
|
||||
@@ -213,24 +214,47 @@ pub fn configured_factory() -> MinerFactory {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// Unix: the file is mode 0600. Windows relies on the profile directory's
|
||||
/// owner-only ACL (see `alkane_core::platform::write_private`), so only
|
||||
/// existence is checked there.
|
||||
fn assert_owner_only(path: &std::path::Path, why: &str) {
|
||||
let meta = std::fs::metadata(path).unwrap();
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
assert_eq!(meta.permissions().mode() & 0o777, 0o600, "{why}");
|
||||
}
|
||||
#[cfg(not(unix))]
|
||||
assert!(meta.is_file(), "{why}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn credentials_round_trip_and_file_perms() {
|
||||
// config_path() reads $HOME; point it at a private temp dir. HOME is
|
||||
// process-global, so this stays one test and restores HOME afterward
|
||||
// (matching the pattern used by scan::tests::auto_secs_round_trip_and_defaults).
|
||||
let prev = std::env::var_os("HOME");
|
||||
// process-global, so serialise against other HOME-mutating tests via the
|
||||
// shared lock and restore HOME afterward (round 18 #12).
|
||||
let _home = crate::model::HOME_TEST_LOCK
|
||||
.lock()
|
||||
.unwrap_or_else(|e| e.into_inner());
|
||||
let root_var = alkane_core::platform::CONFIG_ROOT_VAR;
|
||||
let prev = std::env::var_os(root_var);
|
||||
let dir = std::env::temp_dir().join(format!("alkane-cred-test-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
std::env::set_var("HOME", &dir);
|
||||
std::env::set_var(root_var, &dir);
|
||||
|
||||
assert_eq!(load_credentials(), None, "no file -> None");
|
||||
|
||||
save_credentials("admin", "hunter2").unwrap();
|
||||
assert_eq!(load_credentials(), Some(("admin".to_string(), "hunter2".to_string())));
|
||||
assert_eq!(
|
||||
load_credentials(),
|
||||
Some(("admin".to_string(), "hunter2".to_string()))
|
||||
);
|
||||
|
||||
let meta = std::fs::metadata(config_path(CREDENTIALS_FILE)).unwrap();
|
||||
assert_eq!(meta.permissions().mode() & 0o777, 0o600, "credentials file must be 0600");
|
||||
assert_owner_only(
|
||||
&config_path(CREDENTIALS_FILE),
|
||||
"credentials file must be 0600",
|
||||
);
|
||||
|
||||
// configured_factory() must construct in both branches without panicking:
|
||||
// with saved credentials (fans auth across every firmware backend) ...
|
||||
@@ -240,15 +264,20 @@ mod tests {
|
||||
// 0600 by save_credentials (the create-mode is ignored for an existing
|
||||
// file, so the explicit set_permissions is what protects it).
|
||||
let path = config_path(CREDENTIALS_FILE);
|
||||
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o644)).unwrap();
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o644)).unwrap();
|
||||
}
|
||||
save_credentials("admin2", "s3cr3t").unwrap();
|
||||
let meta = std::fs::metadata(&path).unwrap();
|
||||
assert_owner_only(
|
||||
&path,
|
||||
"save over a pre-existing 0644 file must re-tighten to 0600",
|
||||
);
|
||||
assert_eq!(
|
||||
meta.permissions().mode() & 0o777,
|
||||
0o600,
|
||||
"save over a pre-existing 0644 file must re-tighten to 0600"
|
||||
load_credentials(),
|
||||
Some(("admin2".to_string(), "s3cr3t".to_string()))
|
||||
);
|
||||
assert_eq!(load_credentials(), Some(("admin2".to_string(), "s3cr3t".to_string())));
|
||||
|
||||
clear_credentials().unwrap();
|
||||
// ... and with no saved credentials (plain MinerFactory::new()).
|
||||
@@ -256,38 +285,74 @@ mod tests {
|
||||
assert_eq!(load_credentials(), None, "cleared -> None");
|
||||
clear_credentials().unwrap(); // idempotent: clearing an already-clear store is not an error
|
||||
|
||||
// SSH login: separate file, same 0600 rule, clear is idempotent.
|
||||
assert_eq!(load_ssh_credentials(), None, "no SSH file -> None");
|
||||
save_ssh_credentials("root", "ssh-pass").unwrap();
|
||||
assert_owner_only(
|
||||
&config_path(SSH_CREDENTIALS_FILE),
|
||||
"SSH creds file must be 0600",
|
||||
);
|
||||
assert_eq!(
|
||||
load_ssh_credentials(),
|
||||
Some(("root".to_string(), "ssh-pass".to_string()))
|
||||
);
|
||||
assert_eq!(
|
||||
load_credentials(),
|
||||
None,
|
||||
"SSH save leaves the web login alone"
|
||||
);
|
||||
clear_ssh_credentials().unwrap();
|
||||
assert_eq!(load_ssh_credentials(), None, "cleared -> None");
|
||||
clear_ssh_credentials().unwrap();
|
||||
assert_eq!(ssh_user_or_default(""), "root");
|
||||
assert_eq!(ssh_user_or_default(" admin "), "admin");
|
||||
|
||||
// Per-firmware overrides (round 12 #13). Kept in this same test because
|
||||
// HOME is process-global: a second #[test] mutating it would race under
|
||||
// cargo's default parallel runner.
|
||||
assert!(load_firmware_credentials().is_empty(), "no file -> empty map");
|
||||
assert!(
|
||||
load_firmware_credentials().is_empty(),
|
||||
"no file -> empty map"
|
||||
);
|
||||
let mut fw = HashMap::new();
|
||||
fw.insert("WhatsMiner Stock".to_string(), ("super".to_string(), "sup3r".to_string()));
|
||||
fw.insert(
|
||||
"WhatsMiner Stock".to_string(),
|
||||
("super".to_string(), "sup3r".to_string()),
|
||||
);
|
||||
fw.insert("VNish".to_string(), ("".to_string(), "hunter2".to_string()));
|
||||
save_firmware_credentials(&fw).unwrap();
|
||||
// 0600, and it round-trips (an empty username survives the tab format).
|
||||
let fwpath = config_path(FIRMWARE_CREDENTIALS_FILE);
|
||||
assert_eq!(
|
||||
std::fs::metadata(&fwpath).unwrap().permissions().mode() & 0o777,
|
||||
0o600,
|
||||
"per-firmware creds file must be 0600"
|
||||
);
|
||||
assert_owner_only(&fwpath, "per-firmware creds file must be 0600");
|
||||
let back = load_firmware_credentials();
|
||||
assert_eq!(back.get("WhatsMiner Stock"), Some(&("super".into(), "sup3r".into())));
|
||||
assert_eq!(
|
||||
back.get("WhatsMiner Stock"),
|
||||
Some(&("super".into(), "sup3r".into()))
|
||||
);
|
||||
assert_eq!(back.get("VNish"), Some(&("".into(), "hunter2".into())));
|
||||
assert_eq!(back.len(), 2);
|
||||
// Shipped-default lookup: known firmware resolves, unknown blanks.
|
||||
assert_eq!(firmware_default_for("AntMiner Stock"), ("root".into(), "root".into()));
|
||||
assert_eq!(firmware_default_for("nonexistent"), (String::new(), String::new()));
|
||||
assert_eq!(
|
||||
firmware_default_for("AntMiner Stock"),
|
||||
("root".into(), "root".into())
|
||||
);
|
||||
assert_eq!(
|
||||
firmware_default_for("nonexistent"),
|
||||
(String::new(), String::new())
|
||||
);
|
||||
// Overrides + no global still builds a factory.
|
||||
let _ = configured_factory();
|
||||
// Saving an empty map clears the file (no stale overrides linger).
|
||||
save_firmware_credentials(&HashMap::new()).unwrap();
|
||||
assert!(load_firmware_credentials().is_empty(), "empty save -> cleared");
|
||||
assert!(
|
||||
load_firmware_credentials().is_empty(),
|
||||
"empty save -> cleared"
|
||||
);
|
||||
clear_firmware_credentials().unwrap(); // idempotent
|
||||
|
||||
match prev {
|
||||
Some(v) => std::env::set_var("HOME", v),
|
||||
None => std::env::remove_var("HOME"),
|
||||
Some(v) => std::env::set_var(root_var, v),
|
||||
None => std::env::remove_var(root_var),
|
||||
}
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
@@ -9,13 +9,43 @@
|
||||
|
||||
use crate::model::{BoardHealth, MinerRow};
|
||||
|
||||
/// Escape one CSV field: quote it when it contains a comma, quote, CR or LF, and
|
||||
/// double any embedded quotes.
|
||||
/// A compact UTC timestamp "YYYYMMDD-HHMMSS" for a default export filename
|
||||
/// (round 19 #5). UTC keeps this dependency-free (no chrono) and unambiguous.
|
||||
pub fn timestamp_utc() -> String {
|
||||
let secs = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map(|d| d.as_secs())
|
||||
.unwrap_or(0);
|
||||
let sod = secs % 86_400;
|
||||
let (h, mi, s) = (sod / 3600, (sod % 3600) / 60, sod % 60);
|
||||
// Civil date from days since 1970-01-01 (Howard Hinnant's civil_from_days).
|
||||
let z = (secs / 86_400) as i64 + 719_468;
|
||||
let era = if z >= 0 { z } else { z - 146_096 } / 146_097;
|
||||
let doe = z - era * 146_097; // [0, 146096]
|
||||
let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365; // [0, 399]
|
||||
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); // [0, 365]
|
||||
let mp = (5 * doy + 2) / 153; // [0, 11]
|
||||
let d = doy - (153 * mp + 2) / 5 + 1; // [1, 31]
|
||||
let m = if mp < 10 { mp + 3 } else { mp - 9 }; // [1, 12]
|
||||
let y = yoe + era * 400 + i64::from(m <= 2);
|
||||
format!("{y:04}{m:02}{d:02}-{h:02}{mi:02}{s:02}")
|
||||
}
|
||||
|
||||
/// Escape one CSV field: neutralise spreadsheet formula injection, then quote it
|
||||
/// when it contains a comma, quote, CR or LF (doubling any embedded quotes).
|
||||
///
|
||||
/// Miner-supplied fields (hostname, worker, serial, ...) can start with a formula
|
||||
/// trigger; prefixing a single quote makes Excel/Sheets treat the cell as text so
|
||||
/// opening the export can't execute an injected formula (round 18 #12).
|
||||
fn esc(s: &str) -> String {
|
||||
if s.contains([',', '"', '\n', '\r']) {
|
||||
format!("\"{}\"", s.replace('"', "\"\""))
|
||||
let mut val = s.to_string();
|
||||
if val.starts_with(['=', '+', '-', '@', '\t', '\r']) {
|
||||
val.insert(0, '\'');
|
||||
}
|
||||
if val.contains([',', '"', '\n', '\r']) {
|
||||
format!("\"{}\"", val.replace('"', "\"\""))
|
||||
} else {
|
||||
s.to_string()
|
||||
val
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,41 +100,116 @@ fn scalar_columns() -> Vec<Col> {
|
||||
("slow", |r| r.slow.to_string()),
|
||||
("paused", |r| r.paused.to_string()),
|
||||
("pic", |r| opt_bool(r.pic_present, "PIC", "noPIC")),
|
||||
("write_locked", |r| opt_bool(r.write_locked, "locked", "unlocked")),
|
||||
("light_flashing", |r| opt_bool(r.light_flashing, "on", "off")),
|
||||
("write_locked", |r| {
|
||||
opt_bool(r.write_locked, "locked", "unlocked")
|
||||
}),
|
||||
("light_flashing", |r| {
|
||||
opt_bool(r.light_flashing, "on", "off")
|
||||
}),
|
||||
("hashrate_ths", |r| num(r.hashrate_ths)),
|
||||
("expected_ths", |r| num(r.expected_ths)),
|
||||
("efficiency_j_th", |r| num(r.efficiency)),
|
||||
("power_w", |r| num(r.power_w)),
|
||||
("tuning_percent", |r| r.tuning_percent.map(|v| v.to_string()).unwrap_or_default()),
|
||||
("tuning_percent", |r| {
|
||||
r.tuning_percent.map(|v| v.to_string()).unwrap_or_default()
|
||||
}),
|
||||
("chip_temp_c", |r| num(r.chip_temp_c)),
|
||||
("fluid_temp_c", |r| num(r.fluid_temp_c)),
|
||||
("env_temp_c", |r| num(r.env_temp_c)),
|
||||
("outlet_temp_c", |r| num(r.outlet_temp_c)),
|
||||
("psu_fan_rpm", |r| num(r.psu_fan_rpm)),
|
||||
("fan_count", |r| r.fan_count.map(|v| v.to_string()).unwrap_or_default()),
|
||||
("expected_fans", |r| r.expected_fans.map(|v| v.to_string()).unwrap_or_default()),
|
||||
("total_chips", |r| r.total_chips.map(|v| v.to_string()).unwrap_or_default()),
|
||||
("expected_chips", |r| r.expected_chips.map(|v| v.to_string()).unwrap_or_default()),
|
||||
("hashboards", |r| r.hashboards.map(|v| v.to_string()).unwrap_or_default()),
|
||||
("expected_hashboards", |r| r.expected_hashboards.map(|v| v.to_string()).unwrap_or_default()),
|
||||
("uptime_secs", |r| if r.uptime_secs > 0 { r.uptime_secs.to_string() } else { String::new() }),
|
||||
("fan_count", |r| {
|
||||
r.fan_count.map(|v| v.to_string()).unwrap_or_default()
|
||||
}),
|
||||
("expected_fans", |r| {
|
||||
r.expected_fans.map(|v| v.to_string()).unwrap_or_default()
|
||||
}),
|
||||
("total_chips", |r| {
|
||||
r.total_chips.map(|v| v.to_string()).unwrap_or_default()
|
||||
}),
|
||||
("expected_chips", |r| {
|
||||
r.expected_chips.map(|v| v.to_string()).unwrap_or_default()
|
||||
}),
|
||||
("hashboards", |r| {
|
||||
r.hashboards.map(|v| v.to_string()).unwrap_or_default()
|
||||
}),
|
||||
("expected_hashboards", |r| {
|
||||
r.expected_hashboards
|
||||
.map(|v| v.to_string())
|
||||
.unwrap_or_default()
|
||||
}),
|
||||
("uptime_secs", |r| {
|
||||
if r.uptime_secs > 0 {
|
||||
r.uptime_secs.to_string()
|
||||
} else {
|
||||
String::new()
|
||||
}
|
||||
}),
|
||||
("best_share", |r| r.best_share.map(num).unwrap_or_default()),
|
||||
("session_best_share", |r| r.session_best_share.map(num).unwrap_or_default()),
|
||||
("pool_account", |r| r.worker.split_once('.').map(|(a, _)| a).unwrap_or(&r.worker).to_string()),
|
||||
("session_best_share", |r| {
|
||||
r.session_best_share.map(num).unwrap_or_default()
|
||||
}),
|
||||
("pool_account", |r| {
|
||||
r.worker
|
||||
.split_once('.')
|
||||
.map(|(a, _)| a)
|
||||
.unwrap_or(&r.worker)
|
||||
.to_string()
|
||||
}),
|
||||
("pool_url", |r| r.pool_url.clone()),
|
||||
("errors", |r| r.errors.join("; ")),
|
||||
]
|
||||
}
|
||||
|
||||
/// Build the full CSV for `rows`. Empty rows still emit a header line.
|
||||
pub fn rows_to_csv(rows: &[MinerRow]) -> String {
|
||||
let cols = scalar_columns();
|
||||
// Widest variable-length fields across the set.
|
||||
let max_board_temps = rows.iter().map(|r| r.board_temps.len()).max().unwrap_or(0);
|
||||
let max_fans = rows.iter().map(|r| r.fan_rpms.len()).max().unwrap_or(0);
|
||||
let max_boards = rows.iter().map(|r| r.board_status.len()).max().unwrap_or(0);
|
||||
let max_pools = rows.iter().map(|r| r.pools.len()).max().unwrap_or(0);
|
||||
// Group keys for the four variable-length field families (each expands to
|
||||
// per-index columns). Shown as single checkboxes in the export picker (round 19 #6).
|
||||
pub const GROUP_BOARD_TEMPS: &str = "board temps";
|
||||
pub const GROUP_FANS: &str = "fan rpms";
|
||||
pub const GROUP_BOARD_STATUS: &str = "board status";
|
||||
pub const GROUP_POOLS: &str = "pools";
|
||||
|
||||
/// Every selectable export field, in output order: each scalar column header,
|
||||
/// then the four variable-length group keys.
|
||||
pub fn export_field_names() -> Vec<String> {
|
||||
let mut v: Vec<String> = scalar_columns()
|
||||
.iter()
|
||||
.map(|(h, _)| (*h).to_string())
|
||||
.collect();
|
||||
v.push(GROUP_BOARD_TEMPS.to_string());
|
||||
v.push(GROUP_FANS.to_string());
|
||||
v.push(GROUP_BOARD_STATUS.to_string());
|
||||
v.push(GROUP_POOLS.to_string());
|
||||
v
|
||||
}
|
||||
|
||||
/// Build the CSV for `rows` including only the selected fields (scalar column
|
||||
/// headers and/or the group keys above), in the fixed output order (round 19 #6).
|
||||
/// Empty rows still emit a header line.
|
||||
pub fn rows_to_csv_selected(
|
||||
rows: &[MinerRow],
|
||||
selected: &std::collections::HashSet<String>,
|
||||
) -> String {
|
||||
let cols: Vec<Col> = scalar_columns()
|
||||
.into_iter()
|
||||
.filter(|(h, _)| selected.contains(*h))
|
||||
.collect();
|
||||
// A group's columns appear only when it is selected; an unselected group's
|
||||
// width stays 0, so the header/value loops below emit nothing for it.
|
||||
let width = |want: bool, f: &dyn Fn(&MinerRow) -> usize| -> usize {
|
||||
if want {
|
||||
rows.iter().map(f).max().unwrap_or(0)
|
||||
} else {
|
||||
0
|
||||
}
|
||||
};
|
||||
let max_board_temps = width(selected.contains(GROUP_BOARD_TEMPS), &|r| {
|
||||
r.board_temps.len()
|
||||
});
|
||||
let max_fans = width(selected.contains(GROUP_FANS), &|r| r.fan_rpms.len());
|
||||
let max_boards = width(selected.contains(GROUP_BOARD_STATUS), &|r| {
|
||||
r.board_status.len()
|
||||
});
|
||||
let max_pools = width(selected.contains(GROUP_POOLS), &|r| r.pools.len());
|
||||
|
||||
// Header.
|
||||
let mut headers: Vec<String> = cols.iter().map(|(h, _)| (*h).to_string()).collect();
|
||||
@@ -135,7 +240,13 @@ pub fn rows_to_csv(rows: &[MinerRow]) -> String {
|
||||
vals.push(r.fan_rpms.get(i).map(|&t| num(t)).unwrap_or_default());
|
||||
}
|
||||
for i in 0..max_boards {
|
||||
vals.push(r.board_status.get(i).map(board_health_str).unwrap_or("").to_string());
|
||||
vals.push(
|
||||
r.board_status
|
||||
.get(i)
|
||||
.map(board_health_str)
|
||||
.unwrap_or("")
|
||||
.to_string(),
|
||||
);
|
||||
}
|
||||
for i in 0..max_pools {
|
||||
let (u, w) = r.pools.get(i).cloned().unwrap_or_default();
|
||||
@@ -164,9 +275,14 @@ mod tests {
|
||||
fan_rpms: vec![6000.0, 6100.0],
|
||||
..Default::default()
|
||||
},
|
||||
MinerRow { ip: "10.0.0.2".into(), model: "M30S".into(), ..Default::default() },
|
||||
MinerRow {
|
||||
ip: "10.0.0.2".into(),
|
||||
model: "M30S".into(),
|
||||
..Default::default()
|
||||
},
|
||||
];
|
||||
let csv = rows_to_csv(&rows);
|
||||
let all: std::collections::HashSet<String> = export_field_names().into_iter().collect();
|
||||
let csv = rows_to_csv_selected(&rows, &all);
|
||||
let lines: Vec<&str> = csv.lines().collect();
|
||||
assert_eq!(lines.len(), 3, "header + 2 rows");
|
||||
assert!(lines[0].starts_with("ip,model,brand,"));
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//! Bench-fleet fixtures: the expected hardware output of the 9 boards on
|
||||
//! 10.255.1.0/24 (see ../../../HANDOFF.md section 7 and the parent project's
|
||||
//! [removed]). These are the reference values the workflow tests compare
|
||||
//! Fixture fleet: the expected hardware output of nine representative miners
|
||||
//! (one per control-board family / firmware). These are the reference values
|
||||
//! the workflow tests compare
|
||||
//! the application's actual output against, and the rows the offline
|
||||
//! `ALKANE_FAKE_SCAN` demo mode renders. Only miners that respond become rows,
|
||||
//! so there is no synthetic "offline" fixture.
|
||||
@@ -42,9 +42,22 @@ impl Board {
|
||||
let slow = is_slow(self.mining, self.hashrate, self.expected);
|
||||
// A few extra metrics so the demo/offline mode shows non-empty values
|
||||
// for some of the additional columns.
|
||||
let last_octet: u8 = self.ip.rsplit('.').next().and_then(|s| s.parse().ok()).unwrap_or(0);
|
||||
let last_octet: u8 = self
|
||||
.ip
|
||||
.rsplit('.')
|
||||
.next()
|
||||
.and_then(|s| s.parse().ok())
|
||||
.unwrap_or(0);
|
||||
let (boards, chips, efficiency) = if self.mining {
|
||||
(Some(3u32), Some(210u32), if self.hashrate > 0.0 { self.power / self.hashrate } else { 0.0 })
|
||||
(
|
||||
Some(3u32),
|
||||
Some(210u32),
|
||||
if self.hashrate > 0.0 {
|
||||
self.power / self.hashrate
|
||||
} else {
|
||||
0.0
|
||||
},
|
||||
)
|
||||
} else {
|
||||
(None, None, 0.0)
|
||||
};
|
||||
@@ -63,10 +76,20 @@ impl Board {
|
||||
// Per-board hashrate for the R2 Health tooltip demo: split the total three
|
||||
// ways with a little variety (index-aligned with the 3 boards).
|
||||
let board_hashrates = if self.mining && self.hashrate > 0.0 {
|
||||
vec![self.hashrate * 0.34, self.hashrate * 0.35, self.hashrate * 0.31]
|
||||
vec![
|
||||
self.hashrate * 0.34,
|
||||
self.hashrate * 0.35,
|
||||
self.hashrate * 0.31,
|
||||
]
|
||||
} else {
|
||||
vec![]
|
||||
};
|
||||
// Per-board configured frequency/voltage for the G7 read-side tooltip demo.
|
||||
let (board_frequencies, board_voltages) = if self.mining {
|
||||
(vec![590.0, 590.0, 565.0], vec![13.50, 13.50, 13.35])
|
||||
} else {
|
||||
(vec![], vec![])
|
||||
};
|
||||
// Air-cooled WhatsMiners report an ambient reading (asic-rs surfaces it in
|
||||
// its fluid_temperature field); the scan routes it to env_temp for
|
||||
// air-cooled models, so the demo mirrors that (round 11 #17).
|
||||
@@ -86,6 +109,11 @@ impl Board {
|
||||
"10.255.1.166" => vec![Ok, Ok, Missing],
|
||||
_ => vec![Ok, Ok, Ok],
|
||||
}
|
||||
} else if self.paused {
|
||||
// A paused miner's boards are physically present but idle (asic-rs
|
||||
// reports active=false -> Fault), so the Health column shows them blue
|
||||
// rather than red (round 19 #19); the offline demo mirrors that.
|
||||
vec![Fault, Fault, Fault]
|
||||
} else {
|
||||
vec![]
|
||||
};
|
||||
@@ -104,6 +132,8 @@ impl Board {
|
||||
let control_board = crate::naming::control_board_code(self.make, self.board, &model);
|
||||
let cpu = crate::naming::control_board_cpu(self.make, self.board, &model);
|
||||
let board_code = crate::naming::control_board_board(self.make, self.board, &model);
|
||||
// Computed before `model` is moved into the struct literal below.
|
||||
let pic_present = crate::scan::infer_pic(&model);
|
||||
MinerRow {
|
||||
ip: self.ip.to_string(),
|
||||
selected: false,
|
||||
@@ -115,8 +145,14 @@ impl Board {
|
||||
cpu,
|
||||
board_code,
|
||||
hashrate_ths: self.hashrate,
|
||||
// A miner that is mining or paused reported a current hashrate (a paused
|
||||
// one reports 0 -> "0.00T"); the blank stock demo miner reports none, so
|
||||
// it keeps the "-" dash (round 19 follow-up).
|
||||
hashrate_known: self.mining || self.paused,
|
||||
expected_ths: self.expected,
|
||||
board_hashrates,
|
||||
board_voltages,
|
||||
board_frequencies,
|
||||
efficiency,
|
||||
power_w: self.power,
|
||||
chip_temp_c: self.temp,
|
||||
@@ -128,7 +164,10 @@ impl Board {
|
||||
slow,
|
||||
paused: self.paused,
|
||||
// offline defaults to false (online); these bench rows are reachable.
|
||||
mac: format!("AA:BB:CC:00:{:02X}:{last_octet:02X}", last_octet.wrapping_mul(3)),
|
||||
mac: format!(
|
||||
"AA:BB:CC:00:{:02X}:{last_octet:02X}",
|
||||
last_octet.wrapping_mul(3)
|
||||
),
|
||||
hostname: format!("miner-{last_octet}"),
|
||||
hashboards: boards,
|
||||
expected_hashboards: Some(3),
|
||||
@@ -137,22 +176,40 @@ impl Board {
|
||||
psu_fan_rpm: if self.mining { self.fan * 0.9 } else { 0.0 },
|
||||
// PSU info demo (round 17 R5): mining units show a version + rated
|
||||
// envelope + live output, so the columns and the R2 PSU tooltip render.
|
||||
psu_version: if self.mining { "22 (117)".to_string() } else { String::new() },
|
||||
psu_version: if self.mining {
|
||||
"22 (117)".to_string()
|
||||
} else {
|
||||
String::new()
|
||||
},
|
||||
psu_voltage_min_v: self.mining.then_some(11.88),
|
||||
psu_voltage_max_v: self.mining.then_some(15.18),
|
||||
psu_out_voltage_v: self.mining.then_some(13.2),
|
||||
psu_out_current_a: self.mining.then_some(210.0),
|
||||
uptime_secs: if self.mining { 3600 * 24 * 2 + 3600 * 5 } else { 0 },
|
||||
uptime_secs: if self.mining {
|
||||
3600 * 24 * 2 + 3600 * 5
|
||||
} else {
|
||||
0
|
||||
},
|
||||
// One unit is "being located" so the Light column's yellow dot is
|
||||
// demonstrable offline (round 11 #10).
|
||||
light_flashing: Some(self.ip == "10.255.1.167"),
|
||||
pool_url: if self.worker.is_empty() { String::new() } else { "stratum+tcp://pool.blueflare:3333".to_string() },
|
||||
pool_url: if self.worker.is_empty() {
|
||||
String::new()
|
||||
} else {
|
||||
"stratum+tcp://pool.blueflare:3333".to_string()
|
||||
},
|
||||
pools: if self.worker.is_empty() {
|
||||
Vec::new()
|
||||
} else {
|
||||
vec![
|
||||
("stratum+tcp://pool.blueflare:3333".to_string(), self.worker.to_string()),
|
||||
("stratum+tcp://backup.blueflare:3333".to_string(), format!("{}_bak", self.worker)),
|
||||
(
|
||||
"stratum+tcp://pool.blueflare:3333".to_string(),
|
||||
self.worker.to_string(),
|
||||
),
|
||||
(
|
||||
"stratum+tcp://backup.blueflare:3333".to_string(),
|
||||
format!("{}_bak", self.worker),
|
||||
),
|
||||
]
|
||||
},
|
||||
operating_state: if self.paused {
|
||||
@@ -173,6 +230,7 @@ impl Board {
|
||||
power_limit: self.mining,
|
||||
tuning: self.mining,
|
||||
hashrate_target: self.mining,
|
||||
supports_presets: self.mining,
|
||||
firmware_flash,
|
||||
pools: true,
|
||||
timezone: self.mining,
|
||||
@@ -183,13 +241,9 @@ impl Board {
|
||||
// show variety offline). control_board is derived above via naming.
|
||||
algorithm: "SHA256".to_string(),
|
||||
control_board,
|
||||
pic_present: if self.model_id.contains("S21") {
|
||||
Some(false) // noPIC example
|
||||
} else if self.make.eq_ignore_ascii_case("antminer") {
|
||||
Some(true)
|
||||
} else {
|
||||
None // WhatsMiner: unknown over the network
|
||||
},
|
||||
// Use the same PIC inference the live scan uses (round 18 #12: the demo
|
||||
// previously reimplemented a simplified version that disagreed with it).
|
||||
pic_present,
|
||||
// Access: idle/view-only units (no worker) shown locked; the rest
|
||||
// unlocked. (Live rows infer this from control capabilities.)
|
||||
write_locked: Some(self.worker.is_empty()),
|
||||
@@ -203,6 +257,26 @@ impl Board {
|
||||
} else {
|
||||
Vec::new()
|
||||
},
|
||||
// Demo firmware presets (round 18 / G3) so the "Set profile..." dialog
|
||||
// is exercisable offline on units that advertise preset support.
|
||||
presets: if self.mining {
|
||||
vec![
|
||||
crate::model::MinerPreset {
|
||||
name: "default".into(),
|
||||
pretty: Some("2794 W ~ 121.8 TH @ 590 MHz".into()),
|
||||
},
|
||||
crate::model::MinerPreset {
|
||||
name: "540MHz".into(),
|
||||
pretty: Some("2485 W ~ 111.5 TH @ 540 MHz".into()),
|
||||
},
|
||||
crate::model::MinerPreset {
|
||||
name: "490MHz".into(),
|
||||
pretty: Some("2192 W ~ 101.1 TH @ 490 MHz".into()),
|
||||
},
|
||||
]
|
||||
} else {
|
||||
Vec::new()
|
||||
},
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
@@ -220,35 +294,152 @@ pub fn bench_fleet() -> Vec<MinerRow> {
|
||||
let boards = [
|
||||
// firmware families span the palette (Stock/VNish/Braiins/LuxOS/ePIC/
|
||||
// Hive/BixBit/Alkane) so the offline demo shows the Firmware colouring.
|
||||
Board { ip: "10.255.1.112", make: "Antminer", model_id: "S17", board: "Xilinx", firmware: "Stock", fw_version: "",
|
||||
hashrate: 0.0, expected: 0.0, power: 0.0, temp: 0.0, fan: 0.0,
|
||||
worker: "", mining: false, paused: false },
|
||||
Board { ip: "10.255.1.166", make: "Antminer", model_id: "S19", board: "BeagleBoneBlack", firmware: "VNish", fw_version: "1.2.8",
|
||||
hashrate: 95.0, expected: 96.0, power: 3250.0, temp: 65.0, fan: 4200.0,
|
||||
worker: "blueflare.s19", mining: true, paused: false },
|
||||
Board { ip: "10.255.1.152", make: "Antminer", model_id: "S19jPro", board: "AMLogic", firmware: "Braiins", fw_version: "25.05.1",
|
||||
hashrate: 100.0, expected: 104.0, power: 3100.0, temp: 62.0, fan: 3900.0,
|
||||
worker: "blueflare.s19jpro", mining: true, paused: false },
|
||||
Board { ip: "10.255.1.167", make: "Antminer", model_id: "S19KPro", board: "AMLogic", firmware: "LuxOS", fw_version: "2024.9.0",
|
||||
hashrate: 120.0, expected: 128.0, power: 3300.0, temp: 68.0, fan: 4500.0,
|
||||
worker: "blueflare.s19kpro", mining: true, paused: false },
|
||||
Board { ip: "10.255.1.149", make: "Antminer", model_id: "S21Pro", board: "CVITek", firmware: "ePIC", fw_version: "1.0.3",
|
||||
hashrate: 60.0, expected: 200.0, power: 1500.0, temp: 55.0, fan: 3000.0,
|
||||
worker: "blueflare.s21pro", mining: true, paused: false }, // slow: 60/200 = 30%
|
||||
Board { ip: "10.255.1.126", make: "Whatsminer", model_id: "M30SVE40", board: "H3", firmware: "Stock", fw_version: "H3.0.1",
|
||||
hashrate: 92.0, expected: 96.0, power: 3400.0, temp: 70.0, fan: 4800.0,
|
||||
worker: "blueflare.h3", mining: true, paused: false },
|
||||
Board { ip: "10.255.1.131", make: "Whatsminer", model_id: "M30SPlusVE40", board: "H616", firmware: "Hiveon", fw_version: "1.05",
|
||||
hashrate: 100.0, expected: 100.0, power: 3450.0, temp: 71.0, fan: 4900.0,
|
||||
worker: "blueflare.h616", mining: true, paused: false },
|
||||
Board { ip: "10.255.1.148", make: "Whatsminer", model_id: "M50VH50", board: "H6", firmware: "Bixbit", fw_version: "0.9.9.4",
|
||||
hashrate: 118.0, expected: 122.0, power: 3300.0, temp: 66.0, fan: 4600.0,
|
||||
worker: "blueflare.m50", mining: true, paused: false },
|
||||
Board {
|
||||
ip: "10.255.1.112",
|
||||
make: "Antminer",
|
||||
model_id: "S17",
|
||||
board: "Xilinx",
|
||||
firmware: "Stock",
|
||||
fw_version: "",
|
||||
hashrate: 0.0,
|
||||
expected: 0.0,
|
||||
power: 0.0,
|
||||
temp: 0.0,
|
||||
fan: 0.0,
|
||||
worker: "",
|
||||
mining: false,
|
||||
paused: false,
|
||||
},
|
||||
Board {
|
||||
ip: "10.255.1.166",
|
||||
make: "Antminer",
|
||||
model_id: "S19",
|
||||
board: "BeagleBoneBlack",
|
||||
firmware: "VNish",
|
||||
fw_version: "1.2.8",
|
||||
hashrate: 95.0,
|
||||
expected: 96.0,
|
||||
power: 3250.0,
|
||||
temp: 65.0,
|
||||
fan: 4200.0,
|
||||
worker: "blueflare.s19",
|
||||
mining: true,
|
||||
paused: false,
|
||||
},
|
||||
Board {
|
||||
ip: "10.255.1.152",
|
||||
make: "Antminer",
|
||||
model_id: "S19jPro",
|
||||
board: "AMLogic",
|
||||
firmware: "Braiins",
|
||||
fw_version: "25.05.1",
|
||||
hashrate: 100.0,
|
||||
expected: 104.0,
|
||||
power: 3100.0,
|
||||
temp: 62.0,
|
||||
fan: 3900.0,
|
||||
worker: "blueflare.s19jpro",
|
||||
mining: true,
|
||||
paused: false,
|
||||
},
|
||||
Board {
|
||||
ip: "10.255.1.167",
|
||||
make: "Antminer",
|
||||
model_id: "S19KPro",
|
||||
board: "AMLogic",
|
||||
firmware: "LuxOS",
|
||||
fw_version: "2024.9.0",
|
||||
hashrate: 120.0,
|
||||
expected: 128.0,
|
||||
power: 3300.0,
|
||||
temp: 68.0,
|
||||
fan: 4500.0,
|
||||
worker: "blueflare.s19kpro",
|
||||
mining: true,
|
||||
paused: false,
|
||||
},
|
||||
Board {
|
||||
ip: "10.255.1.149",
|
||||
make: "Antminer",
|
||||
model_id: "S21Pro",
|
||||
board: "CVITek",
|
||||
firmware: "ePIC",
|
||||
fw_version: "1.0.3",
|
||||
hashrate: 60.0,
|
||||
expected: 200.0,
|
||||
power: 1500.0,
|
||||
temp: 55.0,
|
||||
fan: 3000.0,
|
||||
worker: "blueflare.s21pro",
|
||||
mining: true,
|
||||
paused: false,
|
||||
}, // slow: 60/200 = 30%
|
||||
Board {
|
||||
ip: "10.255.1.126",
|
||||
make: "Whatsminer",
|
||||
model_id: "M30SVE40",
|
||||
board: "H3",
|
||||
firmware: "Stock",
|
||||
fw_version: "H3.0.1",
|
||||
hashrate: 92.0,
|
||||
expected: 96.0,
|
||||
power: 3400.0,
|
||||
temp: 70.0,
|
||||
fan: 4800.0,
|
||||
worker: "blueflare.h3",
|
||||
mining: true,
|
||||
paused: false,
|
||||
},
|
||||
Board {
|
||||
ip: "10.255.1.131",
|
||||
make: "Whatsminer",
|
||||
model_id: "M30SPlusVE40",
|
||||
board: "H616",
|
||||
firmware: "Hiveon",
|
||||
fw_version: "1.05",
|
||||
hashrate: 100.0,
|
||||
expected: 100.0,
|
||||
power: 3450.0,
|
||||
temp: 71.0,
|
||||
fan: 4900.0,
|
||||
worker: "blueflare.h616",
|
||||
mining: true,
|
||||
paused: false,
|
||||
},
|
||||
Board {
|
||||
ip: "10.255.1.148",
|
||||
make: "Whatsminer",
|
||||
model_id: "M50VH50",
|
||||
board: "H6",
|
||||
firmware: "Bixbit",
|
||||
fw_version: "0.9.9.4",
|
||||
hashrate: 118.0,
|
||||
expected: 122.0,
|
||||
power: 3300.0,
|
||||
temp: 66.0,
|
||||
fan: 4600.0,
|
||||
worker: "blueflare.m50",
|
||||
mining: true,
|
||||
paused: false,
|
||||
},
|
||||
// Rebucketed from idle/not-hashing to paused so the "paused"
|
||||
// status/KPI bucket is exercisable in the fake-scan/offline demo.
|
||||
Board { ip: "10.255.1.115", make: "Antminer", model_id: "S19", board: "BeagleBoneBlack", firmware: "Alkane", fw_version: "0.1",
|
||||
hashrate: 0.0, expected: 0.0, power: 0.0, temp: 0.0, fan: 0.0,
|
||||
worker: "", mining: false, paused: true },
|
||||
Board {
|
||||
ip: "10.255.1.115",
|
||||
make: "Antminer",
|
||||
model_id: "S19",
|
||||
board: "BeagleBoneBlack",
|
||||
firmware: "Alkane",
|
||||
fw_version: "0.1",
|
||||
hashrate: 0.0,
|
||||
expected: 0.0,
|
||||
power: 0.0,
|
||||
temp: 0.0,
|
||||
fan: 0.0,
|
||||
worker: "",
|
||||
mining: false,
|
||||
paused: true,
|
||||
},
|
||||
];
|
||||
boards.into_iter().map(Board::into_row).collect()
|
||||
}
|
||||
|
||||
@@ -27,14 +27,14 @@ impl Lang {
|
||||
pub fn label(self) -> &'static str {
|
||||
match self {
|
||||
Lang::En => "English",
|
||||
Lang::Zh => "\u{4e2d}\u{6587}", // 中文
|
||||
Lang::Zh => "\u{4e2d}\u{6587}", // 中文
|
||||
Lang::Ru => "\u{0420}\u{0443}\u{0441}\u{0441}\u{043a}\u{0438}\u{0439}", // Русский
|
||||
Lang::Fr => "Fran\u{e7}ais",
|
||||
Lang::Es => "Espa\u{f1}ol",
|
||||
Lang::Pt => "Portugu\u{ea}s",
|
||||
}
|
||||
}
|
||||
fn code(self) -> &'static str {
|
||||
pub fn code(self) -> &'static str {
|
||||
match self {
|
||||
Lang::En => "en",
|
||||
Lang::Zh => "zh",
|
||||
@@ -71,8 +71,7 @@ static CURRENT: AtomicU8 = AtomicU8::new(0);
|
||||
const LANG_FILE: &str = "language.txt";
|
||||
|
||||
fn config_path() -> PathBuf {
|
||||
let home = std::env::var("HOME").unwrap_or_else(|_| ".".into());
|
||||
PathBuf::from(home).join(".config/alkane-toolkit").join(LANG_FILE)
|
||||
alkane_core::platform::config_path(LANG_FILE)
|
||||
}
|
||||
|
||||
/// Load the saved language into the global (call once at startup).
|
||||
@@ -138,6 +137,13 @@ const TABLE: &[(&str, [&str; 6])] = &[
|
||||
("Credentials", ["Credentials", "\u{51ed}\u{636e}", "\u{0423}\u{0447}\u{0451}\u{0442}\u{043d}\u{044b}\u{0435} \u{0434}\u{0430}\u{043d}\u{043d}\u{044b}\u{0435}", "Identifiants", "Credenciales", "Credenciais"]),
|
||||
("Language", ["Language", "\u{8bed}\u{8a00}", "\u{042f}\u{0437}\u{044b}\u{043a}", "Langue", "Idioma", "Idioma"]),
|
||||
("Logs", ["Logs", "\u{65e5}\u{5fd7}", "\u{0416}\u{0443}\u{0440}\u{043d}\u{0430}\u{043b}", "Journaux", "Registros", "Registos"]),
|
||||
("Bug report", ["Bug report", "\u{95ee}\u{9898}\u{53cd}\u{9988}", "\u{421}\u{43e}\u{43e}\u{431}\u{449}\u{438}\u{442}\u{44c} \u{43e}\u{431} \u{43e}\u{448}\u{438}\u{431}\u{43a}\u{435}", "Signaler un bug", "Informar de un error", "Relatar um erro"]),
|
||||
("Type", ["Type", "\u{7c7b}\u{578b}", "\u{422}\u{438}\u{43f}", "Type", "Tipo", "Tipo"]),
|
||||
("Bug", ["Bug", "\u{7f3a}\u{9677}", "\u{41e}\u{448}\u{438}\u{431}\u{43a}\u{430}", "Bug", "Error", "Erro"]),
|
||||
("UI change", ["UI change", "\u{754c}\u{9762}\u{4fee}\u{6539}", "\u{418}\u{437}\u{43c}\u{435}\u{43d}\u{435}\u{43d}\u{438}\u{435} \u{438}\u{43d}\u{442}\u{435}\u{440}\u{444}\u{435}\u{439}\u{441}\u{430}", "Modification de l'interface", "Cambio de interfaz", "Altera\u{e7}\u{e3}o da interface"]),
|
||||
("Feature request", ["Feature request", "\u{529f}\u{80fd}\u{5efa}\u{8bae}", "\u{41f}\u{440}\u{435}\u{434}\u{43b}\u{43e}\u{436}\u{435}\u{43d}\u{438}\u{435} \u{444}\u{443}\u{43d}\u{43a}\u{446}\u{438}\u{438}", "Demande de fonctionnalit\u{e9}", "Solicitud de funci\u{f3}n", "Pedido de recurso"]),
|
||||
("Send", ["Send", "\u{53d1}\u{9001}", "\u{41e}\u{442}\u{43f}\u{440}\u{430}\u{432}\u{438}\u{442}\u{44c}", "Envoyer", "Enviar", "Enviar"]),
|
||||
("Open issue", ["Open issue", "\u{6253}\u{5f00}\u{95ee}\u{9898}", "\u{41e}\u{442}\u{43a}\u{440}\u{44b}\u{442}\u{44c} \u{437}\u{430}\u{434}\u{430}\u{447}\u{443}", "Ouvrir le ticket", "Abrir incidencia", "Abrir issue"]),
|
||||
// KPI captions
|
||||
("Total", ["Total", "\u{603b}\u{6570}", "\u{0412}\u{0441}\u{0435}\u{0433}\u{043e}", "Total", "Total", "Total"]),
|
||||
("Hashing", ["Hashing", "\u{6b63}\u{5728}\u{6316}\u{77ff}", "\u{0425}\u{0435}\u{0448}\u{0438}\u{0440}\u{043e}\u{0432}\u{0430}\u{043d}\u{0438}\u{0435}", "Minage", "Minando", "Minerando"]),
|
||||
|
||||
@@ -1,58 +1,48 @@
|
||||
//! Fetch a miner's log over its firmware API and regex-scan it for useful
|
||||
//! signals (round 12 #12). This is the "logs through API" path the operator
|
||||
//! asked for: where a firmware exposes its log we can read board/PIC lines and
|
||||
//! other metrics without SSH.
|
||||
//! Fetch a miner's log for the "View log (API)" viewer (round 12 #12, reworked
|
||||
//! round 19 #11-16).
|
||||
//!
|
||||
//! Per firmware:
|
||||
//! - VNish (aftermarket Antminer): POST /api/v1/unlock to get a bearer token,
|
||||
//! then GET /api/v1/logs/miner -- the full miner log (PSU init, per-chain
|
||||
//! chip detection, tuning, errors).
|
||||
//! - Stock Antminer: GET /cgi-bin/log.cgi behind digest auth (older models
|
||||
//! such as the Z15 do not serve it -> reported as unavailable).
|
||||
//! - WhatsMiner: the btminer log is not exposed on :4028, but get_error_code
|
||||
//! (unauthenticated) returns the active fault codes, which we format as the
|
||||
//! log view.
|
||||
//! The log is fetched through asic-rs's per-backend `read_logs()` API, which
|
||||
//! knows each firmware's real transport (Antminer/Z15 + BixBit stock web,
|
||||
//! Braiins gRPC/journal, VNish and ePIC HTTP, ...). This replaced the GUI's
|
||||
//! narrow three-way curl fetch, whose stock `/cgi-bin/log.cgi` `else` branch
|
||||
//! failed on every firmware that does not serve that CGI (Braiins, LuxOS, Z15,
|
||||
//! BixBit).
|
||||
//!
|
||||
//! We shell out to `curl` (already a dependency of the access probe) so the
|
||||
//! offline/vendored build gains no new crate, and cap the fetched text so a huge
|
||||
//! log never bloats the viewer.
|
||||
//! WhatsMiner is the exception: its btminer log is not exposed over the API, and
|
||||
//! asic-rs has no `read_logs` for it, so the viewer falls back to the active
|
||||
//! fault codes (get_error_code on :4028, unauthenticated). LuxOS likewise has no
|
||||
//! asic-rs log API and reports the log as unavailable.
|
||||
|
||||
use std::process::Command;
|
||||
use std::time::Duration;
|
||||
|
||||
/// Cap on the log text we keep for display (bytes); the regex summary is still
|
||||
/// computed over everything fetched.
|
||||
/// Cap on the log text we keep for display (bytes); a miner's freshest lines are
|
||||
/// at the tail, so we keep the tail.
|
||||
const MAX_LOG_BYTES: usize = 256 * 1024;
|
||||
|
||||
/// A fetched log plus the signals a regex pass pulled out of it.
|
||||
/// A fetched log for the viewer.
|
||||
pub struct LogScan {
|
||||
/// The (possibly tail-trimmed) log text for the viewer.
|
||||
/// The (possibly tail-trimmed) log text.
|
||||
pub text: String,
|
||||
/// One-line-per-finding summary shown above the log.
|
||||
pub summary: Vec<String>,
|
||||
}
|
||||
|
||||
/// Fetch and scan one miner's log. `brand` is the make ("Antminer"/"Whatsminer"),
|
||||
/// `firmware` the family ("VNish"/"Stock"/...). `vnish_pass`/`wm` creds are used
|
||||
/// where that firmware needs auth. Returns `Err` only when no log could be read.
|
||||
pub fn scan_log(
|
||||
ip: &str,
|
||||
brand: &str,
|
||||
firmware: &str,
|
||||
vnish_pass: &str,
|
||||
antminer_user: &str,
|
||||
antminer_pass: &str,
|
||||
) -> Result<LogScan, String> {
|
||||
let raw = if firmware.eq_ignore_ascii_case("vnish") {
|
||||
fetch_vnish_log(ip, vnish_pass)?
|
||||
} else if brand.eq_ignore_ascii_case("whatsminer") {
|
||||
fetch_whatsminer_errors(ip)?
|
||||
} else {
|
||||
// Stock / other Antminer-family web firmware.
|
||||
fetch_antminer_log(ip, antminer_user, antminer_pass)?
|
||||
};
|
||||
let summary = summarize_log(&raw);
|
||||
Ok(LogScan { text: tail(&raw, MAX_LOG_BYTES), summary })
|
||||
/// Fetch one miner's log. `brand` is the make ("Antminer"/"Whatsminer"/...);
|
||||
/// `timeout_secs` is the per-miner identification timeout. Returns `Err` only
|
||||
/// when no log could be read.
|
||||
pub fn scan_log(ip: &str, brand: &str, timeout_secs: u64) -> Result<LogScan, String> {
|
||||
// WhatsMiner's real log is not exposed over its API and asic-rs has no
|
||||
// read_logs for it, so show the active fault codes instead of failing.
|
||||
if brand.eq_ignore_ascii_case("whatsminer") {
|
||||
let resp = crate::access_probe::whatsminer_get_error_code(ip)
|
||||
.ok_or_else(|| "WhatsMiner did not answer get_error_code on :4028".to_string())?;
|
||||
return Ok(LogScan {
|
||||
text: tail(&resp, MAX_LOG_BYTES),
|
||||
});
|
||||
}
|
||||
let text = crate::scan::fetch_log(ip, timeout_secs)?;
|
||||
if text.trim().is_empty() {
|
||||
return Err("the miner returned an empty log".to_string());
|
||||
}
|
||||
Ok(LogScan {
|
||||
text: tail(&text, MAX_LOG_BYTES),
|
||||
})
|
||||
}
|
||||
|
||||
/// Keep the last `max` bytes of `s` (a log's tail is the freshest), on a line
|
||||
@@ -61,8 +51,14 @@ fn tail(s: &str, max: usize) -> String {
|
||||
if s.len() <= max {
|
||||
return s.to_string();
|
||||
}
|
||||
let start = s.len() - max;
|
||||
// advance to the next line boundary so we don't cut a line in half
|
||||
// Nudge the cut forward to a UTF-8 char boundary first: a raw byte offset can
|
||||
// land inside a multi-byte character and slicing there would panic (round 18
|
||||
// #12; miner logs are not guaranteed pure ASCII).
|
||||
let mut start = s.len() - max;
|
||||
while start < s.len() && !s.is_char_boundary(start) {
|
||||
start += 1;
|
||||
}
|
||||
// then advance to the next line boundary so we don't cut a line in half
|
||||
let start = s[start..]
|
||||
.find('\n')
|
||||
.map(|i| start + i + 1)
|
||||
@@ -70,188 +66,18 @@ fn tail(s: &str, max: usize) -> String {
|
||||
format!("[... earlier log trimmed ...]\n{}", &s[start..])
|
||||
}
|
||||
|
||||
/// Regex-free scan for the high-value signals in a miner log: PIC / hashboard
|
||||
/// firmware lines, per-chain chip detection, and the most recent errors.
|
||||
fn summarize_log(log: &str) -> Vec<String> {
|
||||
let mut out = Vec::new();
|
||||
let lc_has = |line: &str, needle: &str| line.to_ascii_lowercase().contains(needle);
|
||||
|
||||
// PIC / hashboard-controller status from the log. Aftermarket firmwares state
|
||||
// it explicitly ("no-PIC miner"); stock boot logs print a PIC version. This
|
||||
// is the log-regex PIC scan the operator asked for (round 12 #12).
|
||||
let pic_lines: Vec<&str> = log.lines().filter(|l| lc_has(l, "pic")).collect();
|
||||
if pic_lines.iter().any(|l| lc_has(l, "no-pic") || lc_has(l, "no pic")) {
|
||||
out.push("PIC: not present (log reports a no-PIC miner)".to_string());
|
||||
} else if let Some(l) = pic_lines
|
||||
.iter()
|
||||
.find(|l| lc_has(l, "version") || lc_has(l, "fw") || lc_has(l, "found"))
|
||||
{
|
||||
out.push(format!("PIC: present -- {}", l.trim()));
|
||||
} else if !pic_lines.is_empty() {
|
||||
out.push(format!("PIC: {} log line(s) mention PIC", pic_lines.len()));
|
||||
for l in pic_lines.iter().take(3) {
|
||||
out.push(format!(" {}", l.trim()));
|
||||
}
|
||||
} else {
|
||||
out.push("PIC: no PIC lines in the log".to_string());
|
||||
}
|
||||
|
||||
// Latest per-chain chip-detection line (the most recent board bring-up).
|
||||
if let Some(l) = log.lines().rev().find(|l| lc_has(l, "chips detected")) {
|
||||
out.push(format!("Chains: {}", l.trim()));
|
||||
}
|
||||
|
||||
// Recent errors (log history, beyond asic-rs's current-message snapshot).
|
||||
// Skip the get_error_code header line (it contains "error" but is not one).
|
||||
let errors: Vec<&str> = log
|
||||
.lines()
|
||||
.rev()
|
||||
.filter(|l| (lc_has(l, "error") || lc_has(l, "fail")) && !lc_has(l, "get_error_code"))
|
||||
.take(3)
|
||||
.collect();
|
||||
if errors.is_empty() {
|
||||
out.push("Errors: none in the log".to_string());
|
||||
} else {
|
||||
out.push(format!("Errors: {} recent", errors.len()));
|
||||
for l in errors {
|
||||
out.push(format!(" {}", l.trim()));
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// VNish: unlock -> bearer -> the full miner log.
|
||||
fn fetch_vnish_log(ip: &str, pass: &str) -> Result<String, String> {
|
||||
let unlock = curl(&[
|
||||
"-s",
|
||||
"--connect-timeout",
|
||||
"5",
|
||||
"--max-time",
|
||||
"9",
|
||||
"-X",
|
||||
"POST",
|
||||
"-H",
|
||||
"Content-Type: application/json",
|
||||
"-d",
|
||||
&format!("{{\"pw\":\"{pass}\"}}"),
|
||||
&format!("http://{ip}/api/v1/unlock"),
|
||||
])?;
|
||||
let token = serde_json::from_str::<serde_json::Value>(&unlock)
|
||||
.ok()
|
||||
.and_then(|v| v.get("token").and_then(|t| t.as_str()).map(str::to_string))
|
||||
.filter(|t| !t.is_empty())
|
||||
.ok_or_else(|| "VNish unlock rejected (check the VNish password in scan credentials)".to_string())?;
|
||||
let log = curl(&[
|
||||
"-s",
|
||||
"--connect-timeout",
|
||||
"5",
|
||||
"--max-time",
|
||||
"12",
|
||||
"-H",
|
||||
&format!("Authorization: Bearer {token}"),
|
||||
&format!("http://{ip}/api/v1/logs/miner"),
|
||||
])?;
|
||||
if log.trim().is_empty() {
|
||||
return Err("VNish returned an empty log".to_string());
|
||||
}
|
||||
Ok(log)
|
||||
}
|
||||
|
||||
/// Stock Antminer web log (digest auth). Absent on older models (404).
|
||||
fn fetch_antminer_log(ip: &str, user: &str, pass: &str) -> Result<String, String> {
|
||||
let body = curl(&[
|
||||
"-s",
|
||||
"--digest",
|
||||
"-u",
|
||||
&format!("{user}:{pass}"),
|
||||
"--connect-timeout",
|
||||
"5",
|
||||
"--max-time",
|
||||
"12",
|
||||
&format!("http://{ip}/cgi-bin/log.cgi"),
|
||||
])?;
|
||||
if body.contains("404 - Not Found") || body.trim().is_empty() {
|
||||
return Err("this firmware does not expose a log over its web API".to_string());
|
||||
}
|
||||
Ok(body)
|
||||
}
|
||||
|
||||
/// WhatsMiner: format the active error codes (get_error_code, unauthenticated).
|
||||
fn fetch_whatsminer_errors(ip: &str) -> Result<String, String> {
|
||||
let resp = crate::access_probe::whatsminer_get_error_code(ip)
|
||||
.ok_or_else(|| "WhatsMiner did not answer get_error_code on :4028".to_string())?;
|
||||
Ok(resp)
|
||||
}
|
||||
|
||||
/// Run curl and return stdout, with a hard wall-clock cap. stdout is drained on a
|
||||
/// separate thread so a large log (hundreds of KB) never deadlocks on a full pipe
|
||||
/// -- the reason a naive "wait then read" times out on the VNish log.
|
||||
fn curl(args: &[&str]) -> Result<String, String> {
|
||||
use std::io::Read;
|
||||
let mut cmd = Command::new("curl");
|
||||
cmd.args(args);
|
||||
cmd.stdout(std::process::Stdio::piped());
|
||||
cmd.stderr(std::process::Stdio::null());
|
||||
let mut child = cmd.spawn().map_err(|e| format!("spawn curl: {e}"))?;
|
||||
let mut stdout = child.stdout.take().ok_or("no curl stdout")?;
|
||||
let reader = std::thread::spawn(move || {
|
||||
let mut buf = Vec::new();
|
||||
let _ = stdout.read_to_end(&mut buf);
|
||||
buf
|
||||
});
|
||||
let timeout = Duration::from_secs(20);
|
||||
let start = std::time::Instant::now();
|
||||
let mut timed_out = false;
|
||||
loop {
|
||||
match child.try_wait().map_err(|e| e.to_string())? {
|
||||
Some(_) => break,
|
||||
None => {
|
||||
if start.elapsed() >= timeout {
|
||||
let _ = child.kill();
|
||||
timed_out = true;
|
||||
break;
|
||||
}
|
||||
std::thread::sleep(Duration::from_millis(50));
|
||||
}
|
||||
}
|
||||
}
|
||||
let buf = reader.join().unwrap_or_default();
|
||||
if timed_out && buf.is_empty() {
|
||||
return Err("log fetch timed out".into());
|
||||
}
|
||||
Ok(String::from_utf8_lossy(&buf).into_owned())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn summarize_finds_pic_chains_and_errors() {
|
||||
let log = "\
|
||||
[t] INFO: Initializing PSU
|
||||
[t] INFO: chain#1 - 126 chips detected
|
||||
[t] INFO: check PIC on chain 1: version 0x03
|
||||
[t] ERROR: Failed load miner config
|
||||
[t] INFO: chain#2 - 126 chips detected";
|
||||
let s = summarize_log(log);
|
||||
assert!(s.iter().any(|l| l.contains("PIC: present")), "PIC present: {s:?}");
|
||||
assert!(s.iter().any(|l| l.contains("chips detected")), "chains: {s:?}");
|
||||
assert!(s.iter().any(|l| l.contains("Errors: 1")), "errors: {s:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn summarize_detects_no_pic_miner() {
|
||||
let log = "[t] ERROR: This is a no-PIC miner, so all connected chains ...\n[t] INFO: mining";
|
||||
let s = summarize_log(log);
|
||||
assert!(s.iter().any(|l| l.contains("PIC: not present")), "{s:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn summarize_handles_no_pic_lines() {
|
||||
let log = "[t] INFO: chain#1 - 126 chips detected\n[t] INFO: mining";
|
||||
let s = summarize_log(log);
|
||||
assert!(s.iter().any(|l| l.contains("no PIC lines")), "{s:?}");
|
||||
fn tail_does_not_panic_mid_multibyte_char() {
|
||||
// A byte-offset cut can land inside a multi-byte character; tail must nudge
|
||||
// to a char boundary rather than panic (round 18 #12).
|
||||
let s = "\u{00e9}".repeat(200); // 400 bytes of 2-byte chars, no newlines
|
||||
let out = tail(&s, 101); // cut at an odd offset -> inside a char
|
||||
assert!(out.starts_with("[... earlier log trimmed ...]"));
|
||||
assert!(out.len() < s.len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -12,7 +12,10 @@ static LOG: Mutex<Vec<String>> = Mutex::new(Vec::new());
|
||||
/// Local wall-clock HH:MM:SS for a log line. Uses the system clock offset from
|
||||
/// UTC via the TZ-less epoch; good enough for an activity log timestamp.
|
||||
fn stamp() -> String {
|
||||
let secs = SystemTime::now().duration_since(UNIX_EPOCH).map(|d| d.as_secs()).unwrap_or(0);
|
||||
let secs = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.map(|d| d.as_secs())
|
||||
.unwrap_or(0);
|
||||
let t = secs % 86_400;
|
||||
format!("{:02}:{:02}:{:02}", t / 3600, (t % 3600) / 60, t % 60)
|
||||
}
|
||||
|
||||
@@ -1,24 +1,39 @@
|
||||
//! alkane-toolkit — primary view (docs/UI-SPEC.md).
|
||||
//! alkane-toolkit: primary view (docs/UI-SPEC.md).
|
||||
//! Pure-Rust wxWidgets app on the vendored, patched wx-rs.
|
||||
|
||||
// A GUI application on Windows: no console window opens behind it. The
|
||||
// headless modes below re-attach to the console they were started from.
|
||||
#![cfg_attr(windows, windows_subsystem = "windows")]
|
||||
|
||||
mod access_probe;
|
||||
mod alkane_detect;
|
||||
mod askpass;
|
||||
mod bug_report;
|
||||
mod credentials;
|
||||
mod export;
|
||||
mod fixtures;
|
||||
mod i18n;
|
||||
mod logs;
|
||||
mod log_scan;
|
||||
mod logs;
|
||||
mod model;
|
||||
mod naming;
|
||||
mod pool_prefs;
|
||||
mod presets;
|
||||
mod restore;
|
||||
mod scan;
|
||||
mod stock_ops;
|
||||
mod ui;
|
||||
|
||||
use wx_rs;
|
||||
|
||||
fn main() {
|
||||
// ssh runs this executable as its askpass helper for password logins
|
||||
// (askpass.rs); answer and exit before anything else starts.
|
||||
if let Some(code) = askpass::serve_if_askpass() {
|
||||
std::process::exit(code);
|
||||
}
|
||||
if headless_mode() {
|
||||
attach_parent_console();
|
||||
}
|
||||
|
||||
// Headless workflow self-test: render the bench-fleet fixtures through the
|
||||
// real widgets and check the table + KPIs against the expected hardware
|
||||
// output. Run under a virtual display (see scripts/ui-selftest.sh).
|
||||
@@ -44,27 +59,17 @@ fn main() {
|
||||
std::process::exit(0);
|
||||
}
|
||||
|
||||
// Headless log-scan check: `ALKANE_LOG_SCAN="<ip> <brand> <firmware>"` fetches
|
||||
// the miner's log over its firmware API and prints the regex summary (round
|
||||
// 12 #12), so the VNish/Antminer/WhatsMiner log paths can be verified without
|
||||
// the GUI.
|
||||
// Headless log-scan check: `ALKANE_LOG_SCAN="<ip> <brand>"` fetches the miner's
|
||||
// log through asic-rs's per-backend read_logs() (round 19 #11-16), so the log
|
||||
// paths can be verified without the GUI. WhatsMiner falls back to fault codes.
|
||||
if let Some(spec) = std::env::var_os("ALKANE_LOG_SCAN") {
|
||||
let spec = spec.to_string_lossy();
|
||||
let mut it = spec.split_whitespace();
|
||||
let ip = it.next().unwrap_or("");
|
||||
let brand = it.next().unwrap_or("");
|
||||
let firmware = it.next().unwrap_or("");
|
||||
let (au, ap) = credentials::load_credentials().unwrap_or_else(|| ("root".into(), "root".into()));
|
||||
let vp = credentials::firmware_default_for("VNish").1;
|
||||
let vp = if vp.is_empty() { "admin".to_string() } else { vp };
|
||||
match log_scan::scan_log(ip, brand, firmware, &vp, &au, &ap) {
|
||||
Ok(s) => {
|
||||
println!("LOGSCAN {ip} ({brand}/{firmware}): {} log bytes", s.text.len());
|
||||
for line in &s.summary {
|
||||
println!(" {line}");
|
||||
}
|
||||
}
|
||||
Err(e) => println!("LOGSCAN {ip} ({brand}/{firmware}): ERROR {e}"),
|
||||
match log_scan::scan_log(ip, brand, scan::load_scan_timeout()) {
|
||||
Ok(s) => println!("LOGSCAN {ip} ({brand}): {} log bytes", s.text.len()),
|
||||
Err(e) => println!("LOGSCAN {ip} ({brand}): ERROR {e}"),
|
||||
}
|
||||
std::process::exit(0);
|
||||
}
|
||||
@@ -72,10 +77,10 @@ fn main() {
|
||||
// Load the saved UI language before any strings are built (round 15 #13).
|
||||
i18n::init();
|
||||
|
||||
// Bootstrap the wx app + main frame (upstream bridge).
|
||||
// Bootstrap the wx app + main frame (upstream bridge). This also picks the
|
||||
// light/dark appearance (system, or ALKANE_APPEARANCE=light|dark) before the
|
||||
// frame exists, which wxMSW requires.
|
||||
wx_rs::init_app("alkane-toolkit", 1280, 800);
|
||||
// Follow the system light/dark appearance.
|
||||
wx_rs::set_appearance_system();
|
||||
|
||||
// Build the UI-SPEC primary view into the frame.
|
||||
let frame = wx_rs::Widget::main_frame();
|
||||
@@ -97,6 +102,17 @@ fn run_selftest() -> i32 {
|
||||
let frame = wx_rs::Widget::main_frame();
|
||||
let mut app = ui::build(frame);
|
||||
let ok = app.run_fixture_selftest();
|
||||
// ALKANE_SNAPSHOT=path (macOS): also write a PNG of the window as the app
|
||||
// draws it, to check rendering without screen-recording permission.
|
||||
if let Some(path) = std::env::var_os("ALKANE_SNAPSHOT") {
|
||||
let path = path.to_string_lossy();
|
||||
let wrote = wx_rs::snapshot_png(&frame, &path);
|
||||
println!(
|
||||
"SNAPSHOT {} {}",
|
||||
if wrote { "wrote" } else { "unavailable" },
|
||||
path
|
||||
);
|
||||
}
|
||||
println!("SELFTEST {}", if ok { "PASS" } else { "FAIL" });
|
||||
if ok {
|
||||
0
|
||||
@@ -104,3 +120,37 @@ fn run_selftest() -> i32 {
|
||||
1
|
||||
}
|
||||
}
|
||||
|
||||
/// True when the process runs one of the headless check modes instead of the GUI.
|
||||
fn headless_mode() -> bool {
|
||||
["ALKANE_SELFTEST", "ALKANE_ACCESS_PROBE", "ALKANE_LOG_SCAN"]
|
||||
.iter()
|
||||
.any(|v| std::env::var_os(v).is_some())
|
||||
}
|
||||
|
||||
/// A Windows GUI-subsystem process has no console, so output from the headless
|
||||
/// modes would vanish when they are run from a terminal. Attach to the parent's
|
||||
/// console in that case. Redirected output (pipes, files, CI) already has valid
|
||||
/// standard handles and is left alone.
|
||||
#[cfg(windows)]
|
||||
fn attach_parent_console() {
|
||||
type Handle = *mut core::ffi::c_void;
|
||||
const STD_OUTPUT_HANDLE: u32 = -11i32 as u32;
|
||||
const ATTACH_PARENT_PROCESS: u32 = u32::MAX;
|
||||
#[link(name = "kernel32")]
|
||||
extern "system" {
|
||||
fn GetStdHandle(which: u32) -> Handle;
|
||||
fn AttachConsole(process_id: u32) -> i32;
|
||||
}
|
||||
// SAFETY: plain Win32 calls with constant arguments; failure (no parent
|
||||
// console, already attached) is harmless and leaves the process unchanged.
|
||||
unsafe {
|
||||
let out = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
if out.is_null() || out as isize == -1 {
|
||||
AttachConsole(ATTACH_PARENT_PROCESS);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(windows))]
|
||||
fn attach_parent_console() {}
|
||||
|
||||