1use anyhow::{Context, Error};
6use fidl_fuchsia_hardware_power_statecontrol::{
7 AdminMarker, AdminProxy, RebootOptions, RebootReason2,
8};
9use fidl_fuchsia_paver::{BootManagerMarker, BootManagerProxy, PaverMarker};
10use fuchsia_component::client::connect_to_protocol;
11use zx::Status;
12
13pub async fn handle_revert() -> Result<(), Error> {
15 let admin = connect_to_protocol::<AdminMarker>().context("while connecting to admin")?;
16
17 let paver = connect_to_protocol::<PaverMarker>().context("while connecting to paver")?;
18 let (boot_manager, server_end) = fidl::endpoints::create_proxy::<BootManagerMarker>();
19 let () = paver.find_boot_manager(server_end).context("while connecting to boot manager")?;
20
21 println!("Reverting the update.");
22 handle_revert_impl(&admin, &boot_manager).await
23}
24
25async fn handle_revert_impl(
27 admin: &AdminProxy,
28 boot_manager: &BootManagerProxy,
29) -> Result<(), Error> {
30 let current_config = boot_manager
31 .query_current_configuration()
32 .await
33 .context("while calling query_current_configuration")?
34 .map_err(Status::from_raw)
35 .context("query_current_configuration responded with")?;
36
37 let () = Status::ok(
38 boot_manager
39 .set_configuration_unbootable(current_config)
40 .await
41 .context("while calling set_configuration_unbootable")?,
42 )
43 .context("set_configuration_unbootable responded with")?;
44
45 let () = Status::ok(boot_manager.flush().await.context("while calling flush")?)
46 .context("flush responded with")?;
47
48 admin
49 .perform_reboot(&RebootOptions {
50 reasons: Some(vec![RebootReason2::UserRequest]),
51 ..Default::default()
52 })
53 .await
54 .context("while performing reboot call")?
55 .map_err(Status::from_raw)
56 .context("reboot responded with")
57}