diff --git a/crates/alkane-gui/src/stock_ops.rs b/crates/alkane-gui/src/stock_ops.rs index 04a338e9..81359dda 100644 --- a/crates/alkane-gui/src/stock_ops.rs +++ b/crates/alkane-gui/src/stock_ops.rs @@ -336,3 +336,74 @@ pub fn start_restore(ips: Vec, firmware: PathBuf, password: Option) -> 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, password: Option) -> 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 } +} diff --git a/crates/alkane-gui/src/ui.rs b/crates/alkane-gui/src/ui.rs index 89543edc..027e232b 100644 --- a/crates/alkane-gui/src/ui.rs +++ b/crates/alkane-gui/src/ui.rs @@ -116,6 +116,8 @@ const ID_INSTALL_RELOAD: i32 = 1012; const ID_LOG_TOGGLE: i32 = 1013; const ID_UNINSTALL_OK: i32 = 1009; const ID_UNINSTALL_CANCEL: i32 = 1010; +// Browse for a stock firmware file in the uninstall/restore dialog. +const ID_UNINSTALL_BROWSE: i32 = 1014; // Pool-preset manager dialog (round 18 #8b): edit the whole saved list at once. // NB: 790-795 belong to the language menu (ID_LANG_BASE + Lang index), so these // live in a separate high block to avoid an id collision (round 18 #12). @@ -973,6 +975,9 @@ pub struct App { report_text: Widget, report_log: Widget, report_status: Widget, + // After a report is filed this button becomes the clickable "issue #N" link; + // hidden until then (issue #50: no always-present Open-issue button). + report_open_btn: Widget, report_rx: Option>>, report_url: Option, // Restore-to-stock (round 12 #8a): background job + its typed-confirm dialog. @@ -985,7 +990,6 @@ pub struct App { stock_op: Option, install_dialog: Widget, install_release_choice: Widget, - install_pass_input: Widget, install_unsigned_check: Widget, install_releases: Vec, install_ips: Vec, @@ -1006,8 +1010,11 @@ pub struct App { // blueflare/alkane-os, editable + reloadable to any compatible repo. install_repo_input: Widget, uninstall_dialog: Widget, - uninstall_pass_input: Widget, uninstall_firmware_input: Widget, + // Restore-to-stock source: the board's on-board stock backup (checkbox, + // greyed when absent) vs the firmware file below. + uninstall_onboard_check: Widget, + uninstall_onboard_available: bool, uninstall_ips: Vec, // Configure-pools dialog (round 14 #7, expanded round 15 #1): 3 pool rows x 4 // fields (address, account, worker, password) = 12 text controls, indexed @@ -1394,6 +1401,7 @@ pub fn build(frame: Widget) -> App { report_text: frame, report_log: frame, report_status: frame, + report_open_btn: frame, report_rx: None, report_url: None, restore: None, @@ -1402,7 +1410,6 @@ pub fn build(frame: Widget) -> App { stock_op: None, install_dialog: frame, install_release_choice: frame, - install_pass_input: frame, install_unsigned_check: frame, install_releases: Vec::new(), install_ips: Vec::new(), @@ -1413,8 +1420,9 @@ pub fn build(frame: Widget) -> App { install_plan_label: frame, install_repo_input: frame, uninstall_dialog: frame, - uninstall_pass_input: frame, uninstall_firmware_input: frame, + uninstall_onboard_check: frame, + uninstall_onboard_available: false, uninstall_ips: Vec::new(), pools_dialog: frame, pool_fields: Vec::new(), @@ -2729,15 +2737,33 @@ impl App { /// True when any hardware-touching job is in flight. A new install/uninstall /// must not start while a scan, config action, refresh, flash, restore, or /// another stock op is running (they would race on the same boards). - fn hardware_busy(&self) -> bool { + /// True when a flash/restore-level operation is in flight (ignores an + /// in-flight scan, which an install/uninstall + /// dialog may be opened and prepared while a scan runs (issue #43). The scan + /// is stopped by `stop_scan_for_op` only when the operation actually starts. + fn flash_busy(&self) -> bool { self.flashing - || self.scanning || self.action.is_some() || self.refresh.is_some() || self.restore.is_some() || self.stock_op.is_some() } + /// Cancel any in-flight scan before a flash/restore starts, so the two never + /// probe/write the same board at once (issue #43). + fn stop_scan_for_op(&mut self) { + if self.scanning || self.scan.is_some() { + self.playing = false; + self.scan_stopped = true; + self.countdown = 0; + self.scanning = false; + if let Some(old) = self.scan.take() { + old.stop(); + } + wx_rs::set_status_text("scan stopped for the flash operation"); + } + } + /// Build the "Install alkane OS" dialog (or, with `update`, the "Update /// alkane OS" dialog for boards already running it): a scanned list of /// alkane-os releases to pick from, an optional board credential, the @@ -2805,7 +2831,7 @@ impl App { } fn build_install_dialog(&mut self, update: bool) -> Option { - if self.hardware_busy() { + if self.flash_busy() { wx_rs::set_status_text("busy -- try again once the current operation finishes"); return None; } @@ -2953,15 +2979,8 @@ impl App { root.add(plan, 0, sf::BORDER_ALL, 12); self.install_plan_label = plan; - root.add( - Widget::static_text(dlg, "Board password (optional):"), - 0, - sf::BORDER_LEFT | sf::BORDER_RIGHT | sf::BORDER_TOP, - 12, - ); - let pass = Widget::text_ctrl(dlg, false); - root.add(pass, 0, sf::EXPAND | sf::BORDER_LEFT | sf::BORDER_RIGHT, 12); - self.install_pass_input = pass; + // No password field (issue #46): the board login comes from Settings (or + // the firmware's shipped default when unset). // Current bench .afw files are unsigned (SHA-256 integrity, no Ed25519). let unsigned = Widget::check_box(dlg, "Allow unsigned .afw (bench only)"); @@ -3010,7 +3029,7 @@ impl App { /// Apply the install confirmation: validate the typed keyword, resolve the /// chosen release asset, and start the network-only from-stock install. fn apply_install_confirm(&mut self) { - if self.hardware_busy() { + if self.flash_busy() { wx_rs::set_status_text("busy -- try again once the current operation finishes"); self.install_dialog.close(0); return; @@ -3051,14 +3070,11 @@ impl App { ) }; let allow_unsigned = self.install_unsigned_check.is_checked(); - let password = { - let p = self.install_pass_input.get_value(); - if p.is_empty() { - None - } else { - Some(p) - } - }; + // Board login from Settings; empty -> the firmware's shipped default + // applies inside the engine (issue #46). + let password = credentials::load_ssh_credentials() + .map(|(_, p)| p) + .filter(|p| !p.is_empty()); let ips = std::mem::take(&mut self.install_ips); if ips.is_empty() { self.set_error_indicator("no board selected to install onto; close this and reselect"); @@ -3075,6 +3091,7 @@ impl App { "installing alkane OS onto {} board(s) from {source_name}...", ips.len() )); + self.stop_scan_for_op(); self.set_op_status(&ips, "installing..."); // Remember these targets so a successful install arms the post-install // alkane verification (prevents the stock->alkane re-scan flicker). @@ -3092,7 +3109,7 @@ impl App { /// Build the "Uninstall alkane OS (restore to stock)" dialog: the board's /// alkane root credential and a genuine Bitmain firmware file. fn build_uninstall_dialog(&mut self) -> Option { - if self.hardware_busy() { + if self.flash_busy() { wx_rs::set_status_text("busy -- try again once the current operation finishes"); return None; } @@ -3118,28 +3135,68 @@ impl App { sf::BORDER_ALL, 12, ); + // Credentials come from Settings (the alkane root login), not this dialog + // (issue #46). Probe each selected board for a restorable on-board stock + // backup so the on-board option can be offered or greyed out (#48). + let cred = credentials::load_ssh_credentials() + .map(|(_, p)| p) + .filter(|p| !p.is_empty()); + let onboard_ok = ips + .iter() + .all(|ip| stock_ops::onboard_backup_present(ip, cred.as_deref())); + self.uninstall_onboard_available = onboard_ok; + + // Source 1: the board's own on-board stock backup (always shown; greyed + // when no backup is present) (#48). + let onboard = Widget::check_box( + dlg, + "Restore from the board's on-board stock backup (left by the install)", + ); + onboard.set_checked(onboard_ok); + onboard.set_enabled(onboard_ok); + root.add(onboard, 0, sf::BORDER_ALL, 12); + self.uninstall_onboard_check = onboard; root.add( Widget::static_text( dlg, - "Genuine Bitmain stock firmware file for this board\n(an OTA update .tar.gz or an SD recovery .zip):", + if onboard_ok { + "An on-board stock backup was detected -- this is the default." + } else { + "No on-board stock backup detected; restore from a file below." + }, + ), + 0, + sf::BORDER_LEFT | sf::BORDER_RIGHT, + 12, + ); + + // Source 2: a genuine Bitmain stock firmware file (any X17-series model); + // Browse to pick it (#47). + root.add( + Widget::static_text( + dlg, + "Or restore from a Bitmain stock firmware file\n(OTA update .tar.gz or SD recovery .zip, any X17-series model):", ), 0, sf::BORDER_LEFT | sf::BORDER_RIGHT | sf::BORDER_TOP, 12, ); - let fw = Widget::text_ctrl(dlg, false); - root.add(fw, 0, sf::EXPAND | sf::BORDER_LEFT | sf::BORDER_RIGHT, 12); - self.uninstall_firmware_input = fw; - - root.add( - Widget::static_text(dlg, "Board (alkane root) password (optional):"), + let fw_row = Widget::box_sizer(false); + fw_row.add( + Widget::button(dlg, "Browse firmware file...", ID_UNINSTALL_BROWSE), 0, - sf::BORDER_LEFT | sf::BORDER_RIGHT | sf::BORDER_TOP, + sf::BORDER_RIGHT, + 6, + ); + let fw = Widget::text_ctrl(dlg, false); + fw_row.add(fw, 1, sf::ALIGN_CENTRE_VERTICAL, 0); + self.uninstall_firmware_input = fw; + root.add_sizer( + fw_row, + 0, + sf::EXPAND | sf::BORDER_LEFT | sf::BORDER_RIGHT, 12, ); - let pass = Widget::text_ctrl(dlg, false); - root.add(pass, 0, sf::EXPAND | sf::BORDER_LEFT | sf::BORDER_RIGHT, 12); - self.uninstall_pass_input = pass; // No typed-FLASH step (issue #41): the dialog and the Restore button are // the confirmation. @@ -3175,42 +3232,60 @@ impl App { } fn apply_uninstall_confirm(&mut self) { - if self.hardware_busy() { + if self.flash_busy() { wx_rs::set_status_text("busy -- try again once the current operation finishes"); self.uninstall_dialog.close(0); return; } + // On-board backup restore takes precedence when its box is checked and a + // backup was detected; otherwise restore from the chosen firmware file. + let use_onboard = + self.uninstall_onboard_available && self.uninstall_onboard_check.is_checked(); let firmware = self.uninstall_firmware_input.get_value(); - let firmware = firmware.trim(); - if firmware.is_empty() { - wx_rs::set_status_text("a genuine Bitmain stock firmware file is required"); - return; - } - if !std::path::Path::new(firmware).is_file() { - wx_rs::set_status_text(&format!("firmware file not found: {firmware}")); - return; - } - let password = { - let p = self.uninstall_pass_input.get_value(); - if p.is_empty() { - None - } else { - Some(p) + let firmware = firmware.trim().to_string(); + if !use_onboard { + if firmware.is_empty() { + self.set_error_indicator( + "pick a stock firmware file (Browse), or check the on-board backup option", + ); + return; } - }; + if !std::path::Path::new(&firmware).is_file() { + self.set_error_indicator(&format!("firmware file not found: {firmware}")); + return; + } + } + // Alkane root credential comes from Settings; empty -> the method's + // shipped default applies (issue #46, #51). + let password = credentials::load_ssh_credentials() + .map(|(_, p)| p) + .filter(|p| !p.is_empty()); let ips = std::mem::take(&mut self.uninstall_ips); if ips.is_empty() { self.uninstall_dialog.close(0); return; } self.uninstall_dialog.close(1); - crate::logs::log(format!("restore {} alkane board(s) to stock", ips.len())); + self.stop_scan_for_op(); self.set_op_status(&ips, "restoring..."); - self.stock_op = Some(stock_ops::start_restore( - ips, - std::path::PathBuf::from(firmware), - password, - )); + if use_onboard { + crate::logs::log(format!( + "restore {} alkane board(s) to stock from on-board backup", + ips.len() + )); + self.stock_op = Some(stock_ops::start_restore_onboard(ips, password)); + } else { + crate::logs::log(format!( + "restore {} alkane board(s) to stock from {}", + ips.len(), + firmware + )); + self.stock_op = Some(stock_ops::start_restore( + ips, + std::path::PathBuf::from(firmware), + password, + )); + } } /// Drain the alkane install/uninstall job: progress onto the status bar, @@ -5454,8 +5529,11 @@ impl App { 8, ); let btns = Widget::box_sizer(false); - let open = Widget::button(dlg, &t("Open issue"), ID_REPORT_OPEN); + // Hidden until a report is filed; then it becomes the "issue #N" link. + let open = Widget::button(dlg, "", ID_REPORT_OPEN); + open.show(false); btns.add(open, 0, sf::BORDER_ALL, 6); + self.report_open_btn = open; btns.add_stretch(1); btns.add( Widget::button(dlg, &t("Close"), ID_REPORT_CLOSE), @@ -5539,16 +5617,18 @@ impl App { match result { Ok(filed) => { crate::logs::log(format!("bug report filed as issue #{}", filed.number)); - self.report_status.set_label(&format!( - "{} #{}", - t("Thank you. Filed as issue"), - filed.number - )); + self.report_status + .set_label(&t("Thank you for the report.")); self.report_text.set_value(""); self.report_kind.choice_set_selection(0); if !filed.url.is_empty() { self.report_url = Some(filed.url); } + // Turn the filed issue into a clickable link (issue #50). + self.report_open_btn + .set_label(&format!("{} #{}", t("View issue"), filed.number)); + self.report_open_btn.show(true); + self.report_dialog.layout(); } Err(e) => { crate::logs::log(format!("bug report not sent: {e}")); @@ -6234,6 +6314,17 @@ impl App { self.install_dialog.close(0); } ID_UNINSTALL_OK => self.apply_uninstall_confirm(), + ID_UNINSTALL_BROWSE => { + if let Some(path) = Widget::file_dialog( + self.uninstall_dialog, + "Choose a Bitmain stock firmware file (.tar.gz or .zip)", + ) { + self.uninstall_firmware_input.set_value(&path); + // Picking a file means restoring from the file, not the + // on-board backup. + self.uninstall_onboard_check.set_checked(false); + } + } ID_UNINSTALL_CANCEL => { self.uninstall_ips.clear(); self.uninstall_dialog.close(0); diff --git a/thirdparty/wx-rs/patches/0013-enable-control.patch b/thirdparty/wx-rs/patches/0013-enable-control.patch new file mode 100644 index 00000000..fdd9998a --- /dev/null +++ b/thirdparty/wx-rs/patches/0013-enable-control.patch @@ -0,0 +1,38 @@ +Subject: [PATCH 0013] linux: Enable/disable (grey out) a control + +--- a/cpp_src/widgets_bridge.cpp ++++ b/cpp_src/widgets_bridge.cpp +@@ -945,6 +945,11 @@ + ((wxCheckBox*)win)->SetValue(v != 0); + } + ++// Enable or disable (grey out) any window/control. ++void wxbr_enable(void* win, int on) { ++ ((wxWindow*)win)->Enable(on != 0); ++} ++ + // ---- check list box --------------------------------------------------------- + + void* wxbr_check_list_box(void* parent) { +--- a/src/widgets.rs ++++ b/src/widgets.rs +@@ -49,6 +49,7 @@ + pub fn wxbr_check_box(parent: *mut c_void, label: *const c_char) -> *mut c_void; + pub fn wxbr_check_box_get(win: *mut c_void) -> c_int; + pub fn wxbr_check_box_set(win: *mut c_void, v: c_int); ++ pub fn wxbr_enable(win: *mut c_void, on: c_int); + pub fn wxbr_check_list_box(parent: *mut c_void) -> *mut c_void; + pub fn wxbr_clb_append(win: *mut c_void, label: *const c_char); + pub fn wxbr_clb_clear(win: *mut c_void); +@@ -508,6 +509,11 @@ + unsafe { notsafe::wxbr_check_box_set(self.0, v as c_int) } + } + ++ /// Enable or disable (grey out) this control. ++ pub fn set_enabled(&self, on: bool) { ++ unsafe { notsafe::wxbr_enable(self.0, on as c_int) } ++ } ++ + pub fn check_list_box(parent: Widget) -> Widget { + Widget(unsafe { notsafe::wxbr_check_list_box(parent.0) }) + } diff --git a/thirdparty/wx-rs/wx-rs-0.2.0-patched/cpp_src/widgets_bridge.cpp b/thirdparty/wx-rs/wx-rs-0.2.0-patched/cpp_src/widgets_bridge.cpp index d4d8e126..02818d53 100644 --- a/thirdparty/wx-rs/wx-rs-0.2.0-patched/cpp_src/widgets_bridge.cpp +++ b/thirdparty/wx-rs/wx-rs-0.2.0-patched/cpp_src/widgets_bridge.cpp @@ -945,6 +945,11 @@ void wxbr_check_box_set(void* win, int v) { ((wxCheckBox*)win)->SetValue(v != 0); } +// Enable or disable (grey out) any window/control. +void wxbr_enable(void* win, int on) { + ((wxWindow*)win)->Enable(on != 0); +} + // ---- check list box --------------------------------------------------------- void* wxbr_check_list_box(void* parent) { diff --git a/thirdparty/wx-rs/wx-rs-0.2.0-patched/src/widgets.rs b/thirdparty/wx-rs/wx-rs-0.2.0-patched/src/widgets.rs index 796a9693..2b89079e 100644 --- a/thirdparty/wx-rs/wx-rs-0.2.0-patched/src/widgets.rs +++ b/thirdparty/wx-rs/wx-rs-0.2.0-patched/src/widgets.rs @@ -49,6 +49,7 @@ mod notsafe { pub fn wxbr_check_box(parent: *mut c_void, label: *const c_char) -> *mut c_void; pub fn wxbr_check_box_get(win: *mut c_void) -> c_int; pub fn wxbr_check_box_set(win: *mut c_void, v: c_int); + pub fn wxbr_enable(win: *mut c_void, on: c_int); pub fn wxbr_check_list_box(parent: *mut c_void) -> *mut c_void; pub fn wxbr_clb_append(win: *mut c_void, label: *const c_char); pub fn wxbr_clb_clear(win: *mut c_void); @@ -508,6 +509,11 @@ impl Widget { unsafe { notsafe::wxbr_check_box_set(self.0, v as c_int) } } + /// Enable or disable (grey out) this control. + pub fn set_enabled(&self, on: bool) { + unsafe { notsafe::wxbr_enable(self.0, on as c_int) } + } + pub fn check_list_box(parent: Widget) -> Widget { Widget(unsafe { notsafe::wxbr_check_list_box(parent.0) }) }