- Install verify no longer false-fails on images that serve only SSH. The install worker now polls the target's SSH banner after writing (stock Antminers ship dropbear 2018/2019; any newer banner on the board we just installed to means alkane booted), independent of the HTTP /api/identity scan probe. The GUI's HTTP awaiting message is softened to make clear it is only about the row auto-refresh, not an install failure. Fixes the "installed but did not answer as alkane" seen on the A113D bench image (SSH-only, no HTTP API). - Toolbar icons 22px -> 16px so they fit the control row. Note: a minimal image without the HTTP API still will not appear in the scan (the scan probe is HTTP); the proper fix is an A113D image that serves the flare-ui/API (separate alkane-os/sdk work). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B1W6qKwKxq3oUPDKWicM6i
561 lines
21 KiB
Rust
561 lines
21 KiB
Rust
//! Install alkane-os onto a board on stock, and restore an alkane board to
|
|
//! stock, from the GUI - using the toolkit's own self-contained, network-only
|
|
//! engine (`alkane_flash::stock`), the same code the CLI drives. There is no
|
|
//! external helper and no runtime dependency on any other tool: the
|
|
//! stock-crossing method is compiled into this binary. This module drives that
|
|
//! engine on a background thread and reports progress.
|
|
//!
|
|
//! Both operations are DESTRUCTIVE (they reflash the control board) and run on a
|
|
//! background thread so the reboot/flash latency never blocks the wx event loop.
|
|
//! Results stream back over a channel, drained by the UI timer tick exactly like
|
|
//! restore.rs. The UI gates each behind a typed-FLASH confirmation.
|
|
|
|
use std::io::Read as _;
|
|
use std::net::TcpStream;
|
|
use std::path::{Path, PathBuf};
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
use std::sync::mpsc::{channel, Receiver, Sender};
|
|
use std::sync::Arc;
|
|
use std::time::{Duration, Instant};
|
|
|
|
use alkane_artifacts::afw::{verify_afw, VerifyOptions};
|
|
use alkane_artifacts::fetch::{self, ReleaseSource};
|
|
use alkane_flash::stock::{self, RestoreImages};
|
|
|
|
/// Read the SSH identification banner (no auth) from `host:22`, e.g.
|
|
/// `SSH-2.0-dropbear_2026.93`.
|
|
fn ssh_banner(host: &str) -> Option<String> {
|
|
let addr: std::net::SocketAddr = format!("{host}:22").parse().ok()?;
|
|
let mut s = TcpStream::connect_timeout(&addr, Duration::from_secs(4)).ok()?;
|
|
s.set_read_timeout(Some(Duration::from_secs(4))).ok()?;
|
|
let mut buf = [0u8; 128];
|
|
let n = s.read(&mut buf).ok()?;
|
|
if n == 0 {
|
|
return None;
|
|
}
|
|
Some(
|
|
String::from_utf8_lossy(&buf[..n])
|
|
.lines()
|
|
.next()
|
|
.unwrap_or("")
|
|
.trim()
|
|
.to_string(),
|
|
)
|
|
}
|
|
|
|
/// Whether a just-installed board's SSH banner looks like alkane. We installed
|
|
/// alkane to this specific board, so any SSH banner that is NOT the stock
|
|
/// Antminer dropbear (2018/2019) means our image booted. Alkane images ship a
|
|
/// current dropbear regardless of whether the image also serves the HTTP API, so
|
|
/// this verify works even for minimal images that do not serve `:80` (the
|
|
/// HTTP-only scan probe cannot see those).
|
|
fn banner_is_alkane(banner: &str) -> bool {
|
|
let b = banner.to_ascii_lowercase();
|
|
if b.contains("dropbear_2018") || b.contains("dropbear_2019") {
|
|
return false;
|
|
}
|
|
b.contains("dropbear") || b.contains("openssh")
|
|
}
|
|
|
|
/// Poll a just-installed board until it comes up as alkane over SSH (not the
|
|
/// stock dropbear) or the deadline passes. Streams a progress line on success.
|
|
fn verify_alkane_live(ip: &str, tx: &Sender<StockMsg>, cancel: &AtomicBool) -> bool {
|
|
let deadline = Instant::now() + Duration::from_secs(150);
|
|
loop {
|
|
if cancel.load(Ordering::Relaxed) {
|
|
return false;
|
|
}
|
|
if let Some(b) = ssh_banner(ip) {
|
|
if banner_is_alkane(&b) {
|
|
let _ = tx.send(StockMsg::Progress(format!(
|
|
"{ip}: alkane is live (ssh \"{b}\")"
|
|
)));
|
|
return true;
|
|
}
|
|
}
|
|
if Instant::now() >= deadline {
|
|
return false;
|
|
}
|
|
std::thread::sleep(Duration::from_secs(5));
|
|
}
|
|
}
|
|
use alkane_flash::stock_firmware;
|
|
|
|
use crate::scan::fake_scan_enabled;
|
|
|
|
/// Control boards alkane-os can be installed onto from stock (mirrors the CLI's
|
|
/// `SUPPORTED`). Only these are offered as install targets.
|
|
pub const SUPPORTED_BOARDS: &[(&str, &str)] = &[
|
|
("zynq-7010", "Antminer S17 (Xilinx Zynq-7010 control board)"),
|
|
(
|
|
"a113d",
|
|
"Antminer S19j Pro (Amlogic A113D/AXG control board)",
|
|
),
|
|
];
|
|
|
|
/// The default install board when the user has not chosen another.
|
|
pub const DEFAULT_BOARD: &str = "zynq-7010";
|
|
|
|
pub fn is_supported_board(board: &str) -> bool {
|
|
SUPPORTED_BOARDS.iter().any(|(b, _)| *b == board)
|
|
}
|
|
|
|
/// Where the `.afw` to install comes from. `Release` is a scanned Gitea asset
|
|
/// (its filename and download URL), fetched on the worker thread so the download
|
|
/// never blocks the UI. `Local` is a file the operator picked with the Browse
|
|
/// button -- the same local-file install the CLI offers, now in the GUI too.
|
|
pub enum AfwSource {
|
|
Release { name: String, url: String },
|
|
Local(PathBuf),
|
|
}
|
|
|
|
impl AfwSource {
|
|
/// A short human label for the status line (the filename in both cases).
|
|
fn label(&self) -> String {
|
|
match self {
|
|
AfwSource::Release { name, .. } => name.clone(),
|
|
AfwSource::Local(p) => p
|
|
.file_name()
|
|
.map(|n| n.to_string_lossy().into_owned())
|
|
.unwrap_or_else(|| p.to_string_lossy().into_owned()),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Progress + results streamed from the background worker to the UI.
|
|
pub enum StockMsg {
|
|
Started,
|
|
/// A human-readable status line for the status bar (acquiring, verifying...).
|
|
Progress(String),
|
|
/// One board's outcome.
|
|
Result {
|
|
ip: String,
|
|
ok: bool,
|
|
detail: String,
|
|
},
|
|
Done,
|
|
}
|
|
|
|
pub struct StockHandle {
|
|
pub rx: Receiver<StockMsg>,
|
|
// Cancellation is honoured between boards; a UI stop control is not wired yet.
|
|
#[allow(dead_code)]
|
|
cancel: Arc<AtomicBool>,
|
|
}
|
|
|
|
impl StockHandle {
|
|
/// Construct a handle around a worker's receiver and cancel flag. Used by
|
|
/// other op modules (e.g. `media_ops`) that reuse this streaming channel.
|
|
pub fn new(rx: Receiver<StockMsg>, cancel: Arc<AtomicBool>) -> StockHandle {
|
|
StockHandle { rx, cancel }
|
|
}
|
|
|
|
#[allow(dead_code)]
|
|
pub fn stop(&self) {
|
|
self.cancel.store(true, Ordering::Relaxed);
|
|
}
|
|
}
|
|
|
|
fn download_dir() -> PathBuf {
|
|
alkane_core::platform::config_path("downloads")
|
|
}
|
|
|
|
/// Install alkane-os onto the given boards (currently on stock), over the
|
|
/// network, via the compiled-in from-stock method. `board` is the target board
|
|
/// family (gates the `.afw`); `password`, if set, is passed in-process to the
|
|
/// method (never to the environment or an argument).
|
|
pub fn start_install(
|
|
ips: Vec<String>,
|
|
board: String,
|
|
source: AfwSource,
|
|
allow_unsigned: bool,
|
|
password: Option<String>,
|
|
is_update: bool,
|
|
) -> StockHandle {
|
|
let (tx, rx) = channel();
|
|
let cancel = Arc::new(AtomicBool::new(false));
|
|
|
|
if fake_scan_enabled() {
|
|
let _ = tx.send(StockMsg::Started);
|
|
let _ = tx.send(StockMsg::Progress(
|
|
"install (demo): no hardware touched".into(),
|
|
));
|
|
for ip in ips {
|
|
let _ = tx.send(StockMsg::Result {
|
|
ip,
|
|
ok: true,
|
|
detail: "installed alkane-os (demo)".into(),
|
|
});
|
|
}
|
|
let _ = tx.send(StockMsg::Done);
|
|
return StockHandle { rx, cancel };
|
|
}
|
|
|
|
let tc = cancel.clone();
|
|
let err_tx = tx.clone();
|
|
let spawned = std::thread::Builder::new()
|
|
.name("install-from-stock".into())
|
|
.spawn(move || {
|
|
let _ = tx.send(StockMsg::Started);
|
|
|
|
// 1. Acquire the .afw: a local file is used in place; a release asset
|
|
// is downloaded on this worker thread (never blocking the UI).
|
|
let afw_path = match source {
|
|
AfwSource::Local(path) => {
|
|
let _ = tx.send(StockMsg::Progress(format!(
|
|
"using local {}...",
|
|
AfwSource::Local(path.clone()).label()
|
|
)));
|
|
if !path.is_file() {
|
|
for ip in &ips {
|
|
let _ = tx.send(StockMsg::Result {
|
|
ip: ip.clone(),
|
|
ok: false,
|
|
detail: format!("local .afw not found: {}", path.display()),
|
|
});
|
|
}
|
|
let _ = tx.send(StockMsg::Done);
|
|
return;
|
|
}
|
|
path
|
|
}
|
|
AfwSource::Release { name, url } => {
|
|
let _ = tx.send(StockMsg::Progress("acquiring .afw...".into()));
|
|
match fetch::fetch_afw_url(
|
|
&ReleaseSource::alkane_os(),
|
|
&name,
|
|
&url,
|
|
&download_dir(),
|
|
) {
|
|
Ok(p) => p,
|
|
Err(e) => {
|
|
// One failure line per target so the UI clears its op-status.
|
|
for ip in &ips {
|
|
let _ = tx.send(StockMsg::Result {
|
|
ip: ip.clone(),
|
|
ok: false,
|
|
detail: format!("could not fetch .afw: {e}"),
|
|
});
|
|
}
|
|
let _ = tx.send(StockMsg::Done);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
// 2. Verify + gate the bundle once (board/arch/bootloader + integrity).
|
|
let _ = tx.send(StockMsg::Progress("verifying .afw...".into()));
|
|
let verified = match verify_afw(
|
|
&afw_path,
|
|
&VerifyOptions {
|
|
allow_unsigned,
|
|
expect_board: Some(board.clone()),
|
|
},
|
|
) {
|
|
Ok(v) => v,
|
|
Err(e) => {
|
|
for ip in &ips {
|
|
let _ = tx.send(StockMsg::Result {
|
|
ip: ip.clone(),
|
|
ok: false,
|
|
detail: format!(".afw rejected: {e}"),
|
|
});
|
|
}
|
|
let _ = tx.send(StockMsg::Done);
|
|
return;
|
|
}
|
|
};
|
|
if !is_supported_board(&verified.manifest.board) {
|
|
for ip in &ips {
|
|
let _ = tx.send(StockMsg::Result {
|
|
ip: ip.clone(),
|
|
ok: false,
|
|
detail: format!(
|
|
"{} is not a supported install target",
|
|
verified.manifest.board
|
|
),
|
|
});
|
|
}
|
|
let _ = tx.send(StockMsg::Done);
|
|
return;
|
|
}
|
|
// An in-place Update must never rewrite the bootloader (review #1):
|
|
// refuse a tier-3 (bootloader-writing) bundle so the "BOOT.bin is
|
|
// never written" guarantee holds for an already-alkane board.
|
|
if is_update && verified.manifest.flash_plan().writes_uboot {
|
|
for ip in &ips {
|
|
let _ = tx.send(StockMsg::Result {
|
|
ip: ip.clone(),
|
|
ok: false,
|
|
detail: "update refused: this release reflashes the bootloader (tier 3); \
|
|
pick a tier-1/2 release or use Install"
|
|
.to_string(),
|
|
});
|
|
}
|
|
let _ = tx.send(StockMsg::Done);
|
|
return;
|
|
}
|
|
|
|
// 3. Install onto each board via the network-only from-stock method.
|
|
for ip in ips {
|
|
if tc.load(Ordering::Relaxed) {
|
|
break;
|
|
}
|
|
let _ = tx.send(StockMsg::Progress(format!(
|
|
"installing {} onto {ip} (from stock)...",
|
|
verified.manifest.board
|
|
)));
|
|
let (ok, detail) =
|
|
match stock::install_from_stock(&ip, &afw_path, &verified, password.as_deref())
|
|
{
|
|
Ok(()) => {
|
|
// The write succeeded; now confirm the board rebooted
|
|
// into alkane. Use the SSH banner (works even for
|
|
// images that do not serve the HTTP API) rather than
|
|
// only the HTTP scan probe.
|
|
let _ = tx.send(StockMsg::Progress(format!(
|
|
"{ip}: installed; awaiting reboot into alkane (up to 150s)..."
|
|
)));
|
|
if verify_alkane_live(&ip, &tx, &tc) {
|
|
(true, "installed; alkane is live (SSH-verified)".to_string())
|
|
} else {
|
|
(
|
|
true,
|
|
"installed; alkane not confirmed over SSH within the window -- \
|
|
check the board (a minimal image without the HTTP API still \
|
|
installs and boots; it just will not show in the scan)"
|
|
.to_string(),
|
|
)
|
|
}
|
|
}
|
|
Err(e) => (false, format!("install failed: {e}")),
|
|
};
|
|
if tx.send(StockMsg::Result { ip, ok, detail }).is_err() {
|
|
return;
|
|
}
|
|
}
|
|
let _ = tx.send(StockMsg::Done);
|
|
});
|
|
|
|
if spawned.is_err() {
|
|
let _ = err_tx.send(StockMsg::Result {
|
|
ip: String::new(),
|
|
ok: false,
|
|
detail: "could not start the install worker thread".into(),
|
|
});
|
|
let _ = err_tx.send(StockMsg::Done);
|
|
}
|
|
StockHandle { rx, cancel }
|
|
}
|
|
|
|
/// Restore one board to genuine Bitmain stock using the user-supplied vendor
|
|
/// firmware package: parse it into the stock images and write them back over the
|
|
/// network. `password`, if given, is the board's alkane root credential.
|
|
fn restore_one(ip: &str, firmware: &Path, password: Option<&str>) -> (bool, String) {
|
|
let fw = match stock_firmware::parse(firmware) {
|
|
Ok(f) => f,
|
|
Err(e) => return (false, format!("firmware: {e}")),
|
|
};
|
|
// The device tree comes from the toolkit's per-board asset, not the package,
|
|
// so any genuine Bitmain firmware (OTA or SD) provides a usable kernel+rootfs.
|
|
let images = RestoreImages {
|
|
kernel: &fw.kernel,
|
|
rootfs: &fw.rootfs,
|
|
dtb: fw.dtb.as_deref(),
|
|
};
|
|
match stock::restore_to_stock(ip, &images, password) {
|
|
Ok(()) => (
|
|
true,
|
|
"restored to stock (verify independently it no longer answers as alkane)".to_string(),
|
|
),
|
|
Err(e) => (false, format!("restore failed: {e}")),
|
|
}
|
|
}
|
|
|
|
/// Restore the given alkane boards to genuine Bitmain stock, over the network,
|
|
/// by writing the stock images from `firmware` (a genuine vendor firmware
|
|
/// package) back to NAND. `password`, if set, is the board's alkane root
|
|
/// credential.
|
|
pub fn start_restore(ips: Vec<String>, firmware: PathBuf, password: Option<String>) -> StockHandle {
|
|
let (tx, rx) = channel();
|
|
let cancel = Arc::new(AtomicBool::new(false));
|
|
|
|
if fake_scan_enabled() {
|
|
let _ = tx.send(StockMsg::Started);
|
|
for ip in ips {
|
|
let _ = tx.send(StockMsg::Result {
|
|
ip,
|
|
ok: true,
|
|
detail: "restored to stock (demo)".into(),
|
|
});
|
|
}
|
|
let _ = tx.send(StockMsg::Done);
|
|
return StockHandle { rx, cancel };
|
|
}
|
|
|
|
let tc = cancel.clone();
|
|
let err_tx = tx.clone();
|
|
let spawned = std::thread::Builder::new()
|
|
.name("restore-to-stock".into())
|
|
.spawn(move || {
|
|
let _ = tx.send(StockMsg::Started);
|
|
for ip in ips {
|
|
if tc.load(Ordering::Relaxed) {
|
|
break;
|
|
}
|
|
let _ = tx.send(StockMsg::Progress(format!(
|
|
"restoring {ip} to stock (network-only)..."
|
|
)));
|
|
let (ok, detail) = restore_one(&ip, &firmware, password.as_deref());
|
|
if tx.send(StockMsg::Result { ip, ok, detail }).is_err() {
|
|
return;
|
|
}
|
|
}
|
|
let _ = tx.send(StockMsg::Done);
|
|
});
|
|
|
|
if spawned.is_err() {
|
|
let _ = err_tx.send(StockMsg::Result {
|
|
ip: String::new(),
|
|
ok: false,
|
|
detail: "could not start the restore worker thread".into(),
|
|
});
|
|
let _ = err_tx.send(StockMsg::Done);
|
|
}
|
|
StockHandle { rx, cancel }
|
|
}
|
|
|
|
/// Whether a board holds a restorable on-board stock backup (network probe). A
|
|
/// quick, synchronous check the restore dialog uses to grey out the on-board
|
|
/// option when no backup is present. Any error reads as "not present".
|
|
pub fn onboard_backup_present(ip: &str, password: Option<&str>) -> bool {
|
|
if fake_scan_enabled() {
|
|
return true;
|
|
}
|
|
stock::onboard_stock_backup_present(ip, password).unwrap_or(false)
|
|
}
|
|
|
|
/// Restore the given alkane boards to genuine stock, over the network, from each
|
|
/// board's own on-board stock backup (no vendor firmware file). `password`, if
|
|
/// set, is the board's alkane root credential.
|
|
pub fn start_restore_onboard(ips: Vec<String>, password: Option<String>) -> StockHandle {
|
|
let (tx, rx) = channel();
|
|
let cancel = Arc::new(AtomicBool::new(false));
|
|
|
|
if fake_scan_enabled() {
|
|
let _ = tx.send(StockMsg::Started);
|
|
for ip in ips {
|
|
let _ = tx.send(StockMsg::Result {
|
|
ip,
|
|
ok: true,
|
|
detail: "restored to stock from on-board backup (demo)".into(),
|
|
});
|
|
}
|
|
let _ = tx.send(StockMsg::Done);
|
|
return StockHandle { rx, cancel };
|
|
}
|
|
|
|
let tc = cancel.clone();
|
|
let err_tx = tx.clone();
|
|
let spawned = std::thread::Builder::new()
|
|
.name("restore-onboard".into())
|
|
.spawn(move || {
|
|
let _ = tx.send(StockMsg::Started);
|
|
let timeout = std::time::Duration::from_secs(300);
|
|
for ip in ips {
|
|
if tc.load(Ordering::Relaxed) {
|
|
break;
|
|
}
|
|
let _ = tx.send(StockMsg::Progress(format!(
|
|
"restoring {ip} to stock from its on-board backup..."
|
|
)));
|
|
let (ok, detail) =
|
|
match stock::restore_from_onboard_backup(&ip, password.as_deref(), timeout) {
|
|
Ok(()) => (
|
|
true,
|
|
"restored to stock from on-board backup (verify over the network)"
|
|
.to_string(),
|
|
),
|
|
Err(e) => (false, format!("restore failed: {e}")),
|
|
};
|
|
if tx.send(StockMsg::Result { ip, ok, detail }).is_err() {
|
|
return;
|
|
}
|
|
}
|
|
let _ = tx.send(StockMsg::Done);
|
|
});
|
|
|
|
if spawned.is_err() {
|
|
let _ = err_tx.send(StockMsg::Result {
|
|
ip: String::new(),
|
|
ok: false,
|
|
detail: "could not start the restore worker thread".into(),
|
|
});
|
|
let _ = err_tx.send(StockMsg::Done);
|
|
}
|
|
StockHandle { rx, cancel }
|
|
}
|
|
|
|
/// Restore the given alkane A113D boards to genuine Bitmain stock, over the
|
|
/// network, by clearing the one-byte boot flag in-band from the running alkane
|
|
/// OS. The A113D install overwrites no stock partition, so this flag flip is the
|
|
/// complete restore: clear `ALAN`, reboot, and stock boots via `storeboot` from
|
|
/// the untouched `boot` MTD. No vendor firmware file or on-board backup needed.
|
|
/// `password`, if set, is the board's alkane root credential.
|
|
pub fn start_restore_flag(ips: Vec<String>, password: Option<String>) -> StockHandle {
|
|
let (tx, rx) = channel();
|
|
let cancel = Arc::new(AtomicBool::new(false));
|
|
|
|
if fake_scan_enabled() {
|
|
let _ = tx.send(StockMsg::Started);
|
|
for ip in ips {
|
|
let _ = tx.send(StockMsg::Result {
|
|
ip,
|
|
ok: true,
|
|
detail: "restored to stock (flag flip, demo)".into(),
|
|
});
|
|
}
|
|
let _ = tx.send(StockMsg::Done);
|
|
return StockHandle { rx, cancel };
|
|
}
|
|
|
|
let tc = cancel.clone();
|
|
let err_tx = tx.clone();
|
|
let spawned = std::thread::Builder::new()
|
|
.name("restore-flag".into())
|
|
.spawn(move || {
|
|
let _ = tx.send(StockMsg::Started);
|
|
for ip in ips {
|
|
if tc.load(Ordering::Relaxed) {
|
|
break;
|
|
}
|
|
let _ = tx.send(StockMsg::Progress(format!(
|
|
"restoring {ip} to stock (clearing the alkane boot flag)..."
|
|
)));
|
|
let (ok, detail) = match stock::restore_aml_flag(&ip, password.as_deref()) {
|
|
Ok(()) => (
|
|
true,
|
|
"restored to stock (flag cleared, rebooting; verify over the network)"
|
|
.to_string(),
|
|
),
|
|
Err(e) => (false, format!("restore failed: {e}")),
|
|
};
|
|
if tx.send(StockMsg::Result { ip, ok, detail }).is_err() {
|
|
return;
|
|
}
|
|
}
|
|
let _ = tx.send(StockMsg::Done);
|
|
});
|
|
|
|
if spawned.is_err() {
|
|
let _ = err_tx.send(StockMsg::Result {
|
|
ip: String::new(),
|
|
ok: false,
|
|
detail: "could not start the restore worker thread".into(),
|
|
});
|
|
let _ = err_tx.send(StockMsg::Done);
|
|
}
|
|
StockHandle { rx, cancel }
|
|
}
|