Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tool: Restrict port IO interfaces to x86 #507

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 47 additions & 41 deletions tool/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion tool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ clap = { version = "4.5", features = ["derive"], optional = true }
libc = { version = "0.2", optional = true }
# NOTE: Upgrading to 2.x blocked on Ubuntu shipping newer hidapi
hidapi = { version = "1.5", default-features = false, features = ["linux-shared-hidraw"], optional = true }
redox_hwio = { version = "0.1.6", default-features = false, optional = true }
downcast-rs = { version = "1.2.0", default-features = false }

[target.'cfg(any(target_arch = "x86", target_arch = "x86_64"))'.dependencies]
redox_hwio = { version = "0.1.6", default-features = false, optional = true }

[features]
default = ["std", "hidapi", "clap"]
std = ["libc", "downcast-rs/std"]
Expand Down
5 changes: 1 addition & 4 deletions tool/src/access/hid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
24 changes: 5 additions & 19 deletions tool/src/access/lpc/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@

use hwio::{Io, Pio};

use crate::{
Access,
Error,
SuperIo,
Timeout,
timeout,
};
use crate::{timeout, Access, Error, SuperIo, Timeout};

use super::*;

Expand All @@ -24,9 +18,7 @@ impl<T: Timeout + Send> AccessLpcDirect<T> {
pub unsafe fn new(timeout: T) -> Result<Self, Error> {
// 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)),
Expand All @@ -41,24 +33,18 @@ impl<T: Timeout + Send> AccessLpcDirect<T> {

/// Read from the command space
unsafe fn read_cmd(&mut self, addr: u8) -> u8 {
Pio::<u8>::new(
self.cmd + (addr as u16)
).read()
Pio::<u8>::new(self.cmd + (addr as u16)).read()
}

/// Write to the command space
unsafe fn write_cmd(&mut self, addr: u8, data: u8) {
Pio::<u8>::new(
self.cmd + (addr as u16)
).write(data)
Pio::<u8>::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::<u8>::new(
self.dbg + (addr as u16)
).read()
Pio::<u8>::new(self.dbg + (addr as u16)).read()
}

/// Returns Ok if a command can be sent
Expand Down
30 changes: 7 additions & 23 deletions tool/src/access/lpc/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand All @@ -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;
Expand All @@ -57,26 +45,22 @@ 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;
let pos = self.file.seek(SeekFrom::Start(port as u64))?;
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(())
Expand Down Expand Up @@ -106,7 +90,7 @@ impl AccessLpcLinux {
/// Locks ports and then returns access object
pub unsafe fn new(timeout: Duration) -> Result<Self, Error> {
// 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",
Expand Down
Loading
Loading