Skip to content

Commit 19260ef

Browse files
committed
Simplify features and detect based on OS
Now we can build on Windows just with `cargo build`! Signed-off-by: Daniel Schaefer <[email protected]>
1 parent 58960fa commit 19260ef

File tree

16 files changed

+202
-224
lines changed

16 files changed

+202
-224
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,15 @@ jobs:
9393

9494
# Build debug library first to fail fast
9595
- name: Build library (Windows)
96-
run: cargo build -p framework_lib --no-default-features --features "windows"
96+
run: cargo build -p framework_lib
9797

9898
- name: Build Windows tool
9999
run: |
100-
cargo build -p framework_tool --no-default-features --features "windows"
101-
cargo build -p framework_tool --no-default-features --features "windows" --release
100+
cargo build -p framework_tool
101+
cargo build -p framework_tool --release
102102
103103
- name: Check if Windows tool can start
104-
run: cargo run --no-default-features --features "windows" -- --help --release
104+
run: cargo run -- --help --release
105105

106106
# Upload release build so that vcruntime is statically linked
107107
- name: Upload Windows App

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,10 @@ Building on Windows or in general with fewer features:
130130

131131
```ps1
132132
# Build the library and tool
133-
cargo build --no-default-features --features "windows"
133+
cargo build
134134
135135
# Running the tool
136-
cargo run --no-default-features --features "windows"
136+
cargo run
137137
```
138138

139139
Cross compile from Linux to FreeBSD:
@@ -146,7 +146,7 @@ cargo install cross
146146
sudo systemctl start docker
147147

148148
# Build
149-
cross build --target=x86_64-unknown-freebsd --no-default-features --features unix
149+
cross build --target=x86_64-unknown-freebsd --no-default-features --features cross_freebsd
150150
```
151151

152152
## Running
@@ -375,8 +375,8 @@ Keyboard backlight: 0%
375375
sudo pkg install hidapi
376376
377377
# Build the library and tool
378-
cargo build --no-default-features --features freebsd
378+
cargo build
379379
380380
# Running the tool
381-
cargo run --no-default-features --features freebsd
381+
cargo run
382382
```

framework_lib/Cargo.toml

Lines changed: 35 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,79 +8,66 @@ rust-version = "1.74"
88
build = "build.rs"
99

1010
[features]
11-
default = ["linux"]
12-
# Linux/FreeBSD
13-
unix = ["std", "raw_pio", "smbios", "dep:nix", "dep:libc"]
14-
linux = ["unix", "linux_pio", "cros_ec_driver", "hidapi", "rusb"]
15-
freebsd = ["unix", "freebsd_pio", "hidapi", "rusb"]
11+
default = ["std"]
12+
std = ["hidapi", "rusb"]
13+
1614
# hidapi and rusb don't seem to build in the cross container at the moment
17-
cross_freebsd = ["unix", "freebsd_pio"]
18-
# Windows does not have the cros_ec driver nor raw port I/O access to userspace
19-
windows = ["std", "smbios", "dep:windows", "win_driver", "raw_pio", "hidapi", "rusb", "dep:wmi"]
20-
smbios = ["dep:smbios-lib"]
21-
std = ["dep:clap", "dep:clap-num", "dep:clap-verbosity-flag", "dep:env_logger", "smbios-lib?/std"]
15+
cross_freebsd = []
2216
rusb = ["dep:rusb"]
2317
hidapi = ["dep:hidapi"]
2418
uefi = [
25-
"dep:plain", "raw_pio", "smbios", "lazy_static/spin_no_std", "dep:uefi", "dep:uefi-services",
19+
"lazy_static/spin_no_std",
2620
# Otherwise I get: `LLVM ERROR: Do not know how to split the result of this operator!`
2721
# Seems to be a Ruset/LLVM bug when SSE is enabled.
2822
# See: https://github.com/rust-lang/rust/issues/61721
2923
"sha2/force-soft"
3024
]
3125

