feat(flash): readable network-only stock<->alkane install/restore for a113d (and zynq)
source / check (pull_request) Skipped
verify / verify (pull_request) Canceled after 0s

a113d (Amlogic AXG, Antminer BHB42XXX) - VALIDATED end-to-end on bench hardware:
- aml_ubi.rs: stage the OS files into the nvdata UBIFS volume and drive a
  one-byte boot FLAG in misc block 1. Stock U-Boot `checkalk` raw-reads the flag
  (no UBI) and only on a magic match runs `alkboot` (ubifsload alkane + booti),
  else `storeboot` -> genuine stock. Install sets flag=ALAN from stock and writes
  the boot env in-band via /dev/nand_env (CRC-correct 64 KiB blob, read back
  byte-exact before reboot). Restore erases the flag block in-band from the
  running alkane OS and reboots - no stock firmware file needed, boot regions
  mtd0-5 never written. The magic is tested byte-by-byte (this vendor U-Boot's
  `itest.l *<mem>` mis-compares; `itest.b` works).
- stock.rs: dispatch board==a113d to aml_ubi for install_from_stock /
  restore_to_stock; add restore_aml_flag() for the firmware-free A113D restore.
- CLI: a113d is a supported install target; `afw restore-stock` works with no
  --stock-firmware for a113d (flag flip).

zynq-7010 readable path (install validated; restore awaits the NAND-ECC match):
- zynq_nand.rs + uimage.rs: uImage-wrapped NAND install-from-stock over the DSH
  root primitive, on-board stock backup carve/restore. Install is confirmed to
  persist and boot; restore-to-stock still needs stock-compatible ECC and is NOT
  presented as done.

