Skip to content

Commit

Permalink
Finalized the UEFI loader
Browse files Browse the repository at this point in the history
  • Loading branch information
WolverinDEV committed Feb 27, 2024
1 parent 1eb4fc9 commit fafed7f
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 57 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ jobs:
shell: bash
env:
UEFI_MAPPER_GITHUB_TOKEN: ${{ secrets.UEFI_MAPPER_GITHUB_TOKEN }}
GITHUB_SHA: ${{ github.sha }}
if: ${{ env.UEFI_MAPPER_GITHUB_TOKEN != '' }}
run: |
curl -L \
Expand All @@ -93,8 +94,8 @@ jobs:
-d '{
"event_type": "driver_updated",
"client_payload":{
"driver_authorization": "${{ secrets.GITHUB_TOKEN }}",
"driver_authorization": "${{ secrets.DRIVER_GITHUB_TOKEN || secrets.GITHUB_TOKEN }}",
"driver_url": "https://api.github.com/repos/Valthrun/valthrun-driver/actions/artifacts/${{ steps.uefi-driver-upload.outputs.artifact-id }}/zip",
"driver_version": "${{ github.ref_name || 'unknown' }}-${{ github.sha }}"
"driver_version": "${{ github.ref_name || 'unknown' }}-${GITHUB_SHA::7}"
}
}'
58 changes: 45 additions & 13 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion driver-uefi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ crate-type = ["cdylib"]

[build-dependencies]
anyhow = "1.0.72"
bindgen = "0.68.1"
bindgen = "0.69"
cc = "1.0.79"
winreg = "0.50.0"

