Skip to content

Commit 8f4d55c

Browse files
committed
Allow building library with uefi support and without
Using `uefi` feature. Going all the way to supporting `no_std` is more difficult and not needed for me right now. Tested: - Build and run Linux command - `cargo build -p framework_tool && sudo target/debug/framework_tool -t` - `cargo build -p framework_tool && sudo target/debug/framework_tool -v` - Build just the library - `cargo build -p framework_lib - Run library tests - `cargo test -p framework_lib - Run clippy - `cargo clippy -p framework_lib -p framework_tool` Signed-off-by: Daniel Schaefer <[email protected]>
1 parent 0d6eb82 commit 8f4d55c

File tree

15 files changed

+269
-38
lines changed

15 files changed

+269
-38
lines changed

.github/workflows/ci.yml

+4-8
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,11 @@ jobs:
1212
- name: Setup Rust toolchain
1313
run: rustup show
1414

15-
- name: Build without optional features
15+
- name: Build defaeult features
1616
uses: actions-rs/cargo@v1
1717
with:
1818
command: build
19-
20-
- name: Build with all features enabled
21-
uses: actions-rs/cargo@v1
22-
with:
23-
command: build
24-
args: --all-features
19+
args: -p framework_lib -p framework_tool
2520

2621
test:
2722
name: Test Suite
@@ -37,6 +32,7 @@ jobs:
3732
uses: actions-rs/cargo@v1
3833
with:
3934
command: test
35+
args: -p framework_lib
4036

4137
lints:
4238
name: Lints
@@ -57,4 +53,4 @@ jobs:
5753
uses: actions-rs/cargo@v1
5854
with:
5955
command: clippy
60-
args: -- -D warnings
56+
args: -p framework_lib -p framework_tool -- -D warnings

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
target/
2+
build/

Cargo.lock

+40-15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
members = [
44
# Linux and Windows tool to inspect and customize the system
55
"framework_tool",
6+
# UEFI tool to inspect and customize the system
7+
"framework_uefi",
68
# Catchall library that we'll probably want to split up further
79
"framework_lib",
810
]

framework_lib/Cargo.toml

+10-9
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ name = "framework_lib"
33
version = "0.1.0"
44
edition = "2021"
55

6-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7-
86
[features]
9-
default = []
7+
default = ["std", "linux_pio", "cros_ec_driver", "raw_pio" ]
8+
std = ["dep:regex", "dep:num", "dep:num-derive", "dep:num-traits", "dep:clap"]
9+
uefi = ["dep:redox_uefi_std"]
1010
# EC communication via Port I/O on Linux
1111
linux_pio = ["dep:libc"]
1212
# EC communication via raw Port I/O (e.g. UEFI or other ring 0 code)
@@ -15,18 +15,19 @@ raw_pio = []
1515
cros_ec_driver = ["dep:nix"]
1616

1717
[dependencies]
18-
regex = "1.6.0" # TODO: Can update to 1.7.0
18+
regex = { version = "1.6.0", optional = true } # TODO: Can update to 1.7.0
1919
# TODO: This doesn't work on:
2020
# - Windows
2121
# - Linux when SecureBoot (Lockdown) is enabled
2222
# Need to use the driver in those cases
23-
redox_hwio = "0.1.6"
23+
redox_hwio = { version = "0.1.5", default_features = false }
2424
libc = { version = "0.2.137", optional = true }
25-
clap = { version = "4.0", features = ["derive"] }
25+
clap = { version = "4.0", features = ["derive"], optional = true }
2626
nix = { version = "0.25.0", optional = true }
27-
num = "0.4"
28-
num-derive = "0.3"
29-
num-traits = "0.2"
27+
num = { version = "0.4", optional = true }
28+
num-derive = { version = "0.3", optional = true }
29+
num-traits = { version = "0.2", optional = true }
30+
redox_uefi_std = { version = "0.1.8", optional = true }
3031

3132
[dependencies.windows]
3233
optional = true

framework_lib/src/chromium_ec/mod.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
use crate::util;
22

3+
#[cfg(not(feature = "uefi"))]
34
use num_derive::FromPrimitive;
45
#[cfg(feature = "cros_ec_driver")]
56
mod cros_ec;
67
mod portio;
78
//mod windows;
89

10+
#[cfg(feature = "uefi")]
11+
use core::prelude::rust_2021::derive;
12+
913
/// Total size of EC memory mapped region
1014
const EC_MEMMAP_SIZE: u16 = 255;
1115

1216
/// Command to read data from EC memory map
17+
#[cfg(feature = "cros_ec_driver")]
1318
const EC_CMD_READ_MEMMAP: u16 = 0x0007;
1419