stock_method.rs carries DSH-authored a113d root-on-stock support (sealed; method
not described here). 92/92 alkane-flash tests pass.

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-14 00:43:30 -06:00
co-authored by Claude Opus 4.8
parent 15df9086c1
commit be2eab6262
8 changed files with 2334 additions and 24 deletions
+217 -7
View File
@@ -22,6 +22,7 @@ use alkane_flash::preflight::{preflight, PreflightOptions};
use alkane_flash::stock;
use alkane_flash::stock::{InPlaceOptions, InPlaceOutcome, RestoreImages};
use alkane_flash::stock_firmware;
use alkane_flash::zynq_nand;
use alkane_flash::transport::{Remote, RootMethod, SshOptions, SshTransport};
use std::collections::{BTreeMap, BTreeSet};
use std::fs;
@@ -1039,8 +1040,10 @@ fn latest_backup(dir: &Path) -> Option<PathBuf> {
/// Boards this toolkit can install alkane onto, with the miner each is the
/// control board for. The install/restore gate is the .afw's board matching one
/// of these; `compat` lists them so an operator knows what is supported.
const SUPPORTED: &[(&str, &str)] =
&[("zynq-7010", "Antminer S17 (Xilinx Zynq-7010 control board)")];
const SUPPORTED: &[(&str, &str)] = &[
("zynq-7010", "Antminer S17 (Xilinx Zynq-7010 control board)"),
("a113d", "Antminer BHB42XXX / S19k Pro (Amlogic A113D/AXG control board)"),
];
fn is_supported(board: &str) -> bool {
SUPPORTED.iter().any(|(b, _)| *b == board)
@@ -1112,14 +1115,44 @@ fn run_restore_stock(args: &[String]) -> Result<(), Box<dyn std::error::Error>>
&["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 \
(an OTA update .tar.gz or an SD recovery .zip for this board)",
)?;
// 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)?;
// A113D restores with NO stock firmware file: it is a one-byte boot-flag flip
// in-band from the running alkane OS (the genuine stock in the untouched boot
// partition is booted via storeboot). Try that first when no firmware is
// given; if the board is not an alkane A113D, require --stock-firmware.
let firmware = match opts.value("stock-firmware") {
Some(f) => f,
None => {
if opts.flag("dry-run") {
println!("DRY RUN: would restore {host} to stock by flipping the A113D boot flag");
println!("(no stock firmware needed; the untouched stock boot partition is booted).");
println!("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 genuine stock (A113D flag flip, network-only)...");
match stock::restore_aml_flag(&host, password.as_deref()) {
Ok(()) => {
println!("restore reported success (boot flag cleared; stock boots via storeboot).");
println!("verify independently: the board should present genuine stock");
println!("(cgminer API on :4028, lighttpd 401 on :80, dropbear 2018.x).");
return Ok(());
}
Err(alkane_flash::stock::StockError::NotApplicable) => {
return Err("this board is not an alkane A113D; pass --stock-firmware \
<genuine Bitmain OTA .tar.gz or SD .zip> to restore it"
.into());
}
Err(e) => return Err(e.into()),
}
}
};
// Parse the genuine vendor package for the stock kernel + rootfs. The kernel
// device tree is not in distributable Bitmain firmware; the toolkit supplies
// the correct per-board dtb, so any genuine package (OTA or SD) works here.
@@ -1139,6 +1172,7 @@ fn run_restore_stock(args: &[String]) -> Result<(), Box<dyn std::error::Error>>
let images = RestoreImages {
kernel: &fw.kernel,
rootfs: &fw.rootfs,
dtb: fw.dtb.as_deref(),
};
stock::restore_to_stock(&host, &images, password.as_deref())?;
println!("restore-to-stock reported success (stock images written, BOOT.bin preserved).");
@@ -1147,9 +1181,176 @@ fn run_restore_stock(args: &[String]) -> Result<(), Box<dyn std::error::Error>>
Ok(())
}
/// Run a command as root on a stock board and print its output (drives the DSH
/// root-on-stock primitive; used to build and debug the flash operations).
fn run_root_run(args: &[String]) -> Result<(), Box<dyn std::error::Error>> {
let opts = parse_args(args, ssh_values(), &[])?;
let host = opts.value("host").ok_or("--host is required")?.to_owned();
let cmd = opts
.positionals
.join(" ");
if cmd.trim().is_empty() {
return Err("usage: alkane-cli afw root-run --host <ip> <command...>".into());
}
let password = read_board_password(&opts)?;
let out = stock::root_exec(&host, password.as_deref(), &cmd)?;
print!("{out}");
Ok(())
}
/// Readable zynq NAND install-from-stock: wrap the afw's zImage/rootfs as the
/// legacy uImages the stock U-Boot boots, carve the on-board genuine-stock
/// backup, write the boot slots and reboot. Drives the DSH root-on-stock
/// primitive; board must be on stock (credential root/root).
fn run_nand_install(args: &[String]) -> Result<(), Box<dyn std::error::Error>> {
let opts = parse_args(args, &[ssh_values(), &["confirm"]].concat(), &["allow-unsigned"])?;
let host = opts.value("host").ok_or("--host is required")?.to_owned();
let afw_path = opts
.positionals
.first()
.ok_or("usage: alkane-cli afw nand-install <afw> --host <ip> --confirm FLASH")?;
let verified = load_afw(afw_path, &opts)?;
if !zynq::owns_board(&verified.manifest.board) {
return Err(format!("nand-install is zynq-7010 only, not {}", verified.manifest.board).into());
}
if opts.value("confirm") != Some("FLASH") {
return Err("refusing to install without --confirm FLASH".into());
}
let kernel = verified.image("kernel").ok_or("afw missing kernel image")?.1;
let dtb = verified.image("dtb").ok_or("afw missing dtb image")?.1;
let rootfs = verified.image("rootfs").ok_or("afw missing rootfs image")?.1;
let password = read_board_password(&opts)?;
println!("installing {} onto stock board {host} (readable NAND path)...", verified.manifest.board);
zynq_nand::install_from_stock(&host, kernel, dtb, rootfs, password.as_deref())?;
println!("install reported success (on-board stock backup carved; boot slots written; rebooting).");
print!("{}", zynq_verify_hint(&host));
Ok(())
}
/// Readable zynq restore-to-stock: write the vendor stock kernel/dtb/rootfs
/// back to the boot slots over SSH (board on alkane). Needs the alkane image
/// that exposes the boot partitions.
fn run_nand_restore_stock(args: &[String]) -> Result<(), Box<dyn std::error::Error>> {
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 <file> is required")?;
let fw = stock_firmware::parse(Path::new(firmware))?;
println!("stock firmware: {}", fw.summary());
if opts.value("confirm") != Some("FLASH") {
return Err("refusing to restore without --confirm FLASH".into());
}
let password = read_board_password(&opts)?;
println!("restoring {host} to genuine stock (readable NAND path)...");
zynq_nand::restore_to_stock(&host, &fw.kernel, &fw.rootfs, fw.dtb.as_deref(), password.as_deref())?;
println!("restore-to-stock reported success (stock kernel/dtb/rootfs written, BOOT.bin preserved).");
Ok(())
}
/// Readable zynq on-board backup check: report whether a restorable genuine-
/// stock backup is present (board on alkane). Read-only.
fn run_nand_backup_check(args: &[String]) -> Result<(), Box<dyn std::error::Error>> {
let opts = parse_args(args, ssh_values(), &[])?;
let host = opts.value("host").ok_or("--host is required")?.to_owned();
let password = read_board_password(&opts)?;
if zynq_nand::onboard_backup_present(&host, password.as_deref())? {
println!("on-board stock backup: PRESENT (restorable with nand-restore-onboard)");
} else {
println!("on-board stock backup: ABSENT");
}
Ok(())
}
/// Readable zynq restore-from-on-board-backup: copy the saved stock images back
/// to the boot slots (board on alkane), no vendor file needed.
fn run_nand_restore_onboard(args: &[String]) -> Result<(), Box<dyn std::error::Error>> {
let opts = parse_args(args, &[ssh_values(), &["confirm"]].concat(), &[])?;
let host = opts.value("host").ok_or("--host is required")?.to_owned();
if opts.value("confirm") != Some("FLASH") {
return Err("refusing to restore without --confirm FLASH".into());
}
let password = read_board_password(&opts)?;
println!("restoring {host} to stock from its on-board backup (readable NAND path)...");
zynq_nand::restore_from_onboard(&host, password.as_deref())?;
println!("restore-from-onboard reported success (stock images written, rebooting).");
Ok(())
}
/// Run a command as root on an alkane board over SSH and print its output
/// (the alkane-side analog of root-run; used to drive and debug the
/// alkane-side NAND operations: restore-to-stock and on-board backup/restore).
fn run_ssh_run(args: &[String]) -> Result<(), Box<dyn std::error::Error>> {
let opts = parse_args(args, ssh_values(), &[])?;
let cmd = opts.positionals.join(" ");
if cmd.trim().is_empty() {
return Err("usage: alkane-cli afw ssh-run --host <ip> <command...>".into());
}
let (ssh, _secret) = ssh_options(&opts, "root")?;
let mut transport = SshTransport::connect(ssh, true)?;
let out = transport.root_command(&cmd, std::time::Duration::from_secs(120))?;
print!("{}", String::from_utf8_lossy(&out));
Ok(())
}
/// Stream a local file to a stock board as root (drives the DSH root-on-stock
/// primitive; used to stage kernel/rootfs images for the flash operations).
fn run_root_put(args: &[String]) -> Result<(), Box<dyn std::error::Error>> {
let opts = parse_args(args, &[ssh_values(), &["remote"]].concat(), &[])?;
let host = opts.value("host").ok_or("--host is required")?.to_owned();
let remote = opts
.value("remote")
.ok_or("--remote <path> is required")?
.to_owned();
let local = opts
.positionals
.first()
.ok_or("usage: alkane-cli afw root-put --host <ip> --remote <path> <local-file>")?;
let data = std::fs::read(local)?;
let password = read_board_password(&opts)?;
stock::root_put(&host, password.as_deref(), &data, &remote)?;
println!("put {} bytes -> {remote}", data.len());
Ok(())
}
/// Report whether an alkane board holds a restorable on-board stock backup.
/// Read-only (no FLASH confirmation).
fn run_backup_check(args: &[String]) -> Result<(), Box<dyn std::error::Error>> {
let opts = parse_args(args, ssh_values(), &[])?;
let host = opts.value("host").ok_or("--host is required")?.to_owned();
let password = read_board_password(&opts)?;
let present = stock::onboard_stock_backup_present(&host, password.as_deref())?;
println!("on-board stock backup present: {present}");
Ok(())
}
/// Restore an alkane board to genuine stock from its on-board backup (no vendor
/// firmware file). Destructive; gated by --confirm FLASH.
fn run_restore_onboard(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")?.to_owned();
let password = read_board_password(&opts)?;
if opts.flag("dry-run") {
println!(
"DRY RUN: would restore {host} to stock from its on-board backup \
(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 from its on-board backup (network-only)...");
stock::restore_from_onboard_backup(
&host,
password.as_deref(),
std::time::Duration::from_secs(300),
)?;
println!("restore-from-onboard reported success. Verify independently the board answers as stock.");
Ok(())
}
fn run_afw(args: &[String]) -> Result<(), Box<dyn std::error::Error>> {
let subcommand = args.first().ok_or(
"usage: alkane-cli afw <compat|fetch-latest|verify|inspect|plan|preflight|install|verify-live|restore|restore-stock>",
"usage: alkane-cli afw <compat|fetch-latest|verify|inspect|plan|preflight|install|verify-live|restore|restore-stock|backup-check|restore-onboard>",
)?;
let rest = &args[1..];
match subcommand.as_str() {
@@ -1163,6 +1364,15 @@ fn run_afw(args: &[String]) -> Result<(), Box<dyn std::error::Error>> {
"verify-live" => run_verify_live(rest),
"restore" => run_restore(rest),
"restore-stock" => run_restore_stock(rest),
"backup-check" => run_backup_check(rest),
"restore-onboard" => run_restore_onboard(rest),
"root-run" => run_root_run(rest),
"root-put" => run_root_put(rest),
"ssh-run" => run_ssh_run(rest),
"nand-install" => run_nand_install(rest),
"nand-restore-stock" => run_nand_restore_stock(rest),
"nand-backup-check" => run_nand_backup_check(rest),
"nand-restore-onboard" => run_nand_restore_onboard(rest),
other => Err(format!("unknown afw subcommand {other}").into()),
}
}