flash: restore to stock from genuine Bitmain firmware files

Restore-to-stock now writes the genuine Bitmain stock images (parsed from a
vendor firmware package) back to NAND, instead of carrying carved stock blobs.

- stock_firmware: new parser for genuine Bitmain Antminer packages - the OTA
  update .tar.gz (outer sign wrapper -> inner fw.tar.gz) and the SD recovery
  .zip. Extracts BOOT.bin / devicetree.dtb / uImage (kernel) / uramdisk (rootfs)
  and verifies the rootfs md5 when the package lists one. Shells out to the
  system tar/unzip. Verified against real S17 packages.
- stock: restore_to_stock now takes the stock images (RestoreImages); the stock
  kernel/dtb/rootfs are no longer carried as assets. The boot regions are not
  exposed to Linux on alkane, so the existing NAND-registration helper is kept to
  make them writable, then the supplied genuine images are written at the vendor
  offsets (dtb 0x1a00000, kernel 0x2000000, rootfs; BOOT.bin preserved).
- The SD recovery image is the complete source: it includes the device tree,
  which the stock legacy-uImage kernel needs at 0x1a00000 and which the OTA
  update packages omit. Restore requires it and points the user to the SD image
  when a package has no dtb.
- Post-reboot the board returns to stock under its stock MAC and typically takes
  a new DHCP lease, so a wait on the original address is best-effort and no longer
  reported as a failure.
- cli: afw restore-stock takes --stock-firmware <file>. gui: the uninstall dialog
  takes the firmware file path.

Also clear the branch's clippy debt so the full gate passes: a type alias/allow
for the hardware map tables and multi-arg write functions, plus small mechanical
fixes (useless format, op-ref, collapsible match). wx-rs patch 0011 records the
wxWidgets 3.2 build-compat edits.

Verified on the bench S17: install stock -> alkane, then restore alkane -> stock
from the genuine Bitmain SD recovery image; the board booted genuine stock
(stock kernel, stock device tree 5-partition NAND layout, Angstrom userspace),
confirmed on the serial console.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B1W6qKwKxq3oUPDKWicM6i
This commit is contained in:
2026-11-11 18:08:09 -06:00
co-authored by Claude Opus 4.8
parent 82492231bc
commit b242e34ca0
12 changed files with 553 additions and 120 deletions
+40 -15
View File
@@ -15,6 +15,8 @@ use alkane_flash::boards::am335x::{
};
use alkane_flash::boards::cv1835::Cv1835Plan;
use alkane_flash::boards::zynq::{self, ZynqInstallOptions, ZynqOutcome, ZynqPlan};
use alkane_flash::stock::RestoreImages;
use alkane_flash::stock_firmware;
use alkane_flash::boards::a113d::{self, A113dInstallOptions, A113dOutcome, A113dPlan};
use alkane_flash::install::{
install, restore, verify_live, InstallOptions, InstallOutcome, RestoreOptions,
@@ -468,7 +470,7 @@ fn run_preflight(args: &[String]) -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}
/// Install onto a board currently on stock via the sealed from-stock method.
/// Install onto a board currently on stock via the from-stock install method.
fn install_from_stock_flow(
verified: &VerifiedAfw,
host: &str,
@@ -477,7 +479,7 @@ fn install_from_stock_flow(
) -> Result<(), Box<dyn std::error::Error>> {
if opts.flag("dry-run") {
println!(
"DRY RUN: would install {} onto stock board {host} via the sealed from-stock method ({})",
"DRY RUN: would install {} onto stock board {host} via the from-stock install method ({})",
verified.manifest.board,
afw.display()
);
@@ -528,7 +530,7 @@ fn run_install(args: &[String]) -> Result<(), Box<dyn std::error::Error>> {
)
.into());
}
// A board on stock: the first install goes through the sealed from-stock
// A board on stock: the first install goes through the from-stock install
// method; the tier-2 partition-write path below is for a board already on
// alkane.
if opts.flag("from-stock") {
@@ -976,26 +978,49 @@ fn resolve_afw_source(opts: &Opts) -> Result<PathBuf, Box<dyn std::error::Error>
.ok_or_else(|| "usage: alkane-cli afw install <file>|--latest --host <ip> --confirm FLASH".into())
}
/// Return an alkane board to genuine stock via the sealed stock-restore method,
/// then leave the independent verification to the operator/bench.
/// Return an alkane board to genuine Bitmain stock by writing the stock images
/// from a genuine vendor firmware package back to NAND, over the network.
fn run_restore_stock(args: &[String]) -> Result<(), Box<dyn std::error::Error>> {
let opts = parse_args(args, &[ssh_values(), &["confirm"]].concat(), &["dry-run"])?;
let host = opts.value("host").ok_or("--host is required")?;
// Restore runs over the network against the alkane board, so it needs the
// board's root credential; it is passed to the compiled-in method in-process.
let opts = parse_args(
args,
&[ssh_values(), &["confirm", "stock-firmware"]].concat(),
&["dry-run"],
)?;
let host = opts.value("host").ok_or("--host is required")?.to_owned();
let firmware = opts.value("stock-firmware").ok_or(
"--stock-firmware <genuine Bitmain firmware file> is required \
(SD recovery image SD_Antminer-*.zip, which includes the device tree)",
)?;
// Restore reaches the alkane board over SSH as root; the board credential is
// supplied via --password-file / ALKANE_BOARD_PASSWORD(_FILE).
let password = read_board_password(&opts)?;
// Parse the genuine vendor package into the stock images to write back.
let fw = stock_firmware::parse(Path::new(firmware))?;
let dtb = fw.dtb.as_deref().ok_or(
"this firmware package has no device tree; use the Bitmain SD recovery \
image (SD_Antminer-S17-*.zip), which includes devicetree.dtb",
)?;
println!("stock firmware: {}", fw.summary());
if opts.flag("dry-run") {
println!("DRY RUN: would restore {host} to stock via the network-only restore-to-stock method");
println!("DRY RUN: would restore {host} to stock by writing the stock dtb, kernel and");
println!("rootfs back to NAND (BOOT.bin preserved). Re-run with --confirm FLASH.");
return Ok(());
}
if opts.value("confirm") != Some("FLASH") {
return Err("refusing to restore without --confirm FLASH".into());
}
println!("restoring {host} to stock (network-only method)...");
stock::restore_to_stock(host, password.as_deref())?;
println!("restore-to-stock reported success.");
println!("verify independently: the board should no longer answer as alkane (no alkane console/API),");
println!("and should present its genuine stock OS. Confirm over the network or serial before reuse.");
println!("restoring {host} to genuine stock from {firmware} (network-only)...");
let images = RestoreImages {
dtb,
kernel: &fw.kernel,
rootfs: &fw.rootfs,
};
stock::restore_to_stock(&host, &images, password.as_deref())?;
println!("restore-to-stock reported success (stock images written, BOOT.bin preserved).");
println!("verify independently: the board should no longer answer as alkane and should");
println!("present genuine stock (lighttpd 401 on :80, TCP 22 closed).");
Ok(())
}