Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ is on main, the release is published with that section as its notes (AGENTS.md,

## [Unreleased]

### Installer and launcher

- On a Mac, installing on an iPad mini 2 no longer stops early in DFU mode
with "DFU clear status: unknown (error 0xe00002ed)": Orchard connects to
it again, checks that it is ready and goes on (theChristopher16).

### Command line

- The iPhone 5's GPU firmware and its graphics event template now build from
Expand Down
127 changes: 112 additions & 15 deletions crates/runtime/src/ibss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use orchard_boards::firmware_build::Boot;
use orchard_boards::{boot_firmware, pack};
use orchard_cache::store::Store;
use orchard_cache::{Code, Error, Result};
use orchard_device::host::Connect;
use orchard_device::host::{Connect, DfuMode};
use orchard_device::session::Session;
use orchard_device::usb;
use orchard_flow::Event;
Expand Down Expand Up @@ -153,7 +153,10 @@ pub fn start(executor: &Executor, session: &mut Session<Host>, board: &str, keys
/// reset starts the new session; on Linux the boot ROM leaves USB before the
/// answer, which shows as a disconnect, and comes back by itself in a new,
/// idle session (gaster's mark stays). Sending CLRSTATUS to that new session
/// would start it over again, so it is sent once. The boot ROM can also take
/// would start it over again, so it is sent once. On a Mac the answer can
/// also get lost while the boot ROM did take the request (an iPad mini 2 on
/// an Intel Mac: IOKit's "not responding"); then the device is opened again
/// and the hand-over goes on only if it reads idle. The boot ROM can also take
/// several connections before the host addresses it, so opening it is
/// retried for up to thirty seconds (ORCHARD_GASTER_RECONNECT_SECONDS).
fn fresh_dfu_after_gaster(session: &mut Session<Host>, say: &mut dyn FnMut(&str)) -> Result<()> {
Expand All @@ -162,20 +165,35 @@ fn fresh_dfu_after_gaster(session: &mut Session<Host>, say: &mut dyn FnMut(&str)
let mut waited = false;
loop {
let error = match session.dfu() {
Ok(mut dfu) => match dfu.clear_status(session.timeout(Duration::from_secs(10))?) {
Ok(()) => {
dfu.reset().map_err(usb::error)?;
break;
Ok(mut dfu) => {
let cleared = dfu.clear_status(session.timeout(Duration::from_secs(10))?);
match after_clear(&cleared) {
AfterClear::Reset => {
dfu.reset().map_err(usb::error)?;
break;
}
// Leaving mid-transfer can also end it with a protocol error
// (Linux EPROTO: Kind::Fault), as on an A7 (N51); wait_marked
// below still requires the boot ROM back with gaster's mark.
AfterClear::NewSession => {
say("The boot ROM started a new DFU session");
break;
}
AfterClear::CheckIdle => {
let Err(e) = cleared else { unreachable!("only a failed clear is checked") };
// Once: open the marked device again and read its status.
drop(dfu);
wait_marked(session, deadline, "after its DFU clear went unanswered")?;
let timeout = session.timeout(Duration::from_secs(10))?;
let idle = session.dfu().is_ok_and(|mut dfu| reads_idle(&mut *dfu, timeout));
if !idle {
return Err(usb::error(e));
}
say("The boot ROM is idle after its DFU clear went unanswered");
break;
}
}
// Leaving mid-transfer can also end it with a protocol error
// (Linux EPROTO: Kind::Fault), as on an A7 (N51); wait_marked
// below still requires the boot ROM back with gaster's mark.
Err(e) if matches!(e.kind, orchard_usb::Kind::Disconnected | orchard_usb::Kind::Fault) => {
say("The boot ROM started a new DFU session");
break;
}
Err(e) => return Err(usb::error(e)),
},
}
Err(e) => e,
};
if Instant::now() >= deadline {
Expand All @@ -198,6 +216,34 @@ fn fresh_dfu_after_gaster(session: &mut Session<Host>, say: &mut dyn FnMut(&str)
Ok(())
}

/// What the hand-over after gaster does once CLRSTATUS has returned.
#[derive(Debug, PartialEq)]
enum AfterClear {
/// Answered: a USB reset starts the new session.
Reset,
/// The boot ROM left USB and comes back in a new session by itself.
NewSession,
/// Any other error: the answer may be lost although the request was
/// taken, so the device is opened again and must read idle.
CheckIdle,
}

fn after_clear(cleared: &std::result::Result<(), orchard_usb::Error>) -> AfterClear {
match cleared {
Ok(()) => AfterClear::Reset,
Err(e) if matches!(e.kind, orchard_usb::Kind::Disconnected | orchard_usb::Kind::Fault) => {
AfterClear::NewSession
}
Err(_) => AfterClear::CheckIdle,
}
}

/// One status read: OK and dfuIDLE (state 2). Anything else, an error
/// included, is not idle.
fn reads_idle(dfu: &mut dyn DfuMode, timeout: Duration) -> bool {
matches!(dfu.status(timeout), Ok(status) if status.status == 0 && status.state == 2)
}

/// Waits until `deadline` for the boot ROM in DFU mode, with gaster's mark.
fn wait_marked(session: &mut Session<Host>, deadline: Instant, when: &str) -> Result<()> {
let left = deadline.saturating_duration_since(Instant::now());
Expand Down Expand Up @@ -380,4 +426,55 @@ mod tests {
assert_eq!(checkm8_ibec(&unencrypted).unwrap(), b"ibec.plain.img3");
assert!(checkm8_ibec(&images(&["ibss.raw"])).is_err());
}

fn usb_error(kind: orchard_usb::Kind) -> orchard_usb::Error {
orchard_usb::Error { action: "DFU clear status", kind, detail: "test".into() }
}

#[test]
fn only_an_unexplained_clear_error_is_checked_for_idle() {
use orchard_usb::Kind;
assert_eq!(after_clear(&Ok(())), AfterClear::Reset);
assert_eq!(after_clear(&Err(usb_error(Kind::Disconnected))), AfterClear::NewSession);
assert_eq!(after_clear(&Err(usb_error(Kind::Fault))), AfterClear::NewSession);
// IOKit's 0xe00002ed (not responding) on an Intel Mac arrives as Other.
assert_eq!(after_clear(&Err(usb_error(Kind::Other))), AfterClear::CheckIdle);
}

struct Reads(std::result::Result<orchard_usb::dfu::Status, orchard_usb::Error>);

impl DfuMode for Reads {
fn status(&mut self, _: Duration) -> std::result::Result<orchard_usb::dfu::Status, orchard_usb::Error> {
self.0.clone()
}
fn clear_status(&mut self, _: Duration) -> std::result::Result<(), orchard_usb::Error> {
unreachable!("the check only reads the status")
}
fn abort(&mut self, _: Duration) -> std::result::Result<(), orchard_usb::Error> {
unreachable!("the check only reads the status")
}
fn reset(&mut self) -> std::result::Result<(), orchard_usb::Error> {
unreachable!("the check only reads the status")
}
fn send(&mut self, _: &[u8], _: Duration) -> std::result::Result<(), orchard_usb::Error> {
unreachable!("the check only reads the status")
}
fn start_pwned(&mut self, _: Pwned, _: &[u8], _: Duration) -> std::result::Result<Sent, orchard_usb::Error> {
unreachable!("the check only reads the status")
}
}

fn status(status: u8, state: u8) -> Reads {
Reads(Ok(orchard_usb::dfu::Status { status, state, state_name: "test", poll_timeout_ms: 0 }))
}

#[test]
fn only_ok_and_dfu_idle_reads_idle() {
let t = Duration::from_secs(1);
assert!(reads_idle(&mut status(0, 2), t));
assert!(!reads_idle(&mut status(0, 5), t), "dfuDNLOAD-IDLE");
assert!(!reads_idle(&mut status(0, 10), t), "dfuERROR");
assert!(!reads_idle(&mut status(14, 2), t), "an error status");
assert!(!reads_idle(&mut Reads(Err(usb_error(orchard_usb::Kind::Other))), t));
}
}
Loading