feat(cli): afw make-media -- build stock/alkane SD cards
`alkane-cli afw make-media` wraps the sd_media builders:
--kind stock --stock-package <zip|dir> --out card.img
--kind alkane --mode simpleboot|installer --afw <f.afw> --out card.img
[--allow-unsigned] [--write /dev/sdX --confirm WRITE]
Zynq-7010 only for now. A .zip stock package is unpacked to a temp dir; a
directory is used as-is. Writing to a real device requires --confirm WRITE.
Verified end-to-end: building a stock card from the genuine vendor S17
package yields a card image byte-identical in content/sizes to the hand-built
card that recovered the bench S17 on 2026-11-17.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B1W6qKwKxq3oUPDKWicM6i
This commit is contained in:
@@ -1388,9 +1388,118 @@ fn run_restore_onboard(args: &[String]) -> Result<(), Box<dyn std::error::Error>
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Build a bootable SD installation-media card image (and optionally write it to
|
||||
/// a device). Zynq-7010 only for now.
|
||||
///
|
||||
/// afw make-media --kind stock --stock-package <zip|dir> --out card.img
|
||||
/// afw make-media --kind alkane --mode simpleboot|installer --afw <f.afw> --out card.img
|
||||
/// [--allow-unsigned] [--write /dev/sdX --confirm WRITE]
|
||||
fn run_make_media(args: &[String]) -> Result<(), Box<dyn std::error::Error>> {
|
||||
use alkane_flash::sd_media;
|
||||
let opts = parse_args(
|
||||
args,
|
||||
&[
|
||||
"kind",
|
||||
"mode",
|
||||
"afw",
|
||||
"stock-package",
|
||||
"board",
|
||||
"out",
|
||||
"write",
|
||||
"confirm",
|
||||
"expect-board",
|
||||
],
|
||||
&["allow-unsigned"],
|
||||
)?;
|
||||
let kind = opts
|
||||
.value("kind")
|
||||
.ok_or("usage: alkane-cli afw make-media --kind <stock|alkane> ... --out <card.img>")?;
|
||||
let board = opts.value("board").unwrap_or("zynq-7010");
|
||||
if board != "zynq-7010" {
|
||||
return Err(format!("make-media supports --board zynq-7010 only (got {board})").into());
|
||||
}
|
||||
let out = opts
|
||||
.value("out")
|
||||
.map(PathBuf::from)
|
||||
.or_else(|| opts.positionals.first().map(PathBuf::from))
|
||||
.ok_or("make-media: --out <card.img> required")?;
|
||||
let now = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map(|d| d.as_secs() as u32)
|
||||
.unwrap_or(0);
|
||||
|
||||
match kind {
|
||||
"stock" => {
|
||||
let pkg = opts
|
||||
.value("stock-package")
|
||||
.ok_or("make-media --kind stock needs --stock-package <zip|dir>")?;
|
||||
let pkg_path = Path::new(pkg);
|
||||
// Unpack a .zip to an ephemeral dir; use a directory as-is.
|
||||
let staged;
|
||||
let dir: PathBuf = if pkg_path.is_dir() {
|
||||
pkg_path.to_path_buf()
|
||||
} else if pkg_path
|
||||
.extension()
|
||||
.is_some_and(|e| e.eq_ignore_ascii_case("zip"))
|
||||
{
|
||||
staged =
|
||||
std::env::temp_dir().join(format!("alkane-media-pkg-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&staged);
|
||||
std::fs::create_dir_all(&staged)?;
|
||||
let status = std::process::Command::new("unzip")
|
||||
.args(["-o", "-q"])
|
||||
.arg(pkg_path)
|
||||
.arg("-d")
|
||||
.arg(&staged)
|
||||
.status()?;
|
||||
if !status.success() {
|
||||
return Err(format!("unzip {pkg} failed").into());
|
||||
}
|
||||
staged
|
||||
} else {
|
||||
return Err(
|
||||
format!("--stock-package must be a directory or a .zip (got {pkg})").into(),
|
||||
);
|
||||
};
|
||||
sd_media::build_stock_recovery_card(&dir, &out)?;
|
||||
}
|
||||
"alkane" => {
|
||||
let afw_path = opts
|
||||
.value("afw")
|
||||
.ok_or("make-media --kind alkane needs --afw <file.afw>")?;
|
||||
let verified = load_afw(afw_path, &opts)?;
|
||||
match opts.value("mode").unwrap_or("simpleboot") {
|
||||
"simpleboot" | "simple-boot" => {
|
||||
sd_media::build_zynq_simpleboot_card(&verified, &out, now)?
|
||||
}
|
||||
"installer" => sd_media::build_zynq_installer_card(&verified, &out, now)?,
|
||||
other => {
|
||||
return Err(format!("--mode must be simpleboot|installer (got {other})").into())
|
||||
}
|
||||
}
|
||||
}
|
||||
other => return Err(format!("--kind must be stock|alkane (got {other})").into()),
|
||||
}
|
||||
println!("wrote card image: {}", out.display());
|
||||
|
||||
if let Some(dev) = opts.value("write") {
|
||||
if opts.value("confirm") != Some("WRITE") {
|
||||
return Err(format!(
|
||||
"refusing to write to {dev} without --confirm WRITE (card image is at {})",
|
||||
out.display()
|
||||
)
|
||||
.into());
|
||||
}
|
||||
eprintln!("writing {} to {dev} ...", out.display());
|
||||
sd_media::write_image_to_device(&out, Path::new(dev))?;
|
||||
println!("written to {dev}; sync complete");
|
||||
}
|
||||
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|backup-check|restore-onboard>",
|
||||
"usage: alkane-cli afw <compat|fetch-latest|verify|inspect|plan|preflight|install|verify-live|restore|restore-stock|backup-check|restore-onboard|make-media>",
|
||||
)?;
|
||||
let rest = &args[1..];
|
||||
match subcommand.as_str() {
|
||||
@@ -1413,6 +1522,7 @@ fn run_afw(args: &[String]) -> Result<(), Box<dyn std::error::Error>> {
|
||||
"nand-restore-stock" => run_nand_restore_stock(rest),
|
||||
"nand-backup-check" => run_nand_backup_check(rest),
|
||||
"nand-restore-onboard" => run_nand_restore_onboard(rest),
|
||||
"make-media" => run_make_media(rest),
|
||||
other => Err(format!("unknown afw subcommand {other}").into()),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user