Skip to content

Commit 4577bb1

Browse files
committed
chromium_ec: Don't panic if no EC driver is available
On platforms without any EC driver, like macOS aarch64 Linux without /dev/cros_ec, CrosEc::new() indexed into an empty driver list. Because the commandline always creates a CrosEc at startup, every command crashed.
1 parent bd6e949 commit 4577bb1

1 file changed

Lines changed: 17 additions & 13 deletions

File tree

  • framework_lib/src/chromium_ec

‎framework_lib/src/chromium_ec/mod.rs‎

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,8 @@ pub trait CrosEcDriver {
233233

234234
#[derive(Clone)]
235235
pub struct CrosEc {
236-
driver: CrosEcDriverType,
236+
/// None if no driver is available on this platform
237+
driver: Option<CrosEcDriverType>,
237238
}
238239

239240
impl Default for CrosEc {
@@ -245,7 +246,7 @@ impl Default for CrosEc {
245246
/// Find out which drivers are available
246247
///
247248
/// Depending on the availability we choose the first one as default
248-
#[allow(clippy::vec_init_then_push)]
249+
#[allow(clippy::vec_init_then_push, unused_mut)]
249250
fn available_drivers() -> Vec<CrosEcDriverType> {
250251
let mut drivers = vec![];
251252

@@ -265,10 +266,9 @@ fn available_drivers() -> Vec<CrosEcDriverType> {
265266

266267
impl CrosEc {
267268
pub fn new() -> CrosEc {
268-
debug!("Chromium EC Driver: {:?}", available_drivers()[0]);
269-
CrosEc {
270-
driver: available_drivers()[0],
271-
}
269+
let driver = available_drivers().first().copied();
270+
debug!("Chromium EC Driver: {:?}", driver);
271+
CrosEc { driver }
272272
}
273273

274274
pub fn with(driver: CrosEcDriverType) -> Option<CrosEc> {
@@ -277,7 +277,9 @@ impl CrosEc {
277277
}
278278
debug!("Chromium EC Driver: {:?}", driver);
279279

280-
Some(CrosEc { driver })
280+
Some(CrosEc {
281+
driver: Some(driver),
282+
})
281283
}
282284

283285
/// Lock bus to PD controller in the beginning of flashing
@@ -2173,11 +2175,11 @@ impl CrosEcDriver for CrosEc {
21732175
// TODO: Change this function to return EcResult instead and print the error only in UI code
21742176
print_err(match self.driver {
21752177
#[cfg(all(not(windows), target_arch = "x86_64"))]
2176-
CrosEcDriverType::Portio => portio::read_memory(offset, length),
2178+
Some(CrosEcDriverType::Portio) => portio::read_memory(offset, length),
21772179
#[cfg(windows)]
2178-
CrosEcDriverType::Windows => windows::read_memory(offset, length),
2180+
Some(CrosEcDriverType::Windows) => windows::read_memory(offset, length),
21792181
#[cfg(target_os = "linux")]
2180-
CrosEcDriverType::CrosEc => cros_ec::read_memory(offset, length),
2182+
Some(CrosEcDriverType::CrosEc) => cros_ec::read_memory(offset, length),
21812183
_ => Err(EcError::DeviceError("No EC driver available".to_string())),
21822184
})
21832185
}
@@ -2195,11 +2197,13 @@ impl CrosEcDriver for CrosEc {
21952197

21962198
match self.driver {
21972199
#[cfg(all(not(windows), target_arch = "x86_64"))]
2198-
CrosEcDriverType::Portio => portio::send_command(command, command_version, data),
2200+
Some(CrosEcDriverType::Portio) => portio::send_command(command, command_version, data),
21992201
#[cfg(windows)]
2200-
CrosEcDriverType::Windows => windows::send_command(command, command_version, data),
2202+
Some(CrosEcDriverType::Windows) => {
2203+
windows::send_command(command, command_version, data)
2204+
}
22012205
#[cfg(target_os = "linux")]
2202-
CrosEcDriverType::CrosEc => cros_ec::send_command(command, command_version, data),
2206+
Some(CrosEcDriverType::CrosEc) => cros_ec::send_command(command, command_version, data),
22032207
_ => Err(EcError::DeviceError("No EC driver available".to_string())),
22042208
}
22052209
}

0 commit comments

Comments
 (0)