From 7e58bcc61b8d2b29e9d6293f2e95e61ce5ac6a60 Mon Sep 17 00:00:00 2001 From: Tim Crawford Date: Mon, 30 Dec 2024 11:48:05 -0700 Subject: [PATCH] tool: Format code Signed-off-by: Tim Crawford --- tool/src/access/hid.rs | 5 +- tool/src/access/lpc/direct.rs | 24 +-- tool/src/access/lpc/linux.rs | 30 +--- tool/src/access/lpc/sim.rs | 20 +-- tool/src/access/mod.rs | 10 +- tool/src/ec.rs | 135 +++++---------- tool/src/legacy.rs | 11 +- tool/src/lib.rs | 3 +- tool/src/main.rs | 299 ++++++++++++++++++---------------- tool/src/pmc.rs | 6 +- tool/src/spi.rs | 100 ++++++------ tool/src/timeout.rs | 4 +- 12 files changed, 275 insertions(+), 372 deletions(-) diff --git a/tool/src/access/hid.rs b/tool/src/access/hid.rs index 28fd89aaf..8f0f4eba4 100644 --- a/tool/src/access/hid.rs +++ b/tool/src/access/hid.rs @@ -2,10 +2,7 @@ use hidapi::HidDevice; -use crate::{ - Access, - Error, -}; +use crate::{Access, Error}; /// Use USB HID access, only for USB ECs pub struct AccessHid { diff --git a/tool/src/access/lpc/direct.rs b/tool/src/access/lpc/direct.rs index 9c53dbed2..2c4a4e5c0 100644 --- a/tool/src/access/lpc/direct.rs +++ b/tool/src/access/lpc/direct.rs @@ -2,13 +2,7 @@ use hwio::{Io, Pio}; -use crate::{ - Access, - Error, - SuperIo, - Timeout, - timeout, -}; +use crate::{timeout, Access, Error, SuperIo, Timeout}; use super::*; @@ -24,9 +18,7 @@ impl AccessLpcDirect { pub unsafe fn new(timeout: T) -> Result { // Make sure EC ID matches let mut sio = SuperIo::new(0x2E); - let id = - (sio.read(0x20) as u16) << 8 | - (sio.read(0x21) as u16); + let id = (sio.read(0x20) as u16) << 8 | (sio.read(0x21) as u16); match id { 0x5570 | 0x8587 => (), _ => return Err(Error::SuperIoId(id)), @@ -41,24 +33,18 @@ impl AccessLpcDirect { /// Read from the command space unsafe fn read_cmd(&mut self, addr: u8) -> u8 { - Pio::::new( - self.cmd + (addr as u16) - ).read() + Pio::::new(self.cmd + (addr as u16)).read() } /// Write to the command space unsafe fn write_cmd(&mut self, addr: u8, data: u8) { - Pio::::new( - self.cmd + (addr as u16) - ).write(data) + Pio::::new(self.cmd + (addr as u16)).write(data) } /// Read from the debug space //TODO: better public interface pub unsafe fn read_debug(&mut self, addr: u8) -> u8 { - Pio::::new( - self.dbg + (addr as u16) - ).read() + Pio::::new(self.dbg + (addr as u16)).read() } /// Returns Ok if a command can be sent diff --git a/tool/src/access/lpc/linux.rs b/tool/src/access/lpc/linux.rs index f26a24806..b4dd46cd9 100644 --- a/tool/src/access/lpc/linux.rs +++ b/tool/src/access/lpc/linux.rs @@ -2,25 +2,13 @@ use std::{ fs, - io::{ - self, - Read, - Write, - Seek, - SeekFrom, - }, + io::{self, Read, Seek, SeekFrom, Write}, os::unix::io::AsRawFd, path::Path, time::Duration, }; -use crate::{ - Access, - Error, - StdTimeout, - Timeout, - timeout, -}; +use crate::{timeout, Access, Error, StdTimeout, Timeout}; use super::*; @@ -35,7 +23,7 @@ impl PortLock { if end < start { return Err(io::Error::new( io::ErrorKind::InvalidInput, - "PortLock::new: end < start" + "PortLock::new: end < start", )); } let len = (end - start) + 1; @@ -57,18 +45,14 @@ impl PortLock { return Err(io::Error::last_os_error()); } - Ok(Self { - start, - len, - file, - }) + Ok(Self { start, len, file }) } fn seek(&mut self, offset: u16) -> io::Result<()> { if offset >= self.len { return Err(io::Error::new( io::ErrorKind::InvalidInput, - "PortLock::seek: offset >= len" + "PortLock::seek: offset >= len", )); } let port = self.start + offset; @@ -76,7 +60,7 @@ impl PortLock { if pos != port as u64 { return Err(io::Error::new( io::ErrorKind::UnexpectedEof, - "PortLock::seek: failed to seek to port" + "PortLock::seek: failed to seek to port", )); } Ok(()) @@ -106,7 +90,7 @@ impl AccessLpcLinux { /// Locks ports and then returns access object pub unsafe fn new(timeout: Duration) -> Result { // TODO: is there a better way to probe before running a command? - if ! Path::new("/sys/bus/acpi/devices/17761776:00").is_dir() { + if !Path::new("/sys/bus/acpi/devices/17761776:00").is_dir() { return Err(Error::Io(io::Error::new( io::ErrorKind::NotFound, "Failed to find System76 ACPI device", diff --git a/tool/src/access/lpc/sim.rs b/tool/src/access/lpc/sim.rs index 46f3b243c..b563054e8 100644 --- a/tool/src/access/lpc/sim.rs +++ b/tool/src/access/lpc/sim.rs @@ -1,18 +1,8 @@ // SPDX-License-Identifier: MIT -use std::{ - io, - net::UdpSocket, - time::Duration, -}; - -use crate::{ - Access, - Error, - StdTimeout, - Timeout, - timeout, -}; +use std::{io, net::UdpSocket, time::Duration}; + +use crate::{timeout, Access, Error, StdTimeout, Timeout}; use super::*; @@ -39,7 +29,7 @@ impl AccessLpcSim { if self.socket.send(&request)? != request.len() { return Err(io::Error::new( io::ErrorKind::UnexpectedEof, - "Socket request incorrect size" + "Socket request incorrect size", )); } @@ -47,7 +37,7 @@ impl AccessLpcSim { if self.socket.recv(&mut response)? != response.len() { return Err(io::Error::new( io::ErrorKind::UnexpectedEof, - "Socket response incorrect size" + "Socket response incorrect size", )); } diff --git a/tool/src/access/mod.rs b/tool/src/access/mod.rs index 32ebcb8c0..dad443340 100644 --- a/tool/src/access/mod.rs +++ b/tool/src/access/mod.rs @@ -11,15 +11,9 @@ pub use self::hid::AccessHid; #[cfg(feature = "hidapi")] mod hid; -#[cfg(any( - feature = "redox_hwio", - all(feature = "std", target_os = "linux") -))] +#[cfg(any(feature = "redox_hwio", all(feature = "std", target_os = "linux")))] pub use self::lpc::*; -#[cfg(any( - feature = "redox_hwio", - all(feature = "std", target_os = "linux") -))] +#[cfg(any(feature = "redox_hwio", all(feature = "std", target_os = "linux")))] mod lpc; /// Access method for running an EC command diff --git a/tool/src/ec.rs b/tool/src/ec.rs index 0bd793784..ae2111954 100644 --- a/tool/src/ec.rs +++ b/tool/src/ec.rs @@ -1,18 +1,10 @@ // SPDX-License-Identifier: MIT #[cfg(not(feature = "std"))] -use alloc::{ - boxed::Box, - vec, -}; +use alloc::{boxed::Box, vec}; use core::convert::TryFrom; -use crate::{ - Access, - Error, - Spi, - SpiTarget, -}; +use crate::{Access, Error, Spi, SpiTarget}; #[derive(Clone, Copy, Debug, Eq, PartialEq)] #[repr(u8)] @@ -83,10 +75,7 @@ impl Ec { /// Probes for a compatible EC pub unsafe fn new(access: A) -> Result { // Create EC struct with provided access method and timeout - let mut ec = Ec { - access, - version: 0, - }; + let mut ec = Ec { access, version: 0 }; // Read version of protocol ec.version = ec.probe()?; @@ -188,117 +177,76 @@ impl Ec { /// Read fan duty cycle by fan index pub unsafe fn fan_get(&mut self, index: u8) -> Result { - let mut data = [ - index, - 0 - ]; + let mut data = [index, 0]; self.command(Cmd::FanGet, &mut data)?; Ok(data[1]) } /// Set fan duty cycle by fan index pub unsafe fn fan_set(&mut self, index: u8, duty: u8) -> Result<(), Error> { - let mut data = [ - index, - duty - ]; + let mut data = [index, duty]; self.command(Cmd::FanSet, &mut data) } /// Read keymap data by layout, output pin, and input pin pub unsafe fn keymap_get(&mut self, layer: u8, output: u8, input: u8) -> Result { - let mut data = [ - layer, - output, - input, - 0, - 0 - ]; + let mut data = [layer, output, input, 0, 0]; self.command(Cmd::KeymapGet, &mut data)?; - Ok( - (data[3] as u16) | - ((data[4] as u16) << 8) - ) + Ok((data[3] as u16) | ((data[4] as u16) << 8)) } /// Set keymap data by layout, output pin, and input pin - pub unsafe fn keymap_set(&mut self, layer: u8, output: u8, input: u8, value: u16) -> Result<(), Error> { - let mut data = [ - layer, - output, - input, - value as u8, - (value >> 8) as u8 - ]; + pub unsafe fn keymap_set( + &mut self, + layer: u8, + output: u8, + input: u8, + value: u16, + ) -> Result<(), Error> { + let mut data = [layer, output, input, value as u8, (value >> 8) as u8]; self.command(Cmd::KeymapSet, &mut data) } // Get LED value by index pub unsafe fn led_get_value(&mut self, index: u8) -> Result<(u8, u8), Error> { - let mut data = [ - index, - 0, - 0, - ]; + let mut data = [index, 0, 0]; self.command(Cmd::LedGetValue, &mut data)?; Ok((data[1], data[2])) } // Set LED value by index pub unsafe fn led_set_value(&mut self, index: u8, value: u8) -> Result<(), Error> { - let mut data = [ - index, - value, - ]; + let mut data = [index, value]; self.command(Cmd::LedSetValue, &mut data) } // Get LED color by index pub unsafe fn led_get_color(&mut self, index: u8) -> Result<(u8, u8, u8), Error> { - let mut data = [ - index, - 0, - 0, - 0, - ]; + let mut data = [index, 0, 0, 0]; self.command(Cmd::LedGetColor, &mut data)?; - Ok(( - data[1], - data[2], - data[3], - )) + Ok((data[1], data[2], data[3])) } // Set LED color by index - pub unsafe fn led_set_color(&mut self, index: u8, red: u8, green: u8, blue: u8) -> Result<(), Error> { - let mut data = [ - index, - red, - green, - blue, - ]; + pub unsafe fn led_set_color( + &mut self, + index: u8, + red: u8, + green: u8, + blue: u8, + ) -> Result<(), Error> { + let mut data = [index, red, green, blue]; self.command(Cmd::LedSetColor, &mut data) } pub unsafe fn led_get_mode(&mut self, layer: u8) -> Result<(u8, u8), Error> { - let mut data = [ - layer, - 0, - 0, - ]; + let mut data = [layer, 0, 0]; self.command(Cmd::LedGetMode, &mut data)?; - Ok(( - data[1], - data[2] - )) + Ok((data[1], data[2])) } pub unsafe fn led_set_mode(&mut self, layer: u8, mode: u8, speed: u8) -> Result<(), Error> { - let mut data = [ - layer, - mode, - speed, - ]; + let mut data = [layer, mode, speed]; self.command(Cmd::LedSetMode, &mut data) } @@ -316,23 +264,24 @@ impl Ec { /// Get security state pub unsafe fn security_get(&mut self) -> Result { - let mut data = [0]; - self.command(Cmd::SecurityGet, &mut data)?; - SecurityState::try_from(data[0]) + let mut data = [0]; + self.command(Cmd::SecurityGet, &mut data)?; + SecurityState::try_from(data[0]) } /// Set security state pub unsafe fn security_set(&mut self, state: SecurityState) -> Result<(), Error> { - let mut data = [state as u8]; - self.command(Cmd::SecuritySet, &mut data) + let mut data = [state as u8]; + self.command(Cmd::SecuritySet, &mut data) } pub fn into_dyn(self) -> Ec> - where A: 'static { + where + A: 'static, + { Ec { access: Box::new(self.access), version: self.version, - } } } @@ -364,7 +313,7 @@ impl<'a, A: Access> EcSpi<'a, A> { SpiTarget::Main => (), SpiTarget::Backup => { flags |= CMD_SPI_FLAG_BACKUP; - }, + } } flags @@ -394,7 +343,8 @@ impl<'a, A: Access> Spi for EcSpi<'a, A> { 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)])?; + self.ec + .command(Cmd::Spi, &mut self.buffer[..(chunk.len() + 2)])?; if self.buffer[1] != chunk.len() as u8 { return Err(Error::Verify); } @@ -414,7 +364,8 @@ impl<'a, A: Access> Spi for EcSpi<'a, A> { for i in 0..chunk.len() { self.buffer[i + 2] = chunk[i]; } - self.ec.command(Cmd::Spi, &mut self.buffer[..(chunk.len() + 2)])?; + 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/legacy.rs b/tool/src/legacy.rs index bc698bc6e..1ebc6cbea 100644 --- a/tool/src/legacy.rs +++ b/tool/src/legacy.rs @@ -1,11 +1,6 @@ // SPDX-License-Identifier: MIT -use crate::{ - Error, - Pmc, - SuperIo, - Timeout, -}; +use crate::{Error, Pmc, SuperIo, Timeout}; /// Run some EC commands on previous proprietary firmware pub struct EcLegacy { @@ -17,9 +12,7 @@ impl EcLegacy { pub unsafe fn new(primary: bool, timeout: T) -> Result { let mut sio = SuperIo::new(if primary { 0x2E } else { 0x4E }); - let id = - (sio.read(0x20) as u16) << 8 | - (sio.read(0x21) as u16); + let id = (sio.read(0x20) as u16) << 8 | (sio.read(0x21) as u16); match id { 0x5570 | 0x8587 => (), diff --git a/tool/src/lib.rs b/tool/src/lib.rs index 5afcac464..72bd0df64 100644 --- a/tool/src/lib.rs +++ b/tool/src/lib.rs @@ -16,7 +16,6 @@ #![allow(clippy::missing_safety_doc)] #![allow(clippy::needless_range_loop)] - #![cfg_attr(not(feature = "std"), no_std)] #[cfg(not(feature = "std"))] @@ -52,7 +51,7 @@ pub use self::super_io::SuperIo; #[cfg(feature = "redox_hwio")] mod super_io; -pub use self::timeout::Timeout; #[cfg(feature = "std")] pub use self::timeout::StdTimeout; +pub use self::timeout::Timeout; mod timeout; diff --git a/tool/src/main.rs b/tool/src/main.rs index a31da3fe9..b49747761 100644 --- a/tool/src/main.rs +++ b/tool/src/main.rs @@ -4,27 +4,11 @@ use clap::Parser; use ectool::{ - Access, - AccessHid, - AccessLpcLinux, - AccessLpcSim, - Ec, - Error, - Firmware, - SecurityState, - StdTimeout, - Spi, - SpiRom, - SpiTarget, + Access, AccessHid, AccessLpcLinux, AccessLpcSim, Ec, Error, Firmware, SecurityState, Spi, + SpiRom, SpiTarget, StdTimeout, }; use hidapi::HidApi; -use std::{ - fs, - process, - str, - time::Duration, - thread, -}; +use std::{fs, process, str, thread, time::Duration}; unsafe fn console(ec: &mut Ec>) -> Result<(), Error> { //TODO: driver support for reading debug region? @@ -38,7 +22,9 @@ unsafe fn console(ec: &mut Ec>) -> Result<(), Error> { } else { while head != tail { head += 1; - if head >= 256 { head = 1; } + if head >= 256 { + head = 1; + } let c = access.read_debug(head as u8)?; print!("{}", c as char); } @@ -46,14 +32,21 @@ unsafe fn console(ec: &mut Ec>) -> Result<(), Error> { } } -unsafe fn flash_read(spi: &mut SpiRom, rom: &mut [u8], sector_size: usize) -> Result<(), Error> { +unsafe fn flash_read( + spi: &mut SpiRom, + rom: &mut [u8], + sector_size: usize, +) -> Result<(), Error> { let mut address = 0; while address < rom.len() { eprint!("\rSPI Read {}K", address / 1024); let next_address = address + sector_size; let count = spi.read_at(address as u32, &mut rom[address..next_address])?; if count != sector_size { - eprintln!("\ncount {} did not match sector size {}", count, sector_size); + eprintln!( + "\ncount {} did not match sector size {}", + count, sector_size + ); return Err(Error::Verify); } address = next_address; @@ -62,7 +55,12 @@ unsafe fn flash_read(spi: &mut SpiRom, rom: &mut [u8], se Ok(()) } -unsafe fn flash_inner(ec: &mut Ec>, firmware: &Firmware, target: SpiTarget, scratch: bool) -> Result<(), Error> { +unsafe fn flash_inner( + ec: &mut Ec>, + firmware: &Firmware, + target: SpiTarget, + scratch: bool, +) -> Result<(), Error> { let new_rom = firmware.data.to_vec(); // XXX: Get flash size programatically? @@ -73,10 +71,7 @@ unsafe fn flash_inner(ec: &mut Ec>, firmware: &Firmware, target: } let mut spi_bus = ec.spi(target, scratch)?; - let mut spi = SpiRom::new( - &mut spi_bus, - StdTimeout::new(Duration::new(1, 0)) - ); + let mut spi = SpiRom::new(&mut spi_bus, StdTimeout::new(Duration::new(1, 0))); let sector_size = spi.sector_size(); let mut rom = vec![0xFF; rom_size]; @@ -109,14 +104,17 @@ unsafe fn flash_inner(ec: &mut Ec>, firmware: &Firmware, target: } } - if ! matches { - if ! erased { + if !matches { + if !erased { spi.erase_sector(address as u32)?; } - if ! new_erased { + if !new_erased { let count = spi.write_at(address as u32, &new_rom[address..next_address])?; if count != sector_size { - eprintln!("\nWrite count {} did not match sector size {}", count, sector_size); + eprintln!( + "\nWrite count {} did not match sector size {}", + count, sector_size + ); return Err(Error::Verify); } } @@ -130,7 +128,10 @@ unsafe fn flash_inner(ec: &mut Ec>, firmware: &Firmware, target: flash_read(&mut spi, &mut rom, sector_size)?; for i in 0..rom.len() { if rom[i] != new_rom[i] { - eprintln!("Failed to program: {:X} is {:X} instead of {:X}", i, rom[i], new_rom[i]); + eprintln!( + "Failed to program: {:X} is {:X} instead of {:X}", + i, rom[i], new_rom[i] + ); return Err(Error::Verify); } } @@ -159,7 +160,10 @@ unsafe fn flash(ec: &mut Ec>, path: &str, target: SpiTarget) -> let ec_board = &data[..size]; println!("ec board: {:?}", str::from_utf8(ec_board)); - assert!(ec_board == firmware.board, "file board does not match ec board"); + assert!( + ec_board == firmware.board, + "file board does not match ec board" + ); } { @@ -277,14 +281,25 @@ unsafe fn fan_set(ec: &mut Ec>, index: u8, duty: u8) -> Result<( ec.fan_set(index, duty) } -unsafe fn keymap_get(ec: &mut Ec>, layer: u8, output: u8, input: u8) -> Result<(), Error> { +unsafe fn keymap_get( + ec: &mut Ec>, + layer: u8, + output: u8, + input: u8, +) -> Result<(), Error> { let value = ec.keymap_get(layer, output, input)?; println!("{:04X}", value); Ok(()) } -unsafe fn keymap_set(ec: &mut Ec>, layer: u8, output: u8, input: u8, value: u16) -> Result<(), Error> { +unsafe fn keymap_set( + ec: &mut Ec>, + layer: u8, + output: u8, + input: u8, + value: u16, +) -> Result<(), Error> { ec.keymap_set(layer, output, input, value) } @@ -311,7 +326,6 @@ fn parse_color(s: &str) -> Result<(u8, u8, u8), String> { } } - #[derive(Parser)] #[clap(rename_all = "snake_case")] enum SubCommand { @@ -373,11 +387,7 @@ enum AccessMode { #[derive(Parser)] #[clap(name = "system76_ectool")] struct Args { - #[clap( - long = "access", - value_enum, - default_value = "lpc-linux", - )] + #[clap(long = "access", value_enum, default_value = "lpc-linux")] access: AccessMode, #[clap(subcommand)] subcommand: SubCommand, @@ -392,11 +402,11 @@ fn main() { AccessMode::LpcLinux => { let access = AccessLpcLinux::new(Duration::new(1, 0))?; Ok(Ec::new(access)?.into_dyn()) - }, + } AccessMode::LpcSim => { let access = AccessLpcSim::new(Duration::new(1, 0))?; Ok(Ec::new(access)?.into_dyn()) - }, + } AccessMode::Hid => { let api = HidApi::new()?; for info in api.device_list() { @@ -436,33 +446,29 @@ fn main() { Err(err) => { eprintln!("failed to read console: {:X?}", err); process::exit(1); - }, - }, - SubCommand::Fan { index, duty } => { - match duty { - Some(duty) => match unsafe { fan_set(&mut ec, index, duty) } { - Ok(()) => (), - Err(err) => { - eprintln!("failed to set fan {} to {}: {:X?}", index, duty, err); - process::exit(1); - }, - }, - None => match unsafe { fan_get(&mut ec, index) } { - Ok(()) => (), - Err(err) => { - eprintln!("failed to get fan {}: {:X?}", index, err); - process::exit(1); - }, - }, } }, - SubCommand::Flash { path } => { - match unsafe { flash(&mut ec, &path, SpiTarget::Main) } { + SubCommand::Fan { index, duty } => match duty { + Some(duty) => match unsafe { fan_set(&mut ec, index, duty) } { Ok(()) => (), Err(err) => { - eprintln!("failed to flash '{}': {:X?}", path, err); + eprintln!("failed to set fan {} to {}: {:X?}", index, duty, err); process::exit(1); - }, + } + }, + None => match unsafe { fan_get(&mut ec, index) } { + Ok(()) => (), + Err(err) => { + eprintln!("failed to get fan {}: {:X?}", index, err); + process::exit(1); + } + }, + }, + SubCommand::Flash { path } => match unsafe { flash(&mut ec, &path, SpiTarget::Main) } { + Ok(()) => (), + Err(err) => { + eprintln!("failed to flash '{}': {:X?}", path, err); + process::exit(1); } }, SubCommand::FlashBackup { path } => { @@ -471,39 +477,48 @@ fn main() { Err(err) => { eprintln!("failed to flash '{}': {:X?}", path, err); process::exit(1); - }, + } } - }, + } SubCommand::Info => match unsafe { info(&mut ec) } { Ok(()) => (), Err(err) => { eprintln!("failed to read info: {:X?}", err); process::exit(1); - }, + } }, - SubCommand::Keymap { layer, output, input, value } => { - match value { - Some(value_str) => match u16::from_str_radix(value_str.trim_start_matches("0x"), 16) { - Ok(value) => match unsafe { keymap_set(&mut ec, layer, output, input, value) } { - Ok(()) => (), - Err(err) => { - eprintln!("failed to set keymap {}, {}, {} to {}: {:X?}", layer, output, input, value, err); - process::exit(1); - }, - }, - Err(err) => { - eprintln!("failed to parse value: '{}': {}", value_str, err); - process::exit(1); - } - }, - None => match unsafe { keymap_get(&mut ec, layer, output, input) } { + SubCommand::Keymap { + layer, + output, + input, + value, + } => match value { + Some(value_str) => match u16::from_str_radix(value_str.trim_start_matches("0x"), 16) { + Ok(value) => match unsafe { keymap_set(&mut ec, layer, output, input, value) } { Ok(()) => (), Err(err) => { - eprintln!("failed to get keymap {}, {}, {}: {:X?}", layer, output, input, err); + eprintln!( + "failed to set keymap {}, {}, {} to {}: {:X?}", + layer, output, input, value, err + ); process::exit(1); - }, + } }, - } + Err(err) => { + eprintln!("failed to parse value: '{}': {}", value_str, err); + process::exit(1); + } + }, + None => match unsafe { keymap_get(&mut ec, layer, output, input) } { + Ok(()) => (), + Err(err) => { + eprintln!( + "failed to get keymap {}, {}, {}: {:X?}", + layer, output, input, err + ); + process::exit(1); + } + }, }, SubCommand::LedColor { index, value } => { if let Some(value) = value { @@ -513,7 +528,7 @@ fn main() { Err(err) => { eprintln!("failed to set color {}: {:X?}", value, err); process::exit(1); - }, + } } } else { match unsafe { ec.led_get_color(index) } { @@ -521,10 +536,10 @@ fn main() { Err(err) => { eprintln!("failed to get color: {:X?}", err); process::exit(1); - }, + } } } - }, + } SubCommand::LedValue { index, value } => { if let Some(value) = value { match unsafe { ec.led_set_value(index, value) } { @@ -532,111 +547,115 @@ fn main() { Err(err) => { eprintln!("failed to set value {}: {:X?}", value, err); process::exit(1); - }, + } } } else { match unsafe { ec.led_get_value(index) } { Ok((value, max)) => { println!("value: {}", value); println!("max: {}", max); - }, + } Err(err) => { eprintln!("failed to get value: {:X?}", err); process::exit(1); - }, + } } } - }, + } SubCommand::LedMode { layer, mode, speed } => { if let (Some(mode), Some(speed)) = (mode, speed) { match unsafe { ec.led_set_mode(layer, mode, speed) } { Ok(()) => (), Err(err) => { - eprintln!("failed to set layer {} mode {} at speed {}: {:X?}", layer, mode, speed, err); + eprintln!( + "failed to set layer {} mode {} at speed {}: {:X?}", + layer, mode, speed, err + ); process::exit(1); - }, + } } } else { match unsafe { ec.led_get_mode(layer) } { Ok((mode, speed)) => { println!("mode: {}", mode); println!("speed: {}", speed); - }, + } Err(err) => { eprintln!("failed to get mode for layer {}: {:X?}", layer, err); process::exit(1); - }, + } } } - }, + } SubCommand::LedSave => match unsafe { ec.led_save() } { Ok(()) => (), Err(err) => { eprintln!("failed to save LED settings: {:X?}", err); process::exit(1); - }, + } }, SubCommand::Reset => match unsafe { ec.reset() } { Ok(()) => (), Err(err) => { eprintln!("failed to reset device: {:X?}", err); process::exit(1); - }, + } }, SubCommand::Matrix => match unsafe { matrix(&mut ec) } { Ok(()) => (), Err(err) => { eprintln!("failed to read matrix: {:X?}", err); process::exit(1); - }, - }, - SubCommand::Print { message } => for arg in message { - let mut arg = arg.to_owned(); - arg.push('\n'); - match unsafe { print(&mut ec, arg.as_bytes()) } { - Ok(()) => (), - Err(err) => { - eprintln!("failed to print '{}': {:X?}", arg, err); - process::exit(1); - }, } }, - SubCommand::SetNoInput { value } => { - match unsafe { ec.set_no_input(value) } { - Ok(()) => (), - Err(err) => { - eprintln!("failed to set no_input mode: {:X?}", err); - process::exit(1); + SubCommand::Print { message } => { + for arg in message { + let mut arg = arg.to_owned(); + arg.push('\n'); + match unsafe { print(&mut ec, arg.as_bytes()) } { + Ok(()) => (), + Err(err) => { + eprintln!("failed to print '{}': {:X?}", arg, err); + process::exit(1); + } } } + } + SubCommand::SetNoInput { value } => match unsafe { ec.set_no_input(value) } { + Ok(()) => (), + Err(err) => { + eprintln!("failed to set no_input mode: {:X?}", err); + process::exit(1); + } }, - SubCommand::Security { state } => { - match state { - Some(value) => { - let state = match value.as_str() { - "lock" => SecurityState::PrepareLock, - "unlock" => SecurityState::PrepareUnlock, - _ => { - eprintln!("invalid security state '{}': must be 'lock' or 'unlock'", value); - process::exit(1); - } - }; - match unsafe { security_set(&mut ec, state) } { - Ok(()) => (), - Err(err) => { - eprintln!("failed to set security state to '{}': {:X?}", value, err); - process::exit(1); - }, + SubCommand::Security { state } => match state { + Some(value) => { + let state = match value.as_str() { + "lock" => SecurityState::PrepareLock, + "unlock" => SecurityState::PrepareUnlock, + _ => { + eprintln!( + "invalid security state '{}': must be 'lock' or 'unlock'", + value + ); + process::exit(1); } - }, - None => match unsafe { security_get(&mut ec) } { + }; + match unsafe { security_set(&mut ec, state) } { Ok(()) => (), Err(err) => { - eprintln!("failed to get security state: {:X?}", err); + eprintln!("failed to set security state to '{}': {:X?}", value, err); process::exit(1); - }, - }, + } + } } + None => match unsafe { security_get(&mut ec) } { + Ok(()) => (), + Err(err) => { + eprintln!("failed to get security state: {:X?}", err); + process::exit(1); + } + }, }, } } diff --git a/tool/src/pmc.rs b/tool/src/pmc.rs index 6ebbc6360..d5ddfb457 100644 --- a/tool/src/pmc.rs +++ b/tool/src/pmc.rs @@ -2,11 +2,7 @@ use hwio::{Io, Pio}; -use crate::{ - Error, - Timeout, - timeout -}; +use crate::{timeout, Error, Timeout}; /// Standard ACPI EC interface pub struct Pmc { diff --git a/tool/src/spi.rs b/tool/src/spi.rs index abb6fe858..719dd437d 100644 --- a/tool/src/spi.rs +++ b/tool/src/spi.rs @@ -1,9 +1,6 @@ // SPDX-License-Identifier: MIT -use crate::{ - Error, - Timeout, -}; +use crate::{Error, Timeout}; /// SPI bus transactions pub trait Spi { @@ -38,10 +35,7 @@ pub struct SpiRom<'a, S: Spi, T: Timeout> { impl<'a, S: Spi, T: Timeout> SpiRom<'a, S, T> { /// Create a SPI ROM using the specified SPI bus and timeout pub fn new(spi: &'a mut S, timeout: T) -> Self { - Self { - spi, - timeout, - } + Self { spi, timeout } } /// Get sector size in bytes @@ -153,55 +147,55 @@ impl<'a, S: Spi, T: Timeout> SpiRom<'a, S, T> { //TODO: automatically detect write command match self.spi.target() { - SpiTarget::Main => for (i, word) in data.chunks(2).enumerate() { - #[allow(clippy::get_first)] - let low = *word.get(0).unwrap_or(&0xFF); - let high = *word.get(1).unwrap_or(&0xFF); - - self.spi.reset()?; - if i == 0 { - self.spi.write(&[ - 0xAD, - (address >> 16) as u8, - (address >> 8) as u8, - address as u8, - low, - high - ])?; - } else { + SpiTarget::Main => { + for (i, word) in data.chunks(2).enumerate() { + #[allow(clippy::get_first)] + let low = *word.get(0).unwrap_or(&0xFF); + let high = *word.get(1).unwrap_or(&0xFF); + + self.spi.reset()?; + if i == 0 { + self.spi.write(&[ + 0xAD, + (address >> 16) as u8, + (address >> 8) as u8, + address as u8, + low, + high, + ])?; + } else { + self.spi.write(&[0xAD, low, high])?; + } + + // Poll status for busy unset + self.status_wait(1, 0)?; + } + } + SpiTarget::Backup => { + for (i, page) in data.chunks(256).enumerate() { + let page_address = address + i as u32 * 256; + if page_address % 256 != 0 { + return Err(Error::Parameter); + } + + if i > 0 { + // Write enable clears after each page is written + self.write_enable()?; + } + + self.spi.reset()?; self.spi.write(&[ - 0xAD, - low, - high + 0xF2, + (page_address >> 16) as u8, + (page_address >> 8) as u8, + page_address as u8, ])?; - } - - // Poll status for busy unset - self.status_wait(1, 0)?; - }, - SpiTarget::Backup => for (i, page) in data.chunks(256).enumerate() { - let page_address = address + i as u32 * 256; - if page_address % 256 != 0 { - return Err(Error::Parameter); - } + self.spi.write(page)?; - if i > 0 { - // Write enable clears after each page is written - self.write_enable()?; + // Poll status for busy unset + self.status_wait(1, 0)?; } - - self.spi.reset()?; - self.spi.write(&[ - 0xF2, - (page_address >> 16) as u8, - (page_address >> 8) as u8, - page_address as u8, - ])?; - self.spi.write(page)?; - - // Poll status for busy unset - self.status_wait(1, 0)?; - }, + } } self.write_disable()?; diff --git a/tool/src/timeout.rs b/tool/src/timeout.rs index 5ee7919ba..78364e708 100644 --- a/tool/src/timeout.rs +++ b/tool/src/timeout.rs @@ -12,7 +12,7 @@ macro_rules! timeout { Ok(ok) => { result = Ok(ok); break; - }, + } Err(err) => match err { $crate::Error::WouldBlock => (), _ => { @@ -48,7 +48,7 @@ impl StdTimeout { pub fn new(duration: Duration) -> Self { StdTimeout { instant: Instant::now(), - duration + duration, } } }