diff --git a/tool/Cargo.lock b/tool/Cargo.lock index eb21cca..cc0eed1 100644 --- a/tool/Cargo.lock +++ b/tool/Cargo.lock @@ -11,13 +11,13 @@ version = "1.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "cc 1.0.60 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.78 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.79 (registry+https://github.com/rust-lang/crates.io-index)", "pkg-config 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "libc" -version = "0.2.78" +version = "0.2.79" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -32,16 +32,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "system76_ectool" -version = "0.2.1" +version = "0.2.2" dependencies = [ "hidapi 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.78 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.79 (registry+https://github.com/rust-lang/crates.io-index)", "redox_hwio 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", ] [metadata] "checksum cc 1.0.60 (registry+https://github.com/rust-lang/crates.io-index)" = "ef611cc68ff783f18535d77ddd080185275713d852c4f5cbb6122c462a7a825c" "checksum hidapi 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "5c6ffb97f2ec5835ec73bcea5256fc2cd57a13c5958230778ef97f11900ba661" -"checksum libc 0.2.78 (registry+https://github.com/rust-lang/crates.io-index)" = "aa7087f49d294270db4e1928fc110c976cd4b9e5a16348e0a1df09afa99e6c98" +"checksum libc 0.2.79 (registry+https://github.com/rust-lang/crates.io-index)" = "2448f6066e80e3bfc792e9c98bf705b4b0fc6e8ef5b43e5889aff0eaa9c58743" "checksum pkg-config 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)" = "d36492546b6af1463394d46f0c834346f31548646f6ba10849802c9c9a27ac33" "checksum redox_hwio 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "41aa2c4c67329a04106644cad336238aa5adecfd73d06fb10339d472ce6d8070" diff --git a/tool/Cargo.toml b/tool/Cargo.toml index 9b6cde8..20b4c61 100644 --- a/tool/Cargo.toml +++ b/tool/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "system76_ectool" -version = "0.2.1" +version = "0.2.2" edition = "2018" description = "System76 EC tool" license = "MIT" diff --git a/tool/src/ec.rs b/tool/src/ec.rs index f8515c7..8491b25 100644 --- a/tool/src/ec.rs +++ b/tool/src/ec.rs @@ -1,3 +1,9 @@ +#[cfg(not(feature = "std"))] +use alloc::{ + boxed::Box, + vec, +}; + use crate::{ Access, Error, @@ -125,10 +131,12 @@ impl Ec { /// Access EC SPI bus pub unsafe fn spi(&mut self, target: SpiTarget, scratch: bool) -> Result, Error> { + let data_size = self.access.data_size(); let mut spi = EcSpi { ec: self, target, scratch, + buffer: vec![0; data_size].into_boxed_slice(), }; spi.reset()?; Ok(spi) @@ -191,6 +199,7 @@ pub struct EcSpi<'a, A: Access> { ec: &'a mut Ec, target: SpiTarget, scratch: bool, + buffer: Box<[u8]>, } impl<'a, A: Access> EcSpi<'a, A> { @@ -228,12 +237,10 @@ impl<'a, A: Access> Spi for EcSpi<'a, A> { /// Disable SPI chip, must be done before and after a transaction unsafe fn reset(&mut self) -> Result<(), Error> { let flags = self.flags(false, true); - let mut data = [ - flags, - 0, - ]; - self.ec.command(Cmd::Spi, &mut data)?; - if data[1] != 0 { + self.buffer[0] = flags; + self.buffer[1] = 0; + self.ec.command(Cmd::Spi, &mut self.buffer[..2])?; + if self.buffer[1] != 0 { return Err(Error::Verify); } Ok(()) @@ -241,18 +248,16 @@ impl<'a, A: Access> Spi for EcSpi<'a, A> { /// SPI read unsafe fn read(&mut self, data: &mut [u8]) -> Result { - //TODO: use self.access.data_size() let flags = self.flags(true, false); - for chunk in data.chunks_mut(256 - 4) { - let mut data = [0; 256 - 2]; - data[0] = flags; - data[1] = chunk.len() as u8; - self.ec.command(Cmd::Spi, &mut data)?; - if data[1] != chunk.len() as u8 { + for chunk in data.chunks_mut(self.buffer.len() - 2) { + self.buffer[0] = flags; + self.buffer[1] = chunk.len() as u8; + self.ec.command(Cmd::Spi, &mut self.buffer[..(chunk.len() + 2)])?; + if self.buffer[1] != chunk.len() as u8 { return Err(Error::Verify); } for i in 0..chunk.len() { - chunk[i] = data[i + 2]; + chunk[i] = self.buffer[i + 2]; } } Ok(data.len()) @@ -260,17 +265,15 @@ impl<'a, A: Access> Spi for EcSpi<'a, A> { /// SPI write unsafe fn write(&mut self, data: &[u8]) -> Result { - //TODO: use self.access.data_size() let flags = self.flags(false, false); - for chunk in data.chunks(256 - 4) { - let mut data = [0; 256 - 2]; - data[0] = flags; - data[1] = chunk.len() as u8; + for chunk in data.chunks(self.buffer.len() - 2) { + self.buffer[0] = flags; + self.buffer[1] = chunk.len() as u8; for i in 0..chunk.len() { - data[i + 2] = chunk[i]; + self.buffer[i + 2] = chunk[i]; } - self.ec.command(Cmd::Spi, &mut data)?; - if data[1] != chunk.len() as u8 { + self.ec.command(Cmd::Spi, &mut self.buffer[..(chunk.len() + 2)])?; + if self.buffer[1] != chunk.len() as u8 { return Err(Error::Verify); } } diff --git a/tool/src/lib.rs b/tool/src/lib.rs index 1129f2d..1761c7e 100644 --- a/tool/src/lib.rs +++ b/tool/src/lib.rs @@ -1,5 +1,3 @@ -#![cfg_attr(not(feature = "std"), no_std)] - //! Library for accessing System76 ECs //! First, construct an access method, using an object implementing the `Access` trait. Next, an Ec //! object can be contructed, which exposes the command interface. @@ -14,6 +12,11 @@ //! compiler. It is only recommended to use these in firmware, as mutual exclusion is not //! guaranteed. +#![cfg_attr(not(feature = "std"), no_std)] + +#[cfg(not(feature = "std"))] +extern crate alloc; + pub use self::access::*; mod access;