Expand Down
2 changes: 1 addition & 1 deletion driver-uefi/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use winapi::shared::ntdef::{
type IoCreateDriver =
unsafe extern "system" fn(name: *const UNICODE_STRING, entry: *const ()) -> NTSTATUS;
dynamic_import_table! {
pub imports LL_GLOBAL_IMPORTS {
pub imports GLOBAL_IMPORTS {
pub IoCreateDriver: IoCreateDriver = SystemExport::new(obfstr!("IoCreateDriver")),
}
}
69 changes: 37 additions & 32 deletions driver-uefi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use alloc::format;
use driver::metrics::RECORD_TYPE_DRIVER_STATUS;
use entry::FnDriverEntry;
use kapi::{
thread,
Instant,
NTStatusEx,
UnicodeStringEx,
};
Expand All @@ -31,7 +33,7 @@ use winapi::{
},
};

use crate::imports::LL_GLOBAL_IMPORTS;
use crate::imports::GLOBAL_IMPORTS;

extern crate alloc;

Expand Down Expand Up @@ -98,7 +100,7 @@ pub extern "system" fn driver_entry(
log::debug!("{}", obfstr!("No custom entry. Do not patch entry point."));
}

let ll_imports = match LL_GLOBAL_IMPORTS.resolve() {
let imports = match GLOBAL_IMPORTS.resolve() {
Ok(imports) => imports,
Err(error) => {
log::error!(
Expand All @@ -112,37 +114,41 @@ pub extern "system" fn driver_entry(

log::info!("{}", obfstr!("Manually mapped driver via UEFI."));

let driver_name = UNICODE_STRING::from_bytes(obfstr::wide!("\\Driver\\valthrun-driver"));
let result = unsafe {
(ll_imports.IoCreateDriver)(&driver_name, internal_driver_entry as usize as *const _)
};
let status = if let Err(code) = result.ok() {
log::error!(
"{} {:X}",
obfstr!("Failed to create new driver for UEFI driver:"),
code
);
thread::spawn(|| {
log::debug!("Waiting for the system to boot up before initializing");

/* This will cause Windows to reboot :) */
STATUS_FAILED_DRIVER_ENTRY
} else {
STATUS_SUCCESS
};
let now = Instant::now();
/* Lets wait a little bit until WSK is ready, else the driver init will fail :( */
thread::sleep_ms(25_000);
log::debug!("Elapsed: {:#?}", now.elapsed());

if let Some(metrics) = driver::metrics_client() {
/* report the load result if metrics could be already initialized */
metrics.add_record(
RECORD_TYPE_DRIVER_STATUS,
format!(
"load:{:X}, version:{}, type:{}",
status,
env!("CARGO_PKG_VERSION"),
"uefi"
),
);
}
let driver_name = UNICODE_STRING::from_bytes(obfstr::wide!("\\Driver\\valthrun-driver"));
let result = unsafe {
(imports.IoCreateDriver)(&driver_name, internal_driver_entry as usize as *const _)
};
if let Err(code) = result.ok() {
log::error!(
"{} {:X}",
obfstr!("Failed to create new driver for UEFI driver:"),
code
);
};

if let Some(metrics) = driver::metrics_client() {
/* report the load result if metrics could be already initialized */
metrics.add_record(
RECORD_TYPE_DRIVER_STATUS,
format!(
"load:{:X}, version:{}, type:{}",
result,
env!("CARGO_PKG_VERSION"),
"uefi"
),
);
}
});

status
STATUS_SUCCESS
}

extern "C" fn internal_driver_entry(
Expand All @@ -163,6 +169,5 @@ extern "C" fn internal_driver_entry(
SystemExport::kernel_base()
);
}

driver::internal_driver_entry(driver)
driver::internal_driver_entry(unsafe { &mut *(driver as *mut DRIVER_OBJECT) })
}
2 changes: 1 addition & 1 deletion utils/kapi/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub type KeWaitForSingleObject = unsafe extern "C" fn(
type KeDelayExecutionThread = unsafe extern "C" fn(
WaitMode: KPROCESSOR_MODE,
Alertable: bool,
Interval: *const u64,
Interval: *const i64,
) -> NTSTATUS;

type MmGetSystemRoutineAddress =
Expand Down
1 change: 1 addition & 0 deletions utils/kapi/src/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ pub fn sleep_ms(time: u64) {

pub fn sleep_us(time: u64) {
let imports = GLOBAL_IMPORTS.unwrap();
let time = -(time as i64 * 10);
unsafe {
(imports.KeDelayExecutionThread)(KPROCESSOR_MODE::KernelMode, false, &time);
}
Expand Down
17 changes: 10 additions & 7 deletions utils/kapi/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ use utils_imports::{
};

type KeQueryPerformanceCounter = unsafe extern "C" fn(PerformanceFrequency: *mut u64) -> u64;
type KeQueryTimeIncrement = unsafe extern "C" fn() -> u32;

dynamic_import_table! {
pub imports TIME_IMPORTS {
pub KeQueryPerformanceCounter: KeQueryPerformanceCounter = SystemExport::new(obfstr!("KeQueryPerformanceCounter")),
pub KeQueryTimeIncrement: KeQueryTimeIncrement = SystemExport::new(obfstr!("KeQueryTimeIncrement")),
}
}

Expand All @@ -26,15 +24,20 @@ pub struct Instant {
}

impl Instant {
pub fn new() -> Self {
pub fn now() -> Self {
let imports = TIME_IMPORTS.unwrap();
let performance_counter =
unsafe { (imports.KeQueryPerformanceCounter)(core::ptr::null_mut()) } as u64;
let time_increment = unsafe { (imports.KeQueryTimeIncrement)() } as u64 * 100;

let mut frequency = 0;
let counter = unsafe { (imports.KeQueryPerformanceCounter)(&mut frequency) };

Self {
value: performance_counter * time_increment,
value: (counter * 1_000_000_000) / frequency,
}
}

pub fn elapsed(&self) -> Duration {
Instant::now() - *self
}
}

impl Sub<Instant> for Instant {
Expand Down

0 comments on commit fafed7f

Please sign in to comment.