32-
# EC communication via Port I/O on FreeBSD
33-
freebsd_pio = ["redox_hwio/std"]
34-
# EC communication via Port I/O on Linux
35-
linux_pio = ["dep:libc", "redox_hwio/std"]
36-
# EC communication via raw Port I/O (e.g. UEFI or other ring 0 code)
37-
raw_pio = []
38-
# EC communication via cros_ec driver on Linux
39-
cros_ec_driver = []
40-
41-
# Chromium EC driver by DHowett
42-
win_driver = []
43-
4426
[build-dependencies]
4527
built = { version = "0.5", features = ["chrono", "git2"] }
4628

4729
[dependencies]
4830
lazy_static = "1.4.0"
4931
sha2 = { version = "0.10.8", default-features = false, features = [ "force-soft" ] }
5032
regex = { version = "1.11.1", default-features = false }
51-
redox_hwio = { git = "https://github.com/FrameworkComputer/rust-hwio", branch = "freebsd", default-features = false }
52-
libc = { version = "0.2.155", optional = true }
53-
clap = { version = "4.5", features = ["derive", "cargo"], optional = true }
54-
clap-num = { version = "1.2.0", optional = true }
55-
clap-verbosity-flag = { version = "2.2.1", optional = true }
56-
nix = { version = "0.29.0", features = ["ioctl", "user"], optional = true }
5733
num = { version = "0.4", default-features = false }
5834
num-derive = { version = "0.4", default-features = false }
5935
num-traits = { version = "0.2", default-features = false }
60-
env_logger = { version = "0.11", optional = true }
6136
log = { version = "0.4", default-features = true }
62-
uefi = { version = "0.20", features = ["alloc"], optional = true }
63-
uefi-services = { version = "0.17", optional = true }
64-
plain = { version = "0.2.3", optional = true }
65-
spin = { version = "0.9.8", optional = false }
66-
hidapi = { version = "2.6.3", optional = true, features = [ "windows-native" ] }
67-
rusb = { version = "0.9.4", optional = true }
37+
spin = { version = "0.9.8" }
6838
no-std-compat = { version = "0.4.1", features = [ "alloc" ] }
6939
guid_macros = { path = "../guid_macros" }
70-
wmi = { version = "0.15.0", optional = true }
7140

72-
[dependencies.smbios-lib]
73-
git = "https://github.com/FrameworkComputer/smbios-lib.git"
74-
branch = "no-std"
75-
optional = true
76-
default-features = false
77-
# Local development
78-
#path = "../../smbios-lib"
79-
# After my changes are upstreamed
80-
#version = "0.9.0"
41+
[target.'cfg(target_os = "uefi")'.dependencies]
42+
uefi = { version = "0.20", features = ["alloc"] }
43+
uefi-services = "0.17"
44+
plain = "0.2.3"
45+
redox_hwio = { git = "https://github.com/FrameworkComputer/rust-hwio", branch = "freebsd", default-features = false }
46+
smbios-lib = { git = "https://github.com/FrameworkComputer/smbios-lib.git", branch = "no-std", default-features = false }
47+
48+
[target.'cfg(windows)'.dependencies]
49+
wmi = "0.15.0"
50+
smbios-lib = { git = "https://github.com/FrameworkComputer/smbios-lib.git", branch = "no-std" }
51+
env_logger = "0.11"
52+
clap = { version = "4.5", features = ["derive", "cargo"] }
53+
clap-num = { version = "1.2.0" }
54+
clap-verbosity-flag = { version = "2.2.1" }
55+
hidapi = { version = "2.6.3", features = [ "windows-native" ] }
56+
rusb = "0.9.4"
57+
58+
[target.'cfg(unix)'.dependencies]
59+
libc = "0.2.155"
60+
nix = { version = "0.29.0", features = ["ioctl", "user"] }
61+
redox_hwio = { git = "https://github.com/FrameworkComputer/rust-hwio", branch = "freebsd" }
62+
smbios-lib = { git = "https://github.com/FrameworkComputer/smbios-lib.git", branch = "no-std" }
63+
env_logger = "0.11"
64+
clap = { version = "4.5", features = ["derive", "cargo"] }
65+
clap-num = { version = "1.2.0" }
66+
clap-verbosity-flag = { version = "2.2.1" }
67+
hidapi = { version = "2.6.3", features = [ "windows-native" ], optional = true }
68+
rusb = { version = "0.9.4", optional = true }
8169

