Skip to content
Merged
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
865 changes: 629 additions & 236 deletions Cargo.lock

Large diffs are not rendered by default.

17 changes: 4 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "inputmodules"
version = "0.4.8"
edition = "2021"
rust-version = "1.61"
rust-version = "1.89"
build = "build.rs"

[features]
Expand All @@ -12,34 +12,25 @@ azalea = []
default = ["12in"]

[build-dependencies]
built = { version = "0.5", no-default-features = true, features = ["chrono", "git2"] }
built = { version = "0.8", default-features = false, features = ["chrono", "git2"] }

[dependencies]
cfg-if = "1.0.0"
embedded-graphics = "0.7.1"
getargs = { version = "0.5.0", default-features = false }
log = "0.4"
uefi = { version = "0.20", features = ["alloc"] }
uefi-services = "0.17"
uefi = { version = "0.41", features = ["alloc", "global_allocator", "logger", "panic_handler"] }
zune-core = "0.2.14"
micromath = "2.1.0"

# Temporarily override because we need pending PRs for
# for the Shell and ShellParameters protocols
[patch.crates-io]
uefi = { git = "https://github.com/FrameworkComputer/uefi-rs", branch = "merged" }
uefi-services = { git = "https://github.com/FrameworkComputer/uefi-rs", branch = "merged" }

[dependencies.zune-png]
version = "0.2.1"
default-features = false

[dependencies.framework_lib]
# path = "/home/zoid/clone/framework-system/framework_lib"
git = "https://github.com/FrameworkComputer/framework-system"
# rev = "32ab4f7a2b9fb55f9a53d4b60c17624029ba017b"
# Based on 3da46424ad9fac924bb83e9a2f87dda73c971752
branch = "adc-laptop12"
rev = "844d64061120676388cbd741f6ba78469cfcef3a"
features = ["uefi"]
default-features = false

Expand Down
2 changes: 1 addition & 1 deletion src/drawing_12in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use framework_lib::chromium_ec::commands::*;
use framework_lib::power::{AccelData, LidAngle, ReducedPowerInfo};
#[allow(unused_imports)]
use log::{debug, error, info, trace};
use uefi::boot::ScopedProtocol;
use uefi::proto::console::gop::GraphicsOutput;
use uefi::table::boot::ScopedProtocol;
use zune_core::result::DecodingResult;
use zune_png::PngDecoder;

Expand Down
2 changes: 1 addition & 1 deletion src/ec_comm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ pub fn get_als_data(ec: &Option<CrosEc>) -> u32 {
return 0;
};

get_als_reading(ec).unwrap_or(0)
get_als_reading(ec, 0).unwrap_or(0)
}

