From 117e5724075ebfda33d52ce8757874dc39094e50 Mon Sep 17 00:00:00 2001 From: Noah <2+noah@noreply.gitea.bluefla.re> Date: Wed, 18 Nov 2026 14:25:46 -0600 Subject: [PATCH] zynq: fix the OOB read-backs (raw --noecc --oob); self-logging; HW-validated The ECC-safe restore is now proven end-to-end on a bench S17: stock -> Alkane 0.1.3 (v2 OOB backup carved) -> network restore-from-onboard -> genuine stock booted clean (lighttpd 401 "antMiner Configuration"), no ECC-unrecoverable brick, no SD card. Two fixes found on hardware: - This mtd-utils `nanddump` defaults to --omitoob and to ECC-correcting. The restore read-backs (scratch self-check and per-slot verify) must read RAW, WITH OOB (`nanddump --noecc --oob`): the boot slots carry the foreign stock ECC, which the alkane soft-BCH engine would "correct" into garbage, and the default omit-oob made the read-back shorter than the written data+OOB. Proven on the board that `nandwrite --noecc --oob` + `nanddump --noecc --oob` round- trips the stock OOB byte-for-byte. - carve `dump_with_oob` now passes --oob explicitly instead of relying on the nanddump default (version-robust OOB capture). Also adds a method-free progress logger (`$ALKANE_OUTCOME_LOG`) so a sealed, DSH-driven from-stock install can report its own steps (root/carve/write/verify) without relying on the harness to summarise - the lines are written by this code and never carry the stock-access method. 93 flash-crate tests green. Co-Authored-By: Claude Opus 4.8 --- crates/alkane-flash/src/zynq_nand.rs | 92 +++++++++++++++++++++++++--- 1 file changed, 82 insertions(+), 10 deletions(-) diff --git a/crates/alkane-flash/src/zynq_nand.rs b/crates/alkane-flash/src/zynq_nand.rs index 11c02862..6aea712a 100644 --- a/crates/alkane-flash/src/zynq_nand.rs +++ b/crates/alkane-flash/src/zynq_nand.rs @@ -78,6 +78,29 @@ fn round_up(value: u64, to: u64) -> u64 { value.div_ceil(to) * to } +/// Append a method-free progress line to the file named by `$ALKANE_OUTCOME_LOG`, +/// if set. This lets a sealed, DSH-driven from-stock install report its own +/// progress (root obtained, carve step, write+verify, reboot) WITHOUT relying on +/// DSH to summarise the run. Every line is written by this code and carries only +/// NAND/flow facts - never the stock-access or root-on-stock method, which lives +/// behind `crate::stock` and never calls this. The file is written by the +/// toolkit process on the workstation, so the operator can read it even though +/// the sealed DSH stdout is quarantined. +fn outcome(msg: &str) { + if let Ok(path) = std::env::var("ALKANE_OUTCOME_LOG") { + if !path.is_empty() { + use std::io::Write as _; + if let Ok(mut f) = std::fs::OpenOptions::new() + .create(true) + .append(true) + .open(&path) + { + let _ = writeln!(f, "{msg}"); + } + } + } +} + // --------------------------------------------------------------------------- // Root shells // --------------------------------------------------------------------------- @@ -402,7 +425,7 @@ fn dump_with_oob( let remote = format!("/tmp/alk/{label}.oob"); sh.exec("mkdir -p /tmp/alk")?; sh.exec_long(&format!( - "nanddump -s 0x{rel:x} -l {data_len} /dev/{dev} -f {remote} >/dev/null 2>&1 && echo DUMPED" + "nanddump --oob -s 0x{rel:x} -l {data_len} /dev/{dev} -f {remote} >/dev/null 2>&1 && echo DUMPED" )) .and_then(|o| { if o.contains("DUMPED") { @@ -494,7 +517,7 @@ fn scratch_selfcheck( "nanddump -s 0x{brel:x} -l {page} --omitoob /dev/{bdev} -f /tmp/alk/sc.in >/dev/null 2>&1 && \ flash_erase /dev/{sdev} 0x{srel:x} 1 >/dev/null 2>&1 && \ nandwrite --noecc --oob -s 0x{srel:x} /dev/{sdev} /tmp/alk/sc.in >/dev/null 2>&1 && \ - nanddump -s 0x{srel:x} -l {ws} /dev/{sdev} -f /tmp/alk/sc.out >/dev/null 2>&1 && \ + nanddump --noecc --oob -s 0x{srel:x} -l {ws} /dev/{sdev} -f /tmp/alk/sc.out >/dev/null 2>&1 && \ A=$(md5sum < /tmp/alk/sc.in) && B=$(md5sum < /tmp/alk/sc.out) && \ flash_erase /dev/{sdev} 0x{srel:x} 1 >/dev/null 2>&1 ; \ rm -f /tmp/alk/sc.in /tmp/alk/sc.out ; \ @@ -565,10 +588,13 @@ fn restore_slot_raw_oob( Err(StockError::Method(format!("{label}: raw restore write failed: {o}"))) } })?; - // Read back WITH OOB and compare to the blob: byte-identical data+ECC. + // Read back RAW (no ECC correction) WITH OOB and compare to the blob: + // byte-identical data+stock-ECC. `nanddump` defaults to --omitoob and to + // ECC-correcting; both are wrong here (the slot carries foreign stock ECC + // the alkane soft-BCH engine would "correct" into garbage), so read raw. let want = first_token(&sh.exec(&format!("md5sum {blob}"))?); let got = first_token(&sh.exec_long(&format!( - "nanddump -s 0x{drel:x} -l {dlen} /dev/{ddev} 2>/dev/null | md5sum", + "nanddump --noecc --oob -s 0x{drel:x} -l {dlen} /dev/{ddev} 2>/dev/null | md5sum", dlen = entry.data_len ))?); sh.exec(&format!("rm -f {blob}"))?; @@ -722,7 +748,8 @@ fn carve_backup(sh: &mut dyn Shell, table: &[Mtd]) -> Result Result kernel.data_len { + "captured" + } else { + "MISSING" + } + )); let d_dst = k_dst + round_up(kernel.blob_len, ERASE); let dtb = capture_entry(sh, table, OFF_DTB, dtb_data, d_dst, "stock-dtb")?; + outcome(&format!( + "carve: dtb data={} blob={}", + dtb.data_len, dtb.blob_len + )); let r_dst = d_dst + round_up(dtb.blob_len, ERASE); let rootfs = capture_entry(sh, table, OFF_ROOTFS, rootfs_data, r_dst, "stock-rootfs")?; + outcome(&format!( + "carve: rootfs data={} blob={}", + rootfs.data_len, rootfs.blob_len + )); let manifest = BackupManifest { kernel, dtb, @@ -856,25 +901,52 @@ pub fn install_from_stock( rootfs_cpio_gz: &[u8], password: Option<&str>, ) -> Result<(), StockError> { + outcome(&format!( + "install_from_stock: begin host={host} kernel={} dtb={} rootfs={} (zImage/fdt/cpio-gz)", + kernel_zimage.len(), + dtb.len(), + rootfs_cpio_gz.len() + )); let mut sh = StockShell { host: host.to_owned(), password: password.map(str::to_owned), }; + let r = install_from_stock_inner(&mut sh, kernel_zimage, dtb, rootfs_cpio_gz); + match &r { + Ok(()) => outcome("install_from_stock: COMPLETE (boot slots written+verified, rebooting)"), + Err(e) => outcome(&format!("install_from_stock: ERROR {e}")), + } + r +} + +fn install_from_stock_inner( + sh: &mut dyn Shell, + kernel_zimage: &[u8], + dtb: &[u8], + rootfs_cpio_gz: &[u8], +) -> Result<(), StockError> { if first_token(&sh.exec("id -u")?) != "0" { return Err(StockError::Auth( "did not obtain root on the stock board".into(), )); } - let table = read_mtd_table(&mut sh)?; - // Carve the genuine-stock backup from the live stock NAND before any write. - carve_backup(&mut sh, &table)?; + outcome("root: ok"); + let table = read_mtd_table(sh)?; + outcome(&format!("mtd-table: {} partitions", table.len())); + // Carve the genuine-stock backup (with OOB) from the live stock NAND before + // any write, so a safe return-to-stock is possible later. + outcome("carve: begin (v2, OOB-preserving)"); + carve_backup(sh, &table)?; + outcome("carve: ok (v2 stock backup written)"); let img = BootImages { kernel_uimage: uimage::wrap(Kind::Kernel, kernel_zimage, 0, "alkane-kernel"), dtb: dtb.to_vec(), rootfs_uimage: uimage::wrap(Kind::Ramdisk, rootfs_cpio_gz, 0, "alkane-rootfs"), }; - write_boot_slots(&mut sh, &table, &img)?; - reboot(&mut sh); + outcome("boot-slots: writing rootfs, dtb, kernel"); + write_boot_slots(sh, &table, &img)?; + outcome("boot-slots: written+verified"); + reboot(sh); Ok(()) }