diff --git a/crates/cli/src/commands/install.rs b/crates/cli/src/commands/install.rs index f10e96e..72fce4a 100644 --- a/crates/cli/src/commands/install.rs +++ b/crates/cli/src/commands/install.rs @@ -89,7 +89,7 @@ pub struct InstallArgs { pub busy_cpu: bool, /// How iPadOS is entered to resize it (palera1n, checkra1n, dopamine); /// otherwise the first that fits the device - #[arg(long, value_name = "ROUTE", value_parser = ["palera1n", "checkra1n", "dopamine"])] + #[arg(long, value_name = "ROUTE", value_parser = ACCESS_ROUTES)] pub access: Option, /// The iPadOS or iOS version on the device, where it can't say (DFU and /// recovery mode): 14, 15.8, ...; asked when not given @@ -143,32 +143,48 @@ pub struct ToolFlags { pub port_reset: Option, } +/// The access routes `install --access` runs: each has its helper in +/// [`access_helper`]. A route the board data names but this list lacks is +/// refused when the command line is read, never run as another one. +const ACCESS_ROUTES: [&str; 3] = ["palera1n", "checkra1n", "dopamine"]; + +/// The helper of an access route, by its name; None for one this version +/// does not run. +fn access_helper(route: &str) -> Option { + use orchard_flow::Helper; + match route { + "palera1n" => Some(Helper::Palera1n), + "checkra1n" => Some(Helper::Checkra1n), + "dopamine" => Some(Helper::Dopamine), + _ => None, + } +} + /// The flow's Options from the flags; None when none of them was given, so /// the environment's switches count as before. -fn flow_options(access: Option<&str>, tool: &ToolFlags, busy_cpu: bool) -> Option { - use orchard_flow::{Helper, Palera1n}; +fn flow_options(access: Option<&str>, tool: &ToolFlags, busy_cpu: bool) -> Result, String> { + use orchard_flow::Palera1n; if access.is_none() && tool.palera1n_version.is_none() && tool.port_reset.is_none() { - return None; + return Ok(None); } let env = |name: &str| std::env::var(name).ok().map(|v| v.trim().to_owned()).filter(|v| !v.is_empty()); - let helper = access.map(|a| match a { - "dopamine" => Helper::Dopamine, - "checkra1n" => Helper::Checkra1n, - _ => Helper::Palera1n, - }); + let helper = match access { + Some(route) => Some(access_helper(route).ok_or_else(|| format!("this version does not run the {route} access route"))?), + None => None, + }; let two = match tool.palera1n_version.as_deref() { Some(version) => version == "2.4", None => env("ORCHARD_PALERA1N_BETA").as_deref() == Some("0"), }; let seconds = tool.port_reset.or_else(|| env("ORCHARD_PORT_RESET_SECONDS")?.parse::().ok().map(|s| s.clamp(0., 15.).round() as u8)).unwrap_or(10); - Some(orchard_flow::Options { + Ok(Some(orchard_flow::Options { helper, start: None, palera1n: if two { Palera1n::Two } else { Palera1n::Three }, port_reset: (seconds > 0).then_some(seconds), busy_cpu: busy_cpu || orchard_runtime::flags::busy_cpu(), stop_in_dfu: false, - }) + })) } /// `--ios VERSION` as the version question's answer: below 15 or not. @@ -1460,8 +1476,10 @@ pub fn install(global: &Global, args: &InstallArgs) -> ExitCode { let send = |e: Event| { let _ = handle.events.send(e); }; - if let Some(options) = flow_options(args.access.as_deref(), &args.tool, args.busy_cpu) { - send(Event::SetOptions(options)); + match flow_options(args.access.as_deref(), &args.tool, args.busy_cpu) { + Ok(Some(options)) => send(Event::SetOptions(options)), + Ok(None) => {} + Err(why) => return finish(&mut out, Exit::Unsupported, "access", &why), } let mut shared = Shared::new(App::Installer, args.wait, global); let mut found: Option = None; @@ -2269,7 +2287,7 @@ pub fn launch(global: &Global, args: &LaunchArgs) -> ExitCode { // starts only the entry it names. send(Event::AskEntry); } - if let Some(options) = flow_options(None, &args.tool, args.busy_cpu) { + if let Ok(Some(options)) = flow_options(None, &args.tool, args.busy_cpu) { send(Event::SetOptions(options)); } let mut shared = Shared::new(App::Launcher, args.wait, global); @@ -2624,16 +2642,34 @@ mod tests { #[test] fn the_flags_are_the_windows_options() { let tool = |palera1n_version: Option<&str>, port_reset| ToolFlags { palera1n_version: palera1n_version.map(str::to_owned), port_reset }; - assert!(flow_options(None, &tool(None, None), true).is_none()); - let o = flow_options(Some("dopamine"), &tool(Some("2.4"), Some(0)), false).unwrap(); + assert!(flow_options(None, &tool(None, None), true).unwrap().is_none()); + let o = flow_options(Some("dopamine"), &tool(Some("2.4"), Some(0)), false).unwrap().unwrap(); assert_eq!((o.helper, o.palera1n, o.port_reset), (Some(orchard_flow::Helper::Dopamine), orchard_flow::Palera1n::Two, None)); - let o = flow_options(Some("checkra1n"), &tool(Some("3"), Some(15)), true).unwrap(); + let o = flow_options(Some("checkra1n"), &tool(Some("3"), Some(15)), true).unwrap().unwrap(); assert_eq!((o.helper, o.palera1n, o.port_reset, o.busy_cpu), (Some(orchard_flow::Helper::Checkra1n), orchard_flow::Palera1n::Three, Some(15), true)); assert_eq!(ios_answer("14.8"), Some(orchard_flow::OsVersion::Below15)); assert_eq!(ios_answer("15"), Some(orchard_flow::OsVersion::AtLeast15)); assert_eq!(ios_answer("new"), None); } + /// A route `--access` takes always runs as itself: every name has its + /// own helper, and one without is refused, never run as palera1n. + #[test] + fn every_access_route_runs_as_itself() { + let tool = ToolFlags { palera1n_version: None, port_reset: None }; + let mut helpers = Vec::new(); + for route in ACCESS_ROUTES { + let helper = access_helper(route).unwrap_or_else(|| panic!("--access {route} has no helper")); + assert!(!helpers.contains(&helper), "--access {route} runs as another route"); + helpers.push(helper); + assert_eq!(flow_options(Some(route), &tool, false).unwrap().unwrap().helper, Some(helper)); + } + let refused = flow_options(Some("amethyst"), &tool, false).unwrap_err(); + assert!(refused.contains("does not run the amethyst access route"), "{refused}"); + use clap::Parser; + assert!(crate::args::Cli::try_parse_from(["orchard", "install", "--access", "amethyst"]).is_err()); + } + #[test] fn launch_can_end_instead_of_waiting_for_pwned_dfu_mode() { use clap::Parser;