pub fn get_accel_reading(ec: &Option<CrosEc>) -> (AccelData, AccelData, LidAngle) {
Expand Down
30 changes: 12 additions & 18 deletions src/gui_12in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ use framework_lib::chromium_ec::input_deck::InputDeckState;
use framework_lib::chromium_ec::{CrosEc, EcRequestRaw};
#[allow(unused_imports)]
use log::{debug, error, info};
use uefi::boot::{self, OpenProtocolAttributes, OpenProtocolParams, SearchType};
use uefi::proto::console::gop::GraphicsOutput;
use uefi::proto::console::text::{Input, Key, ScanCode};
use uefi::table::boot::BootServices;
use uefi::table::boot::{OpenProtocolAttributes, OpenProtocolParams, SearchType};
use uefi::Identify;
use uefi::Result;
#[allow(unused_imports)]
use uefi::{prelude::*, Char16};
#[allow(unused_imports)]
use uefi_services::{print, println};
use uefi::{print, println};

use embedded_graphics::{
image::Image,
Expand Down Expand Up @@ -124,7 +123,7 @@ pub fn get_input_deck(
}
}

pub fn draw_background(bs: &BootServices, st: &SystemTable<Boot>, ec: &Option<CrosEc>) -> Result {
pub fn draw_background(ec: &Option<CrosEc>) -> Result {
let emulated_deck = InputDeckStatus12In {
chassis_closed: false,
pwrbtn_present: false,
Expand Down Expand Up @@ -160,7 +159,7 @@ pub fn draw_background(bs: &BootServices, st: &SystemTable<Boot>, ec: &Option<Cr
//};

let gop_handles = if let Ok(gop_handles) =
bs.locate_handle_buffer(SearchType::ByProtocol(&GraphicsOutput::GUID))
boot::locate_handle_buffer(SearchType::ByProtocol(&GraphicsOutput::GUID))
{
gop_handles
} else {
Expand All @@ -170,10 +169,10 @@ pub fn draw_background(bs: &BootServices, st: &SystemTable<Boot>, ec: &Option<Cr

let mut gop = if let Some(handle) = gop_handles.iter().next() {
unsafe {
bs.open_protocol::<GraphicsOutput>(
boot::open_protocol::<GraphicsOutput>(
OpenProtocolParams {
handle: *handle,
agent: bs.image_handle(),
agent: boot::image_handle(),
controller: None,
},
OpenProtocolAttributes::GetProtocol,
Expand Down Expand Up @@ -294,8 +293,8 @@ pub fn draw_background(bs: &BootServices, st: &SystemTable<Boot>, ec: &Option<Cr
// Draw the buffer to the screen.
buffer.blit(&mut gop)?;

let input_handle = bs.get_handle_for_protocol::<Input>().unwrap();
let mut input = bs.open_protocol_exclusive::<Input>(input_handle).unwrap();
let input_handle = boot::get_handle_for_protocol::<Input>().unwrap();
let mut input = boot::open_protocol_exclusive::<Input>(input_handle).unwrap();

let zero_key = Char16::try_from('0').unwrap();
let one_key = Char16::try_from('1').unwrap();
Expand Down Expand Up @@ -331,17 +330,12 @@ pub fn draw_background(bs: &BootServices, st: &SystemTable<Boot>, ec: &Option<Cr
}
//TODO: REmove this
println!("Shutting down;");
bs.stall(1_000_000);
boot::stall(core::time::Duration::from_micros(1_000_000));
// Shut down the system
let rt = st.runtime_services();
rt.reset(
uefi::table::runtime::ResetType::Shutdown,
Status::SUCCESS,
None,
);
uefi::runtime::reset(uefi::runtime::ResetType::SHUTDOWN, Status::SUCCESS, None);
}
// Wait a few seconds and if it's still pressed next time around, it was a long press
bs.stall(1_000_000);
boot::stall(core::time::Duration::from_micros(1_000_000));
long_press += 1;
} else {
long_press = 0;
Expand Down Expand Up @@ -402,7 +396,7 @@ pub fn draw_background(bs: &BootServices, st: &SystemTable<Boot>, ec: &Option<Cr
5,
);
buffer.blit(&mut gop)?;
bs.stall(2_000_000);
boot::stall(core::time::Duration::from_micros(2_000_000));

// Clear screen to black. Otherwise the next app is responsible for clearing.
// If the app was launched from the UEFI shell, the user would have to manually clear
Expand Down
65 changes: 37 additions & 28 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@

extern crate alloc;

use alloc::string::ToString;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use framework_lib::chromium_ec::commands::*;
use framework_lib::chromium_ec::input_deck::InputDeckState;
use framework_lib::chromium_ec::{print_err, CrosEc};
#[allow(unused_imports)]
use log::{debug, error, info, trace};
use uefi::boot::{self, OpenProtocolAttributes, OpenProtocolParams, SearchType};
use uefi::prelude::*;
use uefi::proto::shell_params::*;
use uefi::table::boot::{OpenProtocolAttributes, OpenProtocolParams, SearchType};
use uefi::proto::shell_params::ShellParameters;
use uefi::Identify;
#[allow(unused_imports)]
use uefi_services::{print, println};
use uefi::{print, println};

mod buffer;
#[cfg(not(feature = "12in"))]
Expand All @@ -40,10 +41,25 @@ mod built_info {
include!(concat!(env!("OUT_DIR"), "/built.rs"));
}

/// Since Rust 1.99 (LLVM 23), loops that search for a NUL u16, like in
/// `CStr16::from_ptr`, are turned into calls to wcslen.
/// There's no libc on UEFI and compiler_builtins doesn't provide it.
///
/// # Safety
/// `s` must point to a NUL-terminated UCS-2 string
#[no_mangle]
pub unsafe extern "C" fn wcslen(s: *const u16) -> usize {
let mut len = 0;
// Volatile read, so LLVM can't turn this loop into a call to itself
while core::ptr::read_volatile(s.add(len)) != 0 {
len += 1;
}
len
}

#[entry]
fn main(image_handle: Handle, mut system_table: SystemTable<Boot>) -> Status {
let event = uefi_services::init(&mut system_table).unwrap();
let boot_services = system_table.boot_services();
fn main() -> Status {
uefi::helpers::init().unwrap();

// Only error prints in release build
#[cfg(not(debug_assertions))]
Expand All @@ -52,7 +68,7 @@ fn main(image_handle: Handle, mut system_table: SystemTable<Boot>) -> Status {
//#[cfg(debug_assertions)]
//log::set_max_level(log::LevelFilter::Trace);

let arg = get_shell_action(boot_services);
let arg = get_shell_action();
if let Ok(ShellAction::Info) = arg {
println!("Provide Parameter: 'gui' to launch UI and show issues");
println!("Provide Parameter: 'force' to launch UI even if everything is ok");
Expand All @@ -64,7 +80,7 @@ fn main(image_handle: Handle, mut system_table: SystemTable<Boot>) -> Status {
print_12in_status();
} else if let Ok(ShellAction::GopInfo) = arg {
println!("GOP Info");
util::print_gop_info(boot_services);
util::print_gop_info();
} else {
info!("No shell arg, launching GUI");

Expand All @@ -76,29 +92,22 @@ fn main(image_handle: Handle, mut system_table: SystemTable<Boot>) -> Status {
ec_comm::set_uefi_app_mode(&ec, true);
let quick_boot = !matches!(arg, Ok(ShellAction::Force));
#[cfg(not(feature = "12in"))]
let gui_res = state_machine::start(boot_services, &system_table, &ec, quick_boot);
let gui_res = state_machine::start(&ec, quick_boot);
#[cfg(feature = "12in")]
let gui_res = state_machine_12in::start(boot_services, &system_table, &ec, quick_boot);
let gui_res = state_machine_12in::start(&ec, quick_boot);
ec_comm::set_uefi_app_mode(&ec, false);

let reset = gui_res.unwrap();
if let Some(reset_type) = reset {
#[cfg(debug_assertions)]
info!("Shutting down");
boot_services.stall(1_000_000);
boot::stall(core::time::Duration::from_micros(1_000_000));

// Shut down the system
// TODO: I should do this outside of this function
let rt = system_table.runtime_services();
rt.reset(reset_type, Status::SUCCESS, None);
uefi::runtime::reset(reset_type, Status::SUCCESS, None);
}
}

if let Some(event) = event {
// Will crash when Linux boots if we don't call this
boot_services.close_event(event).unwrap();
}

Status::SUCCESS
}

Expand All @@ -110,17 +119,17 @@ enum ShellAction {
GopInfo,
}

fn get_shell_action(boot_services: &BootServices) -> uefi::Result<ShellAction> {
fn get_shell_action() -> uefi::Result<ShellAction> {
// TODO: I think I should open this from the ImageHandle?
let shell_params_h =
boot_services.locate_handle_buffer(SearchType::ByProtocol(&ShellParameters::GUID))?;
trace!("Found {} ShellParameters handles", (*shell_params_h).len());
for handle in &*shell_params_h {
boot::locate_handle_buffer(SearchType::ByProtocol(&ShellParameters::GUID))?;
trace!("Found {} ShellParameters handles", shell_params_h.len());
for handle in shell_params_h.iter() {
let params_handle = unsafe {
boot_services.open_protocol::<ShellParameters>(
boot::open_protocol::<ShellParameters>(
OpenProtocolParams {
handle: *handle,
agent: boot_services.image_handle(),
agent: boot::image_handle(),
controller: None,
},
OpenProtocolAttributes::GetProtocol,
Expand All @@ -129,11 +138,11 @@ fn get_shell_action(boot_services: &BootServices) -> uefi::Result<ShellAction> {

// Ehm why are there two and one has no args?
// Maybe one is the shell itself?
if params_handle.argc == 0 {
if params_handle.args_len() == 0 {
continue;
}

let args = params_handle.get_args();
let args: Vec<String> = params_handle.args().map(|arg| arg.to_string()).collect();
// First arg is the executable path
return Ok(if args.len() == 2 && args[1] == "gui" {
ShellAction::Gui
Expand Down
16 changes: 7 additions & 9 deletions src/sleep.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
use core::ffi::c_void;
use core::ptr::NonNull;
use core::time::Duration;

#[allow(unused_imports)]
use uefi::prelude::*;
use uefi::table::boot::{BootServices, EventType, Tpl};
use uefi::boot::{self, EventType, TimerTrigger, Tpl};
use uefi::Event;

unsafe extern "efiapi" fn idle_callback(_event: Event, _context: Option<NonNull<c_void>>) {}

// Continue execution after a timeout or when the event fires
// This is much better than gBS->Stall because it'll actually pause the CPU instead of busywaiting.
pub fn sleep_or(bs: &BootServices, ms: u64, or_event: Option<Event>) {
pub fn sleep_or(ms: u64, or_event: Option<Event>) {
let oneshot_timer = unsafe {
bs.create_event(EventType::TIMER, Tpl::CALLBACK, Some(idle_callback), None)
.unwrap()
boot::create_event(EventType::TIMER, Tpl::CALLBACK, Some(idle_callback), None).unwrap()
};

bs.set_timer(
boot::set_timer(
&oneshot_timer,
uefi::table::boot::TimerTrigger::Relative(ms * 10_000),
TimerTrigger::Relative(Duration::from_millis(ms)),
)
.unwrap();

Expand All @@ -27,5 +25,5 @@ pub fn sleep_or(bs: &BootServices, ms: u64, or_event: Option<Event>) {
} else {
[unsafe { oneshot_timer.unsafe_clone() }, oneshot_timer]
};
let _which_event_fired = bs.wait_for_event(&mut events).discard_errdata().unwrap();
let _which_event_fired = boot::wait_for_event(&mut events).unwrap();
}
Loading