1use anyhow::Error;
6use async_lock::OnceCell;
7use fidl_fuchsia_feedback::{LastRebootInfoProviderMarker, RebootReason};
8use fidl_fuchsia_io as fio;
9use fuchsia_component::client::connect_to_protocol_sync;
10use fuchsia_fs::node::OpenError;
11use log::{debug, info};
12use zx_status::Status;
13
14const STARTED_ONCE: &str = "component-started-once";
16
17const LRIP_FIDL_TIMEOUT: zx::MonotonicDuration = zx::MonotonicDuration::INFINITE;
19static ANDROID_BOOTREASON: OnceCell<Result<String, Error>> = OnceCell::new();
20
21pub async fn get_android_bootreason(
23 dir: Option<fio::DirectoryProxy>,
24) -> &'static Result<String, Error> {
25 ANDROID_BOOTREASON.get_or_init(async || update_android_bootreason(dir).await).await
26}
27
28async fn update_android_bootreason(dir: Option<fio::DirectoryProxy>) -> Result<String, Error> {
33 if let Some(dir) = dir {
34 match fuchsia_fs::directory::open_file(&dir, STARTED_ONCE, fio::Flags::FLAG_MUST_CREATE)
35 .await
36 {
37 Ok(_file) => (),
38 Err(OpenError::OpenError(Status::ALREADY_EXISTS)) => {
39 info!("Session restart observed, set android bootreason to kernel_panic.");
40 return Ok("kernel_panic".to_string());
41 }
42 Err(err) => {
43 info!(
44 "Failed to generate the file with err {err:#?}. Continue with LastRebootInfo."
45 );
46 }
47 }
48 }
49
50 info!("Converting LastRebootInfo to an android-friendly bootreason.");
51 let reboot_info_proxy = connect_to_protocol_sync::<LastRebootInfoProviderMarker>()?;
52 let deadline = zx::MonotonicInstant::after(LRIP_FIDL_TIMEOUT);
53 let reboot_info = reboot_info_proxy.get(deadline)?;
54
55 let bootreason = match reboot_info.reason {
56 Some(RebootReason::Unknown) => "reboot,unknown",
57 Some(RebootReason::Cold) => "reboot,cold",
58 Some(RebootReason::BriefPowerLoss) => "reboot,hard_reset",
59 Some(RebootReason::Brownout) => "reboot,undervoltage",
60 Some(RebootReason::KernelPanic) => "kernel_panic",
61 Some(RebootReason::SystemOutOfMemory) => "kernel_panic,oom",
62 Some(RebootReason::HardwareWatchdogTimeout) => "watchdog",
63 Some(RebootReason::SoftwareWatchdogTimeout) => "watchdog,sw",
64 Some(RebootReason::RootJobTermination) => "kernel_panic",
65 Some(RebootReason::UserRequest) => "reboot,userrequested",
66 Some(RebootReason::DeveloperRequest) => "reboot,shell",
67 Some(RebootReason::RetrySystemUpdate) => "reboot,ota",
68 Some(RebootReason::HighTemperature) => "shutdown,thermal",
69 Some(RebootReason::SessionFailure) => "kernel_panic",
70 Some(RebootReason::SysmgrFailure) => "kernel_panic",
71 Some(RebootReason::FactoryDataReset) => "reboot,factory_reset",
72 Some(RebootReason::CriticalComponentFailure) => "kernel_panic",
73 Some(RebootReason::ZbiSwap) => "reboot,normal",
74 Some(RebootReason::SystemUpdate) => "reboot,ota",
75 Some(RebootReason::NetstackMigration) => "reboot,normal",
76 Some(RebootReason::AndroidUnexpectedReason) => "reboot,normal",
77 Some(RebootReason::AndroidRescueParty) => "reboot,rescueparty",
78 Some(RebootReason::AndroidCriticalProcessFailure) => "reboot,userspace_failed",
79 Some(RebootReason::__SourceBreaking { .. }) => "reboot,normal",
80 None => "reboot,unknown",
81 };
82 Ok(bootreason.to_string())
83}
84
85fn get_reboot_reason() -> Option<RebootReason> {
87 let reboot_info_proxy = connect_to_protocol_sync::<LastRebootInfoProviderMarker>().ok();
88 let deadline = zx::MonotonicInstant::after(LRIP_FIDL_TIMEOUT);
89 let reboot_info = reboot_info_proxy?.get(deadline);
90 match reboot_info {
91 Ok(info) => match info.reason {
92 Some(r) => Some(r),
93 None => {
94 info!("Failed to get the reboot reason.");
95 Some(RebootReason::unknown())
96 }
97 },
98 Err(e) => {
99 info!("Failed to get the reboot info: {:?}", e);
100 Some(RebootReason::unknown())
101 }
102 }
103}
104
105pub fn get_console_ramoops() -> Option<Vec<u8>> {
110 debug!("Getting console-ramoops contents");
111 match ANDROID_BOOTREASON.wait_blocking() {
112 Ok(reason) => match reason.as_str() {
113 "kernel_panic" | "watchdog" | "watchdog,sw" => Some(
114 format!("Last Reboot Reason: {:?}\n", get_reboot_reason()?).as_bytes().to_vec(),
115 ),
116 _ => None,
117 },
118 Err(e) => {
119 info!("Failed to get android bootreason for console_ramoops: {:?}", e);
120 None
121 }
122 }
123}