82-
[dependencies.windows]
83-
optional = true
70+
[target.'cfg(windows)'.dependencies.windows]
8471
version = "0.59.0"
8572
features = [
8673
"Win32_Foundation",

framework_lib/src/chromium_ec/command.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ pub trait EcRequestRaw<R> {
173173
{
174174
let response = self.send_command_vec_extra(ec, extra_data)?;
175175
// TODO: The Windows driver seems to return 20 more bytes than expected
176-
#[cfg(feature = "win_driver")]
176+
#[cfg(windows)]
177177
let expected = response.len() != std::mem::size_of::<R>() + 20;
178-
#[cfg(not(feature = "win_driver"))]
178+
#[cfg(not(windows))]
179179
let expected = response.len() != std::mem::size_of::<R>();
180180
if expected {
181181
return Err(EcError::DeviceError(format!(

framework_lib/src/chromium_ec/mod.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@ use num_derive::FromPrimitive;
2121

2222
pub mod command;
2323
pub mod commands;
24-
#[cfg(feature = "cros_ec_driver")]
24+
#[cfg(target_os = "linux")]
2525
mod cros_ec;
2626
pub mod i2c_passthrough;
2727
pub mod input_deck;
28+
#[cfg(not(windows))]
2829
mod portio;
30+
#[cfg(not(windows))]
2931
mod portio_mec;
30-
#[cfg(feature = "win_driver")]
32+
#[allow(dead_code)]
33+
mod protocol;
34+
#[cfg(windows)]
3135
mod windows;
3236

3337
use alloc::format;
@@ -224,15 +228,15 @@ impl Default for CrosEc {
224228
fn available_drivers() -> Vec<CrosEcDriverType> {
225229
let mut drivers = vec![];
226230

227-
#[cfg(feature = "win_driver")]
231+
#[cfg(windows)]
228232
drivers.push(CrosEcDriverType::Windows);
229233

230-
#[cfg(feature = "cros_ec_driver")]
234+
#[cfg(target_os = "linux")]
231235
if std::path::Path::new(cros_ec::DEV_PATH).exists() {
232236
drivers.push(CrosEcDriverType::CrosEc);
233237
}
234238

235-
#[cfg(not(feature = "windows"))]
239+
#[cfg(not(windows))]
236240
drivers.push(CrosEcDriverType::Portio);
237241

238242
drivers
@@ -1320,10 +1324,11 @@ impl CrosEcDriver for CrosEc {
13201324

13211325
// TODO: Change this function to return EcResult instead and print the error only in UI code
13221326
print_err(match self.driver {
1327+
#[cfg(not(windows))]
13231328
CrosEcDriverType::Portio => portio::read_memory(offset, length),
1324-
#[cfg(feature = "win_driver")]
1329+
#[cfg(windows)]
13251330
CrosEcDriverType::Windows => windows::read_memory(offset, length),
1326-
#[cfg(feature = "cros_ec_driver")]
1331+
#[cfg(target_os = "linux")]
13271332
CrosEcDriverType::CrosEc => cros_ec::read_memory(offset, length),
13281333
_ => Err(EcError::DeviceError("No EC driver available".to_string())),
13291334
})
@@ -1341,10 +1346,11 @@ impl CrosEcDriver for CrosEc {
13411346
}
13421347

13431348
match self.driver {
1349+
#[cfg(not(windows))]
13441350
CrosEcDriverType::Portio => portio::send_command(command, command_version, data),
1345-
#[cfg(feature = "win_driver")]
1351+
#[cfg(windows)]
13461352
CrosEcDriverType::Windows => windows::send_command(command, command_version, data),
1347-
#[cfg(feature = "cros_ec_driver")]
1353+
#[cfg(target_os = "linux")]
13481354
CrosEcDriverType::CrosEc => cros_ec::send_command(command, command_version, data),
13491355
_ => Err(EcError::DeviceError("No EC driver available".to_string())),
13501356
}

framework_lib/src/chromium_ec/portio.rs

Lines changed: 6 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -4,130 +4,21 @@ use alloc::string::ToString;
44
use alloc::vec;
55
use alloc::vec::Vec;
66
use core::convert::TryInto;
7-
#[cfg(any(feature = "linux_pio", feature = "freebsd_pio", feature = "raw_pio"))]
7+
#[cfg(not(windows))]
88
use hwio::{Io, Pio};
9-
#[cfg(all(feature = "linux_pio", target_os = "linux"))]
9+
#[cfg(target_os = "linux")]
1010
use libc::ioperm;
1111
use log::Level;
12-
#[cfg(feature = "linux_pio")]
12+
#[cfg(target_os = "linux")]
1313
use nix::unistd::Uid;
1414
use num::FromPrimitive;
1515
use spin::Mutex;
1616

17+
use crate::chromium_ec::protocol::*;
1718
use crate::chromium_ec::{portio_mec, EC_MEMMAP_ID};
1819
use crate::os_specific;
1920
use crate::util;
2021

21-
/*
22-
* Value written to legacy command port / prefix byte to indicate protocol
23-
* 3+ structs are being used. Usage is bus-dependent.
24-
*/
25-
const EC_COMMAND_PROTOCOL_3: u8 = 0xda;
26-
27-
// LPC command status byte masks
28-
/// EC has written data but host hasn't consumed it yet
29-
const _EC_LPC_STATUS_TO_HOST: u8 = 0x01;
30-
/// Host has written data/command but EC hasn't consumed it yet
31-
const EC_LPC_STATUS_FROM_HOST: u8 = 0x02;
32-
/// EC is still processing a command
33-
const EC_LPC_STATUS_PROCESSING: u8 = 0x04;
34-
/// Previous command wasn't data but command
35-
const _EC_LPC_STATUS_LAST_CMD: u8 = 0x08;
36-
/// EC is in burst mode
37-
const _EC_LPC_STATUS_BURST_MODE: u8 = 0x10;
38-
/// SCI event is pending (requesting SCI query)
39-
const _EC_LPC_STATUS_SCI_PENDING: u8 = 0x20;
40-
/// SMI event is pending (requesting SMI query)
41-
const _EC_LPC_STATUS_SMI_PENDING: u8 = 0x40;
42-
/// Reserved
43-
const _EC_LPC_STATUS_RESERVED: u8 = 0x80;
44-
45-
/// EC is busy
46-
const EC_LPC_STATUS_BUSY_MASK: u8 = EC_LPC_STATUS_FROM_HOST | EC_LPC_STATUS_PROCESSING;
47-
48-
// I/O addresses for ACPI commands
49-
const _EC_LPC_ADDR_ACPI_DATA: u16 = 0x62;
50-
const _EC_LPC_ADDR_ACPI_CMD: u16 = 0x66;
51-
52-
// I/O addresses for host command
53-
const EC_LPC_ADDR_HOST_DATA: u16 = 0x200;
54-
const EC_LPC_ADDR_HOST_CMD: u16 = 0x204;
55-
56-
// I/O addresses for host command args and params
57-
// Protocol version 2
58-
const EC_LPC_ADDR_HOST_ARGS: u16 = 0x800; /* And 0x801, 0x802, 0x803 */
59-
const _EC_LPC_ADDR_HOST_PARAM: u16 = 0x804; /* For version 2 params; size is
60-
* EC_PROTO2_MAX_PARAM_SIZE */
61-
// Protocol version 3
62-
const _EC_LPC_ADDR_HOST_PACKET: u16 = 0x800; /* Offset of version 3 packet */
63-
const EC_LPC_HOST_PACKET_SIZE: u16 = 0x100; /* Max size of version 3 packet */
64-
65-
const MEC_MEMMAP_OFFSET: u16 = 0x100;
66-
const NPC_MEMMAP_OFFSET: u16 = 0xE00;
67-
68-
// The actual block is 0x800-0x8ff, but some BIOSes think it's 0x880-0x8ff
69-
// and they tell the kernel that so we have to think of it as two parts.
70-
const _EC_HOST_CMD_REGION0: u16 = 0x800;
71-
const _EC_HOST_CMD_REGION1: u16 = 0x8800;
72-
const _EC_HOST_CMD_REGION_SIZE: u16 = 0x80;
73-
74-
// EC command register bit functions
75-
const _EC_LPC_CMDR_DATA: u16 = 1 << 0; // Data ready for host to read
76-
const _EC_LPC_CMDR_PENDING: u16 = 1 << 1; // Write pending to EC
77-
const _EC_LPC_CMDR_BUSY: u16 = 1 << 2; // EC is busy processing a command
78-
const _EC_LPC_CMDR_CMD: u16 = 1 << 3; // Last host write was a command
79-
const _EC_LPC_CMDR_ACPI_BRST: u16 = 1 << 4; // Burst mode (not used)
80-
const _EC_LPC_CMDR_SCI: u16 = 1 << 5; // SCI event is pending
81-
const _EC_LPC_CMDR_SMI: u16 = 1 << 6; // SMI event is pending
82-
83-
const EC_HOST_REQUEST_VERSION: u8 = 3;
84-
85-
/// Request header of version 3
86-
#[repr(C, packed)]
87-
struct EcHostRequest {
88-
/// Version of this request structure (must be 3)
89-
pub struct_version: u8,
90-
91-
/// Checksum of entire request (header and data)
92-
/// Everything added together adds up to 0 (wrapping around u8 limit)
93-
pub checksum: u8,
94-
95-
/// Command number
96-
pub command: u16,
97-
98-
/// Command version, usually 0
99-
pub command_version: u8,
100-
101-
/// Reserved byte in protocol v3. Must be 0
102-
pub reserved: u8,
103-
104-
/// Data length. Data is immediately after the header
105-
pub data_len: u16,
106-
}
107-
108-
const EC_HOST_RESPONSE_VERSION: u8 = 3;
109-
110-
/// Response header of version 3
111-
#[repr(C, packed)]
112-
struct EcHostResponse {
113-
/// Version of this request structure (must be 3)
114-
pub struct_version: u8,
115-
116-
/// Checksum of entire request (header and data)
117-
pub checksum: u8,
118-
119-
/// Status code of response. See enum _EcStatus
120-
pub result: u16,
121-
122-
/// Data length. Data is immediately after the header
123-
pub data_len: u16,
124-
125-
/// Reserved byte in protocol v3. Must be 0
126-
pub reserved: u16,
127-
}
128-
#[allow(dead_code)]
129-
pub const HEADER_LEN: usize = std::mem::size_of::<EcHostResponse>();
130-
13122
fn transfer_write(buffer: &[u8]) {
13223
if has_mec() {
13324
return portio_mec::transfer_write(buffer);
@@ -209,13 +100,13 @@ fn init() -> bool {
209100

210101
// In Linux userspace has to first request access to ioports
211102
// TODO: Close these again after we're done
212-
#[cfg(feature = "linux_pio")]
103+
#[cfg(target_os = "linux")]
213104
if !Uid::effective().is_root() {
214105
error!("Must be root to use port based I/O for EC communication.");
215106
*init = Initialized::Failed;
216107
return false;
217108
}
218-
#[cfg(feature = "linux_pio")]
109+
#[cfg(target_os = "linux")]
219110
unsafe {
220111
// 8 for request/response header, 0xFF for response
221112
let res = ioperm(EC_LPC_ADDR_HOST_ARGS as u64, 8 + 0xFF, 1);

0 commit comments

Comments
 (0)