1520
/// Response codes returned by commands
16-
#[derive(FromPrimitive, Debug)]
17-
enum EcResponseStatus {
21+
#[cfg_attr(not(feature = "uefi"), derive(FromPrimitive))]
22+
#[derive(Debug)]
23+
pub enum EcResponseStatus {
1824
Success = 0,
1925
InvalidCommand = 1,
2026
Error = 2,

framework_lib/src/ec_binary.rs

+28
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ const CROS_EC_IMAGE_DATA_COOKIE2: u32 = 0xceaabbdd;
33
// Absolute offset of the version struct inside the entire EC binary
44
const EC_VERSION_OFFSET: usize = 0x1158;
55

6+
#[cfg(not(feature = "uefi"))]
7+
#[cfg(feature = "std")]
8+
use regex;
9+
10+
#[cfg(feature = "uefi")]
11+
use core::prelude::rust_2021::derive;
12+
613
#[derive(Clone, Copy, Debug)]
714
#[repr(C, packed)]
815
struct _ImageVersionData {
@@ -46,6 +53,27 @@ pub fn print_ec_version(ver: &ImageVersionData) {
4653
println!(" Size: {:>20} KB", ver.size / 1024);
4754
}
4855

56+
#[cfg(feature = "uefi")]
57+
fn parse_ec_version(data: &_ImageVersionData) -> Option<ImageVersionData> {
58+
let version = std::str::from_utf8(&data.version)
59+
.ok()?
60+
.trim_end_matches(char::from(0));
61+
62+
// TODO: regex crate does not support no_std
63+
64+
Some(ImageVersionData {
65+
version: version.to_string(),
66+
size: data.size,
67+
rollback_version: data.rollback_version,
68+
platform: "".to_string(),
69+
major: 0,
70+
minor: 0,
71+
patch: 0,
72+
commit: "".to_string(),
73+
})
74+
}
75+
76+
#[cfg(not(feature = "uefi"))]
4977
fn parse_ec_version(data: &_ImageVersionData) -> Option<ImageVersionData> {
5078
let version = std::str::from_utf8(&data.version)
5179
.ok()?

framework_lib/src/lib.rs

+13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
1+
#![cfg_attr(feature = "uefi", no_std)]
2+
#![feature(prelude_import)]
3+
4+
#[cfg(feature = "uefi")]
5+
#[macro_use]
6+
extern crate uefi_std as std;
7+
8+
#[cfg(feature = "uefi")]
9+
#[allow(unused_imports)]
10+
#[prelude_import]
11+
use std::prelude::*;
12+
113
pub mod chromium_ec;
14+
#[cfg(not(feature = "uefi"))]
215
pub mod commandline;
316
pub mod ec_binary;
417
mod os_specific;

framework_lib/src/os_specific.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1+
#[cfg(not(feature = "uefi"))]
12
use std::{thread, time};
23

34
//#[cfg(target_os = "linux")]
45
//#[cfg(target_os = "windows")]
6+
#[cfg(not(feature = "uefi"))]
57
pub fn sleep(micros: u64) {
68
let ten_millis = time::Duration::from_micros(micros);
79
thread::sleep(ten_millis);
810
// TODO: If UEFI
911
//let uefi = std::system_table();
1012
//let _ = (uefi.BootServices.Stall)(1000);
1113
}
14+
15+
#[cfg(feature = "uefi")]
16+
pub fn sleep(_micros: u64) {
17+
// TODO: Implement something for UEFI
18+
let _foo: Vec<u8> = vec![];
19+
}

framework_tool/Cargo.toml

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ name = "framework_tool"
33
version = "0.1.0"
44
edition = "2021"
55

6-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7-
8-
[dependencies]
9-
framework_lib = { path = "../framework_lib" }
6+
[dependencies.framework_lib]
7+
path = "../framework_lib"
8+
features = ["std", "linux_pio", "raw_pio", "cros_ec_driver"]

framework_tool/Makefile

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
TARGET?=x86_64-unknown-uefi
2+
BUILD=build/$(TARGET)
3+
4+
SRC_DIR=.
5+
6+
7+
$(BUILD)/boot.efi: $(SRC_DIR)/Cargo.toml $(SRC_DIR)/src/* #$(SRC_DIR)/src/*/*
8+
mkdir -p $(BUILD)
9+
cargo rustc \
10+
-Z build-std=core,alloc \
11+
-Z build-std-features=compiler-builtins-mem \
12+
--target $(TARGET) \
13+
--bin uefi \
14+
--release \
15+
-- \
16+
-C soft-float \
17+
--emit link=$@
18+

framework_tool/src/uefi_main.rs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#![no_std]
2+
#![no_main]
3+
#![feature(prelude_import)]
4+
#![feature(try_trait_v2)]
5+
#![feature(control_flow_enum)]
6+
#![allow(clippy::collapsible_if)]
7+
#![allow(clippy::many_single_char_names)]
8+
#![allow(clippy::missing_safety_doc)]
9+
#![allow(dead_code)]
10+
#![allow(unused_variables)]
11+
12+
extern crate alloc;
13+
#[macro_use]
14+
extern crate uefi_std as std;
15+
16+
#[allow(unused_imports)]
17+
#[prelude_import]
18+
use std::prelude::*;
19+
20+
use core::ops::{ControlFlow, Try};
21+
use core::ptr;
22+
use std::uefi;
23+
use std::uefi::reset::ResetType;
24+
use std::uefi::status::{Result, Status};
25+
26+
#[no_mangle]
27+
pub extern "C" fn main() -> Status {
28+
let uefi = std::system_table();
29+
30+
//cli_flags();
31+
32+
// If I don't return 1, we crash(?). Or I think it tries other boot options and they fail.
33+
// But if I return 1, then we land in UEFI shell and we can run the command manually.
34+
Status(1)
35+
}

framework_uefi/Cargo.toml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[package]
2+
name = "framework_uefi"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[[bin]]
7+
name = "uefi"
8+
path = "src/main.rs"
9+
10+
[profile.release]
11+
lto = true
12+
13+
[dependencies]
14+
redox_uefi_std = "0.1.8"
15+
16+
[dependencies.framework_lib]
17+
path = "../framework_lib"
18+
features = ["uefi"]
19+
default-features = false

0 commit comments

Comments
 (0)