diff --git a/Cargo.lock b/Cargo.lock index 5fa792e..0c45523 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,39 +2,147 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "addr2line" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + [[package]] name = "autocfg" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "backtrace" +version = "0.3.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2089b7e3f35b9dd2d0ed921ead4f6d318c27680d4a5bd167b3ee120edb105837" +dependencies = [ + "addr2line", + "cc", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", +] + [[package]] name = "bitflags" version = "2.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf" +[[package]] +name = "cc" +version = "1.0.90" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8cd6604a82acf3039f1144f54b8eb34e91ffba622051189e71b781822d5ee1f5" + [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "color-eyre" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a667583cca8c4f8436db8de46ea8233c42a7d9ae424a82d338f2e4675229204" +dependencies = [ + "backtrace", + "color-spantrace", + "eyre", + "indenter", + "once_cell", + "owo-colors", + "tracing-error", +] + +[[package]] +name = "color-spantrace" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd6be1b2a7e382e2b98b43b2adcca6bb0e465af0bdd38123873ae61eb17a72c2" +dependencies = [ + "once_cell", + "owo-colors", + "tracing-core", + "tracing-error", +] + [[package]] name = "crosec-rs" version = "0.1.0" dependencies = [ + "color-eyre", "nix", "num-derive", "num-traits", + "thiserror", +] + +[[package]] +name = "eyre" +version = "0.6.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7cd915d99f24784cdc19fd37ef22b97e3ff0ae756c7e492e9fbfe897d61e2aec" +dependencies = [ + "indenter", + "once_cell", ] +[[package]] +name = "gimli" +version = "0.28.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" + +[[package]] +name = "indenter" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683" + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libc" version = "0.2.153" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" +[[package]] +name = "memchr" +version = "2.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" + +[[package]] +name = "miniz_oxide" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d811f3e15f28568be3407c8e7fdb6514c1cda3cb30683f15b6a1a1dc4ea14a7" +dependencies = [ + "adler", +] + [[package]] name = "nix" version = "0.27.1" @@ -66,6 +174,33 @@ dependencies = [ "autocfg", ] +[[package]] +name = "object" +version = "0.32.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6a622008b6e321afc04970976f62ee297fdbaa6f95318ca343e3eebb9648441" +dependencies = [ + "memchr", +] + +[[package]] +name = "once_cell" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" + +[[package]] +name = "owo-colors" +version = "3.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f" + +[[package]] +name = "pin-project-lite" +version = "0.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" + [[package]] name = "proc-macro2" version = "1.0.78" @@ -84,6 +219,21 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rustc-demangle" +version = "0.1.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" + +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + [[package]] name = "syn" version = "2.0.49" @@ -95,8 +245,85 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "thiserror" +version = "1.0.57" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e45bcbe8ed29775f228095caf2cd67af7a4ccf756ebff23a306bf3e8b47b24b" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.57" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a953cb265bef375dae3de6663da4d3804eee9682ea80d8e2542529b73c531c81" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "thread_local" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c" +dependencies = [ + "cfg-if", + "once_cell", +] + +[[package]] +name = "tracing" +version = "0.1.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" +dependencies = [ + "pin-project-lite", + "tracing-core", +] + +[[package]] +name = "tracing-core" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" +dependencies = [ + "once_cell", + "valuable", +] + +[[package]] +name = "tracing-error" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d686ec1c0f384b1277f097b2f279a2ecc11afe8c133c1aabf036a27cb4cd206e" +dependencies = [ + "tracing", + "tracing-subscriber", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" +dependencies = [ + "sharded-slab", + "thread_local", + "tracing-core", +] + [[package]] name = "unicode-ident" version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" diff --git a/Cargo.toml b/Cargo.toml index 616d9f5..81c204d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,8 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +color-eyre = "0.6.2" nix = { version = "0.27.1", features = ["ioctl"] } num-derive = "0.4.2" num-traits = "0.2.18" +thiserror = "1.0.57" diff --git a/src/bin/ectool.rs b/src/bin/ectool.rs index d21ae40..262841f 100644 --- a/src/bin/ectool.rs +++ b/src/bin/ectool.rs @@ -1,10 +1,13 @@ +use color_eyre::eyre::Result; use crosec_rs::commands::get_chip_info::ec_cmd_get_chip_info; use crosec_rs::commands::hello::ec_cmd_hello; use crosec_rs::commands::version::ec_cmd_version; -fn main() { +fn main() -> Result<()> { + color_eyre::install()?; + println!("Hello command"); - let status = ec_cmd_hello(); + let status = ec_cmd_hello()?; if status { println!("EC says hello!"); } else { @@ -12,7 +15,7 @@ fn main() { } println!("Version command"); - let (ro_ver, rw_ver, firmware_copy, build_info, tool_version) = ec_cmd_version(); + let (ro_ver, rw_ver, firmware_copy, build_info, tool_version) = ec_cmd_version()?; println!("RO version: {ro_ver}"); println!("RW version: {rw_ver}"); println!("Firmware copy: {firmware_copy}"); @@ -20,9 +23,11 @@ fn main() { println!("Tool version: {tool_version}"); println!("Chip info command"); - let (vendor, name, revision) = ec_cmd_get_chip_info(); + let (vendor, name, revision) = ec_cmd_get_chip_info()?; println!("Chip info:"); println!(" vendor: {vendor}"); println!(" name: {name}"); println!(" revision: {revision}"); + + Ok(()) } diff --git a/src/commands/get_chip_info.rs b/src/commands/get_chip_info.rs index 44e5590..cf61e38 100644 --- a/src/commands/get_chip_info.rs +++ b/src/commands/get_chip_info.rs @@ -1,5 +1,6 @@ use crate::commands::CrosEcCmds; use crate::crosec::dev::ec_command; +use crate::crosec::EcCmdResult; use std::mem::size_of; use std::slice; @@ -10,7 +11,7 @@ struct EcResponseGetChipInfo { revision: [u8; 32], } -pub fn ec_cmd_get_chip_info() -> (String, String, String) { +pub fn ec_cmd_get_chip_info() -> EcCmdResult<(String, String, String)> { let params = EcResponseGetChipInfo { vendor: [0; 32], name: [0; 32], @@ -21,13 +22,12 @@ pub fn ec_cmd_get_chip_info() -> (String, String, String) { let params_slice = unsafe { slice::from_raw_parts(params_ptr, size_of::()) }; - let result = ec_command(CrosEcCmds::GetChipInfo as u32, 0, params_slice) - .unwrap_or_else(|error| panic!("EC error: {error:?}")); + let result = ec_command(CrosEcCmds::GetChipInfo as u32, 0, params_slice)?; let response: EcResponseGetChipInfo = unsafe { std::ptr::read(result.as_ptr() as *const _) }; let vendor = String::from_utf8(response.vendor.to_vec()).unwrap_or(String::from("")); let name = String::from_utf8(response.name.to_vec()).unwrap_or(String::from("")); let revision = String::from_utf8(response.revision.to_vec()).unwrap_or(String::from("")); - (vendor, name, revision) + Ok((vendor, name, revision)) } diff --git a/src/commands/hello.rs b/src/commands/hello.rs index 8c94ca1..4275812 100644 --- a/src/commands/hello.rs +++ b/src/commands/hello.rs @@ -1,5 +1,6 @@ use crate::commands::CrosEcCmds; use crate::crosec::dev::ec_command; +use crate::crosec::EcCmdResult; use std::mem::size_of; use std::slice; @@ -16,15 +17,15 @@ struct EcResponseHello { out_data: u32, } -pub fn ec_cmd_hello() -> bool { +pub fn ec_cmd_hello() -> EcCmdResult { let params = EcParamsHello { in_data: INPUT_DATA, }; let params_ptr = ¶ms as *const _ as *const u8; let params_slice = unsafe { slice::from_raw_parts(params_ptr, size_of::()) }; - let result = ec_command(CrosEcCmds::Hello as u32, 0, params_slice) - .unwrap_or_else(|error| panic!("EC error: {error:?}")); + let result = ec_command(CrosEcCmds::Hello as u32, 0, params_slice)?; let response: EcResponseHello = unsafe { std::ptr::read(result.as_ptr() as *const _) }; - response.out_data == EXPECTED_OUTPUT + + Ok(response.out_data == EXPECTED_OUTPUT) } diff --git a/src/commands/version.rs b/src/commands/version.rs index 77a43c8..ca35f1a 100644 --- a/src/commands/version.rs +++ b/src/commands/version.rs @@ -1,6 +1,7 @@ use crate::commands::CrosEcCmds; use crate::crosec::dev::ec_command; use crate::crosec::dev::BUF_SIZE; +use crate::crosec::EcCmdResult; use num_derive::FromPrimitive; use num_traits::FromPrimitive; use std::mem::size_of; @@ -26,7 +27,7 @@ enum EcImage { RwB = 4, } -pub fn ec_cmd_version() -> (String, String, String, String, String) { +pub fn ec_cmd_version() -> EcCmdResult<(String, String, String, String, String)> { let params = EcResponseVersionV1 { version_string_ro: [0; 32], version_string_rw: [0; 32], @@ -40,8 +41,7 @@ pub fn ec_cmd_version() -> (String, String, String, String, String) { let params_slice = unsafe { slice::from_raw_parts(params_ptr, size_of::()) }; - let result = ec_command(CrosEcCmds::Version as u32, 0, params_slice) - .unwrap_or_else(|error| panic!("EC error: {error:?}")); + let result = ec_command(CrosEcCmds::Version as u32, 0, params_slice)?; let response: EcResponseVersionV1 = unsafe { std::ptr::read(result.as_ptr() as *const _) }; let ro_ver = String::from_utf8(response.version_string_ro.to_vec()).unwrap_or(String::from("")); @@ -59,10 +59,9 @@ pub fn ec_cmd_version() -> (String, String, String, String, String) { let build_string_ptr = &build_string as *const _ as *const u8; let build_string_slice = unsafe { slice::from_raw_parts(build_string_ptr, BUF_SIZE) }; - let result = ec_command(CrosEcCmds::GetBuildInfo as u32, 0, build_string_slice) - .unwrap_or_else(|error| panic!("EC error: {error:?}")); + let result = ec_command(CrosEcCmds::GetBuildInfo as u32, 0, build_string_slice)?; let response: [u8; BUF_SIZE] = unsafe { std::ptr::read(result.as_ptr() as *const _) }; let build_info = String::from_utf8(response.to_vec()).unwrap_or(String::from("")); - (ro_ver, rw_ver, image, build_info, String::from(TOOLVERSION)) + Ok((ro_ver, rw_ver, image, build_info, String::from(TOOLVERSION))) } diff --git a/src/crosec/dev.rs b/src/crosec/dev.rs index 3364581..8b890df 100644 --- a/src/crosec/dev.rs +++ b/src/crosec/dev.rs @@ -72,8 +72,6 @@ pub fn ec_command(command: u32, command_version: u8, data: &[u8]) -> EcCmdResult }; result .map(|result| cmd.data[0..result as usize].to_vec()) - .map_err(|err| { - EcError::DeviceError(format!("ioctl to send command to EC failed with {err:?}")) - }) + .map_err(|err| EcError::DeviceError(err)) } } diff --git a/src/crosec/mod.rs b/src/crosec/mod.rs index c5836de..71b3db4 100644 --- a/src/crosec/mod.rs +++ b/src/crosec/mod.rs @@ -1,4 +1,6 @@ +use nix::errno::Errno; use num_derive::FromPrimitive; +use thiserror::Error; pub mod dev; #[derive(FromPrimitive, Debug, Copy, Clone)] @@ -26,11 +28,14 @@ pub enum EcResponseStatus { DUPUnavailable = 20, } -#[derive(Debug)] +#[derive(Error, Debug)] pub enum EcError { + #[error("command failed with status {0:?}")] Response(EcResponseStatus), + #[error("received unknown response code {0}")] UnknownResponseCode(u32), - DeviceError(String), + #[error("device error with errno {0:?}")] + DeviceError(Errno), } pub type EcCmdResult = Result;