1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use std::{
fs,
io::{
self,
Read,
Write,
Seek,
SeekFrom,
},
os::unix::io::AsRawFd,
path::Path,
time::Duration,
};
use crate::{
Access,
Error,
StdTimeout,
Timeout,
timeout,
};
use super::*;
struct PortLock {
start: u16,
len: u16,
file: fs::File,
}
impl PortLock {
pub fn new(start: u16, end: u16) -> io::Result<Self> {
if end < start {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"PortLock::new: end < start"
));
}
let len = (end - start) + 1;
let file = fs::OpenOptions::new()
.read(true)
.write(true)
.open("/dev/port")?;
let mut flock = libc::flock {
l_type: libc::F_WRLCK as _,
l_whence: libc::SEEK_SET as _,
l_start: start as _,
l_len: len as _,
l_pid: 0,
};
if unsafe { libc::fcntl(file.as_raw_fd(), libc::F_SETLK, &mut flock) < 0 } {
return Err(io::Error::last_os_error());
}
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"
));
}
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"
));
}
Ok(())
}
pub fn read(&mut self, offset: u16) -> io::Result<u8> {
self.seek(offset)?;
let mut data = [0];
self.file.read_exact(&mut data)?;
Ok(data[0])
}
pub fn write(&mut self, offset: u16, value: u8) -> io::Result<()> {
self.seek(offset)?;
self.file.write_all(&[value])
}
}
pub struct AccessLpcLinux {
cmd: PortLock,
dbg: PortLock,
timeout: StdTimeout,
}
impl AccessLpcLinux {
pub unsafe fn new(timeout: Duration) -> Result<Self, Error> {
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",
)));
}
let cmd = PortLock::new(SMFI_CMD_BASE, SMFI_CMD_BASE + SMFI_CMD_SIZE as u16 - 1)?;
let dbg = PortLock::new(SMFI_DBG_BASE, SMFI_DBG_BASE + SMFI_DBG_SIZE as u16 - 1)?;
Ok(Self {
cmd,
dbg,
timeout: StdTimeout::new(timeout),
})
}
unsafe fn read_cmd(&mut self, addr: u8) -> Result<u8, Error> {
Ok(self.cmd.read(addr as u16)?)
}
unsafe fn write_cmd(&mut self, addr: u8, data: u8) -> Result<(), Error> {
Ok(self.cmd.write(addr as u16, data)?)
}
unsafe fn command_check(&mut self) -> Result<(), Error> {
if self.read_cmd(SMFI_CMD_CMD)? == 0 {
Ok(())
} else {
Err(Error::WouldBlock)
}
}
}
impl Access for AccessLpcLinux {
unsafe fn command(&mut self, cmd: u8, data: &mut [u8]) -> Result<u8, Error> {
if data.len() > self.data_size() {
return Err(Error::DataLength(data.len()));
}
self.command_check()?;
for i in 0..data.len() {
self.write_cmd(i as u8 + SMFI_CMD_DATA, data[i])?;
}
self.write_cmd(SMFI_CMD_CMD, cmd as u8)?;
self.timeout.reset();
timeout!(self.timeout, self.command_check())?;
for i in 0..data.len() {
data[i] = self.read_cmd(i as u8 + SMFI_CMD_DATA)?;
}
self.read_cmd(SMFI_CMD_RES)
}
fn data_size(&self) -> usize {
SMFI_CMD_SIZE - SMFI_CMD_DATA as usize
}
unsafe fn read_debug(&mut self, addr: u8) -> Result<u8, Error> {
Ok(self.dbg.read(addr as u16)?)
}
}